From e65c7e5d8ca6dbe59fb1167c55a70abc139d51fb Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Tue, 1 Dec 2026 12:08:32 +0100 Subject: [PATCH 1/2] Don't compensate assignment-rule species on compartment resize When a period implemented as an event changes a compartment size, `ExperimentsToSbmlConverter` adds event assignments to keep the concentrations of contained species. Species that are assignment rule targets were included, producing invalid SBML (libSBML error 10306). Their values are determined by the rule, so they need no compensation. Assignment rule targets are collected once per event, so the check stays linear in model size. Fixes #646. Co-Authored-By: Claude Opus 5.5 --- ...nts-to-sbml-assignment-rule-species.fix.md | 4 +++ petab/v2/converters.py | 20 +++++++++++-- tests/v2/test_converters.py | 29 +++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 changelog.d/+experiments-to-sbml-assignment-rule-species.fix.md diff --git a/changelog.d/+experiments-to-sbml-assignment-rule-species.fix.md b/changelog.d/+experiments-to-sbml-assignment-rule-species.fix.md new file mode 100644 index 00000000..cc3a5f98 --- /dev/null +++ b/changelog.d/+experiments-to-sbml-assignment-rule-species.fix.md @@ -0,0 +1,4 @@ +`ExperimentsToSbmlConverter` no longer creates event assignments for species +that are assignment rule targets when a compartment is resized after the +first period, which resulted in invalid SBML +([#646](https://github.com/PEtab-dev/libpetab-python/issues/646)) diff --git a/petab/v2/converters.py b/petab/v2/converters.py index 5fd578ee..f4ac0847 100644 --- a/petab/v2/converters.py +++ b/petab/v2/converters.py @@ -784,7 +784,7 @@ def _create_event_assignments_for_period( Therefore, to correctly implement a PEtab change of a compartment size in SBML, we need to compensate for the automatic update of species concentrations by adding event assignments for all contained - concentration-based species. + concentration-based species, except for assignment rule targets. :param event: The SBML event to which the assignments should be added. :param changes: The PEtab condition changes that are to be applied @@ -792,6 +792,20 @@ def _create_event_assignments_for_period( """ _add_assignment = ExperimentsToSbmlConverter._add_assignment sbml_model = event.getModel() + assignment_rule_targets = { + rule.getVariable() + for rule in sbml_model.getListOfRules() + if rule.isAssignment() + } + + def needs_volume_compensation(species: libsbml.Species) -> bool: + """Whether the event must keep the species' concentration + when its compartment is resized.""" + return ( + not species.getHasOnlySubstanceUnits() + and species.getId() not in assignment_rule_targets + ) + # collect IDs of compartments that are changed in this period changed_compartments = { change.target_id @@ -831,7 +845,7 @@ def _create_event_assignments_for_period( species.getId() for species in sbml_model.getListOfSpecies() if species.getCompartment() == change.target_id - and not species.getHasOnlySubstanceUnits() + and needs_volume_compensation(species) ] for species_id in conc_species: if species_change := next( @@ -855,7 +869,7 @@ def _create_event_assignments_for_period( elif ( target_type != libsbml.SBML_SPECIES or sbml_target.getCompartment() not in changed_compartments - or sbml_target.getHasOnlySubstanceUnits() is True + or not needs_volume_compensation(sbml_target) ): # Handle any changes other than compartments and # concentration-based species inside resized compartments diff --git a/tests/v2/test_converters.py b/tests/v2/test_converters.py index 48339620..025a4669 100644 --- a/tests/v2/test_converters.py +++ b/tests/v2/test_converters.py @@ -717,3 +717,32 @@ def test_time_in_target_value(): assert { s.name for s in ia_expr.free_symbols if not isinstance(s, TimeSymbol) } == {"_petab_experiment_indicator_e1"} + + +def test_resize_compartment_skips_assignment_rule_species(): + """Resizing a compartment in an event doesn't create event assignments + for assignment rule targets.""" + problem = Problem() + problem.model = SbmlModel.from_antimony(""" + compartment C = 1; + species B in C = 1; + species S_rule in C; + S_rule := 2 * B; + J: B -> ; 0.1 * B; + """) + problem.add_observable("obs_B", "B", noise_formula="1") + problem.add_condition("c_resize", C=2) + problem.add_experiment("e1", 0, "", 5, "c_resize") + problem.add_measurement( + "obs_B", experiment_id="e1", time=10, measurement=1 + ) + + converted = ExperimentsToSbmlConverter(problem).convert() + sbml_model = converted.model.sbml_model + + ev = sbml_model.getEvent("_petab_event_e1_1") + assert {ea.getVariable() for ea in ev.getListOfEventAssignments()} == { + "C", + "B", + } + assert converted.model.is_valid() From d5837805eee1b95896f64e9422adce0d0b36b46d Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Tue, 1 Dec 2026 12:09:52 +0100 Subject: [PATCH 2/2] Link changelog entry to PR Co-Authored-By: Claude Opus 5.5 --- changelog.d/+experiments-to-sbml-assignment-rule-species.fix.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/+experiments-to-sbml-assignment-rule-species.fix.md b/changelog.d/+experiments-to-sbml-assignment-rule-species.fix.md index cc3a5f98..145fb14a 100644 --- a/changelog.d/+experiments-to-sbml-assignment-rule-species.fix.md +++ b/changelog.d/+experiments-to-sbml-assignment-rule-species.fix.md @@ -1,4 +1,4 @@ `ExperimentsToSbmlConverter` no longer creates event assignments for species that are assignment rule targets when a compartment is resized after the first period, which resulted in invalid SBML -([#646](https://github.com/PEtab-dev/libpetab-python/issues/646)) +([#651](https://github.com/PEtab-dev/libpetab-python/pull/651))