Skip to content

Commit

Permalink
Make model module accessible from model instance (#1932)
Browse files Browse the repository at this point in the history
This makes it more convenient to create a new (unmodified) model instance, or to find out where the model files are located.

Also adds a snake_case version of `getModel`.
  • Loading branch information
dweindl authored Jan 16, 2023
1 parent 7f1eee3 commit 6e44a70
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
9 changes: 7 additions & 2 deletions python/sdist/amici/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,19 @@ def _imported_from_setup() -> bool:
from .sbml_import import SbmlImporter, assignmentRules2observables
from .ode_export import ODEModel, ODEExporter

from typing import Protocol
from typing import Protocol, runtime_checkable


@runtime_checkable
class ModelModule(Protocol):
"""Enable Python static type checking for AMICI-generated model
modules"""

def getModel(self) -> amici.Model:
pass
...

def get_model(self) -> amici.Model:
...


class add_path:
Expand Down
3 changes: 2 additions & 1 deletion python/sdist/amici/__init__.template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'version currently installed.'
)

from TPL_MODELNAME._TPL_MODELNAME import *
from .TPL_MODELNAME import *
from .TPL_MODELNAME import getModel as get_model

__version__ = 'TPL_PACKAGE_VERSION'
5 changes: 5 additions & 0 deletions python/tests/test_sbml_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ def test_logging_works(observable_dependent_error_model, caplog):
assert rdata.status != amici.AMICI_SUCCESS
assert "mxstep steps taken" in caplog.text

@skip_on_valgrind
def test_model_module_is_set(observable_dependent_error_model):
model_module = observable_dependent_error_model
assert isinstance(model_module.getModel().module, amici.ModelModule)


@pytest.fixture(scope='session')
def model_steadystate_module():
Expand Down
7 changes: 7 additions & 0 deletions swig/modelname.template.i
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ using namespace amici;
%}


// Make model module accessible from the model
%feature("pythonappend") amici::generic_model::getModel %{
import sys
val.module = sys.modules['.'.join(__name__.split('.')[:-1])]
%}


// Process symbols in header
%include "wrapfunctions.h"

0 comments on commit 6e44a70

Please sign in to comment.