sbmlmath 0.1.1__tar.gz → 0.2.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.1.1 → sbmlmath-0.2.0}/.github/workflows/ci.yaml +3 -0
- sbmlmath-0.2.0/.pre-commit-config.yaml +26 -0
- {sbmlmath-0.1.1/sbmlmath.egg-info → sbmlmath-0.2.0}/PKG-INFO +7 -2
- sbmlmath-0.2.0/pyproject.toml +52 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/sbmlmath/__init__.py +39 -1
- sbmlmath-0.2.0/sbmlmath/cfunction.py +145 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/sbmlmath/csymbol.py +22 -3
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/sbmlmath/mathml_parser.py +10 -4
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/sbmlmath/mathml_printer.py +4 -1
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/sbmlmath/species_symbol.py +1 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0/sbmlmath.egg-info}/PKG-INFO +7 -2
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/sbmlmath.egg-info/requires.txt +1 -1
- sbmlmath-0.2.0/tests/test_csymbol.py +59 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/tests/test_mathml_parser_sbml_semantic_suite.py +4 -1
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/tests/test_misc.py +24 -3
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/tests/test_species_symbol.py +3 -1
- sbmlmath-0.1.1/.pre-commit-config.yaml +0 -25
- sbmlmath-0.1.1/pyproject.toml +0 -29
- sbmlmath-0.1.1/sbmlmath/cfunction.py +0 -61
- sbmlmath-0.1.1/tests/test_csymbol.py +0 -13
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/.github/dependabot.yml +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/.github/workflows/deploy_release.yaml +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/.gitignore +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/.readthedocs.yml +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/LICENSE +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/README.md +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/doc/.gitignore +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/doc/Makefile +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/doc/about.rst +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/doc/api.rst +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/doc/conf.py +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/doc/index.rst +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/doc/make.bat +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/doc/rtd_requirements.txt +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/sbmlmath.egg-info/SOURCES.txt +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/sbmlmath.egg-info/dependency_links.txt +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/sbmlmath.egg-info/top_level.txt +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/setup.cfg +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/tests/test_parser.py +0 -0
- {sbmlmath-0.1.1 → sbmlmath-0.2.0}/tests/test_printer.py +0 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# See https://pre-commit.com for more information
|
|
2
|
+
# See https://pre-commit.com/hooks.html for more hooks
|
|
3
|
+
repos:
|
|
4
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
5
|
+
rev: v5.0.0
|
|
6
|
+
hooks:
|
|
7
|
+
- id: trailing-whitespace
|
|
8
|
+
- id: end-of-file-fixer
|
|
9
|
+
- id: check-yaml
|
|
10
|
+
- id: check-added-large-files
|
|
11
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
12
|
+
# Ruff version.
|
|
13
|
+
rev: v0.6.9
|
|
14
|
+
hooks:
|
|
15
|
+
# Run the linter.
|
|
16
|
+
- id: ruff
|
|
17
|
+
args:
|
|
18
|
+
- --fix
|
|
19
|
+
- --config
|
|
20
|
+
- pyproject.toml
|
|
21
|
+
|
|
22
|
+
# Run the formatter.
|
|
23
|
+
- id: ruff-format
|
|
24
|
+
args:
|
|
25
|
+
- --config
|
|
26
|
+
- pyproject.toml
|
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: sbmlmath
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: SBML Math <-> SymPy
|
|
5
5
|
Author-email: Daniel Weindl <sci@danielweindl.de>
|
|
6
6
|
License: BSD-3-Clause
|
|
7
|
+
Project-URL: Homepage, https://github.com/dweindl/sbmlmath
|
|
8
|
+
Project-URL: Documentation, https://sbmlmath.readthedocs.io/
|
|
9
|
+
Project-URL: Repository, https://github.com/dweindl/sbmlmath.git
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/dweindl/sbmlmath/issues
|
|
11
|
+
Project-URL: PyPI, https://pypi.org/project/sbmlmath/
|
|
7
12
|
Requires-Python: >=3.9
|
|
8
13
|
Description-Content-Type: text/markdown
|
|
9
14
|
License-File: LICENSE
|
|
10
|
-
Requires-Dist: python-libsbml
|
|
15
|
+
Requires-Dist: python-libsbml>=5.20.4
|
|
11
16
|
Requires-Dist: sympy
|
|
12
17
|
Requires-Dist: pint
|
|
13
18
|
Requires-Dist: lxml>=4.6.4
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=64", "setuptools-scm>=8"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sbmlmath"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
authors = [
|
|
9
|
+
{name = "Daniel Weindl", email = "sci@danielweindl.de"},
|
|
10
|
+
]
|
|
11
|
+
description = "SBML Math <-> SymPy"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.9"
|
|
14
|
+
license = {text = "BSD-3-Clause"}
|
|
15
|
+
dependencies = [
|
|
16
|
+
"python-libsbml>=5.20.4",
|
|
17
|
+
"sympy",
|
|
18
|
+
"pint",
|
|
19
|
+
"lxml>=4.6.4",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.urls]
|
|
23
|
+
Homepage = "https://github.com/dweindl/sbmlmath"
|
|
24
|
+
Documentation = "https://sbmlmath.readthedocs.io/"
|
|
25
|
+
Repository = "https://github.com/dweindl/sbmlmath.git"
|
|
26
|
+
"Bug Tracker" = "https://github.com/dweindl/sbmlmath/issues"
|
|
27
|
+
PyPI = "https://pypi.org/project/sbmlmath/"
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
test = ["pytest>=7", "pre-commit>=3"]
|
|
31
|
+
|
|
32
|
+
[tool.setuptools_scm]
|
|
33
|
+
|
|
34
|
+
[tool.ruff]
|
|
35
|
+
line-length = 79
|
|
36
|
+
exclude = ["sbml-test-suite"]
|
|
37
|
+
lint.extend-select = [
|
|
38
|
+
"T", "W", "E", "F", "B", "C", "S", "I", "UP",
|
|
39
|
+
# "D",
|
|
40
|
+
# "ANN",
|
|
41
|
+
]
|
|
42
|
+
lint.ignore = [
|
|
43
|
+
"S101", # Use of assert detected
|
|
44
|
+
"F405", # star import
|
|
45
|
+
"F403", # star import
|
|
46
|
+
"E501", # Line too long
|
|
47
|
+
]
|
|
48
|
+
[tool.ruff.lint.per-file-ignores]
|
|
49
|
+
"tests/*" = ["T201"] # print statement
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
addopts = "--doctest-modules --ignore=doc/conf.py"
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
1
2
|
from typing import Union
|
|
2
3
|
|
|
3
4
|
import libsbml
|
|
@@ -10,6 +11,7 @@ from .mathml_printer import SBMLMathMLPrinter
|
|
|
10
11
|
from .species_symbol import SpeciesSymbol
|
|
11
12
|
|
|
12
13
|
__all__ = [
|
|
14
|
+
"set_math",
|
|
13
15
|
"SBMLMathMLParser",
|
|
14
16
|
"SBMLMathMLPrinter",
|
|
15
17
|
"SpeciesSymbol",
|
|
@@ -20,6 +22,12 @@ __all__ = [
|
|
|
20
22
|
*cfunction.__all__,
|
|
21
23
|
]
|
|
22
24
|
|
|
25
|
+
try:
|
|
26
|
+
__version__ = version("sbmlmath")
|
|
27
|
+
except PackageNotFoundError:
|
|
28
|
+
# package is not installed
|
|
29
|
+
pass
|
|
30
|
+
|
|
23
31
|
|
|
24
32
|
def sympy_to_sbml_math(sp_expr: sp.Expr) -> libsbml.ASTNode:
|
|
25
33
|
"""Convert sympy expression to SBML math ASTNode.
|
|
@@ -48,7 +56,7 @@ def sympy_to_sbml_math(sp_expr: sp.Expr) -> libsbml.ASTNode:
|
|
|
48
56
|
|
|
49
57
|
|
|
50
58
|
def sbml_math_to_sympy(
|
|
51
|
-
sbml_obj: Union[libsbml.SBase, libsbml.ASTNode]
|
|
59
|
+
sbml_obj: Union[libsbml.SBase, libsbml.ASTNode],
|
|
52
60
|
) -> sp.Expr:
|
|
53
61
|
"""Convert SBML MathML to sympy expression.
|
|
54
62
|
|
|
@@ -68,3 +76,33 @@ def sbml_math_to_sympy(
|
|
|
68
76
|
)
|
|
69
77
|
mathml = libsbml.writeMathMLToString(ast_node)
|
|
70
78
|
return SBMLMathMLParser().parse_str(mathml)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def set_math(
|
|
82
|
+
element: libsbml.SBase,
|
|
83
|
+
expr: sp.Expr,
|
|
84
|
+
) -> None:
|
|
85
|
+
"""Set the math expression of an SBML object.
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
element:
|
|
89
|
+
The SBML object to set the math expression for.
|
|
90
|
+
expr:
|
|
91
|
+
The sympy expression to set as the math expression.
|
|
92
|
+
"""
|
|
93
|
+
sbml_document = element.getSBMLDocument()
|
|
94
|
+
mathml = SBMLMathMLPrinter(
|
|
95
|
+
sbml_level=sbml_document.getLevel(),
|
|
96
|
+
sbml_version=sbml_document.getVersion(),
|
|
97
|
+
).doprint(expr)
|
|
98
|
+
|
|
99
|
+
if ast_node := libsbml.readMathMLFromString(mathml):
|
|
100
|
+
if element.setMath(ast_node) == libsbml.LIBSBML_OPERATION_SUCCESS:
|
|
101
|
+
return
|
|
102
|
+
raise ValueError(
|
|
103
|
+
f"Error setting math expression:\n{expr}\n"
|
|
104
|
+
f"{mathml}\n{sbml_document.getErrorLog().toString()}"
|
|
105
|
+
)
|
|
106
|
+
raise ValueError(
|
|
107
|
+
f"Unknown error parsing math expression:\n{expr}\n{mathml}"
|
|
108
|
+
)
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""Handling of ``<csymbol>`` functions"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from sympy.core.function import UndefinedFunction
|
|
6
|
+
|
|
7
|
+
__all__ = ["CFunction", "delay", "rate_of", "Delay", "RateOf"]
|
|
8
|
+
|
|
9
|
+
DEF_URL_BASE = "http://www.sbml.org/sbml/symbols/"
|
|
10
|
+
DEF_URL_RATE_OF = DEF_URL_BASE + "rateOf"
|
|
11
|
+
DEF_URL_DELAY = DEF_URL_BASE + "delay"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class CFunction(UndefinedFunction):
|
|
15
|
+
"""
|
|
16
|
+
Represents ``<csymbol>`` functions.
|
|
17
|
+
|
|
18
|
+
Examples: :func:`rateOf() <rate_of>`, :func:`delay`,
|
|
19
|
+
distributions from the `distrib package
|
|
20
|
+
<https://synonym.caltech.edu/documents/specifications/level-3/version-1/distrib/>`_.
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
See also https://www.w3.org/TR/MathML2/chapter4.html#contm.deffun.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
DEFINITION_URL = None
|
|
27
|
+
_cache = {}
|
|
28
|
+
_definition_url_to_derived_class = {}
|
|
29
|
+
|
|
30
|
+
def __new__(
|
|
31
|
+
cls: type[CFunction],
|
|
32
|
+
*args,
|
|
33
|
+
definition_url: str = None,
|
|
34
|
+
encoding: str = "text",
|
|
35
|
+
**kwargs,
|
|
36
|
+
):
|
|
37
|
+
definition_url = definition_url or cls.DEFINITION_URL
|
|
38
|
+
if definition_url is None:
|
|
39
|
+
raise ValueError("definition_url must be provided")
|
|
40
|
+
|
|
41
|
+
# Cache instances.
|
|
42
|
+
# If not done: (CFunction("A", definition_url="x")
|
|
43
|
+
# == CFunction("A", definition_url="y")) == False
|
|
44
|
+
if not (name := kwargs.get("name")):
|
|
45
|
+
if not len(args):
|
|
46
|
+
raise ValueError("name argument must be provided")
|
|
47
|
+
name = args[0]
|
|
48
|
+
|
|
49
|
+
cache_key = (name, definition_url, encoding)
|
|
50
|
+
if cached := cls._cache.get(cache_key):
|
|
51
|
+
return cached
|
|
52
|
+
|
|
53
|
+
obj = super().__new__(cls, *args, **kwargs)
|
|
54
|
+
|
|
55
|
+
# ensure there are no collisions with super() attributes
|
|
56
|
+
assert not hasattr(obj, "definition_url")
|
|
57
|
+
assert not hasattr(obj, "encoding")
|
|
58
|
+
obj.definition_url = definition_url
|
|
59
|
+
obj.encoding = encoding
|
|
60
|
+
|
|
61
|
+
cls._cache[cache_key] = obj
|
|
62
|
+
|
|
63
|
+
return obj
|
|
64
|
+
|
|
65
|
+
def __eq__(self, other):
|
|
66
|
+
if not isinstance(other, CFunction):
|
|
67
|
+
return False
|
|
68
|
+
|
|
69
|
+
# if they represent the same value, they are equal
|
|
70
|
+
return self.definition_url == other.definition_url
|
|
71
|
+
|
|
72
|
+
def __hash__(self):
|
|
73
|
+
return hash(
|
|
74
|
+
(
|
|
75
|
+
self.__class__.__name__,
|
|
76
|
+
self.DEFINITION_URL,
|
|
77
|
+
self.definition_url,
|
|
78
|
+
self.name,
|
|
79
|
+
)
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
@classmethod
|
|
83
|
+
def register_subclass(cls, derived_class: type[CFunction]):
|
|
84
|
+
cls._definition_url_to_derived_class[derived_class.DEFINITION_URL] = (
|
|
85
|
+
derived_class
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
# Derived classes for specific SBML functions
|
|
90
|
+
class Delay(CFunction):
|
|
91
|
+
"""Produces a SBML ``delay()`` function.
|
|
92
|
+
|
|
93
|
+
Usually, it's preferable to use the :func:`delay` function.
|
|
94
|
+
This class can be used if a *delay* function with a different name is
|
|
95
|
+
needed.
|
|
96
|
+
|
|
97
|
+
Examples:
|
|
98
|
+
>>> from sympy.abc import a
|
|
99
|
+
>>> my_delay = Delay("my_delay")
|
|
100
|
+
>>> my_delay(a)
|
|
101
|
+
my_delay(a)
|
|
102
|
+
>>> delay(a) == my_delay(a)
|
|
103
|
+
True
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
DEFINITION_URL = DEF_URL_DELAY
|
|
107
|
+
|
|
108
|
+
def __new__(cls, *args, **kwargs):
|
|
109
|
+
return super().__new__(cls, *args, **kwargs)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
CFunction.register_subclass(Delay)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class RateOf(CFunction):
|
|
116
|
+
"""Produces a SBML ``rateOf()`` function.
|
|
117
|
+
|
|
118
|
+
Usually, it's preferable to use the :func:`rate_of` function.
|
|
119
|
+
This class can be used if a *rateOf* function with a different name is
|
|
120
|
+
needed.
|
|
121
|
+
|
|
122
|
+
Examples:
|
|
123
|
+
>>> from sympy.abc import a
|
|
124
|
+
>>> my_rate_of = RateOf("my_rate_of")
|
|
125
|
+
>>> my_rate_of(a)
|
|
126
|
+
my_rate_of(a)
|
|
127
|
+
>>> rate_of(a) == my_rate_of(a)
|
|
128
|
+
True
|
|
129
|
+
"""
|
|
130
|
+
|
|
131
|
+
DEFINITION_URL = DEF_URL_RATE_OF
|
|
132
|
+
|
|
133
|
+
def __new__(cls, *args, **kwargs):
|
|
134
|
+
return super().__new__(cls, *args, **kwargs)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
CFunction.register_subclass(RateOf)
|
|
138
|
+
|
|
139
|
+
# SBML-defined functions
|
|
140
|
+
|
|
141
|
+
#: The SBML ``delay()`` function.
|
|
142
|
+
delay = Delay("delay")
|
|
143
|
+
|
|
144
|
+
#: The SBML ``rateOf()`` function.
|
|
145
|
+
rate_of = RateOf("rateOf")
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"""Handling of <csymbol> constants"""
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
3
4
|
|
|
4
5
|
import sympy as sp
|
|
5
6
|
|
|
@@ -30,7 +31,9 @@ class CSymbol(sp.Dummy):
|
|
|
30
31
|
See also https://www.w3.org/TR/MathML2/chapter4.html#contm.csymbol.
|
|
31
32
|
"""
|
|
32
33
|
|
|
34
|
+
DEFINITION_URL = None
|
|
33
35
|
_cache = {}
|
|
36
|
+
_definition_url_to_derived_class = {}
|
|
34
37
|
|
|
35
38
|
def __new__(
|
|
36
39
|
cls,
|
|
@@ -47,7 +50,12 @@ class CSymbol(sp.Dummy):
|
|
|
47
50
|
if cached := cls._cache.get(cache_key):
|
|
48
51
|
return cached
|
|
49
52
|
|
|
50
|
-
|
|
53
|
+
# return the matching subtype, depending on the definition URL
|
|
54
|
+
obj = super().__new__(
|
|
55
|
+
cls._definition_url_to_derived_class.get(definition_url, cls),
|
|
56
|
+
*args,
|
|
57
|
+
**kwargs,
|
|
58
|
+
)
|
|
51
59
|
|
|
52
60
|
# ensure there are no collisions with super() attributes
|
|
53
61
|
assert not hasattr(obj, "definition_url")
|
|
@@ -79,6 +87,12 @@ class CSymbol(sp.Dummy):
|
|
|
79
87
|
return SBML_L3V2_AVOGADRO_VALUE
|
|
80
88
|
return super().__float__()
|
|
81
89
|
|
|
90
|
+
@classmethod
|
|
91
|
+
def register_subclass(cls, derived_class: type[CSymbol]):
|
|
92
|
+
cls._definition_url_to_derived_class[derived_class.DEFINITION_URL] = (
|
|
93
|
+
derived_class
|
|
94
|
+
)
|
|
95
|
+
|
|
82
96
|
|
|
83
97
|
class TimeSymbol(CSymbol):
|
|
84
98
|
"""The current internal simulation time.
|
|
@@ -92,6 +106,8 @@ class TimeSymbol(CSymbol):
|
|
|
92
106
|
name: The name of the symbol.
|
|
93
107
|
"""
|
|
94
108
|
|
|
109
|
+
DEFINITION_URL = "http://www.sbml.org/sbml/symbols/time"
|
|
110
|
+
|
|
95
111
|
def __new__(
|
|
96
112
|
cls,
|
|
97
113
|
name: str,
|
|
@@ -99,5 +115,8 @@ class TimeSymbol(CSymbol):
|
|
|
99
115
|
return super().__new__(
|
|
100
116
|
cls,
|
|
101
117
|
name=name,
|
|
102
|
-
definition_url=
|
|
118
|
+
definition_url=cls.DEFINITION_URL,
|
|
103
119
|
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
CSymbol.register_subclass(TimeSymbol)
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"""SBML mathml to sympy."""
|
|
2
|
+
|
|
2
3
|
import contextlib
|
|
3
4
|
import operator as operators
|
|
4
5
|
from functools import reduce
|
|
@@ -191,7 +192,9 @@ class SBMLMathMLParser:
|
|
|
191
192
|
and the MathML ``math`` element.
|
|
192
193
|
:return: The sympy representation of the MathML expression.
|
|
193
194
|
"""
|
|
194
|
-
|
|
195
|
+
# Using `lxml` to parse untrusted data is known to be vulnerable to XML
|
|
196
|
+
# attacks
|
|
197
|
+
element_tree = etree.parse(file_like) # noqa S320
|
|
195
198
|
for element in element_tree.iter():
|
|
196
199
|
if element.tag == f"{{{mathml_ns}}}math":
|
|
197
200
|
continue
|
|
@@ -240,7 +243,7 @@ class SBMLMathMLParser:
|
|
|
240
243
|
|
|
241
244
|
raise NotImplementedError(f"Unhandled element: {element.tag}.")
|
|
242
245
|
|
|
243
|
-
def handle_apply(self, element: etree._Element) -> sp.Expr:
|
|
246
|
+
def handle_apply(self, element: etree._Element) -> sp.Expr: # noqa C901
|
|
244
247
|
"""Handle <apply>"""
|
|
245
248
|
operator, *operands = element
|
|
246
249
|
sym_operands = list(map(self._parse_element, operands))
|
|
@@ -289,7 +292,8 @@ class SBMLMathMLParser:
|
|
|
289
292
|
expr = True
|
|
290
293
|
for i in range(len(sym_operands) - 1):
|
|
291
294
|
expr = sp.And(
|
|
292
|
-
expr,
|
|
295
|
+
expr,
|
|
296
|
+
sym_operator(sym_operands[i], sym_operands[i + 1]),
|
|
293
297
|
)
|
|
294
298
|
return expr
|
|
295
299
|
|
|
@@ -383,7 +387,9 @@ class SBMLMathMLParser:
|
|
|
383
387
|
species_reference = element.attrib.get(
|
|
384
388
|
f"{{{self.sbml_multi_ns}}}speciesReference", None
|
|
385
389
|
) or element.attrib.get("multi:speciesReference", None)
|
|
386
|
-
symbol_name = self.preprocess_symbol_name(
|
|
390
|
+
symbol_name = self.preprocess_symbol_name(
|
|
391
|
+
element.text.strip(), element
|
|
392
|
+
)
|
|
387
393
|
if representation_type or species_reference:
|
|
388
394
|
return SpeciesSymbol(
|
|
389
395
|
name=symbol_name,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"""Convenience functions for libsbml core"""
|
|
2
|
+
|
|
2
3
|
from numbers import Number
|
|
3
4
|
|
|
4
5
|
import sympy as sp
|
|
@@ -56,7 +57,9 @@ class SBMLMathMLPrinter(MathMLContentPrinter):
|
|
|
56
57
|
def _print_SpeciesSymbol(self, sym: SpeciesSymbol):
|
|
57
58
|
ci = self._print_Symbol(sym)
|
|
58
59
|
if sym.representation_type:
|
|
59
|
-
ci.setAttribute(
|
|
60
|
+
ci.setAttribute(
|
|
61
|
+
"multi:representationType", sym.representation_type
|
|
62
|
+
)
|
|
60
63
|
if sym.species_reference:
|
|
61
64
|
ci.setAttribute("multi:speciesReference", sym.species_reference)
|
|
62
65
|
return ci
|
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: sbmlmath
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: SBML Math <-> SymPy
|
|
5
5
|
Author-email: Daniel Weindl <sci@danielweindl.de>
|
|
6
6
|
License: BSD-3-Clause
|
|
7
|
+
Project-URL: Homepage, https://github.com/dweindl/sbmlmath
|
|
8
|
+
Project-URL: Documentation, https://sbmlmath.readthedocs.io/
|
|
9
|
+
Project-URL: Repository, https://github.com/dweindl/sbmlmath.git
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/dweindl/sbmlmath/issues
|
|
11
|
+
Project-URL: PyPI, https://pypi.org/project/sbmlmath/
|
|
7
12
|
Requires-Python: >=3.9
|
|
8
13
|
Description-Content-Type: text/markdown
|
|
9
14
|
License-File: LICENSE
|
|
10
|
-
Requires-Dist: python-libsbml
|
|
15
|
+
Requires-Dist: python-libsbml>=5.20.4
|
|
11
16
|
Requires-Dist: sympy
|
|
12
17
|
Requires-Dist: pint
|
|
13
18
|
Requires-Dist: lxml>=4.6.4
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import sympy as sp
|
|
2
|
+
from sympy.core.function import UndefinedFunction
|
|
3
|
+
|
|
4
|
+
from sbmlmath.cfunction import *
|
|
5
|
+
from sbmlmath.cfunction import DEF_URL_DELAY, DEF_URL_RATE_OF
|
|
6
|
+
from sbmlmath.csymbol import *
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def test_time_symbol():
|
|
10
|
+
assert TimeSymbol("t") is TimeSymbol("t")
|
|
11
|
+
|
|
12
|
+
assert TimeSymbol("t") == TimeSymbol("time")
|
|
13
|
+
assert TimeSymbol("t") is not TimeSymbol("time")
|
|
14
|
+
|
|
15
|
+
t1, t2 = TimeSymbol("t"), TimeSymbol("time")
|
|
16
|
+
eight_t = t1 + t2 + 3 * t1 + 3 * t2
|
|
17
|
+
# is the result defined?
|
|
18
|
+
assert eight_t in [8 * t1, 8 * t2]
|
|
19
|
+
|
|
20
|
+
# test with a non-cached symbol (i.e. a new name)
|
|
21
|
+
assert isinstance(
|
|
22
|
+
CSymbol(
|
|
23
|
+
"new_name_for_time",
|
|
24
|
+
definition_url=TimeSymbol.DEFINITION_URL,
|
|
25
|
+
),
|
|
26
|
+
TimeSymbol,
|
|
27
|
+
)
|
|
28
|
+
assert TimeSymbol("new_name_for_time2") == CSymbol(
|
|
29
|
+
"new_name_for_time3",
|
|
30
|
+
definition_url=TimeSymbol.DEFINITION_URL,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
assert (2 * t1).has(t1) is True
|
|
34
|
+
assert (2 * t1).has(TimeSymbol) is True
|
|
35
|
+
assert (2 * t1).has(CSymbol) is True
|
|
36
|
+
assert (sp.sympify("l * e * e * t")).has(TimeSymbol) is False
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def test_cfunction():
|
|
40
|
+
rate_of = CFunction("rateOf", definition_url=DEF_URL_RATE_OF)
|
|
41
|
+
# test that the cache works
|
|
42
|
+
assert rate_of is CFunction("rateOf", definition_url=DEF_URL_RATE_OF)
|
|
43
|
+
# test that definition URL is considered, not only the name
|
|
44
|
+
assert rate_of is not CFunction("rateOf", definition_url=DEF_URL_DELAY)
|
|
45
|
+
|
|
46
|
+
assert rate_of == CFunction("rateOf", definition_url=DEF_URL_RATE_OF)
|
|
47
|
+
assert rate_of != CFunction("rateOf", definition_url=DEF_URL_DELAY)
|
|
48
|
+
assert isinstance(rate_of, CFunction)
|
|
49
|
+
assert isinstance(rate_of, RateOf)
|
|
50
|
+
assert not isinstance(rate_of, delay)
|
|
51
|
+
a, b = sp.symbols("a b")
|
|
52
|
+
assert (rate_of(a) * 4).has(rate_of) is True
|
|
53
|
+
assert (rate_of(a) * 4).has(RateOf("rAteOf")) is True
|
|
54
|
+
assert rate_of(a) == rate_of(a)
|
|
55
|
+
assert rate_of(a) != rate_of(b)
|
|
56
|
+
assert (rate_of(a) * 4).has(RateOf) is False
|
|
57
|
+
|
|
58
|
+
assert UndefinedFunction(rate_of.name) != rate_of
|
|
59
|
+
assert UndefinedFunction(rate_of.name)(a).has(rate_of) is False
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
(test suite at https://github.com/sbmlteam/sbml-test-suite/)
|
|
4
4
|
"""
|
|
5
|
+
|
|
5
6
|
import os
|
|
6
7
|
from pathlib import Path
|
|
7
8
|
|
|
@@ -20,7 +21,9 @@ assert len(cases)
|
|
|
20
21
|
|
|
21
22
|
|
|
22
23
|
@pytest.mark.parametrize(
|
|
23
|
-
("sbml_file",),
|
|
24
|
+
("sbml_file",),
|
|
25
|
+
[[file] for file in cases],
|
|
26
|
+
ids=map(lambda x: x.stem, cases), # noqa: C417
|
|
24
27
|
)
|
|
25
28
|
def test_sbml(sbml_file):
|
|
26
29
|
print()
|
|
@@ -1,10 +1,27 @@
|
|
|
1
1
|
import libsbml
|
|
2
|
-
import pytest
|
|
3
2
|
import sympy as sp
|
|
4
3
|
|
|
5
4
|
from sbmlmath import *
|
|
6
5
|
|
|
7
6
|
|
|
7
|
+
def test_set_math():
|
|
8
|
+
"""Test setting math on SBML objects."""
|
|
9
|
+
# create some model
|
|
10
|
+
doc = libsbml.SBMLDocument(3, 1)
|
|
11
|
+
model = doc.createModel()
|
|
12
|
+
s = model.createSpecies()
|
|
13
|
+
s.setId("s")
|
|
14
|
+
p = model.createParameter()
|
|
15
|
+
p.setId("p")
|
|
16
|
+
ia = model.createInitialAssignment()
|
|
17
|
+
ia.setSymbol("s")
|
|
18
|
+
|
|
19
|
+
# actual test
|
|
20
|
+
expr = sp.sympify("2 * p^2")
|
|
21
|
+
set_math(ia, expr)
|
|
22
|
+
assert expr == sbml_math_to_sympy(ia)
|
|
23
|
+
|
|
24
|
+
|
|
8
25
|
def test_large_mathml():
|
|
9
26
|
"""
|
|
10
27
|
lxml.etree.iterparse simply truncates long input (>2**15 chars);
|
|
@@ -55,7 +72,9 @@ def test_abs():
|
|
|
55
72
|
|
|
56
73
|
|
|
57
74
|
def test_math_dimensionless():
|
|
58
|
-
xml = SBMLMathMLPrinter(literals_dimensionless=False).doprint(
|
|
75
|
+
xml = SBMLMathMLPrinter(literals_dimensionless=False).doprint(
|
|
76
|
+
sp.Integer(1)
|
|
77
|
+
)
|
|
59
78
|
assert "dimensionless" not in xml
|
|
60
79
|
|
|
61
80
|
xml = SBMLMathMLPrinter(literals_dimensionless=True).doprint(sp.Integer(1))
|
|
@@ -152,7 +171,9 @@ def test_integer_overflow():
|
|
|
152
171
|
</math>"""
|
|
153
172
|
|
|
154
173
|
i = 2**31 - 1
|
|
155
|
-
assert (
|
|
174
|
+
assert (
|
|
175
|
+
ast_node := libsbml.readMathMLFromString(get_mathml(i))
|
|
176
|
+
) is not None
|
|
156
177
|
assert str(i) in libsbml.writeMathMLToString(ast_node)
|
|
157
178
|
|
|
158
179
|
# FIXME: change to assert not None, if that restriction is ever lifted
|
|
@@ -75,7 +75,9 @@ def test_species_symbol_operations():
|
|
|
75
75
|
'<cn sbml:units="dimensionless">2</cn></apply></apply>'
|
|
76
76
|
"</math>"
|
|
77
77
|
)
|
|
78
|
-
sym_expr = SBMLMathMLParser(floats_as_rationals=False).parse_str(
|
|
78
|
+
sym_expr = SBMLMathMLParser(floats_as_rationals=False).parse_str(
|
|
79
|
+
mathml_exp
|
|
80
|
+
)
|
|
79
81
|
mathml_act = SBMLMathMLPrinter().doprint(sym_expr)
|
|
80
82
|
|
|
81
83
|
# not an exact match, but close enough, for now
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
# See https://pre-commit.com for more information
|
|
2
|
-
# See https://pre-commit.com/hooks.html for more hooks
|
|
3
|
-
repos:
|
|
4
|
-
- repo: https://github.com/pycqa/isort
|
|
5
|
-
rev: 5.13.2
|
|
6
|
-
hooks:
|
|
7
|
-
- id: isort
|
|
8
|
-
name: isort (python)
|
|
9
|
-
args: ["--profile", "black", "--filter-files"]
|
|
10
|
-
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
11
|
-
rev: v4.5.0
|
|
12
|
-
hooks:
|
|
13
|
-
- id: trailing-whitespace
|
|
14
|
-
- id: end-of-file-fixer
|
|
15
|
-
- id: check-yaml
|
|
16
|
-
- id: check-added-large-files
|
|
17
|
-
- repo: https://github.com/psf/black
|
|
18
|
-
rev: 23.12.0
|
|
19
|
-
hooks:
|
|
20
|
-
- id: black
|
|
21
|
-
# It is recommended to specify the latest version of Python
|
|
22
|
-
# supported by your project here, or alternatively use
|
|
23
|
-
# pre-commit's default_language_version, see
|
|
24
|
-
# https://pre-commit.com/#top_level-default_language_version
|
|
25
|
-
language_version: python3.11
|
sbmlmath-0.1.1/pyproject.toml
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
[build-system]
|
|
2
|
-
requires = ["setuptools", "setuptools-scm"]
|
|
3
|
-
build-backend = "setuptools.build_meta"
|
|
4
|
-
|
|
5
|
-
[project]
|
|
6
|
-
name = "sbmlmath"
|
|
7
|
-
version = "0.1.1"
|
|
8
|
-
authors = [
|
|
9
|
-
{name = "Daniel Weindl", email = "sci@danielweindl.de"},
|
|
10
|
-
]
|
|
11
|
-
description = "SBML Math <-> SymPy"
|
|
12
|
-
readme = "README.md"
|
|
13
|
-
requires-python = ">=3.9"
|
|
14
|
-
license = {text = "BSD-3-Clause"}
|
|
15
|
-
dependencies = [
|
|
16
|
-
"python-libsbml",
|
|
17
|
-
"sympy",
|
|
18
|
-
"pint",
|
|
19
|
-
"lxml>=4.6.4",
|
|
20
|
-
]
|
|
21
|
-
|
|
22
|
-
[project.optional-dependencies]
|
|
23
|
-
test = ["pytest>=7", "pre-commit>=3"]
|
|
24
|
-
|
|
25
|
-
[tool.black]
|
|
26
|
-
line-length = 80
|
|
27
|
-
|
|
28
|
-
[tool.pytest.ini_options]
|
|
29
|
-
addopts = "--doctest-modules --ignore=doc/conf.py"
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
"""Handling of ``<csymbol>`` functions"""
|
|
2
|
-
import sympy as sp
|
|
3
|
-
from sympy.core.function import UndefinedFunction
|
|
4
|
-
|
|
5
|
-
__all__ = ["CFunction", "delay", "rate_of"]
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class CFunction(UndefinedFunction):
|
|
9
|
-
"""
|
|
10
|
-
Represents ``<csymbol>`` functions.
|
|
11
|
-
|
|
12
|
-
Examples: :func:`rateOf() <rate_of>`, :func:`delay`,
|
|
13
|
-
distributions from the `distrib package
|
|
14
|
-
<https://synonym.caltech.edu/documents/specifications/level-3/version-1/distrib/>`_.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
See also https://www.w3.org/TR/MathML2/chapter4.html#contm.deffun.
|
|
18
|
-
"""
|
|
19
|
-
|
|
20
|
-
_cache = {}
|
|
21
|
-
|
|
22
|
-
def __new__(
|
|
23
|
-
cls,
|
|
24
|
-
*args,
|
|
25
|
-
definition_url: str,
|
|
26
|
-
encoding: str = "text",
|
|
27
|
-
**kwargs,
|
|
28
|
-
):
|
|
29
|
-
# Cache instances.
|
|
30
|
-
# If not done: (CFunction("A", definition_url="x")
|
|
31
|
-
# == CFunction("A", definition_url="y")) == False
|
|
32
|
-
if not (name := kwargs.get("name")):
|
|
33
|
-
name = args[0]
|
|
34
|
-
cache_key = (name, definition_url, encoding)
|
|
35
|
-
if cached := cls._cache.get(cache_key):
|
|
36
|
-
return cached
|
|
37
|
-
|
|
38
|
-
obj = super().__new__(cls, *args, **kwargs)
|
|
39
|
-
|
|
40
|
-
# ensure there are no collisions with super() attributes
|
|
41
|
-
assert not hasattr(obj, "definition_url")
|
|
42
|
-
assert not hasattr(obj, "encoding")
|
|
43
|
-
obj.definition_url = definition_url
|
|
44
|
-
obj.encoding = encoding
|
|
45
|
-
|
|
46
|
-
cls._cache[cache_key] = obj
|
|
47
|
-
|
|
48
|
-
return obj
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
# SBML-defined functions
|
|
52
|
-
|
|
53
|
-
#: The SBML ``delay()`` function.
|
|
54
|
-
delay = CFunction(
|
|
55
|
-
"delay", definition_url="http://www.sbml.org/sbml/symbols/delay"
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
#: The SBML ``rateOf()`` function.
|
|
59
|
-
rate_of = CFunction(
|
|
60
|
-
"rateOf", definition_url="http://www.sbml.org/sbml/symbols/rateOf"
|
|
61
|
-
)
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
from sbmlmath.csymbol import *
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
def test_time_symbol():
|
|
5
|
-
assert TimeSymbol("t") is TimeSymbol("t")
|
|
6
|
-
|
|
7
|
-
assert TimeSymbol("t") == TimeSymbol("time")
|
|
8
|
-
assert TimeSymbol("t") is not TimeSymbol("time")
|
|
9
|
-
|
|
10
|
-
t1, t2 = TimeSymbol("t"), TimeSymbol("time")
|
|
11
|
-
eight_t = t1 + t2 + 3 * t1 + 3 * t2
|
|
12
|
-
# is the result defined?
|
|
13
|
-
assert eight_t in [8 * t1, 8 * t2]
|
|
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
|