Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev add re test items #196

Merged
merged 10 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
BenchmarkCI = "20533458-34a3-403d-a444-e18f38190b5b"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Coverage = "a2441757-f6aa-5fb2-8edb-039e3f45d037"
CpuId = "adafc99b-e345-5852-983c-f28acb93d879"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Expand All @@ -54,10 +55,11 @@ Optimisers = "3bd65402-5787-11e9-1adc-39752487f4e2"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
PkgBenchmark = "32113eaa-f34f-5b0d-bd6c-c81e245fc73d"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
ReTestItems = "817f1d60-ba6b-4fd5-9520-3cf149f6a823"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestSetExtensions = "98d24dd4-01ad-11ea-1b02-c9a08f80db04"

[targets]
test = ["Test", "Pkg", "Logging", "InteractiveUtils", "TestSetExtensions", "Coverage", "Dates", "Distributed", "Documenter", "Plots", "BenchmarkCI", "BenchmarkTools", "PkgBenchmark", "Aqua", "StableRNGs", "StatsFuns", "Optimisers"]
test = ["Test", "Pkg", "Logging", "InteractiveUtils", "TestSetExtensions", "Coverage", "CpuId", "Dates", "Distributed", "Documenter", "Plots", "BenchmarkCI", "BenchmarkTools", "PkgBenchmark", "Aqua", "StableRNGs", "StatsFuns", "Optimisers", "ReTestItems"]
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
module RxInferFactorisationConstraintsTest

using Test, Logging
using RxInfer

import ReactiveMP: resolve_factorisation, setanonymous!
import ReactiveMP: activate!

@testset "Factorisation constraints resolution with @constraints" begin
@testitem "Factorisation constraints resolution with @constraints" begin
using Logging
import ReactiveMP: resolve_factorisation, setanonymous!
import ReactiveMP: activate!

# Factorisation constrains resolution function accepts a `fform` symbol as an input for error printing
# We don't care about actual symbol in tests
Expand Down Expand Up @@ -635,5 +630,3 @@ import ReactiveMP: activate!
@test_throws ErrorException ReactiveMP.resolve_factorisation(cs, getvariables(model), fform, (x, y))
end
end

end
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
module RxInferPointMassFormConstraintTest
@testitem "PointMassFormConstraint" begin
using LinearAlgebra
using Random, StableRNGs, DomainSets, Distributions
import RxInfer: PointMassFormConstraint, is_point_mass_form_constraint, call_boundaries, call_starting_point, call_optimizer

using Test
using RxInfer, LinearAlgebra
using Random, StableRNGs, DomainSets, Distributions

import ReactiveMP: constrain_form
import RxInfer: PointMassFormConstraint, is_point_mass_form_constraint, call_boundaries, call_starting_point, call_optimizer

struct MyDistributionWithMode <: ContinuousUnivariateDistribution
mode::Float64
end
struct MyDistributionWithMode <: ContinuousUnivariateDistribution
mode::Float64
end

# We are testing specifically that the point mass optimizer does not call `logpdf` and
# chooses a fast path with `mode` for `<: Distribution` objects
Distributions.logpdf(::MyDistributionWithMode, _) = error("This should not be called")
Distributions.mode(d::MyDistributionWithMode) = d.mode
Distributions.support(::MyDistributionWithMode) = RealInterval(-Inf, Inf)
# We are testing specifically that the point mass optimizer does not call `logpdf` and
# chooses a fast path with `mode` for `<: Distribution` objects
Distributions.logpdf(::MyDistributionWithMode, _) = error("This should not be called")
Distributions.mode(d::MyDistributionWithMode) = d.mode
Distributions.support(::MyDistributionWithMode) = RealInterval(-Inf, Inf)

const arbitrary_dist_1 = ContinuousUnivariateLogPdf(RealLine(), (x) -> logpdf(NormalMeanVariance(0, 1), x))
const arbitrary_dist_2 = ContinuousUnivariateLogPdf(HalfLine(), (x) -> logpdf(Gamma(1, 1), x))
const arbitrary_dist_3 = ContinuousUnivariateLogPdf(RealLine(), (x) -> logpdf(NormalMeanVariance(-10, 10), x))
const arbitrary_dist_4 = ContinuousUnivariateLogPdf(HalfLine(), (x) -> logpdf(GammaShapeRate(100, 10), x))
const arbitrary_dist_5 = ContinuousUnivariateLogPdf(HalfLine(), (x) -> logpdf(GammaShapeRate(100, 100), x))
const arbitrary_dist_1 = ContinuousUnivariateLogPdf(RealLine(), (x) -> logpdf(NormalMeanVariance(0, 1), x))
const arbitrary_dist_2 = ContinuousUnivariateLogPdf(HalfLine(), (x) -> logpdf(Gamma(1, 1), x))
const arbitrary_dist_3 = ContinuousUnivariateLogPdf(RealLine(), (x) -> logpdf(NormalMeanVariance(-10, 10), x))
const arbitrary_dist_4 = ContinuousUnivariateLogPdf(HalfLine(), (x) -> logpdf(GammaShapeRate(100, 10), x))
const arbitrary_dist_5 = ContinuousUnivariateLogPdf(HalfLine(), (x) -> logpdf(GammaShapeRate(100, 100), x))

@testset "PointMassFormConstraint" begin
@testset "is_point_mass_form_constraint" begin
@test is_point_mass_form_constraint(PointMassFormConstraint())
end
Expand Down Expand Up @@ -112,5 +107,3 @@ const arbitrary_dist_5 = ContinuousUnivariateLogPdf(HalfLine(), (x) -> logpdf(Ga
end
end
end

end
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@

module RxInferSampleListFormConstraintTest

using Test
using RxInfer, LinearAlgebra
using Random, StableRNGs, DomainSets

import RxInfer: SampleListFormConstraint, is_point_mass_form_constraint, constrain_form

@testset "PointMassFormConstraint" begin
@testitem "PointMassFormConstraint" begin
using DomainSets, StableRNGs, DomainSets, Distributions, Random, LinearAlgebra
import RxInfer: SampleListFormConstraint, is_point_mass_form_constraint, constrain_form
@testset "is_point_mass_form_constraint" begin
@test !is_point_mass_form_constraint(SampleListFormConstraint(100))
end
Expand Down Expand Up @@ -58,5 +51,3 @@ import RxInfer: SampleListFormConstraint, is_point_mass_form_constraint, constra
end
end
end

end
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
module RxInferFormConstraintsSpecificationTest

using Test, Logging
using RxInfer

import RxInfer: PointMassFormConstraint, SampleListFormConstraint, FixedMarginalFormConstraint
import ReactiveMP: CompositeFormConstraint
import ReactiveMP: resolve_marginal_form_prod, resolve_messages_form_prod
import ReactiveMP: activate!

@testset "Form constraints specification with @constraints macro" begin
@testitem "Form constraints specification with @constraints macro" begin
using Logging
import RxInfer: PointMassFormConstraint, SampleListFormConstraint, FixedMarginalFormConstraint
import ReactiveMP: CompositeFormConstraint
import ReactiveMP: resolve_marginal_form_prod, resolve_messages_form_prod
import ReactiveMP: activate!
@testset "Use case #1" begin
cs = @constraints begin
q(x)::PointMass
Expand Down Expand Up @@ -410,5 +405,3 @@ import ReactiveMP: activate!
end
end
end

end
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
module ReactiveMPMetaSpecificationHelpers

using Test
using RxInfer
using Distributions
using Logging

@testset "Meta specification with @meta macro" begin
@testitem "Meta specification with @meta macro" begin
import ReactiveMP: resolve_meta, make_node, activate!
using Distributions
using Logging

struct SomeNode end
struct SomeOtherNode end
Expand Down Expand Up @@ -231,5 +226,3 @@ using Logging
@test resolve_meta(meta, SomeNode, (z,)) === nothing
end
end

end
13 changes: 3 additions & 10 deletions test/test_helpers.jl → test/helpers_tests.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
module RxInferHelpersTest

using Test
using RxInfer

@testset "NamedTuple helpers" begin
@testitem "NamedTuple helpers" begin
import RxInfer: fields, nthasfield

@test fields((x = 1, y = 2)) === (:x, :y)
Expand All @@ -17,7 +12,7 @@ using RxInfer
@test nthasfield(:c, typeof((x = 1, y = 2))) === false
end

@testset "Tuple helpers" begin
@testitem "Tuple helpers" begin
import RxInfer: as_tuple

@test as_tuple(1) === (1,)
Expand All @@ -27,7 +22,7 @@ end
@test as_tuple(("string",)) === ("string",)
end

@testset "Val helpers" begin
@testitem "Val helpers" begin
import RxInfer: unval

@test unval(Val(1)) === 1
Expand All @@ -38,5 +33,3 @@ end
@test_throws ErrorException unval(())
@test_throws ErrorException unval(nothing)
end

end
22 changes: 7 additions & 15 deletions test/test_inference.jl → test/inference_test.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
module RxInferInferenceTest

using Test
using RxInfer
using Random

@testset "__inference_check_itertype" begin
@testitem "__inference_check_itertype" begin
import RxInfer: __inference_check_itertype

@test __inference_check_itertype(:something, nothing) === nothing
Expand All @@ -18,7 +12,7 @@ using Random
@test_throws ErrorException __inference_check_itertype(:something, missing)
end

@testset "__inference_check_dicttype" begin
@testitem "__inference_check_dicttype" begin
import RxInfer: __inference_check_dicttype

@test __inference_check_dicttype(:something, nothing) === nothing
Expand All @@ -33,7 +27,7 @@ end
@test_throws ErrorException __inference_check_dicttype(:something, (missing))
end

@testset "`@autoupdates` macro" begin
@testitem "`@autoupdates` macro" begin
function somefunction(something)
return nothing
end
Expand Down Expand Up @@ -143,7 +137,7 @@ end
end
end

@testset "Static inference with `inference`" begin
@testitem "Static inference with `inference`" begin

# A simple model for testing that resembles a simple kalman filter with
# random walk state transition and unknown observational noise
Expand Down Expand Up @@ -311,7 +305,7 @@ end
end
end

@testset "Test warn argument in `inference()`" begin
@testitem "Test warn argument in `inference()`" begin
@testset "Test warning for addons" begin

#Add a new case for testing warning of addons
Expand Down Expand Up @@ -406,7 +400,7 @@ end
end
end

@testset "Reactive inference with `rxinference` for test model #1" begin
@testitem "Reactive inference with `rxinference` for test model #1" begin

# A simple model for testing that resembles a simple kalman filter with
# random walk state transition and unknown observational noise
Expand Down Expand Up @@ -815,7 +809,7 @@ end
end
end

@testset "Predictions functionality" begin
@testitem "Predictions functionality" begin

# test #1 (array with missing + predictvars)
data = (y = [1.0, -500.0, missing, 100.0],)
Expand Down Expand Up @@ -1032,5 +1026,3 @@ end

@test all(result.predictions[:y] .== Bernoulli(mean(Beta(1.0, 1.0))))
end

end
11 changes: 2 additions & 9 deletions test/test_model.jl → test/model_tests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
module RxInferModelTest

using Test
using RxInfer
using Random

@testset "@model macro tests" begin
@testitem "@model macro tests" begin
@testset "Tuple based variables usage #1" begin
@model function mixture_model()
mean1 ~ Normal(mean = 10, variance = 10000)
Expand Down Expand Up @@ -97,6 +91,7 @@ using Random
end

@testset "Priors in arguments" begin
import Random: MersenneTwister
@model function coin_model_priors1(n, prior)
y = datavar(Float64, n)
θ ~ prior
Expand Down Expand Up @@ -187,5 +182,3 @@ using Random
@test_throws ErrorException ReactiveMP.make_node(FactorGraphModel(), FactorNodeCreationOptions(), DummyDistributionTestModelError3, randomvar(:θ))
end
end

end
21 changes: 21 additions & 0 deletions test/models/aliases/aliases_binary_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@testitem "aliases for binary operations" begin
@model function binary_aliases()
x1 ~ Bernoulli(0.5)
x2 ~ Bernoulli(0.5)
x3 ~ Bernoulli(0.5)
x4 ~ Bernoulli(0.5)

x ~ x1 -> x2 && x3 || ¬x4

y = datavar(Float64)
x ~ Bernoulli(y)
end

function binary_aliases_inference()
return inference(model = binary_aliases(), data = (y = 0.5,), free_energy = true)
end
results = binary_aliases_inference()
# Here we simply test that it ran and gave some output
@test mean(results.posteriors[:x1]) ≈ 0.5
@test first(results.free_energy) ≈ 0.6931471805599454
end
43 changes: 43 additions & 0 deletions test/models/aliases/aliases_normal_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@testitem "aliases for `Normal` family of distributions" begin
@model function normal_aliases()
x1 ~ MvNormal(μ = zeros(2), Σ⁻¹ = diageye(2))
x2 ~ MvNormal(μ = zeros(2), Λ = diageye(2))
x3 ~ MvNormal(mean = zeros(2), W = diageye(2))
x4 ~ MvNormal(μ = zeros(2), prec = diageye(2))
x5 ~ MvNormal(m = zeros(2), precision = diageye(2))

y1 ~ MvNormal(mean = zeros(2), Σ = diageye(2))
y2 ~ MvNormal(m = zeros(2), Λ⁻¹ = diageye(2))
y3 ~ MvNormal(μ = zeros(2), V = diageye(2))
y4 ~ MvNormal(mean = zeros(2), cov = diageye(2))
y5 ~ MvNormal(mean = zeros(2), covariance = diageye(2))

x ~ x1 + x2 + x3 + x4 + x5
y ~ y1 + y2 + y3 + y4 + y5

r1 ~ Normal(μ = dot(x + y, ones(2)), τ = 1.0)
r2 ~ Normal(m = r1, γ = 1.0)
r3 ~ Normal(mean = r2, σ⁻² = 1.0)
r4 ~ Normal(mean = r3, w = 1.0)
r5 ~ Normal(mean = r4, p = 1.0)
r6 ~ Normal(mean = r5, prec = 1.0)
r7 ~ Normal(mean = r6, precision = 1.0)

s1 ~ Normal(μ = r7, σ² = 1.0)
s2 ~ Normal(m = s1, τ⁻¹ = 1.0)
s3 ~ Normal(mean = s2, v = 1.0)
s4 ~ Normal(mean = s3, var = 1.0)
s5 ~ Normal(mean = s4, variance = 1.0)

d = datavar(Float64)
d ~ Normal(μ = s5, variance = 1.0)
end

function normal_aliases_inference()
return inference(model = normal_aliases(), data = (d = 1.0,), returnvars = (x1 = KeepLast(),), free_energy = true)
end
result = normal_aliases_inference()
# Here we simply test that it ran and gave some output
@test first(mean(result.posteriors[:x1])) ≈ 0.04182509505703423
@test first(result.free_energy) ≈ 2.319611135721246
end
29 changes: 0 additions & 29 deletions test/models/aliases/test_aliases_binary.jl

This file was deleted.

Loading