Skip to content

Commit

Permalink
Simplify test_utils
Browse files Browse the repository at this point in the history
Turn all of them into modules or merge them into other files that used
to `include` them.
  • Loading branch information
mhauru committed May 31, 2024
1 parent 0b80dbc commit 2d7b101
Show file tree
Hide file tree
Showing 21 changed files with 53 additions and 49 deletions.
3 changes: 1 addition & 2 deletions test/essential/ad.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module AdTests

using ..Models: gdemo_default
using Distributions: logpdf
using DynamicPPL: getlogp, getval
using ForwardDiff
Expand All @@ -12,8 +13,6 @@ using Turing
using Turing: SampleFromPrior
using Zygote

include(pkgdir(Turing) * "/test/test_utils/models.jl")

function test_model_ad(model, f, syms::Vector{Symbol})
# Set up VI.
vi = Turing.VarInfo(model)
Expand Down
6 changes: 3 additions & 3 deletions test/experimental/gibbs.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module ExperimentalGibbsTests

using ..Models: MoGtest_default, MoGtest_default_z_vector, gdemo
using ..NumericalTests: check_MoGtest_default, check_MoGtest_default_z_vector, check_gdemo,
check_numerical
using DynamicPPL
using Random
using Test
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

function check_transition_varnames(
transition::Turing.Inference.Transition,
parent_varnames
Expand Down
3 changes: 1 addition & 2 deletions test/ext/Optimisation.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module OptimisationTests

using ..Models: gdemo, gdemo_default
import Random
using Optimization
using OptimizationOptimJL: BFGS, Fminbox, LBFGS, NelderMead, optimize
using Test: @test, @testset
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")

@testset "ext/Optimisation.jl" begin
@testset "gdemo" begin
@testset "MLE" begin
Expand Down
5 changes: 2 additions & 3 deletions test/ext/dynamichmc.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module DynamicHMCTests

using ..Models: gdemo_default
using ..NumericalTests: check_gdemo
using Test: @test, @testset
using Distributions: sample
import DynamicHMC
Expand All @@ -8,9 +10,6 @@ using DynamicPPL: Sampler
import Random
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

@testset "TuringDynamicHMCExt" begin
Random.seed!(100)

Expand Down
5 changes: 2 additions & 3 deletions test/mcmc/Inference.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module InferenceTests

using ..Models: gdemo_d, gdemo_default
using ..NumericalTests: check_gdemo, check_numerical
using Distributions: Bernoulli, Beta, InverseGamma, Normal
using Distributions: sample
import DynamicPPL
Expand All @@ -12,9 +14,6 @@ import ReverseDiff
using Test: @test, @test_throws, @testset
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

@testset "Testing inference.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false))
# Only test threading if 1.3+.
if VERSION > v"1.2"
Expand Down
5 changes: 2 additions & 3 deletions test/mcmc/emcee.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
module EmceeTests

using ..Models: gdemo_default
using ..NumericalTests: check_gdemo
using Distributions: sample
import DynamicPPL
using DynamicPPL: Sampler
import Random
using Test: @test, @test_throws, @testset
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

@testset "emcee.jl" begin
@testset "gdemo" begin
Random.seed!(9876)
Expand Down
5 changes: 2 additions & 3 deletions test/mcmc/ess.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
module ESSTests

using ..Models: MoGtest, MoGtest_default, gdemo, gdemo_default
using ..NumericalTests: check_MoGtest_default, check_numerical
using Distributions: Normal, sample
import DynamicPPL
using DynamicPPL: Sampler
import Random
using Test: @test, @testset
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

@testset "ESS" begin
@model function demo(x)
m ~ Normal()
Expand Down
5 changes: 2 additions & 3 deletions test/mcmc/gibbs.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module GibbsTests

using ..Models: MoGtest_default, gdemo, gdemo_default
using ..NumericalTests: check_MoGtest_default, check_gdemo, check_numerical
using Distributions: InverseGamma, Normal
using Distributions: sample
import ForwardDiff
Expand All @@ -10,9 +12,6 @@ using Turing
using Turing: Inference
using Turing.RandomMeasures: ChineseRestaurantProcess, DirichletProcess

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

@testset "Testing gibbs.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false))
@testset "gibbs constructor" begin
N = 500
Expand Down
5 changes: 2 additions & 3 deletions test/mcmc/gibbs_conditional.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module GibbsConditionalTests

using ..Models: gdemo, gdemo_default
using ..NumericalTests: check_gdemo, check_numerical
import Clustering
using Distributions: Categorical, InverseGamma, Normal, sample
import ForwardDiff
Expand All @@ -12,9 +14,6 @@ import StatsFuns
using Test: @test, @testset
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

@testset "Testing gibbs conditionals.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false))
Random.seed!(1000); rng = StableRNG(123)

Expand Down
6 changes: 3 additions & 3 deletions test/mcmc/hmc.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module HMCTests

using ..Models: gdemo_default
#using ..Models: gdemo
using ..NumericalTests: check_gdemo, check_numerical
using Distributions: Bernoulli, Beta, Categorical, Dirichlet, Normal, Wishart, sample
import DynamicPPL
using DynamicPPL: Sampler
Expand All @@ -13,9 +16,6 @@ using StatsFuns: logistic
using Test: @test, @test_logs, @testset
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

@testset "Testing hmc.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false))
# Set a seed
rng = StableRNG(123)
Expand Down
4 changes: 2 additions & 2 deletions test/mcmc/mh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ using Test: @test, @testset
using Turing
using Turing.Inference: Inference

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")
using ..Models: gdemo_default, MoGtest_default
using ..NumericalTests: check_MoGtest_default, check_gdemo, check_numerical

GKernel(var) = (x) -> Normal(x, sqrt.(var))

Expand Down
4 changes: 2 additions & 2 deletions test/mcmc/particle_mcmc.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module ParticleMCMCTests

using ..Models: gdemo_default
#using ..Models: MoGtest, MoGtest_default
using AdvancedPS: ResampleWithESSThreshold, resample_systematic, resample_multinomial
using Distributions: Bernoulli, Beta, Gamma, Normal, sample
using DynamicPPL: getspace
import Random
using Test: @test, @test_throws, @testset
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")

@testset "SMC" begin
@testset "constructor" begin
s = SMC()
Expand Down
5 changes: 2 additions & 3 deletions test/mcmc/sghmc.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module SGHMCTests

using ..Models: gdemo_default
using ..NumericalTests: check_gdemo
using Distributions: sample
import ForwardDiff
using LinearAlgebra: dot
Expand All @@ -8,9 +10,6 @@ using StableRNGs: StableRNG
using Test: @test, @testset
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

@testset "Testing sghmc.jl with $adbackend" for adbackend in (AutoForwardDiff(; chunksize=0), AutoReverseDiff(false))
@testset "sghmc constructor" begin
alg = SGHMC(; learning_rate=0.01, momentum_decay=0.1, adtype=adbackend)
Expand Down
3 changes: 1 addition & 2 deletions test/mcmc/utilities.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module MCMCUtilitiesTests

using ..Models: gdemo_default
using Distributions: Normal, sample, truncated
using LinearAlgebra: I, vec
import Random
using Random: MersenneTwister
using Test: @test, @testset
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")

@testset "predict" begin
Random.seed!(100)

Expand Down
3 changes: 1 addition & 2 deletions test/optimisation/OptimInterface.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module OptimInterfaceTests

using ..Models: gdemo_default
using Distributions.FillArrays: Zeros
using LinearAlgebra: I
import Optim
Expand All @@ -10,8 +11,6 @@ using StatsBase: coef, coefnames, coeftable, informationmatrix, stderror, vcov
using Test: @test, @testset
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")

# Used for testing how well it works with nested contexts.
struct OverrideContext{C,T1,T2} <: DynamicPPL.AbstractContext
context::C
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ using Test
using TimerOutputs: TimerOutputs, @timeit
import Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

Turing.setprogress!(false)

included_paths, excluded_paths = parse_args(ARGS)
Expand Down
3 changes: 1 addition & 2 deletions test/stdlib/distributions.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module DistributionsTests

using ..NumericalTests: check_dist_numerical
using Distributions
using LinearAlgebra: I
import Random
Expand All @@ -8,8 +9,6 @@ using StatsFuns: logistic
using Test: @testset, @test
using Turing

include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

@testset "distributions.jl" begin
rng = StableRNG(12345)
@testset "distributions functions" begin
Expand Down
11 changes: 9 additions & 2 deletions test/test_utils/models.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module Models

export MoGtest, MoGtest_default, MoGtest_default_z_vector, MoGtest_z_vector, gdemo, gdemo_d,
gdemo_default

using Distributions
using Turing: @model

# The old-gdemo model.
@model function gdemo(x, y)
s ~ InverseGamma(2, 3)
Expand Down Expand Up @@ -83,5 +91,4 @@ end

MoGtest_default_z_vector = MoGtest_z_vector([1.0 1.0 4.0 4.0])

# Declare empty model to make the Sampler constructor work.
@model empty_model() = x = 1
end
10 changes: 10 additions & 0 deletions test/test_utils/numerical_tests.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module NumericalTests

using Distributions
using Test: @test, @testset

export check_MoGtest_default, check_MoGtest_default_z_vector, check_dist_numerical,
check_gdemo, check_numerical

function check_dist_numerical(dist, chn; mean_tol = 0.1, var_atol = 1.0, var_tol = 0.5)
@testset "numerical" begin
# Extract values.
Expand Down Expand Up @@ -71,3 +79,5 @@ function check_MoGtest_default_z_vector(chain; atol=0.2, rtol=0.0)
[1.0, 1.0, 2.0, 2.0, 1.0, 4.0],
atol=atol, rtol=rtol)
end

end
5 changes: 2 additions & 3 deletions test/variational/advi.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module AdvancedVITests

using ..Models: gdemo_default
using ..NumericalTests: check_gdemo
import AdvancedVI
using AdvancedVI: TruncatedADAGrad, DecayedADAGrad
using Distributions: Dirichlet, Normal
Expand All @@ -10,9 +12,6 @@ using Test: @test, @testset
using Turing
using Turing.Essential: TuringDiagMvNormal

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

@testset "advi.jl" begin
@testset "advi constructor" begin
Random.seed!(0)
Expand Down
3 changes: 0 additions & 3 deletions test/variational/optimisers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import ReverseDiff
using Test: @test, @testset
using Turing

include(pkgdir(Turing) * "/test/test_utils/models.jl")
include(pkgdir(Turing) * "/test/test_utils/numerical_tests.jl")

function test_opt(ADPack, opt)
θ = randn(10, 10)
θ_fit = randn(10, 10)
Expand Down

0 comments on commit 2d7b101

Please sign in to comment.