Skip to content

Commit 4e87d9c

Browse files
authored
Return amici.Model from sbml2amici, pysb2amici (#2971)
The import function `sbml2amici`, `pysb2amici`, and `antimony2amici` now return an instance of the generated model class if called with `compile=True` (default) avoiding the need for a separate call to `amici.import_model_module`. Closes #2970.
1 parent 1e006bb commit 4e87d9c

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ See also our [versioning policy](https://amici.readthedocs.io/en/latest/versioni
7474
This only works on shared file systems, as the solver state is stored in a
7575
temporary HDF5 file.
7676
* `amici.ExpData` is now picklable.
77+
* The import function `sbml2amici`, `pysb2amici`, and `antimony2amici` now
78+
return an instance of the generated model class if called with `compile=True`
79+
(default).
7780

7881
## v0.X Series
7982

python/sdist/amici/pysb_import.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import pysb.pattern
2323
import sympy as sp
2424

25+
import amici
26+
2527
from .de_export import (
2628
Constant,
2729
DEExporter,
@@ -161,7 +163,7 @@ def pysb2amici(
161163
generate_sensitivity_code: bool = True,
162164
model_name: str | None = None,
163165
pysb_model_has_obs_and_noise: bool = False,
164-
):
166+
) -> amici.Model | None:
165167
r"""
166168
Generate AMICI C++ files for the provided model.
167169
@@ -238,6 +240,10 @@ def pysb2amici(
238240
:param pysb_model_has_obs_and_noise:
239241
if set to ``True``, the pysb model is expected to have extra
240242
observables and noise variables added
243+
244+
:return:
245+
If `compile` is `True` and compilation was successful, an instance
246+
of the generated model class, otherwise `None`.
241247
"""
242248
if observation_model is None:
243249
observation_model = []
@@ -275,6 +281,14 @@ def pysb2amici(
275281
if compile:
276282
exporter.compile_model()
277283

284+
from . import import_model_module
285+
286+
return import_model_module(
287+
module_name=model_name, module_path=output_dir
288+
).get_model()
289+
290+
return None
291+
278292

279293
@log_execution_time("creating ODE model", logger)
280294
def ode_model_from_pysb_importer(

python/sdist/amici/sbml_import.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
from sympy.logic.boolalg import Boolean, BooleanFalse, BooleanTrue
2727
from sympy.matrices.dense import MutableDenseMatrix
2828

29+
import amici
30+
2931
from . import has_clibs
3032
from .constants import SymbolId
3133
from .de_export import (
@@ -290,7 +292,7 @@ def sbml2amici(
290292
cache_simplify: bool = False,
291293
generate_sensitivity_code: bool = True,
292294
hardcode_symbols: Sequence[str] = None,
293-
) -> None:
295+
) -> amici.Model | None:
294296
"""
295297
Generate and compile AMICI C++ files for the model provided to the
296298
constructor.
@@ -379,6 +381,10 @@ def sbml2amici(
379381
Their values cannot be changed anymore after model import.
380382
Currently, only parameters that are not targets of rules or
381383
initial assignments are supported.
384+
385+
:return:
386+
If `compile` is `True` and compilation was successful, an instance
387+
of the generated model class, otherwise `None`.
382388
"""
383389
set_log_level(logger, verbose)
384390

@@ -413,6 +419,14 @@ def sbml2amici(
413419
)
414420
exporter.compile_model()
415421

422+
from . import import_model_module
423+
424+
return import_model_module(
425+
module_name=model_name, module_path=output_dir
426+
).get_model()
427+
428+
return None
429+
416430
def sbml2jax(
417431
self,
418432
model_name: str,

python/tests/test_pysb.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,17 +343,13 @@ def test_heavyside_and_special_symbols():
343343
)
344344

345345
with TemporaryDirectoryWinSafe(prefix=model.name) as outdir:
346-
pysb2amici(
346+
amici_model = pysb2amici(
347347
model,
348348
outdir,
349349
verbose=True,
350350
observation_model=[amici.MeasurementChannel("a")],
351351
)
352352

353-
model_module = amici.import_model_module(
354-
module_name=model.name, module_path=outdir
355-
)
356-
amici_model = model_module.get_model()
357353
assert amici_model.ne
358354

359355

python/tests/test_sbml_import.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def test_presimulation_events_and_sensitivities(tempdir):
377377
from amici.antimony_import import antimony2amici
378378

379379
model_name = "test_presim_events2"
380-
antimony2amici(
380+
model = antimony2amici(
381381
"""
382382
some_time = time
383383
some_time' = 1
@@ -394,9 +394,6 @@ def test_presimulation_events_and_sensitivities(tempdir):
394394
output_dir=tempdir,
395395
)
396396

397-
model_module = import_model_module(model_name, tempdir)
398-
399-
model = model_module.get_model()
400397
model.set_timepoints([0, 1, 2])
401398
edata = amici.ExpData(model)
402399
edata.t_presim = 2
@@ -992,11 +989,12 @@ def test_import_same_model_name(tempdir):
992989
if sys.platform == "win32":
993990
return
994991

995-
antimony2amici(
996-
ant_model_3,
997-
model_name=module_name,
998-
output_dir=outdir_2,
999-
)
992+
with pytest.raises(RuntimeError, match="in the same location"):
993+
antimony2amici(
994+
ant_model_3,
995+
model_name=module_name,
996+
output_dir=outdir_2,
997+
)
1000998

1001999
with pytest.raises(RuntimeError, match="in the same location"):
10021000
import_model_module(module_name=module_name, module_path=outdir_2)

0 commit comments

Comments
 (0)