-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathstats.jl
219 lines (199 loc) · 11.1 KB
/
stats.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using Random, PooledArrays, CategoricalArrays
@testset "topk" begin
# general usage
for i in 1:100
x = rand(Int, 11)
for j in 1:11
@test partialsort(x, 1:j) == topk(x, j, rev=true)
@test partialsort(x, 1:j, rev=true) == topk(x, j)
@test partialsortperm(x, 1:j) == topkperm(x, j, rev=true)
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j)
end
x = rand(11)
for j in 1:11
@test partialsort(x, 1:j) == topk(x, j, rev=true)
@test partialsort(x, 1:j, rev=true) == topk(x, j)
@test partialsortperm(x, 1:j) == topkperm(x, j, rev=true)
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j)
end
x = randn(11)
for j in 1:11
@test partialsort(x, 1:j) == topk(x, j, rev=true)
@test partialsort(x, 1:j, rev=true) == topk(x, j)
@test partialsortperm(x, 1:j) == topkperm(x, j, rev=true)
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j)
end
x = rand(Int8, 10000)
for j in 1:30
@test partialsort(x, 1:j) == topk(x, j, rev=true) == topk(x, j, rev=true, threads=true)
@test partialsort(x, 1:j, rev=true) == topk(x, j) == topk(x, j, threads=true)
@test partialsortperm(x, 1:j) == topkperm(x, j, rev=true) == topkperm(x, j, rev=true, threads=true)
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j) == topkperm(x, j, threads=true)
@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))
@test abs.(partialsort(x, 1:j, rev=true, by=abs)) == abs.(topk(x, j, by=abs)) == abs.(topk(x, j, by=abs, threads=true))
@test partialsortperm(x, 1:j, by=abs) == topkperm(x, j, rev=true, by=abs) == topkperm(x, j, rev=true, by=abs, threads=true)
@test partialsortperm(x, 1:j, rev=true, by=abs) == topkperm(x, j, by=abs) == topkperm(x, j, by=abs, threads=true)
end
x = zeros(Bool, 11)
for j in 1:15
@test partialsort(x, 1:min(11, j)) == topk(x, j, rev=true)
@test partialsort(x, 1:min(j, 11), rev=true) == topk(x, j)
@test partialsortperm(x, 1:min(11, j)) == topkperm(x, j, rev=true)
@test partialsortperm(x, 1:min(11, j), rev=true) == topkperm(x, j)
end
x = ones(Bool, 11)
for j in 1:15
@test partialsort(x, 1:min(11, j)) == topk(x, j, rev=true)
@test partialsort(x, 1:min(j, 11), rev=true) == topk(x, j)
@test partialsortperm(x, 1:min(11, j)) == topkperm(x, j, rev=true)
@test partialsortperm(x, 1:min(11, j), rev=true) == topkperm(x, j)
end
x = [randstring() for _ in 1:101]
for j in 1:30
@test partialsort(x, 1:j) == topk(x, j, rev=true) == topk(x, j, rev=true, threads=true)
@test partialsort(x, 1:j, rev=true) == topk(x, j) == topk(x, j, threads=true)
@test partialsortperm(x, 1:j) == topkperm(x, j, rev=true) == topkperm(x, j, rev=true, threads=true)
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j) == topkperm(x, j, threads=true)
end
x = PooledArray(rand(1:100, 100))
for j in 1:50
@test partialsort(x, 1:j) == topk(x, j, rev=true) == topk(x, j, rev=true, threads=true)
@test partialsort(x, 1:j, rev=true) == topk(x, j) == topk(x, j, threads=true)
@test partialsortperm(x, 1:j) == topkperm(x, j, rev=true) == topkperm(x, j, rev=true, threads=true)
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j) == topkperm(x, j, threads=true)
end
x = CategoricalArray(rand(100))
for j in 1:50
@test partialsort(x, 1:j) == topk(x, j, rev=true, lt=isless)
@test partialsort(x, 1:j, rev=true) == topk(x, j, lt=isless)
@test partialsortperm(x, 1:j) == topkperm(x, j, rev=true, lt=isless)
@test partialsortperm(x, 1:j, rev=true) == topkperm(x, j, lt=isless)
end
end
x = [1, 10, missing, 100, -1000, 32, 54, 0, missing, missing, -1]
@test topk(x, 2) == [100, 54] == topk(x, 2, threads=true)
@test topk(x, 2, rev=true) == [-1000, -1] == topk(x, 2, rev=true, threads=true)
@test topkperm(x, 2) == [4, 7] == topkperm(x, 2, threads=true)
@test topkperm(x, 2, rev=true) == [5, 11] == topkperm(x, 2, rev=true, threads=true)
@test topk(x, 10) == [100, 54, 32, 10, 1, 0, -1, -1000] == topk(x, 10, threads=true)
@test topk(x, 10, rev=true) == [-1000, -1, 0, 1, 10, 32, 54, 100] == topk(x, 10, rev=true, threads=true)
@test topkperm(x, 10) == [4, 7, 6, 2, 1, 8, 11, 5] == topkperm(x, 10, threads=true)
@test topkperm(x, 10, rev=true) == [5, 11, 8, 1, 2, 6, 7, 4] == topkperm(x, 10, rev=true, threads=true)
@test isequal(topk([missing, missing], 2), [missing])
@test isequal(topk([missing, missing], 2, rev=true), [missing])
@test isequal(topkperm([missing, missing], 2), [missing])
@test isequal(topkperm([missing, missing], 2, rev=true), [missing])
@test topk(x, 2, by=abs) == [-1000, 100] == topk(x, 2, by=abs, threads=true)
@test topk(x, 2, by=abs, rev=true) == [0, 1] == topk(x, 2, by=abs, rev=true, threads=true)
@test topkperm(x, 2, by=abs) == [5, 4] == topkperm(x, 2, by=abs, threads=true)
@test topkperm(x, 2, by=abs, rev=true) == [8, 1] == topkperm(x, 2, by=abs, rev=true, threads=true)
x = Int8[-128, -128, -128]
y = Union{Int8,Missing}[-128, -128, missing, missing, -128]
@test topk(x, 2) == [-128, -128]
@test topk(x, 2, rev=true) == [-128, -128]
@test topkperm(x, 2, rev=true) == [1, 2]
@test topkperm(x, 2) == [1, 2]
@test topk(y, 3) == [-128, -128, -128]
@test topk(y, 3, rev=true) == [-128, -128, -128]
@test topkperm(y, 3, rev=true) == [1, 2, 5]
@test topkperm(y, 3) == [1, 2, 5]
ff678(x) = isequal(x, 1) ? missing : abs(x)
x = [-1, 1, 1, missing, 1, 1, missing, -100]
@test topk(x, 3, by=ff678) == [-100, -1]
@test topkperm(x, 3, by=ff678) == [8, 1]
@test topk(x, 3, by=ff678, rev=true) == [-1, -100]
@test topkperm(x, 3, by=ff678, rev=true) == [1, 8]
x = [missing for _ in 1:1000]
@test isequal(topk(x, 10), topk(x, 10, threads=true))
@test isequal(topk(x, 10), [missing])
@test isequal(topk(x, 100), topk(x, 100, threads=true))
@test isequal(topk(x, 100), [missing])
@test isequal(topkperm(x, 100), topkperm(x, 100, threads=true))
@test isequal(topkperm(x, 100), [missing])
@test isequal(topkperm(x, 10), topkperm(x, 10, threads=true))
@test isequal(topkperm(x, 10), [missing])
@test isequal(topkperm(x, 10, rev=true), topkperm(x, 10, threads=true, rev=true))
@test isequal(topkperm(x, 10, rev=true), [missing])
x = CategoricalArray(rand(1000))
# TODO categorical array is not thread safe - fortunately, it throws Errors - however, in future we may need to fix it
@test_throws UndefRefError topk(x, 10, lt=isless, threads=true)
@test isequal(topk([NaN, NaN, NaN, 3], 2, rev=true), [3.0, NaN])
@test isequal(topk([NaN, NaN, NaN, 3], 2, rev=false), [NaN, NaN])
@test isequal(topk([missing, NaN, NaN, NaN, 3, missing], 2, rev=true), [3.0, NaN])
@test isequal(topk([NaN, missing, missing, missing, NaN, NaN, 3, missing], 2, rev=false), [NaN, NaN])
end
@testset "cum*" begin
x1 = [1,-1,2,-2]
x2 = [1, -1, missing, -2]
x3 = [missing, 3, -9, 2]
x4 = [missing, missing, missing, 2]
x5 = [missing, missing, -9.0, 2.0]
x6 = [missing, missing, missing, missing]
@test isequal(IMD.cumsum(x1, missings = :ignore), [1,0,2,0])
@test isequal(IMD.cumsum(x2, missings = :ignore), [1,0,0,-2])
@test isequal(IMD.cumsum(x3, missings = :ignore), [missing,3,-6,-4])
@test isequal(IMD.cumsum(x4, missings = :ignore), [missing,missing,missing,2])
@test isequal(IMD.cumsum(x5, missings = :ignore), [missing,missing,-9.0,-7.0])
@test isequal(IMD.cumsum(x6, missings = :ignore), [missing,missing, missing, missing])
@test isequal(IMD.cumsum(x1, missings = :skip), [1,0,2,0])
@test isequal(IMD.cumsum(x2, missings = :skip), [1,0,missing,-2])
@test isequal(IMD.cumsum(x3, missings = :skip), [missing,3,-6,-4])
@test isequal(IMD.cumsum(x4, missings = :skip), [missing,missing,missing,2])
@test isequal(IMD.cumsum(x5, missings = :skip), [missing,missing,-9.0,-7.0])
@test isequal(IMD.cumsum(x6, missings = :skip), [missing,missing, missing, missing])
@test isequal(IMD.cumprod(x1, missings = :ignore), [1,-1,-2,4])
@test isequal(IMD.cumprod(x2, missings = :ignore), [1,-1,-1,2])
@test isequal(IMD.cumprod(x3, missings = :ignore), [missing,3,-27,-54])
@test isequal(IMD.cumprod(x4, missings = :ignore), [missing,missing,missing,2])
@test isequal(IMD.cumprod(x5, missings = :ignore), [missing,missing,-9.0,-18.0])
@test isequal(IMD.cumprod(x6, missings = :ignore), [missing,missing, missing, missing])
@test isequal(IMD.cumprod(x1, missings = :skip), [1,-1,-2,4])
@test isequal(IMD.cumprod(x2, missings = :skip), [1,-1,missing,2])
@test isequal(IMD.cumprod(x3, missings = :skip), [missing,3,-27,-54])
@test isequal(IMD.cumprod(x4, missings = :skip), [missing,missing,missing,2])
@test isequal(IMD.cumprod(x5, missings = :skip), [missing,missing,-9.0,-18.0])
@test isequal(IMD.cumprod(x6, missings = :skip), [missing,missing, missing, missing])
end
@testset "IMD.sum & IMD.mean & IMD.var" begin
x = Union{Missing, Int32}[missing, missing, missing, missing]
@test isequal(IMD.sum(x), missing)
@test IMD.sum(y->ismissing(y) ? 1 : y, x) == 4
push!(x, 1)
@test IMD.sum(x) == 1
@test IMD.sum(y->ismissing(y) ? 1 : y, x) == 5
@test IMD.mean(x) == 1
@test ismissing(IMD.mean(y->isequal(y,1) ? missing : y, x) )
@test IMD.mean(y->ismissing(y) ? 1 : y, x) == 1
@test isequal(IMD.var(x),missing)
@test isequal(IMD.var(x, false), 0.0)
@test isequal(IMD.var(y->ismissing(y) ? 1 : y, x), 0.0)
@test isequal(IMD.var(y->ismissing(y) ? 1 : y, x, false), 0.0)
x = [true, false, true, missing]
@test IMD.sum(x) == 2
@test IMD.sum(y->isequal(y, true) ? 100 : y, x) == 200
for i in 1:10
x=rand(1:10000, 100)
@test IMD.sum(x) == sum(x)
x = allowmissing(x)
x[50] = missing
@test IMD.sum(y->ismissing(y) ? 0 : y, x) == sum(y->ismissing(y) ? 0 : y, x)
end
if VERSION > v"1.8" # it causes problem in v"1.6", however, we can ignore it for those versions
x = rand(10)
n_a = [@allocated IMD.sum(x) for _ in 1:10]
@test n_a[end] <= 16
x = Union{Int32, Missing}[1,2,missing, 4]
n_a = [@allocated IMD.sum(x) for _ in 1:10]
@test n_a[end] == 0
n_a = [@allocated IMD.sum(y->ismissing(y) ? 0 : y, x) for _ in 1:10]
@test n_a[end] <= 16
x = rand(10)
n_a = [@allocated IMD.mean(x) for _ in 1:10]
@test n_a[end] <= 16
x = Union{Int32, Missing}[1,2,missing, 4]
n_a = [@allocated IMD.mean(x) for _ in 1:10]
@test n_a[end] <= 16
n_a = [@allocated IMD.mean(y->ismissing(y) ? 0 : y, x) for _ in 1:10]
@test n_a[end] <= 16
end
end