Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
* `petab.v2.Problem.get_unused_id` returns an identifier that is not yet
used in the problem or its model
(by @dweindl in https://github.com/PEtab-dev/libpetab-python/pull/561)
* `petab.v1.math.petab_symbol` (also in `petab.v2.math`) creates sympy
symbols with the same assumptions as `sympify_petab`
(by @dweindl in https://github.com/PEtab-dev/libpetab-python/pull/563)

## 0.9 series

Expand Down
10 changes: 4 additions & 6 deletions petab/v1/calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import petab.v1 as petab

from .C import *
from .math import sympify_petab
from .math import petab_symbol, sympify_petab

__all__ = [
"calculate_chi2",
Expand Down Expand Up @@ -202,18 +202,16 @@ def evaluate_noise_formula(
)
# fill in measurement specific parameters
overrides = {
sp.Symbol(
f"noiseParameter{i_obs_par + 1}_{observable_id}", real=True
): obs_par
petab_symbol(f"noiseParameter{i_obs_par + 1}_{observable_id}"): obs_par
for i_obs_par, obs_par in enumerate(observable_parameter_overrides)
}

# fill in observables
overrides[sp.Symbol(observable_id, real=True)] = simulation
overrides[petab_symbol(observable_id)] = simulation

# fill in general parameters
for row in parameter_df.itertuples():
overrides[sp.Symbol(row.Index, real=True)] = row.nominalValue
overrides[petab_symbol(row.Index)] = row.nominalValue

# replace parametric measurement specific parameters
for key, value in overrides.items():
Expand Down
16 changes: 14 additions & 2 deletions petab/v1/math/SympyVisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ._generated.PetabMathExprParser import PetabMathExprParser
from ._generated.PetabMathExprParserVisitor import PetabMathExprParserVisitor

__all__ = ["MathVisitorSympy"]
__all__ = ["MathVisitorSympy", "petab_symbol"]

# Mappings of PEtab math functions to sympy functions

Expand Down Expand Up @@ -65,6 +65,18 @@
}


def petab_symbol(name: str) -> sp.Symbol:
"""Create a sympy symbol for a PEtab identifier.

The symbol has the same assumptions as the symbols created by
:func:`sympify_petab`.

:param name: The identifier.
:return: The symbol.
"""
return sp.Symbol(name, real=True)


class MathVisitorSympy(PetabMathExprParserVisitor):
"""
ANTLR4 visitor for PEtab-math-to-sympy conversion.
Expand Down Expand Up @@ -101,7 +113,7 @@ def visitVar(self, ctx: PetabMathExprParser.VarContext) -> sp.Symbol:
"""Convert identifier to sympy Symbol."""
if ctx.getText().lower() in _reserved_names:
raise ValueError(f"Use of reserved name {ctx.getText()!r}")
return sp.Symbol(ctx.getText(), real=True)
return petab_symbol(ctx.getText())

def visitMultExpr(
self, ctx: PetabMathExprParser.MultExprContext
Expand Down
1 change: 1 addition & 0 deletions petab/v1/math/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

from .printer import PetabStrPrinter, petab_math_str # noqa: F401
from .sympify import sympify_petab # noqa: F401
from .SympyVisitor import petab_symbol # noqa: F401
2 changes: 1 addition & 1 deletion petab/v1/math/sympify.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def sympify_petab(
.. note::

All symbols in the returned expression will have the ``real=True``
assumption.
assumption, as created by :func:`petab_symbol`.

:example:
>>> from petab.v1.math import sympify_petab
Expand Down
8 changes: 4 additions & 4 deletions petab/v2/calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from petab.v1 import is_empty, split_parameter_replacement_list

from .C import *
from .math import sympify_petab
from .math import petab_symbol, sympify_petab

__all__ = [
"calculate_chi2",
Expand Down Expand Up @@ -226,7 +226,7 @@ def evaluate_noise_formula(

# fill in measurement specific parameters
overrides = {
sp.Symbol(placeholder, real=True): override
petab_symbol(placeholder): override
for placeholder, override in zip(
[
p.strip()
Expand All @@ -240,11 +240,11 @@ def evaluate_noise_formula(
}

# fill in observables
overrides[sp.Symbol(observable_id, real=True)] = simulation
overrides[petab_symbol(observable_id)] = simulation

# fill in general parameters
for row in parameter_df.itertuples():
overrides[sp.Symbol(row.Index, real=True)] = row.nominalValue
overrides[petab_symbol(row.Index)] = row.nominalValue

# replace parametric measurement specific parameters
for key, value in overrides.items():
Expand Down
6 changes: 5 additions & 1 deletion tests/v1/math/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sympy.abc import _clash
from sympy.logic.boolalg import Boolean, BooleanFalse, BooleanTrue

from petab.v1.math import petab_math_str, sympify_petab
from petab.v1.math import petab_math_str, petab_symbol, sympify_petab


def test_sympify_numpy():
Expand Down Expand Up @@ -36,6 +36,10 @@ def test_assumptions():
# non-real symbols are changed to real
assert sympify_petab(sp.Symbol("x", real=False)).is_real

# `petab_symbol` creates the same symbols as the parser
assert petab_symbol("x") == sympify_petab("x")
assert petab_symbol("x") != sp.Symbol("x")


def test_printer():
a, b, c, d = sp.symbols("a b c d", real=True)
Expand Down
Loading