From c56a1a1e7486774aef52ee1241d019c40361844a Mon Sep 17 00:00:00 2001 From: Glenn Moynihan Date: Fri, 18 Jun 2021 17:04:50 +0100 Subject: [PATCH] Remove KeyedArray and AxisArray tests --- test/linear_combination.jl | 96 -------------- test/one_hot_encoding.jl | 56 -------- test/periodic.jl | 159 ---------------------- test/power.jl | 67 +--------- test/scaling.jl | 264 ------------------------------------- test/temporal.jl | 80 ----------- 6 files changed, 1 insertion(+), 721 deletions(-) diff --git a/test/linear_combination.jl b/test/linear_combination.jl index 6b6305c..664f4f5 100644 --- a/test/linear_combination.jl +++ b/test/linear_combination.jl @@ -29,100 +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 end diff --git a/test/one_hot_encoding.jl b/test/one_hot_encoding.jl index e084fef..e015bbb 100644 --- a/test/one_hot_encoding.jl +++ b/test/one_hot_encoding.jl @@ -29,60 +29,4 @@ @test_throws KeyError FeatureTransforms.apply(x, ohe) 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 end diff --git a/test/periodic.jl b/test/periodic.jl index d9fa806..089e83e 100644 --- a/test/periodic.jl +++ b/test/periodic.jl @@ -78,120 +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 "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 - end end @testset "TimeType input" begin @@ -239,51 +125,6 @@ @test FeatureTransforms.apply(x, p) ≈ expected atol=1e-14 @test p(x) ≈ expected atol=1e-14 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 end end diff --git a/test/power.jl b/test/power.jl index a8bdb48..04d2352 100644 --- a/test/power.jl +++ b/test/power.jl @@ -4,71 +4,6 @@ @test p isa Transform @test cardinality(p) == OneToOne() - @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] + @test FeatureTransforms.apply([1, 2, 3], p) == [1, 8, 27] - @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 end diff --git a/test/scaling.jl b/test/scaling.jl index c1875ad..2846b27 100644 --- a/test/scaling.jl +++ b/test/scaling.jl @@ -9,93 +9,6 @@ @test IdentityScaling(123) == IdentityScaling() @test IdentityScaling([1, 2, 3]) == IdentityScaling() 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 end @testset "MeanStdScaling" begin @@ -149,183 +62,6 @@ @test FeatureTransforms.apply(transformed, scaling; inverse=true) ≈ M atol=1e-5 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.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 - - @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 "Zero std" begin x = ones(Float64, 3) expected = zeros(Float64, 3) diff --git a/test/temporal.jl b/test/temporal.jl index 6cf23a5..bcd1425 100644 --- a/test/temporal.jl +++ b/test/temporal.jl @@ -29,84 +29,4 @@ @test hod(x) == expected_dst 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 end