Fix SBML math parsing - #509
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
dweindl
marked this pull request as ready for review
August 12, 2026 08:13
dilpath
approved these changes
Aug 12, 2026
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 |
Member
Author
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
sympify_sbmlnow parses SBML MathML directly viasbmlmath.sbml_math_to_sympyinstead of round-tripping throughlibsbml.formulaToL3String+sympy.sympify, which silently applies Python operator semantics to SBML L3 formulas.sbmlmathfrom thedocextra to a core dependency, sincepetab.v1now needs it unconditionally.%uses sign-of-dividend semantics, Python's%uses sign-of-divisor — the old code silently returned the wrong value for negative operands.Fixes #283