Skip to content

Commit

Permalink
Don't eliminate parameters that are initial assignment targets (pt2) (#…
Browse files Browse the repository at this point in the history
…2305)

Currently, parameters that are targets of initial assignments don't show up as parameters or expressions in the amici model. This is rather not what most users would expect.

Therefore, treat all SBML parameters that are initial assignment targets and whose initial assignment does not evaluate to a number (for those that do, see #2304) as amici expressions.
Those static expressions will be handled more efficiently after #2303.

Related to #2150.

See also #2304.
  • Loading branch information
dweindl authored Feb 27, 2024
1 parent e679e51 commit 5e386ee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 9 additions & 8 deletions python/sdist/amici/sbml_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ def _gather_base_locals(
"INF": sp.oo,
"NaN": sp.nan,
"rem": sp.Mod,
"time": symbol_with_assumptions("time"),
"time": sbml_time_symbol,
# SBML L3 explicitly defines this value, which is not equal
# to the most recent SI definition.
"avogadro": sp.Float(6.02214179e23),
Expand Down Expand Up @@ -1108,11 +1108,13 @@ def _process_parameters(
}

# Parameters that need to be turned into expressions
# so far, this concerns parameters with initial assignments containing rateOf(.)
# (those have been skipped above)
# so far, this concerns parameters with symbolic initial assignments
# (those have been skipped above) that are not rate rule targets
for par in self.sbml.getListOfParameters():
if (ia := par_id_to_ia.get(par.getId())) is not None and ia.find(
sp.core.function.UndefinedFunction("rateOf")
if (
(ia := par_id_to_ia.get(par.getId())) is not None
and not ia.is_Number
and not self.is_rate_rule_target(par)
):
self.symbols[SymbolId.EXPRESSION][
_get_identifier_symbol(par)
Expand Down Expand Up @@ -1890,6 +1892,7 @@ def _process_initial_assignments(self):
if identifier in itt.chain(
self.symbols[SymbolId.SPECIES],
self.compartments,
self.symbols[SymbolId.EXPRESSION],
self.symbols[SymbolId.PARAMETER],
self.symbols[SymbolId.FIXED_PARAMETER],
):
Expand Down Expand Up @@ -1965,9 +1968,7 @@ def _make_initial(
if "init" in species:
sym_math = smart_subs(sym_math, species_id, species["init"])

sym_math = smart_subs(
sym_math, self._local_symbols["time"], sp.Float(0)
)
sym_math = smart_subs(sym_math, sbml_time_symbol, sp.Float(0))

sym_math = _dummy_to_rateof(sym_math, rateof_to_dummy)

Expand Down
4 changes: 3 additions & 1 deletion tests/testSBMLSuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ def verify_results(settings, rdata, expected, wrapper, model, atol, rtol):
# collect parameters
for par in model.getParameterIds():
simulated[par] = rdata["ts"] * 0 + model.getParameterById(par)
# collect fluxes
# collect fluxes and other expressions
for expr_idx, expr_id in enumerate(model.getExpressionIds()):
if expr_id.startswith("flux_"):
simulated[expr_id.removeprefix("flux_")] = rdata.w[:, expr_idx]
elif expr_id.removeprefix("amici_") not in simulated.columns:
simulated[expr_id] = rdata.w[:, expr_idx]
# handle renamed reserved symbols
simulated.rename(
columns={c: c.replace("amici_", "") for c in simulated.columns},
Expand Down

0 comments on commit 5e386ee

Please sign in to comment.