SimBio 1.1.0__tar.gz → 1.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.
Files changed (32) hide show
  1. {simbio-1.1.0 → simbio-1.2.0}/PKG-INFO +2 -2
  2. simbio-1.2.0/pyproject.toml +187 -0
  3. simbio-1.1.0/pyproject.toml → simbio-1.2.0/pyproject.toml.orig +5 -7
  4. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/__init__.py +2 -1
  5. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/graph/networkx.py +13 -11
  6. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/mathML/exporter.py +22 -10
  7. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/mathML/importer.py +16 -6
  8. simbio-1.2.0/src/simbio/io/mathML/symbol.py +19 -0
  9. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/mathML/to_symbolite.py +1 -1
  10. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/sbml/importer.py +27 -20
  11. simbio-1.2.0/src/simbio/io/sbml/substitute_by_name.py +62 -0
  12. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/sbml/test_importer.py +3 -3
  13. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/sbml/tests/test_sbml_test_suite.py +7 -12
  14. simbio-1.2.0/src/simbio/latex.py +65 -0
  15. simbio-1.1.0/src/simbio/io/mathML/symbol.py +0 -9
  16. {simbio-1.1.0 → simbio-1.2.0}/README.md +0 -0
  17. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/core.py +0 -0
  18. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/__init__.py +0 -0
  19. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/biomodels.py +0 -0
  20. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/mathML/__init__.py +0 -0
  21. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/mathML/test_mathML.py +0 -0
  22. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/sbml/__init__.py +0 -0
  23. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/sbml/from_libsbml.py +0 -0
  24. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/sbml/tests/atol_factor.csv +0 -0
  25. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/io/sbml/types.py +0 -0
  26. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/reactions/__init__.py +0 -0
  27. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/reactions/compound.py +0 -0
  28. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/reactions/enzymatic.py +0 -0
  29. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/reactions/single.py +0 -0
  30. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/reactions/test_reactions.py +0 -0
  31. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/rebop/__init__.py +0 -0
  32. {simbio-1.1.0 → simbio-1.2.0}/src/simbio/test_core.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: simbio
3
- Version: 1.1.0
3
+ Version: 1.2.0
4
4
  Summary: A Python-based package for simulation of Chemical Reaction Networks (CRNs).
5
5
  Keywords: mass action,michaelis menten,ode,compartment,reaction,SBML,poincare
6
6
  Author: Hernán E. Grecco, Mauro Silberberg
@@ -12,7 +12,7 @@ Classifier: Topic :: Scientific/Engineering
12
12
  Classifier: Topic :: Software Development :: Libraries
13
13
  Classifier: Programming Language :: Python :: 3
14
14
  Classifier: Operating System :: OS Independent
15
- Requires-Dist: poincare>=1.1.0
15
+ Requires-Dist: poincare>=1.2.0
16
16
  Requires-Dist: biomodels ; extra == 'io'
17
17
  Requires-Dist: python-libsbml ; extra == 'io'
18
18
  Requires-Python: >=3.12
@@ -0,0 +1,187 @@
1
+ [project]
2
+ name = "simbio"
3
+ description = "A Python-based package for simulation of Chemical Reaction Networks (CRNs)."
4
+ version = "1.2.0"
5
+ license = "MIT"
6
+ classifiers = [
7
+ "Intended Audience :: Developers",
8
+ "Intended Audience :: Science/Research",
9
+ "Topic :: Scientific/Engineering",
10
+ "Topic :: Software Development :: Libraries",
11
+ "Programming Language :: Python :: 3",
12
+ "Operating System :: OS Independent",
13
+ ]
14
+ keywords = [
15
+ "mass action",
16
+ "michaelis menten",
17
+ "ode",
18
+ "compartment",
19
+ "reaction",
20
+ "SBML",
21
+ "poincare",
22
+ ]
23
+ requires-python = ">=3.12"
24
+ dependencies = ["poincare >= 1.2.0"]
25
+
26
+ [[project.authors]]
27
+ name = "Hernán E. Grecco"
28
+ email = "hernan.grecco@gmail.com"
29
+
30
+ [[project.authors]]
31
+ name = "Mauro Silberberg"
32
+ email = "maurosilber@gmail.com"
33
+
34
+ [project.readme]
35
+ file = "README.md"
36
+ content-type = "text/markdown"
37
+
38
+ [project.urls]
39
+ homepage = "https://github.com/dyscolab/simbio"
40
+ issues = "https://github.com/dyscolab/simbio/issues"
41
+ changelog = "https://github.com/dyscolab/simbio/blob/main/CHANGELOG.md"
42
+ documentation = "https://dyscolab.github.io/simbio"
43
+
44
+ [project.optional-dependencies]
45
+ io = [
46
+ "biomodels",
47
+ "python-libsbml",
48
+ ]
49
+
50
+ [build-system]
51
+ requires = ["uv_build>=0.9,<0.10"]
52
+ build-backend = "uv_build"
53
+
54
+ [tool.pytest.ini_options]
55
+ addopts = ["--import-mode=importlib"]
56
+
57
+ [tool.ruff.format]
58
+ docstring-code-format = true
59
+
60
+ [tool.ruff.lint]
61
+ extend-select = ["I"]
62
+
63
+ [tool.pyright]
64
+ typeCheckingMode = "standard"
65
+ venv = "test"
66
+ venvPath = ".pixi/envs/"
67
+
68
+ [tool.pyrefly]
69
+ python-interpreter-path = ".pixi/envs/default/bin/python"
70
+ project-excludes = [
71
+ ".pixi",
72
+ "**/__pycache__",
73
+ ]
74
+
75
+ [tool.ty.environment]
76
+ python = ".pixi/envs/default"
77
+
78
+ [tool.pixi.workspace]
79
+ channels = ["https://conda.anaconda.org/conda-forge/"]
80
+ platforms = [
81
+ "linux-64",
82
+ "osx-arm64",
83
+ "win-64",
84
+ ]
85
+ preview = ["pixi-build"]
86
+
87
+ [tool.pixi.package.build.backend]
88
+ name = "pixi-build-python"
89
+ version = "*"
90
+
91
+ [tool.pixi.package.build.config]
92
+ ignore-pypi-mapping = false
93
+
94
+ [tool.pixi.package.host-dependencies]
95
+ uv-build = "*"
96
+
97
+ [tool.pixi.environments]
98
+ default = [
99
+ "test",
100
+ "py312",
101
+ ]
102
+ test-py312 = [
103
+ "test",
104
+ "io",
105
+ "py312",
106
+ ]
107
+ test-py313 = [
108
+ "test",
109
+ "io",
110
+ "py313",
111
+ ]
112
+ test-py314 = [
113
+ "test",
114
+ "io",
115
+ "py314",
116
+ ]
117
+
118
+ [tool.pixi.environments.lint]
119
+ features = ["lint"]
120
+ no-default-feature = true
121
+
122
+ [tool.pixi.environments.build]
123
+ features = ["build"]
124
+ no-default-feature = true
125
+
126
+ [tool.pixi.dependencies.simbio]
127
+ path = "."
128
+
129
+ [tool.pixi.pypi-dependencies]
130
+ biomodels = "*"
131
+ python-libsbml = "*"
132
+
133
+ [tool.pixi.feature.lint.dependencies]
134
+ pre-commit = "*"
135
+ pre-commit-hooks = "*"
136
+ taplo = "*"
137
+ ruff = "*"
138
+ mdformat = "*"
139
+ mdformat-ruff = "*"
140
+ pyright = "*"
141
+
142
+ [tool.pixi.feature.lint.tasks]
143
+ pre-commit-install = "pre-commit install"
144
+ lint = "pre-commit run"
145
+
146
+ [tool.pixi.feature.build.dependencies]
147
+ uv = "*"
148
+ python = "*"
149
+
150
+ [tool.pixi.feature.build.pypi-dependencies]
151
+ mdremotifier = "*"
152
+
153
+ [tool.pixi.feature.build.tasks]
154
+ publish = "uv publish"
155
+
156
+ [tool.pixi.feature.build.tasks.build]
157
+ cmd = "uv build"
158
+ depends-on = ["remotify"]
159
+
160
+ [tool.pixi.feature.build.tasks.remotify]
161
+ cmd = [
162
+ "mdremotifier",
163
+ "-i",
164
+ "README.md",
165
+ "-o",
166
+ "README.md",
167
+ "--url-prefix",
168
+ "https://github.com/dyscolab/simbio/blob/main",
169
+ "--img-url-prefix",
170
+ "https://raw.githubusercontent.com/dyscolab/simbio/refs/heads/main/",
171
+ ]
172
+
173
+ [tool.pixi.feature.test.dependencies]
174
+ pytest = "*"
175
+ pooch = "*"
176
+
177
+ [tool.pixi.feature.test.tasks]
178
+ test = "pytest src/simbio/test_core.py src/simbio/reactions/test_reactions.py --doctest-modules"
179
+
180
+ [tool.pixi.feature.py312.dependencies]
181
+ python = "3.12.*"
182
+
183
+ [tool.pixi.feature.py313.dependencies]
184
+ python = "3.13.*"
185
+
186
+ [tool.pixi.feature.py314.dependencies]
187
+ python = "3.14.*"
@@ -1,7 +1,7 @@
1
1
  [project]
2
2
  name = "simbio"
3
3
  description = "A Python-based package for simulation of Chemical Reaction Networks (CRNs)."
4
- version = "1.1.0"
4
+ version = "1.2.0"
5
5
  authors = [
6
6
  { name = "Hernán E. Grecco", email = "hernan.grecco@gmail.com" },
7
7
  { name = "Mauro Silberberg", email = "maurosilber@gmail.com" },
@@ -26,7 +26,7 @@ keywords = [
26
26
  "poincare",
27
27
  ]
28
28
  requires-python = ">=3.12"
29
- dependencies = ["poincare >= 1.1.0"]
29
+ dependencies = ["poincare >= 1.2.0"]
30
30
 
31
31
  [project.urls]
32
32
  homepage = "https://github.com/dyscolab/simbio"
@@ -63,19 +63,17 @@ project-excludes = [".pixi", "**/__pycache__"]
63
63
  python = ".pixi/envs/default"
64
64
 
65
65
  [tool.pixi.workspace]
66
- channels = ["https://prefix.dev/conda-forge"]
66
+ channels = ["https://conda.anaconda.org/conda-forge/"]
67
67
  platforms = ["linux-64", "osx-arm64", "win-64"]
68
68
  preview = ["pixi-build"]
69
69
 
70
70
  [tool.pixi.package.build]
71
- backend = { name = "pixi-build-python", version = "0.4.*" }
72
- # config = { ignore-pypi-mapping = false } # Enable automatic PyPI-to-conda mapping
71
+ backend = { name = "pixi-build-python", version = "*" }
72
+ config = { ignore-pypi-mapping = false } # Enable automatic PyPI-to-conda mapping
73
73
 
74
74
  [tool.pixi.package.host-dependencies]
75
75
  uv-build = "*"
76
76
 
77
- [tool.pixi.package.run-dependencies]
78
- # poincare = ">=1.0.0"
79
77
 
80
78
  [tool.pixi.environments]
81
79
  default = ["test", "py312"]
@@ -6,7 +6,6 @@ from poincare import (
6
6
  Variable,
7
7
  assign,
8
8
  initial,
9
- model_report,
10
9
  )
11
10
  from poincare.reactions import (
12
11
  AbsoluteRateLaw,
@@ -28,6 +27,8 @@ from .core import (
28
27
  volume,
29
28
  )
30
29
 
30
+ from .latex import model_report
31
+
31
32
  __all__ = [
32
33
  "Constant",
33
34
  "Independent",
@@ -1,16 +1,18 @@
1
1
  from typing import assert_never
2
2
 
3
3
  from networkx import DiGraph, Graph, bipartite
4
+ from poincare.reactions.reactions import RateLaw
4
5
 
5
- from ...core import Compartment, RateLaw, Species
6
+ from ...core import System, Variable
7
+ from ...import RateLaw
6
8
 
7
9
 
8
- def graph(model: type[Compartment], /) -> DiGraph:
9
- """Construct a directed bipartite graph of Species and Reactions."""
10
+ def graph(model: type[System], /) -> DiGraph:
11
+ """Construct a directed bipartite graph of Variable and Reactions."""
10
12
  g = DiGraph()
11
- for x in model._yield(Species | RateLaw): # type: ignore
13
+ for x in model._yield(Variable | RateLaw): # type: ignore
12
14
  match x:
13
- case Species():
15
+ case Variable():
14
16
  g.add_node(x)
15
17
  case RateLaw():
16
18
  name = str(x)
@@ -24,23 +26,23 @@ def graph(model: type[Compartment], /) -> DiGraph:
24
26
  return g
25
27
 
26
28
 
27
- def to_species_graph(graph: DiGraph, /) -> Graph:
28
- """Project a bipartite graph into the Species graph.
29
+ def to_Variable_graph(graph: DiGraph, /) -> Graph:
30
+ """Project a bipartite graph into the Variable graph.
29
31
 
30
- Two Species are connected if they take part in the same Reaction.
32
+ Two Variable are connected if they take part in the same Reaction.
31
33
  """
32
34
  return bipartite.projected_graph(
33
35
  graph,
34
- nodes=[n for n in graph if isinstance(n, Species)],
36
+ nodes=[n for n in graph if isinstance(n, Variable)],
35
37
  )
36
38
 
37
39
 
38
40
  def to_reaction_graph(graph: DiGraph, /) -> Graph:
39
41
  """Project a bipartite graph into the Reaction graph.
40
42
 
41
- Two Reactions are connected if they share a common Species.
43
+ Two Reactions are connected if they share a common Variable.
42
44
  """
43
45
  return bipartite.projected_graph(
44
46
  graph,
45
- nodes=[n for n in graph if not isinstance(n, Species)],
47
+ nodes=[n for n in graph if not isinstance(n, Variable)],
46
48
  )
@@ -3,6 +3,9 @@ from functools import singledispatch as dispatch
3
3
  import libsbml
4
4
  from symbolite import Real
5
5
  from symbolite.abstract import real
6
+ from symbolite.core.function import BinaryOperator
7
+ from symbolite.core.value import Value, Name
8
+ from symbolite.core.call import Call
6
9
  from symbolite.core.symbolite_object import get_symbolite_info
7
10
 
8
11
  mapper = {
@@ -11,7 +14,7 @@ mapper = {
11
14
  real.neg: libsbml.AST_MINUS,
12
15
  real.mul: libsbml.AST_TIMES,
13
16
  real.truediv: libsbml.AST_DIVIDE,
14
- real.pow: libsbml.AST_POWER,
17
+ real.pow_op: libsbml.AST_POWER,
15
18
  "libsbml.AST_INTEGER": libsbml.AST_INTEGER,
16
19
  "libsbml.AST_REAL": libsbml.AST_REAL,
17
20
  "libsbml.AST_REAL_E": libsbml.AST_REAL_E,
@@ -189,17 +192,26 @@ def float_to_mathML(x: float):
189
192
 
190
193
 
191
194
  @to_mathML.register
192
- def real_to_mathML(x: Real):
193
- if x.expression is None:
195
+ def value_to_mathML(x: Value):
196
+ value = get_symbolite_info(x).value
197
+ if isinstance(value, Name):
194
198
  node = libsbml.ASTNode(libsbml.AST_NAME)
195
199
  node.setId(str(x))
196
- node.setName(get_symbolite_info(x).value.name)
200
+ node.setName(value.name)
197
201
  return node
202
+ elif isinstance(value, Call):
203
+ callinfo = get_symbolite_info(value)
204
+ if len(callinfo.kwargs_items) > 0:
205
+ raise NotImplementedError("mathML does not support functions with kwargs")
198
206
 
199
- if len(x.expression.kwargs) > 0:
200
- raise NotImplementedError("mathML does not support functions with kwargs")
207
+ node = libsbml.ASTNode(to_mathML(callinfo.func))
208
+ for arg in callinfo.args:
209
+ node.addChild(to_mathML(arg))
210
+ return node
211
+ else:
212
+ return to_mathML(value)
201
213
 
202
- node = libsbml.ASTNode(to_mathML(x.expression.func))
203
- for arg in x.expression.args:
204
- node.addChild(to_mathML(arg))
205
- return node
214
+ # @to_mathML.register
215
+ # def binop_to_mathML(x: BinaryOperator):
216
+ # #
217
+ # raise NotImplementedError(f"got type {type(BinaryOperator)}")
@@ -4,9 +4,11 @@ from collections import ChainMap
4
4
 
5
5
  import libsbml
6
6
  from libsbml import ASTNode
7
- from symbolite import Real
7
+ from symbolite.core.symbolite_object import get_symbolite_info
8
+ from symbolite.core.value import Value
8
9
  from symbolite.abstract import real
9
- from symbolite.core import as_function
10
+ import symbolite.abstract as abstract
11
+ from symbolite.impl.libpythoncode._codeexpr import as_function
10
12
 
11
13
  from .symbol import MathMLSpecialSymbol, MathMLSymbol
12
14
 
@@ -41,7 +43,7 @@ def root(*args):
41
43
  case (arg,) | (2, arg):
42
44
  return real.sqrt(arg)
43
45
  case (exp, arg):
44
- return real.pow(arg, real.truediv(1, exp))
46
+ return real.pow_op(arg, real.truediv(1, exp))
45
47
  case _:
46
48
  raise TypeError(f"unexpected number of arguments: {len(args)}")
47
49
 
@@ -66,7 +68,7 @@ mapper = {
66
68
  libsbml.AST_MINUS: minus,
67
69
  libsbml.AST_TIMES: real.mul,
68
70
  libsbml.AST_DIVIDE: real.truediv,
69
- libsbml.AST_POWER: real.pow,
71
+ libsbml.AST_POWER: real.pow_op,
70
72
  libsbml.AST_INTEGER: get_value(int),
71
73
  libsbml.AST_REAL: get_value(float),
72
74
  libsbml.AST_REAL_E: get_value(float),
@@ -246,8 +248,8 @@ class mathMLImporter:
246
248
  def compile_function(self, func_name: str, node: libsbml.ASTNode):
247
249
  *params, body = self.yield_children(node)
248
250
 
249
- if any(keyword.iskeyword(p.name) for p in params):
250
- name_mapping = {p.name: p for p in params}
251
+ if any(keyword.iskeyword(get_symbolite_name(p)) for p in params):
252
+ name_mapping = {get_symbolite_name(p): p for p in params}
251
253
  for name in filter(keyword.iskeyword, name_mapping):
252
254
  new_name = name
253
255
  while new_name in name_mapping:
@@ -267,3 +269,11 @@ class mathMLImporter:
267
269
  self.mapper[libsbml.AST_FUNCTION] = {}
268
270
 
269
271
  self.mapper[libsbml.AST_FUNCTION][name] = func
272
+
273
+
274
+ def get_symbolite_name(obj: Value) -> str | None:
275
+ value = get_symbolite_info(obj).value
276
+ try:
277
+ return value.name
278
+ except AttributeError:
279
+ raise TypeError(f"{obj} does not have name, it's value is a {type(value)}")
@@ -0,0 +1,19 @@
1
+ from symbolite import Real
2
+ from symbolite import translate
3
+ from typing import Any
4
+
5
+ class MathMLSpecialSymbol(Real):
6
+ pass
7
+
8
+
9
+ class MathMLSymbol(Real):
10
+ pass
11
+
12
+
13
+ @translate.register(MathMLSymbol)
14
+ def translate_MathMLSymbol(obj: MathMLSymbol, libsl: types.ModuleType) -> Any:
15
+ return translate(Real(obj.__symbolite_info__.value), libsl)
16
+
17
+ @translate.register(MathMLSpecialSymbol)
18
+ def translate_MathMLSymbol(obj: MathMLSpecialSymbol, libsl: types.ModuleType) -> Any:
19
+ return translate(Real(obj.__symbolite_info__.value), libsl)
@@ -45,7 +45,7 @@ tags = {
45
45
  "minus": partial(star_reduce, real.sub),
46
46
  "times": partial(star_reduce, real.mul),
47
47
  "divide": partial(star_reduce, real.truediv),
48
- "power": real.pow,
48
+ "power": real.pow_op, # TODO: should this be pow_op (operator) or pow (binary function)
49
49
  "root": NotImplemented,
50
50
  "abs": real.abs,
51
51
  "exp": real.exp,
@@ -4,28 +4,32 @@ from dataclasses import replace
4
4
  from functools import reduce, singledispatchmethod
5
5
  from operator import mul
6
6
  from os import PathLike
7
- from typing import Callable, Mapping, Sequence, TypeVar
7
+ from collections.abc import Callable, Mapping, Sequence
8
+ from typing import TypeVar
9
+ from itertools import chain
8
10
 
9
11
  import libsbml
10
12
  import pint
11
13
  from poincare.compile import depends_on_at_least_one_variable_or_time
12
14
  from symbolite import Real, substitute
15
+ from symbolite.core.value import ValueInfo, Name
13
16
 
14
17
  from ... import (
15
- Compartment,
18
+ System,
16
19
  Constant,
17
20
  Independent,
18
21
  Parameter,
19
22
  RateLaw,
20
- Species,
23
+ Reactant,
21
24
  initial,
22
25
  )
26
+ from ... import Variable as Species # TODO: handle propperly
23
27
  from ..mathML.importer import MathMLSpecialSymbol, MathMLSymbol
24
28
  from . import from_libsbml, types
29
+ from .substitute_by_name import substitute_by_name
25
30
 
26
31
  T = TypeVar("T")
27
32
 
28
-
29
33
  class _Namespaces:
30
34
  SUPPORTED = {""}
31
35
  IGNORED = {"layout", "render"}
@@ -39,15 +43,15 @@ def nan_to_none(x):
39
43
  return x
40
44
 
41
45
 
42
- class DynamicCompartment:
46
+ class DynamicSystem:
43
47
  def __init__(self, *, name_mapper: Callable[[str], str] = lambda x: x):
44
48
  self.name_mapping: dict[str, str] = {}
45
49
  self.name_mapper = name_mapper
46
- self.namespace = Compartment.__prepare__(None, ())
50
+ self.namespace = System.__prepare__(None, ())
47
51
  self._annotations = self.namespace.setdefault("__annotations__", {})
48
52
 
49
53
  def build(self, name: str):
50
- return type(name, (Compartment,), self.namespace)
54
+ return type(name, (System,), self.namespace)
51
55
 
52
56
  def add(self, name: str, value, *, init: bool = True):
53
57
  self.name_mapping[name] = new_name = self.name_mapper(name)
@@ -103,7 +107,7 @@ def convert(
103
107
  *,
104
108
  name: str | None = None,
105
109
  identity_mapper: Callable[[str], str] = lambda x: x,
106
- ) -> type[Compartment]:
110
+ ) -> type[System]:
107
111
  if name is None:
108
112
  name = model.name
109
113
  if name is None:
@@ -134,7 +138,7 @@ class SBMLImporter:
134
138
  raise NotImplementedError("global conversion factor")
135
139
 
136
140
  self.model = model
137
- self.simbio = DynamicCompartment(name_mapper=_extra_check(identity_mapper))
141
+ self.simbio = DynamicSystem(name_mapper=_extra_check(identity_mapper))
138
142
 
139
143
  self.use_units = units
140
144
  if self.use_units:
@@ -179,11 +183,11 @@ class SBMLImporter:
179
183
 
180
184
  def get(self, item, default=None):
181
185
  match item:
182
- case Real(name=None):
186
+ case Real(__symboilte_info__=ValueInfo(name=None)):
183
187
  return item
184
- case MathMLSymbol(name=name):
188
+ case MathMLSymbol(__symbolite_info__ = ValueInfo(value = Name(name = name))):
185
189
  return getattr(self.simbio, name)
186
- case MathMLSpecialSymbol(name=name):
190
+ case MathMLSpecialSymbol(__symbolite_info__ = ValueInfo(value = Name(name = name))):
187
191
  return self.get_or_create_independent(name)
188
192
  case _:
189
193
  return item
@@ -266,13 +270,13 @@ class SBMLImporter:
266
270
  raise TypeError(f"unexpected type: {type(value)}")
267
271
  return value
268
272
 
269
- def get_species_reference(self, s: types.SimpleSpeciesReference) -> Species:
273
+ def get_species_reference(self, s: types.SimpleSpeciesReference) -> Reactant:
270
274
  # s.constant: bool
271
275
  species = self.get_symbol(s.species, Species)
272
276
  if isinstance(s, types.SpeciesReference) and s.stoichiometry is not None:
273
- return Species(species, s.stoichiometry)
277
+ return Reactant(species, s.stoichiometry)
274
278
  else:
275
- return Species(species)
279
+ return Reactant(species)
276
280
 
277
281
  @add.register
278
282
  def add_reaction(self, r: types.Reaction):
@@ -301,11 +305,14 @@ class SBMLImporter:
301
305
  for p in kinetic_law.parameters:
302
306
  new_id = f"{r.id}__{p.id}"
303
307
  self.add_parameter(replace(p, id=new_id))
304
- mapping[p.id] = MathMLSymbol(new_id) # TODO: replace substitute by name
305
- # formula = substitute_by_name(
306
- # formula, **mapping
307
- # )
308
- # formula = substitute(formula, GetAsVariable(self.get))
308
+ mapping[p.id] = MathMLSymbol(new_id)
309
+ formula = substitute_by_name(
310
+ formula, **mapping
311
+ )
312
+ formula = substitute(formula, GetAsVariable(self.get))
313
+ rate_law = RateLaw(reactants=reactants, products=products, rate_law=formula)
314
+ for reactant in chain(rate_law.reactants, rate_law.products):
315
+ reactant.__set_name__(cls= rate_law,name= reactant.variable.name,)
309
316
 
310
317
  self.simbio.add(
311
318
  r.id,
@@ -0,0 +1,62 @@
1
+ from functools import singledispatch
2
+ from typing import Any
3
+ from symbolite.core.value import Value, Name
4
+ from symbolite.core.symbolite_object import get_symbolite_info
5
+ from symbolite.core.call import Call
6
+
7
+ @singledispatch
8
+ def substitute_by_name(expr: Any, **mapper: Any) -> Any:
9
+ return expr
10
+
11
+
12
+ @substitute_by_name.register(Value)
13
+ def call_substitute_by_name(expr: Value, **mapper: Any) -> Any:
14
+ """Replace Value by values or objects, matching by name.
15
+
16
+ If multiple mappers are provided,
17
+ they will be used in order (using a ChainMap)
18
+
19
+ If a given object is not found in the mappers,
20
+ the same object will be returned.
21
+
22
+ Parameters
23
+ ----------
24
+ **mapper
25
+ keyword arguments connecting names to values.
26
+ """
27
+
28
+ value = get_symbolite_info(expr).value
29
+ if isinstance(value, Name):
30
+ return mapper.get(value.name, expr)
31
+ return substitute_by_name(value, **mapper)
32
+
33
+
34
+ @substitute_by_name.register(Call)
35
+ def value_substitute_by_name(expr: Call, **mapper: Any) -> Any:
36
+ """Replace symbols, functions, values, etc by others.
37
+
38
+ If multiple mappers are provided,
39
+ they will be used in order (using a ChainMap)
40
+
41
+ If a given object is not found in the mappers,
42
+ the same object will be returned.
43
+
44
+ Parameters
45
+ ----------
46
+ mappers
47
+ dictionary mapping source to destination objects.
48
+ """
49
+ info = get_symbolite_info(expr)
50
+
51
+ func = mapper.get(str(info.func), info.func)
52
+ args = tuple(substitute_by_name(arg, **mapper) for arg in info.args)
53
+ kwargs = {k: substitute_by_name(arg, **mapper) for k, arg in info.kwargs_items}
54
+
55
+ try:
56
+ return func(*args, **kwargs)
57
+ except Exception as ex:
58
+ try:
59
+ ex.add_note(f"While evaluating {func}(*{args}, **{kwargs}): {ex}")
60
+ except AttributeError:
61
+ pass
62
+ raise ex
@@ -2,7 +2,7 @@ import math
2
2
 
3
3
  from pytest import mark, param, raises
4
4
 
5
- from ...core import Parameter, Species
5
+ from ... import Parameter, Variable
6
6
  from . import types
7
7
  from .importer import convert, nan_to_none
8
8
 
@@ -36,7 +36,7 @@ def test_parameter(value, units, constant):
36
36
  @mark.parametrize("boundary_condition", [False, True])
37
37
  @mark.parametrize("constant", [False, True])
38
38
  @mark.parametrize("conversion_factor", [None])
39
- def test_species(
39
+ def test_variable(
40
40
  initial_amount,
41
41
  initial_concentration,
42
42
  substance_units,
@@ -71,7 +71,7 @@ def test_species(
71
71
  else:
72
72
  compartment = convert(model, name="model")
73
73
  s = getattr(compartment, name)
74
- assert isinstance(s, Species)
74
+ from ... import Variable
75
75
  if initial_amount is None:
76
76
  assert s.initial == initial_concentration
77
77
  else:
@@ -1,7 +1,7 @@
1
1
  import zipfile
2
2
  from dataclasses import dataclass
3
3
  from pathlib import Path
4
- from typing import Iterable
4
+ from collections.abc import Iterable
5
5
 
6
6
  import numpy as np
7
7
  import pandas as pd
@@ -125,17 +125,11 @@ def test_running(model_id):
125
125
 
126
126
  try:
127
127
  df = (
128
- Simulator(
129
- model,
130
- transform=[getattr(model, name) for name in settings.variables],
131
- )
132
- .solve(
133
- save_at=save_at,
134
- solver=LSODA(
135
- atol=settings.absolute,
136
- rtol=settings.relative,
137
- ),
138
- )
128
+ Simulator(model)
129
+ .with_transform([getattr(model, name) for name in settings.variables])
130
+ .with_solver(LSODA(atol=settings.absolute, rtol=settings.relative,))
131
+ .solve(save_at=save_at)
132
+ .to_dataframe()
139
133
  .reset_index()
140
134
  )
141
135
  except TypeError as e:
@@ -145,6 +139,7 @@ def test_running(model_id):
145
139
  except KeyError:
146
140
  xfail("dependency solving error")
147
141
 
142
+
148
143
  df_expected = loader.read_results()
149
144
  assert_frame_equal(
150
145
  df[settings.variables],
@@ -0,0 +1,65 @@
1
+ from collections.abc import Iterable, Callable, Mapping
2
+
3
+ from poincare.reactions import RateLaw, Reactant
4
+ from poincare.printing.latex import Latex, ToLatex, normalize_eq, default_sections
5
+ from poincare.printing.latex import model_report as _model_report
6
+ from symbolite.ops import translate, substitute
7
+ from symbolite.impl import liblatex
8
+ from poincare import System
9
+
10
+
11
+
12
+ def reaction_to_latex(reaction: RateLaw, latex: ToLatex) -> Latex:
13
+ lhs = " + ".join(map(lambda x: reactant_to_latex(x, latex), reaction.reactants))
14
+ rhs = " + ".join(map(lambda x: reactant_to_latex(x, latex), reaction.products))
15
+ rate_law = normalize_eq(reaction.rate_law, transform=latex.transform, base = latex.system)
16
+
17
+ return f"{lhs} &\\rightarrow {rhs},\\quad \\text{{rate:}}\\ {rate_law}"
18
+
19
+ def reactant_to_latex(reactant: Reactant,latex: ToLatex) -> Latex:
20
+ return f"{reactant.stoichiometry if reactant.stoichiometry != 1 else ""} {substitute(reactant.variable, latex.transform)}"
21
+
22
+
23
+ def aligned_reactions(model: type[System], latex: ToLatex) -> Latex:
24
+ lines = []
25
+ lines.append("\\begin{aligned}")
26
+ lines.extend(reaction_to_latex(reaction=reaction, latex= latex) for reaction in model._yield(RateLaw))
27
+ lines.append("\\end{aligned}")
28
+ return "\\\\\n".join(lines)
29
+
30
+
31
+ def latex_reactions(
32
+ model: type[System], transform: dict | None = None, latex: ToLatex | None = None
33
+ ) -> Latex:
34
+ if latex is None:
35
+ transform = transform if transform is not None else {}
36
+ latex = ToLatex(model, transform=transform)
37
+ return "\\[ " + aligned_reactions(model = model, latex=latex) + " \\]"
38
+
39
+
40
+ def model_report(
41
+ model: type[System],
42
+ path: str | None = None,
43
+ transform: dict | None = None,
44
+ descriptions: dict | None = None,
45
+ standalone: bool = True,
46
+ replace_algebraics: bool = False,
47
+ sections: Mapping[
48
+ str, Callable[[type[System], ToLatex], str]
49
+ ] = {"Reactions": latex_reactions} | default_sections
50
+ ) -> Latex | None:
51
+ return _model_report(
52
+ model=model,
53
+ path=path,
54
+ transform=transform,
55
+ descriptions=descriptions,
56
+ standalone=standalone,
57
+ replace_algebraics=replace_algebraics,
58
+ sections=sections,
59
+ )
60
+
61
+
62
+
63
+
64
+
65
+
@@ -1,9 +0,0 @@
1
- from symbolite import Real
2
-
3
-
4
- class MathMLSpecialSymbol(Real):
5
- pass
6
-
7
-
8
- class MathMLSymbol(Real):
9
- pass
File without changes
File without changes
File without changes
File without changes