Skip to content

Commit

Permalink
Use StableRNGs.jl and CairoMakie.jl in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juliohm committed May 28, 2024
1 parent 4dc3235 commit eed66e3
Show file tree
Hide file tree
Showing 22 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/kliep/convex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function DensityRatioEstimation._kliep_coeffs(K_nu, K_de, dre::KLIEP, optlib::Ty
problem = Convex.maximize(objective, constraints)

# solve problem with ECOS solver
Convex.solve!(problem, ECOS.Optimizer, silent_solver=true)
Convex.solve!(problem, ECOS.Optimizer, silent=true)

# optimal coefficients
vec.value)
Expand Down
8 changes: 2 additions & 6 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
[deps]
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
Convex = "f65535da-76fb-5f13-bab9-19810c17039a"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
ECOS = "e2685f51-7e38-5353-a97d-a921fd2c8199"
GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71"
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
GR = "=0.59.0"
Plots = "=1.22.4"
4 changes: 2 additions & 2 deletions test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
end

for (d_nu, d_de) in [pair₁, pair₂]
Random.seed!(123)
x_nu, x_de = rand(d_nu, 100), rand(d_de, 200)
rng = StableRNG(123)
x_nu, x_de = rand(rng, d_nu, 100), rand(rng, d_de, 200)
@testset "$(typeof(dre).name) -- $optlib" for (dre, optlib) in [
(KMM(), JuMPLib),
(KLIEP(), OptimLib),
Expand Down
Binary file modified test/data/KLIEP-ConvexLib-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/KLIEP-ConvexLib-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/KLIEP-OptimLib-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/KLIEP-OptimLib-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/KMM-JuMPLib-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/KMM-JuMPLib-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/LSIF-JuMPLib-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/LSIF-JuMPLib-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/LSIF-OptimLib-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/LSIF-OptimLib-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/uKMM-JuMPLib-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/uKMM-JuMPLib-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/uKMM-JuliaLib-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/uKMM-JuliaLib-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions test/kliep.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testset "KLIEP -- $optlib" for optlib in [OptimLib, ConvexLib]
for (i, (pair, rtol)) in enumerate([(pair₁, 2e-1), (pair₂, 4e-1)])
for (i, (pair, rtol)) in enumerate([(pair₁, 5e-1), (pair₂, 5e-1)])
d_nu, d_de = pair
rng = MersenneTwister(42)
rng = StableRNG(42)
x_nu, x_de = rand(rng, d_nu, 1_000), rand(rng, d_de, 500)

# estimated density ratio
Expand All @@ -16,7 +16,6 @@
@test r r̂ rtol = rtol

if visualtests
gr(size=(800, 800))
@test_reference "data/KLIEP-$optlib-$i.png" plot_d_nu(pair, x_de, r̂)
end
end
Expand Down
5 changes: 2 additions & 3 deletions test/kmm.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testset "$(nameof(dreType)) -- $optlib" for (dreType, optlib) in zip([uKMM, uKMM, KMM], [JuliaLib, JuMPLib, JuMPLib])
for (i, (pair, rtol)) in enumerate([(pair₁, 2e-1), (pair₂, 4e-1)])
for (i, (pair, rtol)) in enumerate([(pair₁, 5e-1), (pair₂, 5e-1)])
d_nu, d_de = pair
rng = MersenneTwister(42)
rng = StableRNG(42)
x_nu, x_de = rand(rng, d_nu, 2_000), rand(rng, d_de, 1_000)

# estimated density ratio
Expand Down Expand Up @@ -33,7 +33,6 @@
end

if visualtests
gr(size=(800, 800))
@test_reference "data/$(nameof(dreType))-$optlib-$i.png" plot_d_nu(pair, x_de, r̂)
end
end
Expand Down
5 changes: 2 additions & 3 deletions test/lsif.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testset "LSIF -- $optlib" for optlib in [OptimLib, JuMPLib]
for (i, (pair, rtol)) in enumerate([(pair₁, 2e-1), (pair₂, 4e-1)])
for (i, (pair, rtol)) in enumerate([(pair₁, 5e-1), (pair₂, 5e-1)])
d_nu, d_de = pair
rng = MersenneTwister(42)
rng = StableRNG(42)
x_nu, x_de = rand(rng, d_nu, 1000), rand(rng, d_de, 500)

# estimated density ratio
Expand All @@ -16,7 +16,6 @@
@test r r̂ rtol = rtol

if visualtests
gr(size=(800, 800))
@test_reference "data/LSIF-$optlib-$i.png" plot_d_nu(pair, x_de, r̂)
end
end
Expand Down
5 changes: 2 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ using Statistics
using Optim
using JuMP, Ipopt
using Convex, ECOS
using Test, Random, Plots
using Test, StableRNGs
using ReferenceTests, ImageIO

# workaround GR warnings
ENV["GKSwstype"] = "100"
import CairoMakie as Mke

# environment settings
isCI = "CI" keys(ENV)
Expand Down
8 changes: 6 additions & 2 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ function plot_d_nu(pair, x_de, r̂)
ps = pdf.(d_nu, xs)
p̂s = r̂s .* pdf.(d_de, xs)

plot(xs, ps, label="d_nu (actual)")
plot!(xs, p̂s, label="d_nu (estimate)")
fig = Mke.Figure(size=(800, 800))
ax = Mke.Axis(fig[1,1])
Mke.lines!(ax, xs, ps, label="d_nu (actual)")
Mke.lines!(ax, xs, p̂s, label="d_nu (estimate)")
Mke.axislegend(ax)
fig
end

0 comments on commit eed66e3

Please sign in to comment.