Skip to content

Fix SBML math parsing - #509

Merged
dweindl merged 2 commits into
PEtab-dev:mainfrom
dweindl:fix-sbml-math-parsing
Aug 12, 2026
Merged

Fix SBML math parsing#509
dweindl merged 2 commits into
PEtab-dev:mainfrom
dweindl:fix-sbml-math-parsing

Conversation

@dweindl

@dweindl dweindl commented Aug 12, 2026

Copy link
Copy Markdown
Member
  • sympify_sbml now parses SBML MathML directly via sbmlmath.sbml_math_to_sympy instead of round-tripping through libsbml.formulaToL3String + sympy.sympify, which silently applies Python operator semantics to SBML L3 formulas.
  • Promoted sbmlmath from the doc extra to a core dependency, since petab.v1 now needs it unconditionally.
  • Added a regression test: SBML's % uses sign-of-dividend semantics, Python's % uses sign-of-divisor — the old code silently returned the wrong value for negative operands.

Fixes #283

sympy.sympify(formulaToL3String(...)) applies Python operator semantics
to SBML L3 formulas, which silently diverge for constructs like modulo
(sign-of-dividend in SBML vs. sign-of-divisor in Python). Parse the SBML
MathML directly via sbmlmath instead.

Fixes PEtab-dev#283
@codecov-commenter

codecov-commenter commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.61%. Comparing base (4d9422b) to head (3a79ae2).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #509      +/-   ##
==========================================
+ Coverage   75.57%   75.61%   +0.04%     
==========================================
  Files          65       65              
  Lines        7357     7354       -3     
  Branches     1323     1323              
==========================================
+ Hits         5560     5561       +1     
+ Misses       1293     1289       -4     
  Partials      504      504              

☔ 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 August 12, 2026 08:13
@dweindl
dweindl requested a review from a team as a code owner August 12, 2026 08:13

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

👍

Comment thread tests/v1/test_sbml.py
Comment on lines +155 to +171
def test_sympify_sbml_modulo():
"""SBML's ``%`` follows C semantics (result has the sign of the
dividend), unlike Python's ``%`` (result has the sign of the divisor).

Naively converting the SBML math to a string and handing it to
``sympy.sympify`` therefore silently produces a wrong result (gh-283).
"""
sbml_document = libsbml.SBMLDocument(3, 2)
sbml_model = sbml_document.createModel()
parameter = sbml_model.createParameter()
parameter.setId("e")
parameter.setConstant(False)
initial_assignment = sbml_model.createInitialAssignment()
initial_assignment.setSymbol("e")
initial_assignment.setMath(libsbml.parseL3Formula("-5 % 3"))

assert sympify_sbml(initial_assignment).doit() == -2

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.

Move to sbmlmath?

@dweindl dweindl Aug 12, 2026

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 can be added there as well, but I think it makes sense to have it here. This tests exactly what changed in this PR. Would have failed before, passes now.

@dweindl
dweindl merged commit f1c7679 into PEtab-dev:main Aug 12, 2026
7 checks passed
@dweindl
dweindl deleted the fix-sbml-math-parsing branch August 12, 2026 13:01
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.

Fix SBML math parsing

3 participants