Skip to content

Commit 2cd3222

Browse files
dweindlclaude
andcommitted
ExperimentsToSbmlConverter: map time to the SBML time csymbol
Previously, `time` in condition target values was added to the model as a constant parameter with value 0. Now it is converted to the SBML time csymbol in event assignments and initial assignments, and csymbols are skipped when adding missing parameters. Fixes #589. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 09c1998 commit 2cd3222

2 files changed

Lines changed: 62 additions & 4 deletions

File tree

‎petab/v2/converters.py‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99

1010
import libsbml
1111
import sympy as sp
12-
from sbmlmath import sbml_math_to_sympy, set_math
12+
from sbmlmath import CSymbol, TimeSymbol, sbml_math_to_sympy, set_math
1313

1414
from .assignments_to_experiments import (
1515
DEFAULT_EXPERIMENT_ID,
1616
assignments_to_experiments,
1717
)
18+
from .C import TIME_SYMBOL
1819
from .core import (
1920
Change,
2021
Condition,
@@ -39,6 +40,17 @@
3940
logger = logging.getLogger(__name__)
4041

4142

43+
def _petab_to_sbml_math(expr: sp.Basic) -> sp.Basic:
44+
"""Replace the PEtab ``time`` symbol by the SBML time csymbol."""
45+
return expr.subs(
46+
{
47+
s: TimeSymbol(TIME_SYMBOL)
48+
for s in expr.free_symbols
49+
if s.name == TIME_SYMBOL and not isinstance(s, CSymbol)
50+
}
51+
)
52+
53+
4254
class ExperimentsToSbmlConverter:
4355
"""Convert PEtab experiments to SBML.
4456
@@ -377,7 +389,9 @@ def _convert_experiment(
377389
for change in self._new_problem.get_changes_for_period(period):
378390
period0_assignments.setdefault(
379391
change.target_id, []
380-
).append((exp_ind_id, change.target_value))
392+
).append(
393+
(exp_ind_id, _petab_to_sbml_math(change.target_value))
394+
)
381395
else:
382396
ev = self._create_period_start_event(
383397
experiment=experiment,
@@ -454,7 +468,10 @@ def _create_initial_assignments(
454468
# introduced in the PEtab parameter table - those need
455469
# to be added to the model
456470
for sym in free_symbols_in_assignments:
457-
if model.getElementBySId(sym.name) is None:
471+
if (
472+
not isinstance(sym, CSymbol)
473+
and model.getElementBySId(sym.name) is None
474+
):
458475
add_sbml_parameter(model, id_=sym.name, constant=True, value=0)
459476

460477
@staticmethod
@@ -670,6 +687,7 @@ def _add_assignment(
670687
"""Add a single event assignment to the given event
671688
and apply any necessary changes to the model."""
672689
sbml_model = event.getModel()
690+
target_value = _petab_to_sbml_math(target_value)
673691
ea = event.createEventAssignment()
674692
ea.setVariable(target_id)
675693
set_math(ea, target_value)
@@ -691,7 +709,10 @@ def _add_assignment(
691709
# introduced in the PEtab parameter table - those need
692710
# to be added to the model
693711
for sym in target_value.free_symbols:
694-
if sbml_model.getElementBySId(sym.name) is None:
712+
if (
713+
not isinstance(sym, CSymbol)
714+
and sbml_model.getElementBySId(sym.name) is None
715+
):
695716
add_sbml_parameter(
696717
sbml_model, id_=sym.name, constant=True, value=0
697718
)

‎tests/v2/test_converters.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,40 @@ def test_infinite_priority():
408408
"E2": 5,
409409
PETAB_EVENT: inf,
410410
}
411+
412+
413+
def test_time_in_target_value():
414+
"""`time` in target values maps to the SBML time csymbol."""
415+
from sbmlmath import TimeSymbol
416+
417+
problem = Problem()
418+
problem.model = SbmlModel.from_antimony("species A = 1; species B = 1")
419+
# first period -> initial assignment; second period -> event
420+
problem.add_condition("c0", B="time + 2")
421+
problem.add_condition("c1", A="time + 3")
422+
problem.add_experiment("e1", 0, "c0", 5, "c1")
423+
problem.add_observable("obs1", "A", noise_formula="1")
424+
problem.add_measurement("obs1", experiment_id="e1", time=1, measurement=1)
425+
426+
converted = ExperimentsToSbmlConverter(problem).convert()
427+
sbml_model = converted.model.sbml_model
428+
429+
assert sbml_model.getElementBySId("time") is None
430+
431+
(ea,) = (
432+
ea
433+
for ev in sbml_model.getListOfEvents()
434+
for ea in ev.getListOfEventAssignments()
435+
if ea.getVariable() == "A"
436+
)
437+
ea_expr = sbml_math_to_sympy(ea.getMath())
438+
(t,) = ea_expr.free_symbols
439+
assert isinstance(t, TimeSymbol)
440+
assert ea_expr.subs(t, 7) == 10
441+
442+
ia = sbml_model.getInitialAssignmentBySymbol("B")
443+
ia_expr = sbml_math_to_sympy(ia.getMath())
444+
assert {type(s) for s in ia_expr.free_symbols} == {sp.Symbol, TimeSymbol}
445+
assert {
446+
s.name for s in ia_expr.free_symbols if not isinstance(s, TimeSymbol)
447+
} == {"_petab_experiment_indicator_e1"}

0 commit comments

Comments
 (0)