Skip to content

Commit

Permalink
fix path SNAFU
Browse files Browse the repository at this point in the history
  • Loading branch information
FFroehlich committed Dec 5, 2024
1 parent 513ca2a commit 69f2fa4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
5 changes: 2 additions & 3 deletions python/sdist/amici/jax/ode_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,10 @@ def _generate_jax_code(self) -> None:
"MODEL_API_VERSION": f"'{JAXModel.MODEL_API_VERSION}'",
},
}
outdir = self.model_path / (self.model_name + "_jax")
outdir.mkdir(parents=True, exist_ok=True)

apply_template(
Path(amiciModulePath) / "jax" / "jax.template.py",
outdir / "__init__.py",
self.model_path / "__init__.py",
tpl_data,
)

Expand All @@ -258,6 +256,7 @@ def set_paths(self, output_dir: str | Path | None = None) -> None:
output_dir = Path(os.getcwd()) / f"amici-{self.model_name}"

self.model_path = Path(output_dir).resolve()
self.model_path.mkdir(parents=True, exist_ok=True)

Check warning on line 259 in python/sdist/amici/jax/ode_export.py

View check run for this annotation

Codecov / codecov/patch

python/sdist/amici/jax/ode_export.py#L259

Added line #L259 was not covered by tests

def set_name(self, model_name: str) -> None:
"""
Expand Down
25 changes: 24 additions & 1 deletion python/sdist/amici/petab/import_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ def _can_import_model(
"""
# try to import (in particular checks version)
try:
model_module = amici.import_model_module(model_name, model_output_dir)
model_module = amici.import_model_module(
*_get_package_name_and_path(model_name, model_output_dir, jax)
)
except ModuleNotFoundError:
return False

Expand Down Expand Up @@ -268,3 +270,24 @@ def check_model(
"the current model might also resolve this. Parameters: "
f"{amici_ids_free_required.difference(amici_ids_free)}"
)


def _get_package_name_and_path(
model_name: str, model_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:
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)
return outdir.stem, outdir.parent

Check warning on line 291 in python/sdist/amici/petab/import_helpers.py

View check run for this annotation

Codecov / codecov/patch

python/sdist/amici/petab/import_helpers.py#L290-L291

Added lines #L290 - L291 were not covered by tests
else:
return model_name, Path(model_output_dir)
13 changes: 10 additions & 3 deletions python/sdist/amici/petab/petab_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
from petab.v1.models import MODEL_TYPE_PYSB, MODEL_TYPE_SBML

from ..logging import get_logger
from .import_helpers import _can_import_model, _create_model_name, check_model
from .import_helpers import (
_can_import_model,
_create_model_name,
check_model,
_get_package_name_and_path,
)
from .sbml_import import import_model_sbml

try:
Expand Down Expand Up @@ -136,7 +141,7 @@ def import_petab_problem(
)

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

logger.info(f"Compiling model {model_name} to {model_output_dir}.")
Expand All @@ -160,7 +165,9 @@ def import_petab_problem(
)

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

if jax:
model = model_module.Model()
Expand Down

0 comments on commit 69f2fa4

Please sign in to comment.