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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ See also our [versioning policy](https://amici.readthedocs.io/en/latest/versioni
See the `compile_` argument.
* Removals without deprecation:
* `amici.sbml_import.species_to_parameters` has been removed.

* Model output directory keyword arguments have been harmonized:
What was previously `model_output_dir`, `output_dir`, `outdir` is now
consistently called `output_dir` across the API.

**Features**

Expand Down
6 changes: 3 additions & 3 deletions python/sdist/amici/exporters/sundials/de_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class DEExporter:
def __init__(
self,
de_model: DEModel,
outdir: Path | str | None = None,
output_dir: Path | str | None = None,
verbose: bool | int | None = False,
assume_pow_positivity: bool | None = False,
compiler: str | None = None,
Expand All @@ -160,7 +160,7 @@ def __init__(
:param de_model:
DE model definition

:param outdir:
:param output_dir:
see :meth:`amici.de_export.DEExporter.set_paths`

:param verbose:
Expand Down Expand Up @@ -208,7 +208,7 @@ def __init__(
)

self.set_name(model_name)
self.set_paths(outdir)
self.set_paths(output_dir)

self._code_printer = AmiciCxxCodePrinter()
for fun in CUSTOM_FUNCTIONS:
Expand Down
4 changes: 2 additions & 2 deletions python/sdist/amici/importers/petab/_cli/import_petab.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _parse_cli_args():
parser.add_argument(
"-o",
"--output-dir",
dest="model_output_dir",
dest="output_dir",
help="Name of the model directory to create",
)
parser.add_argument(
Expand Down Expand Up @@ -96,7 +96,7 @@ def _main():
import_model_sbml(
model_name=args.model_name,
petab_problem=pp,
model_output_dir=args.model_output_dir,
output_dir=args.output_dir,
compile=args.compile,
generate_sensitivity_code=args.generate_sensitivity_code,
verbose=args.verbose,
Expand Down
32 changes: 16 additions & 16 deletions python/sdist/amici/importers/petab/_petab_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __init__(
module_name: str = None,
# TODO: model_id for selecting the model in multi-model problems
# model_id: str = None,
outdir: str | Path = None,
output_dir: str | Path = None,
jax: bool = False,
output_parameter_defaults: dict[str, float] | None = None,
verbose: int | bool = logging.INFO,
Expand All @@ -107,7 +107,7 @@ def __init__(
:param compile_: Whether to compile the model extension after import.
:param validate: Whether to validate the PEtab problem before import.
:param module_name: The name of model module to generate.
:param outdir:
:param output_dir:
The output directory where the model files are written to.
:param jax: Whether to generate a JAX model instead of a
SUNDIALS model. Currently, only ``False`` is supported.
Expand Down Expand Up @@ -178,8 +178,8 @@ def __init__(
"was specified in the PEtab problem."
)

self._outdir: Path | None = (
None if outdir is None else Path(outdir).absolute()
self._output_dir: Path | None = (
None if output_dir is None else Path(output_dir).absolute()
)
self._jax = jax
self._non_estimated_parameters_as_constants: bool = (
Expand Down Expand Up @@ -281,11 +281,11 @@ def model_id(self) -> str:
return self._model_id

@property
def outdir(self) -> Path:
def output_dir(self) -> Path:
"""The output directory where the model files are written to."""
if self._outdir is None:
self._outdir = get_model_dir(self._module_name, jax=self._jax)
return self._outdir
if self._output_dir is None:
self._output_dir = get_model_dir(self._module_name, jax=self._jax)
return self._output_dir

def _do_import_sbml(self):
"""Import the model.
Expand All @@ -309,7 +309,7 @@ def _do_import_sbml(self):

logger.info(
f"Module name is '{self._module_name}'.\n"
f"Writing model code to '{self.outdir}'."
f"Writing model code to '{self.output_dir}'."
)

observation_model = self._get_observation_model()
Expand Down Expand Up @@ -354,7 +354,7 @@ def _do_import_sbml(self):
if self._jax:
sbml_importer.sbml2jax(
model_name=self._module_name,
output_dir=self.outdir,
output_dir=self.output_dir,
observation_model=observation_model,
verbose=self._verbose,
# **kwargs,
Expand All @@ -365,7 +365,7 @@ def _do_import_sbml(self):
allow_reinit_fixpar_initcond = True
sbml_importer.sbml2amici(
model_name=self._module_name,
output_dir=self.outdir,
output_dir=self.output_dir,
observation_model=observation_model,
fixed_parameters=fixed_parameters,
allow_reinit_fixpar_initcond=allow_reinit_fixpar_initcond,
Expand Down Expand Up @@ -398,7 +398,7 @@ def _do_import_pysb(

logger.info(
f"Module name is '{self._module_name}'.\n"
f"Writing model code to '{self.outdir}'."
f"Writing model code to '{self.output_dir}'."
)

observation_model = self._get_observation_model()
Expand Down Expand Up @@ -433,7 +433,7 @@ def _do_import_pysb(
pysb2jax(
model=pysb_model,
model_name=self._module_name,
output_dir=self.outdir,
output_dir=self.output_dir,
observation_model=observation_model,
verbose=self._verbose,
pysb_model_has_obs_and_noise=True,
Expand All @@ -445,7 +445,7 @@ def _do_import_pysb(
pysb2amici(
model=pysb_model,
model_name=self._module_name,
output_dir=self.outdir,
output_dir=self.output_dir,
verbose=True,
fixed_parameters=fixed_parameters,
observation_model=observation_model,
Expand Down Expand Up @@ -571,15 +571,15 @@ def import_module(self, force_import: bool = False) -> amici.ModelModule:
Whether to force re-import even if the model module already exists.
:return: The imported model module.
"""
if not self.outdir.is_dir() or force_import:
if not self.output_dir.is_dir() or force_import:
if self.petab_problem.model.type_id == MODEL_TYPE_SBML:
self._do_import_sbml()
else:
self._do_import_pysb()

return amici.import_model_module(
self._module_name,
self.outdir,
self.output_dir,
)

def create_model(self) -> amici.sim.sundials.Model:
Expand Down
12 changes: 6 additions & 6 deletions python/sdist/amici/importers/petab/v1/import_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ def _create_model_name(folder: str | Path) -> str:


def _can_import_model(
model_name: str, model_output_dir: str | Path, jax: bool = False
model_name: str, output_dir: str | Path, jax: bool = False
) -> bool:
"""
Check whether a module of that name can already be imported.
"""
# try to import (in particular checks version)
try:
model_module = amici.import_model_module(
*_get_package_name_and_path(model_name, model_output_dir, jax)
*_get_package_name_and_path(model_name, output_dir, jax)
)
except ModuleNotFoundError:
return False
Expand Down Expand Up @@ -277,21 +277,21 @@ def check_model(


def _get_package_name_and_path(
model_name: str, model_output_dir: str | Path, jax: bool = False
model_name: str, output_dir: str | Path, jax: bool = False
) -> tuple[str, Path]:
"""
Get the package name and path for the generated model module.

:param model_name:
Name of the model
:param model_output_dir:
:param output_dir:
Target directory for the generated model module
:param jax:
Whether to generate the paths for a JAX or CPP model
:return:
"""
if jax:
outdir = Path(model_output_dir)
outdir = Path(output_dir)
return outdir.stem, outdir.parent
else:
return model_name, Path(model_output_dir)
return model_name, Path(output_dir)
44 changes: 20 additions & 24 deletions python/sdist/amici/importers/petab/v1/petab_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@

def import_petab_problem(
petab_problem: petab.Problem,
model_output_dir: str | Path | None = None,
output_dir: str | Path | None = None,
*,
model_name: str = None,
compile_: bool = None,
non_estimated_parameters_as_constants=True,
jax=False,
**kwargs,
) -> "amici.Model | amici.jax.JAXProblem":
) -> "amici.sim.sundials.Model | amici.jax.JAXProblem":
"""
Create an AMICI model for a PEtab problem.

:param petab_problem:
A petab problem containing all relevant information on the model.

:param model_output_dir:
:param output_dir:
Directory to write the model code to. It will be created if it doesn't
exist. Defaults to :func:`amici.get_model_dir`.

Expand Down Expand Up @@ -95,34 +95,33 @@ def import_petab_problem(

if petab_problem.model.type_id == MODEL_TYPE_PYSB and model_name is None:
model_name = petab_problem.pysb_model.name
elif model_name is None and model_output_dir:
model_name = _create_model_name(model_output_dir)
elif model_name is None and output_dir:
model_name = _create_model_name(output_dir)

# generate folder and model name if necessary
if model_output_dir is None:
model_output_dir = amici.get_model_dir(model_name, jax=jax).absolute()
if output_dir is None:
output_dir = amici.get_model_dir(model_name, jax=jax).absolute()
else:
model_output_dir = Path(model_output_dir).absolute()
output_dir = Path(output_dir).absolute()

model_output_dir.mkdir(parents=True, exist_ok=True)
output_dir.mkdir(parents=True, exist_ok=True)

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

# remove folder if exists
if not jax and os.path.exists(model_output_dir):
shutil.rmtree(model_output_dir)
if not jax and os.path.exists(output_dir):
shutil.rmtree(output_dir)

logger.info(f"Compiling model {model_name} to {model_output_dir}.")
logger.info(f"Compiling model {model_name} to {output_dir}.")

if "sciml" in petab_problem.extensions_config:
from petab_sciml.standard import NNModelStandard
Expand Down Expand Up @@ -235,15 +234,15 @@ def import_petab_problem(
import_model_pysb(
petab_problem,
model_name=model_name,
model_output_dir=model_output_dir,
output_dir=output_dir,
jax=jax,
**kwargs,
)
else:
import_model_sbml(
petab_problem=petab_problem,
model_name=model_name,
model_output_dir=model_output_dir,
output_dir=output_dir,
non_estimated_parameters_as_constants=non_estimated_parameters_as_constants,
hybridization=hybridization,
jax=jax,
Expand All @@ -252,7 +251,7 @@ def import_petab_problem(

# import model
model_module = amici.import_model_module(
*_get_package_name_and_path(model_name, model_output_dir, jax=jax)
*_get_package_name_and_path(model_name, output_dir, jax=jax)
)

if jax:
Expand All @@ -261,8 +260,7 @@ def import_petab_problem(
model = model_module.Model()

logger.info(
f"Successfully loaded jax model {model_name} "
f"from {model_output_dir}."
f"Successfully loaded jax model {model_name} from {output_dir}."
)

# Create and return JAXProblem
Expand All @@ -272,9 +270,7 @@ def import_petab_problem(
model = model_module.get_model()
check_model(amici_model=model, petab_problem=petab_problem)

logger.info(
f"Successfully loaded model {model_name} from {model_output_dir}."
)
logger.info(f"Successfully loaded model {model_name} from {output_dir}.")

return model

Expand Down
8 changes: 4 additions & 4 deletions python/sdist/amici/importers/petab/v1/pysb_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def _add_initialization_variables(
@log_execution_time("Importing PEtab model", logger)
def import_model_pysb(
petab_problem: petab.Problem,
model_output_dir: str | Path | None = None,
output_dir: str | Path | None = None,
verbose: bool | int | None = True,
model_name: str | None = None,
jax: bool = False,
Expand All @@ -207,7 +207,7 @@ def import_model_pysb(
:param petab_problem:
PySB PEtab problem

:param model_output_dir:
:param output_dir:
Directory to write the model code to. Will be created if doesn't
exist. Defaults to current directory.

Expand Down Expand Up @@ -293,7 +293,7 @@ def import_model_pysb(

pysb2jax(
model=pysb_model,
output_dir=model_output_dir,
output_dir=output_dir,
model_name=model_name,
verbose=True,
observation_model=observation_model,
Expand All @@ -306,7 +306,7 @@ def import_model_pysb(

pysb2amici(
model=pysb_model,
output_dir=model_output_dir,
output_dir=output_dir,
model_name=model_name,
verbose=True,
fixed_parameters=fixed_parameters,
Expand Down
Loading
Loading