sbmlmath 0.3.0__tar.gz → 0.4.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/.github/workflows/ci.yaml +6 -6
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/.github/workflows/deploy_release.yaml +2 -2
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/.pre-commit-config.yaml +1 -1
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/CITATION.cff +1 -3
- {sbmlmath-0.3.0/sbmlmath.egg-info → sbmlmath-0.4.1}/PKG-INFO +8 -5
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/README.md +4 -2
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/doc/about.rst +7 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/pyproject.toml +4 -1
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/sbmlmath/__init__.py +8 -5
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/sbmlmath/csymbol.py +13 -1
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/sbmlmath/mathml_parser.py +133 -53
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/sbmlmath/mathml_printer.py +11 -2
- {sbmlmath-0.3.0 → sbmlmath-0.4.1/sbmlmath.egg-info}/PKG-INFO +8 -5
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/tests/test_csymbol.py +6 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/tests/test_mathml_parser_sbml_semantic_suite.py +1 -1
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/tests/test_misc.py +32 -2
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/tests/test_parser.py +1 -1
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/tests/test_printer.py +20 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/.github/dependabot.yml +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/.gitignore +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/.readthedocs.yml +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/LICENSE +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/doc/.gitignore +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/doc/Makefile +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/doc/api.rst +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/doc/conf.py +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/doc/index.rst +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/doc/make.bat +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/doc/rtd_requirements.txt +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/sbmlmath/cfunction.py +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/sbmlmath/species_symbol.py +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/sbmlmath.egg-info/SOURCES.txt +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/sbmlmath.egg-info/dependency_links.txt +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/sbmlmath.egg-info/requires.txt +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/sbmlmath.egg-info/top_level.txt +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/setup.cfg +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.1}/tests/test_species_symbol.py +0 -0
|
@@ -16,10 +16,10 @@ jobs:
|
|
|
16
16
|
|
|
17
17
|
steps:
|
|
18
18
|
- name: Check out repository
|
|
19
|
-
uses: actions/checkout@
|
|
19
|
+
uses: actions/checkout@v5
|
|
20
20
|
|
|
21
21
|
- name: Set up Python ${{ matrix.python-version }}
|
|
22
|
-
uses: actions/setup-python@
|
|
22
|
+
uses: actions/setup-python@v6
|
|
23
23
|
with:
|
|
24
24
|
python-version: ${{ matrix.python-version }}
|
|
25
25
|
|
|
@@ -63,10 +63,10 @@ jobs:
|
|
|
63
63
|
|
|
64
64
|
steps:
|
|
65
65
|
- name: Check out repository
|
|
66
|
-
uses: actions/checkout@
|
|
66
|
+
uses: actions/checkout@v5
|
|
67
67
|
|
|
68
68
|
- name: Set up Python ${{ matrix.python-version }}
|
|
69
|
-
uses: actions/setup-python@
|
|
69
|
+
uses: actions/setup-python@v6
|
|
70
70
|
with:
|
|
71
71
|
python-version: ${{ matrix.python-version }}
|
|
72
72
|
|
|
@@ -107,10 +107,10 @@ jobs:
|
|
|
107
107
|
|
|
108
108
|
steps:
|
|
109
109
|
- name: Check out repository
|
|
110
|
-
uses: actions/checkout@
|
|
110
|
+
uses: actions/checkout@v5
|
|
111
111
|
|
|
112
112
|
- name: Set up Python ${{ matrix.python-version }}
|
|
113
|
-
uses: actions/setup-python@
|
|
113
|
+
uses: actions/setup-python@v6
|
|
114
114
|
with:
|
|
115
115
|
python-version: ${{ matrix.python-version }}
|
|
116
116
|
|
|
@@ -3,9 +3,6 @@
|
|
|
3
3
|
|
|
4
4
|
cff-version: 1.2.0
|
|
5
5
|
title: sbmlmath
|
|
6
|
-
message: >-
|
|
7
|
-
If you use this software, please cite it using the
|
|
8
|
-
metadata from this file.
|
|
9
6
|
type: software
|
|
10
7
|
authors:
|
|
11
8
|
- given-names: Daniel
|
|
@@ -23,3 +20,4 @@ keywords:
|
|
|
23
20
|
- math expressions
|
|
24
21
|
- Python
|
|
25
22
|
license: BSD-3-Clause
|
|
23
|
+
doi: 10.5281/zenodo.14814669
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: sbmlmath
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.1
|
|
4
4
|
Summary: SBML Math <-> SymPy
|
|
5
5
|
Author-email: Daniel Weindl <sci@danielweindl.de>
|
|
6
|
-
License: BSD-3-Clause
|
|
6
|
+
License-Expression: BSD-3-Clause
|
|
7
7
|
Project-URL: Homepage, https://github.com/dweindl/sbmlmath
|
|
8
8
|
Project-URL: Documentation, https://sbmlmath.readthedocs.io/
|
|
9
9
|
Project-URL: Repository, https://github.com/dweindl/sbmlmath.git
|
|
@@ -19,10 +19,13 @@ Requires-Dist: lxml>=4.6.4
|
|
|
19
19
|
Provides-Extra: test
|
|
20
20
|
Requires-Dist: pytest>=7; extra == "test"
|
|
21
21
|
Requires-Dist: pre-commit>=3; extra == "test"
|
|
22
|
+
Dynamic: license-file
|
|
22
23
|
|
|
23
24
|
# sbmlmath — a Python library for handling SBML MathML
|
|
24
25
|
|
|
25
26
|
[](https://pypi.org/project/sbmlmath/)
|
|
27
|
+
[](https://doi.org/10.5281/zenodo.14814669)
|
|
28
|
+
|
|
26
29
|
|
|
27
30
|
`sbmlmath` is a Python library for interconverting [SymPy](https://github.com/sympy/sympy/)
|
|
28
31
|
expressions and [SBML](https://sbml.org/)-MathML.
|
|
@@ -35,7 +38,7 @@ Main functionality:
|
|
|
35
38
|
* SBML MathML -> sympy
|
|
36
39
|
* in particular for cases where `sympy.sympify(libsbml.formulaToL3String(...))`
|
|
37
40
|
won't do the job
|
|
38
|
-
(mind the differences operator precedence between
|
|
41
|
+
(mind, e.g., the differences in operator precedence between
|
|
39
42
|
sympy/[Python](https://docs.python.org/3/reference/expressions.html#operator-precedence)
|
|
40
43
|
and [libsbml L3 formulas](https://synonym.caltech.edu/software/libsbml/5.18.0/docs/formatted/python-api/libsbml-math.html#math-l3)!)
|
|
41
44
|
* retaining unit annotations and other `<ci>` attributes
|
|
@@ -72,5 +75,5 @@ pip install sbmlmath
|
|
|
72
75
|
|
|
73
76
|
The latest development version from GitHub:
|
|
74
77
|
```bash
|
|
75
|
-
pip install https://github.com/dweindl/sbmlmath
|
|
78
|
+
pip install git+https://github.com/dweindl/sbmlmath.git#egg=sbmlmath
|
|
76
79
|
```
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# sbmlmath — a Python library for handling SBML MathML
|
|
2
2
|
|
|
3
3
|
[](https://pypi.org/project/sbmlmath/)
|
|
4
|
+
[](https://doi.org/10.5281/zenodo.14814669)
|
|
5
|
+
|
|
4
6
|
|
|
5
7
|
`sbmlmath` is a Python library for interconverting [SymPy](https://github.com/sympy/sympy/)
|
|
6
8
|
expressions and [SBML](https://sbml.org/)-MathML.
|
|
@@ -13,7 +15,7 @@ Main functionality:
|
|
|
13
15
|
* SBML MathML -> sympy
|
|
14
16
|
* in particular for cases where `sympy.sympify(libsbml.formulaToL3String(...))`
|
|
15
17
|
won't do the job
|
|
16
|
-
(mind the differences operator precedence between
|
|
18
|
+
(mind, e.g., the differences in operator precedence between
|
|
17
19
|
sympy/[Python](https://docs.python.org/3/reference/expressions.html#operator-precedence)
|
|
18
20
|
and [libsbml L3 formulas](https://synonym.caltech.edu/software/libsbml/5.18.0/docs/formatted/python-api/libsbml-math.html#math-l3)!)
|
|
19
21
|
* retaining unit annotations and other `<ci>` attributes
|
|
@@ -50,5 +52,5 @@ pip install sbmlmath
|
|
|
50
52
|
|
|
51
53
|
The latest development version from GitHub:
|
|
52
54
|
```bash
|
|
53
|
-
pip install https://github.com/dweindl/sbmlmath
|
|
55
|
+
pip install git+https://github.com/dweindl/sbmlmath.git#egg=sbmlmath
|
|
54
56
|
```
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
sbmlmath — a Python library for handling SBML MathML
|
|
2
2
|
====================================================
|
|
3
3
|
|
|
4
|
+
.. image:: https://img.shields.io/pypi/v/sbmlmath
|
|
5
|
+
:target: https://pypi.org/project/sbmlmath/
|
|
6
|
+
:alt: PyPI
|
|
7
|
+
.. image:: https://zenodo.org/badge/617871948.svg
|
|
8
|
+
:target: https://doi.org/10.5281/zenodo.14814669
|
|
9
|
+
:alt: DOI
|
|
10
|
+
|
|
4
11
|
This is a Python library for interconverting `SymPy <https://github.com/sympy/sympy/>`_
|
|
5
12
|
expressions and `SBML <https://sbml.org/>`_-MathML.
|
|
6
13
|
SBML uses a subset of `MathML <https://www.w3.org/Math/>`_ that this library
|
|
@@ -11,7 +11,7 @@ authors = [
|
|
|
11
11
|
description = "SBML Math <-> SymPy"
|
|
12
12
|
readme = "README.md"
|
|
13
13
|
requires-python = ">=3.9"
|
|
14
|
-
license =
|
|
14
|
+
license = "BSD-3-Clause"
|
|
15
15
|
dependencies = [
|
|
16
16
|
"python-libsbml>=5.20.4",
|
|
17
17
|
"sympy",
|
|
@@ -29,6 +29,9 @@ PyPI = "https://pypi.org/project/sbmlmath/"
|
|
|
29
29
|
[project.optional-dependencies]
|
|
30
30
|
test = ["pytest>=7", "pre-commit>=3"]
|
|
31
31
|
|
|
32
|
+
[tool.setuptools]
|
|
33
|
+
packages = ["sbmlmath"]
|
|
34
|
+
|
|
32
35
|
[tool.setuptools_scm]
|
|
33
36
|
|
|
34
37
|
[tool.ruff]
|
|
@@ -38,12 +38,12 @@ def sympy_to_sbml_math(sp_expr: sp.Expr) -> libsbml.ASTNode:
|
|
|
38
38
|
return ast_node
|
|
39
39
|
|
|
40
40
|
raise ValueError(
|
|
41
|
-
f"Unknown error handling math expression:\n{sp_expr}\n
|
|
41
|
+
f"Unknown error handling math expression:\n{sp_expr}\n{mathml}"
|
|
42
42
|
)
|
|
43
43
|
|
|
44
44
|
|
|
45
45
|
def sbml_math_to_sympy(
|
|
46
|
-
sbml_obj: Union[libsbml.SBase, libsbml.ASTNode],
|
|
46
|
+
sbml_obj: Union[libsbml.SBase, libsbml.ASTNode], **kwargs
|
|
47
47
|
) -> sp.Expr:
|
|
48
48
|
"""Convert SBML MathML to sympy expression.
|
|
49
49
|
|
|
@@ -53,6 +53,9 @@ def sbml_math_to_sympy(
|
|
|
53
53
|
sbml_obj:
|
|
54
54
|
The SBML object to be converted.
|
|
55
55
|
(Either directly the ASTNode or the surrounding SBase object).
|
|
56
|
+
kwargs:
|
|
57
|
+
Additional keyword arguments passed to
|
|
58
|
+
:attr:`SBMLMathMLParser.__init__`.
|
|
56
59
|
Returns:
|
|
57
60
|
The resulting sympy expression.
|
|
58
61
|
"""
|
|
@@ -73,9 +76,9 @@ def sbml_math_to_sympy(
|
|
|
73
76
|
mathml = libsbml.writeMathMLWithNamespaceToString(
|
|
74
77
|
ast_node, libsbml.SBMLNamespaces(level, version)
|
|
75
78
|
)
|
|
76
|
-
return SBMLMathMLParser(
|
|
77
|
-
|
|
78
|
-
)
|
|
79
|
+
return SBMLMathMLParser(
|
|
80
|
+
sbml_level=level, sbml_version=version, **kwargs
|
|
81
|
+
).parse_str(mathml)
|
|
79
82
|
|
|
80
83
|
|
|
81
84
|
def set_math(
|
|
@@ -4,7 +4,8 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import sympy as sp
|
|
6
6
|
|
|
7
|
-
__all__ = ["CSymbol", "TimeSymbol"]
|
|
7
|
+
__all__ = ["CSymbol", "TimeSymbol", "avogadro"]
|
|
8
|
+
|
|
8
9
|
|
|
9
10
|
#: The `Avogadro constant <https://en.wikipedia.org/wiki/Avogadro_constant>`_
|
|
10
11
|
# as defined in SBML L3V2 section 3.4.6.
|
|
@@ -87,6 +88,11 @@ class CSymbol(sp.Dummy):
|
|
|
87
88
|
return SBML_L3V2_AVOGADRO_VALUE
|
|
88
89
|
return super().__float__()
|
|
89
90
|
|
|
91
|
+
def _eval_evalf(self, prec):
|
|
92
|
+
if self.definition_url == "http://www.sbml.org/sbml/symbols/avogadro":
|
|
93
|
+
return sp.Float(SBML_L3V2_AVOGADRO_VALUE)
|
|
94
|
+
return super()._eval_evalf()
|
|
95
|
+
|
|
90
96
|
@classmethod
|
|
91
97
|
def register_subclass(cls, derived_class: type[CSymbol]):
|
|
92
98
|
cls._definition_url_to_derived_class[derived_class.DEFINITION_URL] = (
|
|
@@ -94,6 +100,12 @@ class CSymbol(sp.Dummy):
|
|
|
94
100
|
)
|
|
95
101
|
|
|
96
102
|
|
|
103
|
+
#: SBML's `Avogadro constant <https://en.wikipedia.org/wiki/Avogadro_constant>`_
|
|
104
|
+
avogadro = CSymbol(
|
|
105
|
+
"avogadro", definition_url="http://www.sbml.org/sbml/symbols/avogadro"
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
97
109
|
class TimeSymbol(CSymbol):
|
|
98
110
|
"""The current internal simulation time.
|
|
99
111
|
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
import contextlib
|
|
4
4
|
import operator as operators
|
|
5
|
-
import warnings
|
|
6
5
|
from functools import reduce
|
|
7
6
|
from io import BytesIO
|
|
8
7
|
from typing import Union
|
|
@@ -10,7 +9,12 @@ from typing import Union
|
|
|
10
9
|
import sympy as sp
|
|
11
10
|
from lxml import etree
|
|
12
11
|
from pint import UnitRegistry
|
|
13
|
-
from sympy
|
|
12
|
+
from sympy import Piecewise
|
|
13
|
+
from sympy.logic.boolalg import (
|
|
14
|
+
Boolean,
|
|
15
|
+
BooleanFalse,
|
|
16
|
+
BooleanTrue,
|
|
17
|
+
)
|
|
14
18
|
|
|
15
19
|
from . import _DEFAULT_SBML_LEVEL, _DEFAULT_SBML_VERSION
|
|
16
20
|
from .cfunction import CFunction
|
|
@@ -27,6 +31,29 @@ _ureg = UnitRegistry()
|
|
|
27
31
|
_ureg.Quantity.name = property(fget=lambda s: f"({s})")
|
|
28
32
|
_ureg.Quantity_sympy_ = lambda s: sp.sympify(f"{s.m}*{s.u:~}")
|
|
29
33
|
|
|
34
|
+
|
|
35
|
+
# some operator implementations to handle `evaluate`
|
|
36
|
+
# *and* be compatible with non Expr operands
|
|
37
|
+
# (non-Expr is deprecated for Add, Mul, Pow, ...)
|
|
38
|
+
def _divide(*args, evaluate: bool = False):
|
|
39
|
+
with sp.evaluate(evaluate):
|
|
40
|
+
return operators.truediv(*args)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _mul(*args, evaluate: bool = False):
|
|
44
|
+
with sp.evaluate(evaluate):
|
|
45
|
+
if len(args) >= 2:
|
|
46
|
+
return reduce(operators.mul, args[1:], args[0])
|
|
47
|
+
if len(args) == 1:
|
|
48
|
+
return args[0]
|
|
49
|
+
return sp.Integer(1)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _pow(base, exponent, evaluate: bool = False):
|
|
53
|
+
with sp.evaluate(evaluate):
|
|
54
|
+
return base**exponent
|
|
55
|
+
|
|
56
|
+
|
|
30
57
|
mathml_op_sympy_trigonometric = {
|
|
31
58
|
f"{{{mathml_ns}}}sin": sp.sin,
|
|
32
59
|
f"{{{mathml_ns}}}cos": sp.cos,
|
|
@@ -54,27 +81,40 @@ mathml_op_sympy_trigonometric = {
|
|
|
54
81
|
f"{{{mathml_ns}}}arccoth": sp.acoth,
|
|
55
82
|
}
|
|
56
83
|
mathml_op_sympy_boolean = {
|
|
57
|
-
f"{{{mathml_ns}}}and": sp.And,
|
|
58
|
-
|
|
59
|
-
|
|
84
|
+
f"{{{mathml_ns}}}and": lambda *args, **kwargs: sp.And(*args, **kwargs)
|
|
85
|
+
if kwargs.get("evaluate", False)
|
|
86
|
+
else sp.And(*map(_num2bool, args), **kwargs),
|
|
87
|
+
f"{{{mathml_ns}}}or": lambda *args, **kwargs: sp.Or(*args, **kwargs)
|
|
88
|
+
if kwargs.get("evaluate", False)
|
|
89
|
+
else sp.Or(*map(_num2bool, args), **kwargs),
|
|
90
|
+
f"{{{mathml_ns}}}xor": lambda *args, **kwargs: sp.Xor(*args, **kwargs)
|
|
91
|
+
if kwargs.get("evaluate", False)
|
|
92
|
+
else sp.Xor(*map(_num2bool, args), **kwargs),
|
|
93
|
+
f"{{{mathml_ns}}}implies": sp.Implies,
|
|
60
94
|
}
|
|
61
95
|
|
|
62
96
|
mathml_op_sympy = {
|
|
63
|
-
|
|
64
|
-
|
|
97
|
+
# Using non-Expr arguments in Mul and Pow is deprecated
|
|
98
|
+
# and we might use pint.Quantitity, so we use operator.mul
|
|
99
|
+
f"{{{mathml_ns}}}times": _mul,
|
|
100
|
+
f"{{{mathml_ns}}}divide": _divide,
|
|
101
|
+
f"{{{mathml_ns}}}power": _pow,
|
|
102
|
+
f"{{{mathml_ns}}}eq": lambda *args, **kwargs: sp.Equality(*args, **kwargs),
|
|
103
|
+
f"{{{mathml_ns}}}neq": lambda *args, **kwargs: sp.Unequality(
|
|
104
|
+
*args, **kwargs
|
|
105
|
+
),
|
|
106
|
+
f"{{{mathml_ns}}}lt": lambda *args, **kwargs: sp.StrictLessThan(
|
|
107
|
+
*args, **kwargs
|
|
65
108
|
),
|
|
66
|
-
f"{{{mathml_ns}}}
|
|
67
|
-
|
|
68
|
-
f"{{{mathml_ns}}}eq": lambda *args: sp.Equality(*args, evaluate=False),
|
|
69
|
-
f"{{{mathml_ns}}}neq": lambda *args: sp.Unequality(*args, evaluate=False),
|
|
70
|
-
f"{{{mathml_ns}}}lt": lambda *args: sp.StrictLessThan(
|
|
71
|
-
*args, evaluate=False
|
|
109
|
+
f"{{{mathml_ns}}}gt": lambda *args, **kwargs: sp.StrictGreaterThan(
|
|
110
|
+
*args, **kwargs
|
|
72
111
|
),
|
|
73
|
-
f"{{{mathml_ns}}}
|
|
74
|
-
*args,
|
|
112
|
+
f"{{{mathml_ns}}}geq": lambda *args, **kwargs: sp.GreaterThan(
|
|
113
|
+
*args, **kwargs
|
|
114
|
+
),
|
|
115
|
+
f"{{{mathml_ns}}}leq": lambda *args, **kwargs: sp.LessThan(
|
|
116
|
+
*args, **kwargs
|
|
75
117
|
),
|
|
76
|
-
f"{{{mathml_ns}}}geq": lambda *args: sp.GreaterThan(*args, evaluate=False),
|
|
77
|
-
f"{{{mathml_ns}}}leq": lambda *args: sp.LessThan(*args, evaluate=False),
|
|
78
118
|
f"{{{mathml_ns}}}max": sp.Max,
|
|
79
119
|
f"{{{mathml_ns}}}min": sp.Min,
|
|
80
120
|
f"{{{mathml_ns}}}abs": sp.Abs,
|
|
@@ -85,7 +125,6 @@ mathml_op_sympy = {
|
|
|
85
125
|
f"{{{mathml_ns}}}ln": sp.log,
|
|
86
126
|
f"{{{mathml_ns}}}not": sp.Not,
|
|
87
127
|
f"{{{mathml_ns}}}rem": sp.Mod,
|
|
88
|
-
f"{{{mathml_ns}}}implies": sp.Implies,
|
|
89
128
|
**mathml_op_sympy_trigonometric,
|
|
90
129
|
**mathml_op_sympy_boolean,
|
|
91
130
|
}
|
|
@@ -142,8 +181,9 @@ class SBMLMathMLParser:
|
|
|
142
181
|
|
|
143
182
|
See also: `MathML 2.0 specification <https://www.w3.org/TR/MathML2/>`_.
|
|
144
183
|
|
|
145
|
-
:param sbml_level: SBML level
|
|
146
|
-
|
|
184
|
+
:param sbml_level: SBML level.
|
|
185
|
+
Note that SBML level < 3 has not been thoroughly tested.
|
|
186
|
+
:param sbml_version: SBML version.
|
|
147
187
|
:param ureg:
|
|
148
188
|
Optional :class:`pint.UnitRegistry` to use for unit conversion.
|
|
149
189
|
:param floats_as_rationals:
|
|
@@ -168,14 +208,9 @@ class SBMLMathMLParser:
|
|
|
168
208
|
floats_as_rationals=True,
|
|
169
209
|
ignore_units=False,
|
|
170
210
|
symbol_kwargs=None,
|
|
211
|
+
evaluate=False,
|
|
171
212
|
):
|
|
172
213
|
"""Constructor"""
|
|
173
|
-
if sbml_level < 3:
|
|
174
|
-
warnings.warn(
|
|
175
|
-
"SBML level < 3 has not been thoroughly tested.",
|
|
176
|
-
UserWarning,
|
|
177
|
-
stacklevel=2,
|
|
178
|
-
)
|
|
179
214
|
self.ureg = ureg or _ureg or UnitRegistry()
|
|
180
215
|
self.sbml_core_ns = f"http://www.sbml.org/sbml/level{sbml_level}/version{sbml_version}/core"
|
|
181
216
|
# TODO {prefix=>url}
|
|
@@ -188,6 +223,7 @@ class SBMLMathMLParser:
|
|
|
188
223
|
self.symbol_kwargs = (
|
|
189
224
|
{} if symbol_kwargs is None else symbol_kwargs.copy()
|
|
190
225
|
)
|
|
226
|
+
self.evaluate = evaluate
|
|
191
227
|
|
|
192
228
|
def parse_file(self, file_like) -> sp.Expr:
|
|
193
229
|
"""Parse a file-like object containing MathML.
|
|
@@ -286,7 +322,7 @@ class SBMLMathMLParser:
|
|
|
286
322
|
and len(operands) >= 3
|
|
287
323
|
):
|
|
288
324
|
# n-ary relational operators:
|
|
289
|
-
# a < b < c cannot
|
|
325
|
+
# a < b < c cannot directly be represented in sympy.
|
|
290
326
|
# either change to `a < b and b < c`, to be sympy-evaluatable,
|
|
291
327
|
# or to Function('MathML_Lt')(a, b, c), which will cycle
|
|
292
328
|
# (although potentially collide with other functions with
|
|
@@ -299,31 +335,41 @@ class SBMLMathMLParser:
|
|
|
299
335
|
for i in range(len(sym_operands) - 1):
|
|
300
336
|
expr = sp.And(
|
|
301
337
|
expr,
|
|
302
|
-
sym_operator(
|
|
338
|
+
sym_operator(
|
|
339
|
+
sym_operands[i],
|
|
340
|
+
sym_operands[i + 1],
|
|
341
|
+
evaluate=self.evaluate,
|
|
342
|
+
),
|
|
303
343
|
)
|
|
304
344
|
return expr
|
|
305
345
|
|
|
306
|
-
#
|
|
307
|
-
#
|
|
308
|
-
# {int,float} -> bool
|
|
309
|
-
# `Piecewise((True, expr != 0),(False,True))`
|
|
310
|
-
# as sympy doesn't do that automatically
|
|
346
|
+
# explicit boolean->{int,float} conversion for non-boolean functions,
|
|
347
|
+
# since sympy does not do that automatically
|
|
311
348
|
if operator.tag not in mathml_op_sympy_boolean:
|
|
312
349
|
sym_operands = list(map(_bool2num, sym_operands))
|
|
313
350
|
|
|
314
351
|
with contextlib.suppress(KeyError):
|
|
315
|
-
return mathml_op_sympy[operator.tag](
|
|
352
|
+
return mathml_op_sympy[operator.tag](
|
|
353
|
+
*sym_operands, evaluate=self.evaluate
|
|
354
|
+
)
|
|
316
355
|
|
|
317
356
|
if operator.tag == f"{{{mathml_ns}}}plus":
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
357
|
+
with sp.evaluate(self.evaluate):
|
|
358
|
+
if len(sym_operands) >= 2:
|
|
359
|
+
return reduce(
|
|
360
|
+
operators.add, sym_operands[1:], sym_operands[0]
|
|
361
|
+
)
|
|
362
|
+
if len(sym_operands) == 1:
|
|
363
|
+
return sym_operands[0]
|
|
364
|
+
if len(sym_operands) == 0:
|
|
365
|
+
return sp.Integer(0)
|
|
321
366
|
|
|
322
367
|
if operator.tag == f"{{{mathml_ns}}}minus":
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
368
|
+
with sp.evaluate(self.evaluate):
|
|
369
|
+
if len(sym_operands) == 2:
|
|
370
|
+
return sym_operands[0] - sym_operands[1]
|
|
371
|
+
if len(sym_operands) == 1:
|
|
372
|
+
return -sym_operands[0]
|
|
327
373
|
raise AssertionError(list(element))
|
|
328
374
|
|
|
329
375
|
if operator.tag == f"{{{mathml_ns}}}root":
|
|
@@ -335,7 +381,8 @@ class SBMLMathMLParser:
|
|
|
335
381
|
degree = sym_operands[0]
|
|
336
382
|
assert operands[0].tag == f"{{{mathml_ns}}}degree"
|
|
337
383
|
|
|
338
|
-
|
|
384
|
+
with sp.evaluate(self.evaluate):
|
|
385
|
+
return sym_operands[-1] ** (1 / degree)
|
|
339
386
|
|
|
340
387
|
if operator.tag == f"{{{mathml_ns}}}log":
|
|
341
388
|
assert len(operands) == 2
|
|
@@ -343,7 +390,7 @@ class SBMLMathMLParser:
|
|
|
343
390
|
assert len(operands[0]) == 1
|
|
344
391
|
base, x = sym_operands
|
|
345
392
|
# won't cycle - will be transformed to ``log(x)/log(base)``
|
|
346
|
-
return sp.log(x, base)
|
|
393
|
+
return sp.log(x, base, evaluate=self.evaluate)
|
|
347
394
|
|
|
348
395
|
if operator.tag == f"{{{mathml_ns}}}quotient":
|
|
349
396
|
# there is no direct correspondence for integer division in sympy,
|
|
@@ -351,7 +398,8 @@ class SBMLMathMLParser:
|
|
|
351
398
|
# a // b = (a - a mod b) / b
|
|
352
399
|
assert len(operands) == 2
|
|
353
400
|
a, b = sym_operands
|
|
354
|
-
|
|
401
|
+
with sp.evaluate(self.evaluate):
|
|
402
|
+
return (a - a % b) / b
|
|
355
403
|
|
|
356
404
|
name = self.preprocess_symbol_name(operator.text.strip(), operator)
|
|
357
405
|
|
|
@@ -460,15 +508,16 @@ class SBMLMathMLParser:
|
|
|
460
508
|
expr_cond_pairs = []
|
|
461
509
|
for e in element:
|
|
462
510
|
# TODO: cannot currently handle functions as condition-arguments.
|
|
463
|
-
# they need to be derived sympy.BooleanFunction
|
|
511
|
+
# they need to be derived from `sympy.BooleanFunction`
|
|
512
|
+
# Nested piecewise functions may work, though, if the inner
|
|
513
|
+
# expressions are all Boolean
|
|
464
514
|
if e.tag == f"{{{mathml_ns}}}piece":
|
|
465
515
|
assert len(e) == 2
|
|
466
516
|
cond = self._parse_element(e[1])
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
)
|
|
517
|
+
# Only Boolean conditions are supported
|
|
518
|
+
# -> convert
|
|
519
|
+
# this won't round-trip
|
|
520
|
+
cond = _num2bool(cond)
|
|
472
521
|
expr_cond_pairs.append(
|
|
473
522
|
(self._parse_element(e[0]), cond),
|
|
474
523
|
)
|
|
@@ -481,7 +530,9 @@ class SBMLMathMLParser:
|
|
|
481
530
|
)
|
|
482
531
|
else:
|
|
483
532
|
raise AssertionError(e.tag)
|
|
484
|
-
|
|
533
|
+
|
|
534
|
+
with sp.evaluate(self.evaluate):
|
|
535
|
+
return sp.Piecewise(*expr_cond_pairs)
|
|
485
536
|
|
|
486
537
|
def handle_lambda(self, element: etree._Element) -> sp.Expr:
|
|
487
538
|
# See https://www.w3.org/TR/MathML2/chapter4.html#id.4.2.1.7
|
|
@@ -529,10 +580,39 @@ class SBMLMathMLParser:
|
|
|
529
580
|
return name
|
|
530
581
|
|
|
531
582
|
|
|
532
|
-
def _bool2num(x):
|
|
533
|
-
"""Convert sympy Booleans to
|
|
583
|
+
def _bool2num(x: sp.Basic) -> sp.Basic:
|
|
584
|
+
"""Convert sympy Booleans to expressions or Integers.
|
|
585
|
+
|
|
586
|
+
Peforms boolean->{int,float} conversion via
|
|
587
|
+
``Piecewise((1, expr), (0, True))``.
|
|
588
|
+
|
|
589
|
+
Return anything else as is.
|
|
590
|
+
"""
|
|
534
591
|
if isinstance(x, BooleanFalse):
|
|
535
592
|
return sp.Integer(0)
|
|
536
593
|
if isinstance(x, BooleanTrue):
|
|
537
594
|
return sp.Integer(1)
|
|
595
|
+
if isinstance(x, Boolean) and not isinstance(x, sp.Symbol):
|
|
596
|
+
# `Piecewise((1, expr),(0,True))`
|
|
597
|
+
return Piecewise((sp.Integer(1), x), (sp.Integer(0), sp.true))
|
|
598
|
+
|
|
599
|
+
return x
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
def _num2bool(x: sp.Basic) -> sp.Basic:
|
|
603
|
+
"""Convert non-Booleans to Boolean expressions.
|
|
604
|
+
|
|
605
|
+
Convert non-Boolean expressions to Piecewise or Boolean expressions:
|
|
606
|
+
|
|
607
|
+
``expr -> Piecewise((True, expr != 0), (False, True))``
|
|
608
|
+
|
|
609
|
+
Return anything else as is.
|
|
610
|
+
"""
|
|
611
|
+
if not isinstance(x, Boolean):
|
|
612
|
+
if isinstance(x, Piecewise):
|
|
613
|
+
# apply recursively to the expressiosn
|
|
614
|
+
return Piecewise(
|
|
615
|
+
*((_num2bool(expr), cond) for expr, cond in x.args)
|
|
616
|
+
)
|
|
617
|
+
return Piecewise((True, x != 0), (False, True))
|
|
538
618
|
return x
|
|
@@ -109,7 +109,7 @@ class SBMLMathMLPrinter(MathMLContentPrinter):
|
|
|
109
109
|
|
|
110
110
|
def _print_Number(self, e):
|
|
111
111
|
# only try printing as int if it fits int32
|
|
112
|
-
if isinstance(e, int) and e
|
|
112
|
+
if isinstance(e, int) and _is_sbml_compatible_int(e):
|
|
113
113
|
res = self._print_int(e)
|
|
114
114
|
else:
|
|
115
115
|
res = super()._print_Number(e)
|
|
@@ -123,6 +123,10 @@ class SBMLMathMLPrinter(MathMLContentPrinter):
|
|
|
123
123
|
# don't divide by one
|
|
124
124
|
return self._print_int(e.p)
|
|
125
125
|
|
|
126
|
+
if not (_is_sbml_compatible_int(e.p) and _is_sbml_compatible_int(e.q)):
|
|
127
|
+
# avoid int32 under/overflow in libsbml and print as float
|
|
128
|
+
return self._print_Number(e.evalf())
|
|
129
|
+
|
|
126
130
|
res = self.dom.createElement("cn")
|
|
127
131
|
res.setAttribute("type", "rational")
|
|
128
132
|
res.appendChild(self.dom.createTextNode(f"{e.p} <sep/> {e.q}"))
|
|
@@ -132,7 +136,7 @@ class SBMLMathMLPrinter(MathMLContentPrinter):
|
|
|
132
136
|
return res
|
|
133
137
|
|
|
134
138
|
def _print_int(self, e):
|
|
135
|
-
if
|
|
139
|
+
if not _is_sbml_compatible_int(e):
|
|
136
140
|
# avoid int32 under/overflow in libsbml and print as float
|
|
137
141
|
return self._print_Number(e)
|
|
138
142
|
res = super()._print_int(e)
|
|
@@ -192,3 +196,8 @@ class SBMLMathMLPrinter(MathMLContentPrinter):
|
|
|
192
196
|
dom_element.appendChild(self._print(arg))
|
|
193
197
|
|
|
194
198
|
return dom_element
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _is_sbml_compatible_int(value: int) -> bool:
|
|
202
|
+
"""Check if integer is compatible with SBML (fits into signed int32)."""
|
|
203
|
+
return 2**31 > value >= -(2**31)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: sbmlmath
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.1
|
|
4
4
|
Summary: SBML Math <-> SymPy
|
|
5
5
|
Author-email: Daniel Weindl <sci@danielweindl.de>
|
|
6
|
-
License: BSD-3-Clause
|
|
6
|
+
License-Expression: BSD-3-Clause
|
|
7
7
|
Project-URL: Homepage, https://github.com/dweindl/sbmlmath
|
|
8
8
|
Project-URL: Documentation, https://sbmlmath.readthedocs.io/
|
|
9
9
|
Project-URL: Repository, https://github.com/dweindl/sbmlmath.git
|
|
@@ -19,10 +19,13 @@ Requires-Dist: lxml>=4.6.4
|
|
|
19
19
|
Provides-Extra: test
|
|
20
20
|
Requires-Dist: pytest>=7; extra == "test"
|
|
21
21
|
Requires-Dist: pre-commit>=3; extra == "test"
|
|
22
|
+
Dynamic: license-file
|
|
22
23
|
|
|
23
24
|
# sbmlmath — a Python library for handling SBML MathML
|
|
24
25
|
|
|
25
26
|
[](https://pypi.org/project/sbmlmath/)
|
|
27
|
+
[](https://doi.org/10.5281/zenodo.14814669)
|
|
28
|
+
|
|
26
29
|
|
|
27
30
|
`sbmlmath` is a Python library for interconverting [SymPy](https://github.com/sympy/sympy/)
|
|
28
31
|
expressions and [SBML](https://sbml.org/)-MathML.
|
|
@@ -35,7 +38,7 @@ Main functionality:
|
|
|
35
38
|
* SBML MathML -> sympy
|
|
36
39
|
* in particular for cases where `sympy.sympify(libsbml.formulaToL3String(...))`
|
|
37
40
|
won't do the job
|
|
38
|
-
(mind the differences operator precedence between
|
|
41
|
+
(mind, e.g., the differences in operator precedence between
|
|
39
42
|
sympy/[Python](https://docs.python.org/3/reference/expressions.html#operator-precedence)
|
|
40
43
|
and [libsbml L3 formulas](https://synonym.caltech.edu/software/libsbml/5.18.0/docs/formatted/python-api/libsbml-math.html#math-l3)!)
|
|
41
44
|
* retaining unit annotations and other `<ci>` attributes
|
|
@@ -72,5 +75,5 @@ pip install sbmlmath
|
|
|
72
75
|
|
|
73
76
|
The latest development version from GitHub:
|
|
74
77
|
```bash
|
|
75
|
-
pip install https://github.com/dweindl/sbmlmath
|
|
78
|
+
pip install git+https://github.com/dweindl/sbmlmath.git#egg=sbmlmath
|
|
76
79
|
```
|
|
@@ -61,3 +61,9 @@ def test_cfunction():
|
|
|
61
61
|
assert rate_of(1) == 0
|
|
62
62
|
assert rate_of(1, evaluate=False).has(rate_of) is True
|
|
63
63
|
assert rate_of(1, evaluate=False).doit() == 0
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_avogadro():
|
|
67
|
+
from sbmlmath import avogadro
|
|
68
|
+
|
|
69
|
+
assert avogadro.evalf() == float(avogadro)
|
|
@@ -34,7 +34,7 @@ def test_sbml(sbml_file):
|
|
|
34
34
|
sbml_doc = sbml_reader.readSBMLFromFile(str(sbml_file))
|
|
35
35
|
sbml_model = sbml_doc.getModel()
|
|
36
36
|
|
|
37
|
-
parser = SBMLMathMLParser()
|
|
37
|
+
parser = SBMLMathMLParser(evaluate=True)
|
|
38
38
|
for element in sbml_model.getListOfAllElements():
|
|
39
39
|
if (get_math := getattr(element, "getMath", None)) and (
|
|
40
40
|
mathml := libsbml.writeMathMLToString(get_math())
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import libsbml
|
|
2
2
|
import sympy as sp
|
|
3
|
+
from sympy import Piecewise
|
|
3
4
|
|
|
4
5
|
from sbmlmath import *
|
|
6
|
+
from sbmlmath.mathml_parser import _bool2num, _num2bool
|
|
5
7
|
|
|
6
8
|
|
|
7
9
|
def test_set_math():
|
|
@@ -19,7 +21,7 @@ def test_set_math():
|
|
|
19
21
|
# actual test
|
|
20
22
|
expr = sp.sympify("2 * p^2")
|
|
21
23
|
set_math(ia, expr)
|
|
22
|
-
assert expr == sbml_math_to_sympy(ia)
|
|
24
|
+
assert expr == sbml_math_to_sympy(ia, ignore_units=True)
|
|
23
25
|
|
|
24
26
|
|
|
25
27
|
def test_large_mathml():
|
|
@@ -38,7 +40,7 @@ def test_large_mathml():
|
|
|
38
40
|
print(len(mathml_exp))
|
|
39
41
|
assert len(mathml_exp) >= 2**15
|
|
40
42
|
|
|
41
|
-
assert SBMLMathMLParser().parse_str(mathml_exp) == sp.sympify(
|
|
43
|
+
assert SBMLMathMLParser(evaluate=True).parse_str(mathml_exp) == sp.sympify(
|
|
42
44
|
"600 * some_long_identifier_whose_name_doesnt_matter"
|
|
43
45
|
)
|
|
44
46
|
|
|
@@ -206,3 +208,31 @@ def test_integer_overflow():
|
|
|
206
208
|
)
|
|
207
209
|
is not None
|
|
208
210
|
)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def test_bool2num():
|
|
214
|
+
a, b = sp.symbols("a, b")
|
|
215
|
+
assert _bool2num(1) == 1
|
|
216
|
+
assert _bool2num(a) is a
|
|
217
|
+
assert _bool2num(sp.true) == 1
|
|
218
|
+
assert _bool2num(sp.false) == 0
|
|
219
|
+
assert _bool2num(a | b) == Piecewise(
|
|
220
|
+
(1, a | b),
|
|
221
|
+
(0, True),
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def test_num2_bool():
|
|
226
|
+
a, b = sp.symbols("a, b")
|
|
227
|
+
|
|
228
|
+
assert _num2bool(sp.true) == sp.true
|
|
229
|
+
assert _num2bool(0.1) == sp.true
|
|
230
|
+
assert _num2bool(-0.1) == sp.true
|
|
231
|
+
assert _num2bool(0) == sp.false
|
|
232
|
+
assert _num2bool(-0) == sp.false
|
|
233
|
+
assert _num2bool(0.0) == sp.false
|
|
234
|
+
assert _num2bool(-0.0) == sp.false
|
|
235
|
+
|
|
236
|
+
# no "conversion" if piecewise expressions are already boolean!
|
|
237
|
+
expr = sp.Piecewise((sp.true, a < 10), (sp.false, sp.true))
|
|
238
|
+
assert _num2bool(expr) == expr
|
|
@@ -24,7 +24,7 @@ from sbmlmath import *
|
|
|
24
24
|
def test_mathmlparser_vs_sympify(formula_str):
|
|
25
25
|
ast_node = libsbml.parseL3Formula(formula_str)
|
|
26
26
|
mathml = libsbml.writeMathMLToString(ast_node)
|
|
27
|
-
parser = SBMLMathMLParser(floats_as_rationals=False)
|
|
27
|
+
parser = SBMLMathMLParser(floats_as_rationals=False, evaluate=True)
|
|
28
28
|
sym_expr = parser.parse_str(mathml)
|
|
29
29
|
print(sym_expr)
|
|
30
30
|
assert sym_expr == sp.sympify(formula_str)
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
from math import fabs
|
|
2
|
+
|
|
3
|
+
import libsbml
|
|
4
|
+
from sympy import Rational
|
|
5
|
+
|
|
1
6
|
from sbmlmath import SBMLMathMLPrinter, SpeciesSymbol
|
|
2
7
|
|
|
3
8
|
|
|
@@ -23,3 +28,18 @@ def test_species_symbol_spec_ref():
|
|
|
23
28
|
'<ci multi:speciesReference="ref_to_A">A</ci>'
|
|
24
29
|
"</math>"
|
|
25
30
|
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def test_print_large_rational():
|
|
34
|
+
# too large for SBML int32 -> must be printed as float
|
|
35
|
+
assert "rational" not in SBMLMathMLPrinter().doprint(Rational(1, 2**42))
|
|
36
|
+
assert "rational" not in SBMLMathMLPrinter().doprint(Rational(2**42, 7))
|
|
37
|
+
assert (
|
|
38
|
+
fabs(
|
|
39
|
+
libsbml.readMathMLFromString(
|
|
40
|
+
SBMLMathMLPrinter().doprint(Rational(1, 2**42))
|
|
41
|
+
).getValue()
|
|
42
|
+
- 1 / 2**42
|
|
43
|
+
)
|
|
44
|
+
< 1e-15
|
|
45
|
+
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|