Skip to content

Commit

Permalink
Use StableRNGs.jl in tests (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv authored Oct 17, 2024
1 parent 648d3c6 commit da8e83a
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 29 deletions.
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PairPlots = "43a3c2be-4208-490b-832a-a21dcd55d7da"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Expand Down
Binary file modified test/data/eigenanalysis-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/data/eigenanalysis-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/data/projectionpursuit-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/data/projectionpursuit-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/data/projectionpursuit-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ using StatsBase
using Statistics
using DelimitedFiles
using ReferenceTests
using StableRNGs
using PairPlots
using ImageIO
using Random
using Test

import CairoMakie as Mke
Expand All @@ -30,9 +30,8 @@ islinux = Sys.islinux()
visualtests = !isCI || (isCI && islinux)
datadir = joinpath(@__DIR__, "data")

# using MersenneTwister for backward
# compatibility with old Julia versions
rng = MersenneTwister(42)
# using StableRNG for compatibility between Julia versions
rng = StableRNG(42)

# for functor tests in Functional testset
struct Polynomial{T<:Real}
Expand Down
2 changes: 1 addition & 1 deletion test/shows.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
├─ weights: nothing
├─ replace: false
├─ ordered: true
└─ rng: TaskLocalRNG()"""
└─ rng: Random.TaskLocalRNG()"""
end

@testset "Filter" begin
Expand Down
22 changes: 11 additions & 11 deletions test/transforms/eigenanalysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
@test TT.parameters(EigenAnalysis(:V)) == (proj=:V, maxdim=nothing, pratio=1.0)

# PCA test
x = rand(Normal(0, 10), 1500)
y = x + rand(Normal(0, 2), 1500)
x = rand(rng, Normal(0, 10), 1500)
y = x + rand(rng, Normal(0, 2), 1500)
t = Table(; x, y)
T = EigenAnalysis(:V)
n, c = apply(T, t)
Expand All @@ -16,8 +16,8 @@
@test Tables.matrix(t) Tables.matrix(tₒ)

# DRS test
x = rand(Normal(0, 10), 1500)
y = x + rand(Normal(0, 2), 1500)
x = rand(rng, Normal(0, 10), 1500)
y = x + rand(rng, Normal(0, 2), 1500)
t = Table(; x, y)
T = EigenAnalysis(:VD)
n, c = apply(T, t)
Expand All @@ -30,8 +30,8 @@
@test Tables.matrix(t) Tables.matrix(tₒ)

# SDS test
x = rand(Normal(0, 10), 1500)
y = x + rand(Normal(0, 2), 1500)
x = rand(rng, Normal(0, 10), 1500)
y = x + rand(rng, Normal(0, 2), 1500)
t = Table(; x, y)
T = EigenAnalysis(:VDV)
n, c = apply(T, t)
Expand Down Expand Up @@ -73,8 +73,8 @@
end

# row table
x = rand(Normal(0, 10), 1500)
y = x + rand(Normal(0, 2), 1500)
x = rand(rng, Normal(0, 10), 1500)
y = x + rand(rng, Normal(0, 2), 1500)
t = Table(; x, y)
rt = Tables.rowtable(t)
T = EigenAnalysis(:V)
Expand All @@ -84,9 +84,9 @@
@test Tables.matrix(rt) Tables.matrix(rtₒ)

# maxdim
x = randn(1000)
y = x + randn(1000)
z = 2x - y + randn(1000)
x = randn(rng, 1000)
y = x + randn(rng, 1000)
z = 2x - y + randn(rng, 1000)
t = Table(; x, y, z)

# PCA
Expand Down
2 changes: 1 addition & 1 deletion test/transforms/filter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
@test Tables.isrowtable(n)

# performance tests
trng = MersenneTwister(2) # test rng
trng = StableRNG(2) # test rng
x = rand(trng, 100_000)
y = rand(trng, 100_000)
c = CoDaArray((a=rand(trng, 100_000), b=rand(trng, 100_000), c=rand(trng, 100_000)))
Expand Down
10 changes: 5 additions & 5 deletions test/transforms/projectionpursuit.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
@testset "ProjectionPursuit" begin
@test TT.parameters(ProjectionPursuit()) == (tol=1.0e-6, deg=5, perc=0.9, n=100)

rng = MersenneTwister(42)
rng = StableRNG(42)
N = 10_000
a = [2randn(rng, N ÷ 2) .+ 6; randn(rng, N ÷ 2)]
b = [3randn(rng, N ÷ 2); 2randn(rng, N ÷ 2)]
c = randn(rng, N)
d = c .+ 0.6randn(rng, N)
t = (; a, b, c, d)

T = ProjectionPursuit(rng=MersenneTwister(2))
T = ProjectionPursuit(rng=StableRNG(2))
n, c = apply(T, t)

@test Tables.columnnames(n) == (:PP1, :PP2, :PP3, :PP4)

μ = mean(Tables.matrix(n), dims=1)
Σ = cov(Tables.matrix(n))
@test all(isapprox.(μ, 0, atol=1e-8))
@test all(isapprox.(Σ, I(4), atol=1e-8))
@test all(isapprox.(μ, 0, atol=1e-3))
@test all(isapprox.(Σ, I(4), atol=1e-2))

tₒ = revert(T, n, c)

Expand All @@ -35,7 +35,7 @@
b = rand(rng, BetaPrime(2), 4000)
t = Table(; a, b)

T = ProjectionPursuit(rng=MersenneTwister(2))
T = ProjectionPursuit(rng=StableRNG(2))
n, c = apply(T, t)

μ = mean(Tables.matrix(n), dims=1)
Expand Down
14 changes: 7 additions & 7 deletions test/transforms/sample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
@test n.c t.c
@test Tables.rowtable(n) == trows

T = Sample(8, replace=true, rng=MersenneTwister(2))
T = Sample(8, replace=true, rng=StableRNG(2))
n, c = apply(T, t)
@test n.a == [3, 7, 8, 2, 2, 6, 2, 6]
@test n.b == [8, 2, 3, 1, 1, 5, 1, 5]
@test n.c == [1, 2, 9, 5, 5, 8, 5, 8]
@test n.a == [3, 3, 6, 3, 6, 6, 7, 3]
@test n.b == [4, 8, 5, 4, 5, 5, 2, 8]
@test n.c == [4, 1, 8, 4, 8, 8, 2, 1]

w = pweights([0.1, 0.25, 0.15, 0.25, 0.1, 0.15])
T = Sample(10_000, w, replace=true, rng=MersenneTwister(2))
T = Sample(10_000, w, replace=true, rng=StableRNG(2))
n, c = apply(T, t)
nrows = Tables.rowtable(n)
@test isapprox(count(==(trows[1]), nrows) / 10_000, 0.10, atol=0.01)
Expand All @@ -46,7 +46,7 @@
@test isapprox(count(==(trows[6]), nrows) / 10_000, 0.15, atol=0.01)

w = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
T = Sample(10_000, w, replace=true, rng=MersenneTwister(2))
T = Sample(10_000, w, replace=true, rng=StableRNG(2))
n, c = apply(T, t)
nrows = Tables.rowtable(n)
@test isapprox(count(==(trows[1]), nrows) / 10_000, 1 / 21, atol=0.01)
Expand All @@ -57,7 +57,7 @@
@test isapprox(count(==(trows[6]), nrows) / 10_000, 6 / 21, atol=0.01)

# performance tests
trng = MersenneTwister(2) # test rng
trng = StableRNG(2) # test rng
x = rand(trng, 100_000)
y = rand(trng, 100_000)
c = CoDaArray((a=rand(trng, 100_000), b=rand(trng, 100_000), c=rand(trng, 100_000)))
Expand Down

0 comments on commit da8e83a

Please sign in to comment.