You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Non-constant numbers in Boolean context are parsed to constants #581
sympify_petab gets the result wrong when a non-constant numeric expression is used as a Boolean, i.e. as an operand of &&, ||, !, or as a piecewise condition. The PEtab v2 spec ("Boolean ↔ float conversion") says 0.0 is converted to false and any other value to true. Instead, the operand is always treated as true. This happens with both evaluate=True and evaluate=False:
frompetab.v1.mathimportsympify_petabsympify_petab("(x + 1) && (y > 2)") # y > 2.0 (expected: (x + 1 != 0) && (y > 2))sympify_petab("!(x + 1)") # 0.0 (expected: x + 1 == 0)sympify_petab("piecewise(1, x + 1, 0)") # 1.0 (expected: 1 if x + 1 != 0 else 0)sympify_petab("piecewise(1, x > 0, 0) && true") # 1.0 (expected: x > 0)
The cause is the fallback in num2bool (petab/v1/math/SympyVisitor.py), sp.Piecewise((True, x != 0.0), (False, True)). There, x != 0.0 is Python's structural inequality, not sp.Ne, so it evaluates to True for any symbolic x.
In the same function, a bare symbol is passed through unchanged, because sp.Symbol is a subclass of sp.logic.boolalg.Boolean. As a result, x && true is parsed to x instead of x != 0, and x && y is parsed to And(x, y), which fails once numbers are substituted. Boolean-valued piecewise operands such as !piecewise(true, x > 0, false) are not handled correctly either.
petab.v2.math re-exports petab.v1.math, so both versions are affected.
Activity