Skip to content

Commit

Permalink
Fix SbmlModel.get_free_parameter_ids_with_values to handle InitialA… (
Browse files Browse the repository at this point in the history
#248)

So far, `SbmlModel.get_free_parameter_ids_with_values` ignored InitialAssignments,
resulting in incorrect parameter values in the parameter mapping.

Now the correct values are used in case of initial assignments that are parameter-independent, and parameters with other initial assignments are ignored.
  • Loading branch information
dweindl authored Mar 6, 2024
1 parent 7e24e14 commit 8a10ace
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion petab/models/sbml_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Iterable, Optional, Tuple

import libsbml
import sympy as sp

from ..sbml import (
get_sbml_model,
Expand Down Expand Up @@ -103,10 +104,41 @@ def get_free_parameter_ids_with_values(
ar.getVariable() for ar in self.sbml_model.getListOfRules()
}

parser_settings = libsbml.L3ParserSettings(
self.sbml_model,
libsbml.L3P_PARSE_LOG_AS_LOG10,
libsbml.L3P_EXPAND_UNARY_MINUS,
libsbml.L3P_NO_UNITS,
libsbml.L3P_AVOGADRO_IS_CSYMBOL,
libsbml.L3P_COMPARE_BUILTINS_CASE_INSENSITIVE,
None,
libsbml.L3P_MODULO_IS_PIECEWISE,
)

def get_initial(p):
# return the initial assignment value if there is one, and it is a
# number; `None`, if there is a non-numeric initial assignment;
# otherwise, the parameter value
if ia := self.sbml_model.getInitialAssignmentBySymbol(p.getId()):
formula_str = libsbml.formulaToL3StringWithSettings(
ia.getMath(), parser_settings
)
try:
return float(formula_str)
except ValueError:
sym_expr = sp.sympify(formula_str)
return (
float(sym_expr.evalf())
if sym_expr.evalf().is_Number
else None
)
return p.getValue()

return (
(p.getId(), p.getValue())
(p.getId(), initial)
for p in self.sbml_model.getListOfParameters()
if p.getId() not in rule_targets
and (initial := get_initial(p)) is not None
)

def get_parameter_ids(self) -> Iterable[str]:
Expand Down

0 comments on commit 8a10ace

Please sign in to comment.