Skip to content

Commit

Permalink
Added some more tests for Chain and mutating methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
rofinn committed Jul 7, 2019
1 parent 1f99bbd commit 20f084e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ function impute!(data, missing::Function, method::Symbol, args...; limit::Float6
)
imputor_type = imputation_methods[method]
imputor = if length(args) > 0
imputor_type(args...; context=Context(; limit=limit))
imputor_type(args...; context=Context(; is_missing=missing, limit=limit))
else
imputor_type(; context=Context(; limit=limit))
imputor_type(; context=Context(; is_missing=missing, limit=limit))
end

return impute!(imputor, data)
Expand Down Expand Up @@ -160,11 +160,11 @@ Base.@deprecate Fill(val; kwargs...) Fill(; value=val, kwargs...)
# This function is just used to support legacy behaviour and should be removed in a
# future release when we dropping accepting the limit kwarg to impute functions.
function _extract_context_kwargs(kwargs...)
d = Dict(kwargs...)
d = Dict{Symbol, Any}(kwargs...)
limit = 1.0

if haskey(d, :limit)
warn(
@warn(
"Passing `limit` directly to impute functions is deprecated. " *
"Please pass a `context` in the future."
)
Expand Down
22 changes: 22 additions & 0 deletions test/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@
deleteat!(expected, [2, 3, 7])

@test result == expected

# Mutating method
a2 = copy(a)
Impute.drop!(a2; limit=0.2)
@test a2 == expected
end

@testset "Interpolate" begin
result = impute(a, :interp; limit=0.2)
@test result == collect(1.0:1.0:20)
@test result == interp(a)

# Test in-place method
a2 = copy(a)
Impute.interp!(a2; limit=0.2)
@test a2 == result

# Test interpolation between identical points
b = ones(Union{Float64, Missing}, 20)
b[[2, 3, 7]] .= missing
Expand Down Expand Up @@ -45,6 +55,10 @@
expected[[2, 3, 7]] .= mean(a[mask])

@test result == expected

a2 = copy(a)
Impute.fill!(a2; limit=0.2)
@test a2 == result
end
end

Expand All @@ -56,6 +70,9 @@
expected[7] = 6.0

@test result == expected
a2 = copy(a)
impute!(a2, :locf; limit=0.2)
@test a2 == result
end

@testset "NOCB" begin
Expand All @@ -66,6 +83,9 @@
expected[7] = 8.0

@test result == expected
a2 = copy(a)
Impute.nocb!(a2; limit=0.2)
@test a2 == result
end

@testset "DataFrame" begin
Expand Down Expand Up @@ -148,5 +168,7 @@
result1 = chain(data1, Impute.Interpolate(), Impute.Drop(); limit=1.0)
result2 = chain(data2, isnan, Impute.Interpolate(), Impute.Drop(); limit=1.0)
@test result1 == result2

@test Impute.drop(data1; limit=1.0) == impute(data2, isnan, :drop; limit=1.0)
end
end
52 changes: 45 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ import Impute: Drop, Interpolate, Fill, LOCF, NOCB, Context, WeightedContext, Im

@test result == expected
@test result == Impute.drop(a; context=ctx)

a2 = copy(a)
Impute.drop!(a2; context=ctx)
@test a2 == expected
end

@testset "Interpolate" begin
result = impute(Interpolate(; context=ctx), a)
@test result == collect(1.0:1.0:20)
@test result == interp(a; context=ctx)

# Test in-place method
a2 = copy(a)
Impute.interp!(a2; context=ctx)
@test a2 == result

# Test interpolation between identical points
b = ones(Union{Float64, Missing}, 20)
b[[2, 3, 7]] .= missing
Expand Down Expand Up @@ -59,6 +68,10 @@ import Impute: Drop, Interpolate, Fill, LOCF, NOCB, Context, WeightedContext, Im

@test result == expected
@test result == Impute.fill(a; value=mean, context=ctx)

a2 = copy(a)
Impute.fill!(a2; context=ctx)
@test a2 == result
end
end

Expand All @@ -71,6 +84,10 @@ import Impute: Drop, Interpolate, Fill, LOCF, NOCB, Context, WeightedContext, Im

@test result == expected
@test result == Impute.locf(a; context=ctx)

a2 = copy(a)
Impute.locf!(a2; context=ctx)
@test a2 == result
end

@testset "NOCB" begin
Expand All @@ -82,6 +99,10 @@ import Impute: Drop, Interpolate, Fill, LOCF, NOCB, Context, WeightedContext, Im

@test result == expected
@test result == Impute.nocb(a; context=ctx)

a2 = copy(a)
Impute.nocb!(a2; context=ctx)
@test a2 == result
end

@testset "DataFrame" begin
Expand All @@ -104,6 +125,10 @@ import Impute: Drop, Interpolate, Fill, LOCF, NOCB, Context, WeightedContext, Im
result = impute(Fill(; value=0.0, context=ctx), data)
@test size(result) == size(data)
@test result == Impute.fill(data; value=0.0, context=ctx)

data2 = copy(data)
Impute.fill!(data2; value=0.0, context=ctx)
@test data2 == result
end
end

Expand All @@ -118,18 +143,31 @@ import Impute: Drop, Interpolate, Fill, LOCF, NOCB, Context, WeightedContext, Im
ctx = Context(; limit=1.0)

@testset "DataFrame" begin
result = Impute.interp(orig; context=ctx) |> Impute.locf() |> Impute.nocb()
result = Impute.interp(orig; context=ctx) |> Impute.locf!() |> Impute.nocb!()

@test size(result) == size(orig)
# Confirm that we don't have any more missing values
@test !any(ismissing, Matrix(result))


# We can also use the Chain type with explicit Imputor types
result2 = impute(
Impute.Chain(
Impute.Interpolate(; context=ctx),
Impute.LOCF(),
Impute.NOCB()
),
orig,
)

@test result == result2
end

@testset "Column Table" begin
result = Tables.columntable(orig) |>
Impute.interp(; context=ctx) |>
Impute.locf() |>
Impute.nocb() |>
Impute.interp!(; context=ctx) |>
Impute.locf!() |>
Impute.nocb!() |>
Tables.matrix

@test size(result) == size(orig)
Expand All @@ -139,7 +177,7 @@ import Impute: Drop, Interpolate, Fill, LOCF, NOCB, Context, WeightedContext, Im

@testset "Matrix" begin
data = Matrix(orig)
result = Impute.interp(data; context=ctx) |> Impute.locf() |> Impute.nocb()
result = Impute.interp(data; context=ctx) |> Impute.locf!() |> Impute.nocb!()

@test size(result) == size(data)
# Confirm that we don't have any more missing values
Expand All @@ -155,8 +193,8 @@ import Impute: Drop, Interpolate, Fill, LOCF, NOCB, Context, WeightedContext, Im

@test Impute.drop(data1; context=ctx1) == dropmissing(data1)

result1 = Impute.interp(data1; context=ctx1) |> Impute.drop()
result2 = Impute.interp(data2; context=ctx2) |> Impute.drop(; context=ctx2)
result1 = Impute.interp(data1; context=ctx1) |> Impute.drop!()
result2 = Impute.interp(data2; context=ctx2) |> Impute.drop!(; context=ctx2)

@test result1 == result2
end
Expand Down

0 comments on commit 20f084e

Please sign in to comment.