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 ():
@@ -24,6 +24,21 @@ 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+
36+ def test_printer ():
37+ assert petab_math_str (None ) == ""
38+ assert petab_math_str (BooleanTrue ()) == "true"
39+ assert petab_math_str (BooleanFalse ()) == "false"
40+
41+
2742def read_cases ():
2843 """Read test cases from YAML file in the petab_test_suite package."""
2944 yaml_file = importlib .resources .files ("petabtests.cases" ).joinpath (
@@ -55,29 +70,35 @@ def read_cases():
5570@pytest .mark .parametrize ("expr_str, expected" , read_cases ())
5671def test_parse_cases (expr_str , expected ):
5772 """Test PEtab math expressions for the PEtab test suite."""
58- result = sympify_petab (expr_str )
59- if isinstance (result , Boolean ):
60- assert result == expected
73+ sym_expr = sympify_petab (expr_str )
74+ if isinstance (sym_expr , Boolean ):
75+ assert sym_expr == expected
6176 else :
6277 try :
63- result = float (result .evalf ())
78+ result = float (sym_expr .evalf ())
6479 assert np .isclose (result , expected ), (
6580 f"{ expr_str } : Expected { expected } , got { result } "
6681 )
6782 except TypeError :
68- assert result == expected , (
83+ assert sym_expr == expected , (
6984 f"{ expr_str } : Expected { expected } , got { result } "
7085 )
7186
87+ # test parsing, printing, and parsing again
88+ resympified = sympify_petab (petab_math_str (sym_expr ))
89+ if sym_expr .is_number :
90+ assert np .isclose (float (resympified ), float (sym_expr ))
91+ else :
92+ assert resympified .equals (sym_expr ), (sym_expr , resympified )
93+
7294
7395def test_ids ():
7496 """Test symbols in expressions."""
7597 assert sympify_petab ("bla * 2" ) == 2.0 * sp .Symbol ("bla" , real = True )
7698
7799 # test that sympy expressions that are invalid in PEtab raise an error
78- # TODO: handle these cases after
79- # https://github.com/PEtab-dev/libpetab-python/pull/364
80- # sympify_petab(sp.Symbol("föö"))
100+ with pytest .raises (ValueError ):
101+ sympify_petab (sp .Symbol ("föö" ))
81102
82103
83104def test_syntax_error ():
0 commit comments