From d05a8998ea82b06fd5cb827fa1ea9cfdf67977f9 Mon Sep 17 00:00:00 2001 From: LarsKue Date: Mon, 7 Apr 2025 20:10:31 -0400 Subject: [PATCH 1/9] add jupyter notebook tests --- tests/test_examples/__init__.py | 0 tests/test_examples/conftest.py | 8 ++++++++ tests/utils/__init__.py | 5 +++-- tests/utils/jupyter.py | 11 +++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/test_examples/__init__.py create mode 100644 tests/test_examples/conftest.py create mode 100644 tests/utils/jupyter.py diff --git a/tests/test_examples/__init__.py b/tests/test_examples/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_examples/conftest.py b/tests/test_examples/conftest.py new file mode 100644 index 000000000..a1539d144 --- /dev/null +++ b/tests/test_examples/conftest.py @@ -0,0 +1,8 @@ + +import pytest + + +@pytest.fixture(scope="session") +def examples_path(): + from pathlib import Path + return Path(__file__).parents[2] / "examples" diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py index 45f41b5a8..f8d063a83 100644 --- a/tests/utils/__init__.py +++ b/tests/utils/__init__.py @@ -1,5 +1,6 @@ from .assertions import * from .callbacks import * -from .ops import * -from .ecdf import * from .check_combinations import * +from .ecdf import * +from .jupyter import * +from .ops import * diff --git a/tests/utils/jupyter.py b/tests/utils/jupyter.py new file mode 100644 index 000000000..8a825cd8e --- /dev/null +++ b/tests/utils/jupyter.py @@ -0,0 +1,11 @@ + +import nbformat +from nbconvert.preprocessors import ExecutePreprocessor + +def run_notebook(path): + with open(str(path)) as f: + nb = nbformat.read(f, nbformat.NO_CONVERT) + + kernel = ExecutePreprocessor(timeout=600, kernel_name="python3") + + return kernel.preprocess(nb) From a0a22ee28bc7cf8c2026168c6fe91482f8a8c5be Mon Sep 17 00:00:00 2001 From: LarsKue Date: Mon, 7 Apr 2025 20:10:39 -0400 Subject: [PATCH 2/9] add slow marker to pytest ini --- pytest.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/pytest.ini b/pytest.ini index f90bc63a0..b2607d46e 100644 --- a/pytest.ini +++ b/pytest.ini @@ -6,6 +6,7 @@ markers = numpy: mark test as requiring NumPy tensorflow: mark test as requiring TensorFlow torch: mark test as requiring PyTorch + slow: mark test as especially slow, such as running a whole fitting workflow testpaths = tests From 1681c27fda8aae1e61c3051c86df11cd4ddb1b83 Mon Sep 17 00:00:00 2001 From: LarsKue Date: Mon, 7 Apr 2025 20:11:05 -0400 Subject: [PATCH 3/9] add nbconvert to test dependencies --- pyproject.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 5f83611f2..7e37dfc74 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,7 @@ all = [ # dev "jupyter", "jupyterlab", + "nbconvert", "pre-commit", "ruff", "tox", @@ -71,6 +72,7 @@ docs = [ "sphinxcontrib-bibtex", ] test = [ + "nbconvert", "pytest", "pytest-cov", "pytest-rerunfailures", From 707579ba56ef7e817cf143f9f7bfb817ec9f342e Mon Sep 17 00:00:00 2001 From: LarsKue Date: Mon, 7 Apr 2025 20:11:12 -0400 Subject: [PATCH 4/9] add actual notebook test --- tests/test_examples/test_examples.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/test_examples/test_examples.py diff --git a/tests/test_examples/test_examples.py b/tests/test_examples/test_examples.py new file mode 100644 index 000000000..9b8029d56 --- /dev/null +++ b/tests/test_examples/test_examples.py @@ -0,0 +1,9 @@ + +import pytest + +from tests.utils import run_notebook + + +@pytest.mark.slow +def test_two_moons_starter(examples_path): + run_notebook(examples_path / "Two_Moons_Starter.ipynb") From e69c2004de2a11b03140d8a191f67ee0fc0889e8 Mon Sep 17 00:00:00 2001 From: LarsKue Date: Mon, 7 Apr 2025 20:11:27 -0400 Subject: [PATCH 5/9] modify workflow to only run slow tests on manual trigger --- .github/workflows/tests.yaml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 514b1127d..c3cf53e3b 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -99,7 +99,13 @@ jobs: - name: Run Tests run: | - pytest -x + pytest -x -m not slow + + - name: Run Slow Tests + # run only on manual trigger + if: github.event_name == 'workflow_dispatch' + run: | + pytest -x -m slow - name: Create Coverage Report run: | From 8a15783320e6c83b90ee14c9b22318c495f1c49a Mon Sep 17 00:00:00 2001 From: LarsKue Date: Tue, 8 Apr 2025 11:03:25 -0400 Subject: [PATCH 6/9] rename lotka volterra for consistent capitalization --- README.md | 2 +- ...b => Lotka_Volterra_Point_Estimation_and_Expert_Stats.ipynb} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename examples/{Lotka_Volterra_point_estimation_and_expert_stats.ipynb => Lotka_Volterra_Point_Estimation_and_Expert_Stats.ipynb} (100%) diff --git a/README.md b/README.md index da556839d..ccff3f10f 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ For an in-depth exposition, check out our walkthrough notebooks below. 1. [Linear regression starter example](examples/Linear_Regression_Starter.ipynb) 2. [From ABC to BayesFlow](examples/From_ABC_to_BayesFlow.ipynb) 3. [Two moons starter example](examples/Two_Moons_Starter.ipynb) -4. [Rapid iteration with point estimators](examples/Lotka_Volterra_point_estimation_and_expert_stats.ipynb) +4. [Rapid iteration with point estimators](examples/Lotka_Volterra_Point_Estimation_and_Expert_Stats.ipynb) 5. [SIR model with custom summary network](examples/SIR_Posterior_Estimation.ipynb) 6. [Bayesian experimental design](examples/Bayesian_Experimental_Design.ipynb) 7. [Simple model comparison example](examples/One_Sample_TTest.ipynb) diff --git a/examples/Lotka_Volterra_point_estimation_and_expert_stats.ipynb b/examples/Lotka_Volterra_Point_Estimation_and_Expert_Stats.ipynb similarity index 100% rename from examples/Lotka_Volterra_point_estimation_and_expert_stats.ipynb rename to examples/Lotka_Volterra_Point_Estimation_and_Expert_Stats.ipynb From 59ba82f614cee1ce808b29ce0f043bae116d63c5 Mon Sep 17 00:00:00 2001 From: LarsKue Date: Tue, 8 Apr 2025 11:03:32 -0400 Subject: [PATCH 7/9] add tests for all notebooks --- tests/test_examples/test_examples.py | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_examples/test_examples.py b/tests/test_examples/test_examples.py index 9b8029d56..b7895ed23 100644 --- a/tests/test_examples/test_examples.py +++ b/tests/test_examples/test_examples.py @@ -4,6 +4,36 @@ from tests.utils import run_notebook +@pytest.mark.slow +def test_bayesian_experimental_design(examples_path): + run_notebook(examples_path / "Bayesian_Experimental_Design.ipynb") + + +@pytest.mark.slow +def test_from_abc_to_bayesflow(examples_path): + run_notebook(examples_path / "From_ABC_to_BayesFlow.ipynb") + + +@pytest.mark.slow +def test_linear_regression_starter(examples_path): + run_notebook(examples_path / "Linear_Regression_Starter.ipynb") + + +@pytest.mark.slow +def test_lotka_volterra_point_estimation_and_expert_stats(examples_path): + run_notebook(examples_path / "Lotka_Volterra_Point_Estimation_and_Expert_Stats.ipynb") + + +@pytest.mark.slow +def test_one_sample_ttest(examples_path): + run_notebook(examples_path / "One_Sample_TTest.ipynb") + + +@pytest.mark.slow +def test_sir_posterior_estimation(examples_path): + run_notebook(examples_path / "SIR_Posterior_estimation.ipynb") + + @pytest.mark.slow def test_two_moons_starter(examples_path): run_notebook(examples_path / "Two_Moons_Starter.ipynb") From 7303a2353f365b2b43833ba6109fbced75a34154 Mon Sep 17 00:00:00 2001 From: LarsKue Date: Tue, 8 Apr 2025 12:07:46 -0400 Subject: [PATCH 8/9] fix workflow --- .github/workflows/tests.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index c3cf53e3b..0016b0fb4 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -99,13 +99,13 @@ jobs: - name: Run Tests run: | - pytest -x -m not slow + pytest -x -m "not slow" - name: Run Slow Tests # run only on manual trigger if: github.event_name == 'workflow_dispatch' run: | - pytest -x -m slow + pytest -x -m "slow" - name: Create Coverage Report run: | From 20751febcc61a540a290163af950c7339ca4f322 Mon Sep 17 00:00:00 2001 From: LarsKue Date: Tue, 8 Apr 2025 12:12:13 -0400 Subject: [PATCH 9/9] fix flow matching fixture --- tests/test_networks/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_networks/conftest.py b/tests/test_networks/conftest.py index 48cd79ed2..5d646d9ee 100644 --- a/tests/test_networks/conftest.py +++ b/tests/test_networks/conftest.py @@ -22,7 +22,7 @@ def flow_matching(): from bayesflow.networks import FlowMatching return FlowMatching( - subnet_kwargs={"widths": None, "width": 64, "depth": 2}, + subnet_kwargs={"widths": [64, 64]}, integrate_kwargs={"method": "rk45", "steps": 100}, )