Skip to content

Auto-assign event priority to a single unprioritized event - #562

Merged
dweindl merged 2 commits into
PEtab-dev:mainfrom
dweindl:auto_event_priority
Oct 20, 2026
Merged

dweindl merged 2 commits into
PEtab-dev:mainfrom
dweindl:auto_event_priority

Conversation

@dweindl

@dweindl dweindl commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

ExperimentsToSbmlConverter now assigns a priority to a pre-existing model event without priority if it is the only such event and default_priority is not provided. The assigned priority is lower than all other finite priorities in the model (or 0 if there are none), and the condition-change events get max + 1 as before. This way, the condition-change events are executed before this event, e.g., in PEtab test suite case 0030, where previously the order was undefined.

Per SBML L3V2 §4.12.3/§8.2.5, the order of events without priority is undefined, so assigning a priority only selects one admissible order. Since the assigned value is distinct from all other priorities, no ties are introduced, which would require random execution order. With multiple unprioritized events, nothing is assigned automatically, as this would impose an arbitrary order on them if they are executed simultaneously.

This implements step 1 of #526, generalized to the case of one unprioritized event among prioritized ones (the partial-priority example in the issue). For the automatic assignment, a <priority> without math is treated as unset. If any other event has priority -inf or NaN, nothing is assigned, as no distinct lower priority exists. If any pre-existing event has priority +inf or NaN, a warning is emitted, since the condition-change events cannot be assigned a higher priority. The warning for unprioritized events is left for #525.

References #526

🤖 Generated with Claude Code

@codecov-commenter

codecov-commenter commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.44%. Comparing base (57f3a49) to head (12f0553).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #562      +/-   ##
==========================================
+ Coverage   78.20%   78.44%   +0.23%     
==========================================
  Files          68       68              
  Lines        7845     7876      +31     
  Branches     1400     1406       +6     
==========================================
+ Hits         6135     6178      +43     
+ Misses       1224     1214      -10     
+ Partials      486      484       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dweindl
dweindl marked this pull request as ready for review October 19, 2026 18:38
@dweindl
dweindl requested a review from a team as a code owner October 19, 2026 18:38
@dweindl dweindl self-assigned this Oct 19, 2026
@dweindl
dweindl force-pushed the auto_event_priority branch from 6946c4d to c0ba478 Compare October 20, 2026 05:47
Comment thread petab/v2/converters.py Outdated
@@ -31,10 +35,14 @@ class ExperimentsToSbmlConverter:
experiments to initial assignments and events as far as possible.

If the model already contains events, PEtab events are added with a higher

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If the model already contains events, PEtab events are added with a higher
If the model already contains events, SBML events are added with a higher

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworded

Comment thread petab/v2/converters.py Outdated
If the model already contains events, PEtab events are added with a higher
priority than the existing events to guarantee that PEtab condition changes
are applied before any pre-existing assignments.
priority than the existing events, so that PEtab condition changes are

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
priority than the existing events, so that PEtab condition changes are
priority than the pre-existing events, so that PEtab condition changes are

Comment thread tests/v2/test_converters.py Outdated
E0_PRIO = "E0: at time >= 10, priority=-2, fromTrigger=false: C = C * 2"
E1 = "E1: at time >= 20, fromTrigger=false: C = C"
E1_PRIO = "E1: at time >= 20, priority=5, fromTrigger=false: C = C"
E1_PRIO_INF = "E1: at time >= 20, priority=-inf, fromTrigger=false: C = C"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test the priority=inf case, where it becomes impossible to create the new SBML event?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added warning and test

assert _event_priorities(problem.model.sbml_model) == {"E0": None}


def test_explicit_default_priority():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test that default priority only affects events with undefined priority?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extended the test. Includes an event with defined priority which is confirmed to be preserved.

Comment thread petab/v2/converters.py Outdated
Comment on lines +243 to +245
if not math.isfinite(priority):
# no distinct lower priority available
return

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only a lower priority is required here, then I guess +inf is fine here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's allowed now

Comment thread petab/v2/converters.py Outdated
Comment on lines +239 to +241
if priority.free_symbols:
# raises NotImplementedError in _preprocess
return

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raise here too, or will it be fixed here automatically when it's fixed in _preprocess?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now handled in _get_event_priorities

Comment thread tests/v2/test_converters.py
`ExperimentsToSbmlConverter` now assigns a priority to a pre-existing
model event without priority if it is the only such event, so that the
condition-change events are executed before it. The assigned priority
is lower than all other finite priorities in the model, so no ties are
introduced. This only applies if `default_priority` is not provided,
and not if any other event has priority `-inf` or `NaN`.

Previously, the order of such events relative to the condition-change
events was undefined (e.g., PEtab test suite case 0030).

Additionally, warn if a pre-existing event has priority `+inf` or
`NaN`, since the condition-change events cannot be assigned a higher
priority.

References PEtab-dev#526

Co-authored-by: Dilan Pathirana <59329744+dilpath@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dweindl
dweindl force-pushed the auto_event_priority branch from c0ba478 to 4002964 Compare October 20, 2026 19:42
@dweindl
dweindl enabled auto-merge (squash) October 20, 2026 20:15
@dweindl
dweindl merged commit e654711 into PEtab-dev:main Oct 20, 2026
7 checks passed
@dweindl
dweindl deleted the auto_event_priority branch October 20, 2026 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants