sbmlmath 0.2.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.2.0 → sbmlmath-0.4.0}/.github/workflows/ci.yaml +3 -3
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/.gitignore +1 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/.pre-commit-config.yaml +1 -1
- sbmlmath-0.4.0/CITATION.cff +23 -0
- {sbmlmath-0.2.0/sbmlmath.egg-info → sbmlmath-0.4.0}/PKG-INFO +11 -6
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/README.md +7 -3
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/doc/about.rst +7 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/pyproject.toml +9 -3
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/sbmlmath/__init__.py +51 -29
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/sbmlmath/cfunction.py +11 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/sbmlmath/csymbol.py +13 -1
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/sbmlmath/mathml_parser.py +130 -48
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/sbmlmath/mathml_printer.py +11 -2
- {sbmlmath-0.2.0 → sbmlmath-0.4.0/sbmlmath.egg-info}/PKG-INFO +11 -6
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/sbmlmath.egg-info/SOURCES.txt +1 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/tests/test_csymbol.py +10 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/tests/test_mathml_parser_sbml_semantic_suite.py +2 -2
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/tests/test_misc.py +32 -2
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/tests/test_parser.py +1 -1
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/.github/dependabot.yml +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/.github/workflows/deploy_release.yaml +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/.readthedocs.yml +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/LICENSE +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/doc/.gitignore +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/doc/Makefile +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/doc/api.rst +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/doc/conf.py +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/doc/index.rst +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/doc/make.bat +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/doc/rtd_requirements.txt +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/sbmlmath/species_symbol.py +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/sbmlmath.egg-info/dependency_links.txt +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/sbmlmath.egg-info/requires.txt +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/sbmlmath.egg-info/top_level.txt +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/setup.cfg +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/tests/test_printer.py +0 -0
- {sbmlmath-0.2.0 → sbmlmath-0.4.0}/tests/test_species_symbol.py +0 -0
|
@@ -44,7 +44,7 @@ jobs:
|
|
|
44
44
|
|
|
45
45
|
- name: Install local packages
|
|
46
46
|
run: |
|
|
47
|
-
pip install --upgrade pip setuptools && pip install .[test]
|
|
47
|
+
python -m pip install --upgrade pip setuptools && python -m pip install .[test]
|
|
48
48
|
|
|
49
49
|
- name: Unit tests
|
|
50
50
|
env:
|
|
@@ -91,7 +91,7 @@ jobs:
|
|
|
91
91
|
|
|
92
92
|
- name: Install local packages
|
|
93
93
|
run: |
|
|
94
|
-
pip install --upgrade pip setuptools && pip install .[test]
|
|
94
|
+
python -m pip install --upgrade pip setuptools && python -m pip install .[test]
|
|
95
95
|
|
|
96
96
|
- name: Unit tests
|
|
97
97
|
env:
|
|
@@ -135,7 +135,7 @@ jobs:
|
|
|
135
135
|
|
|
136
136
|
- name: Install local packages
|
|
137
137
|
run: |
|
|
138
|
-
pip install --upgrade pip setuptools && pip install .[test]
|
|
138
|
+
python -m pip install --upgrade pip setuptools && python -m pip install .[test]
|
|
139
139
|
|
|
140
140
|
- name: Unit tests
|
|
141
141
|
env:
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# This CITATION.cff file was generated with cffinit.
|
|
2
|
+
# Visit https://bit.ly/cffinit to generate yours today!
|
|
3
|
+
|
|
4
|
+
cff-version: 1.2.0
|
|
5
|
+
title: sbmlmath
|
|
6
|
+
type: software
|
|
7
|
+
authors:
|
|
8
|
+
- given-names: Daniel
|
|
9
|
+
family-names: Weindl
|
|
10
|
+
email: sci@danielweindl.de
|
|
11
|
+
orcid: 'https://orcid.org/0000-0001-9963-6057'
|
|
12
|
+
repository-code: 'https://github.com/dweindl/sbmlmath/'
|
|
13
|
+
url: 'https://sbmlmath.readthedocs.io/'
|
|
14
|
+
abstract: >-
|
|
15
|
+
sbmlmath is a Python package for the interconversion of
|
|
16
|
+
SBML MathML and sympy expressions.
|
|
17
|
+
keywords:
|
|
18
|
+
- SBML
|
|
19
|
+
- SymPy
|
|
20
|
+
- math expressions
|
|
21
|
+
- Python
|
|
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,15 @@ 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
|
+
|
|
29
|
+
|
|
30
|
+
`sbmlmath` is a Python library for interconverting [SymPy](https://github.com/sympy/sympy/)
|
|
26
31
|
expressions and [SBML](https://sbml.org/)-MathML.
|
|
27
32
|
SBML uses a subset of [MathML](https://www.w3.org/Math/) that this library
|
|
28
33
|
aims to support. This is not (intended to be) a general MathML parser.
|
|
@@ -33,7 +38,7 @@ Main functionality:
|
|
|
33
38
|
* SBML MathML -> sympy
|
|
34
39
|
* in particular for cases where `sympy.sympify(libsbml.formulaToL3String(...))`
|
|
35
40
|
won't do the job
|
|
36
|
-
(mind the differences operator precedence between
|
|
41
|
+
(mind, e.g., the differences in operator precedence between
|
|
37
42
|
sympy/[Python](https://docs.python.org/3/reference/expressions.html#operator-precedence)
|
|
38
43
|
and [libsbml L3 formulas](https://synonym.caltech.edu/software/libsbml/5.18.0/docs/formatted/python-api/libsbml-math.html#math-l3)!)
|
|
39
44
|
* retaining unit annotations and other `<ci>` attributes
|
|
@@ -70,5 +75,5 @@ pip install sbmlmath
|
|
|
70
75
|
|
|
71
76
|
The latest development version from GitHub:
|
|
72
77
|
```bash
|
|
73
|
-
pip install https://github.com/dweindl/sbmlmath
|
|
78
|
+
pip install git+https://github.com/dweindl/sbmlmath.git#egg=sbmlmath
|
|
74
79
|
```
|
|
@@ -1,6 +1,10 @@
|
|
|
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
|
+
|
|
6
|
+
|
|
7
|
+
`sbmlmath` is a Python library for interconverting [SymPy](https://github.com/sympy/sympy/)
|
|
4
8
|
expressions and [SBML](https://sbml.org/)-MathML.
|
|
5
9
|
SBML uses a subset of [MathML](https://www.w3.org/Math/) that this library
|
|
6
10
|
aims to support. This is not (intended to be) a general MathML parser.
|
|
@@ -11,7 +15,7 @@ Main functionality:
|
|
|
11
15
|
* SBML MathML -> sympy
|
|
12
16
|
* in particular for cases where `sympy.sympify(libsbml.formulaToL3String(...))`
|
|
13
17
|
won't do the job
|
|
14
|
-
(mind the differences operator precedence between
|
|
18
|
+
(mind, e.g., the differences in operator precedence between
|
|
15
19
|
sympy/[Python](https://docs.python.org/3/reference/expressions.html#operator-precedence)
|
|
16
20
|
and [libsbml L3 formulas](https://synonym.caltech.edu/software/libsbml/5.18.0/docs/formatted/python-api/libsbml-math.html#math-l3)!)
|
|
17
21
|
* retaining unit annotations and other `<ci>` attributes
|
|
@@ -48,5 +52,5 @@ pip install sbmlmath
|
|
|
48
52
|
|
|
49
53
|
The latest development version from GitHub:
|
|
50
54
|
```bash
|
|
51
|
-
pip install https://github.com/dweindl/sbmlmath
|
|
55
|
+
pip install git+https://github.com/dweindl/sbmlmath.git#egg=sbmlmath
|
|
52
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]
|
|
@@ -47,6 +50,9 @@ lint.ignore = [
|
|
|
47
50
|
]
|
|
48
51
|
[tool.ruff.lint.per-file-ignores]
|
|
49
52
|
"tests/*" = ["T201"] # print statement
|
|
50
|
-
|
|
53
|
+
"sbmlmath/__init__.py" = [
|
|
54
|
+
"F401", # module imported but unused
|
|
55
|
+
"E402", # import not at top of file
|
|
56
|
+
]
|
|
51
57
|
[tool.pytest.ini_options]
|
|
52
|
-
addopts = "--doctest-modules --ignore=doc/conf.py"
|
|
58
|
+
addopts = "--doctest-modules --ignore=doc/conf.py --ignore=sbml-test-suite"
|
|
@@ -4,30 +4,17 @@ from typing import Union
|
|
|
4
4
|
import libsbml
|
|
5
5
|
import sympy as sp
|
|
6
6
|
|
|
7
|
-
from .cfunction import *
|
|
8
|
-
from .csymbol import *
|
|
9
|
-
from .mathml_parser import SBMLMathMLParser
|
|
10
|
-
from .mathml_printer import SBMLMathMLPrinter
|
|
11
|
-
from .species_symbol import SpeciesSymbol
|
|
12
|
-
|
|
13
|
-
__all__ = [
|
|
14
|
-
"set_math",
|
|
15
|
-
"SBMLMathMLParser",
|
|
16
|
-
"SBMLMathMLPrinter",
|
|
17
|
-
"SpeciesSymbol",
|
|
18
|
-
"TimeSymbol",
|
|
19
|
-
"sympy_to_sbml_math",
|
|
20
|
-
"sbml_math_to_sympy",
|
|
21
|
-
*csymbol.__all__,
|
|
22
|
-
*cfunction.__all__,
|
|
23
|
-
]
|
|
24
|
-
|
|
25
7
|
try:
|
|
26
8
|
__version__ = version("sbmlmath")
|
|
27
9
|
except PackageNotFoundError:
|
|
28
10
|
# package is not installed
|
|
29
11
|
pass
|
|
30
12
|
|
|
13
|
+
#: Default SBML level if not specified otherwise.
|
|
14
|
+
_DEFAULT_SBML_LEVEL = 3
|
|
15
|
+
#: Default SBML version if not specified otherwise.
|
|
16
|
+
_DEFAULT_SBML_VERSION = 2
|
|
17
|
+
|
|
31
18
|
|
|
32
19
|
def sympy_to_sbml_math(sp_expr: sp.Expr) -> libsbml.ASTNode:
|
|
33
20
|
"""Convert sympy expression to SBML math ASTNode.
|
|
@@ -51,12 +38,12 @@ def sympy_to_sbml_math(sp_expr: sp.Expr) -> libsbml.ASTNode:
|
|
|
51
38
|
return ast_node
|
|
52
39
|
|
|
53
40
|
raise ValueError(
|
|
54
|
-
f"Unknown error handling math expression:\n{sp_expr}\n
|
|
41
|
+
f"Unknown error handling math expression:\n{sp_expr}\n{mathml}"
|
|
55
42
|
)
|
|
56
43
|
|
|
57
44
|
|
|
58
45
|
def sbml_math_to_sympy(
|
|
59
|
-
sbml_obj: Union[libsbml.SBase, libsbml.ASTNode],
|
|
46
|
+
sbml_obj: Union[libsbml.SBase, libsbml.ASTNode], **kwargs
|
|
60
47
|
) -> sp.Expr:
|
|
61
48
|
"""Convert SBML MathML to sympy expression.
|
|
62
49
|
|
|
@@ -66,16 +53,32 @@ def sbml_math_to_sympy(
|
|
|
66
53
|
sbml_obj:
|
|
67
54
|
The SBML object to be converted.
|
|
68
55
|
(Either directly the ASTNode or the surrounding SBase object).
|
|
56
|
+
kwargs:
|
|
57
|
+
Additional keyword arguments passed to
|
|
58
|
+
:attr:`SBMLMathMLParser.__init__`.
|
|
69
59
|
Returns:
|
|
70
60
|
The resulting sympy expression.
|
|
71
61
|
"""
|
|
72
|
-
|
|
73
|
-
sbml_obj
|
|
74
|
-
|
|
75
|
-
|
|
62
|
+
if isinstance(sbml_obj, libsbml.ASTNode):
|
|
63
|
+
ast_node = sbml_obj
|
|
64
|
+
sbml_obj = ast_node.getParentSBMLObject()
|
|
65
|
+
else:
|
|
66
|
+
ast_node = sbml_obj.getMath()
|
|
67
|
+
|
|
68
|
+
level = (
|
|
69
|
+
sbml_obj.getLevel() if sbml_obj is not None else _DEFAULT_SBML_LEVEL
|
|
70
|
+
)
|
|
71
|
+
version = (
|
|
72
|
+
sbml_obj.getVersion()
|
|
73
|
+
if sbml_obj is not None
|
|
74
|
+
else _DEFAULT_SBML_VERSION
|
|
76
75
|
)
|
|
77
|
-
mathml = libsbml.
|
|
78
|
-
|
|
76
|
+
mathml = libsbml.writeMathMLWithNamespaceToString(
|
|
77
|
+
ast_node, libsbml.SBMLNamespaces(level, version)
|
|
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(
|
|
@@ -90,15 +93,15 @@ def set_math(
|
|
|
90
93
|
expr:
|
|
91
94
|
The sympy expression to set as the math expression.
|
|
92
95
|
"""
|
|
93
|
-
sbml_document = element.getSBMLDocument()
|
|
94
96
|
mathml = SBMLMathMLPrinter(
|
|
95
|
-
sbml_level=
|
|
96
|
-
sbml_version=
|
|
97
|
+
sbml_level=element.getLevel(),
|
|
98
|
+
sbml_version=element.getVersion(),
|
|
97
99
|
).doprint(expr)
|
|
98
100
|
|
|
99
101
|
if ast_node := libsbml.readMathMLFromString(mathml):
|
|
100
102
|
if element.setMath(ast_node) == libsbml.LIBSBML_OPERATION_SUCCESS:
|
|
101
103
|
return
|
|
104
|
+
sbml_document = element.getSBMLDocument()
|
|
102
105
|
raise ValueError(
|
|
103
106
|
f"Error setting math expression:\n{expr}\n"
|
|
104
107
|
f"{mathml}\n{sbml_document.getErrorLog().toString()}"
|
|
@@ -106,3 +109,22 @@ def set_math(
|
|
|
106
109
|
raise ValueError(
|
|
107
110
|
f"Unknown error parsing math expression:\n{expr}\n{mathml}"
|
|
108
111
|
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
from .cfunction import *
|
|
115
|
+
from .csymbol import *
|
|
116
|
+
from .mathml_parser import SBMLMathMLParser
|
|
117
|
+
from .mathml_printer import SBMLMathMLPrinter
|
|
118
|
+
from .species_symbol import SpeciesSymbol
|
|
119
|
+
|
|
120
|
+
__all__ = [
|
|
121
|
+
"set_math",
|
|
122
|
+
"SBMLMathMLParser",
|
|
123
|
+
"SBMLMathMLPrinter",
|
|
124
|
+
"SpeciesSymbol",
|
|
125
|
+
"TimeSymbol",
|
|
126
|
+
"sympy_to_sbml_math",
|
|
127
|
+
"sbml_math_to_sympy",
|
|
128
|
+
*csymbol.__all__,
|
|
129
|
+
*cfunction.__all__,
|
|
130
|
+
]
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
from sympy import Number
|
|
5
6
|
from sympy.core.function import UndefinedFunction
|
|
6
7
|
|
|
7
8
|
__all__ = ["CFunction", "delay", "rate_of", "Delay", "RateOf"]
|
|
@@ -57,6 +58,8 @@ class CFunction(UndefinedFunction):
|
|
|
57
58
|
assert not hasattr(obj, "encoding")
|
|
58
59
|
obj.definition_url = definition_url
|
|
59
60
|
obj.encoding = encoding
|
|
61
|
+
if hasattr(cls, "eval"):
|
|
62
|
+
obj.eval = cls.eval
|
|
60
63
|
|
|
61
64
|
cls._cache[cache_key] = obj
|
|
62
65
|
|
|
@@ -126,6 +129,8 @@ class RateOf(CFunction):
|
|
|
126
129
|
my_rate_of(a)
|
|
127
130
|
>>> rate_of(a) == my_rate_of(a)
|
|
128
131
|
True
|
|
132
|
+
>>> rate_of(1)
|
|
133
|
+
0
|
|
129
134
|
"""
|
|
130
135
|
|
|
131
136
|
DEFINITION_URL = DEF_URL_RATE_OF
|
|
@@ -133,6 +138,12 @@ class RateOf(CFunction):
|
|
|
133
138
|
def __new__(cls, *args, **kwargs):
|
|
134
139
|
return super().__new__(cls, *args, **kwargs)
|
|
135
140
|
|
|
141
|
+
@classmethod
|
|
142
|
+
def eval(cls, x):
|
|
143
|
+
if isinstance(x, Number):
|
|
144
|
+
# the rate of a constant is 0
|
|
145
|
+
return 0
|
|
146
|
+
|
|
136
147
|
|
|
137
148
|
CFunction.register_subclass(RateOf)
|
|
138
149
|
|
|
@@ -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
|
|
|
@@ -9,8 +9,14 @@ from typing import Union
|
|
|
9
9
|
import sympy as sp
|
|
10
10
|
from lxml import etree
|
|
11
11
|
from pint import UnitRegistry
|
|
12
|
-
from sympy
|
|
13
|
-
|
|
12
|
+
from sympy import Piecewise
|
|
13
|
+
from sympy.logic.boolalg import (
|
|
14
|
+
Boolean,
|
|
15
|
+
BooleanFalse,
|
|
16
|
+
BooleanTrue,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
from . import _DEFAULT_SBML_LEVEL, _DEFAULT_SBML_VERSION
|
|
14
20
|
from .cfunction import CFunction
|
|
15
21
|
from .csymbol import CSymbol
|
|
16
22
|
from .species_symbol import SpeciesSymbol
|
|
@@ -25,6 +31,29 @@ _ureg = UnitRegistry()
|
|
|
25
31
|
_ureg.Quantity.name = property(fget=lambda s: f"({s})")
|
|
26
32
|
_ureg.Quantity_sympy_ = lambda s: sp.sympify(f"{s.m}*{s.u:~}")
|
|
27
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
|
+
|
|
28
57
|
mathml_op_sympy_trigonometric = {
|
|
29
58
|
f"{{{mathml_ns}}}sin": sp.sin,
|
|
30
59
|
f"{{{mathml_ns}}}cos": sp.cos,
|
|
@@ -52,27 +81,40 @@ mathml_op_sympy_trigonometric = {
|
|
|
52
81
|
f"{{{mathml_ns}}}arccoth": sp.acoth,
|
|
53
82
|
}
|
|
54
83
|
mathml_op_sympy_boolean = {
|
|
55
|
-
f"{{{mathml_ns}}}and": sp.And,
|
|
56
|
-
|
|
57
|
-
|
|
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,
|
|
58
94
|
}
|
|
59
95
|
|
|
60
96
|
mathml_op_sympy = {
|
|
61
|
-
|
|
62
|
-
|
|
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
|
|
63
111
|
),
|
|
64
|
-
f"{{{mathml_ns}}}
|
|
65
|
-
|
|
66
|
-
f"{{{mathml_ns}}}eq": lambda *args: sp.Equality(*args, evaluate=False),
|
|
67
|
-
f"{{{mathml_ns}}}neq": lambda *args: sp.Unequality(*args, evaluate=False),
|
|
68
|
-
f"{{{mathml_ns}}}lt": lambda *args: sp.StrictLessThan(
|
|
69
|
-
*args, evaluate=False
|
|
112
|
+
f"{{{mathml_ns}}}geq": lambda *args, **kwargs: sp.GreaterThan(
|
|
113
|
+
*args, **kwargs
|
|
70
114
|
),
|
|
71
|
-
f"{{{mathml_ns}}}
|
|
72
|
-
*args,
|
|
115
|
+
f"{{{mathml_ns}}}leq": lambda *args, **kwargs: sp.LessThan(
|
|
116
|
+
*args, **kwargs
|
|
73
117
|
),
|
|
74
|
-
f"{{{mathml_ns}}}geq": lambda *args: sp.GreaterThan(*args, evaluate=False),
|
|
75
|
-
f"{{{mathml_ns}}}leq": lambda *args: sp.LessThan(*args, evaluate=False),
|
|
76
118
|
f"{{{mathml_ns}}}max": sp.Max,
|
|
77
119
|
f"{{{mathml_ns}}}min": sp.Min,
|
|
78
120
|
f"{{{mathml_ns}}}abs": sp.Abs,
|
|
@@ -83,7 +125,6 @@ mathml_op_sympy = {
|
|
|
83
125
|
f"{{{mathml_ns}}}ln": sp.log,
|
|
84
126
|
f"{{{mathml_ns}}}not": sp.Not,
|
|
85
127
|
f"{{{mathml_ns}}}rem": sp.Mod,
|
|
86
|
-
f"{{{mathml_ns}}}implies": sp.Implies,
|
|
87
128
|
**mathml_op_sympy_trigonometric,
|
|
88
129
|
**mathml_op_sympy_boolean,
|
|
89
130
|
}
|
|
@@ -140,8 +181,9 @@ class SBMLMathMLParser:
|
|
|
140
181
|
|
|
141
182
|
See also: `MathML 2.0 specification <https://www.w3.org/TR/MathML2/>`_.
|
|
142
183
|
|
|
143
|
-
:param
|
|
144
|
-
|
|
184
|
+
:param sbml_level: SBML level.
|
|
185
|
+
Note that SBML level < 3 has not been thoroughly tested.
|
|
186
|
+
:param sbml_version: SBML version.
|
|
145
187
|
:param ureg:
|
|
146
188
|
Optional :class:`pint.UnitRegistry` to use for unit conversion.
|
|
147
189
|
:param floats_as_rationals:
|
|
@@ -160,28 +202,28 @@ class SBMLMathMLParser:
|
|
|
160
202
|
|
|
161
203
|
def __init__(
|
|
162
204
|
self,
|
|
163
|
-
|
|
164
|
-
|
|
205
|
+
sbml_level: Union[int, str] = _DEFAULT_SBML_LEVEL,
|
|
206
|
+
sbml_version: Union[int, str] = _DEFAULT_SBML_VERSION,
|
|
165
207
|
ureg: UnitRegistry = None,
|
|
166
208
|
floats_as_rationals=True,
|
|
167
209
|
ignore_units=False,
|
|
168
210
|
symbol_kwargs=None,
|
|
211
|
+
evaluate=False,
|
|
169
212
|
):
|
|
170
213
|
"""Constructor"""
|
|
171
214
|
self.ureg = ureg or _ureg or UnitRegistry()
|
|
172
|
-
self.sbml_core_ns =
|
|
173
|
-
f"http://www.sbml.org/sbml/level{level}/version{version}/core"
|
|
174
|
-
)
|
|
215
|
+
self.sbml_core_ns = f"http://www.sbml.org/sbml/level{sbml_level}/version{sbml_version}/core"
|
|
175
216
|
# TODO {prefix=>url}
|
|
176
217
|
self.sbml_multi_ns = (
|
|
177
218
|
# L3V2 doesn't work
|
|
178
|
-
f"http://www.sbml.org/sbml/level{
|
|
219
|
+
f"http://www.sbml.org/sbml/level{sbml_level}/version1/multi/version1"
|
|
179
220
|
)
|
|
180
221
|
self.floats_as_rationals = floats_as_rationals
|
|
181
222
|
self.ignore_units = ignore_units
|
|
182
223
|
self.symbol_kwargs = (
|
|
183
224
|
{} if symbol_kwargs is None else symbol_kwargs.copy()
|
|
184
225
|
)
|
|
226
|
+
self.evaluate = evaluate
|
|
185
227
|
|
|
186
228
|
def parse_file(self, file_like) -> sp.Expr:
|
|
187
229
|
"""Parse a file-like object containing MathML.
|
|
@@ -280,7 +322,7 @@ class SBMLMathMLParser:
|
|
|
280
322
|
and len(operands) >= 3
|
|
281
323
|
):
|
|
282
324
|
# n-ary relational operators:
|
|
283
|
-
# a < b < c cannot
|
|
325
|
+
# a < b < c cannot directly be represented in sympy.
|
|
284
326
|
# either change to `a < b and b < c`, to be sympy-evaluatable,
|
|
285
327
|
# or to Function('MathML_Lt')(a, b, c), which will cycle
|
|
286
328
|
# (although potentially collide with other functions with
|
|
@@ -293,7 +335,11 @@ class SBMLMathMLParser:
|
|
|
293
335
|
for i in range(len(sym_operands) - 1):
|
|
294
336
|
expr = sp.And(
|
|
295
337
|
expr,
|
|
296
|
-
sym_operator(
|
|
338
|
+
sym_operator(
|
|
339
|
+
sym_operands[i],
|
|
340
|
+
sym_operands[i + 1],
|
|
341
|
+
evaluate=self.evaluate,
|
|
342
|
+
),
|
|
297
343
|
)
|
|
298
344
|
return expr
|
|
299
345
|
|
|
@@ -306,18 +352,27 @@ class SBMLMathMLParser:
|
|
|
306
352
|
sym_operands = list(map(_bool2num, sym_operands))
|
|
307
353
|
|
|
308
354
|
with contextlib.suppress(KeyError):
|
|
309
|
-
return mathml_op_sympy[operator.tag](
|
|
355
|
+
return mathml_op_sympy[operator.tag](
|
|
356
|
+
*sym_operands, evaluate=self.evaluate
|
|
357
|
+
)
|
|
310
358
|
|
|
311
359
|
if operator.tag == f"{{{mathml_ns}}}plus":
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
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)
|
|
315
369
|
|
|
316
370
|
if operator.tag == f"{{{mathml_ns}}}minus":
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
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]
|
|
321
376
|
raise AssertionError(list(element))
|
|
322
377
|
|
|
323
378
|
if operator.tag == f"{{{mathml_ns}}}root":
|
|
@@ -329,7 +384,8 @@ class SBMLMathMLParser:
|
|
|
329
384
|
degree = sym_operands[0]
|
|
330
385
|
assert operands[0].tag == f"{{{mathml_ns}}}degree"
|
|
331
386
|
|
|
332
|
-
|
|
387
|
+
with sp.evaluate(self.evaluate):
|
|
388
|
+
return sym_operands[-1] ** (1 / degree)
|
|
333
389
|
|
|
334
390
|
if operator.tag == f"{{{mathml_ns}}}log":
|
|
335
391
|
assert len(operands) == 2
|
|
@@ -337,7 +393,7 @@ class SBMLMathMLParser:
|
|
|
337
393
|
assert len(operands[0]) == 1
|
|
338
394
|
base, x = sym_operands
|
|
339
395
|
# won't cycle - will be transformed to ``log(x)/log(base)``
|
|
340
|
-
return sp.log(x, base)
|
|
396
|
+
return sp.log(x, base, evaluate=self.evaluate)
|
|
341
397
|
|
|
342
398
|
if operator.tag == f"{{{mathml_ns}}}quotient":
|
|
343
399
|
# there is no direct correspondence for integer division in sympy,
|
|
@@ -345,7 +401,8 @@ class SBMLMathMLParser:
|
|
|
345
401
|
# a // b = (a - a mod b) / b
|
|
346
402
|
assert len(operands) == 2
|
|
347
403
|
a, b = sym_operands
|
|
348
|
-
|
|
404
|
+
with sp.evaluate(self.evaluate):
|
|
405
|
+
return (a - a % b) / b
|
|
349
406
|
|
|
350
407
|
name = self.preprocess_symbol_name(operator.text.strip(), operator)
|
|
351
408
|
|
|
@@ -454,15 +511,16 @@ class SBMLMathMLParser:
|
|
|
454
511
|
expr_cond_pairs = []
|
|
455
512
|
for e in element:
|
|
456
513
|
# TODO: cannot currently handle functions as condition-arguments.
|
|
457
|
-
# 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
|
|
458
517
|
if e.tag == f"{{{mathml_ns}}}piece":
|
|
459
518
|
assert len(e) == 2
|
|
460
519
|
cond = self._parse_element(e[1])
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
)
|
|
520
|
+
# Only Boolean conditions are supported
|
|
521
|
+
# -> convert
|
|
522
|
+
# this won't round-trip
|
|
523
|
+
cond = _num2bool(cond)
|
|
466
524
|
expr_cond_pairs.append(
|
|
467
525
|
(self._parse_element(e[0]), cond),
|
|
468
526
|
)
|
|
@@ -475,7 +533,9 @@ class SBMLMathMLParser:
|
|
|
475
533
|
)
|
|
476
534
|
else:
|
|
477
535
|
raise AssertionError(e.tag)
|
|
478
|
-
|
|
536
|
+
|
|
537
|
+
with sp.evaluate(self.evaluate):
|
|
538
|
+
return sp.Piecewise(*expr_cond_pairs)
|
|
479
539
|
|
|
480
540
|
def handle_lambda(self, element: etree._Element) -> sp.Expr:
|
|
481
541
|
# See https://www.w3.org/TR/MathML2/chapter4.html#id.4.2.1.7
|
|
@@ -523,10 +583,32 @@ class SBMLMathMLParser:
|
|
|
523
583
|
return name
|
|
524
584
|
|
|
525
585
|
|
|
526
|
-
def _bool2num(x):
|
|
527
|
-
"""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
|
+
"""
|
|
528
591
|
if isinstance(x, BooleanFalse):
|
|
529
592
|
return sp.Integer(0)
|
|
530
593
|
if isinstance(x, BooleanTrue):
|
|
531
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))
|
|
532
614
|
return x
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"""Convenience functions for libsbml core"""
|
|
2
2
|
|
|
3
|
+
import warnings
|
|
3
4
|
from numbers import Number
|
|
4
5
|
|
|
5
6
|
import sympy as sp
|
|
6
7
|
from sympy.printing.mathml import MathMLContentPrinter
|
|
7
8
|
|
|
9
|
+
from . import _DEFAULT_SBML_LEVEL, _DEFAULT_SBML_VERSION
|
|
8
10
|
from .csymbol import CSymbol
|
|
9
11
|
from .species_symbol import SpeciesSymbol
|
|
10
12
|
|
|
@@ -26,8 +28,8 @@ class SBMLMathMLPrinter(MathMLContentPrinter):
|
|
|
26
28
|
self,
|
|
27
29
|
*args,
|
|
28
30
|
literals_dimensionless=True,
|
|
29
|
-
sbml_level: int =
|
|
30
|
-
sbml_version: int =
|
|
31
|
+
sbml_level: int = _DEFAULT_SBML_LEVEL,
|
|
32
|
+
sbml_version: int = _DEFAULT_SBML_VERSION,
|
|
31
33
|
**kwargs,
|
|
32
34
|
):
|
|
33
35
|
"""Construct.
|
|
@@ -37,6 +39,13 @@ class SBMLMathMLPrinter(MathMLContentPrinter):
|
|
|
37
39
|
"""
|
|
38
40
|
super().__init__(*args, **kwargs)
|
|
39
41
|
|
|
42
|
+
if sbml_level < 3:
|
|
43
|
+
warnings.warn(
|
|
44
|
+
"SBML level < 3 has not been thoroughly tested.",
|
|
45
|
+
UserWarning,
|
|
46
|
+
stacklevel=2,
|
|
47
|
+
)
|
|
48
|
+
|
|
40
49
|
self.mathml_ns = 'xmlns="http://www.w3.org/1998/Math/MathML"'
|
|
41
50
|
self.sbml_ns = f'xmlns:sbml="http://www.sbml.org/sbml/level{sbml_level}/version{sbml_version}/core"'
|
|
42
51
|
# note: so far, there is no L3V2-multi
|
|
@@ -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,15 @@ 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
|
+
|
|
29
|
+
|
|
30
|
+
`sbmlmath` is a Python library for interconverting [SymPy](https://github.com/sympy/sympy/)
|
|
26
31
|
expressions and [SBML](https://sbml.org/)-MathML.
|
|
27
32
|
SBML uses a subset of [MathML](https://www.w3.org/Math/) that this library
|
|
28
33
|
aims to support. This is not (intended to be) a general MathML parser.
|
|
@@ -33,7 +38,7 @@ Main functionality:
|
|
|
33
38
|
* SBML MathML -> sympy
|
|
34
39
|
* in particular for cases where `sympy.sympify(libsbml.formulaToL3String(...))`
|
|
35
40
|
won't do the job
|
|
36
|
-
(mind the differences operator precedence between
|
|
41
|
+
(mind, e.g., the differences in operator precedence between
|
|
37
42
|
sympy/[Python](https://docs.python.org/3/reference/expressions.html#operator-precedence)
|
|
38
43
|
and [libsbml L3 formulas](https://synonym.caltech.edu/software/libsbml/5.18.0/docs/formatted/python-api/libsbml-math.html#math-l3)!)
|
|
39
44
|
* retaining unit annotations and other `<ci>` attributes
|
|
@@ -70,5 +75,5 @@ pip install sbmlmath
|
|
|
70
75
|
|
|
71
76
|
The latest development version from GitHub:
|
|
72
77
|
```bash
|
|
73
|
-
pip install https://github.com/dweindl/sbmlmath
|
|
78
|
+
pip install git+https://github.com/dweindl/sbmlmath.git#egg=sbmlmath
|
|
74
79
|
```
|
|
@@ -57,3 +57,13 @@ def test_cfunction():
|
|
|
57
57
|
|
|
58
58
|
assert UndefinedFunction(rate_of.name) != rate_of
|
|
59
59
|
assert UndefinedFunction(rate_of.name)(a).has(rate_of) is False
|
|
60
|
+
|
|
61
|
+
assert rate_of(1) == 0
|
|
62
|
+
assert rate_of(1, evaluate=False).has(rate_of) is True
|
|
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)
|
|
@@ -16,7 +16,7 @@ if not sbml_test_suite_root:
|
|
|
16
16
|
pytest.skip("$SBML_TEST_SUITE_ROOT not set", allow_module_level=True)
|
|
17
17
|
|
|
18
18
|
cases_root = Path(sbml_test_suite_root, "cases", "semantic")
|
|
19
|
-
cases = sorted(cases_root.rglob("*-sbml-
|
|
19
|
+
cases = sorted(cases_root.rglob("*-sbml-l3v?.xml"))
|
|
20
20
|
assert len(cases)
|
|
21
21
|
|
|
22
22
|
|
|
@@ -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
|