Skip to content

Commit 6d10d6f

Browse files
FFroehlichclaudeCopilot
authored
Fix spurious conservation-law assertion for condition-dependent compartments (#3202)
* Fix spurious conservation-law assertion for condition-dependent compartments When importing a PEtab problem with condition-dependent compartment sizes, the conservation-law coefficients contain a product of `Piecewise` factors that cancels to 1. `self.eq("w")` is passed through `self._simplify`, which collapses this product, while `ConservationLaw.get_x_rdata` returns the unsimplified expression. The strict equality check in the `dwdx` computation then failed on mathematically equivalent but structurally different forms, raising a spurious AssertionError (e.g. PEtab v2 test suite case 0012). Apply the same simplification to both sides before comparing. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01U6qHLHHce8fyPNeoT2BxPf * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent e07af43 commit 6d10d6f

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

python/sdist/amici/_symbolic/de_model.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,14 +1817,25 @@ def _compute_equation(self, name: str) -> None:
18171817
self._eqs[name] = self.sym(name)
18181818

18191819
elif name == "dwdx":
1820-
if (
1821-
expected := list(
1822-
map(
1823-
ConservationLaw.get_x_rdata,
1824-
reversed(self.conservation_laws()),
1825-
)
1820+
expected = list(
1821+
map(
1822+
ConservationLaw.get_x_rdata,
1823+
reversed(self.conservation_laws()),
18261824
)
1827-
) != (actual := self.eq("w")[: self.num_cons_law()]):
1825+
)
1826+
actual = self.eq("w")[: self.num_cons_law()]
1827+
# `self.eq("w")` has been passed through `self._simplify`, whereas
1828+
# `ConservationLaw.get_x_rdata` returns the unsimplified expression.
1829+
# Apply the same simplification to the expected `x_rdata`
1830+
# reconstruction before comparing, so that mathematically
1831+
# equivalent but structurally different forms do not trigger a
1832+
# spurious mismatch. This happens, e.g., for condition-dependent
1833+
# compartment sizes, where the coefficients contain a product of
1834+
# `Piecewise` factors that cancels to 1 in `w` but is retained in
1835+
# `get_x_rdata`.
1836+
if self._simplify:
1837+
expected = [self._simplify(expr) for expr in expected]
1838+
if expected != actual:
18281839
raise AssertionError(
18291840
"Conservation laws are not at the beginning of 'w'. "
18301841
f"Got {actual}, expected {expected}."

0 commit comments

Comments
 (0)