Skip to content

Fix PEtab v2 species condition changes for PySB models - #3582

Draft
dweindl wants to merge 2 commits into
AMICI-dev:mainfrom
dweindl:pysb_v2_species_changes
Draft

dweindl wants to merge 2 commits into
AMICI-dev:mainfrom
dweindl:pysb_v2_species_changes

Conversation

@dweindl

@dweindl dweindl commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Fixes PEtab v2 species condition changes for PySB models. Based on #3581 (only the last commit is new).

Previously:

  • Any condition change in a period other than the first, including the main period after pre-equilibration, failed with TypeError: Can only work with Components, got a <class 'str'>. Fixing that alone would have hit further problems: an AssertionError for non-parameter targets, symbols without AMICI's assumptions in the trigger (generated code failed to compile), event assignments to parameters silently dropped, and conservation laws not supported with events.
  • First-period species changes required a pysb.Initial with a byte-identical pattern string, and failed for the same species changed in multiple experiments.
  • pysb.Expression targets were silently ignored, because the converter assigned to a non-existent pysb.Expression.value attribute. Expressions are algebraic entities and therefore not valid PEtab v2 condition targets.

Now:

  • Species targets (via the mapping table) are resolved by pattern matching against the generated reaction network and supported in all periods. Later-period changes become event assignments to the species state; target values may depend on the current state.
  • pysb.Expression and pysb.Observable targets raise a ValueError. Compartment targets and compartment-size parameters raise as well, since BioNetGen evaluates compartment sizes during network generation.
  • pysb.Parameter targets raise a NotImplementedError; they are added in Support PEtab v2 condition changes of pysb.Parameters #3583.
  • Entities named t (reserved by AMICI) in changes after the first period raise a NotImplementedError, as the event assignments don't use the renames of the PySB import.
  • The JAX backend raises NotImplementedError for PySB problems with changes after the first period (pysb2jax doesn't support events yet).

Relates to #3068.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.12207% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.82%. Comparing base (0ed2e65) to head (6efedc4).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
...hon/sdist/amici/importers/petab/_petab_importer.py 95.55% 4 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3582      +/-   ##
==========================================
+ Coverage   80.52%   80.82%   +0.30%     
==========================================
  Files         332      331       -1     
  Lines       23698    23850     +152     
  Branches     1690     1674      -16     
==========================================
+ Hits        19082    19276     +194     
+ Misses       4610     4563      -47     
- Partials        6       11       +5     
Flag Coverage Δ
cpp 75.19% <14.15%> (+0.07%) ⬆️
cpp_python 34.85% <14.15%> (+0.05%) ⬆️
petab 48.76% <98.12%> (+1.79%) ⬆️
petab_sciml 16.50% <9.43%> (+0.04%) ⬆️
petab_sciml_benchmarks 15.12% <9.43%> (+0.04%) ⬆️
python 72.86% <14.15%> (+0.08%) ⬆️
sbmlsuite 49.72% <ø> (-0.02%) ⬇️

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

Files with missing lines Coverage Δ
python/sdist/amici/_symbolic/de_model.py 94.03% <100.00%> (+0.01%) ⬆️
...ython/sdist/amici/_symbolic/de_model_components.py 96.77% <100.00%> (+0.10%) ⬆️
python/sdist/amici/importers/pysb/__init__.py 94.84% <100.00%> (+0.12%) ⬆️
python/tests/petab_/test_petab_v2.py 100.00% <100.00%> (ø)
...hon/sdist/amici/importers/petab/_petab_importer.py 92.54% <95.55%> (+3.81%) ⬆️

... and 8 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 and others added 2 commits November 16, 2026 14:10
* Event assignments to entities that are not (solver) state variables
  now raise a `ValueError` in `DEModel.generate_basic_variables`, i.e.,
  before code generation, and in `Event.get_state_update`. Such
  assignments were silently dropped.
* Add `Event.assignment_targets` and `Event.check_assignment_targets`.
* `ode_model_from_pysb_importer` disables conservation laws with a
  warning for models with events, as the SBML importer does, instead of
  raising.

Preparation for PEtab v2 condition changes in later periods for PySB
models (AMICI-dev#3068).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Condition changes in any period but the first one, including the main
period after pre-equilibration, failed with a `TypeError`. They are now
implemented as events:

* Species targets are resolved via pattern matching against the generated
  reaction network. This also removes the requirement of a `pysb.Initial`
  with an identical pattern for first-period changes, and fixes changes of
  the same species in multiple experiments.
* Target values of later-period changes may depend on the current model
  state, model parameters, `time`, and parameter-table parameters.
* No events are created for periods without changes.
* `time` is allowed in first-period target values.

Invalid targets now raise:

* `pysb.Expression`s and observables are algebraic entities and not valid
  condition targets. Their changes were silently ignored.
* Compartments and parameters that compartment sizes depend on (also via
  `pysb.Expression`s) are not supported, as BioNetGen evaluates
  compartment sizes during network generation.
* Changes of `pysb.Parameter`s remain unsupported for now.
* Entities named `t` (reserved by AMICI) in changes after the first
  period, as event assignments don't use the renames of the PySB import.

The JAX backend raises `NotImplementedError` for changes after the first
period, as `pysb2jax` doesn't support events.

Relates to AMICI-dev#3068.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dweindl
dweindl force-pushed the pysb_v2_species_changes branch from 107354a to 6efedc4 Compare November 16, 2026 13:11
@dweindl dweindl self-assigned this Nov 16, 2026

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

PEtab PEtab-import related PySB PySB import related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant