Skip to content

Commit da8e83a

Browse files
authored
Use StableRNGs.jl in tests (#294)
1 parent 648d3c6 commit da8e83a

12 files changed

+29
-29
lines changed

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1111
PairPlots = "43a3c2be-4208-490b-832a-a21dcd55d7da"
1212
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1313
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
14+
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
1415
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1516
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1617
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

test/data/eigenanalysis-1.png

-45.7 KB
Loading

test/data/eigenanalysis-2.png

-73.1 KB
Loading

test/data/projectionpursuit-1.png

-65.8 KB
Loading

test/data/projectionpursuit-2.png

-134 KB
Loading

test/data/projectionpursuit-3.png

-53 KB
Loading

test/runtests.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ using StatsBase
1010
using Statistics
1111
using DelimitedFiles
1212
using ReferenceTests
13+
using StableRNGs
1314
using PairPlots
1415
using ImageIO
15-
using Random
1616
using Test
1717

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

33-
# using MersenneTwister for backward
34-
# compatibility with old Julia versions
35-
rng = MersenneTwister(42)
33+
# using StableRNG for compatibility between Julia versions
34+
rng = StableRNG(42)
3635

3736
# for functor tests in Functional testset
3837
struct Polynomial{T<:Real}

test/shows.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
├─ weights: nothing
144144
├─ replace: false
145145
├─ ordered: true
146-
└─ rng: TaskLocalRNG()"""
146+
└─ rng: Random.TaskLocalRNG()"""
147147
end
148148

149149
@testset "Filter" begin

test/transforms/eigenanalysis.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
@test TT.parameters(EigenAnalysis(:V)) == (proj=:V, maxdim=nothing, pratio=1.0)
33

44
# PCA test
5-
x = rand(Normal(0, 10), 1500)
6-
y = x + rand(Normal(0, 2), 1500)
5+
x = rand(rng, Normal(0, 10), 1500)
6+
y = x + rand(rng, Normal(0, 2), 1500)
77
t = Table(; x, y)
88
T = EigenAnalysis(:V)
99
n, c = apply(T, t)
@@ -16,8 +16,8 @@
1616
@test Tables.matrix(t) Tables.matrix(tₒ)
1717

1818
# DRS test
19-
x = rand(Normal(0, 10), 1500)
20-
y = x + rand(Normal(0, 2), 1500)
19+
x = rand(rng, Normal(0, 10), 1500)
20+
y = x + rand(rng, Normal(0, 2), 1500)
2121
t = Table(; x, y)
2222
T = EigenAnalysis(:VD)
2323
n, c = apply(T, t)
@@ -30,8 +30,8 @@
3030
@test Tables.matrix(t) Tables.matrix(tₒ)
3131

3232
# SDS test
33-
x = rand(Normal(0, 10), 1500)
34-
y = x + rand(Normal(0, 2), 1500)
33+
x = rand(rng, Normal(0, 10), 1500)
34+
y = x + rand(rng, Normal(0, 2), 1500)
3535
t = Table(; x, y)
3636
T = EigenAnalysis(:VDV)
3737
n, c = apply(T, t)
@@ -73,8 +73,8 @@
7373
end
7474

7575
# row table
76-
x = rand(Normal(0, 10), 1500)
77-
y = x + rand(Normal(0, 2), 1500)
76+
x = rand(rng, Normal(0, 10), 1500)
77+
y = x + rand(rng, Normal(0, 2), 1500)
7878
t = Table(; x, y)
7979
rt = Tables.rowtable(t)
8080
T = EigenAnalysis(:V)
@@ -84,9 +84,9 @@
8484
@test Tables.matrix(rt) Tables.matrix(rtₒ)
8585

8686
# maxdim
87-
x = randn(1000)
88-
y = x + randn(1000)
89-
z = 2x - y + randn(1000)
87+
x = randn(rng, 1000)
88+
y = x + randn(rng, 1000)
89+
z = 2x - y + randn(rng, 1000)
9090
t = Table(; x, y, z)
9191

9292
# PCA

test/transforms/filter.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
@test Tables.isrowtable(n)
105105

106106
# performance tests
107-
trng = MersenneTwister(2) # test rng
107+
trng = StableRNG(2) # test rng
108108
x = rand(trng, 100_000)
109109
y = rand(trng, 100_000)
110110
c = CoDaArray((a=rand(trng, 100_000), b=rand(trng, 100_000), c=rand(trng, 100_000)))

test/transforms/projectionpursuit.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
@testset "ProjectionPursuit" begin
22
@test TT.parameters(ProjectionPursuit()) == (tol=1.0e-6, deg=5, perc=0.9, n=100)
33

4-
rng = MersenneTwister(42)
4+
rng = StableRNG(42)
55
N = 10_000
66
a = [2randn(rng, N ÷ 2) .+ 6; randn(rng, N ÷ 2)]
77
b = [3randn(rng, N ÷ 2); 2randn(rng, N ÷ 2)]
88
c = randn(rng, N)
99
d = c .+ 0.6randn(rng, N)
1010
t = (; a, b, c, d)
1111

12-
T = ProjectionPursuit(rng=MersenneTwister(2))
12+
T = ProjectionPursuit(rng=StableRNG(2))
1313
n, c = apply(T, t)
1414

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

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

2222
tₒ = revert(T, n, c)
2323

@@ -35,7 +35,7 @@
3535
b = rand(rng, BetaPrime(2), 4000)
3636
t = Table(; a, b)
3737

38-
T = ProjectionPursuit(rng=MersenneTwister(2))
38+
T = ProjectionPursuit(rng=StableRNG(2))
3939
n, c = apply(T, t)
4040

4141
μ = mean(Tables.matrix(n), dims=1)

test/transforms/sample.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
@test n.c t.c
2929
@test Tables.rowtable(n) == trows
3030

31-
T = Sample(8, replace=true, rng=MersenneTwister(2))
31+
T = Sample(8, replace=true, rng=StableRNG(2))
3232
n, c = apply(T, t)
33-
@test n.a == [3, 7, 8, 2, 2, 6, 2, 6]
34-
@test n.b == [8, 2, 3, 1, 1, 5, 1, 5]
35-
@test n.c == [1, 2, 9, 5, 5, 8, 5, 8]
33+
@test n.a == [3, 3, 6, 3, 6, 6, 7, 3]
34+
@test n.b == [4, 8, 5, 4, 5, 5, 2, 8]
35+
@test n.c == [4, 1, 8, 4, 8, 8, 2, 1]
3636

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

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

5959
# performance tests
60-
trng = MersenneTwister(2) # test rng
60+
trng = StableRNG(2) # test rng
6161
x = rand(trng, 100_000)
6262
y = rand(trng, 100_000)
6363
c = CoDaArray((a=rand(trng, 100_000), b=rand(trng, 100_000), c=rand(trng, 100_000)))

0 commit comments

Comments
 (0)