Skip to content

Commit 3189037

Browse files
authored
Harmonize model output directory kwarg (#3105)
Now it's all `output_dir` instead of `model_output_dir` or `outdir`.
1 parent 8603c1e commit 3189037

24 files changed

Lines changed: 137 additions & 142 deletions

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ See also our [versioning policy](https://amici.readthedocs.io/en/latest/versioni
6464
See the `compile_` argument.
6565
* Removals without deprecation:
6666
* `amici.sbml_import.species_to_parameters` has been removed.
67-
67+
* Model output directory keyword arguments have been harmonized:
68+
What was previously `model_output_dir`, `output_dir`, `outdir` is now
69+
consistently called `output_dir` across the API.
6870

6971
**Features**
7072

python/sdist/amici/exporters/sundials/de_export.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class DEExporter:
145145
def __init__(
146146
self,
147147
de_model: DEModel,
148-
outdir: Path | str | None = None,
148+
output_dir: Path | str | None = None,
149149
verbose: bool | int | None = False,
150150
assume_pow_positivity: bool | None = False,
151151
compiler: str | None = None,
@@ -160,7 +160,7 @@ def __init__(
160160
:param de_model:
161161
DE model definition
162162
163-
:param outdir:
163+
:param output_dir:
164164
see :meth:`amici.de_export.DEExporter.set_paths`
165165
166166
:param verbose:
@@ -208,7 +208,7 @@ def __init__(
208208
)
209209

210210
self.set_name(model_name)
211-
self.set_paths(outdir)
211+
self.set_paths(output_dir)
212212

213213
self._code_printer = AmiciCxxCodePrinter()
214214
for fun in CUSTOM_FUNCTIONS:

python/sdist/amici/importers/petab/_cli/import_petab.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def _parse_cli_args():
2626
parser.add_argument(
2727
"-o",
2828
"--output-dir",
29-
dest="model_output_dir",
29+
dest="output_dir",
3030
help="Name of the model directory to create",
3131
)
3232
parser.add_argument(
@@ -96,7 +96,7 @@ def _main():
9696
import_model_sbml(
9797
model_name=args.model_name,
9898
petab_problem=pp,
99-
model_output_dir=args.model_output_dir,
99+
output_dir=args.output_dir,
100100
compile=args.compile,
101101
generate_sensitivity_code=args.generate_sensitivity_code,
102102
verbose=args.verbose,

python/sdist/amici/importers/petab/_petab_importer.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def __init__(
9494
module_name: str = None,
9595
# TODO: model_id for selecting the model in multi-model problems
9696
# model_id: str = None,
97-
outdir: str | Path = None,
97+
output_dir: str | Path = None,
9898
jax: bool = False,
9999
output_parameter_defaults: dict[str, float] | None = None,
100100
verbose: int | bool = logging.INFO,
@@ -107,7 +107,7 @@ def __init__(
107107
:param compile_: Whether to compile the model extension after import.
108108
:param validate: Whether to validate the PEtab problem before import.
109109
:param module_name: The name of model module to generate.
110-
:param outdir:
110+
:param output_dir:
111111
The output directory where the model files are written to.
112112
:param jax: Whether to generate a JAX model instead of a
113113
SUNDIALS model. Currently, only ``False`` is supported.
@@ -178,8 +178,8 @@ def __init__(
178178
"was specified in the PEtab problem."
179179
)
180180

181-
self._outdir: Path | None = (
182-
None if outdir is None else Path(outdir).absolute()
181+
self._output_dir: Path | None = (
182+
None if output_dir is None else Path(output_dir).absolute()
183183
)
184184
self._jax = jax
185185
self._non_estimated_parameters_as_constants: bool = (
@@ -281,11 +281,11 @@ def model_id(self) -> str:
281281
return self._model_id
282282

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

290290
def _do_import_sbml(self):
291291
"""Import the model.
@@ -309,7 +309,7 @@ def _do_import_sbml(self):
309309

310310
logger.info(
311311
f"Module name is '{self._module_name}'.\n"
312-
f"Writing model code to '{self.outdir}'."
312+
f"Writing model code to '{self.output_dir}'."
313313
)
314314

315315
observation_model = self._get_observation_model()
@@ -354,7 +354,7 @@ def _do_import_sbml(self):
354354
if self._jax:
355355
sbml_importer.sbml2jax(
356356
model_name=self._module_name,
357-
output_dir=self.outdir,
357+
output_dir=self.output_dir,
358358
observation_model=observation_model,
359359
verbose=self._verbose,
360360
# **kwargs,
@@ -365,7 +365,7 @@ def _do_import_sbml(self):
365365
allow_reinit_fixpar_initcond = True
366366
sbml_importer.sbml2amici(
367367
model_name=self._module_name,
368-
output_dir=self.outdir,
368+
output_dir=self.output_dir,
369369
observation_model=observation_model,
370370
fixed_parameters=fixed_parameters,
371371
allow_reinit_fixpar_initcond=allow_reinit_fixpar_initcond,
@@ -398,7 +398,7 @@ def _do_import_pysb(
398398

399399
logger.info(
400400
f"Module name is '{self._module_name}'.\n"
401-
f"Writing model code to '{self.outdir}'."
401+
f"Writing model code to '{self.output_dir}'."
402402
)
403403

404404
observation_model = self._get_observation_model()
@@ -433,7 +433,7 @@ def _do_import_pysb(
433433
pysb2jax(
434434
model=pysb_model,
435435
model_name=self._module_name,
436-
output_dir=self.outdir,
436+
output_dir=self.output_dir,
437437
observation_model=observation_model,
438438
verbose=self._verbose,
439439
pysb_model_has_obs_and_noise=True,
@@ -445,7 +445,7 @@ def _do_import_pysb(
445445
pysb2amici(
446446
model=pysb_model,
447447
model_name=self._module_name,
448-
output_dir=self.outdir,
448+
output_dir=self.output_dir,
449449
verbose=True,
450450
fixed_parameters=fixed_parameters,
451451
observation_model=observation_model,
@@ -571,15 +571,15 @@ def import_module(self, force_import: bool = False) -> amici.ModelModule:
571571
Whether to force re-import even if the model module already exists.
572572
:return: The imported model module.
573573
"""
574-
if not self.outdir.is_dir() or force_import:
574+
if not self.output_dir.is_dir() or force_import:
575575
if self.petab_problem.model.type_id == MODEL_TYPE_SBML:
576576
self._do_import_sbml()
577577
else:
578578
self._do_import_pysb()
579579

580580
return amici.import_model_module(
581581
self._module_name,
582-
self.outdir,
582+
self.output_dir,
583583
)
584584

585585
def create_model(self) -> amici.sim.sundials.Model:

python/sdist/amici/importers/petab/v1/import_helpers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ def _create_model_name(folder: str | Path) -> str:
136136

137137

138138
def _can_import_model(
139-
model_name: str, model_output_dir: str | Path, jax: bool = False
139+
model_name: str, output_dir: str | Path, jax: bool = False
140140
) -> bool:
141141
"""
142142
Check whether a module of that name can already be imported.
143143
"""
144144
# try to import (in particular checks version)
145145
try:
146146
model_module = amici.import_model_module(
147-
*_get_package_name_and_path(model_name, model_output_dir, jax)
147+
*_get_package_name_and_path(model_name, output_dir, jax)
148148
)
149149
except ModuleNotFoundError:
150150
return False
@@ -277,21 +277,21 @@ def check_model(
277277

278278

279279
def _get_package_name_and_path(
280-
model_name: str, model_output_dir: str | Path, jax: bool = False
280+
model_name: str, output_dir: str | Path, jax: bool = False
281281
) -> tuple[str, Path]:
282282
"""
283283
Get the package name and path for the generated model module.
284284
285285
:param model_name:
286286
Name of the model
287-
:param model_output_dir:
287+
:param output_dir:
288288
Target directory for the generated model module
289289
:param jax:
290290
Whether to generate the paths for a JAX or CPP model
291291
:return:
292292
"""
293293
if jax:
294-
outdir = Path(model_output_dir)
294+
outdir = Path(output_dir)
295295
return outdir.stem, outdir.parent
296296
else:
297-
return model_name, Path(model_output_dir)
297+
return model_name, Path(output_dir)

python/sdist/amici/importers/petab/v1/petab_import.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@
4040

4141
def import_petab_problem(
4242
petab_problem: petab.Problem,
43-
model_output_dir: str | Path | None = None,
43+
output_dir: str | Path | None = None,
4444
*,
4545
model_name: str = None,
4646
compile_: bool = None,
4747
non_estimated_parameters_as_constants=True,
4848
jax=False,
4949
**kwargs,
50-
) -> "amici.Model | amici.jax.JAXProblem":
50+
) -> "amici.sim.sundials.Model | amici.jax.JAXProblem":
5151
"""
5252
Create an AMICI model for a PEtab problem.
5353
5454
:param petab_problem:
5555
A petab problem containing all relevant information on the model.
5656
57-
:param model_output_dir:
57+
:param output_dir:
5858
Directory to write the model code to. It will be created if it doesn't
5959
exist. Defaults to :func:`amici.get_model_dir`.
6060
@@ -95,34 +95,33 @@ def import_petab_problem(
9595

9696
if petab_problem.model.type_id == MODEL_TYPE_PYSB and model_name is None:
9797
model_name = petab_problem.pysb_model.name
98-
elif model_name is None and model_output_dir:
99-
model_name = _create_model_name(model_output_dir)
98+
elif model_name is None and output_dir:
99+
model_name = _create_model_name(output_dir)
100100

101101
# generate folder and model name if necessary
102-
if model_output_dir is None:
103-
model_output_dir = amici.get_model_dir(model_name, jax=jax).absolute()
102+
if output_dir is None:
103+
output_dir = amici.get_model_dir(model_name, jax=jax).absolute()
104104
else:
105-
model_output_dir = Path(model_output_dir).absolute()
105+
output_dir = Path(output_dir).absolute()
106106

107-
model_output_dir.mkdir(parents=True, exist_ok=True)
107+
output_dir.mkdir(parents=True, exist_ok=True)
108108

109109
# check if compilation necessary
110110
if compile_ or (
111-
compile_ is None
112-
and not _can_import_model(model_name, model_output_dir, jax)
111+
compile_ is None and not _can_import_model(model_name, output_dir, jax)
113112
):
114113
# check if folder exists
115-
if os.listdir(model_output_dir) and not compile_:
114+
if os.listdir(output_dir) and not compile_:
116115
raise ValueError(
117-
f"Cannot compile to {model_output_dir}: not empty. "
116+
f"Cannot compile to {output_dir}: not empty. "
118117
"Please assign a different target or set `compile_` to `True`."
119118
)
120119

121120
# remove folder if exists
122-
if not jax and os.path.exists(model_output_dir):
123-
shutil.rmtree(model_output_dir)
121+
if not jax and os.path.exists(output_dir):
122+
shutil.rmtree(output_dir)
124123

125-
logger.info(f"Compiling model {model_name} to {model_output_dir}.")
124+
logger.info(f"Compiling model {model_name} to {output_dir}.")
126125

127126
if "sciml" in petab_problem.extensions_config:
128127
from petab_sciml.standard import NNModelStandard
@@ -235,15 +234,15 @@ def import_petab_problem(
235234
import_model_pysb(
236235
petab_problem,
237236
model_name=model_name,
238-
model_output_dir=model_output_dir,
237+
output_dir=output_dir,
239238
jax=jax,
240239
**kwargs,
241240
)
242241
else:
243242
import_model_sbml(
244243
petab_problem=petab_problem,
245244
model_name=model_name,
246-
model_output_dir=model_output_dir,
245+
output_dir=output_dir,
247246
non_estimated_parameters_as_constants=non_estimated_parameters_as_constants,
248247
hybridization=hybridization,
249248
jax=jax,
@@ -252,7 +251,7 @@ def import_petab_problem(
252251

253252
# import model
254253
model_module = amici.import_model_module(
255-
*_get_package_name_and_path(model_name, model_output_dir, jax=jax)
254+
*_get_package_name_and_path(model_name, output_dir, jax=jax)
256255
)
257256

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

263262
logger.info(
264-
f"Successfully loaded jax model {model_name} "
265-
f"from {model_output_dir}."
263+
f"Successfully loaded jax model {model_name} from {output_dir}."
266264
)
267265

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

275-
logger.info(
276-
f"Successfully loaded model {model_name} from {model_output_dir}."
277-
)
273+
logger.info(f"Successfully loaded model {model_name} from {output_dir}.")
278274

279275
return model
280276

python/sdist/amici/importers/petab/v1/pysb_import.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def _add_initialization_variables(
195195
@log_execution_time("Importing PEtab model", logger)
196196
def import_model_pysb(
197197
petab_problem: petab.Problem,
198-
model_output_dir: str | Path | None = None,
198+
output_dir: str | Path | None = None,
199199
verbose: bool | int | None = True,
200200
model_name: str | None = None,
201201
jax: bool = False,
@@ -207,7 +207,7 @@ def import_model_pysb(
207207
:param petab_problem:
208208
PySB PEtab problem
209209
210-
:param model_output_dir:
210+
:param output_dir:
211211
Directory to write the model code to. Will be created if doesn't
212212
exist. Defaults to current directory.
213213
@@ -293,7 +293,7 @@ def import_model_pysb(
293293

294294
pysb2jax(
295295
model=pysb_model,
296-
output_dir=model_output_dir,
296+
output_dir=output_dir,
297297
model_name=model_name,
298298
verbose=True,
299299
observation_model=observation_model,
@@ -306,7 +306,7 @@ def import_model_pysb(
306306

307307
pysb2amici(
308308
model=pysb_model,
309-
output_dir=model_output_dir,
309+
output_dir=output_dir,
310310
model_name=model_name,
311311
verbose=True,
312312
fixed_parameters=fixed_parameters,

0 commit comments

Comments
 (0)