From aabe774d2e52840fb8fa0c32d9f7129e6b324df0 Mon Sep 17 00:00:00 2001 From: rofinn Date: Sun, 7 Jul 2019 13:07:02 -0500 Subject: [PATCH] Added some more tests for Chain and mutating methods. --- test/deprecated.jl | 19 +++++++++++++++++ test/runtests.jl | 52 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/test/deprecated.jl b/test/deprecated.jl index 8c80832..b56a3a2 100644 --- a/test/deprecated.jl +++ b/test/deprecated.jl @@ -9,6 +9,10 @@ deleteat!(expected, [2, 3, 7]) @test result == expected + + a2 = copy(a) + impute!(a2, :drop; limit=0.2) + @test a2 == expected end @testset "Interpolate" begin @@ -16,6 +20,11 @@ @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 @@ -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 @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index c5b7f55..085d643 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -21,6 +21,10 @@ 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 @@ -28,6 +32,11 @@ import Impute: Drop, Interpolate, Fill, LOCF, NOCB, Context, WeightedContext, Im @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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 @@ -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