Skip to content

Commit

Permalink
Fix minimum/maximum over dimensions with missing values
Browse files Browse the repository at this point in the history
`v0 != v0` returns `missing` for missing values. Use the largest/smallest
non-missing value to initialize the array. This is an inefficient approach.
Faster alternatives would be to avoid using an initial value at all,
and instead keep track of whether a value has been set in a separate mask;
or to use `typemax`/`typemin` for types that support them.
  • Loading branch information
nalimilan committed Mar 31, 2020
1 parent ddf79a8 commit 347fba7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion base/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ for (f1, f2, initval) in ((:min, :max, :Inf), (:max, :min, :(-Inf)))
# otherwise use the min/max of the first slice as initial value
v0 = mapreduce(f, $f2, A1)

# but NaNs need to be avoided as initial values
# but missings and NaNs need to be avoided as initial values
if ismissing(v0) && !all(ismissing, A)
v0 = mapreduce(f, $f2, skipmissing(A))
end
v0 = v0 != v0 ? typeof(v0)($initval) : v0

T = _realtype(f, promote_union(eltype(A)))
Expand Down
8 changes: 8 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ end
@test maximum(Vector(Int16(1):Int16(100))) === Int16(100)
@test maximum(Int32[1,2]) === Int32(2)

@testset "minimum/maximum over dims with missing (#35308)" begin
x = [1 missing; 2 missing]
@test isequal(minimum(x, dims=1), reshape([1, missing], 1, :))
@test isequal(maximum(x, dims=1), reshape([2, missing], 1, :))
@test isequal(minimum(x, dims=2), reshape([missing, missing], :, 1))
@test isequal(maximum(x, dims=2), reshape([missing, missing], :, 1))
end

A = circshift(reshape(1:24,2,3,4), (0,1,1))
@test extrema(A,dims=1) == reshape([(23,24),(19,20),(21,22),(5,6),(1,2),(3,4),(11,12),(7,8),(9,10),(17,18),(13,14),(15,16)],1,3,4)
@test extrema(A,dims=2) == reshape([(19,23),(20,24),(1,5),(2,6),(7,11),(8,12),(13,17),(14,18)],2,1,4)
Expand Down

0 comments on commit 347fba7

Please sign in to comment.