Skip to content

Commit 2b7ea00

Browse files
dweindlclaude
andcommitted
get_model_for_condition: don't remove rate rules for condition-table overrides
`get_model_for_condition` removed *any* SBML rule (including rate rules) for a species/compartment/parameter overridden in the condition table. For rate-rule targets, this turned dynamic species into constants instead of just setting their initial value. Only assignment rules are removed now; rate rules are left in place. Fixes #197 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 73b680c commit 2b7ea00

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

petab/v1/sbml.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,19 @@ def get_param_value(parameter_id: str):
307307
]
308308

309309
def remove_rules(target_id: str):
310-
if sbml_model.removeRuleByVariable(target_id):
310+
"""Remove any assignment rule and initial assignment for
311+
``target_id`` so that its initial value can be set explicitly.
312+
313+
Rate rules are left untouched, since overriding a component's
314+
initial value in the condition table does not affect its
315+
dynamics.
316+
"""
317+
rule = sbml_model.getRuleByVariable(target_id)
318+
if (
319+
rule is not None
320+
and rule.getTypeCode() == libsbml.SBML_ASSIGNMENT_RULE
321+
):
322+
sbml_model.removeRuleByVariable(target_id)
311323
warn(
312324
"An SBML rule was removed to set the component "
313325
f"{target_id} to a constant value.",

tests/v1/test_sbml.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ def create_test_data():
1717
"\n".join(
1818
[
1919
"compartment compartment_1 = 1",
20-
*(f"species species_{i} = 10 * {i}" for i in range(1, 5)),
20+
*(f"species species_{i} = 10 * {i}" for i in range(1, 6)),
2121
*(f"parameter_{i} = {i}" for i in range(1, 4)),
2222
"species_2 := 25",
23+
"species_5' = 1",
2324
]
2425
)
2526
)
@@ -32,6 +33,7 @@ def create_test_data():
3233
"species_2": [25],
3334
"species_3": ["parameter_1"],
3435
"species_4": ["not_a_model_parameter"],
36+
"species_5": [55],
3537
"compartment_1": [2],
3638
}
3739
)
@@ -85,6 +87,12 @@ def check_model(condition_model):
8587
condition_model.getSpecies("species_4").getInitialConcentration()
8688
== 3.25
8789
)
90+
assert (
91+
condition_model.getSpecies("species_5").getInitialConcentration() == 55
92+
)
93+
assert condition_model.getRuleByVariable("species_5") is not None, (
94+
"RateRule was removed although it should have been kept"
95+
)
8896
assert len(condition_model.getListOfInitialAssignments()) == 0, (
8997
"InitialAssignment not removed"
9098
)

0 commit comments

Comments
 (0)