Skip to content

Fix non-constant Booleans in numeric context - #584

Draft
dweindl wants to merge 2 commits into
PEtab-dev:mainfrom
dweindl:fix-bool2num
Draft

dweindl wants to merge 2 commits into
PEtab-dev:mainfrom
dweindl:fix-bool2num

Conversation

@dweindl

@dweindl dweindl commented Nov 12, 2026

Copy link
Copy Markdown
Member

Fix non-constant Booleans in numeric context, e.g. 1 + (x > 0), which failed with TypeError: Relational cannot be used in Add. Per the PEtab v2 spec, true is converted to 1.0 and false to 0.0.

bool2num now converts non-constant Booleans b to Piecewise((1.0, b), (0.0, True)). piecewise values are converted as well, since piecewise is float-valued. petab_math_str can now print ITE, which sympy may create in Piecewise conditions (e.g. for (x > 0) == (y > 2)).

Behaviour change: sympify_petab applies this conversion to the final result as well, so a top-level Boolean expression like x > 0 is now returned as Piecewise((1.0, x > 0), (0.0, True)) instead of a relational. This is consistent with the existing handling of true -> 1.0 and with the PEtab test suite.

Builds on #583; only the last commit belongs to this PR. It also removes the Piecewise handling that #583 added to num2bool, which is no longer needed, and updates the expected values in #583's tests to floats.

Fixes #582.

🤖 Generated with Claude Code

dweindl and others added 2 commits November 12, 2026 14:27
`num2bool` fell back to `Piecewise((True, x != 0.0), (False, True))`,
where `x != 0.0` is Python's structural inequality, so any non-constant
operand of `&&`, `||`, `!` or a `piecewise` condition was treated as
`true`. Return `Ne(x, 0)` instead.

Also convert symbols (which are sympy `Boolean`s as well) and
Boolean-valued `Piecewise` operands.

Fixes PEtab-dev#581.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`bool2num` only converted `true` and `false`, so e.g. `1 + (x > 0)`
failed with `TypeError: Relational cannot be used in Add`. Convert
other Booleans `b` to `Piecewise((1.0, b), (0.0, True))`, and convert
`piecewise` values, since `piecewise` is float-valued.

Since `sympify_petab` applies `bool2num` to the final result, a
top-level Boolean expression like `x > 0` is now returned as a
`Piecewise` instead of a relational, consistent with `true` -> `1.0`.

Add printing of `ITE`, which sympy may create in `Piecewise`
conditions.

Fixes PEtab-dev#582.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dweindl dweindl self-assigned this Nov 12, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.51%. Comparing base (09c1998) to head (ffcbaba).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #584      +/-   ##
==========================================
+ Coverage   79.48%   79.51%   +0.02%     
==========================================
  Files          70       70              
  Lines        8278     8280       +2     
  Branches     1498     1498              
==========================================
+ Hits         6580     6584       +4     
+ Misses       1207     1206       -1     
+ Partials      491      490       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-constant Booleans in numeric context fail to parse

2 participants