Skip to content

Commit

Permalink
Throw debug messages when substitution aren't possible when all value…
Browse files Browse the repository at this point in the history
…s are missing.
  • Loading branch information
rofinn committed Feb 24, 2021
1 parent 3c31187 commit 988b4d6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/imputors/substitute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@ Substitute(; statistic=defaultstats) = Substitute(statistic)

function _impute!(data::AbstractArray{Union{T, Missing}}, imp::Substitute) where T
mask = .!ismissing.(data)
x = imp.statistic(disallowmissing(data[mask]))
return Base.replace!(data, missing => x)
# Since most summary statistics will require some data, we throw a debug message when
# all supplied values are missing
if any(mask)
x = imp.statistic(disallowmissing(data[mask]))
return Base.replace!(data, missing => x)
else
@debug(
"Cannot apply substitution function ($(imp.statistic)) " *
"when all values are missing"
)
return data
end
end


Expand Down Expand Up @@ -82,8 +92,19 @@ end

function _impute!(data::AbstractArray{Union{T, Missing}}, imp::WeightedSubstitute) where T
mask = .!ismissing.(data)
x = imp.statistic(disallowmissing(data[mask]), imp.weights[mask])
return Base.replace!(data, missing => x)

# Since most summary statistics will require some data, we throw a debug message when
# all supplied values are missing
if any(mask)
x = imp.statistic(disallowmissing(data[mask]), imp.weights[mask])
return Base.replace!(data, missing => x)
else
@debug(
"Cannot apply weighted substitution function ($(imp.statistic)) " *
"when all values are missing"
)
return data
end
end

# Auxiliary functions defining our default substitution rules
Expand Down
12 changes: 12 additions & 0 deletions test/imputors/substitute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
)
@test result == expected
end

@testset "all missing" begin
a = [missing 2; missing 3]
# Test that substitution along columns with all missing data will just return that data.
Impute.substitute(a; dims=2) == a
end
end

@testset "WeightedSubstitute" begin
Expand Down Expand Up @@ -220,4 +226,10 @@ end
)
@test result == expected
end

@testset "all missing" begin
a = [missing 2; missing 3]
# Test that substitution along columns with all missing data will just return that data.
Impute.wsubstitute(a; weights=eweights(2, 0.3), dims=2) == a
end
end

0 comments on commit 988b4d6

Please sign in to comment.