Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71"
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
Expand All @@ -10,6 +9,7 @@ ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TypedTables = "9d95f2ec-7b3d-5a63-8d20-e2491e220bb9"

[compat]
Plots = "=1.22.4"
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using TableTransforms
using Distributions
using Tables
using DataFrames
using TypedTables
using LinearAlgebra
using Statistics
using Test, Random, Plots
Expand Down
98 changes: 84 additions & 14 deletions test/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,94 @@
@testset "Identity" begin
x = rand(4000)
y = rand(4000)
t = DataFrame(:x => x, :y => y)
t = Table(; x, y)
n, c = apply(Identity(), t)
@test t == n
tₒ = revert(Identity(), n, c)
@test t == tₒ
end

@testset "Select" begin
a = rand(4000)
b = rand(4000)
c = rand(4000)
d = rand(4000)
e = rand(4000)
f = rand(4000)
t = Table(; a, b, c, d, e, f)

n₁, c₁ = apply(Select(:f, :d), t)
n₂, c₂ = apply(Select(:f, :d, :b), t)
n₃, c₃ = apply(Select(:d, :c, :b), t)
n₄, c₄ = apply(Select(:e, :c, :b, :a), t)

u₁ = Tables.columnnames(n₁)
u₂ = Tables.columnnames(n₂)
u₃ = Tables.columnnames(n₃)
u₄ = Tables.columnnames(n₄)

@test u₁ == (:f, :d)
@test u₂ == (:f, :d, :b)
@test u₃ == (:d, :c, :b)
@test u₄ == (:e, :c, :b, :a)

tₒ₁ = revert(Select(:f, :d), n₁, c₁)
tₒ₂ = revert(Select(:f, :d, :b), n₂, c₂)
tₒ₃ = revert(Select(:d, :c, :b), n₃, c₃)
tₒ₄ = revert(Select(:e, :c, :b, :a), n₄, c₄)

@test t == tₒ₁
@test t == tₒ₂
@test t == tₒ₃
@test t == tₒ₄
end

@testset "Reject" begin
a = rand(4000)
b = rand(4000)
c = rand(4000)
d = rand(4000)
e = rand(4000)
f = rand(4000)
t = Table(; a, b, c, d, e, f)

n₁, c₁ = apply(Reject(:f, :d), t)
n₂, c₂ = apply(Reject(:f, :d, :b), t)
n₃, c₃ = apply(Reject(:d, :c, :b), t)
n₄, c₄ = apply(Reject(:e, :c, :b, :a), t)

u₁ = Tables.columnnames(n₁)
u₂ = Tables.columnnames(n₂)
u₃ = Tables.columnnames(n₃)
u₄ = Tables.columnnames(n₄)

@test u₁ == (:a, :b, :c, :e)
@test u₂ == (:a, :c, :e)
@test u₃ == (:a, :e, :f)
@test u₄ == (:d, :f)

tₒ₁ = revert(Reject(:f, :d), n₁, c₁)
tₒ₂ = revert(Reject(:f, :d, :b), n₂, c₂)
tₒ₃ = revert(Reject(:d, :c, :b), n₃, c₃)
tₒ₄ = revert(Reject(:e, :c, :b, :a), n₄, c₄)

@test t == tₒ₁
@test t == tₒ₂
@test t == tₒ₃
@test t == tₒ₄
end

@testset "Center" begin
Random.seed!(42) # to reproduce the results
x = rand(Normal(2,1), 4000)
y = rand(Normal(5,1), 4000)
t = DataFrame(:x => x, :y => y)
t = Table(; x, y)
n, c = apply(Center(), t)
μ = mean(Tables.matrix(n), dims=1)
@test isapprox(μ[1], 0; atol=1e-6)
@test isapprox(μ[2], 0; atol=1e-6)
tₒ = revert(Center(), n, c)
@test ttₒ
@test Tables.matrix(t)Tables.matrix(tₒ)

# visual tests
if visualtests
Expand All @@ -35,14 +105,14 @@
Random.seed!(42) # to reproduce the results
x = rand(Normal(4,3), 4000)
y = rand(Normal(7,5), 4000)
t = DataFrame(:x => x, :y => y)
t = Table(; x, y)
n, c = apply(Scale(low=0, high=1), t)
@test all(x -> x <= 1, n.x)
@test all(x -> x >= 0, n.x)
@test all(y -> y <= 1, n.y)
@test all(y -> y >= 0, n.y)
tₒ = revert(Scale(low=0, high=1), n, c)
@test ttₒ
@test Tables.matrix(t)Tables.matrix(tₒ)

# visual tests
if visualtests
Expand All @@ -58,7 +128,7 @@
Random.seed!(42) # to reproduce the results
x = rand(Normal(7,10), 4000)
y = rand(Normal(15,2), 4000)
t = DataFrame(:x => x, :y => y)
t = Table(; x, y)
n, c = apply(ZScore(), t)
μ = mean(Tables.matrix(n), dims=1)
σ = std(Tables.matrix(n), dims=1)
Expand All @@ -67,7 +137,7 @@
@test isapprox(μ[2], 0; atol=1e-6)
@test isapprox(σ[2], 1; atol=1e-6)
tₒ = revert(ZScore(), n, c)
@test ttₒ
@test Tables.matrix(t)Tables.matrix(tₒ)

# visual tests
if visualtests
Expand All @@ -83,46 +153,46 @@
# PCA test
x = rand(Normal(0,10), 1500)
y = x + rand(Normal(0,2), 1500)
t = DataFrame(:x => x, :y => y)
t = Table(; x, y)
n, c = apply(EigenAnalysis(:V), t)
Σ = cov(Tables.matrix(n))
@test Σ[1,1] > 1
@test isapprox(Σ[1,2], 0; atol=1e-6)
@test isapprox(Σ[2,1], 0; atol=1e-6)
@test Σ[2,2] > 1
tₒ = revert(EigenAnalysis(:V), n, c)
@test ttₒ
@test Tables.matrix(t)Tables.matrix(tₒ)

# DRS test
x = rand(Normal(0,10), 1500)
y = x + rand(Normal(0,2), 1500)
t = DataFrame(:x => x, :y => y)
t = Table(; x, y)
n, c = apply(EigenAnalysis(:VD), t)
Σ = cov(Tables.matrix(n))
@test isapprox(Σ[1,2], 0; atol=1e-6)
@test isapprox(Σ[2,1], 0; atol=1e-6)
@test isapprox(Σ[1,1], 1; atol=1e-6)
@test isapprox(Σ[2,2], 1; atol=1e-6)
tₒ = revert(EigenAnalysis(:VD), n, c)
@test ttₒ
@test Tables.matrix(t)Tables.matrix(tₒ)

# SDS test
x = rand(Normal(0,10), 1500)
y = x + rand(Normal(0,2), 1500)
t = DataFrame(:x => x, :y => y)
t = Table(; x, y)
n, c = apply(EigenAnalysis(:VDV), t)
Σ = cov(Tables.matrix(n))
@test isapprox(Σ[1,2], 0; atol=1e-6)
@test isapprox(Σ[2,1], 0; atol=1e-6)
@test isapprox(Σ[1,1], 1; atol=1e-6)
@test isapprox(Σ[2,2], 1; atol=1e-6)
tₒ = revert(EigenAnalysis(:VDV), n, c)
@test ttₒ
@test Tables.matrix(t)Tables.matrix(tₒ)

Random.seed!(42) # to reproduce the results
x = rand(Normal(0,10), 4000)
y = x + rand(Normal(0,2), 4000)
t₁ = DataFrame(:x => x, :y => y)
t₁ = Table(; x, y)
t₂, c₂ = apply(EigenAnalysis(:V), t₁)
t₃, c₃ = apply(EigenAnalysis(:VD), t₁)
t₄, c₄ = apply(EigenAnalysis(:VDV), t₁)
Expand Down