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 ():
@@ -29,6 +29,20 @@ def test_evaluate():
29
29
assert str (act ) == "Piecewise((1.0, 1.0 > 2.0), (0.0, True))"
30
30
31
31
32
+ def test_assumptions ():
33
+ # in PEtab, all symbols are expected to be real-valued
34
+ assert sympify_petab ("x" ).is_real
35
+
36
+ # non-real symbols are changed to real
37
+ assert sympify_petab (sp .Symbol ("x" , real = False )).is_real
38
+
39
+
40
+ def test_printer ():
41
+ assert petab_math_str (None ) == ""
42
+ assert petab_math_str (BooleanTrue ()) == "true"
43
+ assert petab_math_str (BooleanFalse ()) == "false"
44
+
45
+
32
46
def read_cases ():
33
47
"""Read test cases from YAML file in the petab_test_suite package."""
34
48
yaml_file = importlib .resources .files ("petabtests.cases" ).joinpath (
@@ -60,29 +74,35 @@ def read_cases():
60
74
@pytest .mark .parametrize ("expr_str, expected" , read_cases ())
61
75
def test_parse_cases (expr_str , expected ):
62
76
"""Test PEtab math expressions for the PEtab test suite."""
63
- result = sympify_petab (expr_str )
64
- if isinstance (result , Boolean ):
65
- assert result == expected
77
+ sym_expr = sympify_petab (expr_str )
78
+ if isinstance (sym_expr , Boolean ):
79
+ assert sym_expr == expected
66
80
else :
67
81
try :
68
- result = float (result .evalf ())
82
+ result = float (sym_expr .evalf ())
69
83
assert np .isclose (result , expected ), (
70
84
f"{ expr_str } : Expected { expected } , got { result } "
71
85
)
72
86
except TypeError :
73
- assert result == expected , (
87
+ assert sym_expr == expected , (
74
88
f"{ expr_str } : Expected { expected } , got { result } "
75
89
)
76
90
91
+ # test parsing, printing, and parsing again
92
+ resympified = sympify_petab (petab_math_str (sym_expr ))
93
+ if sym_expr .is_number :
94
+ assert np .isclose (float (resympified ), float (sym_expr ))
95
+ else :
96
+ assert resympified .equals (sym_expr ), (sym_expr , resympified )
97
+
77
98
78
99
def test_ids ():
79
100
"""Test symbols in expressions."""
80
101
assert sympify_petab ("bla * 2" ) == 2.0 * sp .Symbol ("bla" , real = True )
81
102
82
103
# 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öö"))
104
+ with pytest .raises (ValueError ):
105
+ sympify_petab (sp .Symbol ("föö" ))
86
106
87
107
88
108
def test_syntax_error ():
0 commit comments