Skip to content

Commit e441598

Browse files
committed
bug fix - wrong function signature for IMD.cumsum and IMD.cumprod
1 parent db9d2b1 commit e441598

File tree

2 files changed

+73
-39
lines changed

2 files changed

+73
-39
lines changed

src/stat/stat.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function cumsum(x::AbstractArray{Union{Missing, T},1}; missings = :ignore) where
7676
throw(ArgumentError("`missings` must be either `:ignore` or `:skip`"))
7777
end
7878
end
79-
cumsum(x) = Base.cumsum(x)
79+
cumsum(x; missings = :ignore) = Base.cumsum(x)
8080
function cumsum!(outx::AbstractVector, x::AbstractArray{Union{Missing, T},1}; missings = :ignore) where T <: Union{INTEGERS, FLOATS}
8181
if missings == :ignore
8282
stat_cumsum!_ignore(outx, x)
@@ -86,7 +86,7 @@ function cumsum!(outx::AbstractVector, x::AbstractArray{Union{Missing, T},1}; mi
8686
throw(ArgumentError("`missings` must be either `:ignore` or `:skip`"))
8787
end
8888
end
89-
cumsum!(x,y) = Base.cumsum!(x,y)
89+
cumsum!(x,y; missings = :ignore) = Base.cumsum!(x,y)
9090
function cumprod(x::AbstractArray{Union{Missing, T},1}; missings = :ignore) where T <: Union{INTEGERS, FLOATS}
9191
if missings == :ignore
9292
stat_cumprod_ignore(x)
@@ -96,7 +96,7 @@ function cumprod(x::AbstractArray{Union{Missing, T},1}; missings = :ignore) wher
9696
throw(ArgumentError("`missings` must be either `:ignore` or `:skip`"))
9797
end
9898
end
99-
cumprod(x)=Base.cumprod(x)
99+
cumprod(x; missings = :ignore)=Base.cumprod(x)
100100
function cumprod!(outx::AbstractVector, x::AbstractArray{Union{Missing, T},1}; missings = :ignore) where T <: Union{INTEGERS, FLOATS}
101101
if missings == :ignore
102102
stat_cumprod!_ignore(outx, x)
@@ -106,7 +106,7 @@ function cumprod!(outx::AbstractVector, x::AbstractArray{Union{Missing, T},1}; m
106106
throw(ArgumentError("`missings` must be either `:ignore` or `:skip`"))
107107
end
108108
end
109-
cumprod!(x,y) = Base.cumprod!(x,y)
109+
cumprod!(x, y; missings=:ignore) = Base.cumprod!(x,y)
110110
function cummin(x::AbstractArray{<:Union{Missing, T},1}; missings = :ignore) where T <: Union{INTEGERS, FLOATS, TimeType}
111111
if missings == :ignore
112112
stat_cummin_ignore(x)

test/stats.jl

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Random,PooledArrays,CategoricalArrays
1+
using Random, PooledArrays, CategoricalArrays
22
@testset "topk" begin
33
# general usage
44
for i in 1:100
@@ -32,7 +32,7 @@ using Random,PooledArrays,CategoricalArrays
3232
@test abs.(partialsort(x, 1:j, by=abs)) == abs.(topk(x, j, rev=true, by=abs)) == abs.(topk(x, j, rev=true, by=abs, threads=true))
3333
@test abs.(partialsort(x, 1:j, rev=true, by=abs)) == abs.(topk(x, j, by=abs)) == abs.(topk(x, j, by=abs, threads=true))
3434
@test partialsortperm(x, 1:j, by=abs) == topkperm(x, j, rev=true, by=abs) == topkperm(x, j, rev=true, by=abs, threads=true)
35-
@test partialsortperm(x, 1:j, rev=true, by=abs) == topkperm(x, j, by=abs) == topkperm(x, j, by=abs, threads = true)
35+
@test partialsortperm(x, 1:j, rev=true, by=abs) == topkperm(x, j, by=abs) == topkperm(x, j, by=abs, threads=true)
3636
end
3737
x = zeros(Bool, 11)
3838
for j in 1:15
@@ -53,41 +53,41 @@ using Random,PooledArrays,CategoricalArrays
5353
@test partialsort(x, 1:j) == topk(x, j, rev=true) == topk(x, j, rev=true, threads=true)
5454
@test partialsort(x, 1:j, rev=true) == topk(x, j) == topk(x, j, threads=true)
5555
@test partialsortperm(x, 1:j) == topkperm(x, j, rev=true) == topkperm(x, j, rev=true, threads=true)
56-
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j) == topkperm(x, j, threads = true)
56+
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j) == topkperm(x, j, threads=true)
5757
end
5858
x = PooledArray(rand(1:100, 100))
5959
for j in 1:50
6060
@test partialsort(x, 1:j) == topk(x, j, rev=true) == topk(x, j, rev=true, threads=true)
6161
@test partialsort(x, 1:j, rev=true) == topk(x, j) == topk(x, j, threads=true)
6262
@test partialsortperm(x, 1:j) == topkperm(x, j, rev=true) == topkperm(x, j, rev=true, threads=true)
63-
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j) == topkperm(x, j, threads = true)
63+
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j) == topkperm(x, j, threads=true)
6464
end
6565
x = CategoricalArray(rand(100))
6666
for j in 1:50
67-
@test partialsort(x, 1:j) == topk(x, j, rev=true, lt = isless)
68-
@test partialsort(x, 1:j, rev=true) == topk(x, j, lt = isless)
69-
@test partialsortperm(x, 1:j) == topkperm(x, j, rev=true, lt = isless)
70-
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j, lt = isless)
67+
@test partialsort(x, 1:j) == topk(x, j, rev=true, lt=isless)
68+
@test partialsort(x, 1:j, rev=true) == topk(x, j, lt=isless)
69+
@test partialsortperm(x, 1:j) == topkperm(x, j, rev=true, lt=isless)
70+
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j, lt=isless)
7171
end
7272

7373
end
7474
x = [1, 10, missing, 100, -1000, 32, 54, 0, missing, missing, -1]
75-
@test topk(x, 2) == [100, 54] == topk(x, 2, threads = true)
76-
@test topk(x, 2, rev=true) == [-1000, -1] == topk(x, 2, rev=true, threads = true)
77-
@test topkperm(x, 2) == [4, 7] == topkperm(x, 2, threads = true)
78-
@test topkperm(x, 2, rev=true) == [5, 11] == topkperm(x, 2, rev=true, threads = true)
79-
@test topk(x, 10) == [100, 54, 32, 10, 1, 0, -1, -1000] == topk(x, 10, threads = true)
80-
@test topk(x, 10, rev=true) == [-1000, -1, 0, 1, 10, 32, 54, 100] == topk(x, 10, rev=true, threads = true)
81-
@test topkperm(x, 10) == [4, 7, 6, 2, 1, 8, 11, 5] == topkperm(x, 10, threads = true)
82-
@test topkperm(x, 10, rev=true) == [5, 11, 8, 1, 2, 6, 7, 4] == topkperm(x, 10, rev=true, threads = true)
75+
@test topk(x, 2) == [100, 54] == topk(x, 2, threads=true)
76+
@test topk(x, 2, rev=true) == [-1000, -1] == topk(x, 2, rev=true, threads=true)
77+
@test topkperm(x, 2) == [4, 7] == topkperm(x, 2, threads=true)
78+
@test topkperm(x, 2, rev=true) == [5, 11] == topkperm(x, 2, rev=true, threads=true)
79+
@test topk(x, 10) == [100, 54, 32, 10, 1, 0, -1, -1000] == topk(x, 10, threads=true)
80+
@test topk(x, 10, rev=true) == [-1000, -1, 0, 1, 10, 32, 54, 100] == topk(x, 10, rev=true, threads=true)
81+
@test topkperm(x, 10) == [4, 7, 6, 2, 1, 8, 11, 5] == topkperm(x, 10, threads=true)
82+
@test topkperm(x, 10, rev=true) == [5, 11, 8, 1, 2, 6, 7, 4] == topkperm(x, 10, rev=true, threads=true)
8383
@test isequal(topk([missing, missing], 2), [missing])
8484
@test isequal(topk([missing, missing], 2, rev=true), [missing])
8585
@test isequal(topkperm([missing, missing], 2), [missing])
8686
@test isequal(topkperm([missing, missing], 2, rev=true), [missing])
87-
@test topk(x, 2, by=abs) == [-1000, 100] == topk(x, 2, by=abs, threads = true)
88-
@test topk(x, 2, by=abs, rev=true) == [0, 1] == topk(x, 2, by=abs, rev=true, threads = true)
89-
@test topkperm(x, 2, by=abs) == [5, 4] == topkperm(x, 2, by=abs, threads = true)
90-
@test topkperm(x, 2, by=abs, rev=true) == [8, 1] == topkperm(x, 2, by=abs, rev=true, threads = true)
87+
@test topk(x, 2, by=abs) == [-1000, 100] == topk(x, 2, by=abs, threads=true)
88+
@test topk(x, 2, by=abs, rev=true) == [0, 1] == topk(x, 2, by=abs, rev=true, threads=true)
89+
@test topkperm(x, 2, by=abs) == [5, 4] == topkperm(x, 2, by=abs, threads=true)
90+
@test topkperm(x, 2, by=abs, rev=true) == [8, 1] == topkperm(x, 2, by=abs, rev=true, threads=true)
9191

9292
x = Int8[-128, -128, -128]
9393
y = Union{Int8,Missing}[-128, -128, missing, missing, -128]
@@ -106,27 +106,61 @@ using Random,PooledArrays,CategoricalArrays
106106
x = [-1, 1, 1, missing, 1, 1, missing, -100]
107107
@test topk(x, 3, by=ff678) == [-100, -1]
108108
@test topkperm(x, 3, by=ff678) == [8, 1]
109-
@test topk(x, 3, by=ff678, rev=true) == [-1,-100]
110-
@test topkperm(x, 3, by=ff678, rev=true) == [1,8]
109+
@test topk(x, 3, by=ff678, rev=true) == [-1, -100]
110+
@test topkperm(x, 3, by=ff678, rev=true) == [1, 8]
111111

112-
x=[missing for _ in 1:1000]
113-
@test isequal(topk(x, 10), topk(x,10,threads=true))
112+
x = [missing for _ in 1:1000]
113+
@test isequal(topk(x, 10), topk(x, 10, threads=true))
114114
@test isequal(topk(x, 10), [missing])
115-
@test isequal(topk(x, 100), topk(x,100,threads=true))
115+
@test isequal(topk(x, 100), topk(x, 100, threads=true))
116116
@test isequal(topk(x, 100), [missing])
117-
@test isequal(topkperm(x, 100), topkperm(x,100,threads=true))
117+
@test isequal(topkperm(x, 100), topkperm(x, 100, threads=true))
118118
@test isequal(topkperm(x, 100), [missing])
119-
@test isequal(topkperm(x, 10), topkperm(x,10,threads=true))
119+
@test isequal(topkperm(x, 10), topkperm(x, 10, threads=true))
120120
@test isequal(topkperm(x, 10), [missing])
121-
@test isequal(topkperm(x, 10,rev=true), topkperm(x,10,threads=true,rev=true))
122-
@test isequal(topkperm(x, 10,rev=true), [missing])
121+
@test isequal(topkperm(x, 10, rev=true), topkperm(x, 10, threads=true, rev=true))
122+
@test isequal(topkperm(x, 10, rev=true), [missing])
123123

124-
x=CategoricalArray(rand(1000))
124+
x = CategoricalArray(rand(1000))
125125
# TODO categorical array is not thread safe - fortunately, it throws Errors - however, in future we may need to fix it
126-
@test_throws UndefRefError topk(x,10,lt=isless,threads=true)
126+
@test_throws UndefRefError topk(x, 10, lt=isless, threads=true)
127+
128+
@test isequal(topk([NaN, NaN, NaN, 3], 2, rev=true), [3.0, NaN])
129+
@test isequal(topk([NaN, NaN, NaN, 3], 2, rev=false), [NaN, NaN])
130+
@test isequal(topk([missing, NaN, NaN, NaN, 3, missing], 2, rev=true), [3.0, NaN])
131+
@test isequal(topk([NaN, missing, missing, missing, NaN, NaN, 3, missing], 2, rev=false), [NaN, NaN])
132+
end
133+
@testset "cum*" begin
134+
x1 = [1,-1,2,-2]
135+
x2 = [1, -1, missing, -2]
136+
x3 = [missing, 3, -9, 2]
137+
x4 = [missing, missing, missing, 2]
138+
x5 = [missing, missing, -9.0, 2.0]
139+
x6 = [missing, missing, missing, missing]
140+
141+
@test isequal(IMD.cumsum(x1, missings = :ignore), [1,0,2,0])
142+
@test isequal(IMD.cumsum(x2, missings = :ignore), [1,0,0,-2])
143+
@test isequal(IMD.cumsum(x3, missings = :ignore), [missing,3,-6,-4])
144+
@test isequal(IMD.cumsum(x4, missings = :ignore), [missing,missing,missing,2])
145+
@test isequal(IMD.cumsum(x5, missings = :ignore), [missing,missing,-9.0,-7.0])
146+
@test isequal(IMD.cumsum(x6, missings = :ignore), [missing,missing, missing, missing])
147+
@test isequal(IMD.cumsum(x1, missings = :skip), [1,0,2,0])
148+
@test isequal(IMD.cumsum(x2, missings = :skip), [1,0,missing,-2])
149+
@test isequal(IMD.cumsum(x3, missings = :skip), [missing,3,-6,-4])
150+
@test isequal(IMD.cumsum(x4, missings = :skip), [missing,missing,missing,2])
151+
@test isequal(IMD.cumsum(x5, missings = :skip), [missing,missing,-9.0,-7.0])
152+
@test isequal(IMD.cumsum(x6, missings = :skip), [missing,missing, missing, missing])
127153

128-
@test isequal(topk([NaN,NaN,NaN,3],2,rev=true), [3.0, NaN])
129-
@test isequal(topk([NaN,NaN,NaN,3],2,rev=false), [NaN, NaN])
130-
@test isequal(topk([missing, NaN,NaN,NaN,3, missing],2,rev=true), [3.0, NaN])
131-
@test isequal(topk([NaN,missing, missing, missing,NaN,NaN,3, missing],2,rev=false), [NaN, NaN])
154+
@test isequal(IMD.cumprod(x1, missings = :ignore), [1,-1,-2,4])
155+
@test isequal(IMD.cumprod(x2, missings = :ignore), [1,-1,-1,2])
156+
@test isequal(IMD.cumprod(x3, missings = :ignore), [missing,3,-27,-54])
157+
@test isequal(IMD.cumprod(x4, missings = :ignore), [missing,missing,missing,2])
158+
@test isequal(IMD.cumprod(x5, missings = :ignore), [missing,missing,-9.0,-18.0])
159+
@test isequal(IMD.cumprod(x6, missings = :ignore), [missing,missing, missing, missing])
160+
@test isequal(IMD.cumprod(x1, missings = :skip), [1,-1,-2,4])
161+
@test isequal(IMD.cumprod(x2, missings = :skip), [1,-1,missing,2])
162+
@test isequal(IMD.cumprod(x3, missings = :skip), [missing,3,-27,-54])
163+
@test isequal(IMD.cumprod(x4, missings = :skip), [missing,missing,missing,2])
164+
@test isequal(IMD.cumprod(x5, missings = :skip), [missing,missing,-9.0,-18.0])
165+
@test isequal(IMD.cumprod(x6, missings = :skip), [missing,missing, missing, missing])
132166
end

0 commit comments

Comments
 (0)