6
6
import sympy as sp
7
7
import yaml
8
8
from sympy .abc import _clash
9
- from sympy .logic .boolalg import Boolean
9
+ from sympy .logic .boolalg import Boolean , BooleanFalse , BooleanTrue
10
10
11
- from petab .math import sympify_petab
11
+ from petab .v1 . math import petab_math_str , sympify_petab
12
12
13
13
14
14
def test_sympify_numpy ():
@@ -28,6 +28,19 @@ def test_evaluate():
28
28
act = sympify_petab ("piecewise(1, 1 > 2, 0)" , evaluate = False )
29
29
assert str (act ) == "Piecewise((1.0, 1.0 > 2.0), (0.0, True))"
30
30
31
+ def test_assumptions ():
32
+ # in PEtab, all symbols are expected to be real-valued
33
+ assert sympify_petab ("x" ).is_real
34
+
35
+ # non-real symbols are changed to real
36
+ assert sympify_petab (sp .Symbol ("x" , real = False )).is_real
37
+
38
+
39
+ def test_printer ():
40
+ assert petab_math_str (None ) == ""
41
+ assert petab_math_str (BooleanTrue ()) == "true"
42
+ assert petab_math_str (BooleanFalse ()) == "false"
43
+
31
44
32
45
def read_cases ():
33
46
"""Read test cases from YAML file in the petab_test_suite package."""
@@ -60,29 +73,35 @@ def read_cases():
60
73
@pytest .mark .parametrize ("expr_str, expected" , read_cases ())
61
74
def test_parse_cases (expr_str , expected ):
62
75
"""Test PEtab math expressions for the PEtab test suite."""
63
- result = sympify_petab (expr_str )
64
- if isinstance (result , Boolean ):
65
- assert result == expected
76
+ sym_expr = sympify_petab (expr_str )
77
+ if isinstance (sym_expr , Boolean ):
78
+ assert sym_expr == expected
66
79
else :
67
80
try :
68
- result = float (result .evalf ())
81
+ result = float (sym_expr .evalf ())
69
82
assert np .isclose (result , expected ), (
70
83
f"{ expr_str } : Expected { expected } , got { result } "
71
84
)
72
85
except TypeError :
73
- assert result == expected , (
86
+ assert sym_expr == expected , (
74
87
f"{ expr_str } : Expected { expected } , got { result } "
75
88
)
76
89
90
+ # test parsing, printing, and parsing again
91
+ resympified = sympify_petab (petab_math_str (sym_expr ))
92
+ if sym_expr .is_number :
93
+ assert np .isclose (float (resympified ), float (sym_expr ))
94
+ else :
95
+ assert resympified .equals (sym_expr ), (sym_expr , resympified )
96
+
77
97
78
98
def test_ids ():
79
99
"""Test symbols in expressions."""
80
100
assert sympify_petab ("bla * 2" ) == 2.0 * sp .Symbol ("bla" , real = True )
81
101
82
102
# test that sympy expressions that are invalid in PEtab raise an error
83
- # TODO: handle these cases after
84
- # https://github.com/PEtab-dev/libpetab-python/pull/364
85
- # sympify_petab(sp.Symbol("föö"))
103
+ with pytest .raises (ValueError ):
104
+ sympify_petab (sp .Symbol ("föö" ))
86
105
87
106
88
107
def test_syntax_error ():
0 commit comments