Skip to content

Formulas are parsed without importing the package petab - #389

Merged
matthiaskoenig merged 2 commits into
developfrom
formulas-without-petab-import
Oct 24, 2026
Merged

matthiaskoenig merged 2 commits into
developfrom
formulas-without-petab-import

Conversation

@matthiaskoenig

@matthiaskoenig matthiaskoenig commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner

Problem

compile_formula imported petab.v2.math for its parser sympify_petab. That import pulls in:

  • the package petab, with the problems of PEtab v1, matplotlib.pyplot and scipy.stats;
  • petab.v2, with its SciML extension, petab_sciml and torch.

Every process and every worker of a pool paid this on its first Formula observable or Data function: 0.7 s on an idle machine, 1.4 s under load. Measured with the sampling profiler of python 3.15 on a scan with observables.

Fix

The parser of petab, petab.v1.math, is self-contained: it uses relative imports and needs only sympy, numpy and the runtime of ANTLR.

  • parse_math(formula) imports that directory as the package sbmlsim._petab_math. It is the same code as sympify_petab, so PEtab math keeps exactly petab's semantics.
  • A process which imported petab already uses petab.v2.math.
  • If a future petab moves its parser, parse_math falls back to importing petab.v2.math as before.

The first formula now takes 0.04 s instead of 0.67 s, and torch, petab, matplotlib.pyplot and scipy.stats are no longer imported.

Tests

  • test_compiling_a_formula_does_not_import_petab: in a fresh python, none of these modules is imported. On develop all four are.
  • test_the_parser_is_the_one_of_petab: ten formulas give the same expression as sympify_petab. They cover the precedence of ^ and unary minus, the booleans, piecewise, the logarithms, comparisons, the constants, min/max and numbers.
  • pytest --skip-slow tests/simulator tests/simulation tests/data tests/uncertainty: 878 passed.
  • tests/test_data*.py tests/test_imports.py tests/fit tests/sciml: 2313 passed.

After review

test_the_math_of_petab_v2_is_the_parser_of_petab_v1 fails if petab.v2.math stops re-exporting the parser of v1, which parse_math reads.

compile_formula imported petab.v2.math, which imports the package petab
with matplotlib.pyplot and scipy.stats and petab.v2 with its SciML extension
and torch: 0.7 to 1.4 s for the first formula of a process and of every
worker of a pool. The parser of petab, petab.v1.math, imports only sympy,
numpy and the runtime of ANTLR; parse_math imports its directory as the
package sbmlsim._petab_math. A process which imported petab already uses
it, and a petab whose parser is elsewhere is imported as a whole.
@matthiaskoenig
matthiaskoenig merged commit 22b8f80 into develop Oct 24, 2026
17 checks passed
@matthiaskoenig
matthiaskoenig deleted the formulas-without-petab-import branch October 24, 2026 06:53
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.

1 participant