Skip to content

Commit

Permalink
User option to fail if model needs to be compiled (#2289)
Browse files Browse the repository at this point in the history
* support "do not compile" for petab problems

* rewrite
  • Loading branch information
dilpath authored Feb 16, 2024
1 parent fa7c0bd commit e4381d4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion documentation/ExampleJax.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@
"source": [
"from amici.petab.petab_import import import_petab_problem\n",
"\n",
"amici_model = import_petab_problem(petab_problem, force_compile=True)"
"amici_model = import_petab_problem(petab_problem, compile_=True)"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions python/examples/example_errors.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"source": [
"petab_problem = benchmark_models_petab.get_problem(\"Fujita_SciSignal2010\")\n",
"amici_model = import_petab_problem(\n",
" petab_problem, verbose=False, force_compile=False\n",
" petab_problem, verbose=False, compile_=None\n",
")\n",
"\n",
"np.random.seed(2991)\n",
Expand Down Expand Up @@ -423,7 +423,7 @@
"source": [
"petab_problem = benchmark_models_petab.get_problem(\"Weber_BMC2015\")\n",
"amici_model = import_petab_problem(\n",
" petab_problem, verbose=False, force_compile=False\n",
" petab_problem, verbose=False, compile_=None\n",
")\n",
"\n",
"np.random.seed(4)\n",
Expand Down Expand Up @@ -699,7 +699,7 @@
"amici_model = import_petab_problem(\n",
" petab_problem,\n",
" verbose=False,\n",
" force_compile=True,\n",
" compile_=True,\n",
" model_name=\"Blasi_CellSystems2016_1\",\n",
")\n",
"\n",
Expand Down Expand Up @@ -819,7 +819,7 @@
" # we need a different model name if we import the model again\n",
" # we cannot load a model with the same name as an already loaded model\n",
" model_name=\"Blasi_CellSystems2016_2\",\n",
" force_compile=True,\n",
" compile_=True,\n",
")\n",
"del os.environ[\"AMICI_EXPERIMENTAL_SBML_NONCONST_CLS\"]\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion python/examples/example_jax/ExampleJax.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@
"from amici.petab.petab_import import import_petab_problem\n",
"\n",
"amici_model = import_petab_problem(\n",
" petab_problem, force_compile=True, verbose=False\n",
" petab_problem, compile_=True, verbose=False\n",
")"
]
},
Expand Down
28 changes: 21 additions & 7 deletions python/sdist/amici/petab/petab_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def import_petab_problem(
petab_problem: petab.Problem,
model_output_dir: Union[str, Path, None] = None,
model_name: str = None,
force_compile: bool = False,
compile_: bool = None,
non_estimated_parameters_as_constants=True,
**kwargs,
) -> "amici.Model":
Expand All @@ -53,9 +53,10 @@ def import_petab_problem(
Name of the generated model module. Defaults to the ID of the model
or the model file name without the extension.
:param force_compile:
Whether to compile the model even if the target folder is not empty,
or the model exists already.
:param compile_:
If ``True``, the model will be compiled. If ``False``, the model will
not be compiled. If ``None``, the model will be compiled if it cannot
be imported.
:param non_estimated_parameters_as_constants:
Whether parameters marked as non-estimated in PEtab should be
Expand All @@ -71,6 +72,16 @@ def import_petab_problem(
:return:
The imported model.
"""
if "force_compile" in kwargs:
if kwargs["force_compile"]:
compile_ = True
warn(
"The `force_compile` option is deprecated, please use the "
"new `compile_` option, which also supports 'do not compile'.",
DeprecationWarning,
stacklevel=2,
)

if petab_problem.model.type_id not in (MODEL_TYPE_SBML, MODEL_TYPE_PYSB):
raise NotImplementedError(
"Unsupported model type " + petab_problem.model.type_id
Expand Down Expand Up @@ -107,12 +118,15 @@ def import_petab_problem(
os.makedirs(model_output_dir)

# check if compilation necessary
if force_compile or not _can_import_model(model_name, model_output_dir):
if compile_ or (
compile_ is None
and not _can_import_model(model_name, model_output_dir)
):
# check if folder exists
if os.listdir(model_output_dir) and not force_compile:
if os.listdir(model_output_dir) and not compile_:
raise ValueError(
f"Cannot compile to {model_output_dir}: not empty. "
"Please assign a different target or set `force_compile`."
"Please assign a different target or set `compile_` to `True`."
)

# remove folder if exists
Expand Down
2 changes: 1 addition & 1 deletion tests/petab_test_suite/test_petab_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _test_case(case, model_type, version):
petab_problem=problem,
model_output_dir=model_output_dir,
model_name=model_name,
force_compile=True,
compile_=True,
)
solver = model.getSolver()
solver.setSteadyStateToleranceFactor(1.0)
Expand Down

0 comments on commit e4381d4

Please sign in to comment.