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

Simplify Imputor API #69

Merged
merged 19 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
cdef813
Remove `context` to simplify imputation code.
rofinn Sep 29, 2020
f1c863f
Replace context limits with a threshold assertion type.
rofinn Sep 29, 2020
640e2cb
Introduce function interface for threshold.
rofinn Sep 29, 2020
1500ece
Initial work on improving support for `dims` and other cleanup.
rofinn Sep 30, 2020
4772961
Finished reworking dims changes.
rofinn Oct 1, 2020
6e6497e
Simplify dims code a bit more by explicitly using NamedDims.jl
rofinn Oct 1, 2020
43fc0fc
Chain is no longer an imputor.
rofinn Oct 1, 2020
c992d51
Added a Standardize imputor which handles replace user defined 'missi…
rofinn Oct 2, 2020
bc15b8f
Replaced Impute.Fill with Impute.Replace for constants and Impute.Sub…
rofinn Oct 3, 2020
10f3737
Update doctests and run documenter doctests in runtests.jl.
rofinn Oct 6, 2020
8ed73e4
Finish moving various imputor testsets into independent files.
rofinn Oct 6, 2020
850d119
Added more NamedDims and AxisKeys tests.
rofinn Oct 6, 2020
0bba5aa
Lots of documentation updates and minor bug fixes/improvements.
rofinn Oct 8, 2020
af45737
Run CI on julia 1.5 and move deprecated matrix block into jobs so doc…
rofinn Oct 8, 2020
8213ce3
Removed redundant/unreachable code and added more tests.
rofinn Oct 9, 2020
27dfb99
Minor changes from review comments.
rofinn Oct 11, 2020
701d85b
Use a ThresholdError for clearer error messages.
rofinn Oct 11, 2020
bb98aa9
Fix outdated imputor docstring.
rofinn Oct 11, 2020
6c3b6d8
Test Threshold showerror method.
rofinn Oct 14, 2020
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
300 changes: 1 addition & 299 deletions src/Impute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,305 +47,7 @@ Base.showerror(io::IO, err::ImputeError) = println(io, "ImputeError: $(err.msg)"
include("utils.jl")
include("assertions.jl")
include("imputors.jl")

#=
These default methods are required because @auto_hash_equals doesn't
play nice with Base.@kwdef
=#
function Base.hash(imp::T, h::UInt) where T <: Imputor
h = hash(Symbol(T), h)

for f in fieldnames(T)
h = hash(getfield(imp, f), h)
end

return h
end

function Base.:(==)(a::T, b::T) where T <: Imputor
result = true

for f in fieldnames(T)
if !isequal(getfield(a, f), getfield(b, f))
result = false
break
end
end

return result
end

const global assertion_methods = (
threshold = Threshold,
)

const global imputation_methods = (
drop = DropObs,
dropobs = DropObs,
dropvars = DropVars,
interp = Interpolate,
interpolate = Interpolate,
fill = Fill,
locf = LOCF,
nocb = NOCB,
srs = SRS,
svd = SVD,
knn = KNN,
)

include("functional.jl")
include("deprecated.jl")

for (f, v) in pairs(assertion_methods)
typename = nameof(v)
@eval $f(data; kwargs...) = assert(data, $typename(; kwargs...))
end

for (f, v) in pairs(imputation_methods)
typename = nameof(v)
f! = Symbol(f, :!)

@eval begin
$f(data; kwargs...) = _impute(data, $typename, kwargs...)
$f!(data; kwargs...) = _impute!(data, $typename, kwargs...)
$f(; kwargs...) = data -> _impute(data, $typename, kwargs...)
$f!(; kwargs...) = data -> _impute!(data, $typename, kwargs...)
end
end

@doc """
Impute.dropobs(data; dims=1)

Removes missing observations from the `AbstractArray` or `Tables.table` provided.
See [DropObs](@ref) for details.

# Example
```
julia> using DataFrames; using Impute: Impute

julia> df = DataFrame(:a => [1.0, 2.0, missing, missing, 5.0], :b => [1.1, 2.2, 3.3, missing, 5.5])
5×2 DataFrames.DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ missing │ 3.3 │
│ 4 │ missing │ missing │
│ 5 │ 5.0 │ 5.5 │

julia> Impute.dropobs(df; dims=2)
3×2 DataFrames.DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼─────────┼─────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ 5.0 │ 5.5 │
```
""" dropobs

@doc """
Impute.dropvars(data; dims=1)

Finds variables with too many missing values in a `AbstractMatrix` or `Tables.table` and
removes them from the input data. See [DropVars](@ref) for details.

# Example
```jldoctest
julia> using DataFrames; using Impute: Impute

julia> df = DataFrame(:a => [1.0, 2.0, missing, missing, 5.0], :b => [1.1, 2.2, 3.3, missing, 5.5])
5×2 DataFrames.DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ missing │ 3.3 │
│ 4 │ missing │ missing │
│ 5 │ 5.0 │ 5.5 │

julia> Impute.dropvars(df)
5×1 DataFrames.DataFrame
│ Row │ b │
│ │ Float64 │
├─────┼──────────┤
│ 1 │ 1.1 │
│ 2 │ 2.2 │
│ 3 │ 3.3 │
│ 4 │ missing │
│ 5 │ 5.5 │
```
""" dropvars

@doc """
Impute.interp(data; dims=1)

Performs linear interpolation between the nearest values in an vector.
See [Interpolate](@ref) for details.

# Example
```jldoctest
julia> using DataFrames; using Impute: Impute

julia> df = DataFrame(:a => [1.0, 2.0, missing, missing, 5.0], :b => [1.1, 2.2, 3.3, missing, 5.5])
5×2 DataFrames.DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ missing │ 3.3 │
│ 4 │ missing │ missing │
│ 5 │ 5.0 │ 5.5 │

julia> Impute.interp(df)
5×2 DataFrames.DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ 3.0 │ 3.3 │
│ 4 │ 4.0 │ 4.4 │
│ 5 │ 5.0 │ 5.5 │
```
""" interp

@doc """
Impute.fill(data; value=mean, dims=1)

Fills in the missing data with a specific value. See [Fill](@ref) for details.

# Example
```jldoctest
julia> using DataFrames; using Impute: Impute

julia> df = DataFrame(:a => [1.0, 2.0, missing, missing, 5.0], :b => [1.1, 2.2, 3.3, missing, 5.5])
5×2 DataFrames.DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ missing │ 3.3 │
│ 4 │ missing │ missing │
│ 5 │ 5.0 │ 5.5 │

julia> Impute.fill(df; value=-1.0)
5×2 DataFrames.DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ -1.0 │ 3.3 │
│ 4 │ -1.0 │ -1.0 │
│ 5 │ 5.0 │ 5.5 │
```
""" fill

@doc """
Impute.locf(data; dims=1)

Iterates forwards through the `data` and fills missing data with the last existing
observation. See [LOCF](@ref) for details.

# Example
```jldoctest
julia> using DataFrames; using Impute: Impute

julia> df = DataFrame(:a => [1.0, 2.0, missing, missing, 5.0], :b => [1.1, 2.2, 3.3, missing, 5.5])
5×2 DataFrames.DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ missing │ 3.3 │
│ 4 │ missing │ missing │
│ 5 │ 5.0 │ 5.5 │

julia> Impute.locf(df)
5×2 DataFrames.DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ 2.0 │ 3.3 │
│ 4 │ 2.0 │ 3.3 │
│ 5 │ 5.0 │ 5.5 │
```
""" locf

@doc """
Impute.nocb(data; dims=1)

Iterates backwards through the `data` and fills missing data with the next existing
observation. See [LOCF](@ref) for details.

# Example
```jldoctest
julia> using DataFrames; using Impute: Impute

julia> df = DataFrame(:a => [1.0, 2.0, missing, missing, 5.0], :b => [1.1, 2.2, 3.3, missing, 5.5])
5×2 DataFrames.DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ missing │ 3.3 │
│ 4 │ missing │ missing │
│ 5 │ 5.0 │ 5.5 │

julia> Impute.nocb(df)
5×2 DataFrames.DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ 5.0 │ 3.3 │
│ 4 │ 5.0 │ 5.5 │
│ 5 │ 5.0 │ 5.5 │
```
""" nocb

@doc """
Impute.srs(data; rng=Random.GLOBAL_RNG)

Simple Random Sampling (SRS) imputation is a method for imputing both continuous and
categorical variables. Furthermore, it completes imputation while preserving the
distributional properties of the variables (e.g., mean, standard deviation).

# Example
```jldoctest
julia> using DataFrames; using Random; using Impute: Impute

julia> df = DataFrame(:a => [1.0, 2.0, missing, missing, 5.0], :b => [1.1, 2.2, 3.3, missing, 5.5])
5×2 DataFrames.DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ missing │ 3.3 │
│ 4 │ missing │ missing │
│ 5 │ 5.0 │ 5.5 │

julia> Impute.srs(df; rng=MersenneTwister(1234))
5×2 DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 1.0 │ 1.1 │
│ 2 │ 2.0 │ 2.2 │
│ 3 │ 1.0 │ 3.3 │
│ 4 │ 5.0 │ 3.3 │
│ 5 │ 5.0 │ 5.5 │
```
""" srs

end # module
Loading