Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make model module accessible from model instance #1932

Merged
merged 3 commits into from
Jan 16, 2023
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
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"