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 aabe774
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
19 changes: 19 additions & 0 deletions test/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@
deleteat!(expected, [2, 3, 7])

@test result == expected

a2 = copy(a)
impute!(a2, :drop; 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!(a2, :interp; 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 +54,10 @@
expected[[2, 3, 7]] .= mean(a[mask])

@test result == expected

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

Expand All @@ -56,6 +69,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 +82,9 @@
expected[7] = 8.0

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

@testset "DataFrame" begin
Expand Down
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 aabe774

Please sign in to comment.