diff --git a/test/linear_combination.jl b/test/linear_combination.jl index 1d58b70..664f4f5 100644 --- a/test/linear_combination.jl +++ b/test/linear_combination.jl @@ -4,102 +4,23 @@ @test lc isa Transform @test cardinality(lc) == ManyToOne() - @testset "Vector" begin - - @testset "all inds" begin - x = [1, 2] - lc = LinearCombination([1, -1]) - @test FeatureTransforms.apply(x, lc) == fill(-1) - @test lc(x) == fill(-1) - end - - @testset "dims behaviour" begin - x = [1, 2] - lc = LinearCombination([1, -1]) - @test FeatureTransforms.apply(x, lc; dims=1) == fill(-1) - @test_throws BoundsError FeatureTransforms.apply(x, lc; dims=2) - end - - @testset "dimension mismatch" begin - x = [1, 2, 3] - lc = LinearCombination([1, -1]) - @test_throws DimensionMismatch FeatureTransforms.apply(x, lc) - end - - @testset "specified inds" begin - x = [1, 2, 3] - lc = LinearCombination([1, -1]) - @test FeatureTransforms.apply(x, lc; inds=[2, 3]) == fill(-1) - @test lc(x; inds=[2, 3]) == fill(-1) - end - - @testset "output is different type" begin - x = [1, 2] - lc = LinearCombination([.1, -.1]) - @test FeatureTransforms.apply(x, lc) == fill(-.1) - @test lc(x) == fill(-.1) - end - - @testset "apply_append" begin - x = [1, 2] - lc = LinearCombination([1, -1]) - @test FeatureTransforms.apply_append(x, lc; append_dim=1) == [1, 2, -1] - end + @testset "default reduces over columns" begin + M = [1 1; 2 2; 3 5] + lc = LinearCombination([1, -1, 1]) + @test FeatureTransforms.apply(M, lc) == [2, 4] + @test lc(M) == [2, 4] end - @testset "Matrix" begin - - @testset "default reduces over columns" begin - M = [1 1; 2 2; 3 5] - lc = LinearCombination([1, -1, 1]) - @test FeatureTransforms.apply(M, lc) == [2, 4] - @test lc(M) == [2, 4] - end - - @testset "dims" begin - @testset "dims = :" begin - M = [1 1; 2 2; 3 5] - lc = LinearCombination([1, -1, 1]) - @test_deprecated FeatureTransforms.apply(M, lc; dims=:) - end - - @testset "dims = 1" begin - M = [1 1; 2 2; 3 5] - lc = LinearCombination([1, -1, 1]) - @test FeatureTransforms.apply(M, lc; dims=1) == [2, 4] - @test lc(M; dims=1) == [2, 4] - end - - @testset "dims = 2" begin - M = [1 1; 2 2; 3 5] - lc = LinearCombination([1, -1]) - @test FeatureTransforms.apply(M, lc; dims=2) == [0, 0, -2] - end - end - - @testset "dimension mismatch" begin - M = [1 1 1; 2 2 2] - lc = LinearCombination([1, -1, 1]) # there are only 2 rows - @test_throws DimensionMismatch FeatureTransforms.apply(M, lc) - end - - @testset "specified inds" begin - M = [1 1; 5 2; 2 4] - lc = LinearCombination([1, -1]) - @test FeatureTransforms.apply(M, lc; inds=[2, 3]) == [3, -2] - @test lc(M; inds=[2, 3]) == [3, -2] - end - - @testset "apply_append" begin - M = [1 1 1; 2 2 2; 3 3 3] - lc = LinearCombination([1, 1, 1]) - - expected1 = [1 1 1; 2 2 2; 3 3 3; 6 6 6] - @test FeatureTransforms.apply_append(M, lc; dims=1, append_dim=1) == expected1 + @testset "dims = :" begin + M = [1 1; 2 2; 3 5] + lc = LinearCombination([1, -1, 1]) + @test_deprecated FeatureTransforms.apply(M, lc; dims=:) + end - expected2 = [1 1 1 3; 2 2 2 6; 3 3 3 9] - @test FeatureTransforms.apply_append(M, lc; dims=2, append_dim=2) == expected2 - end + @testset "dimension mismatch" begin + M = [1 1 1; 2 2 2] + lc = LinearCombination([1, -1, 1]) # there are only 2 rows + @test_throws DimensionMismatch FeatureTransforms.apply(M, lc) end @testset "N-dim Array" begin @@ -108,197 +29,4 @@ @test FeatureTransforms.apply(A, lc) == [2 11 20; 5 14 23; 8 17 26] @test lc(A) == [2 11 20; 5 14 23; 8 17 26] end - - @testset "AxisArray" begin - A = AxisArray([1 2; 4 5], foo=["a", "b"], bar=["x", "y"]) - lc = LinearCombination([1, -1]) - - @testset "all inds" begin - @test FeatureTransforms.apply(A, lc) == [-3, -3] - @test lc(A) == [-3, -3] - end - - @testset "dims" begin - @testset "dims = :" begin - @test_deprecated FeatureTransforms.apply(A, lc; dims=:) - end - - @testset "dims = 1" begin - @test FeatureTransforms.apply(A, lc; dims=1) == [-3, -3] - @test lc(A; dims=1) == [-3, -3] - end - - @testset "dims = 2" begin - @test FeatureTransforms.apply(A, lc; dims=2) == [-1, -1] - @test lc(A; dims=2) == [-1, -1] - end - end - - @testset "dimension mismatch" begin - A = AxisArray([1 2 3; 4 5 5], foo=["a", "b"], bar=["x", "y", "z"]) - @test_throws DimensionMismatch FeatureTransforms.apply(A, lc; dims=2) - end - - @testset "specified inds" begin - A = AxisArray([1 2 3; 4 5 5], foo=["a", "b"], bar=["x", "y", "z"]) - @test FeatureTransforms.apply(A, lc; inds=[1, 2]) == [-3, -3, -2] - @test lc(A; inds=[1, 2]) == [-3, -3, -2] - end - - @testset "apply_append" begin - A = AxisArray([1 2; 4 5], foo=["a", "b"], bar=["x", "y"]) - - expected1 = [1 2; 4 5; -3 -3] - @test FeatureTransforms.apply_append(A, lc; dims=1, append_dim=1) == expected1 - - expected2 = [1 2 -1; 4 5 -1] - @test FeatureTransforms.apply_append(A, lc; dims=2, append_dim=2) == expected2 - end - end - - @testset "AxisKey" begin - A = KeyedArray([1 2; 4 5], foo=["a", "b"], bar=["x", "y"]) - lc = LinearCombination([1, -1]) - - @testset "all inds" begin - @test FeatureTransforms.apply(A, lc) == [-3, -3] - @test lc(A) == [-3, -3] - end - - @testset "dims" begin - @testset "dims = :" begin - @test_deprecated FeatureTransforms.apply(A, lc; dims=:) - end - - @testset "dims = 1" begin - @test FeatureTransforms.apply(A, lc; dims=1) == [-3, -3] - @test FeatureTransforms.apply(A, lc; dims=:foo) == [-3, -3] - @test lc(A; dims=1) == [-3, -3] - end - - @testset "dims = 2" begin - @test FeatureTransforms.apply(A, lc; dims=2) == [-1, -1] - @test FeatureTransforms.apply(A, lc; dims=:bar) == [-1, -1] - @test lc(A; dims=2) == [-1, -1] - end - end - - @testset "dimension mismatch" begin - A = KeyedArray([1 2 3; 4 5 6], foo=["a", "b"], bar=["x", "y", "z"]) - @test_throws DimensionMismatch FeatureTransforms.apply(A, lc; dims=2) - end - - @testset "specified inds" begin - A = KeyedArray([1 2 3; 4 5 5], foo=["a", "b"], bar=["x", "y", "z"]) - @test FeatureTransforms.apply(A, lc; inds=[1, 2]) == [-3, -3, -2] - @test lc(A; inds=[1, 2]) == [-3, -3, -2] - end - - @testset "apply_append" begin - A = KeyedArray([1 2; 4 5], foo=["a", "b"], bar=["x", "y"]) - - expected1 = [1 2; 4 5; -3 -3] - @test FeatureTransforms.apply_append(A, lc; dims=:foo, append_dim=:foo) == expected1 - - expected2 = [1 2 -1; 4 5 -1] - @test FeatureTransforms.apply_append(A, lc; dims=:bar, append_dim=:bar) == expected2 - end - end - - @testset "NamedTuple" begin - @testset "all cols" begin - nt = (a = [1, 2, 3], b = [4, 5, 6]) - lc = LinearCombination([1, -1]) - expected = (Column1 = [-3, -3, -3],) - @test FeatureTransforms.apply(nt, lc) == expected - @test lc(nt) == expected - end - - @testset "custom header" begin - nt = (a = [1, 2, 3], b = [4, 5, 6]) - lc = LinearCombination([1, -1]) - expected = (x = [-3, -3, -3],) - @test FeatureTransforms.apply(nt, lc; header=[:x]) == expected - @test lc(nt; header=[:x]) == expected - end - - @testset "dimension mismatch" begin - nt = (a = [1, 2, 3], b = [4, 5, 6], c = [1, 1, 1]) - lc = LinearCombination([1, -1]) - @test_throws DimensionMismatch FeatureTransforms.apply(nt, lc) - end - - @testset "specified cols" begin - nt = (a = [1, 2, 3], b = [4, 5, 6], c = [1, 1, 1]) - lc = LinearCombination([1, -1]) - expected = (Column1 = [-3, -3, -3],) - @test FeatureTransforms.apply(nt, lc; cols=[:a, :b]) == expected - @test lc(nt; cols=[:a, :b]) == expected - end - - @testset "single col" begin - nt = (a = [1, 2, 3], b = [4, 5, 6]) - lc_single = LinearCombination([-1]) - expected = (Column1 = [-1, -2, -3],) - @test FeatureTransforms.apply(nt, lc_single; cols=:a) == expected - @test FeatureTransforms.apply(nt, lc_single; cols=[:a]) == expected - @test lc_single(nt; cols=:a) == expected - end - - @testset "apply_append" begin - nt = (a = [1, 2, 3], b = [4, 5, 6]) - lc = LinearCombination([1, -1]) - expected = (a = [1, 2, 3], b = [4, 5, 6], Column1 = [-3, -3, -3]) - @test FeatureTransforms.apply_append(nt, lc) == expected - end - end - - @testset "DataFrame" begin - - @testset "all cols" begin - df = DataFrame(:a => [1, 2, 3], :b => [4, 5, 6]) - lc = LinearCombination([1, -1]) - @test FeatureTransforms.apply(df, lc) == DataFrame(:Column1 => [-3, -3, -3]) - @test lc(df) == DataFrame(:Column1 => [-3, -3, -3]) - end - - @testset "custom header" begin - df = DataFrame(:a => [1, 2, 3], :b => [4, 5, 6]) - lc = LinearCombination([1, -1]) - expected = DataFrame(:x => [-3, -3, -3]) - @test FeatureTransforms.apply(df, lc; header=[:x]) == expected - @test lc(df; header=[:x]) == expected - end - - @testset "dimension mismatch" begin - df = DataFrame(:a => [1, 2, 3], :b => [4, 5, 6], :c => [1, 1, 1]) - lc = LinearCombination([1, -1]) - @test_throws DimensionMismatch FeatureTransforms.apply(df, lc) - end - - @testset "specified cols" begin - df = DataFrame(:a => [1, 2, 3], :b => [4, 5, 6], :c => [1, 1, 1]) - lc = LinearCombination([1, -1]) - expected = DataFrame(:Column1 => [3, 4, 5]) - - @test FeatureTransforms.apply(df, lc; cols=[:b, :c]) == expected - @test lc(df; cols=[:b, :c]) == expected - end - - @testset "single col" begin - df = DataFrame(:a => [1, 2, 3], :b => [4, 5, 6]) - lc_single = LinearCombination([-1]) - expected = DataFrame(:Column1 => [-1, -2, -3]) - @test FeatureTransforms.apply(df, lc_single; cols=:a) == expected - @test FeatureTransforms.apply(df, lc_single; cols=[:a]) == expected - @test lc_single(df; cols=:a) == expected - end - - @testset "apply_append" begin - df = DataFrame(:a => [1, 2, 3], :b => [4, 5, 6]) - lc = LinearCombination([1, -1]) - expected = DataFrame(:a => [1, 2, 3], :b => [4, 5, 6], :Column1 => [-3, -3, -3]) - @test FeatureTransforms.apply_append(df, lc) == expected - end - end end diff --git a/test/one_hot_encoding.jl b/test/one_hot_encoding.jl index 4ad47af..e015bbb 100644 --- a/test/one_hot_encoding.jl +++ b/test/one_hot_encoding.jl @@ -5,17 +5,7 @@ @test ohe isa Transform @test cardinality(ohe) == OneToMany() - @testset "Vector" begin - - @testset "simple" begin - x = ["foo", "bar", "baz"] - expected = [1 0 0; 0 1 0; 0 0 1] - - transformed = FeatureTransforms.apply(x, ohe) - @test transformed == expected - @test transformed isa AbstractMatrix{Bool} - @test ohe(x) == expected - end + @testset "Basic" begin @testset "specify return type" begin x = ["foo", "bar", "baz"] @@ -38,160 +28,5 @@ x = ["foo", "baz", "bar", "dne"] @test_throws KeyError FeatureTransforms.apply(x, ohe) end - - @testset "inds" begin - x = ["foo", "baz", "bar", "baz"] - expected = [1 0 0; 0 0 1; 0 1 0; 0 0 1] - - @test FeatureTransforms.apply(x, ohe; inds=2:4) == [0 0 1; 0 1 0; 0 0 1] - @test FeatureTransforms.apply(x, ohe; dims=:) == expected - - @test FeatureTransforms.apply(x, ohe; dims=1) == expected - @test FeatureTransforms.apply(x, ohe; dims=1, inds=[2, 4]) == [0 0 1; 0 0 1] - - @test_throws BoundsError FeatureTransforms.apply(x, ohe; dims=2) - end - - @testset "apply_append" begin - x = ["foo", "baz", "bar", "baz"] - expected = ["foo" 1 0 0; "baz" 0 0 1; "bar" 0 1 0; "baz" 0 0 1] - @test FeatureTransforms.apply_append(x, ohe; append_dim=2) == expected - end - end - - - @testset "Matrix" begin - categories = ["foo", "bar", "baz", "foo2", "bar2"] - ohe = OneHotEncoding(categories) - - M = ["foo" "bar"; "foo2" "bar2"] - expected = [1 0 0 0 0; 0 0 0 1 0; 0 1 0 0 0; 0 0 0 0 1] - - @test FeatureTransforms.apply(M, ohe) == expected - - @testset "dims=:$d" for d in (1, 2, Colon()) - @test FeatureTransforms.apply(M, ohe; dims=d) == expected - end - - @testset "inds" begin - @test FeatureTransforms.apply(M, ohe; inds=[2, 3]) == [0 0 0 1 0; 0 1 0 0 0] - @test FeatureTransforms.apply(M, ohe; dims=:, inds=[2, 3]) == [0 0 0 1 0; 0 1 0 0 0] - end - - @testset "apply_append" begin - M = ["foo" "bar"; "foo2" "bar2"] - @test_throws DimensionMismatch FeatureTransforms.apply_append(M, ohe; append_dim=1) - @test_throws DimensionMismatch FeatureTransforms.apply_append(M, ohe; append_dim=2) - end - end - - @testset "AxisArray" begin - categories = ["foo", "bar", "baz", "foo2", "bar2"] - ohe = OneHotEncoding(categories) - - M = ["foo" "bar"; "foo2" "bar2"] - A = AxisArray(M, foo=["a", "b"], bar=["x", "y"]) - expected = [1 0 0 0 0; 0 0 0 1 0; 0 1 0 0 0; 0 0 0 0 1] - - @testset "dims = $d" for d in (1, 2, Colon()) - transformed = FeatureTransforms.apply(A, ohe; dims=d) - # AxisArray doesn't preserve the type it operates on - @test transformed isa AbstractArray - @test transformed == expected - end - - @testset "inds" begin - @test FeatureTransforms.apply(A, ohe; inds=[2, 3]) == [0 0 0 1 0; 0 1 0 0 0] - @test FeatureTransforms.apply(A, ohe; dims=:, inds=[2, 3]) == [0 0 0 1 0; 0 1 0 0 0] - end - - @testset "apply_append" begin - M = ["foo" "bar"; "foo2" "bar2"] - A = AxisArray(M, foo=["a", "b"], bar=["x", "y"]) - @test_throws DimensionMismatch FeatureTransforms.apply_append(A, ohe; append_dim=1) - @test_throws DimensionMismatch FeatureTransforms.apply_append(A, ohe; append_dim=2) - end - end - - @testset "AxisKey" begin - categories = ["foo", "bar", "baz", "foo2", "bar2"] - ohe = OneHotEncoding(categories) - - M = ["foo" "bar"; "foo2" "bar2"] - A = KeyedArray(M, foo=["a", "b"], bar=["x", "y"]) - expected = [1 0 0 0 0; 0 0 0 1 0; 0 1 0 0 0; 0 0 0 0 1] - - @testset "dims = $d" for d in (1, 2, Colon(), :foo, :bar) - transformed = FeatureTransforms.apply(A, ohe; dims=d) - # This transform doesn't preserve the type it operates on - @test transformed isa AbstractArray - @test transformed == expected - end - - @testset "inds" begin - @test FeatureTransforms.apply(A, ohe; inds=[2, 3]) == [0 0 0 1 0; 0 1 0 0 0] - @test FeatureTransforms.apply(A, ohe; dims=:, inds=[2, 3]) == [0 0 0 1 0; 0 1 0 0 0] - end - - @testset "apply_append" begin - M = ["foo" "bar"; "foo2" "bar2"] - A = KeyedArray(M, foo=["a", "b"], bar=["x", "y"]) - @test_throws DimensionMismatch FeatureTransforms.apply_append(A, ohe; append_dim=:foo) - @test_throws DimensionMismatch FeatureTransforms.apply_append(A, ohe; append_dim=:bar) - end - end - - @testset "NamedTuple" begin - categories = ["foo", "bar", "baz", "foo2", "bar2"] - ohe = OneHotEncoding(categories) - nt = (a = ["foo", "bar"], b = ["foo2", "bar2"]) - - @testset "all cols" begin - expected = NamedTuple{Tuple(Symbol.(:Column, x) for x in 1:5)}( - eachcol(Bool[1 0 0 0 0; 0 1 0 0 0; 0 0 0 1 0; 0 0 0 0 1]) - ) - @test FeatureTransforms.apply(nt, ohe) == expected - @test ohe(nt) == expected - end - - @testset "cols = :a" begin - expected = NamedTuple{Tuple(Symbol.(:Column, x) for x in 1:5)}( - ([1, 0], [0, 1], [0, 0], [0, 0], [0, 0]) - ) - @test FeatureTransforms.apply(nt, ohe; cols=[:a]) == expected - @test FeatureTransforms.apply(nt, ohe; cols=:a) == expected - @test ohe(nt; cols=:a) == expected - end - - @testset "apply_append" begin - nt = (a = ["foo", "bar"], b = ["foo2", "bar2"]) - @test FeatureTransforms.apply_append(nt, ohe) == merge(nt, ohe(nt)) - end - end - - @testset "DataFrame" begin - categories = ["foo", "bar", "baz", "foo2", "bar2"] - ohe = OneHotEncoding(categories) - - df = DataFrame(:a => ["foo", "bar"], :b => ["foo2", "bar2"]) - expected = DataFrame( - Bool[1 0 0 0 0; 0 1 0 0 0; 0 0 0 1 0; 0 0 0 0 1], - [Symbol.(:Column, x) for x in 1:5], - ) - - @test FeatureTransforms.apply(df, ohe) == expected - - @test FeatureTransforms.apply(df, ohe; cols=[:a]) == expected[1:2, :] - @test FeatureTransforms.apply(df, ohe; cols=:a) == expected[1:2, :] - - expected = DataFrame( - [[false, false], [false, false], [false, false], [true, false], [false, true]], - [Symbol.(:Column, x) for x in 1:5], - ) - @test FeatureTransforms.apply(df, ohe; cols=[:b]) == expected - - @testset "apply_append" begin - @test_throws DimensionMismatch FeatureTransforms.apply_append(df, ohe) - end end end diff --git a/test/periodic.jl b/test/periodic.jl index a6d82a2..089e83e 100644 --- a/test/periodic.jl +++ b/test/periodic.jl @@ -78,241 +78,6 @@ @test p(x) ≈ [0.0, 1.0, -1.0] atol=1e-14 end end - - expected_dict = Dict( - sin => [ - -0.5877852522924732, - -0.9510565162951535, - 0.0, - 0.9510565162951535, - 0.5877852522924732, - -0.58778525229247 - ], - cos => [ - -0.8090169943749473, - 0.30901699437494745, - 1.0, - 0.30901699437494745, - -0.8090169943749473, - -0.8090169943749475 - ] - ) - - @testset "$f" for f in (sin, cos) - expected = expected_dict[f] - p = Periodic(f, 5, 2) - - @testset "Vector" begin - x = collect(0.:5.) - - @test FeatureTransforms.apply(x, p) ≈ expected atol=1e-14 - @test p(x) ≈ expected atol=1e-14 - - _x = copy(x) - FeatureTransforms.apply!(_x, p) - @test _x ≈ expected atol=1e-14 - - @testset "inds" begin - @test FeatureTransforms.apply(x, p; inds=2:5) ≈ expected[2:5] atol=1e-14 - @test FeatureTransforms.apply(x, p; dims=:) ≈ expected atol=1e-14 - @test FeatureTransforms.apply(x, p; dims=1) ≈ expected atol=1e-14 - @test FeatureTransforms.apply(x, p; dims=1, inds=[2, 3, 4, 5]) ≈ expected[2:5] atol=1e-14 - - @test_throws BoundsError FeatureTransforms.apply(x, p; dims=2) - end - - @testset "append_apply" begin - @test FeatureTransforms.apply_append(x, p, append_dim=1) ≈ vcat(x, expected) atol=1e-14 - end - end - - @testset "Matrix" begin - M = reshape(0.:5., (3, 2)) - M_expected = reshape(expected, (3, 2)) - - @testset "dims = $d" for d in (Colon(), 1, 2) - @test FeatureTransforms.apply(M, p; dims=d) ≈ M_expected atol=1e-14 - @test p(M; dims=d) ≈ M_expected atol=1e-14 - - _M = copy(M) - FeatureTransforms.apply!(_M, p; dims=d) - @test _M ≈ M_expected atol=1e-14 - end - - @testset "inds" begin - @test FeatureTransforms.apply(M, p; inds=[2, 3]) ≈ M_expected[[2, 3]] atol=1e-14 - @test FeatureTransforms.apply(M, p; dims=:, inds=[2, 3]) ≈ M_expected[[2, 3]] atol=1e-14 - @test FeatureTransforms.apply(M, p; dims=1, inds=[2]) ≈ reshape(M_expected[[2, 5]], 1, 2) atol=1e-14 - @test FeatureTransforms.apply(M, p; dims=2, inds=[2]) ≈ reshape(M_expected[[4, 5, 6]], 3, 1) atol=1e-14 - end - - @testset "append_apply" begin - exp1 = vcat(M, M_expected) - @test FeatureTransforms.apply_append(M, p, append_dim=1) ≈ exp1 atol=1e-14 - exp2 = hcat(M, M_expected) - @test FeatureTransforms.apply_append(M, p, append_dim=2) ≈ exp2 atol=1e-14 - exp3 = cat(M, M_expected, dims=3) - @test FeatureTransforms.apply_append(M, p, append_dim=3) ≈ exp3 atol=1e-14 - end - end - - @testset "AxisArray" begin - x = collect(0.:5.) - A = AxisArray(reshape(x, (3, 2)), foo=["a", "b", "c"], bar=["x", "y"]) - - A_expected = AxisArray( - reshape(expected, (3, 2)), - foo=["a", "b", "c"], - bar=["x", "y"] - ) - - @testset "dims = $d" for d in (Colon(), 1, 2) - transformed = FeatureTransforms.apply(A, p; dims=d) - # AxisArray doesn't preserve type when operations are performed on it - @test transformed isa AbstractArray - @test transformed ≈ A_expected atol=1e-14 - end - - _A = copy(A) - FeatureTransforms.apply!(_A, p) - @test _A isa AxisArray - @test _A ≈ A_expected atol=1e-14 - - @testset "inds" begin - @test FeatureTransforms.apply(A, p; inds=[2, 3]) ≈ A_expected[[2, 3]] atol=1e-14 - @test FeatureTransforms.apply(A, p; dims=:, inds=[2, 3]) ≈ A_expected[[2, 3]] atol=1e-14 - @test FeatureTransforms.apply(A, p; dims=1, inds=[2]) ≈ reshape(A_expected[[2, 5]], 1, 2) atol=1e-14 - @test FeatureTransforms.apply(A, p; dims=2, inds=[2]) ≈ reshape(A_expected[[4, 5, 6]], 3, 1) atol=1e-14 - end - - @testset "append_apply" begin - exp1 = vcat(A, A_expected) - @test FeatureTransforms.apply_append(A, p, append_dim=1) ≈ exp1 atol=1e-14 - exp2 = hcat(A, A_expected) - @test FeatureTransforms.apply_append(A, p, append_dim=2) ≈ exp2 atol=1e-14 - exp3 = cat(A, A_expected, dims=3) - @test FeatureTransforms.apply_append(A, p, append_dim=3) ≈ exp3 atol=1e-14 - end - end - - @testset "AxisKey" begin - x = collect(0.:5.) - A = KeyedArray(reshape(x, (3, 2)), foo=["a", "b", "c"], bar=["x", "y"]) - - A_expected = KeyedArray( - reshape(expected, (3, 2)), - foo=["a", "b", "c"], - bar=["x", "y"] - ) - - @testset "dims = $d" for d in (Colon(), :foo, :bar) - transformed = FeatureTransforms.apply(A, p; dims=d) - @test transformed isa KeyedArray - @test transformed ≈ A_expected atol=1e-14 - end - - _A = copy(A) - FeatureTransforms.apply!(_A, p) - @test _A ≈ A_expected atol=1e-14 - - @testset "inds" begin - @test FeatureTransforms.apply(A, p; inds=[2, 3]) ≈ [A_expected[2], A_expected[3]] atol=1e-14 - @test FeatureTransforms.apply(A, p; dims=:, inds=[2, 3]) ≈ [A_expected[2], A_expected[3]] atol=1e-14 - @test FeatureTransforms.apply(A, p; dims=1, inds=[2]) ≈ reshape([A_expected[2], A_expected[5]], 1, 2) atol=1e-14 - @test FeatureTransforms.apply(A, p; dims=2, inds=[2]) ≈ reshape([A_expected[4], A_expected[5], A_expected[6]], 3, 1) atol=1e-14 - end - - @testset "append_apply" begin - M = reshape(x, (3, 2)) - M_expected = reshape(expected, (3, 2)) - - exp1 = KeyedArray( - vcat(M, M_expected), foo=["a", "b", "c", "a", "b", "c"], bar=["x", "y"] - ) - @test FeatureTransforms.apply_append(A, p, append_dim=:foo) ≈ exp1 atol=1e-14 - - exp2 = KeyedArray( - hcat(M, M_expected), foo=["a", "b", "c"], bar=["x", "y", "x", "y"] - ) - @test FeatureTransforms.apply_append(A, p, append_dim=:bar) ≈ exp2 atol=1e-14 - - exp3 = KeyedArray( - cat(M, M_expected, dims=3), - foo=["a", "b", "c"], - bar=["x", "y"], - baz=Base.OneTo(2) - ) - @test FeatureTransforms.apply_append(A, p, append_dim=:baz) ≈ exp3 atol=1e-14 - end - end - - @testset "NamedTuple" begin - nt = (a = collect(0.:2.), b = collect(3.:5.)) - nt_expected = (Column1 = expected[1:3], Column2 = expected[4:6]) - - @testset "all cols" begin - transformed = FeatureTransforms.apply(nt, p) - @test transformed isa NamedTuple{(:Column1, :Column2)} - @test collect(transformed) ≈ collect(nt_expected) atol=1e-14 - @test collect(p(nt)) ≈ collect(nt_expected) atol=1e-14 - - _nt = deepcopy(nt) - FeatureTransforms.apply!(_nt, p) - @test _nt isa NamedTuple{(:a, :b)} - @test collect(_nt) ≈ collect(nt_expected) atol=1e-14 - end - - @testset "cols = :a" begin - transformed = FeatureTransforms.apply(nt, p; cols=[:a]) - @test collect(transformed) ≈ [nt_expected.Column1] atol=1e-14 - @test collect(p(nt; cols=:a)) ≈ [nt_expected.Column1] atol=1e-14 - - @testset "mutating" for _c in (:a, [:a]) - _nt = deepcopy(nt) - FeatureTransforms.apply!(_nt, p; cols=_c) - @test _nt isa NamedTuple{(:a, :b)} # before applying `collect` - @test collect(_nt) ≈ [nt_expected.Column1, nt.b] atol=1e-14 - end - end - - @testset "append_apply" begin - result = FeatureTransforms.apply_append(nt, p) - @test result isa NamedTuple{(:a, :b, :Column1, :Column2)} - @test collect(result) ≈ collect(merge(nt, nt_expected)) atol=1e-14 - end - end - - @testset "DataFrame" begin - df = DataFrame(:a => collect(0.:2.), :b => collect(3.:5.)) - df_expected = DataFrame(:Column1 => expected[1:3], :Column2 => expected[4:6]) - - @testset "all cols" begin - @test FeatureTransforms.apply(df, p) ≈ df_expected atol=1e-14 - @test p(df) ≈ df_expected atol=1e-14 - - _df = deepcopy(df) - FeatureTransforms.apply!(_df, p) - @test _df isa DataFrame - @test _df ≈ DataFrame(:a=>df_expected.Column1, :b=>df_expected.Column2) atol=1e-14 - end - - @testset "cols = :a" begin - @test FeatureTransforms.apply(df, p; cols=[:a]) ≈ df_expected[!, [:Column1]] atol=1e-14 - @test FeatureTransforms.apply(df, p; cols=:a) ≈ df_expected[!, [:Column1]] atol=1e-14 - @test p(df; cols=:a) ≈ df_expected[!, [:Column1]] - end - - @testset "apply_append" begin - expected = DataFrame( - :a=>df.a, - :b=>df.b, - :Column1=>df_expected.Column1, - :Column2=>df_expected.Column2, - ) - @test FeatureTransforms.apply_append(df, p) ≈ expected atol=1e-14 - end - end - end end @testset "TimeType input" begin @@ -352,7 +117,7 @@ @testset "$f" for f in (sin, cos) p = Periodic(f, Day(5), Day(2)) - @testset "Vector" begin + @testset "Basic" begin x = ZonedDateTime(2020, 1, 1, tz"EST") .+ (Day(0):Day(1):Day(5)) # Use _periodic to get expected outputs because we test it elsewhere expected = _periodic.(f, x, Day(5), Day(2)) @@ -360,102 +125,6 @@ @test FeatureTransforms.apply(x, p) ≈ expected atol=1e-14 @test p(x) ≈ expected atol=1e-14 end - - @testset "Matrix" begin - x = ZonedDateTime(2020, 1, 1, tz"EST") .+ (Day(0):Day(1):Day(5)) - M = reshape(x, (3, 2)) - expected = _periodic.(f, x, Day(5), Day(2)) - expected = reshape(expected, (3, 2)) - - @testset "dims = $d" for d in (Colon(), 1, 2) - @test FeatureTransforms.apply(M, p; dims=d) ≈ expected atol=1e-14 - @test p(M; dims=d) ≈ expected atol=1e-14 - end - end - - @testset "AxisArray" begin - x = ZonedDateTime(2020, 1, 1, tz"EST") .+ (Day(0):Day(1):Day(5)) - A = AxisArray( - reshape(x, (3, 2)), - foo=["a", "b", "c"], - bar=["x", "y"] - ) - - expected = _periodic.(f, x, Day(5), Day(2)) - expected = AxisArray( - reshape(expected, (3, 2)), - foo=["a", "b", "c"], - bar=["x", "y"] - ) - - @testset "dims = $d" for d in (Colon(), 1, 2) - transformed = FeatureTransforms.apply(A, p; dims=d) - # AxisArray doesn't preserve type when operations are performed on it - @test transformed isa AbstractArray - @test transformed ≈ expected atol=1e-14 - end - end - - @testset "AxisKey" begin - x = ZonedDateTime(2020, 1, 1, tz"EST") .+ (Day(0):Day(1):Day(5)) - A = KeyedArray( - reshape(x, (3, 2)), - foo=["a", "b", "c"], - bar=["x", "y"] - ) - - expected = _periodic.(f, x, Day(5), Day(2)) - expected = KeyedArray( - reshape(expected, (3, 2)), - foo=["a", "b", "c"], - bar=["x", "y"] - ) - - @testset "dims = $d" for d in (Colon(), :foo, :bar, 1, 2) - transformed = FeatureTransforms.apply(A, p; dims=d) - @test transformed isa KeyedArray - @test transformed ≈ expected atol=1e-14 - end - end - - @testset "NamedTuple" begin - x = ZonedDateTime(2020, 1, 1, tz"EST") .+ (Day(0):Day(1):Day(5)) - nt = (a = x[1:3], b = x[4:6]) - expected = ( - Column1 = _periodic.(f, x[1:3], Day(5), Day(2)), - Column2 = _periodic.(f, x[4:6], Day(5), Day(2)) - ) - - @testset "all cols" begin - transformed = FeatureTransforms.apply(nt, p) - @test collect(transformed) ≈ collect(expected) atol=1e-14 - @test collect(p(nt)) ≈ collect(expected) atol=1e-14 - end - - @testset "cols = :a" begin - @test collect(FeatureTransforms.apply(nt, p; cols=[:a])) ≈ [expected.Column1] atol=1e-14 - @test collect(FeatureTransforms.apply(nt, p; cols=:a)) ≈ [expected.Column1] atol=1e-14 - @test collect(p(nt; cols=:a)) ≈ [expected.Column1] atol=1e-14 - end - end - - @testset "DataFrame" begin - x = ZonedDateTime(2020, 1, 1, tz"EST") .+ (Day(0):Day(1):Day(5)) - df = DataFrame(:a => x[1:3], :b => x[4:6]) - df_expected = DataFrame( - :Column1 => _periodic.(f, x[1:3], Day(5), Day(2)), - :Column2 => _periodic.(f, x[4:6], Day(5), Day(2)) - ) - - transformed = FeatureTransforms.apply(df, p) - @test transformed ≈ df_expected atol=1e-14 - - @testset "cols = :a" begin - @test FeatureTransforms.apply(df, p; cols=[:a]) ≈ df_expected[!, [:Column1]] atol=1e-14 - @test FeatureTransforms.apply(df, p; cols=:a) ≈ df_expected[!, [:Column1]] atol=1e-14 - @test p(df; cols=[:a]) ≈ df_expected[!, [:Column1]] atol=1e-14 - end - end end end diff --git a/test/power.jl b/test/power.jl index 2566289..04d2352 100644 --- a/test/power.jl +++ b/test/power.jl @@ -4,200 +4,6 @@ @test p isa Transform @test cardinality(p) == OneToOne() - # TODO: all of these should be part of some test utils - @testset "Vector" begin - x = [1, 2, 3, 4, 5] - expected = [1, 8, 27, 64, 125] + @test FeatureTransforms.apply([1, 2, 3], p) == [1, 8, 27] - @test FeatureTransforms.apply(x, p) == expected - @test p(x) == expected - - _x = copy(x) - FeatureTransforms.apply!(_x, p) - @test _x == expected - - @testset "inds" begin - @test FeatureTransforms.apply(x, p; inds=2:5) == expected[2:5] - @test FeatureTransforms.apply(x, p; dims=:) == expected - @test FeatureTransforms.apply(x, p; dims=1) == expected - @test FeatureTransforms.apply(x, p; dims=1, inds=[2, 3, 4, 5]) == expected[2:5] - - @test_throws BoundsError FeatureTransforms.apply(x, p; dims=2) - end - - @testset "apply_append" begin - @test FeatureTransforms.apply_append(x, p, append_dim=1) == vcat(x, expected) - end - end - - @testset "Matrix" begin - M = [1 2 3; 4 5 6] - expected = [1 8 27; 64 125 216] - - @testset "dims = $d" for d in (Colon(), 1, 2) - @test FeatureTransforms.apply(M, p; dims=d) == expected - @test p(M; dims=d) == expected - - _M = copy(M) - FeatureTransforms.apply!(_M, p; dims=d) - @test _M == expected - end - - @testset "inds" begin - @test FeatureTransforms.apply(M, p; inds=[2, 3]) == expected[[2, 3]] - @test FeatureTransforms.apply(M, p; dims=:, inds=[2, 3]) == expected[[2, 3]] - @test FeatureTransforms.apply(M, p; dims=1, inds=[2]) == [64 125 216] - @test FeatureTransforms.apply(M, p; dims=2, inds=[2]) == reshape([8, 125], 2, 1) - end - - @testset "apply_append" begin - @test FeatureTransforms.apply_append(M, p, append_dim=1) == cat(M, expected, dims=1) - @test FeatureTransforms.apply_append(M, p, append_dim=2) == cat(M, expected, dims=2) - @test FeatureTransforms.apply_append(M, p, append_dim=3) == cat(M, expected, dims=3) - end - end - - @testset "AxisArray" begin - A = AxisArray([1 2 3; 4 5 6], foo=["a", "b"], bar=["x", "y", "z"]) - expected = [1 8 27; 64 125 216] - - @testset "dims = $d" for d in (Colon(), 1, 2) - transformed = FeatureTransforms.apply(A, p; dims=d) - # AxisArray doesn't preserve the type it operates on - @test transformed isa AbstractArray - @test transformed == expected - end - - _A = copy(A) - FeatureTransforms.apply!(_A, p) - @test _A isa AxisArray - @test _A == expected - - @testset "inds" begin - @test FeatureTransforms.apply(A, p; inds=[2, 3]) == expected[[2, 3]] - @test FeatureTransforms.apply(A, p; dims=:, inds=[2, 3]) == expected[[2, 3]] - @test FeatureTransforms.apply(A, p; dims=1, inds=[2]) == [64 125 216] - @test FeatureTransforms.apply(A, p; dims=2, inds=[2]) == reshape([8, 125], 2, 1) - end - - @testset "apply_append" begin - @test FeatureTransforms.apply_append(A, p, append_dim=1) == cat(A, expected, dims=1) - @test FeatureTransforms.apply_append(A, p, append_dim=2) == cat(A, expected, dims=2) - @test FeatureTransforms.apply_append(A, p, append_dim=3) == cat(A, expected, dims=3) - end - end - - @testset "AxisKey" begin - A = KeyedArray([1 2 3; 4 5 6], foo=["a", "b"], bar=["x", "y", "z"]) - expected = KeyedArray([1 8 27; 64 125 216], foo=["a", "b"], bar=["x", "y", "z"]) - - @testset "dims = $d" for d in (Colon(), :foo, :bar, 1, 2) - transformed = FeatureTransforms.apply(A, p; dims=d) - @test transformed isa KeyedArray - @test transformed == expected - end - - _A = copy(A) - FeatureTransforms.apply!(_A, p) - @test _A isa KeyedArray - @test _A == expected - - @testset "inds" begin - @test FeatureTransforms.apply(A, p; inds=[2, 3]) == [64, 8] - @test FeatureTransforms.apply(A, p; dims=:, inds=[2, 3]) == [64, 8] - @test FeatureTransforms.apply(A, p; dims=1, inds=[2]) == [64 125 216] - @test FeatureTransforms.apply(A, p; dims=2, inds=[2]) == reshape([8, 125], 2, 1) - end - - @testset "apply_append" begin - expected1 = KeyedArray( - vcat(A, expected), foo=["a", "b", "a", "b"], bar=["x", "y", "z"] - ) - @test FeatureTransforms.apply_append(A, p, append_dim=:foo) == expected1 - expected2 = KeyedArray( - hcat(A, expected), foo=["a", "b"], bar=["x", "y", "z", "x", "y", "z"] - ) - @test FeatureTransforms.apply_append(A, p, append_dim=:bar) == expected2 - expected3 = KeyedArray( - cat(A, expected, dims=3), foo=["a", "b"], bar=["x", "y", "z"], baz=Base.OneTo(2), - ) - @test FeatureTransforms.apply_append(A, p, append_dim=:baz) == expected3 - end - end - - @testset "NamedTuple" begin - nt = (a = [1, 2, 3], b = [4, 5, 6]) - - @testset "all cols" begin - - expected_nt = (Column1 = [1, 8, 27], Column2 = [64, 125, 216]) - @test FeatureTransforms.apply(nt, p) == expected_nt - @test p(nt) == expected_nt - - _nt = deepcopy(nt) - FeatureTransforms.apply!(_nt, p) - @test _nt isa NamedTuple{(:a, :b)} - @test _nt == (a = [1, 8, 27], b = [64, 125, 216]) - end - - @testset "custom header" begin - expected_nt = (x = [1, 8, 27], y = [64, 125, 216]) - @test FeatureTransforms.apply(nt, p; header=[:x, :y]) == expected_nt - @test p(nt; header=[:x, :y]) == expected_nt - end - - @testset "cols = $c" for c in (:a, :b) - expected = getproperty(nt, c) .^3 - - @test FeatureTransforms.apply(nt, p; cols=c) == (Column1 = expected, ) - @test p(nt; cols=c) == (Column1 = expected, ) - - @testset "mutating" for _c in (c, [c]) - _nt = deepcopy(nt) - FeatureTransforms.apply!(_nt, p; cols=_c) - @test _nt == merge(nt, NamedTuple{(c,)}((expected,))) - @test _nt isa NamedTuple - end - end - - @testset "apply_append" begin - expected = merge(nt, (Column1 = [1, 8, 27], Column2 = [64, 125, 216])) - @test FeatureTransforms.apply_append(nt, p) == expected - end - end - - @testset "DataFrame" begin - df = DataFrame(:a => [1, 2, 3], :b => [4, 5, 6]) - - @testset "all cols" begin - expected_df = DataFrame(:Column1 => [1, 8, 27], :Column2 => [64, 125, 216]) - @test FeatureTransforms.apply(df, p) == expected_df - @test p(df) == expected_df - - _df = deepcopy(df) - FeatureTransforms.apply!(_df, p) - @test _df isa DataFrame - @test _df == DataFrame(:a => [1, 8, 27], :b => [64, 125, 216]) - end - - @testset "custom header" begin - expected_df = DataFrame(:x => [1, 8, 27], :y => [64, 125, 216]) - @test FeatureTransforms.apply(df, p; header=[:x, :y]) == expected_df - @test p(df; header=[:x, :y]) == expected_df - end - - @testset "cols = $c" for c in (:a, :b) - expected = getproperty(df, c) .^ 3 - @test FeatureTransforms.apply(df, p; cols=[c]) == DataFrame(:Column1=>expected) - @test FeatureTransforms.apply(df, p; cols=c) == DataFrame(:Column1=>expected) - @test p(df; cols=[c]) == DataFrame(:Column1=>expected) - end - - @testset "apply_append" begin - expected = DataFrame( - :a => df.a, :b => df.b, :Column1 => [1, 8, 27], :Column2 => [64, 125, 216] - ) - @test FeatureTransforms.apply_append(df, p) == expected - end - end end diff --git a/test/runtests.jl b/test/runtests.jl index 54f5507..2e9dd36 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,7 +6,8 @@ using Documenter: doctest using FeatureTransforms using FeatureTransforms: _periodic using FeatureTransforms: cardinality, OneToOne, OneToMany, ManyToOne, ManyToMany -using Tables: rowtable, columntable +using FeatureTransforms.TestUtils +using Tables: columntable, istable, rowtable using Test using TimeZones @@ -22,4 +23,10 @@ using TimeZones include("temporal.jl") include("traits.jl") include("test_utils.jl") + + include("types/tables.jl") + include("types/vector.jl") + include("types/matrix.jl") + include("types/cube.jl") + include("types/xarray.jl") end diff --git a/test/scaling.jl b/test/scaling.jl index 7033413..2846b27 100644 --- a/test/scaling.jl +++ b/test/scaling.jl @@ -9,221 +9,6 @@ @test IdentityScaling(123) == IdentityScaling() @test IdentityScaling([1, 2, 3]) == IdentityScaling() end - - @testset "Vector" begin - x = [1., 2., 3.] - expected = [1., 2., 3.] - - @test FeatureTransforms.apply(x, scaling) == expected - @test scaling(x) == expected - - @testset "Mutating" begin - _x = copy(x) - FeatureTransforms.apply!(_x, scaling) - @test _x == expected - end - - @testset "dims" begin - @test FeatureTransforms.apply(x, scaling; dims=1) == expected - @test_throws BoundsError FeatureTransforms.apply(expected, scaling; dims=2) - end - - @testset "inds" begin - @test FeatureTransforms.apply(x, scaling; inds=[2, 3]) == [2., 3.] - @test FeatureTransforms.apply(x, scaling; dims=:, inds=[2, 3]) == [2., 3.] - @test FeatureTransforms.apply(x, scaling; dims=1, inds=[2, 3]) == [2., 3.] - end - - @testset "Inverse" begin - @test FeatureTransforms.apply(x, scaling; inverse=true) == expected - end - - @testset "apply_append" begin - @test FeatureTransforms.apply_append(x, scaling, append_dim=1) == vcat(x, expected) - end - end - - @testset "Matrix" begin - M = [0.0 -0.5 0.5; 0.0 1.0 2.0] - M_expected = [0.0 -0.5 0.5; 0.0 1.0 2.0] - - @test FeatureTransforms.apply(M, scaling) == M_expected - - @testset "Mutating" begin - _M = copy(M) - FeatureTransforms.apply!(_M, scaling) - @test _M == M_expected - end - - @testset "dims = $d" for d in (Colon(), 1, 2) - @test FeatureTransforms.apply(M, scaling; dims=d) == M_expected - end - - @testset "inds" begin - @test FeatureTransforms.apply(M, scaling; inds=[2, 3]) == [0.0, -0.5] - @test FeatureTransforms.apply(M, scaling; dims=:, inds=[2, 3]) == [0.0, -0.5] - @test FeatureTransforms.apply(M, scaling; dims=1, inds=[2]) == [0.0 1.0 2.0] - @test FeatureTransforms.apply(M, scaling; dims=2, inds=[2]) == reshape([-0.5; 1.0], 2, 1) - end - - @testset "Inverse" begin - @test FeatureTransforms.apply(M, scaling; inverse=true) == M_expected - end - - @testset "apply_append" begin - exp1 = cat(M, M_expected, dims=1) - @test FeatureTransforms.apply_append(M, scaling, append_dim=1) == exp1 - exp2 = cat(M, M_expected, dims=2) - @test FeatureTransforms.apply_append(M, scaling, append_dim=2) == exp2 - exp3 = cat(M, M_expected, dims=3) - @test FeatureTransforms.apply_append(M, scaling, append_dim=3) == exp3 - end - end - - @testset "AxisArray" begin - M = [0.0 -0.5 0.5; 0.0 1.0 2.0] - A = AxisArray(M; foo=["a", "b"], bar=["x", "y", "z"]) - M_expected = [0.0 -0.5 0.5; 0.0 1.0 2.0] - A_expected = AxisArray(M_expected; foo=["a", "b"], bar=["x", "y", "z"]) - - @test FeatureTransforms.apply(A, scaling) == A_expected - - @testset "Mutating" begin - _A = copy(A) - FeatureTransforms.apply!(_A, scaling) - @test _A isa AxisArray - @test _A == A_expected - end - - @testset "dims = $d" for d in (Colon(), 1, 2) - @test FeatureTransforms.apply(M, scaling; dims=d) == A_expected - end - - @testset "inds" begin - @test FeatureTransforms.apply(A, scaling; inds=[2, 3]) == [0.0, -0.5] - @test FeatureTransforms.apply(A, scaling; dims=:, inds=[2, 3]) == [0.0, -0.5] - @test FeatureTransforms.apply(A, scaling; dims=1, inds=[2]) == [0.0 1.0 2.0] - @test FeatureTransforms.apply(A, scaling; dims=2, inds=[2]) == reshape([-0.5; 1.0], 2, 1) - end - - @testset "Inverse" begin - @test FeatureTransforms.apply(A, scaling; inverse=true) == A_expected - end - - @testset "apply_append" begin - # dims = 1, 2 give ArgumentError: Categorical axes must be unique - exp3 = cat(M_expected, M_expected, dims=3) - @test FeatureTransforms.apply_append(A, scaling, append_dim=3) == exp3 - end - end - - @testset "AxisKey" begin - M = [0.0 -0.5 0.5; 0.0 1.0 2.0] - A = KeyedArray(M; foo=["a", "b"], bar=["x", "y", "z"]) - M_expected = [0.0 -0.5 0.5; 0.0 1.0 2.0] - A_expected = KeyedArray(M_expected; foo=["a", "b"], bar=["x", "y", "z"]) - - @test FeatureTransforms.apply(A, scaling) == A_expected - - @testset "Mutating" begin - _A = copy(A) - FeatureTransforms.apply!(_A, scaling) - @test _A isa KeyedArray - @test _A == A_expected - end - - @testset "dims = $d" for d in (Colon(), 1, 2) - @test FeatureTransforms.apply(M, scaling; dims=d) == A_expected - end - - @testset "inds" begin - @test FeatureTransforms.apply(A, scaling; inds=[2, 3]) == [0.0, -0.5] - @test FeatureTransforms.apply(A, scaling; dims=:, inds=[2, 3]) == [0.0, -0.5] - @test FeatureTransforms.apply(A, scaling; dims=1, inds=[2]) == [0.0 1.0 2.0] - @test FeatureTransforms.apply(A, scaling; dims=2, inds=[2]) == reshape([-0.5; 1.0], 2, 1) - end - - @testset "Inverse" begin - @test FeatureTransforms.apply(A, scaling; inverse=true) == A_expected - end - - @testset "apply_append" begin - expected1 = KeyedArray( - vcat(M, M_expected), foo=["a", "b", "a", "b"], bar=["x", "y", "z"], - ) - @test FeatureTransforms.apply_append(A, scaling, append_dim=:foo) == expected1 - - expected2 = KeyedArray( - hcat(M, M_expected), foo=["a", "b"], bar=["x", "y", "z", "x", "y", "z"] - ) - @test FeatureTransforms.apply_append(A, scaling, append_dim=:bar) == expected2 - expected3 = KeyedArray( - cat(M, M_expected, dims=3), - foo=["a", "b"], - bar=["x", "y", "z"], - baz=Base.OneTo(2), - ) - @test FeatureTransforms.apply_append(A, scaling, append_dim=:baz) == expected3 - end - end - - @testset "NamedTuple" begin - nt = (a = [0.0, -0.5, 0.5], b = [1.0, 0.0, 2.0]) - expected = (Column1 = [0.0, -0.5, 0.5], Column2 = [1.0, 0.0, 2.0]) - - @test FeatureTransforms.apply(nt, scaling) == expected - - @testset "Mutating" begin - _nt = deepcopy(nt) - FeatureTransforms.apply!(_nt, scaling) - @test _nt == nt - end - - @testset "cols = :a" begin - exp = (Column1=nt.a, ) - @test FeatureTransforms.apply(nt, scaling; cols=[:a]) == exp - @test FeatureTransforms.apply(nt, scaling; cols=:a) == exp - end - - @testset "Inverse" begin - @test FeatureTransforms.apply(nt, scaling; inverse=true) == expected - end - - @testset "apply_append" begin - @test FeatureTransforms.apply_append(nt, scaling) == merge(nt, expected) - end - end - - @testset "DataFrame" begin - df = DataFrame(:a => [0.0, -0.5, 0.5], :b => [1.0, 0.0, 2.0]) - df_expected = DataFrame(:Column1 => [0.0, -0.5, 0.5], :Column2 => [1.0, 0.0, 2.0]) - - @test FeatureTransforms.apply(df, scaling) == df_expected - - @testset "Mutating" begin - _df = deepcopy(df) - FeatureTransforms.apply!(_df, scaling) - @test _df isa DataFrame - @test _df == df - end - - @testset "cols = :a" begin - expected = DataFrame(:Column1=>df.a) - @test FeatureTransforms.apply(df, scaling; cols=[:a]) == expected - @test FeatureTransforms.apply(df, scaling; cols=:a) == expected - end - - @testset "Inverse" begin - @test FeatureTransforms.apply(df, scaling; inverse=true) == df_expected - end - - @testset "apply_append" begin - expected = DataFrame( - :a => df.a, :b => df.b, :Column1 => df.a, :Column2 => df.b, - ) - @test FeatureTransforms.apply_append(df, scaling) == expected - end - end end @testset "MeanStdScaling" begin @@ -257,456 +42,24 @@ end end - @testset "Vector" begin - x = [1., 2., 3.] - expected = [-1., 0., 1.] - - @testset "Non-mutating" begin - scaling = MeanStdScaling(x) - @test FeatureTransforms.apply(x, scaling) ≈ expected atol=1e-5 - @test scaling(x) ≈ expected atol=1e-5 - - # Test the transform was not mutating - @test !isapprox(x, expected; atol=1e-5) - end - - @testset "Mutating" begin - scaling = MeanStdScaling(x) - _x = copy(x) - FeatureTransforms.apply!(_x, scaling) - @test _x ≈ expected atol=1e-5 - end - - @testset "dims" begin - scaling = MeanStdScaling(x; dims=1) - @test FeatureTransforms.apply(x, scaling; dims=1) == expected - @test_throws BoundsError FeatureTransforms.apply(x, scaling; dims=2) - end - - @testset "inds" begin - scaling = MeanStdScaling(x) - @test FeatureTransforms.apply(x, scaling; inds=[2, 3]) == [0., 1.] - @test FeatureTransforms.apply(x, scaling; dims=:, inds=[2, 3]) == [0., 1.] - - scaling = MeanStdScaling(x; dims=1) - @test FeatureTransforms.apply(x, scaling; dims=1, inds=[2, 3]) == [0., 1.] - end - - @testset "Re-apply" begin - scaling = MeanStdScaling(x) - FeatureTransforms.apply(x, scaling) - - # Expect scaling parameters to be fixed to the first data applied to - @test FeatureTransforms.apply([-0.5, 0.5, 0.0], scaling) ≈ [-2.5, -1.5, -2.0] atol=1e-5 - end - - @testset "Inverse" begin - scaling = MeanStdScaling(x) - transformed = FeatureTransforms.apply(x, scaling) - - @test transformed ≈ expected atol=1e-5 - @test FeatureTransforms.apply(transformed, scaling; inverse=true) ≈ x atol=1e-5 - end - - @testset "apply_append" begin - scaling = MeanStdScaling(x) - @test FeatureTransforms.apply_append(x, scaling, append_dim=1) == vcat(x, expected) - end - end - - @testset "Matrix" begin + @testset "Re-apply" begin M = [0.0 -0.5 0.5; 0.0 1.0 2.0] - M_expected = [-0.559017 -1.11803 0.0; -0.559017 0.559017 1.67705] - - @testset "Non-mutating" begin - scaling = MeanStdScaling(M) - @test FeatureTransforms.apply(M, scaling) ≈ M_expected atol=1e-5 - @test scaling(M) ≈ M_expected atol=1e-5 - - # Test the transform was not mutating - @test !isapprox(M, M_expected; atol=1e-5) - end - - @testset "Mutating" begin - scaling = MeanStdScaling(M) - _M = copy(M) - FeatureTransforms.apply!(_M, scaling) - @test _M ≈ M_expected atol=1e-5 - end - - @testset "dims = :" begin - scaling = MeanStdScaling(M; dims=:) - @test FeatureTransforms.apply(M, scaling; dims=:) ≈ M_expected atol=1e-5 - end - - @testset "dims = 1" begin - scaling = MeanStdScaling(M; dims=1) - @test FeatureTransforms.apply(M, scaling; dims=1) ≈ M_expected atol=1e-5 - end - - @testset "dims = 2" begin - scaling = MeanStdScaling(M; dims=2) - @test FeatureTransforms.apply(M, scaling; dims=2) ≈ M_expected atol=1e-5 - end - - @testset "inds" begin - scaling = MeanStdScaling(M) - @test FeatureTransforms.apply(M, scaling; inds=[2, 3]) ≈ [-0.559017, -1.11803] atol=1e-5 - @test FeatureTransforms.apply(M, scaling; dims=:, inds=[2, 3]) ≈ [-0.559017, -1.11803] atol=1e-5 - - scaling = MeanStdScaling(M; dims=1, inds=[2]) - @test FeatureTransforms.apply(M, scaling; dims=1, inds=[2]) ≈ [-1.0 0.0 1.0] atol=1e-5 - - scaling = MeanStdScaling(M; dims=2, inds=[2]) - @test FeatureTransforms.apply(M, scaling; dims=2, inds=[2]) ≈ [-0.70711 0.70711]' atol=1e-5 - end - - @testset "Re-apply" begin - scaling = MeanStdScaling(M; dims=2) - new_M = [1.0 -2.0 -1.0; 0.5 0.0 0.5] - @test M !== new_M - # Expect scaling parameters to be fixed to the first data applied to - expected_reapply = [0.559017 -2.79508 -1.67705; 0.0 -0.55901 0.0] - @test FeatureTransforms.apply(new_M, scaling; dims=2) ≈ expected_reapply atol=1e-5 - end - - @testset "Inverse" begin - scaling = MeanStdScaling(M) - transformed = FeatureTransforms.apply(M, scaling) - - @test transformed ≈ M_expected atol=1e-5 - @test FeatureTransforms.apply(transformed, scaling; inverse=true) ≈ M atol=1e-5 - end - - @testset "apply_append" begin - scaling = MeanStdScaling(M) - exp1 = cat(M, M_expected, dims=1) - @test FeatureTransforms.apply_append(M, scaling, append_dim=1) ≈ exp1 atol=1e-5 - exp2 = cat(M, M_expected, dims=2) - @test FeatureTransforms.apply_append(M, scaling, append_dim=2) ≈ exp2 atol=1e-5 - exp3 = cat(M, M_expected, dims=3) - @test FeatureTransforms.apply_append(M, scaling, append_dim=3) ≈ exp3 atol=1e-5 - end + scaling = MeanStdScaling(M; dims=2) + new_M = [1.0 -2.0 -1.0; 0.5 0.0 0.5] + @test M !== new_M + # Expect scaling parameters to be fixed to the first data applied to + expected_reapply = [0.559017 -2.79508 -1.67705; 0.0 -0.55901 0.0] + @test FeatureTransforms.apply(new_M, scaling; dims=2) ≈ expected_reapply atol=1e-5 end - @testset "AxisArray" begin + @testset "Inverse" begin M = [0.0 -0.5 0.5; 0.0 1.0 2.0] - A = AxisArray(M; foo=["a", "b"], bar=["x", "y", "z"]) M_expected = [-0.559017 -1.11803 0.0; -0.559017 0.559017 1.67705] - A_expected = AxisArray(M_expected; foo=["a", "b"], bar=["x", "y", "z"]) - - @testset "Non-mutating" begin - scaling = MeanStdScaling(A) - @test FeatureTransforms.apply(A, scaling) ≈ A_expected atol=1e-5 - @test scaling(A) ≈ A_expected atol=1e-5 - - # Test the transform was not mutating - @test !isapprox(A, A_expected; atol=1e-5) - end + scaling = MeanStdScaling(M) + transformed = FeatureTransforms.apply(M, scaling) - @testset "Mutating" begin - scaling = MeanStdScaling(A) - _A = copy(A) - FeatureTransforms.apply!(_A, scaling) - @test _A isa AxisArray - @test _A ≈ A_expected atol=1e-5 - end - - @testset "dims = :" begin - scaling = MeanStdScaling(A, dims=:) - @test FeatureTransforms.apply(A, scaling; dims=:) ≈ A_expected atol=1e-5 - end - - @testset "dims = 1" begin - scaling = MeanStdScaling(A; dims=1) - @test FeatureTransforms.apply(A, scaling; dims=1) ≈ A_expected atol=1e-5 - end - - @testset "dims = 2" begin - scaling = MeanStdScaling(A; dims=2) - @test FeatureTransforms.apply(A, scaling; dims=2) ≈ A_expected atol=1e-5 - end - - @testset "inds" begin - scaling = MeanStdScaling(A) - @test FeatureTransforms.apply(A, scaling; inds=[2, 3]) ≈ [-0.559017, -1.11803] atol=1e-5 - @test FeatureTransforms.apply(A, scaling; dims=:, inds=[2, 3]) ≈ [-0.559017, -1.11803] atol=1e-5 - - scaling = MeanStdScaling(A; dims=1, inds=[2]) - @test FeatureTransforms.apply(A, scaling; dims=1, inds=[2]) ≈ [-1.0 0.0 1.0] atol=1e-5 - - scaling = MeanStdScaling(A; dims=2, inds=[2]) - @test FeatureTransforms.apply(A, scaling; dims=2, inds=[2]) ≈ [-0.70711 0.70711]' atol=1e-5 - end - - @testset "Re-apply" begin - scaling = MeanStdScaling(A) - FeatureTransforms.apply(A, scaling) - - M_new = [1.0 -2.0 -1.0; 0.5 0.0 0.5] - A_new = AxisArray(M_new; foo=["a", "b"], bar=["x", "y", "z"]) - @test A_new != A - - # Expect scaling parameters to be fixed to the first data applied to - expected_reapply = [0.559017 -2.79508 -1.67705; 0.0 -0.55901 0.0] - @test FeatureTransforms.apply(A_new, scaling) ≈ expected_reapply atol=1e-5 - end - - @testset "Inverse" begin - scaling = MeanStdScaling(A) - transformed = FeatureTransforms.apply(A, scaling) - - @test transformed ≈ A_expected atol=1e-5 - @test FeatureTransforms.apply(transformed, scaling; inverse=true) ≈ A atol=1e-5 - end - - @testset "apply_append" begin - scaling = MeanStdScaling(A) - exp1 = cat(M, M_expected, dims=1) - @test FeatureTransforms.apply_append(A, scaling, append_dim=1) ≈ exp1 atol=1e-5 - exp2 = cat(M, M_expected, dims=2) - @test FeatureTransforms.apply_append(A, scaling, append_dim=2) ≈ exp2 atol=1e-5 - exp3 = cat(M, M_expected, dims=3) - @test FeatureTransforms.apply_append(A, scaling, append_dim=3) ≈ exp3 atol=1e-5 - end - end - - @testset "AxisKey" begin - M = [0.0 -0.5 0.5; 0.0 1.0 2.0] - A = KeyedArray(M; foo=["a", "b"], bar=["x", "y", "z"]) - M_expected = [-0.559017 -1.11803 0.0; -0.559017 0.559017 1.67705] - A_expected = KeyedArray(M_expected; foo=["a", "b"], bar=["x", "y", "z"]) - - @testset "Non-mutating" begin - scaling = MeanStdScaling(A) - @test FeatureTransforms.apply(A, scaling) ≈ A_expected atol=1e-5 - @test scaling(A) ≈ A_expected atol=1e-5 - - # Test the transform was not mutating - @test !isapprox(A, A_expected; atol=1e-5) - end - - @testset "Mutating" begin - scaling = MeanStdScaling(A) - _A = copy(A) - FeatureTransforms.apply!(_A, scaling) - @test _A isa KeyedArray - @test _A ≈ A_expected atol=1e-5 - end - - @testset "dims = $d" for d in (Colon(), 1, 2, :foo, :bar) - scaling = MeanStdScaling(A, dims=d) - @test FeatureTransforms.apply(A, scaling; dims=d) ≈ A_expected atol=1e-5 - end - - @testset "inds" begin - scaling = MeanStdScaling(A) - @test FeatureTransforms.apply(A, scaling; inds=[2, 3]) ≈ [-0.559017, -1.11803] atol=1e-5 - @test FeatureTransforms.apply(A, scaling; dims=:, inds=[2, 3]) ≈ [-0.559017, -1.11803] atol=1e-5 - - @testset "dims=$d" for d in (1, :foo) - scaling = MeanStdScaling(A; dims=d, inds=[2]) - @test isapprox( - FeatureTransforms.apply(A, scaling; dims=d, inds=[2]), - [-1.0 0.0 1.0], - atol=1e-5 - ) - end - - @testset "dims=$d" for d in (2, :bar) - scaling = MeanStdScaling(A; dims=2, inds=[2]) - @test isapprox( - FeatureTransforms.apply(A, scaling; dims=2, inds=[2]), - [-0.70711 0.70711]', - atol=1e-5 - ) - end - end - - @testset "Re-apply" begin - scaling = MeanStdScaling(A; dims=2) - FeatureTransforms.apply(A, scaling; dims=2) - - M_new = [1.0 -2.0 -1.0; 0.5 0.0 0.5] - A_new = KeyedArray(M_new; foo=["a", "b"], bar=["x", "y", "z"]) - @test A_new != A - - # Expect scaling parameters to be fixed to the first data applied to - expected_reapply = [0.559017 -2.79508 -1.67705; 0.0 -0.55901 0.0] - @test FeatureTransforms.apply(A_new, scaling; dims=2) ≈ expected_reapply atol=1e-5 - end - - @testset "Inverse" begin - scaling = MeanStdScaling(A) - transformed = FeatureTransforms.apply(A, scaling) - - @test transformed ≈ A_expected atol=1e-5 - @test FeatureTransforms.apply(transformed, scaling; inverse=true) ≈ A atol=1e-5 - end - - @testset "apply_append" begin - scaling = MeanStdScaling(A) - exp1 = KeyedArray( - vcat(M, M_expected), foo=["a", "b", "a", "b"], bar=["x", "y", "z"], - ) - @test FeatureTransforms.apply_append(A, scaling, append_dim=:foo) ≈ exp1 atol=1e-5 - - exp2 = KeyedArray( - hcat(M, M_expected), foo=["a", "b"], bar=["x", "y", "z", "x", "y", "z"] - ) - @test FeatureTransforms.apply_append(A, scaling, append_dim=:bar) ≈ exp2 atol=1e-5 - - exp3 = KeyedArray( - cat(M, M_expected, dims=3), - foo=["a", "b"], - bar=["x", "y", "z"], - baz=Base.OneTo(2), - ) - @test FeatureTransforms.apply_append(A, scaling, append_dim=:baz) ≈ exp3 atol=1e-5 - end - end - - @testset "NamedTuple" begin - nt = (a = [0.0, -0.5, 0.5], b = [1.0, 0.0, 2.0]) - - @testset "Non-mutating" begin - expected = [[-0.55902, -1.11803, 0.0], [0.55902, -0.55902, 1.67705]] - - scaling = MeanStdScaling(nt) - result = FeatureTransforms.apply(nt, scaling) - @test result isa NamedTuple{(:Column1, :Column2)} - @test collect(result) ≈ expected atol=1e-5 - - result = scaling(nt) - @test result isa NamedTuple{(:Column1, :Column2)} - @test collect(result) ≈ expected atol=1e-5 - end - - @testset "Mutating" begin - expected = [[-0.55902, -1.11803, 0.0], [0.55902, -0.55902, 1.67705]] - scaling = MeanStdScaling(nt) - _nt = deepcopy(nt) - FeatureTransforms.apply!(_nt, scaling) - @test _nt isa NamedTuple{(:a, :b)} - @test collect(_nt) ≈ expected atol=1e-5 - end - - @testset "cols = :a" begin - scaling = MeanStdScaling(nt; cols=:a) - - expected = (Column1 = [0.0, -1.0, 1.0], ) - @test FeatureTransforms.apply(nt, scaling; cols=:a) == expected - @test FeatureTransforms.apply(nt, scaling; cols=[:a]) == expected - @test scaling(nt; cols=:a) == expected - - _nt = deepcopy(nt) - FeatureTransforms.apply!(_nt, scaling; cols=:a) - @test _nt == (a=[0.0, -1.0, 1.0], b=[1.0, 0.0, 2.0]) - end - - @testset "Re-apply" begin - scaling = MeanStdScaling(nt) - - # Expect scaling parameters to be fixed to the first data applied to - nt2 = (a = [-1.0, 0.5, 0.0], b = [2.0, 2.0, 1.0]) - @test nt !== nt2 - expected2 = [[-1.67705, 0.0, -0.55902], [1.67705, 1.67705, 0.55902]] - @test collect(FeatureTransforms.apply(nt2, scaling)) ≈ expected2 atol=1e-5 - end - - @testset "Inverse" begin - scaling = MeanStdScaling(nt) - transformed = FeatureTransforms.apply(nt, scaling) - expected_inverse = (Column1 = [0.0, -0.5, 0.5], Column2 = [1.0, 0.0, 2.0]) - inverted = FeatureTransforms.apply(transformed, scaling; inverse=true) - @test expected_inverse == inverted - end - - @testset "apply_append" begin - scaling = MeanStdScaling(nt) - expected = ( - Column1 = [-0.55902, -1.11803, 0.0], - Column2 = [0.55902, -0.55902, 1.67705] - ) - result = FeatureTransforms.apply_append(nt, scaling) - @test result isa NamedTuple{(:a, :b, :Column1, :Column2)} - @test collect(result) ≈ collect(merge(nt, expected)) atol=1e-5 - end - end - - @testset "DataFrame" begin - df = DataFrame(:a => [0.0, -0.5, 0.5], :b => [1.0, 0.0, 2.0]) - - df_expected = DataFrame( - :Column1 => [-0.55902, -1.11803, 0.0], - :Column2 => [0.55902, -0.55902, 1.67705], - ) - - @testset "Non-mutating" begin - scaling = MeanStdScaling(df) - @test FeatureTransforms.apply(df, scaling) ≈ df_expected atol=1e-5 - @test scaling(df) ≈ df_expected atol=1e-5 - end - - @testset "Mutating" begin - scaling = MeanStdScaling(df) - _df = deepcopy(df) - FeatureTransforms.apply!(_df, scaling) - @test _df isa DataFrame - @test isapprox( - _df, - DataFrame(:a => [-0.55902, -1.11803, 0.0], :b => [0.55902, -0.55902, 1.67705]), - atol=1e-5 - ) - end - - @testset "cols = :a" begin - scaling = MeanStdScaling(df; cols=:a) - - @test isequal( - FeatureTransforms.apply(df, scaling; cols=:a), - DataFrame(:Column1 => [0.0, -1.0, 1.0]), - ) - - _df = deepcopy(df) - FeatureTransforms.apply!(_df, scaling; cols=:a) - @test _df isa DataFrame - @test _df == DataFrame(:a => [0.0, -1.0, 1.0], :b => [1.0, 0.0, 2.0]) - end - - @testset "Re-apply" begin - # Expect scaling parameters to be fixed to the first data applied to - df2 = DataFrame(:a => [-1.0, 0.5, 0.0], :b => [2.0, 2.0, 1.0]) - @test df !== df2 - - scaling = MeanStdScaling(df) - - df_expected2 = DataFrame( - :Column1 => [-1.67705, 0.0, -0.55902], - :Column2 => [1.67705, 1.67705, 0.55902], - ) - @test FeatureTransforms.apply(df2, scaling) ≈ df_expected2 atol=1e-5 - end - - @testset "Inverse" begin - scaling = MeanStdScaling(df) - transformed = FeatureTransforms.apply(df, scaling) - - expected_inverse = DataFrame(:Column1=>df.a, :Column2=>df.b) - inverted = FeatureTransforms.apply(transformed, scaling; inverse=true) - @test inverted == expected_inverse - end - - @testset "apply_append" begin - scaling = MeanStdScaling(df) - expected = DataFrame( - :a => df.a, - :b => df.b, - :Column1 => df_expected.Column1, - :Column2 => df_expected.Column2, - ) - @test FeatureTransforms.apply_append(df, scaling) ≈ expected atol=1e-5 - end + @test transformed ≈ M_expected atol=1e-5 + @test FeatureTransforms.apply(transformed, scaling; inverse=true) ≈ M atol=1e-5 end @testset "Zero std" begin diff --git a/test/temporal.jl b/test/temporal.jl index 162e00b..bcd1425 100644 --- a/test/temporal.jl +++ b/test/temporal.jl @@ -4,7 +4,7 @@ @test hod isa Transform @test cardinality(hod) == OneToOne() - @testset "Vector" begin + @testset "Basic" begin x = collect(DateTime(2020, 1, 1, 9, 0):Hour(1):DateTime(2020, 5, 7, 9, 0)) # Expected result is an hour a day starting and ending on the 9th hour inclusive, # with 126 full days in the middle @@ -13,29 +13,12 @@ @test FeatureTransforms.apply(x, hod) == expected @test hod(x) == expected - # Test the tranform was not mutating - @test x != expected - @testset "StepRange" begin x = DateTime(2020, 1, 1, 9, 0):Hour(1):DateTime(2020, 5, 7, 9, 0) @test FeatureTransforms.apply(x, hod) == expected @test hod(x) == expected end - @testset "dims = $d" for d in (Colon(), 1) - @test FeatureTransforms.apply(x, hod; dims=d) == expected - @test hod(x; dims=d) == expected - end - - @test_throws BoundsError FeatureTransforms.apply(x, hod; dims=2) - - @testset "inds" begin - @test FeatureTransforms.apply(x, hod; inds=2:5) == expected[2:5] - @test FeatureTransforms.apply(x, hod; dims=:) == expected - @test FeatureTransforms.apply(x, hod; dims=1) == expected - @test FeatureTransforms.apply(x, hod; dims=1, inds=[2, 3, 4, 5]) == expected[2:5] - end - @testset "DST" begin x = ZonedDateTime(2020, 3, 7, 9, 0, tz"America/New_York"):Hour(1):ZonedDateTime(2020, 3, 8, 9, 0, tz"America/New_York") @@ -45,180 +28,5 @@ @test FeatureTransforms.apply(x, hod) == expected_dst @test hod(x) == expected_dst end - - @testset "apply_append" begin - x = collect(DateTime(2020, 1, 1, 9, 0):Hour(1):DateTime(2020, 5, 7, 9, 0)) - expected = [9:23..., repeat(0:23, 126)..., 0:9...] - @test FeatureTransforms.apply_append(x, hod, append_dim=1) == vcat(x, expected) - end - end - - @testset "Matrix" begin - x = [ - DateTime(2020, 1, 1, 1, 0):Hour(1):DateTime(2020, 1, 1, 3, 0); - DateTime(2020, 1, 1, 9, 0):Hour(1):DateTime(2020, 1, 1, 11, 0) - ] - M = reshape(x, 3, 2) - expected = [1 9; 2 10; 3 11] - - @test FeatureTransforms.apply(M, hod) == expected - @test hod(M) == expected - - # Test the tranform was not mutating - @test M != expected - - @testset "dims = $d" for d in (Colon(), 1, 2) - @test FeatureTransforms.apply(M, hod; dims=d) == expected - @test hod(M; dims=d) == expected - end - - @testset "inds" begin - @test FeatureTransforms.apply(M, hod; inds=[2, 3]) == [2, 3] - @test FeatureTransforms.apply(M, hod; dims=:, inds=[2, 3]) == [2, 3] - @test FeatureTransforms.apply(M, hod; dims=1, inds=[2]) == [2 10] - @test FeatureTransforms.apply(M, hod; dims=2, inds=[2]) == reshape([9; 10; 11], 3, 1) - end - - @testset "apply_append" begin - @test FeatureTransforms.apply_append(M, hod, append_dim=1) == vcat(M, expected) - @test FeatureTransforms.apply_append(M, hod, append_dim=2) == hcat(M, expected) - @test FeatureTransforms.apply_append(M, hod, append_dim=3) == cat(M, expected, dims=3) - end - end - - @testset "AxisArray" begin - x = [ - DateTime(2020, 1, 1, 1, 0):Hour(1):DateTime(2020, 1, 1, 2, 0); - DateTime(2020, 1, 1, 9, 0):Hour(1):DateTime(2020, 1, 1, 10, 0); - DateTime(2020, 1, 1, 11, 0):Hour(1):DateTime(2020, 1, 1, 12, 0) - ] - M = reshape(x, 2, 3) - A = AxisArray(M, foo=["a", "b"], bar=["x", "y", "z"]) - expected = [1 9 11; 2 10 12] - - @test FeatureTransforms.apply(A, hod) == expected - @test hod(A) == expected - - @testset "dims = $d" for d in (Colon(), 1, 2) - transformed = FeatureTransforms.apply(A, hod; dims=d) - # AxisArray doesn't preserve the type it operates on - @test transformed isa AbstractArray - @test transformed == expected - end - - @testset "inds" begin - @test FeatureTransforms.apply(A, hod; inds=[2, 3]) == [2, 9] - @test FeatureTransforms.apply(A, hod; dims=:, inds=[2, 3]) == [2, 9] - @test FeatureTransforms.apply(A, hod; dims=1, inds=[2]) == [2 10 12] - @test FeatureTransforms.apply(A, hod; dims=2, inds=[2]) == reshape([9, 10], 2, 1) - end - - @testset "apply_append" begin - @test FeatureTransforms.apply_append(A, hod, append_dim=1) == vcat(A, expected) - @test FeatureTransforms.apply_append(A, hod, append_dim=2) == hcat(A, expected) - @test FeatureTransforms.apply_append(A, hod, append_dim=3) == cat(M, expected, dims=3) - end - end - - @testset "AxisKey" begin - x = [ - DateTime(2020, 1, 1, 1, 0):Hour(1):DateTime(2020, 1, 1, 2, 0); - DateTime(2020, 1, 1, 9, 0):Hour(1):DateTime(2020, 1, 1, 10, 0); - DateTime(2020, 1, 1, 11, 0):Hour(1):DateTime(2020, 1, 1, 12, 0) - ] - M = reshape(x, 2, 3) - A = KeyedArray(M, foo=["a", "b"], bar=["x", "y", "z"]) - expected = [1 9 11; 2 10 12] - - @test FeatureTransforms.apply(A, hod) == expected - @test hod(A) == expected - - @testset "dims = $d" for d in (Colon(), 1, 2, :foo, :bar) - transformed = FeatureTransforms.apply(A, hod; dims=d) - @test transformed isa KeyedArray - @test transformed == expected - end - - @testset "inds" begin - @test FeatureTransforms.apply(A, hod; inds=[2, 3]) == [2, 9] - @test FeatureTransforms.apply(A, hod; dims=:, inds=[2, 3]) == [2, 9] - @test FeatureTransforms.apply(A, hod; dims=1, inds=[2]) == [2 10 12] - @test FeatureTransforms.apply(A, hod; dims=2, inds=[2]) == reshape([9, 10], 2, 1) - @test FeatureTransforms.apply(A, hod; dims=:foo, inds=[2]) == [2 10 12] - @test FeatureTransforms.apply(A, hod; dims=:bar, inds=[2]) == reshape([9, 10], 2, 1) - end - - @testset "apply_append" begin - expected1 = KeyedArray( - vcat(M, expected), foo=["a", "b", "a", "b"], bar=["x", "y", "z"] - ) - @test FeatureTransforms.apply_append(A, hod, append_dim=:foo) == expected1 - - expected2 = KeyedArray( - hcat(M, expected), foo=["a", "b"], bar=["x", "y", "z", "x", "y", "z"] - ) - @test FeatureTransforms.apply_append(A, hod, append_dim=:bar) == expected2 - - expected3 = KeyedArray( - cat(M, expected, dims=3), foo=["a", "b"], bar=["x", "y", "z"], baz=Base.OneTo(2) - ) - @test FeatureTransforms.apply_append(A, hod, append_dim=:baz) == expected3 - end - end - - @testset "NamedTuple" begin - nt = ( - a = DateTime(2020, 1, 1, 0, 0):Hour(1):DateTime(2020, 1, 1, 2, 0), - b = DateTime(2020, 1, 1, 3, 0):Hour(1):DateTime(2020, 1, 1, 5, 0) - ) - - @testset "all cols" begin - expected = (Column1 = [0, 1, 2], Column2 = [3, 4, 5]) - @test FeatureTransforms.apply(nt, hod) == expected - @test hod(nt) == expected - - # Test the tranform was not mutating - @test nt != expected - end - - @testset "cols = :a" begin - expected = (Column1 = [0, 1, 2],) - @test FeatureTransforms.apply(nt, hod; cols=[:a]) == expected - @test FeatureTransforms.apply(nt, hod; cols=:a) == expected - @test hod(nt; cols=:a) == expected - end - - @testset "apply_append" begin - expected = merge(nt, (Column1 = [0, 1, 2], Column2 = [3, 4, 5])) - @test FeatureTransforms.apply_append(nt, hod) == expected - end - end - - - @testset "DataFrame" begin - df = DataFrame( - :a => DateTime(2020, 1, 1, 0, 0):Hour(1):DateTime(2020, 1, 1, 2, 0), - :b => DateTime(2020, 1, 1, 3, 0):Hour(1):DateTime(2020, 1, 1, 5, 0) - ) - - @testset "all cols" begin - expected = DataFrame(:Column1 => [0, 1, 2], :Column2 => [3, 4, 5]) - @test FeatureTransforms.apply(df, hod) == expected - @test hod(df) == expected - - # Test the tranform was not mutating - @test df != expected - end - - @test FeatureTransforms.apply(df, hod; cols=[:a]) == DataFrame(:Column1 => [0, 1, 2]) - @test FeatureTransforms.apply(df, hod; cols=:a) == DataFrame(:Column1 => [0, 1, 2]) - @test FeatureTransforms.apply(df, hod; cols=[:b]) == DataFrame(:Column1 => [3, 4, 5]) - - @testset "apply_append" begin - expected = DataFrame( - :a=>df.a, :b=>df.b, :Column1 => [0, 1, 2], :Column2 => [3, 4, 5] - ) - @test FeatureTransforms.apply_append(df, hod) == expected - end end end diff --git a/test/test_utils.jl b/test/test_utils.jl index c15dd0e..714b9e8 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -1,5 +1,3 @@ -using FeatureTransforms.TestUtils - @testset "test_utils.jl" begin @testset "FakeOneToOneTransform" begin diff --git a/test/types/cube.jl b/test/types/cube.jl new file mode 100644 index 0000000..421451b --- /dev/null +++ b/test/types/cube.jl @@ -0,0 +1,133 @@ +@testset "cube" begin + + x = reshape(1:24, 2, 3, 4) + @test is_transformable(x) + + @testset "apply" begin + + @testset "OneToOne" begin + T = FakeOneToOneTransform() + @test FeatureTransforms.apply(x, T) == ones(2, 3, 4) + @test FeatureTransforms.apply(x, T; inds=[1, 2]) == ones(2) + @test FeatureTransforms.apply(x, T; dims=2, inds=[2, 3]) == ones(2, 2, 4) + end + + @testset "OneToMany" begin + T = FakeOneToManyTransform() + @test FeatureTransforms.apply(x, T) == ones(2, 6, 4) + @test FeatureTransforms.apply(x, T; inds=[1, 2]) == ones(2, 2) + @test FeatureTransforms.apply(x, T; dims=2, inds=[2, 3]) == ones(2, 4, 4) + end + + @testset "ManyToOne" begin + T = FakeManyToOneTransform() + @test FeatureTransforms.apply(x, T; dims=1) == ones(3, 4) + @test FeatureTransforms.apply(x, T; dims=2, inds=[1, 2]) == ones(2, 4) + @test FeatureTransforms.apply(x, T; dims=3, inds=[1, 3]) == ones(2, 3) + end + + @testset "ManyToMany" begin + T = FakeManyToManyTransform() + @test FeatureTransforms.apply(x, T) == ones(2, 6, 4) + @test FeatureTransforms.apply(x, T; inds=[1, 2]) == ones(2, 2) + @test FeatureTransforms.apply(x, T; dims=2, inds=[2, 3]) == ones(2, 4, 4) + end + + end + + @testset "apply!" begin + + T = FakeOneToOneTransform() + + _x = copy(x) + FeatureTransforms.apply!(_x, T) + @test _x == ones(2, 3, 4) + + # https://github.com/invenia/FeatureTransforms.jl/issues/68 + _x = copy(x) + @test_broken FeatureTransforms.apply!(_x, T; dims=2, inds=[1, 2]) + @test_broken _x == cat( + [1 1 5; 1 1 6], [1 1 11; 1 1 12], [1 1 17; 1 1 18], [1 1 23; 1 1 24]; dims=3 + ) + end + + @testset "apply_append" begin + + @testset "OneToOne" begin + + T = FakeOneToOneTransform() + + @test FeatureTransforms.apply_append(x, T; append_dim=1) == vcat(x, ones(2, 3, 4)) + @test FeatureTransforms.apply_append(x, T; append_dim=2) == hcat(x, ones(2, 3, 4)) + @test FeatureTransforms.apply_append(x, T; append_dim=3) == cat(x, ones(2, 3, 4), dims=3) + + @test isequal( + FeatureTransforms.apply_append(x, T; dims=1, inds=[1], append_dim=1), + vcat(x, ones(1, 3, 4)) + ) + + @test isequal( + FeatureTransforms.apply_append(x, T; dims=2, inds=[1, 2], append_dim=2), + hcat(x, ones(2, 2, 4)) + ) + end + + @testset "OneToMany" begin + + T = FakeOneToManyTransform() + + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=1) + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=3) + + @test isequal( + FeatureTransforms.apply_append(x, T; append_dim=2), + hcat(x, ones(2, 6, 4)) + ) + + @test isequal( + FeatureTransforms.apply_append(x, T; dims=2, inds=[1, 2], append_dim=2), + hcat(x, ones(2, 4, 4)) + ) + end + + @testset "ManyToOne" begin + + T = FakeManyToOneTransform() + + @test isequal( + FeatureTransforms.apply_append(x, T; dims=1, append_dim=1), + vcat(x, ones(1, 3, 4)) + ) + + @test isequal( + FeatureTransforms.apply_append(x, T; dims=2, append_dim=2), + hcat(x, ones(2, 1, 4)) + ) + + @test isequal( + FeatureTransforms.apply_append(x, T; dims=3, append_dim=3), + cat(x, ones(2, 3, 1); dims=3) + ) + end + + @testset "ManyToMany" begin + + T = FakeManyToManyTransform() + + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=1) + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=3) + + @test isequal( + FeatureTransforms.apply_append(x, T; append_dim=2), + hcat(x, ones(2, 6, 4)) + ) + + @test isequal( + FeatureTransforms.apply_append(x, T; dims=2, inds=[1, 2], append_dim=2), + hcat(x, ones(2, 4, 4)) + ) + end + + end + +end diff --git a/test/types/matrix.jl b/test/types/matrix.jl new file mode 100644 index 0000000..f4b3fed --- /dev/null +++ b/test/types/matrix.jl @@ -0,0 +1,118 @@ +@testset "matrix" begin + + x = [1 2 3; 4 5 6] + + @test is_transformable(x) + + @testset "apply" begin + + @testset "OneToOne" begin + T = FakeOneToOneTransform() + @test FeatureTransforms.apply(x, T) == ones(2, 3) + @test FeatureTransforms.apply(x, T; inds=[1, 2]) == ones(2) + @test FeatureTransforms.apply(x, T; dims=2, inds=[2, 3]) == ones(2, 2) + end + + @testset "OneToMany" begin + T = FakeOneToManyTransform() + @test FeatureTransforms.apply(x, T) == ones(2, 6) + @test FeatureTransforms.apply(x, T; inds=[1, 2]) == ones(2, 2) + @test FeatureTransforms.apply(x, T; dims=2, inds=[2, 3]) == ones(2, 4) + end + + @testset "ManyToOne" begin + T = FakeManyToOneTransform() + @test FeatureTransforms.apply(x, T; dims=1) == ones(3) + @test FeatureTransforms.apply(x, T; dims=1, inds=[1, 2]) == ones(3) + @test FeatureTransforms.apply(x, T; dims=2, inds=[1, 2]) == ones(2) + end + + @testset "ManyToMany" begin + T = FakeManyToManyTransform() + @test FeatureTransforms.apply(x, T; dims=1) == ones(2, 6) + @test FeatureTransforms.apply(x, T; inds=[1, 2]) == ones(2, 2) + @test FeatureTransforms.apply(x, T; dims=2, inds=[2, 3]) == ones(2, 4) + end + + end + + @testset "apply!" begin + T = FakeOneToOneTransform() + + _x = copy(x) + FeatureTransforms.apply!(_x, T) + @test _x == ones(2, 3) + + # https://github.com/invenia/FeatureTransforms.jl/issues/68 + _x = copy(x) + @test_broken FeatureTransforms.apply!(_x, T; dims=2, inds=[1, 2]) + @test_broken _x == [1 1 3; 1 1 6] + end + + @testset "apply_append" begin + + @testset "OneToOne" begin + + T = FakeOneToOneTransform() + + @test FeatureTransforms.apply_append(x, T; append_dim=1) == vcat(x, ones(2, 3)) + @test FeatureTransforms.apply_append(x, T; append_dim=2) == hcat(x, ones(2, 3)) + @test FeatureTransforms.apply_append(x, T; append_dim=3) == cat(x, ones(2, 3), dims=3) + + @test isequal( + FeatureTransforms.apply_append(x, T; dims=1, inds=[1], append_dim=1), + vcat(x, ones(1, 3)) + ) + + @test isequal( + FeatureTransforms.apply_append(x, T; dims=2, inds=[1, 2], append_dim=2), + hcat(x, ones(2, 2)) + ) + end + + @testset "OneToMany" begin + + T = FakeOneToManyTransform() + + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=1) + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=3) + + @test isequal( + FeatureTransforms.apply_append(x, T; append_dim=2), + hcat(x, ones(2, 6)) + ) + + @test isequal( + FeatureTransforms.apply_append(x, T; inds=[1, 2], append_dim=2), + hcat(x, ones(2, 2)) + ) + end + + @testset "ManyToOne" begin + + T = FakeManyToOneTransform() + @test FeatureTransforms.apply_append(x, T; dims=1, append_dim=1) == vcat(x, ones(1, 3)) + @test FeatureTransforms.apply_append(x, T; dims=2, append_dim=2) == hcat(x, ones(2)) + end + + @testset "ManyToMany" begin + + T = FakeManyToManyTransform() + + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=1) + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=3) + + @test isequal( + FeatureTransforms.apply_append(x, T; append_dim=2), + hcat(x, ones(2, 6)) + ) + + @test isequal( + FeatureTransforms.apply_append(x, T; inds=[1, 2], append_dim=2), + hcat(x, ones(2, 2)) + ) + end + + end + +end diff --git a/test/types/tables.jl b/test/types/tables.jl new file mode 100644 index 0000000..38e5430 --- /dev/null +++ b/test/types/tables.jl @@ -0,0 +1,185 @@ +# TODO: test on rowtable https://github.com/invenia/FeatureTransforms.jl/issues/64 +@testset "$TableType" for TableType in (columntable, DataFrame) + + table = TableType((a=[1, 2, 3], b=[4, 5, 6])) + + @test is_transformable(table) + + @testset "apply" begin + + @testset "OneToOne" begin + + T = FakeOneToOneTransform() + + @test isequal( + FeatureTransforms.apply(table, T), + TableType((Column1=ones(3), Column2=ones(3))) + ) + @test isequal( + FeatureTransforms.apply(table, T; cols=:a), + TableType((Column1=ones(3), )) + ) + @test isequal( + FeatureTransforms.apply(table, T; header=[:x, :y]), + TableType((x=ones(3), y=ones(3))) + ) + end + + @testset "OneToMany" begin + + T = FakeOneToManyTransform() + + @test isequal( + FeatureTransforms.apply(table, T), + TableType((Column1=ones(3), Column2=ones(3), Column3=ones(3), Column4=ones(3))) + ) + @test isequal( + FeatureTransforms.apply(table, T; cols=:a), + TableType((Column1=ones(3), Column2=ones(3))) + ) + @test isequal( + FeatureTransforms.apply(table, T; header=[:x, :y, :p, :q]), + TableType((x=ones(3), y=ones(3), p=ones(3), q=ones(3))) + ) + end + + @testset "ManyToOne" begin + + T = FakeManyToOneTransform() + + @test isequal( + FeatureTransforms.apply(table, T), + TableType((Column1=ones(3),)) + ) + @test isequal( + FeatureTransforms.apply(table, T; cols=(:a, :b)), + TableType((Column1=ones(3),)) + ) + @test isequal( + FeatureTransforms.apply(table, T; header=[:x]), + TableType((x=ones(3),)) + ) + end + + @testset "ManyToMany" begin + + T = FakeManyToManyTransform() + + @test isequal( + FeatureTransforms.apply(table, T), + TableType(( + Column1=ones(3), Column2=ones(3), Column3=ones(3), Column4=ones(3) + )) + ) + @test isequal( + FeatureTransforms.apply(table, T; cols=(:a, :b)), + TableType(( + Column1=ones(3), Column2=ones(3), Column3=ones(3), Column4=ones(3) + )) + ) + + @test isequal( + FeatureTransforms.apply(table, T; header=[:p, :q, :r, :s]), + TableType((p=ones(3), q=ones(3), r=ones(3), s=ones(3))) + ) + end + + end + + # TODO: test passing in dims / inds too apply! after addressing + # https://github.com/invenia/FeatureTransforms.jl/issues/68 + @testset "apply!" begin + T = FakeOneToOneTransform() + + _table = deepcopy(table) + FeatureTransforms.apply!(_table, T) + @test _table == TableType((a=ones(3), b=ones(3))) + + _table = deepcopy(table) + FeatureTransforms.apply!(_table, T; cols=:a) + @test _table == TableType((a=[1, 1, 1], b=[4, 5, 6])) + + _table = deepcopy(table) + @test_broken FeatureTransforms.apply!(_table, T; cols=:b, dims=[1, 2]) + @test_broken _table == TableType((a=[1, 2, 3], b=[1, 1, 6])) + end + + @testset "apply_append" begin + + @testset "OneToOne" begin + + T = FakeOneToOneTransform() + + @test isequal( + FeatureTransforms.apply_append(table, T), + TableType((a=[1, 2, 3], b=[4, 5, 6], Column1=ones(3), Column2=ones(3))) + ) + + @test isequal( + FeatureTransforms.apply_append(table, T; header=[:x, :y]), + TableType((a=[1, 2, 3], b=[4, 5, 6], x=ones(3), y=ones(3))) + ) + end + + @testset "OneToMany" begin + + T = FakeOneToManyTransform() + + @test isequal( + FeatureTransforms.apply_append(table, T), + TableType(( + a=[1, 2, 3], b=[4, 5, 6], + Column1=ones(3), Column2=ones(3), Column3=ones(3), Column4=ones(3) + )) + ) + @test isequal( + FeatureTransforms.apply_append(table, T; cols=:a), + TableType((a=[1, 2, 3], b=[4, 5, 6], Column1=ones(3), Column2=ones(3))) + ) + @test isequal( + FeatureTransforms.apply_append(table, T; header=[:x, :y, :p, :q]), + TableType(( + a=[1, 2, 3], b=[4, 5, 6], x=ones(3), y=ones(3), p=ones(3), q=ones(3) + )) + ) + end + + @testset "ManyToOne" begin + + T = FakeManyToOneTransform() + + @test isequal( + FeatureTransforms.apply_append(table, T), + TableType((a=[1, 2, 3], b=[4, 5, 6], Column1=ones(3))) + ) + + @test isequal( + FeatureTransforms.apply_append(table, T; header=[:x]), + TableType((a=[1, 2, 3], b=[4, 5, 6], x=ones(3))) + ) + end + + @testset "ManyToMany" begin + + T = FakeManyToManyTransform() + + @test isequal( + FeatureTransforms.apply_append(table, T), + TableType(( + a=[1, 2, 3], b=[4, 5, 6], + Column1=ones(3), Column2=ones(3), + Column3=ones(3), Column4=ones(3), + )) + ) + + @test isequal( + FeatureTransforms.apply_append(table, T; header=[:p, :q, :r, :s]), + TableType(( + a=[1, 2, 3], b=[4, 5, 6], p=ones(3), q=ones(3), r=ones(3), s=ones(3) + )) + ) + end + + end + +end diff --git a/test/types/vector.jl b/test/types/vector.jl new file mode 100644 index 0000000..839ab2d --- /dev/null +++ b/test/types/vector.jl @@ -0,0 +1,86 @@ +@testset "vector" begin + + x = [1, 2, 3] + + @test is_transformable(x) + + @testset "apply" begin + + @testset "OneToOne" begin + T = FakeOneToOneTransform() + @test FeatureTransforms.apply(x, T) == ones(3) + @test FeatureTransforms.apply(x, T; inds=[2, 3]) == ones(2) + end + + @testset "OneToMany" begin + T = FakeOneToManyTransform() + @test FeatureTransforms.apply(x, T) == [ones(3) ones(3)] + @test FeatureTransforms.apply(x, T; inds=[1, 2]) == [ones(2) ones(2)] + end + + @testset "ManyToOne" begin + T = FakeManyToOneTransform() + @test FeatureTransforms.apply(x, T; dims=1) == fill(1.0) + @test FeatureTransforms.apply(x, T; dims=1, inds=[1, 2]) == fill(1.0) + @test_throws BoundsError FeatureTransforms.apply(x, T; dims=2) + end + + @testset "ManyToMany" begin + T = FakeManyToManyTransform() + @test FeatureTransforms.apply(x, T; dims=1) == [ones(3) ones(3)] + @test FeatureTransforms.apply(x, T; dims=1, inds=[1, 3]) == [ones(2) ones(2)] + @test_throws BoundsError FeatureTransforms.apply(x, T; dims=2) + end + + end + + @testset "apply!" begin + T = FakeOneToOneTransform() + + _x = copy(x) + FeatureTransforms.apply!(_x, T) + @test _x == ones(3) + + # https://github.com/invenia/FeatureTransforms.jl/issues/68 + _x = copy(x) + @test_broken FeatureTransforms.apply!(_x, T; inds=[1, 2]) + @test_broken _x == [1, 1, 3] + end + + @testset "apply_append" begin + + @testset "OneToOne" begin + T = FakeOneToOneTransform() + @test FeatureTransforms.apply_append(x, T; append_dim=1) == [1, 2, 3, 1, 1, 1] + @test FeatureTransforms.apply_append(x, T; append_dim=2) == [x ones(3)] + @test FeatureTransforms.apply_append(x, T; inds=[1, 2], append_dim=1) == [1, 2, 3, 1, 1] + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; inds=[1, 2], append_dim=2) + end + + @testset "OneToMany" begin + T = FakeOneToManyTransform() + @test FeatureTransforms.apply_append(x, T; append_dim=2) == [x ones(3) ones(3)] + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=1) + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; inds=[1, 2], append_dim=1) + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; inds=[1, 2], append_dim=2) + end + + @testset "ManyToOne" begin + T = FakeManyToOneTransform() + @test FeatureTransforms.apply_append(x, T; dims=1, append_dim=1) == [1, 2, 3, 1] + @test_throws BoundsError FeatureTransforms.apply_append(x, T; dims=1, append_dim=2) + @test_throws BoundsError FeatureTransforms.apply_append(x, T; dims=2, append_dim=1) + @test_throws BoundsError FeatureTransforms.apply_append(x, T; dims=2, append_dim=2) + end + + @testset "ManyToMany" begin + T = FakeManyToManyTransform() + @test FeatureTransforms.apply_append(x, T; append_dim=2) == [x ones(3) ones(3)] + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=1) + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; inds=[1, 2], append_dim=1) + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; inds=[1, 2], append_dim=2) + end + + end + +end diff --git a/test/types/xarray.jl b/test/types/xarray.jl new file mode 100644 index 0000000..f8bccdc --- /dev/null +++ b/test/types/xarray.jl @@ -0,0 +1,137 @@ +@testset "$ArrayType" for ArrayType in (AxisArray, KeyedArray) + + x = ArrayType([1 2 3; 4 5 6], foo=["a", "b"], bar=[:x, :y, :z]) + + @test is_transformable(x) + + @testset "apply" begin + + @testset "OneToOne" begin + T = FakeOneToOneTransform() + @test FeatureTransforms.apply(x, T) == ones(2, 3) + @test FeatureTransforms.apply(x, T; inds=[1, 2]) == ones(2) + @test FeatureTransforms.apply(x, T; dims=2, inds=[2, 3]) == ones(2, 2) + end + + @testset "OneToMany" begin + T = FakeOneToManyTransform() + @test FeatureTransforms.apply(x, T) == ones(2, 6) + @test FeatureTransforms.apply(x, T; inds=[1, 2]) == ones(2, 2) + @test FeatureTransforms.apply(x, T; dims=2, inds=[2, 3]) == ones(2, 4) + end + + @testset "ManyToOne" begin + T = FakeManyToOneTransform() + @test FeatureTransforms.apply(x, T; dims=1) == ones(3) + @test FeatureTransforms.apply(x, T; dims=1, inds=[1, 2]) == ones(3) + @test FeatureTransforms.apply(x, T; dims=2, inds=[1, 2]) == ones(2) + end + + @testset "ManyToMany" begin + T = FakeManyToManyTransform() + @test FeatureTransforms.apply(x, T; dims=1) == ones(2, 6) + @test FeatureTransforms.apply(x, T; inds=[1, 2]) == ones(2, 2) + @test FeatureTransforms.apply(x, T; dims=2, inds=[2, 3]) == ones(2, 4) + end + + end + + @testset "apply!" begin + T = FakeOneToOneTransform() + + _x = copy(x) + FeatureTransforms.apply!(_x, T) + @test _x == ones(2, 3) + + # https://github.com/invenia/FeatureTransforms.jl/issues/68 + @test_broken FeatureTransforms.apply!(copy(x), T; inds=[1, 2]) + end + + @testset "apply_append" begin + + @testset "OneToOne" begin + + T = FakeOneToOneTransform() + + @test FeatureTransforms.apply_append(x, T; append_dim=1) == vcat(x, ones(2, 3)) + @test FeatureTransforms.apply_append(x, T; append_dim=2) == hcat(x, ones(2, 3)) + @test FeatureTransforms.apply_append(x, T; append_dim=3) == cat(x, ones(2, 3), dims=3) + + @test isequal( + FeatureTransforms.apply_append(x, T; dims=1, inds=[1], append_dim=1), + vcat(x, ones(1, 3)) + ) + + @test isequal( + FeatureTransforms.apply_append(x, T; dims=2, inds=[1, 2], append_dim=2), + hcat(x, ones(2, 2)) + ) + end + + @testset "OneToMany" begin + + T = FakeOneToManyTransform() + + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=1) + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=3) + + @test isequal( + FeatureTransforms.apply_append(x, T; append_dim=2), + hcat(x, ones(2, 6)) + ) + + @test isequal( + FeatureTransforms.apply_append(x, T; inds=[1, 2], append_dim=2), + hcat(x, ones(2, 2)) + ) + end + + @testset "ManyToOne" begin + + T = FakeManyToOneTransform() + + @test FeatureTransforms.apply_append(x, T; dims=1, append_dim=1) == vcat(x, ones(1, 3)) + @test FeatureTransforms.apply_append(x, T; dims=2, append_dim=2) == hcat(x, ones(2)) + end + + @testset "ManyToMany" begin + + T = FakeManyToManyTransform() + + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=1) + @test_throws DimensionMismatch FeatureTransforms.apply_append(x, T; append_dim=3) + + @test isequal( + FeatureTransforms.apply_append(x, T; append_dim=2), + hcat(x, ones(2, 6)) + ) + + @test isequal( + FeatureTransforms.apply_append(x, T; inds=[1, 2], append_dim=2), + hcat(x, ones(2, 2)) + ) + end + + end + + if ArrayType == KeyedArray + @testset "indexing with dims" begin + + T = FakeOneToOneTransform() + + # apply + @test FeatureTransforms.apply(x, T; dims=:foo) == ones(2, 3) + @test FeatureTransforms.apply(x, T; dims=:bar) == ones(2, 3) + + # apply to certain dims/inds + @test FeatureTransforms.apply(x, T; dims=:bar, inds=Key([:x, :z])) == ones(2, 2) + + # apply_append + @test isequal( + FeatureTransforms.apply_append(x, T; dims=:foo, append_dim=:foo), + cat(x, ones(2, 3); dims=:foo) + ) + end + end + +end