Skip to content

Commit

Permalink
Remove matrix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Glenn Moynihan committed Jun 18, 2021
1 parent 520f1ca commit 135e533
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 287 deletions.
66 changes: 15 additions & 51 deletions test/linear_combination.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,23 @@
@test lc isa Transform
@test cardinality(lc) == ManyToOne()

@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])
@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

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
Expand Down
26 changes: 0 additions & 26 deletions test/one_hot_encoding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,6 @@
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)
Expand Down
42 changes: 0 additions & 42 deletions test/periodic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,36 +102,6 @@
expected = expected_dict[f]
p = Periodic(f, 5, 2)

@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"])
Expand Down Expand Up @@ -270,18 +240,6 @@
@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(
Expand Down
27 changes: 0 additions & 27 deletions test/power.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,6 @@
@test p isa Transform
@test cardinality(p) == OneToOne()

@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]
Expand Down
122 changes: 15 additions & 107 deletions test/scaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,6 @@
@test IdentityScaling([1, 2, 3]) == IdentityScaling()
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"])
Expand Down Expand Up @@ -166,79 +129,24 @@
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)
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

@test transformed M_expected atol=1e-5
@test FeatureTransforms.apply(transformed, scaling; inverse=true) M atol=1e-5
end
@testset "Inverse" 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]
scaling = MeanStdScaling(M)
transformed = FeatureTransforms.apply(M, scaling)

@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
@test transformed M_expected atol=1e-5
@test FeatureTransforms.apply(transformed, scaling; inverse=true) M atol=1e-5
end

@testset "AxisArray" begin
Expand Down
33 changes: 0 additions & 33 deletions test/temporal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,6 @@
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);
Expand Down
2 changes: 1 addition & 1 deletion test/types/matrix.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testset "vector" begin
@testset "matrix" begin

@testset "is_transformable" begin
x = [1 2 3; 4 5 6]
Expand Down

0 comments on commit 135e533

Please sign in to comment.