sbmlmath 0.2.0__tar.gz → 0.3.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.
Files changed (37) hide show
  1. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/.github/workflows/ci.yaml +3 -3
  2. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/.gitignore +1 -0
  3. sbmlmath-0.3.0/CITATION.cff +25 -0
  4. {sbmlmath-0.2.0/sbmlmath.egg-info → sbmlmath-0.3.0}/PKG-INFO +5 -3
  5. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/README.md +3 -1
  6. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/pyproject.toml +5 -2
  7. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/sbmlmath/__init__.py +46 -27
  8. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/sbmlmath/cfunction.py +11 -0
  9. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/sbmlmath/mathml_parser.py +14 -8
  10. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/sbmlmath/mathml_printer.py +11 -2
  11. {sbmlmath-0.2.0 → sbmlmath-0.3.0/sbmlmath.egg-info}/PKG-INFO +5 -3
  12. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/sbmlmath.egg-info/SOURCES.txt +1 -0
  13. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/tests/test_csymbol.py +4 -0
  14. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/tests/test_mathml_parser_sbml_semantic_suite.py +1 -1
  15. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/.github/dependabot.yml +0 -0
  16. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/.github/workflows/deploy_release.yaml +0 -0
  17. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/.pre-commit-config.yaml +0 -0
  18. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/.readthedocs.yml +0 -0
  19. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/LICENSE +0 -0
  20. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/doc/.gitignore +0 -0
  21. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/doc/Makefile +0 -0
  22. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/doc/about.rst +0 -0
  23. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/doc/api.rst +0 -0
  24. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/doc/conf.py +0 -0
  25. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/doc/index.rst +0 -0
  26. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/doc/make.bat +0 -0
  27. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/doc/rtd_requirements.txt +0 -0
  28. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/sbmlmath/csymbol.py +0 -0
  29. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/sbmlmath/species_symbol.py +0 -0
  30. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/sbmlmath.egg-info/dependency_links.txt +0 -0
  31. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/sbmlmath.egg-info/requires.txt +0 -0
  32. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/sbmlmath.egg-info/top_level.txt +0 -0
  33. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/setup.cfg +0 -0
  34. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/tests/test_misc.py +0 -0
  35. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/tests/test_parser.py +0 -0
  36. {sbmlmath-0.2.0 → sbmlmath-0.3.0}/tests/test_printer.py +0 -0
  37. {sbmlmath-0.2.0 → sbmlmath-0.3.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:
@@ -6,3 +6,4 @@ dist/
6
6
  activate
7
7
  create_venv.sh
8
8
  *deleteme*
9
+ build/
@@ -0,0 +1,25 @@
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
+ message: >-
7
+ If you use this software, please cite it using the
8
+ metadata from this file.
9
+ type: software
10
+ authors:
11
+ - given-names: Daniel
12
+ family-names: Weindl
13
+ email: sci@danielweindl.de
14
+ orcid: 'https://orcid.org/0000-0001-9963-6057'
15
+ repository-code: 'https://github.com/dweindl/sbmlmath/'
16
+ url: 'https://sbmlmath.readthedocs.io/'
17
+ abstract: >-
18
+ sbmlmath is a Python package for the interconversion of
19
+ SBML MathML and sympy expressions.
20
+ keywords:
21
+ - SBML
22
+ - SymPy
23
+ - math expressions
24
+ - Python
25
+ license: BSD-3-Clause
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: sbmlmath
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: SBML Math <-> SymPy
5
5
  Author-email: Daniel Weindl <sci@danielweindl.de>
6
6
  License: BSD-3-Clause
@@ -22,7 +22,9 @@ Requires-Dist: pre-commit>=3; extra == "test"
22
22
 
23
23
  # sbmlmath — a Python library for handling SBML MathML
24
24
 
25
- This is a Python library for interconverting [SymPy](https://github.com/sympy/sympy/)
25
+ [![PyPI - Version](https://img.shields.io/pypi/v/sbmlmath)](https://pypi.org/project/sbmlmath/)
26
+
27
+ `sbmlmath` is a Python library for interconverting [SymPy](https://github.com/sympy/sympy/)
26
28
  expressions and [SBML](https://sbml.org/)-MathML.
27
29
  SBML uses a subset of [MathML](https://www.w3.org/Math/) that this library
28
30
  aims to support. This is not (intended to be) a general MathML parser.
@@ -1,6 +1,8 @@
1
1
  # sbmlmath — a Python library for handling SBML MathML
2
2
 
3
- This is a Python library for interconverting [SymPy](https://github.com/sympy/sympy/)
3
+ [![PyPI - Version](https://img.shields.io/pypi/v/sbmlmath)](https://pypi.org/project/sbmlmath/)
4
+
5
+ `sbmlmath` is a Python library for interconverting [SymPy](https://github.com/sympy/sympy/)
4
6
  expressions and [SBML](https://sbml.org/)-MathML.
5
7
  SBML uses a subset of [MathML](https://www.w3.org/Math/) that this library
6
8
  aims to support. This is not (intended to be) a general MathML parser.
@@ -47,6 +47,9 @@ lint.ignore = [
47
47
  ]
48
48
  [tool.ruff.lint.per-file-ignores]
49
49
  "tests/*" = ["T201"] # print statement
50
-
50
+ "sbmlmath/__init__.py" = [
51
+ "F401", # module imported but unused
52
+ "E402", # import not at top of file
53
+ ]
51
54
  [tool.pytest.ini_options]
52
- addopts = "--doctest-modules --ignore=doc/conf.py"
55
+ 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.
@@ -69,13 +56,26 @@ def sbml_math_to_sympy(
69
56
  Returns:
70
57
  The resulting sympy expression.
71
58
  """
72
- ast_node = (
73
- sbml_obj
74
- if isinstance(sbml_obj, libsbml.ASTNode)
75
- else sbml_obj.getMath()
59
+ if isinstance(sbml_obj, libsbml.ASTNode):
60
+ ast_node = sbml_obj
61
+ sbml_obj = ast_node.getParentSBMLObject()
62
+ else:
63
+ ast_node = sbml_obj.getMath()
64
+
65
+ level = (
66
+ sbml_obj.getLevel() if sbml_obj is not None else _DEFAULT_SBML_LEVEL
67
+ )
68
+ version = (
69
+ sbml_obj.getVersion()
70
+ if sbml_obj is not None
71
+ else _DEFAULT_SBML_VERSION
72
+ )
73
+ mathml = libsbml.writeMathMLWithNamespaceToString(
74
+ ast_node, libsbml.SBMLNamespaces(level, version)
75
+ )
76
+ return SBMLMathMLParser(sbml_level=level, sbml_version=version).parse_str(
77
+ mathml
76
78
  )
77
- mathml = libsbml.writeMathMLToString(ast_node)
78
- return SBMLMathMLParser().parse_str(mathml)
79
79
 
80
80
 
81
81
  def set_math(
@@ -90,15 +90,15 @@ def set_math(
90
90
  expr:
91
91
  The sympy expression to set as the math expression.
92
92
  """
93
- sbml_document = element.getSBMLDocument()
94
93
  mathml = SBMLMathMLPrinter(
95
- sbml_level=sbml_document.getLevel(),
96
- sbml_version=sbml_document.getVersion(),
94
+ sbml_level=element.getLevel(),
95
+ sbml_version=element.getVersion(),
97
96
  ).doprint(expr)
98
97
 
99
98
  if ast_node := libsbml.readMathMLFromString(mathml):
100
99
  if element.setMath(ast_node) == libsbml.LIBSBML_OPERATION_SUCCESS:
101
100
  return
101
+ sbml_document = element.getSBMLDocument()
102
102
  raise ValueError(
103
103
  f"Error setting math expression:\n{expr}\n"
104
104
  f"{mathml}\n{sbml_document.getErrorLog().toString()}"
@@ -106,3 +106,22 @@ def set_math(
106
106
  raise ValueError(
107
107
  f"Unknown error parsing math expression:\n{expr}\n{mathml}"
108
108
  )
109
+
110
+
111
+ from .cfunction import *
112
+ from .csymbol import *
113
+ from .mathml_parser import SBMLMathMLParser
114
+ from .mathml_printer import SBMLMathMLPrinter
115
+ from .species_symbol import SpeciesSymbol
116
+
117
+ __all__ = [
118
+ "set_math",
119
+ "SBMLMathMLParser",
120
+ "SBMLMathMLPrinter",
121
+ "SpeciesSymbol",
122
+ "TimeSymbol",
123
+ "sympy_to_sbml_math",
124
+ "sbml_math_to_sympy",
125
+ *csymbol.__all__,
126
+ *cfunction.__all__,
127
+ ]
@@ -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
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  import contextlib
4
4
  import operator as operators
5
+ import warnings
5
6
  from functools import reduce
6
7
  from io import BytesIO
7
8
  from typing import Union
@@ -11,6 +12,7 @@ from lxml import etree
11
12
  from pint import UnitRegistry
12
13
  from sympy.logic.boolalg import Boolean, BooleanFalse, BooleanTrue
13
14
 
15
+ from . import _DEFAULT_SBML_LEVEL, _DEFAULT_SBML_VERSION
14
16
  from .cfunction import CFunction
15
17
  from .csymbol import CSymbol
16
18
  from .species_symbol import SpeciesSymbol
@@ -140,8 +142,8 @@ class SBMLMathMLParser:
140
142
 
141
143
  See also: `MathML 2.0 specification <https://www.w3.org/TR/MathML2/>`_.
142
144
 
143
- :param level: SBML level
144
- :param version: SBML version
145
+ :param sbml_level: SBML level
146
+ :param sbml_version: SBML version
145
147
  :param ureg:
146
148
  Optional :class:`pint.UnitRegistry` to use for unit conversion.
147
149
  :param floats_as_rationals:
@@ -160,22 +162,26 @@ class SBMLMathMLParser:
160
162
 
161
163
  def __init__(
162
164
  self,
163
- level: Union[int, str] = 3,
164
- version: Union[int, str] = 2,
165
+ sbml_level: Union[int, str] = _DEFAULT_SBML_LEVEL,
166
+ sbml_version: Union[int, str] = _DEFAULT_SBML_VERSION,
165
167
  ureg: UnitRegistry = None,
166
168
  floats_as_rationals=True,
167
169
  ignore_units=False,
168
170
  symbol_kwargs=None,
169
171
  ):
170
172
  """Constructor"""
173
+ if sbml_level < 3:
174
+ warnings.warn(
175
+ "SBML level < 3 has not been thoroughly tested.",
176
+ UserWarning,
177
+ stacklevel=2,
178
+ )
171
179
  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
- )
180
+ self.sbml_core_ns = f"http://www.sbml.org/sbml/level{sbml_level}/version{sbml_version}/core"
175
181
  # TODO {prefix=>url}
176
182
  self.sbml_multi_ns = (
177
183
  # L3V2 doesn't work
178
- f"http://www.sbml.org/sbml/level{level}/version1/multi/version1"
184
+ f"http://www.sbml.org/sbml/level{sbml_level}/version1/multi/version1"
179
185
  )
180
186
  self.floats_as_rationals = floats_as_rationals
181
187
  self.ignore_units = ignore_units
@@ -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 = 3,
30
- sbml_version: int = 2,
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,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: sbmlmath
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: SBML Math <-> SymPy
5
5
  Author-email: Daniel Weindl <sci@danielweindl.de>
6
6
  License: BSD-3-Clause
@@ -22,7 +22,9 @@ Requires-Dist: pre-commit>=3; extra == "test"
22
22
 
23
23
  # sbmlmath — a Python library for handling SBML MathML
24
24
 
25
- This is a Python library for interconverting [SymPy](https://github.com/sympy/sympy/)
25
+ [![PyPI - Version](https://img.shields.io/pypi/v/sbmlmath)](https://pypi.org/project/sbmlmath/)
26
+
27
+ `sbmlmath` is a Python library for interconverting [SymPy](https://github.com/sympy/sympy/)
26
28
  expressions and [SBML](https://sbml.org/)-MathML.
27
29
  SBML uses a subset of [MathML](https://www.w3.org/Math/) that this library
28
30
  aims to support. This is not (intended to be) a general MathML parser.
@@ -1,6 +1,7 @@
1
1
  .gitignore
2
2
  .pre-commit-config.yaml
3
3
  .readthedocs.yml
4
+ CITATION.cff
4
5
  LICENSE
5
6
  README.md
6
7
  pyproject.toml
@@ -57,3 +57,7 @@ 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
@@ -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-l3v2.xml"))
19
+ cases = sorted(cases_root.rglob("*-sbml-l3v?.xml"))
20
20
  assert len(cases)
21
21
 
22
22
 
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