Skip to content

Option to convert model assignments to experiments in petab1to2 - #527

Open
BSnelling wants to merge 2 commits into
mainfrom
bes/models_to_exps
Open

BSnelling wants to merge 2 commits into
mainfrom
bes/models_to_exps

Conversation

@BSnelling

Copy link
Copy Markdown
Collaborator

The option identifies piecewise time dependent assignments from the model, converts them into rows in the experiments/condition tables in the petab v1 problem, and removes those assignments from the model.

@BSnelling
BSnelling requested a review from a team as a code owner October 7, 2026 10:45
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.29630% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.70%. Comparing base (2610499) to head (32037ac).

Files with missing lines Patch % Lines
petab/v2/petab1to2.py 96.29% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #527      +/-   ##
==========================================
+ Coverage   76.15%   76.70%   +0.54%     
==========================================
  Files          67       67              
  Lines        7528     7576      +48     
  Branches     1342     1351       +9     
==========================================
+ Hits         5733     5811      +78     
+ Misses       1294     1267      -27     
+ Partials      501      498       -3     

☔ 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.

@dilpath dilpath left a comment

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.

I did not receive a notification to review this somehow...

Thanks! I think it's a useful addition but I am a bit unsure whether some cases are handled currently.

Comment thread petab/v2/petab1to2.py
(pw,) = pws
(time,) = pw.atoms(TimeSymbol)
(before, switch), _ = pw.args
t_switch = sp.solve(switch.lhs - switch.rhs, time)[0]

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.

Does _assignments_to_periods work overall for both e.g. t>5 and 5>t?

Comment thread petab/v2/petab1to2.py
pws = [pw for pw in expr.atoms(sp.Piecewise) if pw.has(TimeSymbol)]
if not pws:
continue
(pw,) = pws

@dilpath dilpath Oct 8, 2026 •

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.

Does this assume there is only one piecewise function in the assignment? Will >1 piecewise cause an error here? If so, could add an error message so it's properly handled.

Also are piecewises with >2 arguments (i.e. multiple pieces) handled?

Comment thread petab/v2/petab1to2.py
replaced_columns |= {str(s) for s in expr.free_symbols}

for sim_id, preeq_id in pairs:
t_sim = float(t_switch.subs(overrides[sim_id]))

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.

Is it possible that an error arises here (or downstream) if t_switch has species, i.e. t_switch cannot be solved for a specific float time but might still be some sp.Expr involving species?

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.

The overrides could also be problematic. If a species appears in a v1 condition table column, then its initial condition is being set there. Hence, the initial value of a species might be substituted here via overrides I think, which could silently cause an error because species should rather raise an error.

More specifically, a t_switch that contains species cannot be solved in general for specific time(s), before simulation.

Comment thread petab/v2/petab1to2.py
shutil.copy(str(src), str(dest))


def _assignments_to_periods(

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.

I think this replaces SBML piecewise terms with their values from the simulation condition. Maybe this can change the simulation (and thereby likelihood). For example, I think these piecewises would have triggered during preequilibration in v1. If we move them to the experiment table, then they will no longer trigger during preequilibration.

So maybe the only safe way to implement this is to only perform _assignments_to_periods for v1 simulations that do not involve preequilibration. But this seems complicated...

Comment thread petab/v2/petab1to2.py
Comment on lines +455 to +458
elif {str(s) for s in values[t_sim].free_symbols} <= parameter_ids:
# the model already has the pre-switch value, and the switch
# is valid as first period (only parameter table symbols)
del values[0.0]

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.

I am not sure I follow the logic here but could there be an issue if, for example, the model contains an SBML event that changes the value of a PEtab parameter at some timepoint $t: 0 &lt; t &lt; t_{sim}$? @dweindl ?

@dweindl

dweindl commented Oct 9, 2026

Copy link
Copy Markdown
Member

I see how this can be useful. However, I am wondering if it wouldn't be substantially more maintainable if we would separate that from the v1->v2 conversion, and do that as an independent post-conversion pass. In that case, the input would be a v2 problem with a couple of additional things to handle, but those could be rejected (non-zero starting time, non-sbml models, ...).

This branch has not been deployed

No deployments
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.

5 participants