Skip to content
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
33 changes: 29 additions & 4 deletions petab/v2/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@

import libsbml
import sympy as sp
from sbmlmath import sbml_math_to_sympy, set_math
from sbmlmath import CSymbol, TimeSymbol, sbml_math_to_sympy, set_math

from .assignments_to_experiments import (
DEFAULT_EXPERIMENT_ID,
assignments_to_experiments,
)
from .C import TIME_SYMBOL
from .core import (
Change,
Condition,
Expand All @@ -39,6 +40,21 @@
logger = logging.getLogger(__name__)


def _petab_to_sbml_math(expr: sp.Basic) -> sp.Basic:
"""Convert a PEtab math expression for SBML math export via `sbmlmath`.

Currently, this only replaces the PEtab ``time`` symbol by the SBML
time csymbol.
"""
return expr.subs(
{
s: TimeSymbol(TIME_SYMBOL)
for s in expr.free_symbols
if s.name == TIME_SYMBOL and not isinstance(s, CSymbol)
}
)


class ExperimentsToSbmlConverter:
"""Convert PEtab experiments to SBML.

Expand Down Expand Up @@ -377,7 +393,9 @@ def _convert_experiment(
for change in self._new_problem.get_changes_for_period(period):
period0_assignments.setdefault(
change.target_id, []
).append((exp_ind_id, change.target_value))
).append(
(exp_ind_id, _petab_to_sbml_math(change.target_value))
)
else:
ev = self._create_period_start_event(
experiment=experiment,
Expand Down Expand Up @@ -454,7 +472,10 @@ def _create_initial_assignments(
# introduced in the PEtab parameter table - those need
# to be added to the model
for sym in free_symbols_in_assignments:
if model.getElementBySId(sym.name) is None:
if (
not isinstance(sym, CSymbol)
and model.getElementBySId(sym.name) is None
):
add_sbml_parameter(model, id_=sym.name, constant=True, value=0)

@staticmethod
Expand Down Expand Up @@ -670,6 +691,7 @@ def _add_assignment(
"""Add a single event assignment to the given event
and apply any necessary changes to the model."""
sbml_model = event.getModel()
target_value = _petab_to_sbml_math(target_value)
ea = event.createEventAssignment()
ea.setVariable(target_id)
set_math(ea, target_value)
Expand All @@ -691,7 +713,10 @@ def _add_assignment(
# introduced in the PEtab parameter table - those need
# to be added to the model
for sym in target_value.free_symbols:
if sbml_model.getElementBySId(sym.name) is None:
if (
not isinstance(sym, CSymbol)
and sbml_model.getElementBySId(sym.name) is None
):
add_sbml_parameter(
sbml_model, id_=sym.name, constant=True, value=0
)
Expand Down
37 changes: 37 additions & 0 deletions tests/v2/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,40 @@ def test_infinite_priority():
"E2": 5,
PETAB_EVENT: inf,
}


def test_time_in_target_value():
"""`time` in target values maps to the SBML time csymbol."""
from sbmlmath import TimeSymbol

problem = Problem()
problem.model = SbmlModel.from_antimony("species A = 1; species B = 1")
# first period -> initial assignment; second period -> event
problem.add_condition("c0", B="time + 2")
problem.add_condition("c1", A="time + 3")
problem.add_experiment("e1", 0, "c0", 5, "c1")
problem.add_observable("obs1", "A", noise_formula="1")
problem.add_measurement("obs1", experiment_id="e1", time=1, measurement=1)

converted = ExperimentsToSbmlConverter(problem).convert()
sbml_model = converted.model.sbml_model

assert sbml_model.getElementBySId("time") is None

(ea,) = (
ea
for ev in sbml_model.getListOfEvents()
for ea in ev.getListOfEventAssignments()
if ea.getVariable() == "A"
)
ea_expr = sbml_math_to_sympy(ea.getMath())
(t,) = ea_expr.free_symbols
assert isinstance(t, TimeSymbol)
assert ea_expr.subs(t, 7) == 10

ia = sbml_model.getInitialAssignmentBySymbol("B")
ia_expr = sbml_math_to_sympy(ia.getMath())
assert {type(s) for s in ia_expr.free_symbols} == {sp.Symbol, TimeSymbol}
assert {
s.name for s in ia_expr.free_symbols if not isinstance(s, TimeSymbol)
} == {"_petab_experiment_indicator_e1"}
Loading