Skip to content

Commit

Permalink
Substitutions when all missing (#107)
Browse files Browse the repository at this point in the history
* Throw debug messages when substitution aren't possible when all values are missing.

* Bump patch release version.

* Apply suggestions from code review

Co-authored-by: Glenn Moynihan <[email protected]>

* Test log message instead.

Co-authored-by: Glenn Moynihan <[email protected]>
  • Loading branch information
rofinn and Glenn Moynihan committed Feb 24, 2021
1 parent 065f5f1 commit 70ccac3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
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

2 comments on commit 70ccac3

@rofinn
Copy link
Member Author

@rofinn rofinn commented on 70ccac3 Feb 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/30789

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.3 -m "<description of version>" 70ccac3e6bb861015b113ac114ffbd9ed5c1d2e6
git push origin v0.6.3

Please sign in to comment.