Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substitutions when all missing #107

Merged
merged 4 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Impute"
uuid = "f7bf1975-0170-51b9-8c5f-a992d46b9575"
authors = ["Invenia Technical Computing"]
version = "0.6.2"
version = "0.6.3"

[deps]
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
Expand Down
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
@warn(
"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
@warn(
"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
10 changes: 10 additions & 0 deletions test/imputors/substitute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
)
@test result == expected
end

@testset "all missing" begin
a = [missing 2; missing 3]
@test_logs (:warn, r"all values are missing") Impute.substitute(a; dims=2)
end
end

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

@testset "all missing" begin
a = [missing 2; missing 3]
@test_logs (:warn, r"all values are missing") Impute.substitute(a; dims=2)
end
end