SimBio 1.2.0__tar.gz → 1.3.1__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.
- {simbio-1.2.0 → simbio-1.3.1}/PKG-INFO +2 -2
- {simbio-1.2.0 → simbio-1.3.1}/pyproject.toml +10 -3
- {simbio-1.2.0 → simbio-1.3.1}/pyproject.toml.orig +13 -4
- simbio-1.3.1/skill/share/agent-skills/dyscolab-modeling/examples/data/decay.xml +30 -0
- simbio-1.3.1/skill/share/agent-skills/dyscolab-modeling/examples/simbio_compartments.py +93 -0
- simbio-1.3.1/skill/share/agent-skills/dyscolab-modeling/examples/simbio_sbml.py +41 -0
- simbio-1.3.1/skill/share/agent-skills/dyscolab-modeling/examples/simbio_stochastic.py +28 -0
- simbio-1.3.1/skill/share/agent-skills/dyscolab-modeling/references/simbio-api.md +29 -0
- simbio-1.3.1/skill/share/agent-skills/dyscolab-modeling/references/simbio.md +218 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/__init__.py +2 -1
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/mathML/importer.py +34 -22
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/mathML/symbol.py +1 -0
- simbio-1.3.1/src/simbio/io/mathML/test_mathML.py +110 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/sbml/importer.py +2 -2
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/sbml/tests/test_sbml_test_suite.py +6 -1
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/latex.py +45 -17
- simbio-1.3.1/src/simbio/skill.py +15 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/test_core.py +2 -1
- simbio-1.3.1/src/simbio/test_latex.py +20 -0
- simbio-1.3.1/src/simbio/test_skill.py +26 -0
- simbio-1.2.0/src/simbio/io/mathML/test_mathML.py +0 -32
- {simbio-1.2.0 → simbio-1.3.1}/README.md +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/core.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/__init__.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/biomodels.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/graph/networkx.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/mathML/__init__.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/mathML/exporter.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/mathML/to_symbolite.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/sbml/__init__.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/sbml/from_libsbml.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/sbml/test_importer.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/sbml/tests/atol_factor.csv +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/io/sbml/types.py +0 -0
- {simbio-1.2.0/src/simbio/io/sbml → simbio-1.3.1/src/simbio/io}/substitute_by_name.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/reactions/__init__.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/reactions/compound.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/reactions/enzymatic.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/reactions/single.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/reactions/test_reactions.py +0 -0
- {simbio-1.2.0 → simbio-1.3.1}/src/simbio/rebop/__init__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: simbio
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.1
|
|
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.
|
|
15
|
+
Requires-Dist: poincare>=1.3.1
|
|
16
16
|
Requires-Dist: biomodels ; extra == 'io'
|
|
17
17
|
Requires-Dist: python-libsbml ; extra == 'io'
|
|
18
18
|
Requires-Python: >=3.12
|
|
@@ -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.
|
|
4
|
+
version = "1.3.1"
|
|
5
5
|
license = "MIT"
|
|
6
6
|
classifiers = [
|
|
7
7
|
"Intended Audience :: Developers",
|
|
@@ -21,7 +21,7 @@ keywords = [
|
|
|
21
21
|
"poincare",
|
|
22
22
|
]
|
|
23
23
|
requires-python = ">=3.12"
|
|
24
|
-
dependencies = ["poincare >= 1.
|
|
24
|
+
dependencies = ["poincare >= 1.3.1"]
|
|
25
25
|
|
|
26
26
|
[[project.authors]]
|
|
27
27
|
name = "Hernán E. Grecco"
|
|
@@ -51,6 +51,9 @@ io = [
|
|
|
51
51
|
requires = ["uv_build>=0.9,<0.10"]
|
|
52
52
|
build-backend = "uv_build"
|
|
53
53
|
|
|
54
|
+
[tool.uv.build-backend.data]
|
|
55
|
+
data = "skill"
|
|
56
|
+
|
|
54
57
|
[tool.pytest.ini_options]
|
|
55
58
|
addopts = ["--import-mode=importlib"]
|
|
56
59
|
|
|
@@ -173,9 +176,13 @@ cmd = [
|
|
|
173
176
|
[tool.pixi.feature.test.dependencies]
|
|
174
177
|
pytest = "*"
|
|
175
178
|
pooch = "*"
|
|
179
|
+
networkx = "*"
|
|
180
|
+
|
|
181
|
+
[tool.pixi.feature.test.pypi-dependencies]
|
|
182
|
+
rebop = "*"
|
|
176
183
|
|
|
177
184
|
[tool.pixi.feature.test.tasks]
|
|
178
|
-
test = "pytest
|
|
185
|
+
test = "pytest --doctest-modules"
|
|
179
186
|
|
|
180
187
|
[tool.pixi.feature.py312.dependencies]
|
|
181
188
|
python = "3.12.*"
|
|
@@ -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.
|
|
4
|
+
version = "1.3.1"
|
|
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.
|
|
29
|
+
dependencies = ["poincare >= 1.3.1"]
|
|
30
30
|
|
|
31
31
|
[project.urls]
|
|
32
32
|
homepage = "https://github.com/dyscolab/simbio"
|
|
@@ -41,6 +41,10 @@ io = ["biomodels", "python-libsbml"]
|
|
|
41
41
|
requires = ["uv_build>=0.9,<0.10"]
|
|
42
42
|
build-backend = "uv_build"
|
|
43
43
|
|
|
44
|
+
# The dyscolab-modeling agent skill, installed to <prefix>/share/agent-skills/.
|
|
45
|
+
[tool.uv.build-backend]
|
|
46
|
+
data = { data = "skill" }
|
|
47
|
+
|
|
44
48
|
[tool.pytest.ini_options]
|
|
45
49
|
addopts = ["--import-mode=importlib"]
|
|
46
50
|
|
|
@@ -69,7 +73,7 @@ preview = ["pixi-build"]
|
|
|
69
73
|
|
|
70
74
|
[tool.pixi.package.build]
|
|
71
75
|
backend = { name = "pixi-build-python", version = "*" }
|
|
72
|
-
config = { ignore-pypi-mapping = false }
|
|
76
|
+
config = { ignore-pypi-mapping = false } # Enable automatic PyPI-to-conda mapping
|
|
73
77
|
|
|
74
78
|
[tool.pixi.package.host-dependencies]
|
|
75
79
|
uv-build = "*"
|
|
@@ -128,9 +132,14 @@ remotify = { cmd = [
|
|
|
128
132
|
[tool.pixi.feature.test.dependencies]
|
|
129
133
|
pytest = "*"
|
|
130
134
|
pooch = "*"
|
|
135
|
+
networkx = "*"
|
|
136
|
+
|
|
137
|
+
[tool.pixi.feature.test.pypi-dependencies]
|
|
138
|
+
rebop = "*"
|
|
139
|
+
|
|
131
140
|
|
|
132
141
|
[tool.pixi.feature.test.tasks]
|
|
133
|
-
test = "pytest
|
|
142
|
+
test = "pytest --doctest-modules"
|
|
134
143
|
|
|
135
144
|
[tool.pixi.feature]
|
|
136
145
|
py312.dependencies = { python = "3.12.*" }
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" level="3" version="1">
|
|
3
|
+
<model id="decay" name="Decay">
|
|
4
|
+
<listOfCompartments>
|
|
5
|
+
<compartment id="cell" size="1" constant="true"/>
|
|
6
|
+
</listOfCompartments>
|
|
7
|
+
<listOfSpecies>
|
|
8
|
+
<species id="X" compartment="cell" initialAmount="10" hasOnlySubstanceUnits="true" boundaryCondition="false" constant="false"/>
|
|
9
|
+
</listOfSpecies>
|
|
10
|
+
<listOfParameters>
|
|
11
|
+
<parameter id="k" value="0.5" constant="true"/>
|
|
12
|
+
</listOfParameters>
|
|
13
|
+
<listOfReactions>
|
|
14
|
+
<reaction id="deg" reversible="false" fast="false">
|
|
15
|
+
<listOfReactants>
|
|
16
|
+
<speciesReference species="X" stoichiometry="1" constant="true"/>
|
|
17
|
+
</listOfReactants>
|
|
18
|
+
<kineticLaw>
|
|
19
|
+
<math xmlns="http://www.w3.org/1998/Math/MathML">
|
|
20
|
+
<apply>
|
|
21
|
+
<times/>
|
|
22
|
+
<ci> k </ci>
|
|
23
|
+
<ci> X </ci>
|
|
24
|
+
</apply>
|
|
25
|
+
</math>
|
|
26
|
+
</kineticLaw>
|
|
27
|
+
</reaction>
|
|
28
|
+
</listOfReactions>
|
|
29
|
+
</model>
|
|
30
|
+
</sbml>
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""Reversible binding A + B <-> AB in a simbio Compartment.
|
|
2
|
+
|
|
3
|
+
Checks: mass conservation, the analytic equilibrium, and that tracking AB as an
|
|
4
|
+
amount in a 2 L compartment gives the same concentration as tracking it as a
|
|
5
|
+
concentration.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
import pint
|
|
10
|
+
|
|
11
|
+
from poincare import SteadyState, solvers # simbio does not re-export these
|
|
12
|
+
from simbio import (
|
|
13
|
+
Compartment,
|
|
14
|
+
MassAction,
|
|
15
|
+
Parameter,
|
|
16
|
+
Simulator,
|
|
17
|
+
Species,
|
|
18
|
+
Volume,
|
|
19
|
+
amount,
|
|
20
|
+
assign,
|
|
21
|
+
concentration,
|
|
22
|
+
volume,
|
|
23
|
+
)
|
|
24
|
+
from simbio.reactions import ReversibleSynthesis
|
|
25
|
+
|
|
26
|
+
u = pint.get_application_registry()
|
|
27
|
+
TIGHT = solvers.LSODA(rtol=1e-9, atol=1e-12)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class Binding(Compartment):
|
|
31
|
+
V: Volume = volume(default=2 * u.L)
|
|
32
|
+
A: Species = concentration(default=1 * u.mM)
|
|
33
|
+
B: Species = concentration(default=0.5 * u.mM)
|
|
34
|
+
AB: Species = concentration(default=0 * u.mM)
|
|
35
|
+
kon: Parameter = assign(default=1 / u.mM / u.s)
|
|
36
|
+
koff: Parameter = assign(default=0.1 / u.s)
|
|
37
|
+
|
|
38
|
+
binding = ReversibleSynthesis(A=A, B=B, AB=AB, forward_rate=kon, reverse_rate=koff)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class BindingAmountAB(Compartment):
|
|
42
|
+
"""Same system, but AB is tracked as an amount (mmol) instead of a concentration.
|
|
43
|
+
|
|
44
|
+
With amount species, write the reactions directly in the Compartment.
|
|
45
|
+
Motifs from simbio.reactions are subsystems and do not see the compartment
|
|
46
|
+
volume (simbio 1.2.0): with units they fail, without units they silently
|
|
47
|
+
skip the volume conversion.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
V: Volume = volume(default=2 * u.L)
|
|
51
|
+
A: Species = concentration(default=1 * u.mM)
|
|
52
|
+
B: Species = concentration(default=0.5 * u.mM)
|
|
53
|
+
AB: Species = amount(default=0 * u.mmol)
|
|
54
|
+
kon: Parameter = assign(default=1 / u.mM / u.s)
|
|
55
|
+
koff: Parameter = assign(default=0.1 / u.s)
|
|
56
|
+
|
|
57
|
+
binding = MassAction(reactants=[A, B], products=[AB], rate=kon)
|
|
58
|
+
unbinding = MassAction(reactants=[AB], products=[A, B], rate=koff)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
save_at = np.linspace(0, 30, 61) * u.s
|
|
62
|
+
conc = Simulator(Binding).with_solver(TIGHT).with_times(save_at=save_at).solve()
|
|
63
|
+
|
|
64
|
+
# Conservation: A + AB and B + AB are constant.
|
|
65
|
+
total_A = (conc["A"] + conc["AB"]).pint.to(u.mM).pint.dequantify().values
|
|
66
|
+
np.testing.assert_allclose(total_A, 1.0, rtol=1e-8)
|
|
67
|
+
|
|
68
|
+
# Equilibrium: AB = (A0 - AB)(B0 - AB) / Kd, with Kd = koff / kon = 0.1 mM.
|
|
69
|
+
a0, b0, kd = 1.0, 0.5, 0.1
|
|
70
|
+
ab_eq = ((a0 + b0 + kd) - np.sqrt((a0 + b0 + kd) ** 2 - 4 * a0 * b0)) / 2
|
|
71
|
+
steady = SteadyState().solve(Simulator(Binding).with_solver(TIGHT))
|
|
72
|
+
np.testing.assert_allclose(steady["AB"].pint.to(u.mM).pint.dequantify().item(), ab_eq, rtol=1e-6)
|
|
73
|
+
|
|
74
|
+
# Amount vs concentration bookkeeping: AB_amount / V == AB_concentration.
|
|
75
|
+
amt = (
|
|
76
|
+
Simulator(BindingAmountAB)
|
|
77
|
+
.with_solver(TIGHT)
|
|
78
|
+
.with_times(save_at=save_at)
|
|
79
|
+
.with_transform({"AB_conc": BindingAmountAB.AB / BindingAmountAB.V}, append=True)
|
|
80
|
+
.solve()
|
|
81
|
+
)
|
|
82
|
+
np.testing.assert_allclose(
|
|
83
|
+
amt["AB_conc"].pint.to(u.mM).pint.dequantify().values,
|
|
84
|
+
conc["AB"].pint.to(u.mM).pint.dequantify().values,
|
|
85
|
+
rtol=1e-6,
|
|
86
|
+
atol=1e-12,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
if __name__ == "__main__":
|
|
90
|
+
import matplotlib.pyplot as plt
|
|
91
|
+
|
|
92
|
+
conc.pint.dequantify().to_dataframe()[["A", "B", "AB"]].plot()
|
|
93
|
+
plt.show()
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""Import an SBML model with simbio and simulate it with poincare.
|
|
2
|
+
|
|
3
|
+
data/decay.xml is X -> 0 with k = 0.5 and X(0) = 10, checked against
|
|
4
|
+
X(t) = 10 exp(-0.5 t).
|
|
5
|
+
|
|
6
|
+
Requires the `io` extra (`pip install "simbio[io]"`). simbio 1.2.0's SBML import
|
|
7
|
+
only works on Python >= 3.14; older Pythons fail at import with
|
|
8
|
+
`NameError: name 'types' is not defined`.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
|
|
15
|
+
from poincare import solvers
|
|
16
|
+
from simbio import Simulator
|
|
17
|
+
from simbio.io import sbml
|
|
18
|
+
|
|
19
|
+
path = Path(__file__).parent / "data" / "decay.xml"
|
|
20
|
+
Decay = sbml.load(path) # a poincare System class; pass name=... if the SBML model has none
|
|
21
|
+
|
|
22
|
+
# Components are class attributes named after their SBML ids.
|
|
23
|
+
print(Decay) # summary table of variables, parameters and equations
|
|
24
|
+
assert {"X", "k", "deg"} <= set(vars(Decay))
|
|
25
|
+
|
|
26
|
+
t = np.linspace(0, 6, 31)
|
|
27
|
+
sim = (
|
|
28
|
+
Simulator(Decay)
|
|
29
|
+
.with_solver(solvers.LSODA(rtol=1e-9, atol=1e-12))
|
|
30
|
+
.with_times(save_at=t)
|
|
31
|
+
)
|
|
32
|
+
result = sim.solve()
|
|
33
|
+
np.testing.assert_allclose(result["X"].values, 10 * np.exp(-0.5 * t), rtol=1e-6)
|
|
34
|
+
|
|
35
|
+
# Parameters and initial values are changed like in any other model.
|
|
36
|
+
faster = sim.with_values({Decay.k: 1.0, Decay.X: 5}).solve()
|
|
37
|
+
np.testing.assert_allclose(faster["X"].values, 5 * np.exp(-1.0 * t), rtol=1e-6)
|
|
38
|
+
|
|
39
|
+
# The same model from a string; `name` is required when the SBML has no name.
|
|
40
|
+
Decay2 = sbml.loads(path.read_text(), name="DecayFromString")
|
|
41
|
+
assert Decay2.__name__ == "DecayFromString"
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Stochastic (Gillespie) simulation of a reaction network with rebop.
|
|
2
|
+
|
|
3
|
+
A -> 0 with rate k: the mean of many stochastic runs follows the ODE solution
|
|
4
|
+
N0 exp(-k t). Requires `pip install rebop`.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
import xarray as xr
|
|
9
|
+
|
|
10
|
+
from simbio import MassAction, Simulator, System, Variable, initial
|
|
11
|
+
from simbio.rebop import RebopSimulator
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Decay(System):
|
|
15
|
+
A: Variable = initial(default=500) # copy numbers: integers, no units
|
|
16
|
+
degradation = MassAction(reactants=[A], products=[], rate=0.5)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# n_points equally spaced samples after t = 0, so n_points + 1 time points.
|
|
20
|
+
rsim = RebopSimulator(Decay).with_times(upto_t=4, n_points=8)
|
|
21
|
+
runs = xr.concat([rsim.solve(rng=seed) for seed in range(40)], dim="run")
|
|
22
|
+
mean = runs["A"].mean("run")
|
|
23
|
+
t = mean["time"].values
|
|
24
|
+
np.testing.assert_allclose(mean.values, 500 * np.exp(-0.5 * t), rtol=0.05)
|
|
25
|
+
|
|
26
|
+
# The same model runs deterministically with the ODE simulator.
|
|
27
|
+
ode = Simulator(Decay).with_times(save_at=t).solve()
|
|
28
|
+
np.testing.assert_allclose(ode["A"].values, 500 * np.exp(-0.5 * t), rtol=1e-2)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Public API inventory: simbio (1.2.0 and newer)
|
|
2
|
+
|
|
3
|
+
Use this to check that a name exists before you write code with it. When the installed
|
|
4
|
+
version differs, check the installed source instead
|
|
5
|
+
(`python -c "import simbio.reactions as m; print(dir(m))"`). The poincare inventory is in
|
|
6
|
+
[api-inventory.md](api-inventory.md).
|
|
7
|
+
|
|
8
|
+
Names marked *(newer)* exist only after simbio 1.2.0.
|
|
9
|
+
|
|
10
|
+
## simbio
|
|
11
|
+
|
|
12
|
+
| Module | Names | Reference |
|
|
13
|
+
| --- | --- | --- |
|
|
14
|
+
| `simbio` | re-exports from poincare (`System`, `Variable`, `Parameter`, `Constant`, `Independent`, `Simulator`, `initial`, `assign`, `Reactant`, `reaction_initial`, `RateLaw`, `MassAction`, `AbsoluteRateLaw`) plus `Compartment`, `Species`, `Volume`, `amount`, `concentration`, `volume`, `reaction_amount`, `reaction_concentration`, `model_report`, `notebook_report` *(newer)* | [simbio.md](simbio.md) |
|
|
15
|
+
| `simbio.reactions` (`.single`, `.compound`, `.enzymatic`) | `Creation`, `AutoCreation`, `Destruction`, `Conversion`, `Synthesis`, `Dissociation`, `ReversibleSynthesis`, `Equilibration`, `CatalyzeConvert`, `MichaelisMenten`, `MichaelisMentenEqApprox`, `MichaelisMentenQuasiSSAprox` | [simbio.md](simbio.md#reaction-motifs) |
|
|
16
|
+
| `simbio.io.sbml` | `load`, `loads` (Python ≥ 3.14 with 1.2.0) | [simbio.md](simbio.md#sbml-biomodels-and-mathml) |
|
|
17
|
+
| `simbio.io.biomodels` | `load` | same |
|
|
18
|
+
| `simbio.io.mathML` | `to_mathML`, `mathMLImporter` | same |
|
|
19
|
+
| `simbio.io.sbml.substitute_by_name` (newer: `simbio.io.substitute_by_name`) | `substitute_by_name` | same |
|
|
20
|
+
| `simbio.latex` | `latex_reactions` (*(newer)* `format=`), `model_report`, `notebook_report` *(newer)* | [simbio.md](simbio.md#graphs-reports-stochastic-runs) |
|
|
21
|
+
| `simbio.io.graph.networkx` | `graph`, `to_Variable_graph`, `to_reaction_graph` | [simbio.md](simbio.md#graphs-reports-stochastic-runs) |
|
|
22
|
+
| `simbio.rebop` | `RebopSimulator` | [analysis.md](analysis.md#stochastic-simulation-rebop) |
|
|
23
|
+
| `simbio.skill` *(newer)* | `install`, `check`: the same as `poincare.skill` | [SKILL.md](../SKILL.md#versions-and-environment) |
|
|
24
|
+
|
|
25
|
+
## Internal helpers (don't use)
|
|
26
|
+
|
|
27
|
+
`simbio.core.first_system_parent`, `is_instance_or_subclass`,
|
|
28
|
+
`simbio.io.sbml.substitute_by_name.call_substitute_by_name` / `value_substitute_by_name`,
|
|
29
|
+
and anything whose name starts with `_`.
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# simbio 1.2: chemical reaction networks
|
|
2
|
+
|
|
3
|
+
simbio models are poincare `System`s. `simbio` re-exports `System`, `Variable`,
|
|
4
|
+
`Parameter`, `Constant`, `Independent`, `Simulator`, `initial`, `assign`, the reaction
|
|
5
|
+
primitives and `model_report`. It does **not** re-export `SteadyState`, `Oscillations`
|
|
6
|
+
or `solvers`, so import those from `poincare`.
|
|
7
|
+
|
|
8
|
+
## Contents
|
|
9
|
+
|
|
10
|
+
- [Reactions](#reactions)
|
|
11
|
+
- [Reaction motifs](#reaction-motifs)
|
|
12
|
+
- [Compartments and volume](#compartments-and-volume)
|
|
13
|
+
- [SBML, BioModels and MathML](#sbml-biomodels-and-mathml)
|
|
14
|
+
- [Graphs, reports, stochastic runs](#graphs-reports-stochastic-runs)
|
|
15
|
+
|
|
16
|
+
## Reactions
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
from simbio import MassAction, RateLaw, AbsoluteRateLaw, System, Variable, Parameter, initial, assign
|
|
20
|
+
|
|
21
|
+
class Network(System):
|
|
22
|
+
A: Variable = initial(default=1)
|
|
23
|
+
B: Variable = initial(default=1)
|
|
24
|
+
C: Variable = initial(default=0)
|
|
25
|
+
k: Parameter = assign(default=0.5)
|
|
26
|
+
|
|
27
|
+
r1 = MassAction(reactants=[2 * A, B], products=[C], rate=k) # rate = k·A²·B
|
|
28
|
+
r2 = RateLaw(reactants=[C], products=[], rate_law=0.1 * C / (1 + C)) # any rate law
|
|
29
|
+
feed = RateLaw(reactants=[], products=[A], rate_law=0.2) # zero-order creation
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
- **Stoichiometry by multiplication:** `2 * A` means coefficient 2. Empty `reactants`
|
|
33
|
+
gives creation, and empty `products` gives degradation.
|
|
34
|
+
- `MassAction(rate=k)`: the rate law is `k · Π reactant^stoichiometry`, and each species
|
|
35
|
+
changes by `±stoichiometry · rate law`.
|
|
36
|
+
- `RateLaw(rate_law=expr)`: you supply the full rate law (Hill, Michaelis–Menten,
|
|
37
|
+
inhibition…), interpreted as concentration/time.
|
|
38
|
+
- `AbsoluteRateLaw(rate_law=expr)`: the rate law is amount/time. It only differs from
|
|
39
|
+
`RateLaw` inside a `Compartment`.
|
|
40
|
+
- **Modulators (catalysts, repressors)** that appear in a rate law but are not consumed
|
|
41
|
+
can be listed on both sides (`reactants=[p], products=[m, p]`). That keeps them visible
|
|
42
|
+
to stochastic simulation and to the reaction graph. For ODEs alone it is optional.
|
|
43
|
+
- Reactions combine with plain poincare equations on the same model
|
|
44
|
+
(`x.derive() << ...`). Rates add up.
|
|
45
|
+
- A pint quantity as `rate`/`rate_law` is wrapped automatically in a hidden parameter
|
|
46
|
+
named `_<reaction>_rate_law`. Declare a named `Parameter` instead when you want to change
|
|
47
|
+
it with `with_values`.
|
|
48
|
+
|
|
49
|
+
## Reaction motifs
|
|
50
|
+
|
|
51
|
+
`simbio.reactions` provides `System`s with `Reactant` fields (link them with keywords):
|
|
52
|
+
|
|
53
|
+
| Motif | Scheme | Arguments |
|
|
54
|
+
| --- | --- | --- |
|
|
55
|
+
| `Creation` | ∅ → A | `A, rate` |
|
|
56
|
+
| `AutoCreation` | A → 2A | `A, rate` |
|
|
57
|
+
| `Destruction` | A → ∅ | `A, rate` |
|
|
58
|
+
| `Conversion` | A → B | `A, B, rate` |
|
|
59
|
+
| `Synthesis` | A + B → AB | `A, B, AB, rate` |
|
|
60
|
+
| `Dissociation` | AB → A + B | `AB, A, B, rate` |
|
|
61
|
+
| `ReversibleSynthesis` | A + B ⇌ AB | `A, B, AB, forward_rate, reverse_rate` |
|
|
62
|
+
| `Equilibration` | A ⇌ B | `A, B, forward_rate, reverse_rate` |
|
|
63
|
+
| `CatalyzeConvert` | A + B ⇌ A:B → P | `A, B, AB, P, forward_rate, reverse_rate, conversion_rate` |
|
|
64
|
+
| `MichaelisMenten` | E + S ⇌ ES → E + P (mass action) | `E, S, ES, P, forward_rate, reverse_rate, catalytic_rate` |
|
|
65
|
+
| `MichaelisMentenEqApprox` | S → P, `v = Vmax·S/(Kd + S)` | `S, P, maximum_velocity, dissociation_constant` |
|
|
66
|
+
| `MichaelisMentenQuasiSSAprox` | S → P, `v = Vmax·S/(Km + S)` | `S, P, maximum_velocity, michaelis_constant` |
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from simbio.reactions import Destruction, ReversibleSynthesis, Synthesis
|
|
70
|
+
|
|
71
|
+
class Model(System):
|
|
72
|
+
A: Variable = initial(default=10)
|
|
73
|
+
B: Variable = initial(default=5)
|
|
74
|
+
AB: Variable = initial(default=0)
|
|
75
|
+
binding = ReversibleSynthesis(A=A, B=B, AB=AB, forward_rate=1, reverse_rate=0.1)
|
|
76
|
+
decay = Destruction(A=AB, rate=0.01)
|
|
77
|
+
dimer = Synthesis(A=2 * A, B=B, AB=AB, rate=2) # scale stoichiometry from outside
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
All motif kinetics are mass action unless stated otherwise. `MichaelisMenten.to_eq_approx()`
|
|
81
|
+
and `to_qss_approx()` raise `NotImplementedError` in 1.2.0.
|
|
82
|
+
|
|
83
|
+
**Writing your own motif:** declare `Reactant` fields with `reaction_initial()` and rates
|
|
84
|
+
with `assign()`, then combine them with `MassAction`/`RateLaw`. Use `S.variable` in rate
|
|
85
|
+
law expressions:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from simbio import Reactant, reaction_initial
|
|
89
|
+
|
|
90
|
+
class HillActivation(System):
|
|
91
|
+
"""Activator X drives ∅ -> Y with a Hill rate law."""
|
|
92
|
+
X: Reactant = reaction_initial()
|
|
93
|
+
Y: Reactant = reaction_initial()
|
|
94
|
+
vmax: Parameter = assign(default=1)
|
|
95
|
+
K: Parameter = assign(default=1)
|
|
96
|
+
n: Parameter = assign(default=2)
|
|
97
|
+
reaction = RateLaw(reactants=[X], products=[X, Y],
|
|
98
|
+
rate_law=vmax * X.variable**n / (K**n + X.variable**n))
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Compartments and volume
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from simbio import (Compartment, Species, Volume, amount, concentration, volume,
|
|
105
|
+
MassAction, RateLaw, AbsoluteRateLaw, Parameter, assign)
|
|
106
|
+
|
|
107
|
+
class Cell(Compartment):
|
|
108
|
+
V: Volume = volume(default=2 * u.L) # exactly one Volume per Compartment
|
|
109
|
+
A: Species = concentration(default=2 * u.mM) # tracked as concentration
|
|
110
|
+
B: Species = amount(default=3 * u.mmol) # tracked as amount
|
|
111
|
+
AB: Species = concentration(default=0 * u.mM)
|
|
112
|
+
kon: Parameter = assign(default=1 / u.mM / u.s)
|
|
113
|
+
binding = MassAction(reactants=[A, B], products=[AB], rate=kon)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
How simbio converts with the compartment volume `V`:
|
|
117
|
+
|
|
118
|
+
| Reaction type | Concentration species | Amount species |
|
|
119
|
+
| --- | --- | --- |
|
|
120
|
+
| `RateLaw` / `MassAction` (concentration/time) | as written | × V |
|
|
121
|
+
| `AbsoluteRateLaw` (amount/time) | ÷ V | as written |
|
|
122
|
+
| Inside a `MassAction` rate law | used as is | divided by V (→ concentration) |
|
|
123
|
+
|
|
124
|
+
Rules and pitfalls:
|
|
125
|
+
|
|
126
|
+
- A `Compartment` needs **exactly one** `Volume`. Zero or two raises `AttributeError`.
|
|
127
|
+
- **Inside a Compartment with amount species, write reactions directly with `MassAction`,
|
|
128
|
+
`RateLaw` and `AbsoluteRateLaw`.**
|
|
129
|
+
- Motifs from `simbio.reactions` are subsystems and don't see the compartment volume.
|
|
130
|
+
- With units they fail (`PintError: Units are defined inconsistently`).
|
|
131
|
+
- Without units they **silently skip the volume conversion**.
|
|
132
|
+
- Motifs are fine when all species are concentrations, because no conversion is needed.
|
|
133
|
+
- `Volume` is a variable. Give it an equation for growing compartments:
|
|
134
|
+
`growth = V.derive() << rate`. A *concentration* species keeps its concentration as `V`
|
|
135
|
+
grows, which effectively creates matter. Track amounts and report concentrations with a
|
|
136
|
+
transform (`with_transform({"A_conc": Cell.A / Cell.V}, append=True)`).
|
|
137
|
+
- Growing cell, Michaelis–Menten with amounts (tested pattern):
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
class Cell(Compartment):
|
|
141
|
+
V: Volume = volume(default=1 * u.pL)
|
|
142
|
+
E: Species = amount(default=1e-18 * u.mol)
|
|
143
|
+
S: Species = amount(default=1e-16 * u.mol)
|
|
144
|
+
ES: Species = amount(default=0 * u.mol)
|
|
145
|
+
P: Species = amount(default=0 * u.mol)
|
|
146
|
+
growth_rate: Parameter = assign(default=0.1 * u.pL / u.min)
|
|
147
|
+
growth = V.derive() << growth_rate
|
|
148
|
+
binding = MassAction(reactants=[E, S], products=[ES], rate=1e6 / u.M / u.s)
|
|
149
|
+
unbinding = MassAction(reactants=[ES], products=[E, S], rate=1 / u.s)
|
|
150
|
+
catalysis = MassAction(reactants=[ES], products=[E, P], rate=10 / u.s)
|
|
151
|
+
|
|
152
|
+
sim = (Simulator(Cell)
|
|
153
|
+
.with_solver(solvers.LSODA(rtol=1e-9, atol=1e-27)) # atol in mol / m³
|
|
154
|
+
.with_transform({"P_conc": Cell.P / Cell.V}, append=True))
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Setting `growth_rate` to 0 with `with_values` gives the constant-volume limit.
|
|
158
|
+
- Compartments can be nested, but **their `Species` and `Volume` cannot be linked to
|
|
159
|
+
outside components** (`TypeError`). Only initial values (numbers or quantities) and
|
|
160
|
+
plain `Parameter`s/`Variable`s can be passed: `Inner(A=5, T=outer_T)`.
|
|
161
|
+
- `reaction_amount()` and `reaction_concentration()` create `Reactant` fields backed by
|
|
162
|
+
`Species`, for motif-style subsystems.
|
|
163
|
+
|
|
164
|
+
## SBML, BioModels and MathML
|
|
165
|
+
|
|
166
|
+
These need `pip install "simbio[io]"`, which provides `python-libsbml` and `biomodels`.
|
|
167
|
+
|
|
168
|
+
**Known 1.2.0 bug:** `simbio.io.sbml` and `simbio.io.mathML` fail at import on
|
|
169
|
+
Python < 3.14 (`NameError: name 'types' is not defined`). Use Python 3.14, or tell the user
|
|
170
|
+
that SBML import needs a newer simbio or Python. Fixed in newer simbio (not on PyPI yet),
|
|
171
|
+
which also fixes the import of MathML function definitions.
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from simbio.io import sbml, biomodels
|
|
175
|
+
|
|
176
|
+
Model = sbml.load("model.xml") # returns a System class
|
|
177
|
+
Model = sbml.loads(text, name="MyModel") # name required if the SBML has none
|
|
178
|
+
Model = biomodels.load("BIOMD0000000012") # downloads from BioModels (network)
|
|
179
|
+
sim = Simulator(Model) # simulate like any other model
|
|
180
|
+
Model.k1, Model.X # components named after SBML ids
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
- `load(path, *, name=None, identity_mapper=...)`.
|
|
184
|
+
- `loads(text, *, name=None, identity_mapper=..., ignore_namespaces=[])`.
|
|
185
|
+
- `biomodels.load(model_id, *, name=None, ignore_namespaces=[])`.
|
|
186
|
+
- `print(Model)` lists the imported variables, parameters (including compartments) and
|
|
187
|
+
reactions.
|
|
188
|
+
- Imported models usually have no pint units. Time is in the model's SBML time units
|
|
189
|
+
(seconds unless the file says otherwise), so pass plain `save_at` arrays.
|
|
190
|
+
- Imported species are plain `Variable`s (not `Species` in `Compartment`s), so volume
|
|
191
|
+
conversions are handled the way the SBML specifies them.
|
|
192
|
+
- The SBML packages `comp`, `fbc`, `groups`, `multi` and `qual`, and a global
|
|
193
|
+
conversion factor, raise `NotImplementedError`. `layout`/`render` are ignored. Pass
|
|
194
|
+
`ignore_namespaces=["fbc"]` (for example) to skip a package deliberately.
|
|
195
|
+
- MathML helpers (low level, libsbml AST ↔ symbolite): `simbio.io.mathML.to_mathML(expr)`
|
|
196
|
+
and `simbio.io.mathML.mathMLImporter().convert(node)`.
|
|
197
|
+
- `simbio.io.sbml.substitute_by_name.substitute_by_name(expr, **{"name": value})`
|
|
198
|
+
substitutes symbols by name in an expression. In newer simbio the module moved to
|
|
199
|
+
`simbio.io.substitute_by_name`.
|
|
200
|
+
|
|
201
|
+
## Graphs, reports, stochastic runs
|
|
202
|
+
|
|
203
|
+
- `from simbio.io.graph.networkx import graph, to_Variable_graph, to_reaction_graph`:
|
|
204
|
+
- `graph(Model)` builds a bipartite `networkx.DiGraph` of variables and reactions, with
|
|
205
|
+
`stoichiometry` edge attributes.
|
|
206
|
+
- The projections give species–species or reaction–reaction graphs.
|
|
207
|
+
- Needs `networkx`.
|
|
208
|
+
- `simbio.model_report(Model, path=None, ...)` gives a LaTeX report with a reactions section
|
|
209
|
+
(same arguments as poincare's).
|
|
210
|
+
- `simbio.notebook_report(Model, show=True, ...)` is the notebook version (Jupyter or
|
|
211
|
+
marimo): reactions, then equations, variables and parameters, as Markdown (same arguments
|
|
212
|
+
as `poincare.printing.notebook_report`). Requires simbio and poincare newer than the
|
|
213
|
+
current PyPI releases.
|
|
214
|
+
- `simbio.latex.latex_reactions(Model)` returns only the reactions block as LaTeX. Newer
|
|
215
|
+
simbio takes `format="markdown"` for `$$` display math.
|
|
216
|
+
- `from simbio.rebop import RebopSimulator` runs Gillespie simulations. See
|
|
217
|
+
[analysis.md](analysis.md#stochastic-simulation-rebop). Copy numbers must be unitless
|
|
218
|
+
integers.
|
|
@@ -27,7 +27,7 @@ from .core import (
|
|
|
27
27
|
volume,
|
|
28
28
|
)
|
|
29
29
|
|
|
30
|
-
from .latex import model_report
|
|
30
|
+
from .latex import model_report, notebook_report
|
|
31
31
|
|
|
32
32
|
__all__ = [
|
|
33
33
|
"Constant",
|
|
@@ -54,4 +54,5 @@ __all__ += [
|
|
|
54
54
|
"reaction_amount",
|
|
55
55
|
"volume",
|
|
56
56
|
"model_report",
|
|
57
|
+
"notebook_report",
|
|
57
58
|
]
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import functools
|
|
2
|
-
import keyword
|
|
3
2
|
from collections import ChainMap
|
|
4
3
|
|
|
5
4
|
import libsbml
|
|
6
5
|
from libsbml import ASTNode
|
|
6
|
+
from symbolite.abstract import real
|
|
7
7
|
from symbolite.core.symbolite_object import get_symbolite_info
|
|
8
8
|
from symbolite.core.value import Value
|
|
9
|
-
from symbolite.abstract import real
|
|
10
|
-
import symbolite.abstract as abstract
|
|
11
|
-
from symbolite.impl.libpythoncode._codeexpr import as_function
|
|
12
9
|
|
|
10
|
+
from ..substitute_by_name import substitute_by_name
|
|
13
11
|
from .symbol import MathMLSpecialSymbol, MathMLSymbol
|
|
14
12
|
|
|
15
13
|
|
|
@@ -241,26 +239,38 @@ class mathMLImporter:
|
|
|
241
239
|
else:
|
|
242
240
|
return func
|
|
243
241
|
|
|
244
|
-
def yield_children(self, node: libsbml.ASTNode) -> Real:
|
|
242
|
+
def yield_children(self, node: libsbml.ASTNode) -> real.Real:
|
|
245
243
|
for i in range(node.getNumChildren()):
|
|
246
244
|
yield self.convert(node.getChild(i))
|
|
247
245
|
|
|
248
246
|
def compile_function(self, func_name: str, node: libsbml.ASTNode):
|
|
249
247
|
*params, body = self.yield_children(node)
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
248
|
+
param_names = tuple(get_symbolite_name(p) for p in params)
|
|
249
|
+
|
|
250
|
+
def func(*args, **kwargs):
|
|
251
|
+
if len(args) > len(param_names):
|
|
252
|
+
raise TypeError(
|
|
253
|
+
f"{func_name}() takes {len(param_names)} positional arguments but {len(args)} were given"
|
|
254
|
+
)
|
|
255
|
+
mapping = dict(zip(param_names, args))
|
|
256
|
+
for k, v in kwargs.items():
|
|
257
|
+
if k in mapping:
|
|
258
|
+
raise TypeError(
|
|
259
|
+
f"{func_name}() got multiple values for argument {k!r}"
|
|
260
|
+
)
|
|
261
|
+
if k not in param_names:
|
|
262
|
+
raise TypeError(
|
|
263
|
+
f"{func_name}() got an unexpected keyword argument {k!r}"
|
|
264
|
+
)
|
|
265
|
+
mapping[k] = v
|
|
266
|
+
if len(mapping) != len(param_names):
|
|
267
|
+
missing = [p for p in param_names if p not in mapping]
|
|
268
|
+
raise TypeError(
|
|
269
|
+
f"{func_name}() missing {len(missing)} required positional argument(s): {', '.join(missing)}"
|
|
270
|
+
)
|
|
271
|
+
return substitute_by_name(body, **mapping)
|
|
272
|
+
|
|
273
|
+
func.__name__ = func_name
|
|
264
274
|
self.add_function(func_name, func)
|
|
265
275
|
return func
|
|
266
276
|
|
|
@@ -271,9 +281,11 @@ class mathMLImporter:
|
|
|
271
281
|
self.mapper[libsbml.AST_FUNCTION][name] = func
|
|
272
282
|
|
|
273
283
|
|
|
274
|
-
def get_symbolite_name(obj: Value) -> str
|
|
275
|
-
|
|
284
|
+
def get_symbolite_name(obj: Value | str) -> str:
|
|
285
|
+
if isinstance(obj, str):
|
|
286
|
+
return obj
|
|
276
287
|
try:
|
|
288
|
+
value = get_symbolite_info(obj).value
|
|
277
289
|
return value.name
|
|
278
290
|
except AttributeError:
|
|
279
|
-
raise TypeError(f"{obj} does not have name,
|
|
291
|
+
raise TypeError(f"{obj} does not have name, its value is a {type(obj)}")
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
from pytest import mark
|
|
2
|
+
from symbolite.abstract import real
|
|
3
|
+
|
|
4
|
+
from . import mathMLImporter, to_mathML
|
|
5
|
+
from .symbol import MathMLSymbol as Symbol
|
|
6
|
+
|
|
7
|
+
x, y = map(Symbol, ["x", "y"])
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@mark.parametrize(
|
|
11
|
+
"expr",
|
|
12
|
+
[
|
|
13
|
+
1,
|
|
14
|
+
x,
|
|
15
|
+
x * y,
|
|
16
|
+
x + y,
|
|
17
|
+
x * 2,
|
|
18
|
+
2 * x,
|
|
19
|
+
2 * x + y,
|
|
20
|
+
x**2,
|
|
21
|
+
x**0.5,
|
|
22
|
+
real.cos(x),
|
|
23
|
+
real.sqrt(x),
|
|
24
|
+
x < 1,
|
|
25
|
+
x < y,
|
|
26
|
+
~x,
|
|
27
|
+
],
|
|
28
|
+
)
|
|
29
|
+
def test_mathML_roundtrip(expr: Symbol):
|
|
30
|
+
node = to_mathML(expr)
|
|
31
|
+
expr2 = mathMLImporter().convert(node)
|
|
32
|
+
assert expr2 == expr
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def test_compile_function():
|
|
36
|
+
import libsbml
|
|
37
|
+
from pytest import raises
|
|
38
|
+
|
|
39
|
+
ast = libsbml.parseL3Formula("lambda(a, b, a * b + 2)")
|
|
40
|
+
importer = mathMLImporter()
|
|
41
|
+
func = importer.compile_function("my_func", ast)
|
|
42
|
+
|
|
43
|
+
assert func(x, y) == x * y + 2
|
|
44
|
+
assert func(a=x, b=y) == x * y + 2
|
|
45
|
+
assert func(b=y, a=x) == x * y + 2
|
|
46
|
+
|
|
47
|
+
with raises(TypeError):
|
|
48
|
+
func(x)
|
|
49
|
+
with raises(TypeError):
|
|
50
|
+
func(x, y, x)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def test_sbml_function_definition():
|
|
54
|
+
from ..sbml import loads
|
|
55
|
+
|
|
56
|
+
sbml = """<?xml version="1.0" encoding="UTF-8"?>
|
|
57
|
+
<sbml xmlns="http://www.sbml.org/sbml/level3/version2/core" level="3" version="2">
|
|
58
|
+
<model id="test_func_model" name="test_func_model">
|
|
59
|
+
<listOfFunctionDefinitions>
|
|
60
|
+
<functionDefinition id="rate_func">
|
|
61
|
+
<math xmlns="http://www.w3.org/1998/Math/MathML">
|
|
62
|
+
<lambda>
|
|
63
|
+
<bvar><ci> k </ci></bvar>
|
|
64
|
+
<bvar><ci> s </ci></bvar>
|
|
65
|
+
<apply>
|
|
66
|
+
<times/>
|
|
67
|
+
<ci> k </ci>
|
|
68
|
+
<ci> s </ci>
|
|
69
|
+
</apply>
|
|
70
|
+
</lambda>
|
|
71
|
+
</math>
|
|
72
|
+
</functionDefinition>
|
|
73
|
+
</listOfFunctionDefinitions>
|
|
74
|
+
<listOfCompartments>
|
|
75
|
+
<compartment id="c" spatialDimensions="3" size="1" constant="true"/>
|
|
76
|
+
</listOfCompartments>
|
|
77
|
+
<listOfSpecies>
|
|
78
|
+
<species id="S1" compartment="c" initialConcentration="1.0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
|
|
79
|
+
<species id="S2" compartment="c" initialConcentration="0.0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
|
|
80
|
+
</listOfSpecies>
|
|
81
|
+
<listOfParameters>
|
|
82
|
+
<parameter id="k1" value="0.5" constant="true"/>
|
|
83
|
+
</listOfParameters>
|
|
84
|
+
<listOfReactions>
|
|
85
|
+
<reaction id="r1" reversible="false">
|
|
86
|
+
<listOfReactants>
|
|
87
|
+
<speciesReference species="S1" constant="true"/>
|
|
88
|
+
</listOfReactants>
|
|
89
|
+
<listOfProducts>
|
|
90
|
+
<speciesReference species="S2" constant="true"/>
|
|
91
|
+
</listOfProducts>
|
|
92
|
+
<kineticLaw>
|
|
93
|
+
<math xmlns="http://www.w3.org/1998/Math/MathML">
|
|
94
|
+
<apply>
|
|
95
|
+
<ci> rate_func </ci>
|
|
96
|
+
<ci> k1 </ci>
|
|
97
|
+
<ci> S1 </ci>
|
|
98
|
+
</apply>
|
|
99
|
+
</math>
|
|
100
|
+
</kineticLaw>
|
|
101
|
+
</reaction>
|
|
102
|
+
</listOfReactions>
|
|
103
|
+
</model>
|
|
104
|
+
</sbml>"""
|
|
105
|
+
|
|
106
|
+
model = loads(sbml)
|
|
107
|
+
assert hasattr(model, "S1")
|
|
108
|
+
assert hasattr(model, "S2")
|
|
109
|
+
assert hasattr(model, "r1")
|
|
110
|
+
|
|
@@ -24,9 +24,9 @@ from ... import (
|
|
|
24
24
|
initial,
|
|
25
25
|
)
|
|
26
26
|
from ... import Variable as Species # TODO: handle propperly
|
|
27
|
-
from ..mathML.
|
|
27
|
+
from ..mathML.symbol import MathMLSpecialSymbol, MathMLSymbol
|
|
28
28
|
from . import from_libsbml, types
|
|
29
|
-
from
|
|
29
|
+
from ..substitute_by_name import substitute_by_name
|
|
30
30
|
|
|
31
31
|
T = TypeVar("T")
|
|
32
32
|
|
|
@@ -92,7 +92,7 @@ def test_loading(model):
|
|
|
92
92
|
try:
|
|
93
93
|
model = loads(sbml, name=model)
|
|
94
94
|
except Exception as e:
|
|
95
|
-
|
|
95
|
+
str(e)
|
|
96
96
|
|
|
97
97
|
|
|
98
98
|
@mark.parametrize("model_id", MODELS)
|
|
@@ -138,6 +138,11 @@ def test_running(model_id):
|
|
|
138
138
|
raise e
|
|
139
139
|
except KeyError:
|
|
140
140
|
xfail("dependency solving error")
|
|
141
|
+
except ValueError as e:
|
|
142
|
+
if str(e) == "Integers to negative integer powers are not allowed.":
|
|
143
|
+
xfail("numpy power doesn't support integers to negative powers")
|
|
144
|
+
else:
|
|
145
|
+
raise
|
|
141
146
|
|
|
142
147
|
|
|
143
148
|
df_expected = loader.read_results()
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
from collections.abc import
|
|
1
|
+
from collections.abc import Callable, Mapping
|
|
2
|
+
from functools import partial
|
|
3
|
+
from typing import Literal
|
|
2
4
|
|
|
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
5
|
from poincare import System
|
|
9
|
-
|
|
6
|
+
from poincare.printing.latex import Latex, ToLatex, default_sections, normalize_eq
|
|
7
|
+
from poincare.printing.latex import model_report as _model_report
|
|
8
|
+
from poincare.printing.notebook_display import Markdown, default_notebook_sections
|
|
9
|
+
from poincare.printing.notebook_display import notebook_report as _notebook_report
|
|
10
|
+
from poincare.reactions import RateLaw, Reactant
|
|
11
|
+
from symbolite.ops import substitute
|
|
10
12
|
|
|
11
13
|
|
|
12
14
|
def reaction_to_latex(reaction: RateLaw, latex: ToLatex) -> Latex:
|
|
@@ -21,20 +23,29 @@ def reactant_to_latex(reactant: Reactant,latex: ToLatex) -> Latex:
|
|
|
21
23
|
|
|
22
24
|
|
|
23
25
|
def aligned_reactions(model: type[System], latex: ToLatex) -> Latex:
|
|
24
|
-
lines = []
|
|
25
|
-
lines.
|
|
26
|
-
|
|
26
|
+
lines = ["\\begin{aligned}"]
|
|
27
|
+
lines.extend(
|
|
28
|
+
reaction_to_latex(reaction=reaction, latex=latex) + "\\\\"
|
|
29
|
+
for reaction in model._yield(RateLaw)
|
|
30
|
+
)
|
|
27
31
|
lines.append("\\end{aligned}")
|
|
28
|
-
return "
|
|
32
|
+
return "\n".join(lines)
|
|
29
33
|
|
|
30
34
|
|
|
31
35
|
def latex_reactions(
|
|
32
|
-
model: type[System],
|
|
36
|
+
model: type[System],
|
|
37
|
+
transform: dict | None = None,
|
|
38
|
+
latex: ToLatex | None = None,
|
|
39
|
+
format: Literal["latex", "markdown"] = "latex",
|
|
33
40
|
) -> Latex:
|
|
34
41
|
if latex is None:
|
|
35
42
|
transform = transform if transform is not None else {}
|
|
36
43
|
latex = ToLatex(model, transform=transform)
|
|
37
|
-
|
|
44
|
+
aligned = aligned_reactions(model=model, latex=latex)
|
|
45
|
+
if format == "latex":
|
|
46
|
+
return "\\[ " + aligned + " \\]"
|
|
47
|
+
# Markdown renderers escape \[ \], so use $$ display math instead.
|
|
48
|
+
return "$$\n" + aligned + "\n$$"
|
|
38
49
|
|
|
39
50
|
|
|
40
51
|
def model_report(
|
|
@@ -59,7 +70,24 @@ def model_report(
|
|
|
59
70
|
)
|
|
60
71
|
|
|
61
72
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
73
|
+
def notebook_report(
|
|
74
|
+
model: type[System],
|
|
75
|
+
transform: dict | None = None,
|
|
76
|
+
descriptions: dict | None = None,
|
|
77
|
+
replace_algebraics: bool = False,
|
|
78
|
+
sections: Mapping[str, Callable[[type[System], ToLatex], Markdown]] = {
|
|
79
|
+
"Reactions": partial(latex_reactions, format="markdown")
|
|
80
|
+
}
|
|
81
|
+
| default_notebook_sections,
|
|
82
|
+
level: int = 2,
|
|
83
|
+
show: bool = True,
|
|
84
|
+
) -> Markdown | None:
|
|
85
|
+
return _notebook_report(
|
|
86
|
+
model=model,
|
|
87
|
+
transform=transform,
|
|
88
|
+
descriptions=descriptions,
|
|
89
|
+
replace_algebraics=replace_algebraics,
|
|
90
|
+
sections=sections,
|
|
91
|
+
level=level,
|
|
92
|
+
show=show,
|
|
93
|
+
)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""Install and check the ``dyscolab-modeling`` agent skill.
|
|
2
|
+
|
|
3
|
+
An alias of :mod:`poincare.skill`, which merges the skill files of poincare, simbio and
|
|
4
|
+
jablonski::
|
|
5
|
+
|
|
6
|
+
python -m simbio.skill install --dest .claude/skills
|
|
7
|
+
python -m simbio.skill check .claude/skills/dyscolab-modeling
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from poincare.skill import assemble, check, install, main
|
|
11
|
+
|
|
12
|
+
__all__ = ["assemble", "check", "install", "main"]
|
|
13
|
+
|
|
14
|
+
if __name__ == "__main__":
|
|
15
|
+
raise SystemExit(main())
|
|
@@ -291,7 +291,8 @@ def test_nested_compartments():
|
|
|
291
291
|
sim = Simulator(Model)
|
|
292
292
|
sim.solve(save_at=np.linspace(0, 10, 10))
|
|
293
293
|
|
|
294
|
-
assert Model.A
|
|
294
|
+
assert Model.A in sim.compiled.variables
|
|
295
|
+
assert Model.nested.A in sim.compiled.variables
|
|
295
296
|
assert Model._simbio_volume == Model.V
|
|
296
297
|
assert Model.nested._simbio_volume == Model.nested.V
|
|
297
298
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from . import MassAction, System, Variable, initial, model_report, notebook_report
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Model(System):
|
|
5
|
+
A: Variable = initial(default=1)
|
|
6
|
+
B: Variable = initial(default=0)
|
|
7
|
+
conversion = MassAction(reactants=[A], products=[2 * B], rate=1)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def test_model_report_reactions():
|
|
11
|
+
output = model_report(Model)
|
|
12
|
+
assert "\\subsection{Reactions}" in output
|
|
13
|
+
assert "\\rightarrow" in output
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_notebook_report_reactions():
|
|
17
|
+
output = notebook_report(Model, show=False)
|
|
18
|
+
assert output.startswith("## Reactions\n\n$$")
|
|
19
|
+
assert "\\rightarrow" in output
|
|
20
|
+
assert "\\[" not in output
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""simbio's part of the dyscolab-modeling agent skill (see poincare.skill)."""
|
|
2
|
+
|
|
3
|
+
import importlib.util
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from runpy import run_path
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
from poincare import skill
|
|
9
|
+
|
|
10
|
+
SOURCE = Path(__file__).parents[2] / skill.SOURCE_PATH
|
|
11
|
+
EXAMPLES = sorted((SOURCE / "examples").glob("*.py"))
|
|
12
|
+
NEEDS = {"simbio_sbml.py": "libsbml", "simbio_stochastic.py": "rebop"}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_part_is_found():
|
|
16
|
+
part = skill.find_part("simbio")
|
|
17
|
+
assert {"references/simbio.md", "references/simbio-api.md"} <= set(part.files)
|
|
18
|
+
assert all(Path(f).name.startswith("simbio") or "/data/" in f for f in part.files)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@pytest.mark.filterwarnings("ignore")
|
|
22
|
+
@pytest.mark.parametrize("example", EXAMPLES, ids=lambda p: p.stem)
|
|
23
|
+
def test_example(example: Path):
|
|
24
|
+
if (module := NEEDS.get(example.name)) and importlib.util.find_spec(module) is None:
|
|
25
|
+
pytest.skip(f"needs {module}")
|
|
26
|
+
run_path(str(example), run_name="test")
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
from pytest import mark
|
|
2
|
-
from symbolite.abstract import real
|
|
3
|
-
|
|
4
|
-
from . import mathMLImporter, to_mathML
|
|
5
|
-
from .symbol import MathMLSymbol as Symbol
|
|
6
|
-
|
|
7
|
-
x, y = map(Symbol, ["x", "y"])
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
@mark.parametrize(
|
|
11
|
-
"expr",
|
|
12
|
-
[
|
|
13
|
-
1,
|
|
14
|
-
x,
|
|
15
|
-
x * y,
|
|
16
|
-
x + y,
|
|
17
|
-
x * 2,
|
|
18
|
-
2 * x,
|
|
19
|
-
2 * x + y,
|
|
20
|
-
x**2,
|
|
21
|
-
x**0.5,
|
|
22
|
-
real.cos(x),
|
|
23
|
-
real.sqrt(x),
|
|
24
|
-
x < 1,
|
|
25
|
-
x < y,
|
|
26
|
-
~x,
|
|
27
|
-
],
|
|
28
|
-
)
|
|
29
|
-
def test_mathML_roundtrip(expr: Symbol):
|
|
30
|
-
node = to_mathML(expr)
|
|
31
|
-
expr2 = mathMLImporter().convert(node)
|
|
32
|
-
assert expr2 == expr
|
|
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
|