-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnon_hp_stat.jl
762 lines (686 loc) · 23.3 KB
/
non_hp_stat.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# _stat_add_sum(x::T, y::S) where T where S = convert(promote_type(S,T), x + y)
_stat_add_sum(x, y) = Base.add_sum(x, y)
_stat_add_sum(x::Bool, y::Bool) = x + y
_stat_add_sum(x::Missing, y::Bool) = Int(y)
_stat_add_sum(x::Bool, y::Missing) = Int(x)
_stat_add_sum(x, ::Missing) = x
_stat_add_sum(::Missing, x) = x
_stat_add_sum(::Missing, ::Missing) = missing
_stat_mul_prod(x, y) = Base.mul_prod(x, y)
_stat_mul_prod(x, ::Missing) = x
_stat_mul_prod(::Missing, x) = x
_stat_mul_prod(::Missing, ::Missing) = missing
_stat_min_fun(x, y) = min(x, y)
_stat_min_fun(x, ::Missing) = x
_stat_min_fun(::Missing, y) = y
_stat_min_fun(::Missing, ::Missing) = missing
_stat_max_fun(x, y) = max(x, y)
_stat_max_fun(x, ::Missing) = x
_stat_max_fun(::Missing, y) = y
_stat_max_fun(::Missing, ::Missing) = missing
_stat_realXcY(x, y) = Statistics.realXcY(x, y)
_stat_realXcY(x, ::Missing) = x
_stat_realXcY(::Missing, y) = y
_stat_realXcY(::Missing, ::Missing) = missing
ISNAN(x::Any) = isnan(x)
ISNAN(::Missing) = false
_stat_bool(f) = x -> f(x)::Bool
_stat_ismissing(x::Any)::Int = 0
_stat_ismissing(::Missing)::Int = 1
_stat_notmissing(x::Any)::Int = 1
_stat_notmissing(::Missing)::Int = 0
"""
lag(x, k; default = missing)
lag!(x, k; default = missing)
Create a lag-k of the provided vector `x`. The output will be a vector the
same size as `x` (the input array).
`lag!` replace the input vector.
"""
(lag, lag!)
function lag(x::AbstractVector, k; default=missing)
@assert firstindex(x) == 1 "lag only supports 1-based indexing"
res = Vector{Union{promote_type(typeof(default), eltype(x)),Missing}}(undef, length(x))
for i in 1:k
@inbounds res[i] = default
end
for i in (k+1):length(x)
@inbounds res[i] = x[i-k]
end
res
end
lag(x::AbstractVector; default=missing) = lag(x, 1; default=default)
function lag!(x::AbstractVector, k; default=missing)
@assert firstindex(x) == 1 "lag! only supports 1-based indexing"
@assert promote_type(typeof(default), eltype(x)) <: eltype(x) "`default` must be the same type as the element of the passed vector"
for i in length(x):-1:(k+1)
@inbounds x[i] = x[i-k]
end
for i in 1:k
@inbounds x[i] = default
end
x
end
lag!(x::AbstractVector; default=missing) = lag!(x, 1; default=default)
"""
lead(x, k; default = missing)
lead!(x, k; default = missing)
Create a lead-k of the provided vector `x`. The output will be a vector the
same size as `x` (the input array).
`lead!` replace the input vector.
"""
(lead, lead!)
function lead(x::AbstractVector, k; default=missing)
@assert firstindex(x) == 1 "lead only supports 1-based indexing"
res = Vector{Union{promote_type(typeof(default), eltype(x)),Missing}}(undef, length(x))
for i in 1:length(x)-k
@inbounds res[i] = x[i+k]
end
for i in (length(x)-k+1):length(x)
@inbounds res[i] = default
end
res
end
lead(x::AbstractVector; default=missing) = lead(x, 1; default=default)
function lead!(x::AbstractVector, k; default=missing)
@assert firstindex(x) == 1 "lead! only supports 1-based indexing"
@assert promote_type(typeof(default), eltype(x)) <: eltype(x) "`default` must be the same type as the element of the passed vector"
for i in 1:length(x)-k
@inbounds x[i] = x[i+k]
end
for i in (length(x)-k+1):length(x)
@inbounds x[i] = default
end
x
end
lead!(x::AbstractVector; default=missing) = lead!(x, 1; default=default)
"""
rescale(x,minx,maxx,minval,maxval)
Rescale x to run from minval and maxval, given x originaly runs from minx to maxx.
"""
function rescale(x, minx, maxx, minval, maxval)
-(-maxx * minval + minx * maxval) / (maxx - minx) + (-minval + maxval) * x / (maxx - minx)
end
rescale(::Missing, minx, maxx, minval, maxval) = missing
rescale(x::Vector, minx, maxx, minval, maxval) = rescale.(x, minx, maxx, minval, maxval)
rescale(x, minx, maxx) = rescale(x, minx, maxx, 0.0, 1.0)
"""
stdze(x)
Standardize an array. It returns missing for missing data points.
"""
function stdze(x)
all(ismissing, x) && return x
meandata = mean(x)
vardata = var(x)
(x .- meandata) ./ sqrt(vardata)
end
# this is manual simd version for max(min) function
function stat_maximum(f::typeof(identity), x::AbstractArray{T,1}; lo=1, hi=length(x)) where {T}
all(ismissing, view(x, lo:hi)) && return missing
_dmiss(x) = ismissing(x) ? typemin(nonmissingtype(T)) : x
Base.mapreduce_impl(_dmiss, max, x, lo, hi)
end
function stat_maximum(f::F, x::AbstractArray{T,1}; lo=1, hi=length(x)) where {F,T}
all(ismissing, view(x, lo:hi)) && return missing
Base.mapreduce_impl(f, _stat_max_fun, x, lo, hi)
end
stat_maximum(x::AbstractArray{T,1}; lo=1, hi=length(x)) where {T} = stat_maximum(identity, x; lo=lo, hi=hi)
function _arg_minmax_barrier(x, minmaxval, f)::Int
@inbounds for i in 1:length(x)
isequal(f(x[i]), minmaxval) && return i
end
end
function stat_findmax(f, x::AbstractArray{T,1}) where {T}
isempty(x) && throw(ArgumentError("input vector cannot be empty"))
maxval = stat_maximum(f, x)
ismissing(maxval) && return (missing, missing)
(maxval, _arg_minmax_barrier(x, maxval, f))
end
stat_findmax(x::AbstractArray{T,1}) where {T} = stat_findmax(identity, x)
function stat_minimum(f::typeof(identity), x::AbstractArray{T,1}; lo=1, hi=length(x)) where {T}
all(ismissing, view(x, lo:hi)) && return missing
@inline _dmiss(x) = ismissing(x) ? typemax(nonmissingtype(T)) : x
Base.mapreduce_impl(_dmiss, min, x, lo, hi)
end
function stat_minimum(f::F, x::AbstractArray{T,1}; lo=1, hi=length(x)) where {F,T}
all(ismissing, view(x, lo:hi)) && return missing
Base.mapreduce_impl(f, _stat_min_fun, x, lo, hi)
end
stat_minimum(x::AbstractArray{T,1}; lo=1, hi=length(x)) where {T} = stat_minimum(identity, x; lo=lo, hi=hi)
function stat_findmin(f, x::AbstractArray{T,1}) where {T}
isempty(x) && throw(ArgumentError("input vector cannot be empty"))
minval = stat_minimum(f, x)
(minval, _arg_minmax_barrier(x, minval, f))
end
stat_findmin(x::AbstractArray{T,1}) where {T} = stat_findmin(identity, x)
function stat_sum(f, x::AbstractArray{T,1}; lo=1, hi=length(x)) where {T<:Union{Missing,INTEGERS,FLOATS}}
all(ismissing, view(x, lo:hi)) && return f(first(x))
_dmiss(y) = ifelse(ismissing(f(y)), zero(T), f(y))
Base.mapreduce_impl(_dmiss, _stat_add_sum, x, lo, hi)
end
stat_sum(x::AbstractArray{T,1}; lo=1, hi=length(x)) where {T<:Union{Missing,INTEGERS,FLOATS}} = stat_sum(identity, x; lo=lo, hi=hi)
# function stat_wsum(f, x::AbstractArray{Union{T,Missing},1}, w) where T
# all(ismissing, x) && return missing
# _dmiss(y) = ismissing(y[1])||ismissing(y[2]) ? zero(T) : (f(y[1])*y[2])
# mapreduce(_dmiss, _stat_add_sum, zip(x,w))
# end
# stat_wsum(x::AbstractArray{Union{T,Missing},1}, w) where T = stat_wsum(identity, x, w)
function stat_wsum(f, x::AbstractVector{T}, w::AbstractVector) where {T}
all(ismissing, x) && return missing
_dmiss(y) = ismissing(y[1]) || ismissing(y[2]) ? missing : (f(y[1]) * y[2])
mapreduce(_dmiss, _stat_add_sum, zip(x, w))
end
stat_wsum(x::AbstractVector{T}, w::AbstractVector) where {T} = stat_wsum(identity, x, w)
function stat_mean(f, x::AbstractArray{T,1})::Union{Float64,Missing} where {T<:Union{Missing,INTEGERS,FLOATS}}
length(x) == 1 && return f(first(x))
sval = stat_sum(y -> f(y) * 1.0, x)
n = mapreduce(!ismissing ∘ f, +, x)
n == 0 ? missing : sval / n
end
stat_mean(x::AbstractArray{T,1}) where {T} = stat_mean(identity, x)
stat_cumsum_ignore(x::AbstractVector) = accumulate(_stat_add_sum, x)
stat_cumsum!_ignore(outx, inx::AbstractVector) = accumulate!(_stat_add_sum, outx, inx)
stat_cumprod_ignore(x::AbstractVector) = accumulate(_stat_mul_prod, x)
stat_cumprod!_ignore(outx, inx::AbstractVector) = accumulate!(_stat_mul_prod, outx, inx)
stat_cummin_ignore(x::AbstractVector) = accumulate(_stat_min_fun, x)
stat_cummin!_ignore(outx, inx::AbstractVector) = accumulate!(_stat_min_fun, outx, inx)
stat_cummax_ignore(x::AbstractVector) = accumulate(_stat_max_fun, x)
stat_cummax!_ignore(outx, inx::AbstractVector) = accumulate!(_stat_max_fun, outx, inx)
function stat_cumsum_skip(x::AbstractVector)
locmiss = ismissing.(x)
res = stat_cumsum_ignore(x)
if sum(locmiss) > 0
res[locmiss] .= missing
end
res
end
function stat_cumsum!_skip(outx, inx::AbstractVector)
locmiss = ismissing.(inx)
stat_cumsum!_ignore(outx, inx)
if sum(locmiss) > 0
outx[locmiss] .= missing
end
outx
end
function stat_cumprod_skip(x::AbstractVector)
locmiss = ismissing.(x)
res = stat_cumprod_ignore(x)
if sum(locmiss) > 0
res[locmiss] .= missing
end
res
end
function stat_cumprod!_skip(outx, inx::AbstractVector)
locmiss = ismissing.(inx)
stat_cumprod!_ignore(outx, inx)
if sum(locmiss) > 0
outx[locmiss] .= missing
end
outx
end
function stat_cummin_skip(x::AbstractVector)
locmiss = ismissing.(x)
res = stat_cummin_ignore(x)
if sum(locmiss) > 0
res[locmiss] .= missing
end
res
end
function stat_cummin!_skip(outx, inx::AbstractVector)
locmiss = ismissing.(inx)
stat_cummin!_ignore(outx, inx)
if sum(locmiss) > 0
outx[locmiss] .= missing
end
outx
end
function stat_cummax_skip(x::AbstractVector)
locmiss = ismissing.(x)
res = stat_cummax_ignore(x)
if sum(locmiss) > 0
res[locmiss] .= missing
end
res
end
function stat_cummax!_skip(outx, inx::AbstractVector)
locmiss = ismissing.(inx)
stat_cummax!_ignore(outx, inx)
if sum(locmiss) > 0
outx[locmiss] .= missing
end
outx
end
function stat_wmean(f, x::AbstractVector{T}, w::AbstractArray{S,1}) where {T} where {S}
all(ismissing, x) && return missing
_dmiss(y) = ismissing(y[1]) || ismissing(y[2]) ? zero(T) : (f(y[1]) * y[2])
_dmiss2(y) = ismissing(y[1]) || ismissing(y[2]) ? zero(S) : y[2]
_op(y1, y2) = _stat_add_sum.(y1, y2)
_f(y) = (_dmiss(y), _dmiss2(y))
sval, n = mapreduce(_f, _op, zip(x, w))
n == 0 ? missing : sval / n
end
stat_wmean(x::AbstractVector{T}, w::AbstractArray{S,1}) where {T} where {S} = stat_wmean(identity, x, w)
function stat_var(f, x::AbstractArray{T,1}, dof=true)::Union{Float64,Missing} where {T<:Union{Missing,INTEGERS,FLOATS}}
all(ismissing, x) && return missing
# any(ISNAN, x) && return convert(eltype(x), NaN)
# meanval = stat_mean(f, x)
# n = mapreduce(!ismissing∘f, +, x)
sval = stat_sum(y -> f(y) * 1.0, x)
n = mapreduce(!ismissing ∘ f, +, x)
meanval = n == 0 ? missing : sval / n
ss = 0.0
for i in 1:length(x)
ss = _stat_add_sum(ss, abs2(f(x[i]) - meanval))
end
if n == 0
return missing
elseif n == 1 && dof
return missing
else
return ss / (n - Int(dof))
end
end
stat_var(x::AbstractArray{T,1}, dof=true) where {T} = stat_var(identity, x, dof)
function stat_std(f, x::AbstractArray{T,1}, dof=true)::Union{Float64,Missing} where {T<:Union{Missing,INTEGERS,FLOATS}}
sqrt(stat_var(f, x, dof))
end
stat_std(x::AbstractArray{T,1}, dof=true) where {T} = stat_std(identity, x, dof)
function stat_median(v::AbstractArray{T,1}) where {T}
isempty(v) && throw(ArgumentError("median of an empty array is undefined, $(repr(v))"))
all(ismissing, v) && return missing
(nonmissingtype(eltype(v)) <: AbstractFloat || nonmissingtype(eltype(v)) >: AbstractFloat) && any(ISNAN, v) && return convert(eltype(v), NaN)
nmis::Int = mapreduce(ismissing, +, v)
n = length(v) - nmis
mid = div(1 + n, 2)
if isodd(n)
return middle(partialsort(v, mid))
else
m = partialsort(v, mid:mid+1)
return middle(m[1], m[2])
end
end
function stat_median!(v::AbstractArray{T,1}) where {T}
isempty(v) && throw(ArgumentError("median of an empty array is undefined, $(repr(v))"))
all(ismissing, v) && return missing
(nonmissingtype(eltype(v)) <: AbstractFloat || nonmissingtype(eltype(v)) >: AbstractFloat) && any(ISNAN, v) && return convert(eltype(v), NaN)
nmis::Int = mapreduce(ismissing, +, v)
n = length(v) - nmis
mid = div(1 + n, 2)
if isodd(n)
return middle(partialsort!(v, mid))
else
m = partialsort!(v, mid:mid+1)
return middle(m[1], m[2])
end
end
# finding k largest in an array with missing values
function topk_sort!(v::AbstractVector, lo::Integer, hi::Integer, lt_fun)
@inbounds for i = lo+1:hi
j = i
x = v[i]
while j > lo
if lt_fun(x, v[j-1])
v[j] = v[j-1]
j -= 1
continue
end
break
end
v[j] = x
end
return v
end
function topk_sort_permute!(v::AbstractVector, perm::AbstractVector, lo::Integer, hi::Integer, lt_fun)
@inbounds for i = lo+1:hi
j = i
x = v[i]
y = perm[i]
while j > lo
if lt_fun(x, v[j-1])
v[j] = v[j-1]
perm[j] = perm[j-1]
j -= 1
continue
end
break
end
v[j] = x
perm[j] = y
end
return v
end
function initiate_topk_res!(res, x, by)
cnt = 1
idx = 1
@inbounds for i in 1:length(x)
idx = i
if !ismissing(by(x[i]))
res[cnt] = x[i]
cnt += 1
if cnt > length(res)
break
end
end
end
idx, cnt - 1
end
function initiate_topk_res_perm!(perm, res, x, by; offset=0)
cnt = 1
idx = 1
@inbounds for i in 1:length(x)
idx = i
if !ismissing(by(x[i]))
res[cnt] = x[i]
perm[cnt] = i + offset
cnt += 1
if cnt > length(res)
break
end
end
end
idx, cnt - 1
end
# it is unsafe because x must be continuous in memory
function unsafe_shift_insert!(x::AbstractVector{T}, i, item) where T<:Union{Missing, FLOATS, INTEGERS}
n = length(x)
ccall(:memmove, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), pointer(x, i + 1), pointer(x, i), (n - i) * Base.aligned_sizeof(T))
x[i] = item
x
end
Base.@propagate_inbounds function insert_fixed_sorted_binary!(x, item, lt_fun)
if !lt_fun(item, x[end])
return
end
idx = searchsortedlast(x, item, lt=lt_fun)
unsafe_shift_insert!(x, idx + 1, item)
nothing
end
Base.@propagate_inbounds function insert_fixed_sorted!(x, item, lt_fun)
if !lt_fun(item, x[end])
return
end
x[end] = item
j = length(x)
while j > 1
if lt_fun(item, x[j-1])
x[j] = x[j-1]
j -= 1
continue
end
break
end
x[j] = item
nothing
end
# TODO we do not need x, this is just easier to implement, later we may fix this
Base.@propagate_inbounds function insert_fixed_sorted_perm_binary!(perm, x, idx, item, lt_fun)
if !lt_fun(item, x[end])
return
end
i = searchsortedlast(x, item, lt=lt_fun)
unsafe_shift_insert!(x, i + 1, item)
unsafe_shift_insert!(perm, i + 1, idx)
nothing
end
Base.@propagate_inbounds function insert_fixed_sorted_perm!(perm, x, idx, item, lt_fun)
if !lt_fun(item, x[end])
return
end
x[end] = item
perm[end] = idx
j = length(x)
while j > 1
if lt_fun(item, x[j-1])
x[j] = x[j-1]
perm[j] = perm[j-1]
j -= 1
continue
end
break
end
x[j] = item
perm[j] = idx
nothing
end
Base.@propagate_inbounds function topk_vals(x::AbstractVector{T}, k::Int, lt_fun::F, by) where {T} where {F}
k < 1 && throw(ArgumentError("k must be greater than 1"))
all(ismissing, x) && return Union{Missing,T}[missing]
# TODO should we use similar() here?
res = Vector{T}(undef, k)
idx, cnt = initiate_topk_res!(res, x, by)
topk_sort!(res, 1, cnt, lt_fun)
for i in idx+1:length(x)
if !ismissing(by(x[i]))
insert_fixed_sorted!(res, x[i], lt_fun)
cnt += 1
end
end
if cnt < k
allowmissing(resize!(res, cnt))
else
allowmissing(res)
end
end
#if k is greater than 20 (15 in topkperm) we switch to binary search - 21 and 16 are selected based on simulation study
Base.@propagate_inbounds function topk_vals(x::Union{Vector{T}, SubArray{T, N, Vector{T}, Tuple{I}, L}}, k::Int, lt_fun::F, by) where {T<:Union{Missing, FLOATS, INTEGERS}} where {F} where N where I <: UnitRange{Int} where L
k < 1 && throw(ArgumentError("k must be greater than 1"))
all(ismissing, x) && return Union{Missing,T}[missing]
res = Vector{T}(undef, k)
idx, cnt = initiate_topk_res!(res, x, by)
topk_sort!(res, 1, cnt, lt_fun)
if k < 21
for i in idx+1:length(x)
if !ismissing(by(x[i]))
insert_fixed_sorted!(res, x[i], lt_fun)
cnt += 1
end
end
else
for i in idx+1:length(x)
if !ismissing(by(x[i]))
insert_fixed_sorted_binary!(res, x[i], lt_fun)
cnt += 1
end
end
end
if cnt < k
allowmissing(resize!(res, cnt))
else
allowmissing(res)
end
end
# ktop permutation
#TODO should we return [missing] or Int[] when all elements are missings?
Base.@propagate_inbounds function topk_perm(x::AbstractVector{T}, k::Int, lt_fun::F, by) where {T} where {F}
k < 1 && throw(ArgumentError("k must be greater than 1"))
all(ismissing, x) && return Union{Missing,Int}[missing]
res = Vector{T}(undef, k)
perm = zeros(Int, k)
idx, cnt = initiate_topk_res_perm!(perm, res, x, by)
topk_sort_permute!(res, perm, 1, cnt, lt_fun)
for i in idx+1:length(x)
if !ismissing(by(x[i]))
insert_fixed_sorted_perm!(perm, res, i, x[i], lt_fun)
cnt += 1
end
end
if cnt < k
allowmissing(resize!(perm, cnt))
else
allowmissing(perm)
end
end
Base.@propagate_inbounds function topk_perm(x::Union{Vector{T}, SubArray{T, N, Vector{T}, Tuple{I}, L}}, k::Int, lt_fun::F, by) where {T<:Union{Missing, FLOATS, INTEGERS}} where {F} where N where I <: UnitRange{Int} where L
k < 1 && throw(ArgumentError("k must be greater than 1"))
all(ismissing, x) && return Union{Missing,Int}[missing]
res = Vector{T}(undef, k)
perm = zeros(Int, k)
idx, cnt = initiate_topk_res_perm!(perm, res, x, by)
topk_sort_permute!(res, perm, 1, cnt, lt_fun)
if k < 16
for i in idx+1:length(x)
if !ismissing(by(x[i]))
insert_fixed_sorted_perm!(perm, res, i, x[i], lt_fun)
cnt += 1
end
end
else
for i in idx+1:length(x)
if !ismissing(by(x[i]))
insert_fixed_sorted_perm_binary!(perm, res, i, x[i], lt_fun)
cnt += 1
end
end
end
if cnt < k
allowmissing(resize!(perm, cnt))
else
allowmissing(perm)
end
end
"""
topk(x, k; rev = false, lt = isless, by = identity, threads = false)
Return upto `k` largest nonmissing elements of `x`. When `rev = true` it returns upto `k` smallest nonmissing elements of `x`. When all elements are missing, the function returns `[missing]`. The `by` keyword lets you provide a function that will be applied to each element before comparison; the `lt` keyword allows providing a custom "less than" function (note that for every x and y, only one of `lt(x,y)` and `lt(y,x)` can return true)
> When it is safe, passing `lt = <` improves the performance.
Also see [`topkperm`](@ref), [`partialsort`](@ref)
# Example
```jldoctest
julia> x = [10, -11, missing, 1, 0]
5-element Vector{Union{Missing, Int64}}:
10
-11
missing
1
0
julia> topk(x, 3)
3-element Vector{Union{Missing, Int64}}:
10
1
0
julia> topk(x, 3, by = abs)
3-element Vector{Union{Missing, Int64}}:
-11
10
1
julia> topk(x, 3, by = abs, rev = true)
3-element Vector{Union{Missing, Int64}}:
0
1
10
```
"""
function topk(x::AbstractVector, k::Int; rev::Bool=false, lt=isless, by=identity, threads=false)
isempty(x) && throw(ArgumentError("empty arrays are not allowed"))
@assert firstindex(x) == 1 "topk only supports 1-based indexing"
if threads && length(x) > Threads.nthreads()
if rev
hp_topk_vals(x, k, (y1, y2) -> lt(by(y1), by(y2)), by)
else
hp_topk_vals(x, k, (y1, y2) -> lt(by(y2), by(y1)), by)
end
else
if rev
topk_vals(x, k, (y1, y2) -> lt(by(y1), by(y2)), by)
else
topk_vals(x, k, (y1, y2) -> lt(by(y2), by(y1)), by)
end
end
end
"""
topkperm(x, k; rev = false, lt = isless, by = identity, threads = false)
Return the indices of upto `k` largest nonmissing elements of `x`. When `rev = true` it returns the indices of upto `k` smallest nonmissing elements of `x`. When all elements are missing, the function returns `[missing]`. The `by` keyword lets you provide a function that will be applied to each element before comparison; the `lt` keyword allows providing a custom "less than" function (note that for every x and y, only one of `lt(x,y)` and `lt(y,x)` can return true)
> When it is safe, passing `lt = <` improves the performance.
Also see [`topk`](@ref), [`partialsortperm`](@ref)
# Examples
```jldoctest
julia> x = rand(1000);
julia> topkperm(x, 10)
10-element Vector{Union{Missing, Int64}}:
711
470
291
401
927
124
949
164
216
948
julia> topkperm(x, 10, threads = true)
10-element Vector{Union{Missing, Int64}}:
711
470
291
401
927
124
949
164
216
948
julia> topkperm(x, 10, threads = true, rev = true)
10-element Vector{Union{Missing, Int64}}:
207
351
497
768
135
599
608
798
675
648
```
"""
function topkperm(x::AbstractVector, k::Int; rev::Bool=false, lt=isless, by=identity, threads=false)
isempty(x) && throw(ArgumentError("empty arrays are not allowed"))
@assert firstindex(x) == 1 "topkperm only supports 1-based indexing"
if threads && length(x) > Threads.nthreads()
if rev
hp_topk_perm(x, k, (y1, y2) -> lt(by(y1), by(y2)), by)
else
hp_topk_perm(x, k, (y1, y2) -> lt(by(y2), by(y1)), by)
end
else
if rev
topk_perm(x, k, (y1, y2) -> lt(by(y1), by(y2)), by)
else
topk_perm(x, k, (y1, y2) -> lt(by(y2), by(y1)), by)
end
end
end
"""
ffill(x; [by = ismissing])
ffill!(x; [by = ismissing])
Replace those elements of `x` which returns `true` when `by` is called on them with the previous element which calling `by` on it returns `false`.
`ffill!` modifies the input vector in-place
See also [`bfill`](@ref) and [`bfill!`](@ref)
"""
(ffill, ffill!)
function ffill!(x::AbstractVector; by=ismissing)
@assert firstindex(x) == 1 "ffill!/ffill only support 1-based indexing"
for i in 2:length(x)
if by(x[i])
x[i] = x[i-1]
end
end
x
end
ffill(x; by=ismissing) = ffill!(copy(x), by=by)
"""
bfill(x; [by = ismissing])
bfill!(x; [by = ismissing])
Replace those elements of `x` which returns `true` when `by` is called on them with the next element which calling `by` on it returns `false`.
`bfill!` modifies the input vector in-place
See also [`ffill`](@ref) and [`ffill!`](@ref)
"""
(bfill, bfill!)
function bfill!(x::AbstractVector; by=ismissing)
@assert firstindex(x) == 1 "bfill!/bfill only support 1-based indexing"
for i in length(x)-1:-1:1
if by(x[i])
x[i] = x[i+1]
end
end
x
end
bfill(x; by=ismissing) = bfill!(copy(x), by=by)