Skip to content

Fix compatibility with SymPy 1.15 - #3578

Open
dweindl wants to merge 1 commit into
AMICI-dev:mainfrom
dweindl:sympy_1_15
Open

dweindl wants to merge 1 commit into
AMICI-dev:mainfrom
dweindl:sympy_1_15

Conversation

@dweindl

@dweindl dweindl commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

SymPy 1.15 (currently 1.15.0rc2) solves some Min/Max event triggers for t for which it raised NotImplementedError before. For triggers that AMICI's solve_minmax_trigger_for_t doesn't handle, in particular Or-combinations with a time-independent condition, the sp.solve fallback now returns, e.g., Piecewise((5, p < 1/2), (nan, True)) for Max(t - 5, p - 1/2) ((time > 5) || (p > 0.5)). Such events were treated as having explicit trigger times. Effects, checked with a test model with such triggers (gated Or, decreasing Or, And of gated Ors, different initialValues, parameter-dependent trigger times):

  • JAX backend: the NaN ends up in the known discontinuity times (_known_discs), which are passed to diffrax as jump_ts. As a result, all events that should trigger after t0 were missed, including those of unrelated triggers like time > tau in the same model, and jax.jacfwd failed ("A linear solver received non-finite (NaN or inf) input").
  • SUNDIALS backend: simulation results weren't affected (event counts, states and forward sensitivities matched those with this change up to solver tolerances), as the C++ side skips only the individual NaN trigger time, and triggers that hold from the start are handled by the initial trigger check. However, whenever the NaN branch applies, the simulation logs a warning ("The trigger time of event ... is NaN. The event will not trigger.").

This change discards solutions containing NaN and leaves those triggers to root-finding, as with SymPy<1.15.

Tests now accept the results of both SymPy<1.15 and SymPy>=1.15:

  • C++ code printing changed (-(1.0/10.0)*x instead of -1.0/10.0*x; generally, b - a instead of -a + b)
  • str() of an unevaluated division by 0.0 no longer raises ZeroDivisionError, so test_hill_function_dwdx now checks the denominators directly
  • SymPy 1.15 additionally solves some triggers correctly (Min(t - 5, 10 - t) → {5, 10}; Max(t - 5, Min(t - 10, p)) → {5})

CI was run with sympy>=1.15.0rc2 temporarily required (all checks passed, including the SBML test suite and the manually triggered workflows); that requirement has been reverted, so CI on the current head uses SymPy 1.14. Generated model code differs between the two SymPy versions only by term ordering / parentheses (checked for 134 SBML test suite models).

🤖 Generated with Claude Code

@dweindl dweindl added python dependencies Pull requests that update a dependency file events related to handing discrete events labels Nov 16, 2026
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.72%. Comparing base (7fb8c57) to head (bf31c1d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3578      +/-   ##
==========================================
+ Coverage   80.71%   80.72%   +0.01%     
==========================================
  Files         331      331              
  Lines       23688    23701      +13     
  Branches     1674     1674              
==========================================
+ Hits        19120    19133      +13     
  Misses       4562     4562              
  Partials        6        6              
Flag Coverage Δ
cpp 75.28% <100.00%> (-0.03%) ⬇️
cpp_python 34.86% <75.00%> (-0.01%) ⬇️
petab 47.18% <75.00%> (+0.03%) ⬆️
petab_sciml 16.50% <0.00%> (-0.01%) ⬇️
petab_sciml_benchmarks 15.11% <0.00%> (+<0.01%) ⬆️
python 72.97% <100.00%> (+<0.01%) ⬆️
sbmlsuite 49.72% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ython/sdist/amici/_symbolic/de_model_components.py 96.70% <100.00%> (+0.03%) ⬆️

... and 2 files with indirect coverage changes

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

@dweindl dweindl self-assigned this Nov 16, 2026
SymPy 1.15 solves `Min`/`Max` event triggers for `t` (raising
`NotImplementedError` before), returning, e.g.,
`Piecewise((5, p < 1/2), (nan, True))` for `Max(t - 5, p - 1/2)`. Such
events were treated as having explicit trigger times. With the JAX
backend, the NaN ended up in the known discontinuity times, and
time-triggered events of the model were missed. With the SUNDIALS
backend, it caused spurious warnings about NaN trigger times. Discard
solutions containing NaN and leave those triggers to root-finding.

Update tests to accept the results of both SymPy<1.15 and SymPy>=1.15
(changed C++ code printing, `str()` of unevaluated division by zero no
longer raising, additionally solvable triggers).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dweindl
dweindl marked this pull request as ready for review November 17, 2026 08:40
@dweindl
dweindl requested a review from a team as a code owner November 17, 2026 08:40

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

dependencies Pull requests that update a dependency file events related to handing discrete events python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant