Skip to content

Commit d4dcd9d

Browse files
authored
Handle numpy types in sympify_petab (#294)
1 parent 0bb4e11 commit d4dcd9d

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

petab/v1/math/sympify.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""PEtab math to sympy conversion."""
22

3+
import numpy as np
34
import sympy as sp
45
from antlr4 import CommonTokenStream, InputStream
56
from antlr4.error.ErrorListener import ErrorListener
@@ -25,9 +26,9 @@ def sympify_petab(expr: str | int | float) -> sp.Expr | sp.Basic:
2526
The sympy expression corresponding to `expr`.
2627
Boolean values are converted to numeric values.
2728
"""
28-
if isinstance(expr, int):
29+
if isinstance(expr, int) or isinstance(expr, np.integer):
2930
return sp.Integer(expr)
30-
if isinstance(expr, float):
31+
if isinstance(expr, float) or isinstance(expr, np.floating):
3132
return sp.Float(expr)
3233

3334
# Set error listeners

tests/v1/math/test_math.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
from petab.math import sympify_petab
1212

1313

14+
def test_sympify_numpy():
15+
assert sympify_petab(np.float64(1.0)) == sp.Float(1.0)
16+
17+
1418
def test_parse_simple():
1519
"""Test simple numeric expressions."""
1620
assert float(sympify_petab("1 + 2")) == 3

0 commit comments

Comments
 (0)