Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions examples/Linear_Regression_Starter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
],
"source": [
"import numpy as np\n",
"from pathlib import Path\n",
"\n",
"import keras\n",
"import bayesflow as bf"
Expand Down Expand Up @@ -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\")"
Expand Down Expand Up @@ -1016,7 +1019,7 @@
],
"source": [
"# Load approximator\n",
"approximator = keras.saving.load_model(\"checkpoints/regression.keras\")"
"approximator = keras.saving.load_model(filepath)"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)"
]
},
Expand Down
3 changes: 2 additions & 1 deletion tests/test_examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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")
Expand Down Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions tests/utils/jupyter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import nbformat
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")
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)

return kernel.preprocess(nb)
return result