Skip to content

Commit 22cbd2b

Browse files
committed
..
1 parent 6f7296a commit 22cbd2b

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

petab/v1/math/sympify.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from antlr4 import CommonTokenStream, InputStream
66
from antlr4.error.ErrorListener import ErrorListener
77

8+
from . import petab_math_str
89
from ._generated.PetabMathExprLexer import PetabMathExprLexer
910
from ._generated.PetabMathExprParser import PetabMathExprParser
1011
from .SympyVisitor import MathVisitorSympy, bool2num
@@ -32,8 +33,7 @@ def sympify_petab(expr: str | int | float) -> sp.Expr | sp.Basic:
3233
Boolean values are converted to numeric values.
3334
"""
3435
if isinstance(expr, sp.Expr):
35-
# TODO: check if only PEtab-compatible symbols and functions are used
36-
return expr
36+
return sympify_petab(petab_math_str(expr))
3737

3838
if isinstance(expr, int) or isinstance(expr, np.integer):
3939
return sp.Integer(expr)

petab/v2/core.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from typing_extensions import Self
2626

2727
from ..v1.lint import is_valid_identifier
28-
from ..v1.math import sympify_petab
28+
from ..v1.math import petab_math_str, sympify_petab
2929
from . import C, get_observable_df
3030

3131
__all__ = [
@@ -273,23 +273,8 @@ def to_df(self) -> pd.DataFrame:
273273
for record in records:
274274
obs = record[C.OBSERVABLE_FORMULA]
275275
noise = record[C.NOISE_FORMULA]
276-
record[C.OBSERVABLE_FORMULA] = (
277-
None
278-
if obs is None
279-
# TODO: we need a custom printer for sympy expressions
280-
# to avoid '**'
281-
# https://github.com/PEtab-dev/libpetab-python/issues/362
282-
else str(obs)
283-
if not obs.is_number
284-
else float(obs)
285-
)
286-
record[C.NOISE_FORMULA] = (
287-
None
288-
if noise is None
289-
else str(noise)
290-
if not noise.is_number
291-
else float(noise)
292-
)
276+
record[C.OBSERVABLE_FORMULA] = petab_math_str(obs)
277+
record[C.NOISE_FORMULA] = petab_math_str(noise)
293278
return pd.DataFrame(records).set_index([C.OBSERVABLE_ID])
294279

295280
@classmethod

tests/v1/math/test_math.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from sympy.abc import _clash
99
from sympy.logic.boolalg import Boolean, BooleanFalse, BooleanTrue
1010

11-
from petab.math import petab_math_str, sympify_petab
11+
from petab.v1.math import petab_math_str, sympify_petab
1212

1313

1414
def test_sympify_numpy():
@@ -24,6 +24,15 @@ def test_parse_simple():
2424
assert float(sympify_petab("1 + 2 * (3 + 4) / 2")) == 8
2525

2626

27+
def test_assumptions():
28+
# in PEtab, all symbols are expected to be real-valued
29+
assert sympify_petab("x").is_real
30+
31+
# non-real symbols are changed to real
32+
# TODO: should we raise an error instead?
33+
assert sympify_petab(sp.Symbol("x", real=False)).is_real
34+
35+
2736
def test_printer():
2837
assert petab_math_str(BooleanTrue()) == "true"
2938
assert petab_math_str(BooleanFalse()) == "false"
@@ -87,9 +96,8 @@ def test_ids():
8796
assert sympify_petab("bla * 2") == 2.0 * sp.Symbol("bla", real=True)
8897

8998
# test that sympy expressions that are invalid in PEtab raise an error
90-
# TODO: handle these cases after
91-
# https://github.com/PEtab-dev/libpetab-python/pull/364
92-
# sympify_petab(sp.Symbol("föö"))
99+
with pytest.raises(ValueError):
100+
sympify_petab(sp.Symbol("föö"))
93101

94102

95103
def test_syntax_error():

0 commit comments

Comments
 (0)