From 9338b1b1f231d604ea012cfac8c6a356580d7786 Mon Sep 17 00:00:00 2001 From: Valentin Pratz Date: Tue, 22 Apr 2025 14:42:00 +0000 Subject: [PATCH 1/2] Adjust notebooks and test code to allow for pytest testing --- examples/Linear_Regression_Starter.ipynb | 7 +++++-- ...otka_Volterra_Point_Estimation_and_Expert_Stats.ipynb | 4 +++- tests/test_examples/test_examples.py | 3 ++- tests/utils/jupyter.py | 9 +++++++-- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/examples/Linear_Regression_Starter.ipynb b/examples/Linear_Regression_Starter.ipynb index 54125bfd7..f61bf626b 100644 --- a/examples/Linear_Regression_Starter.ipynb +++ b/examples/Linear_Regression_Starter.ipynb @@ -52,6 +52,7 @@ ], "source": [ "import numpy as np\n", + "from pathlib import Path\n", "\n", "import keras\n", "import bayesflow as bf" @@ -987,7 +988,9 @@ ], "source": [ "# Recommended - full serialization (checkpoints folder must exist)\n", - "workflow.approximator.save(filepath=\"checkpoints/regression.keras\")\n", + "filepath = Path(\"checkpoints\") / \"regression.keras\"\n", + "filepath.parent.mkdir(exist_ok=True)\n", + "workflow.approximator.save(filepath=filepath)\n", "\n", "# Not recommended due to adapter mismatches - weights only\n", "# approximator.save_weights(filepath=\"checkpoints/regression.h5\")" @@ -1016,7 +1019,7 @@ ], "source": [ "# Load approximator\n", - "approximator = keras.saving.load_model(\"checkpoints/regression.keras\")" + "approximator = keras.saving.load_model(filepath)" ] }, { diff --git a/examples/Lotka_Volterra_Point_Estimation_and_Expert_Stats.ipynb b/examples/Lotka_Volterra_Point_Estimation_and_Expert_Stats.ipynb index 3dc7c1b25..d52355fa6 100644 --- a/examples/Lotka_Volterra_Point_Estimation_and_Expert_Stats.ipynb +++ b/examples/Lotka_Volterra_Point_Estimation_and_Expert_Stats.ipynb @@ -37,6 +37,7 @@ "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", + "from pathlib import Path\n", "import seaborn as sns\n", "\n", "import scipy\n", @@ -748,7 +749,8 @@ "metadata": {}, "outputs": [], "source": [ - "checkpoint_path = \"checkpoints/model.keras\"\n", + "checkpoint_path = Path(\"checkpoints\") / \"model.keras\"\n", + "checkpoint_path.parent.mkdir(exist_ok=True)\n", "keras.saving.save_model(point_inference_workflow.approximator, checkpoint_path)" ] }, diff --git a/tests/test_examples/test_examples.py b/tests/test_examples/test_examples.py index 42cf7cf53..0f2527fbb 100644 --- a/tests/test_examples/test_examples.py +++ b/tests/test_examples/test_examples.py @@ -8,6 +8,7 @@ def test_bayesian_experimental_design(examples_path): run_notebook(examples_path / "Bayesian_Experimental_Design.ipynb") +@pytest.mark.skip(reason="Requires setting up Stan.") @pytest.mark.slow def test_from_abc_to_bayesflow(examples_path): run_notebook(examples_path / "From_ABC_to_BayesFlow.ipynb") @@ -30,7 +31,7 @@ def test_one_sample_ttest(examples_path): @pytest.mark.slow def test_sir_posterior_estimation(examples_path): - run_notebook(examples_path / "SIR_Posterior_estimation.ipynb") + run_notebook(examples_path / "SIR_Posterior_Estimation.ipynb") @pytest.mark.slow diff --git a/tests/utils/jupyter.py b/tests/utils/jupyter.py index fad68444b..8480c946c 100644 --- a/tests/utils/jupyter.py +++ b/tests/utils/jupyter.py @@ -1,11 +1,16 @@ import nbformat from nbconvert.preprocessors import ExecutePreprocessor +from pathlib import Path + def run_notebook(path): with open(str(path)) as f: nb = nbformat.read(f, nbformat.NO_CONVERT) - kernel = ExecutePreprocessor(timeout=600, kernel_name="python3") + kernel = ExecutePreprocessor( + timeout=600, kernel_name="python3", resources={"metadata": {"path": Path(path).parent}} + ) - return kernel.preprocess(nb) + result = kernel.preprocess(nb) + return result From cc287af2d4b0bdf0a52270ca74bb0c92d247f3c5 Mon Sep 17 00:00:00 2001 From: Valentin Pratz Date: Tue, 22 Apr 2025 15:12:19 +0000 Subject: [PATCH 2/2] example tests: remove checkpoints after test, minor adjustments --- tests/test_examples/test_examples.py | 2 +- tests/utils/jupyter.py | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/tests/test_examples/test_examples.py b/tests/test_examples/test_examples.py index 0f2527fbb..245052636 100644 --- a/tests/test_examples/test_examples.py +++ b/tests/test_examples/test_examples.py @@ -3,12 +3,12 @@ from tests.utils import run_notebook +@pytest.mark.skip(reason="requires setting up Stan") @pytest.mark.slow def test_bayesian_experimental_design(examples_path): run_notebook(examples_path / "Bayesian_Experimental_Design.ipynb") -@pytest.mark.skip(reason="Requires setting up Stan.") @pytest.mark.slow def test_from_abc_to_bayesflow(examples_path): run_notebook(examples_path / "From_ABC_to_BayesFlow.ipynb") diff --git a/tests/utils/jupyter.py b/tests/utils/jupyter.py index 8480c946c..43c5a5a38 100644 --- a/tests/utils/jupyter.py +++ b/tests/utils/jupyter.py @@ -2,15 +2,26 @@ from nbconvert.preprocessors import ExecutePreprocessor from pathlib import Path +import shutil def run_notebook(path): + path = Path(path) + checkpoint_path = path.parent / "checkpoints" + # only clean up if the directory did not exist before the test + cleanup_checkpoints = not checkpoint_path.exists() with open(str(path)) as f: nb = nbformat.read(f, nbformat.NO_CONVERT) - kernel = ExecutePreprocessor( - timeout=600, kernel_name="python3", resources={"metadata": {"path": Path(path).parent}} - ) + kernel = ExecutePreprocessor(timeout=600, kernel_name="python3", resources={"metadata": {"path": path.parent}}) + + try: + result = kernel.preprocess(nb) + except Exception as e: + raise e + finally: + if cleanup_checkpoints and checkpoint_path.exists(): + # clean up if the directory was created by the test + shutil.rmtree(checkpoint_path) - result = kernel.preprocess(nb) return result