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 ():
@@ -28,6 +28,19 @@ def test_evaluate():
2828 act = sympify_petab ("piecewise(1, 1 > 2, 0)" , evaluate = False )
2929 assert str (act ) == "Piecewise((1.0, 1.0 > 2.0), (0.0, True))"
3030
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+
3144
3245def read_cases ():
3346 """Read test cases from YAML file in the petab_test_suite package."""
@@ -60,29 +73,35 @@ def read_cases():
6073@pytest .mark .parametrize ("expr_str, expected" , read_cases ())
6174def test_parse_cases (expr_str , expected ):
6275 """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
6679 else :
6780 try :
68- result = float (result .evalf ())
81+ result = float (sym_expr .evalf ())
6982 assert np .isclose (result , expected ), (
7083 f"{ expr_str } : Expected { expected } , got { result } "
7184 )
7285 except TypeError :
73- assert result == expected , (
86+ assert sym_expr == expected , (
7487 f"{ expr_str } : Expected { expected } , got { result } "
7588 )
7689
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+
7797
7898def test_ids ():
7999 """Test symbols in expressions."""
80100 assert sympify_petab ("bla * 2" ) == 2.0 * sp .Symbol ("bla" , real = True )
81101
82102 # 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öö" ))
86105
87106
88107def test_syntax_error ():
0 commit comments