From 2105681cbdfe8fae9c086d5b5062c328dfc74c15 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 16 Nov 2026 15:35:42 +0100 Subject: [PATCH 1/2] `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 --- petab/v2/converters.py | 29 +++++++++++++++++++++++++---- tests/v2/test_converters.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/petab/v2/converters.py b/petab/v2/converters.py index 6d7d2d55..8444ea1d 100644 --- a/petab/v2/converters.py +++ b/petab/v2/converters.py @@ -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, @@ -39,6 +40,17 @@ logger = logging.getLogger(__name__) +def _petab_to_sbml_math(expr: sp.Basic) -> sp.Basic: + """Replace 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. @@ -377,7 +389,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, @@ -454,7 +468,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 @@ -670,6 +687,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) @@ -691,7 +709,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 ) diff --git a/tests/v2/test_converters.py b/tests/v2/test_converters.py index 31747237..d121462b 100644 --- a/tests/v2/test_converters.py +++ b/tests/v2/test_converters.py @@ -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"} From e57d616ecb21a2b6cc04265a112aba5fcdc2fcdb Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 16 Nov 2026 21:53:52 +0100 Subject: [PATCH 2/2] Revise `_petab_to_sbml_math` docstring Co-Authored-By: Claude Opus 5.5 --- petab/v2/converters.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/petab/v2/converters.py b/petab/v2/converters.py index 8444ea1d..b001ad54 100644 --- a/petab/v2/converters.py +++ b/petab/v2/converters.py @@ -41,7 +41,11 @@ def _petab_to_sbml_math(expr: sp.Basic) -> sp.Basic: - """Replace the PEtab ``time`` symbol by the SBML time csymbol.""" + """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)