Skip to content

Commit 1eafa48

Browse files
committed
default_priority
1 parent 5ef7981 commit 1eafa48

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

petab/v2/converters.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,21 @@ class ExperimentsToEventsConverter:
4747
#: pre-equilibration indicator to 0.
4848
CONDITION_ID_PREEQ_OFF = "_petab_preequilibration_off"
4949

50-
def __init__(self, problem: Problem):
50+
def __init__(self, problem: Problem, default_priority: float = None):
5151
"""Initialize the converter.
5252
5353
:param problem: The PEtab problem to convert.
5454
This will not be modified.
55+
:param default_priority: The priority value to apply to any events that
56+
preexist in the model and do not have a priority set.
57+
58+
In SBML, for event assignments that are to be applied at the same
59+
simulation time, the order of event execution is determined by the
60+
priority of the respective events.
61+
If no priority is set, the order is undefined.
62+
See SBML specs for details.
63+
To ensure that the PEtab condition-start-events are executed before
64+
any other events, all events should have a priority set.
5565
"""
5666
if not isinstance(problem.model, SbmlModel):
5767
raise ValueError("Only SBML models are supported.")
@@ -66,7 +76,7 @@ def __init__(self, problem: Problem):
6676
self._max_event_priority = None
6777
# The priority that will be used for the PEtab events.
6878
self._petab_event_priority = None
69-
79+
self._default_priority = default_priority
7080
self._preprocess()
7181

7282
def _get_experiment_indicator_condition_id(
@@ -88,6 +98,18 @@ def _preprocess(self):
8898
"triggers and automatic upconversion of the model failed."
8999
)
90100

101+
# Apply default priority to all events that do not have a priority
102+
if self._default_priority is not None:
103+
for event in model.getListOfEvents():
104+
if (
105+
not event.getPriority()
106+
or event.getPriority().getMath() is None
107+
):
108+
priority = event.createPriority()
109+
priority.setMath(
110+
libsbml.parseL3Formula(str(self._default_priority))
111+
)
112+
91113
# Collect event priorities
92114
event_priorities = {
93115
ev.getId() or str(ev): sbml_math_to_sympy(ev.getPriority())
@@ -122,7 +144,9 @@ def _preprocess(self):
122144
f"Event `{event.getId()}` has no priority set. "
123145
"Make sure that this event cannot trigger at the time of "
124146
"a PEtab condition change, otherwise the behavior is "
125-
"undefined.",
147+
"undefined. To avoid this warning, see the "
148+
"`default_priority` parameter of "
149+
f"{self.__class__.__name__}.",
126150
stacklevel=1,
127151
)
128152

0 commit comments

Comments
 (0)