Skip to content

Commit 9338b1b

Browse files
committed
Adjust notebooks and test code to allow for pytest testing
1 parent 8378ad3 commit 9338b1b

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

examples/Linear_Regression_Starter.ipynb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
],
5353
"source": [
5454
"import numpy as np\n",
55+
"from pathlib import Path\n",
5556
"\n",
5657
"import keras\n",
5758
"import bayesflow as bf"
@@ -987,7 +988,9 @@
987988
],
988989
"source": [
989990
"# Recommended - full serialization (checkpoints folder must exist)\n",
990-
"workflow.approximator.save(filepath=\"checkpoints/regression.keras\")\n",
991+
"filepath = Path(\"checkpoints\") / \"regression.keras\"\n",
992+
"filepath.parent.mkdir(exist_ok=True)\n",
993+
"workflow.approximator.save(filepath=filepath)\n",
991994
"\n",
992995
"# Not recommended due to adapter mismatches - weights only\n",
993996
"# approximator.save_weights(filepath=\"checkpoints/regression.h5\")"
@@ -1016,7 +1019,7 @@
10161019
],
10171020
"source": [
10181021
"# Load approximator\n",
1019-
"approximator = keras.saving.load_model(\"checkpoints/regression.keras\")"
1022+
"approximator = keras.saving.load_model(filepath)"
10201023
]
10211024
},
10221025
{

examples/Lotka_Volterra_Point_Estimation_and_Expert_Stats.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"source": [
3838
"import matplotlib.pyplot as plt\n",
3939
"import numpy as np\n",
40+
"from pathlib import Path\n",
4041
"import seaborn as sns\n",
4142
"\n",
4243
"import scipy\n",
@@ -748,7 +749,8 @@
748749
"metadata": {},
749750
"outputs": [],
750751
"source": [
751-
"checkpoint_path = \"checkpoints/model.keras\"\n",
752+
"checkpoint_path = Path(\"checkpoints\") / \"model.keras\"\n",
753+
"checkpoint_path.parent.mkdir(exist_ok=True)\n",
752754
"keras.saving.save_model(point_inference_workflow.approximator, checkpoint_path)"
753755
]
754756
},

tests/test_examples/test_examples.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def test_bayesian_experimental_design(examples_path):
88
run_notebook(examples_path / "Bayesian_Experimental_Design.ipynb")
99

1010

11+
@pytest.mark.skip(reason="Requires setting up Stan.")
1112
@pytest.mark.slow
1213
def test_from_abc_to_bayesflow(examples_path):
1314
run_notebook(examples_path / "From_ABC_to_BayesFlow.ipynb")
@@ -30,7 +31,7 @@ def test_one_sample_ttest(examples_path):
3031

3132
@pytest.mark.slow
3233
def test_sir_posterior_estimation(examples_path):
33-
run_notebook(examples_path / "SIR_Posterior_estimation.ipynb")
34+
run_notebook(examples_path / "SIR_Posterior_Estimation.ipynb")
3435

3536

3637
@pytest.mark.slow

tests/utils/jupyter.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import nbformat
22
from nbconvert.preprocessors import ExecutePreprocessor
33

4+
from pathlib import Path
5+
46

57
def run_notebook(path):
68
with open(str(path)) as f:
79
nb = nbformat.read(f, nbformat.NO_CONVERT)
810

9-
kernel = ExecutePreprocessor(timeout=600, kernel_name="python3")
11+
kernel = ExecutePreprocessor(
12+
timeout=600, kernel_name="python3", resources={"metadata": {"path": Path(path).parent}}
13+
)
1014

11-
return kernel.preprocess(nb)
15+
result = kernel.preprocess(nb)
16+
return result

0 commit comments

Comments
 (0)