sbmlmath 0.3.0__tar.gz → 0.4.0__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.0}/.pre-commit-config.yaml +1 -1
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/CITATION.cff +1 -3
- {sbmlmath-0.3.0/sbmlmath.egg-info → sbmlmath-0.4.0}/PKG-INFO +8 -5
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/README.md +4 -2
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/doc/about.rst +7 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/pyproject.toml +4 -1
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/sbmlmath/__init__.py +8 -5
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/sbmlmath/csymbol.py +13 -1
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/sbmlmath/mathml_parser.py +124 -48
- {sbmlmath-0.3.0 → sbmlmath-0.4.0/sbmlmath.egg-info}/PKG-INFO +8 -5
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/tests/test_csymbol.py +6 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/tests/test_mathml_parser_sbml_semantic_suite.py +1 -1
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/tests/test_misc.py +32 -2
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/tests/test_parser.py +1 -1
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/.github/dependabot.yml +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/.github/workflows/ci.yaml +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/.github/workflows/deploy_release.yaml +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/.gitignore +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/.readthedocs.yml +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/LICENSE +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/doc/.gitignore +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/doc/Makefile +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/doc/api.rst +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/doc/conf.py +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/doc/index.rst +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/doc/make.bat +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/doc/rtd_requirements.txt +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/sbmlmath/cfunction.py +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/sbmlmath/mathml_printer.py +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/sbmlmath/species_symbol.py +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/sbmlmath.egg-info/SOURCES.txt +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/sbmlmath.egg-info/dependency_links.txt +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/sbmlmath.egg-info/requires.txt +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/sbmlmath.egg-info/top_level.txt +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/setup.cfg +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/tests/test_printer.py +0 -0
- {sbmlmath-0.3.0 → sbmlmath-0.4.0}/tests/test_species_symbol.py +0 -0
|
@@ -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.0
|
|
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
|
|
108
|
+
),
|
|
109
|
+
f"{{{mathml_ns}}}gt": lambda *args, **kwargs: sp.StrictGreaterThan(
|
|
110
|
+
*args, **kwargs
|
|
65
111
|
),
|
|
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
|
|
112
|
+
f"{{{mathml_ns}}}geq": lambda *args, **kwargs: sp.GreaterThan(
|
|
113
|
+
*args, **kwargs
|
|
72
114
|
),
|
|
73
|
-
f"{{{mathml_ns}}}
|
|
74
|
-
*args,
|
|
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,7 +335,11 @@ 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
|
|
|
@@ -312,18 +352,27 @@ class SBMLMathMLParser:
|
|
|
312
352
|
sym_operands = list(map(_bool2num, sym_operands))
|
|
313
353
|
|
|
314
354
|
with contextlib.suppress(KeyError):
|
|
315
|
-
return mathml_op_sympy[operator.tag](
|
|
355
|
+
return mathml_op_sympy[operator.tag](
|
|
356
|
+
*sym_operands, evaluate=self.evaluate
|
|
357
|
+
)
|
|
316
358
|
|
|
317
359
|
if operator.tag == f"{{{mathml_ns}}}plus":
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
360
|
+
with sp.evaluate(self.evaluate):
|
|
361
|
+
if len(sym_operands) >= 2:
|
|
362
|
+
return reduce(
|
|
363
|
+
operators.add, sym_operands[1:], sym_operands[0]
|
|
364
|
+
)
|
|
365
|
+
if len(sym_operands) == 1:
|
|
366
|
+
return sym_operands[0]
|
|
367
|
+
if len(sym_operands) == 0:
|
|
368
|
+
return sp.Integer(0)
|
|
321
369
|
|
|
322
370
|
if operator.tag == f"{{{mathml_ns}}}minus":
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
371
|
+
with sp.evaluate(self.evaluate):
|
|
372
|
+
if len(sym_operands) == 2:
|
|
373
|
+
return sym_operands[0] - sym_operands[1]
|
|
374
|
+
if len(sym_operands) == 1:
|
|
375
|
+
return -sym_operands[0]
|
|
327
376
|
raise AssertionError(list(element))
|
|
328
377
|
|
|
329
378
|
if operator.tag == f"{{{mathml_ns}}}root":
|
|
@@ -335,7 +384,8 @@ class SBMLMathMLParser:
|
|
|
335
384
|
degree = sym_operands[0]
|
|
336
385
|
assert operands[0].tag == f"{{{mathml_ns}}}degree"
|
|
337
386
|
|
|
338
|
-
|
|
387
|
+
with sp.evaluate(self.evaluate):
|
|
388
|
+
return sym_operands[-1] ** (1 / degree)
|
|
339
389
|
|
|
340
390
|
if operator.tag == f"{{{mathml_ns}}}log":
|
|
341
391
|
assert len(operands) == 2
|
|
@@ -343,7 +393,7 @@ class SBMLMathMLParser:
|
|
|
343
393
|
assert len(operands[0]) == 1
|
|
344
394
|
base, x = sym_operands
|
|
345
395
|
# won't cycle - will be transformed to ``log(x)/log(base)``
|
|
346
|
-
return sp.log(x, base)
|
|
396
|
+
return sp.log(x, base, evaluate=self.evaluate)
|
|
347
397
|
|
|
348
398
|
if operator.tag == f"{{{mathml_ns}}}quotient":
|
|
349
399
|
# there is no direct correspondence for integer division in sympy,
|
|
@@ -351,7 +401,8 @@ class SBMLMathMLParser:
|
|
|
351
401
|
# a // b = (a - a mod b) / b
|
|
352
402
|
assert len(operands) == 2
|
|
353
403
|
a, b = sym_operands
|
|
354
|
-
|
|
404
|
+
with sp.evaluate(self.evaluate):
|
|
405
|
+
return (a - a % b) / b
|
|
355
406
|
|
|
356
407
|
name = self.preprocess_symbol_name(operator.text.strip(), operator)
|
|
357
408
|
|
|
@@ -460,15 +511,16 @@ class SBMLMathMLParser:
|
|
|
460
511
|
expr_cond_pairs = []
|
|
461
512
|
for e in element:
|
|
462
513
|
# TODO: cannot currently handle functions as condition-arguments.
|
|
463
|
-
# they need to be derived sympy.BooleanFunction
|
|
514
|
+
# they need to be derived from `sympy.BooleanFunction`
|
|
515
|
+
# Nested piecewise functions may work, though, if the inner
|
|
516
|
+
# expressions are all Boolean
|
|
464
517
|
if e.tag == f"{{{mathml_ns}}}piece":
|
|
465
518
|
assert len(e) == 2
|
|
466
519
|
cond = self._parse_element(e[1])
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
)
|
|
520
|
+
# Only Boolean conditions are supported
|
|
521
|
+
# -> convert
|
|
522
|
+
# this won't round-trip
|
|
523
|
+
cond = _num2bool(cond)
|
|
472
524
|
expr_cond_pairs.append(
|
|
473
525
|
(self._parse_element(e[0]), cond),
|
|
474
526
|
)
|
|
@@ -481,7 +533,9 @@ class SBMLMathMLParser:
|
|
|
481
533
|
)
|
|
482
534
|
else:
|
|
483
535
|
raise AssertionError(e.tag)
|
|
484
|
-
|
|
536
|
+
|
|
537
|
+
with sp.evaluate(self.evaluate):
|
|
538
|
+
return sp.Piecewise(*expr_cond_pairs)
|
|
485
539
|
|
|
486
540
|
def handle_lambda(self, element: etree._Element) -> sp.Expr:
|
|
487
541
|
# See https://www.w3.org/TR/MathML2/chapter4.html#id.4.2.1.7
|
|
@@ -529,10 +583,32 @@ class SBMLMathMLParser:
|
|
|
529
583
|
return name
|
|
530
584
|
|
|
531
585
|
|
|
532
|
-
def _bool2num(x):
|
|
533
|
-
"""Convert sympy Booleans to
|
|
586
|
+
def _bool2num(x: sp.Basic) -> sp.Basic:
|
|
587
|
+
"""Convert sympy Booleans or BooleanFunctions to expressions or Integers.
|
|
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
|
+
Return anything else as is.
|
|
606
|
+
"""
|
|
607
|
+
if not isinstance(x, Boolean):
|
|
608
|
+
if isinstance(x, Piecewise):
|
|
609
|
+
# apply recursively to the expressiosn
|
|
610
|
+
return Piecewise(
|
|
611
|
+
*((_num2bool(expr), cond) for expr, cond in x.args)
|
|
612
|
+
)
|
|
613
|
+
return Piecewise((True, x != 0), (False, True))
|
|
538
614
|
return x
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: sbmlmath
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
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)
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|