66import sympy as sp
77import yaml
88from sympy .abc import _clash
9- from sympy .logic .boolalg import Boolean
9+ from sympy .logic .boolalg import Boolean , BooleanFalse , BooleanTrue
1010
11- from petab .math import sympify_petab
11+ from petab .v1 . math import petab_math_str , sympify_petab
1212
1313
1414def test_sympify_numpy ():
@@ -29,6 +29,20 @@ def test_evaluate():
2929 assert str (act ) == "Piecewise((1.0, 1.0 > 2.0), (0.0, True))"
3030
3131
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+
3246def read_cases ():
3347 """Read test cases from YAML file in the petab_test_suite package."""
3448 yaml_file = importlib .resources .files ("petabtests.cases" ).joinpath (
@@ -60,29 +74,35 @@ def read_cases():
6074@pytest .mark .parametrize ("expr_str, expected" , read_cases ())
6175def test_parse_cases (expr_str , expected ):
6276 """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
6680 else :
6781 try :
68- result = float (result .evalf ())
82+ result = float (sym_expr .evalf ())
6983 assert np .isclose (result , expected ), (
7084 f"{ expr_str } : Expected { expected } , got { result } "
7185 )
7286 except TypeError :
73- assert result == expected , (
87+ assert sym_expr == expected , (
7488 f"{ expr_str } : Expected { expected } , got { result } "
7589 )
7690
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+
7798
7899def test_ids ():
79100 """Test symbols in expressions."""
80101 assert sympify_petab ("bla * 2" ) == 2.0 * sp .Symbol ("bla" , real = True )
81102
82103 # 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öö" ))
86106
87107
88108def test_syntax_error ():
0 commit comments