Skip to content

Commit

Permalink
Updated tests to new API and moved existing deprecated tests to a dif…
Browse files Browse the repository at this point in the history
…ferent file.
  • Loading branch information
rofinn committed Jul 5, 2019
1 parent 0b2bbe7 commit 1f99bbd
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 63 deletions.
8 changes: 4 additions & 4 deletions src/Impute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ let

# NOTE: The
@eval begin
$f(data; kwargs...) = impute($typename(; context=Context(Dict(kwargs...))), data)
$f!(data; kwargs...) = impute!($typename(; context=Context(Dict(kwargs...))), data)
$f(; kwargs...) = data -> impute($typename(; context=Context(Dict(kwargs...))), data)
$f!(; kwargs...) = data -> impute!($typename(; context=Context(Dict(kwargs...))), data)
$f(data; kwargs...) = impute($typename(; _extract_context_kwargs(kwargs...)...), data)
$f!(data; kwargs...) = impute!($typename(; _extract_context_kwargs(kwargs...)...), data)
$f(; kwargs...) = data -> impute($typename(; _extract_context_kwargs(kwargs...)...), data)
$f!(; kwargs...) = data -> impute!($typename(; _extract_context_kwargs(kwargs...)...), data)
end
end
end
Expand Down
16 changes: 0 additions & 16 deletions src/context.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,6 @@ function Context(;
Context(0, 0, limit, is_missing, on_complete)
end

# The constructor only exists for legacy reasons
# We should drop this when we're ready to stop accepting limit in
# arbitrary impute functions.
function Context(d::Dict)
if haskey(d, :context)
return d[:context]
else haskey(d, :limit)
return Context(;
# We using a different default limit value here for legacy reason.
limit=get(d, :limit, 1.0),
is_missing=get(d, :is_missing, ismissing),
on_complete=get(d, :on_complete, complete),
)
end
end

function (ctx::Context)(f::Function)
_ctx = copy(ctx)
_ctx.num = 0
Expand Down
23 changes: 23 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,26 @@ end
# Misc Deprecations #
#####################
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...)
limit = 1.0

if haskey(d, :limit)
warn(
"Passing `limit` directly to impute functions is deprecated. " *
"Please pass a `context` in the future."
)

limit = d[:limit]
delete!(d, :limit)
end

if !haskey(d, :context)
d[:context] = Context(; limit=limit)
end

return d
end
152 changes: 152 additions & 0 deletions test/deprecated.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
@testset "deprecated" begin
a = Vector{Union{Float64, Missing}}(1.0:1.0:20.0)
a[[2, 3, 7]] .= missing
mask = map(!ismissing, a)

@testset "Drop" begin
result = impute(a, :drop; limit=0.2)
expected = copy(a)
deleteat!(expected, [2, 3, 7])

@test result == 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 interpolation between identical points
b = ones(Union{Float64, Missing}, 20)
b[[2, 3, 7]] .= missing
@test interp(b) == ones(Union{Float64, Missing}, 20)

# Test interpolation at endpoints
b = ones(Union{Float64, Missing}, 20)
b[[1, 3, 20]] .= missing
result = interp(b)
@test ismissing(result[1])
@test ismissing(result[20])
end

@testset "Fill" begin
@testset "Value" begin
fill_val = -1.0
result = impute(a, :fill, fill_val; limit=0.2)
expected = copy(a)
expected[[2, 3, 7]] .= fill_val

@test result == expected
end

@testset "Mean" begin
result = impute(a, :fill; limit=0.2)
expected = copy(a)
expected[[2, 3, 7]] .= mean(a[mask])

@test result == expected
end
end

@testset "LOCF" begin
result = impute(a, :locf; limit=0.2)
expected = copy(a)
expected[2] = 1.0
expected[3] = 1.0
expected[7] = 6.0

@test result == expected
end

@testset "NOCB" begin
result = impute(a, :nocb; limit=0.2)
expected = copy(a)
expected[2] = 4.0
expected[3] = 4.0
expected[7] = 8.0

@test result == expected
end

@testset "DataFrame" begin
data = dataset("boot", "neuro")
df = impute(data, :interp; limit=1.0)
end

@testset "Matrix" begin
data = Matrix(dataset("boot", "neuro"))

@testset "Drop" begin
result = Iterators.drop(data)
@test size(result, 1) == 4
end

@testset "Fill" begin
result = impute(data, :fill, 0.0; limit=1.0)
@test size(result) == size(data)
end
end

@testset "Not enough data" begin
@test_throws ImputeError impute(a, :drop)
end

@testset "Chain" begin
orig = dataset("boot", "neuro")

@testset "DataFrame" begin
result = chain(
orig,
Impute.Interpolate(),
Impute.LOCF(),
Impute.NOCB();
limit=1.0
)

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

@testset "Column Table" begin
data = Tables.columntable(orig)
result = chain(
data,
Impute.Interpolate(),
Impute.LOCF(),
Impute.NOCB();
limit=1.0
) |> Tables.matrix

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

@testset "Matrix" begin
data = Matrix(orig)
result = chain(
data,
Impute.Interpolate(),
Impute.LOCF(),
Impute.NOCB();
limit=1.0
)

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

@testset "Alternate missing functions" begin
data1 = dataset("boot", "neuro") # Missing values with `missing`
data2 = impute(data1, :fill, NaN; limit=1.0) # Missing values with `NaN`

@test impute(data1, :drop; limit=1.0) == dropmissing(data1)

result1 = chain(data1, Impute.Interpolate(), Impute.Drop(); limit=1.0)
result2 = chain(data2, isnan, Impute.Interpolate(), Impute.Drop(); limit=1.0)
@test result1 == result2
end
end
Loading

0 comments on commit 1f99bbd

Please sign in to comment.