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

Fix petab import: initial assignment targets as constant parameters #2345

Merged
merged 2 commits into from
Mar 6, 2024
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
20 changes: 16 additions & 4 deletions python/sdist/amici/petab/sbml_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,26 @@
petab_problem, non_estimated_parameters_as_constants
)

# exclude targets of rules or initial assignments
# exclude targets of rules or initial assignments that are not numbers
sbml_model = petab_problem.model.sbml_model
parser_settings = libsbml.L3ParserSettings()
parser_settings.setModel(sbml_model)
parser_settings.setParseUnits(libsbml.L3P_NO_UNITS)

for fixed_parameter in fixed_parameters.copy():
# check global parameters
if sbml_model.getInitialAssignmentBySymbol(
fixed_parameter
) or sbml_model.getRuleByVariable(fixed_parameter):
if sbml_model.getRuleByVariable(fixed_parameter):
fixed_parameters.remove(fixed_parameter)
continue

Check warning on line 531 in python/sdist/amici/petab/sbml_import.py

View check run for this annotation

Codecov / codecov/patch

python/sdist/amici/petab/sbml_import.py#L531

Added line #L531 was not covered by tests
if ia := sbml_model.getInitialAssignmentBySymbol(fixed_parameter):
sym_math = sp.sympify(
libsbml.formulaToL3StringWithSettings(
ia.getMath(), parser_settings
)
)
if not sym_math.is_Number:
fixed_parameters.remove(fixed_parameter)
continue

return list(sorted(fixed_parameters))

Expand Down
15 changes: 4 additions & 11 deletions python/sdist/amici/sbml_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,11 @@ def __init__(

self._reset_symbols()

# http://sbml.org/Software/libSBML/5.18.0/docs/python-api/classlibsbml_1_1_l3_parser_settings.html#abcfedd34efd3cae2081ba8f42ea43f52
# https://sbml.org/software/libsbml/5.18.0/docs/formatted/python-api/classlibsbml_1_1_l3_parser_settings.html#ab30d7ed52ca24cbb842d0a7fed7f4bfd
# all defaults except disable unit parsing
self.sbml_parser_settings = sbml.L3ParserSettings(
self.sbml,
sbml.L3P_PARSE_LOG_AS_LOG10,
sbml.L3P_EXPAND_UNARY_MINUS,
sbml.L3P_NO_UNITS,
sbml.L3P_AVOGADRO_IS_CSYMBOL,
sbml.L3P_COMPARE_BUILTINS_CASE_INSENSITIVE,
None,
sbml.L3P_MODULO_IS_PIECEWISE,
)
self.sbml_parser_settings = sbml.L3ParserSettings()
self.sbml_parser_settings.setModel(self.sbml)
self.sbml_parser_settings.setParseUnits(sbml.L3P_NO_UNITS)

self._discard_annotations: bool = discard_annotations

Expand Down
2 changes: 1 addition & 1 deletion python/sdist/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ zip_safe = False
# Don't include any URLs here - they are not supported by PyPI:
# HTTPError: 400 Bad Request from https://upload.pypi.org/legacy/
# Invalid value for requires_dist. Error: Can't have direct dependency: ...
petab = petab>=0.2.1
petab = petab>=0.2.9
pysb = pysb>=1.13.1
test =
benchmark_models_petab @ git+https://github.com/Benchmarking-Initiative/Benchmark-Models-PEtab.git@master#subdirectory=src/python
Expand Down
34 changes: 31 additions & 3 deletions python/tests/test_petab_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,49 @@ def simple_sbml_model():
return document, model


@pytest.fixture()
def get_fixed_parameters_model():
"""Create test SBML model for test_get_fixed_parameters"""
ant_model = """
p1 = 1
p2 = 2
p3 = 3
p4 = 4
p5 = 5
p6 = 3^2
p7 = p6
p8 = 8
p8' = 1
p9 := p8
"""
from amici.antimony_import import antimony2sbml

sbml_str = antimony2sbml(ant_model)
sbml_doc = libsbml.SBMLReader().readSBMLFromString(sbml_str)
return sbml_doc, sbml_doc.getModel()


@skip_on_valgrind
def test_get_fixed_parameters(simple_sbml_model):
def test_get_fixed_parameters(get_fixed_parameters_model):
"""Check for correct identification of fixed parameters:

p1: fixed (via condition table)
p2: (so far) not fixed (parametric override in condition table)
p3: fixed (via parameter table `estimate=0`)
p4: not fixed (via parameter table `estimate=1`)
p5: fixed (implicitly, because not listed as estimated)
p6: fixed (implicitly, because not listed as estimated
initial assignment is a number)
p7: not fixed (initial assignment is not a number)
p8: not fixed (rate rule target)
p9: not fixed (assignment rule target)
"""
from amici.petab.sbml_import import (
_get_fixed_parameters_sbml as get_fixed_parameters,
)
from petab.models.sbml_model import SbmlModel

sbml_doc, sbml_model = simple_sbml_model
sbml_doc, sbml_model = get_fixed_parameters_model
condition_df = petab.get_condition_df(
pd.DataFrame(
{
Expand All @@ -77,13 +104,14 @@ def test_get_fixed_parameters(simple_sbml_model):
"p1",
"p3",
"p5",
"p6",
}

assert set(
get_fixed_parameters(
petab_problem, non_estimated_parameters_as_constants=False
)
) == {"p1", "p5"}
) == {"p1", "p5", "p6"}


@skip_on_valgrind
Expand Down
Loading