Skip to content
Open
Changes from 2 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
f74511a
reuse npse_trained_model fixure for test_npse_map
vagechirkov Mar 17, 2025
f59a023
fix: sde_type and prior_type are now the part of the trained model fi…
vagechirkov Mar 17, 2025
b0c506d
formatting: shorten the parametrize mark
vagechirkov Mar 18, 2025
5fdb087
enh: mark default as NOT slow
vagechirkov Mar 18, 2025
607fce0
enh: clean up tests
vagechirkov Mar 18, 2025
c363126
chore: add syrupy dependency for snapshot testing, add snapshot test …
schroedk Mar 19, 2025
893810c
WIP: pytest_cases spike
schroedk Mar 19, 2025
8c3cc25
Update snapshots
schroedk Mar 20, 2025
8cc1ed0
refactor npse tests
schroedk Mar 20, 2025
d6341db
Update num_simulations and num_samples for slow version of tests
schroedk Mar 20, 2025
cbd35d1
WIP
schroedk Mar 20, 2025
c4dfceb
Snapshot list of floats instead of tensor object
schroedk Mar 20, 2025
43a8778
Increase numbers in snapshot test
schroedk Mar 20, 2025
422bbe4
Merge branch 'sbi-dev:main' into speed-up-tests-with-fixtures
schroedk Mar 20, 2025
0a9803c
refactor sampling test parameters handling
vagechirkov Mar 20, 2025
ba2fb41
Merge branch 'speed-up-tests-with-fixtures' into handle-combinations
vagechirkov Mar 20, 2025
802aec4
Merge pull request #2 from vagechirkov/handle-combinations
vagechirkov Mar 20, 2025
d4c886f
Restrict snapshot test to gaussian prior
schroedk Mar 20, 2025
f6b517d
clean up x_o
vagechirkov Mar 20, 2025
59d56d2
Add snapshot tests in a separate file
schroedk Mar 20, 2025
f42e776
Skip not working test
schroedk Mar 20, 2025
df39484
WIP: DO NOT MERGE!!!: simplify test run in ci for faster iteration
schroedk Mar 20, 2025
dfa60ad
Transform tensor to list
schroedk Mar 20, 2025
1186823
Add *.npz files to LFS
schroedk Mar 20, 2025
f2e5a8e
replace inference.build_posterior with ScorePosterior
vagechirkov Mar 20, 2025
293f91a
Add pytests-regressions to dev dependencies
schroedk Mar 20, 2025
629196e
Use ndarrays_regression to check the output of the test_npse_snapshot…
schroedk Mar 20, 2025
d6efca6
Update .gitattributes
schroedk Mar 20, 2025
e562c8a
Add pytest-regression snapshot files
schroedk Mar 20, 2025
5c354de
Merge remote-tracking branch 'origin/speed-up-tests-with-fixtures' in…
schroedk Mar 20, 2025
76d23dd
Enable lfs in ci checkout step
schroedk Mar 20, 2025
0e21be0
Update test runtime file
schroedk Mar 20, 2025
3869a2b
Remove the tolerance from the snapshot test
schroedk Mar 20, 2025
f0e23fd
Set tolerance for snapshot test in test_snapshot.py
schroedk Mar 20, 2025
44f7b91
Increase tolerance for snapshot test in test_snapshot.py
schroedk Mar 20, 2025
72b9c64
Reduce number of steps in sampling, reduce number of simulations in s…
schroedk Mar 20, 2025
1efac10
Update test run times
schroedk Mar 20, 2025
f88ffab
Update snapshots
schroedk Mar 20, 2025
a721184
Revert "WIP: DO NOT MERGE!!!: simplify test run in ci for faster iter…
schroedk Mar 20, 2025
d74399f
refactor helper functions
vagechirkov Mar 21, 2025
7350ccf
run snapshot on 1 trial
vagechirkov Mar 21, 2025
d23c96f
timeout tests that are taking more than 60s to run
vagechirkov Mar 21, 2025
2fc2925
files for snapshot tests
vagechirkov Mar 21, 2025
830f553
Remove obsolete snapshot file
schroedk Mar 23, 2025
ad53690
Add configurable data directories for regression tests, mirror test f…
schroedk Mar 23, 2025
a6f761b
Delete obsolete regression files from the repository
schroedk Mar 23, 2025
a3fbef2
Move .test_duration to git lfs
schroedk Mar 23, 2025
3cc04f9
Refactor linearGaussian_npse_test.py:
schroedk Mar 23, 2025
67ae45a
Add regression files for npse snapshots
schroedk Mar 23, 2025
50187ce
Remove obsolete test file
schroedk Mar 23, 2025
fd97749
Exclude cases from regression tests, which show non-determinism
schroedk Mar 23, 2025
ade4b06
Remove syrupy dependency
schroedk Mar 23, 2025
0e9d4e6
Add .gitignore to __regressions__ directory
schroedk Mar 23, 2025
2e46877
Make dataclasses frozen to ensure consistent hash and equality evalua…
schroedk Mar 23, 2025
7276ad4
Refactor npse tests:
schroedk Mar 24, 2025
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
64 changes: 32 additions & 32 deletions tests/linearGaussian_npse_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from itertools import product
from typing import List

import pytest
Expand All @@ -17,8 +18,12 @@

from .test_utils import check_c2st, get_dkl_gaussian_prior

SDE_TYPES = ["vp", "ve", "subvp"]
PRIOR_TYPES = ["gaussian", "uniform", None]


# We always test num_dim and sample_with with defaults and mark the rests as slow.
@pytest.mark.slow
@pytest.mark.parametrize(
"sde_type, num_dim, prior_str, sample_with",
[
Expand Down Expand Up @@ -157,21 +162,10 @@ def simulator(theta):
check_c2st(samples, target_samples, alg="npse_different_dims_and_resume_training")


@pytest.fixture(scope="module", params=["vp", "ve", "subvp"])
def sde_type(request):
"""Module-scoped fixture for SDE type."""
return request.param


@pytest.fixture(scope="module", params=["gaussian", "uniform", None])
def prior_type(request):
"""Module-scoped fixture for prior type."""
return request.param


@pytest.fixture(scope="module")
def npse_trained_model(sde_type, prior_type):
def npse_trained_model(request):
"""Module-scoped fixture that trains a score estimator for NPSE tests."""
sde_type, prior_type = request.param
num_dim = 2
num_simulations = 5000

Expand Down Expand Up @@ -212,10 +206,18 @@ def npse_trained_model(sde_type, prior_type):
if prior_type == "gaussian" or prior_type is None
else None,
"num_dim": num_dim,
"x_o": zeros(num_dim),
"sde_type": sde_type,
"prior_type": prior_type,
}


@pytest.mark.slow
@pytest.mark.parametrize(
"npse_trained_model",
list(product(SDE_TYPES, PRIOR_TYPES)),
indirect=True, # So pytest knows to pass to the fixture
)
@pytest.mark.parametrize(
"iid_method, num_trial",
[
Expand All @@ -226,9 +228,7 @@ def npse_trained_model(sde_type, prior_type):
pytest.param("jac_gauss", 8, id="jac_gauss-8trials"),
],
)
def test_npse_iid_inference(
npse_trained_model, iid_method, num_trial, sde_type, prior_type
):
def test_npse_iid_inference(npse_trained_model, iid_method, num_trial):
"""Test whether NPSE infers well a simple example with available ground truth."""
num_samples = 1000

Expand All @@ -241,6 +241,8 @@ def test_npse_iid_inference(
prior_mean = npse_trained_model["prior_mean"]
prior_cov = npse_trained_model["prior_cov"]
num_dim = npse_trained_model["num_dim"]
sde_type = npse_trained_model["sde_type"]
prior_type = npse_trained_model["prior_type"]

x_o = zeros(num_trial, num_dim)
posterior = inference.build_posterior(score_estimator)
Expand Down Expand Up @@ -272,27 +274,25 @@ def test_npse_iid_inference(


@pytest.mark.slow
def test_npse_map():
num_dim = 2
x_o = zeros(num_dim)
num_simulations = 3000

# likelihood_mean will be likelihood_shift+theta
likelihood_shift = -1.0 * ones(num_dim)
likelihood_cov = 0.3 * eye(num_dim)
@pytest.mark.parametrize(
"npse_trained_model",
[
("vp", "gaussian"),
],
indirect=True,
)
def test_npse_map(npse_trained_model):
x_o = npse_trained_model["x_o"]
inference = npse_trained_model["inference"]
prior_mean = npse_trained_model["prior_mean"]
prior_cov = npse_trained_model["prior_cov"]
likelihood_shift = npse_trained_model["likelihood_shift"]
likelihood_cov = npse_trained_model["likelihood_cov"]

prior_mean = zeros(num_dim)
prior_cov = eye(num_dim)
prior = MultivariateNormal(loc=prior_mean, covariance_matrix=prior_cov)
gt_posterior = true_posterior_linear_gaussian_mvn_prior(
x_o, likelihood_shift, likelihood_cov, prior_mean, prior_cov
)
inference = NPSE(prior, show_progress_bars=True)

theta = prior.sample((num_simulations,))
x = linear_gaussian(theta, likelihood_shift, likelihood_cov)

inference.append_simulations(theta, x).train()
posterior = inference.build_posterior().set_default_x(x_o)

map_ = posterior.map(show_progress_bars=True, num_iter=5)
Expand Down