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
52 changes: 29 additions & 23 deletions tests/cpp/generate_expected_results.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#!/usr/bin/env python3
"""Generate HDF5 file with expected results for the C++ tests."""
"""
Generate HDF5 file with expected results for the C++ tests.

Delete `tests/cpp/expected_results_py.h5` and re-run this script to
regenerate the expected results.
Check the diff carefully before committing!
"""

import subprocess
from pathlib import Path

import amici
import h5py
from amici import (
readModelDataFromHDF5,
readSimulationExpData,
readSolverSettingsFromHDF5,
runAmiciSimulation,
writeReturnData,
from amici.sim.sundials import (
read_exp_data_from_hdf5,
read_model_data_from_hdf5,
read_solver_settings_from_hdf5,
run_simulation,
write_return_data_to_hdf5,
)
from amici.testing.models import (
import_model_calvetti,
Expand All @@ -28,7 +34,7 @@
outfile = Path(__file__).parent / "expected_results_py.h5"


def handle_model(id_: str, model: amici.Model):
def handle_model(id_: str, model: amici.sim.sundials.Model):
# write model settings to h5
script = repo_root / "tests" / "generateTestConfig" / f"example_{id_}.py"
subprocess.run([script, str(outfile)])
Expand All @@ -42,36 +48,36 @@ def handle_model(id_: str, model: amici.Model):
for case in cases:
# create a new model instance for each case, to ensure no interference
model = model.module.get_model()
readModelDataFromHDF5(
str(outfile), model.get(), f"/model_{id_}/{case}/options"
)
solver = model.getSolver()
readSolverSettingsFromHDF5(
str(outfile), solver, f"/model_{id_}/{case}/options"
)
options_path = f"/model_{id_}/{case}/options"
data_path = f"/model_{id_}/{case}/data"
result_path = f"/model_{id_}/{case}/results"

read_model_data_from_hdf5(str(outfile), model.get(), options_path)
solver = model.create_solver()
read_solver_settings_from_hdf5(str(outfile), solver, options_path)
# read ExpData if data/ exists
try:
edata = readSimulationExpData(
str(outfile), f"/model_{id_}/{case}/data", model.get()
edata = read_exp_data_from_hdf5(
str(outfile), data_path, model.get()
)
except RuntimeError:
edata = None

rdata = runAmiciSimulation(model, solver, edata)
writeReturnData(
rdata._swigptr.get(), str(outfile), f"/model_{id_}/{case}/results"
rdata = run_simulation(model, solver, edata)
write_return_data_to_hdf5(
rdata._swigptr.get(), str(outfile), result_path
)


def main():
"""Generate expected results for the C++ tests."""
handle_model("calvetti", import_model_calvetti())
handle_model("dirac", import_model_dirac())
handle_model("events", import_model_events())
handle_model("neuron", import_model_neuron())
handle_model("calvetti", import_model_calvetti())
handle_model("robertson", import_model_robertson())
handle_model("jakstat_adjoint", import_model_jakstat())
handle_model("nested_events", import_model_nested_events())
handle_model("neuron", import_model_neuron())
handle_model("robertson", import_model_robertson())
handle_model("steadystate", import_model_steadystate())


Expand Down
7 changes: 3 additions & 4 deletions tests/generateTestConfig/example_jakstat_adjoint.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3

import os
import sys
from pathlib import Path

import numpy as np
import pandas as pd
Expand All @@ -16,10 +16,9 @@ def __init__(self):
self.numP = 17
self.numK = 2

curPath = os.path.dirname(os.path.realpath(__file__))
dataPath = (
curPath
+ "/../../matlab/examples/example_jakstat_adjoint/pnas_data_original.xls"
Path(__file__).parent
/ "example_jakstat_adjoint_pnas_data_original.xls"
)
xls = pd.ExcelFile(dataPath).parse()
self.modelOptions["ts"] = xls.time
Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/generateTestConfig/example_steadystate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys

import numpy as np
from amici import (
from amici.sim.sundials import (
SensitivityOrder,
SteadyStateComputationMode,
SteadyStateSensitivityMode,
Expand Down
Loading