Skip to content

Fix non-constant Booleans in numeric context - #584

Merged
dweindl merged 2 commits into
PEtab-dev:mainfrom
dweindl:fix-bool2num
Nov 16, 2026
Merged

dweindl merged 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.

num2bool converts such a Piecewise back to its condition instead of wrapping it in Ne(..., 0), so e.g. ((x > 0) + 0) && (y > 0) becomes (x > 0) && (y > 0). The expected values in test_num2bool are now floats, since all results are numeric.

bool2num and num2bool now take an evaluate argument, so that sympify_petab(..., evaluate=False) keeps these conversions unevaluated, e.g. 1 + (1 > 0) stays piecewise(1, 1 > 0, 0) + 1 instead of becoming 1 + 1. sympify_petab now also passes evaluate on for sympy inputs.

Fixes #582.

🤖 Generated with Claude Code

@dweindl dweindl self-assigned this Nov 12, 2026
@codecov-commenter

codecov-commenter commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.42857% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 79.72%. Comparing base (6cb938f) to head (c194a3e).

Files with missing lines Patch % Lines
petab/v1/math/SympyVisitor.py 95.83% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #584      +/-   ##
==========================================
+ Coverage   79.71%   79.72%   +0.01%     
==========================================
  Files          70       70              
  Lines        8315     8325      +10     
  Branches     1506     1508       +2     
==========================================
+ Hits         6628     6637       +9     
  Misses       1198     1198              
- Partials      489      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.

dweindl added a commit that referenced this pull request Nov 16, 2026
`HybridizationTable.to_df` (and thus `to_tsv`) and the target value
check in `CheckHybridizationTable` converted target values with `str()`,
which produces sympy syntax that is not valid PEtab math, e.g. `x**2.0`
for `x^2` or `Abs(y)` for `abs(y)`. Written hybridization tables
therefore could not be read again, and the linter reported valid target
values as invalid. Both now use `petab_math_str`.

Relates to #584, which makes this more common, since Boolean target
values like `x > 0` become `Piecewise` expressions.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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`.

`num2bool` converts such a `Piecewise` back to its condition, instead
of wrapping it in `Ne(..., 0)`.

`bool2num` and `num2bool` now take an `evaluate` argument, so that
`sympify_petab(..., evaluate=False)` keeps the conversions unevaluated.
`sympify_petab` now also passes `evaluate` on for sympy inputs.

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 marked this pull request as ready for review November 16, 2026 15:27
@dweindl
dweindl requested a review from a team as a code owner November 16, 2026 15:27
Comment on lines +66 to +68
assert petab_math_str(sp.ITE(a > 0, b > 0, c > 0)) == (
"((a <= 0) || (b > 0)) && ((a > 0) || (c > 0))"
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, more readable for me as ((a > 0) && (b > 0) || (a <= 0 && c > 0)) but fine

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong opinion here, but for other cases, the alternative becomes more bulky. I'd keep it as is. Most users won't encounter that very often.

@dweindl
dweindl added this pull request to the merge queue Nov 16, 2026
Merged via the queue into PEtab-dev:main with commit fa87ed3 Nov 16, 2026
8 checks passed
@dweindl
dweindl deleted the fix-bool2num branch November 16, 2026 20:49
dweindl added a commit to dweindl/libpetab-python that referenced this pull request Nov 17, 2026
Every pull request added its changelog entry at the same place in
`CHANGELOG.md`, so concurrent pull requests conflicted with each other.

Now, each pull request adds its entry as a separate file
`changelog.d/+<name>.<type>.md` (see `changelog.d/README.md`). At release
time, `towncrier build --version X.Y.Z` collects them into `CHANGELOG.md`.

The unreleased 0.11.0 entry is moved to `changelog.d/`, and entries are
added for PEtab-dev#583, PEtab-dev#584, PEtab-dev#588, PEtab-dev#590, PEtab-dev#592 and PEtab-dev#595, which did not add one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dweindl added a commit to dweindl/libpetab-python that referenced this pull request Nov 17, 2026
Every pull request added its changelog entry at the same place in
`CHANGELOG.md`, so concurrent pull requests conflicted with each other.

Now, each pull request adds its entry as a separate file
`changelog.d/+<name>.<type>.md` (see `changelog.d/README.md`). At release
time, `towncrier build --version X.Y.Z` collects them into `CHANGELOG.md`.

The unreleased 0.11.0 entry is moved to `changelog.d/`, and entries are
added for PEtab-dev#583, PEtab-dev#584, PEtab-dev#588, PEtab-dev#590, PEtab-dev#592 and PEtab-dev#595, which did not add one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dweindl added a commit to dweindl/libpetab-python that referenced this pull request Nov 17, 2026
Every pull request added its changelog entry at the same place in
`CHANGELOG.md`, so concurrent pull requests conflicted with each other.

Now, each pull request adds its entry as a separate file
`changelog.d/+<name>.<type>.md` (see `changelog.d/README.md`). At release
time, `towncrier build --version X.Y.Z` collects them into `CHANGELOG.md`.

The unreleased 0.11.0 entry is moved to `changelog.d/`, and entries are
added for PEtab-dev#583, PEtab-dev#584, PEtab-dev#588, PEtab-dev#590, PEtab-dev#592 and PEtab-dev#595, which did not add one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dweindl added a commit to dweindl/libpetab-python that referenced this pull request Nov 17, 2026
…v#600)

Every pull request added its changelog entry at the same place in
`CHANGELOG.md`, so concurrent pull requests conflicted with each other.

Now, each pull request adds its changelog entry as a separate file
`changelog.d/+<name>.<type>.md` (see `changelog.d/README.md`). At
release time, `towncrier build --version X.Y.Z` collects them into
`CHANGELOG.md` and removes the fragments. The template in
`changelog.d/template.md.j2` reproduces the existing format (`**Fixes**`
etc., `* ` bullets with 2-space continuation lines). Entries are sorted
alphabetically within each category; grouping them (e.g., by PEtab
version or module) is done when compiling the release notes. The
configuration is in `pyproject.toml`, and towncrier is added to the
`test` dependency group. The release process in `doc/development.rst` is
updated accordingly.

Entries now link to the pull request only, without "by @user";
contributors can be credited in the release notes.

The unreleased 0.11.0 entry (PEtab-dev#578) is moved to `changelog.d/`, and
entries are added for PEtab-dev#583, PEtab-dev#584, PEtab-dev#588, PEtab-dev#590, PEtab-dev#592 and PEtab-dev#595, which did
not add one. The behaviour changes from PEtab-dev#584 (top-level Boolean
expressions are returned as `Piecewise`) and PEtab-dev#592 (stricter
`PySBModel.pattern_from_string`) are listed as breaking changes.

Open pull requests that edit `CHANGELOG.md` will need to move their
entry to a fragment file.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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

3 participants