diff --git a/changelog.d/+reject-undefined-math.fix.md b/changelog.d/+reject-undefined-math.fix.md new file mode 100644 index 00000000..e24a8141 --- /dev/null +++ b/changelog.d/+reject-undefined-math.fix.md @@ -0,0 +1,4 @@ +Reject NaN and complex infinity when printing PEtab mathematical expressions, +including nested expressions, instead of writing expressions that cannot be +read back. Positive and negative real infinity remain supported +([#613](https://github.com/PEtab-dev/libpetab-python/pull/613)). diff --git a/petab/v1/math/printer.py b/petab/v1/math/printer.py index b6973ebe..ed7abffc 100644 --- a/petab/v1/math/printer.py +++ b/petab/v1/math/printer.py @@ -110,6 +110,16 @@ def _print_NegativeInfinity(self, expr): """Custom printing for negative infinity""" return "-inf" + def _print_NaN(self, expr: sp.Basic) -> typing.NoReturn: + """Reject undefined values instead of emitting invalid PEtab math.""" + raise ValueError("NaN cannot be represented in PEtab.") + + def _print_ComplexInfinity(self, expr: sp.Basic) -> typing.NoReturn: + """Reject complex infinity, which has no PEtab representation.""" + raise ValueError( + "Complex infinity (zoo) cannot be represented in PEtab." + ) + def _print_Function(self, expr): """Custom printing for specific functions""" @@ -153,6 +163,10 @@ def _print_Max(self, expr): def petab_math_str(expr: sp.Basic | sp.Expr | None) -> str: """Convert a sympy expression to a PEtab-compatible math expression string. + Raises: + ValueError: If the expression contains NaN or complex infinity, or + a piecewise expression without a default value. + :example: >>> expr = sp.sympify("x**2 + sin(y)") >>> petab_math_str(expr) diff --git a/tests/v1/math/test_math.py b/tests/v1/math/test_math.py index 03c04ac5..cf775e7f 100644 --- a/tests/v1/math/test_math.py +++ b/tests/v1/math/test_math.py @@ -8,7 +8,12 @@ from sympy.abc import _clash from sympy.logic.boolalg import Boolean, BooleanFalse, BooleanTrue -from petab.v1.math import petab_math_str, petab_symbol, sympify_petab +from petab.v1.math import ( + PetabStrPrinter, + petab_math_str, + petab_symbol, + sympify_petab, +) from petab.v1.math.SympyVisitor import num2bool @@ -16,6 +21,32 @@ def test_sympify_numpy(): assert sympify_petab(np.float64(1.0)) == sp.Float(1.0) +@pytest.mark.parametrize( + "printer", [petab_math_str, PetabStrPrinter().doprint] +) +@pytest.mark.parametrize( + "expr", + [ + sp.nan, + sp.zoo, + sympify_petab("x / 0"), + sp.Add(petab_symbol("x"), sp.nan, evaluate=False), + sp.Piecewise((sp.nan, petab_symbol("x") > 0), (0, True)), + sp.Piecewise((1, petab_symbol("x") > 0), (sp.zoo, True)), + ], +) +def test_printer_rejects_undefined_values(printer, expr): + """Undefined values must not be exported as unreadable PEtab math.""" + with pytest.raises(ValueError, match="cannot be represented in PEtab"): + printer(expr) + + +@pytest.mark.parametrize("expr", [sp.oo, -sp.oo]) +def test_printer_infinity_roundtrip(expr): + """Real infinities remain supported, unlike complex infinity.""" + assert sympify_petab(petab_math_str(expr)) == expr + + def test_parse_simple(): """Test simple numeric expressions.""" assert float(sympify_petab("1 + 2")) == 3