Skip to content

Commit 10d9f00

Browse files
committed
add reinitialization test
1 parent 63f371a commit 10d9f00

14 files changed

+510
-2
lines changed

petabtests/cases/v2.0.0/sbml/0021/0021.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
case = PetabTestCase(
4545
id=21,
46-
brief="Simulation. Nothing special.",
46+
brief="Observable-dependent noise formula.",
4747
description=DESCRIPTION,
4848
model=DEFAULT_SBML_FILE,
4949
condition_dfs=[],
+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
from inspect import cleandoc
2+
3+
from petab.v2.C import *
4+
from petab.v2 import Problem
5+
from petabtests import (
6+
PetabTestCase,
7+
analytical_a,
8+
antimony_to_sbml_str,
9+
analytical_b,
10+
)
11+
from pathlib import Path
12+
13+
DESCRIPTION = cleandoc("""
14+
## Objective
15+
16+
This case tests simultaneous re-initialization of compartment size and
17+
contained species.
18+
19+
## Model
20+
21+
Two simple conversion reactions `A <=> B`, `a <=> b` in a single compartment,
22+
following mass action kinetics.
23+
24+
`A` and `B` are species defined in terms of concentrations, `a` and `b` are
25+
`hasOnlySubstanceUnits=True` species defined in terms of amounts.
26+
27+
The compartment size and the species amounts/concentrations are re-initialized
28+
at time 10. The assigned values are independent of the model state.
29+
30+
At time 10, the conditions table changes:
31+
* The compartment size from 2 to 4.
32+
* The concentration of `A` to `5`. This implies that the amount of
33+
`A` is changed to `5 * 2 = 10` (concentration * compartment size).
34+
* The amount of `a` to `10`.
35+
36+
This leads to the following model state at time 10:
37+
* The concentration of `A` is `10 / 4 = 2.5`
38+
(new amount divided by new volume).
39+
* The amount of `a` is `10`, unaffected by the compartment size change.
40+
* The concentration of `B` is `(2 * 2) / 4 = 1`
41+
(previous amount / new volume
42+
= previous concentration * previous volume / new volume).
43+
* The amount of `b` remains at `4`, unaffected by the compartment size change.
44+
""")
45+
46+
# problem --------------------------------------------------------------------
47+
48+
49+
sbml_file = Path(__file__).parent / "model.xml"
50+
a_c0 = 2
51+
b_c0 = 1
52+
a_c10 = 2.5
53+
b_c10 = 1
54+
vol0 = 2
55+
vol_new = 10
56+
k1 = 2
57+
k2 = 1
58+
59+
ant_model = f"""
60+
model petab_test_0022
61+
compartment default_compartment = {vol0}
62+
initial_conc_A = {a_c0}
63+
initial_conc_B = {b_c0}
64+
65+
species A in default_compartment = initial_conc_A
66+
substanceOnly species a in default_compartment = initial_conc_A * default_compartment
67+
68+
species B in default_compartment = initial_conc_B
69+
substanceOnly species b in default_compartment = initial_conc_B * default_compartment
70+
71+
k1 = {k1}
72+
k2 = {k2}
73+
74+
A' = k2 * B - k1 * A
75+
B' = - k2 * B + k1 * A
76+
a' = k2 * b - k1 * a
77+
b' = - k2 * b + k1 * a
78+
79+
# the result of the PEtab reinitialization should be the same as with
80+
# the following event and no reinitialization:
81+
#
82+
# at time >= 10: A = 5, a = 5 * default_compartment, default_compartment = 4
83+
end
84+
"""
85+
sbml_file.write_text(antimony_to_sbml_str(ant_model))
86+
87+
88+
problem = Problem()
89+
problem.add_observable("obs_a", "a", noise_formula="1")
90+
problem.add_observable("obs_A", "A", noise_formula="1")
91+
problem.add_observable("obs_b", "b", noise_formula="1")
92+
problem.add_observable("obs_B", "B", noise_formula="1")
93+
94+
problem.add_parameter("k1", lb=0, ub=10, nominal_value=k1, scale=LIN)
95+
problem.add_experiment("experiment1", 0, "", 10, "condition2")
96+
problem.add_condition(
97+
"condition2", "condition2", a=5 * vol0, A=5, default_compartment=vol_new
98+
)
99+
100+
101+
ts = [0, 5, 10, 15]
102+
for t in ts:
103+
problem.add_measurement("obs_a", "", t, 2)
104+
problem.add_measurement("obs_A", "", t, 2)
105+
problem.add_measurement("obs_b", "", t, 1)
106+
problem.add_measurement("obs_B", "", t, 1)
107+
108+
# solutions ------------------------------------------------------------------
109+
110+
simulation_df = problem.measurement_df.copy(deep=True).rename(
111+
columns={MEASUREMENT: SIMULATION}
112+
)
113+
simulation_df[SIMULATION] = [
114+
# a, A, b, B
115+
# t=0
116+
a_c0 * vol0,
117+
a_c0,
118+
b_c0 * vol0,
119+
b_c0,
120+
# t=5
121+
analytical_a(5, a_c0, b_c0, k1, k2) * vol0,
122+
analytical_a(5, a_c0, b_c0, k1, k2),
123+
analytical_b(5, a_c0, b_c0, k1, k2) * vol0,
124+
analytical_b(5, a_c0, b_c0, k1, k2),
125+
# t=10, re-initialize compartment size and contained species
126+
a_c10 * vol_new,
127+
a_c10,
128+
b_c10 * vol_new,
129+
b_c10,
130+
# t=15
131+
analytical_a(5, a_c10, b_c10, k1, k2) * vol_new,
132+
analytical_a(5, a_c10, b_c10, k1, k2),
133+
analytical_b(5, a_c10, b_c10, k1, k2) * vol_new,
134+
analytical_b(5, a_c10, b_c10, k1, k2),
135+
]
136+
137+
case = PetabTestCase(
138+
id=22,
139+
brief="Simultaneous re-initialization of compartment size and contained species.",
140+
description=DESCRIPTION,
141+
model=sbml_file,
142+
experiment_dfs=[problem.experiment_df],
143+
condition_dfs=[problem.condition_df],
144+
observable_dfs=[problem.observable_df],
145+
measurement_dfs=[problem.measurement_df],
146+
simulation_dfs=[simulation_df],
147+
parameter_df=problem.parameter_df,
148+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# PEtab test case 0022
2+
3+
## Objective
4+
5+
This case tests simultaneous re-initialization of compartment size and
6+
contained species.
7+
8+
## Model
9+
10+
Two simple conversion reactions `A <=> B`, `a <=> b` in a single compartment,
11+
following mass action kinetics.
12+
13+
`A` and `B` are species defined in terms of concentrations, `a` and `b` are
14+
`hasOnlySubstanceUnits=True` species defined in terms of amounts.
15+
16+
The compartment size and the species amounts/concentrations are re-initialized
17+
at time 10. The assigned values are independent of the model state.
18+
19+
At time 10, the conditions table changes:
20+
* The compartment size from 2 to 4.
21+
* The concentration of `A` to `5`. This implies that the amount of
22+
`A` is changed to `5 * 2 = 10` (concentration * compartment size).
23+
* The amount of `a` to `10`.
24+
25+
This leads to the following model state at time 10:
26+
* The concentration of `A` is `10 / 4 = 2.5`
27+
(new amount divided by new volume).
28+
* The amount of `a` is `10`, unaffected by the compartment size change.
29+
* The concentration of `B` is `(2 * 2) / 4 = 1`
30+
(previous amount / new volume
31+
= previous concentration * previous volume / new volume).
32+
* The amount of `b` remains at `4`, unaffected by the compartment size change.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
format_version: 2.0.0
2+
parameter_file: _parameters.tsv
3+
problems:
4+
- condition_files:
5+
- _conditions.tsv
6+
experiment_files:
7+
- _experiments.tsv
8+
measurement_files:
9+
- _measurements.tsv
10+
model_files:
11+
model_0:
12+
language: sbml
13+
location: _model.xml
14+
observable_files:
15+
- _observables.tsv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
chi2: 1220.9443344556003
2+
llh: -625.1751837590749
3+
simulation_files:
4+
- _simulations.tsv
5+
tol_chi2: 0.001
6+
tol_llh: 0.001
7+
tol_simulations: 0.001
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
conditionId targetId operationType targetValue conditionName
2+
condition2 a setCurrentValue 10 ['condition2']
3+
condition2 A setCurrentValue 5 ['condition2']
4+
condition2 default_compartment setCurrentValue 10 ['condition2']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
experimentId time conditionId
2+
experiment1 0
3+
experiment1 10 condition2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
observableId experimentId time measurement
2+
obs_a 0 2
3+
obs_A 0 2
4+
obs_b 0 1
5+
obs_B 0 1
6+
obs_a 5 2
7+
obs_A 5 2
8+
obs_b 5 1
9+
obs_B 5 1
10+
obs_a 10 2
11+
obs_A 10 2
12+
obs_b 10 1
13+
obs_B 10 1
14+
obs_a 15 2
15+
obs_A 15 2
16+
obs_b 15 1
17+
obs_B 15 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Created by libAntimony version v2.15.0 with libSBML version 5.20.2. -->
3+
<sbml xmlns="http://www.sbml.org/sbml/level3/version2/core" level="3" version="2">
4+
<model metaid="petab_test_0022" id="petab_test_0022">
5+
<listOfCompartments>
6+
<compartment sboTerm="SBO:0000410" id="default_compartment" spatialDimensions="3" size="2" constant="true"/>
7+
</listOfCompartments>
8+
<listOfSpecies>
9+
<species id="A" compartment="default_compartment" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
10+
<species id="a" compartment="default_compartment" hasOnlySubstanceUnits="true" boundaryCondition="false" constant="false"/>
11+
<species id="B" compartment="default_compartment" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
12+
<species id="b" compartment="default_compartment" hasOnlySubstanceUnits="true" boundaryCondition="false" constant="false"/>
13+
</listOfSpecies>
14+
<listOfParameters>
15+
<parameter id="initial_conc_A" value="2" constant="true"/>
16+
<parameter id="initial_conc_B" value="1" constant="true"/>
17+
<parameter id="k1" value="2" constant="true"/>
18+
<parameter id="k2" value="1" constant="true"/>
19+
</listOfParameters>
20+
<listOfInitialAssignments>
21+
<initialAssignment symbol="A">
22+
<math xmlns="http://www.w3.org/1998/Math/MathML">
23+
<ci> initial_conc_A </ci>
24+
</math>
25+
</initialAssignment>
26+
<initialAssignment symbol="a">
27+
<math xmlns="http://www.w3.org/1998/Math/MathML">
28+
<apply>
29+
<times/>
30+
<ci> initial_conc_A </ci>
31+
<ci> default_compartment </ci>
32+
</apply>
33+
</math>
34+
</initialAssignment>
35+
<initialAssignment symbol="B">
36+
<math xmlns="http://www.w3.org/1998/Math/MathML">
37+
<ci> initial_conc_B </ci>
38+
</math>
39+
</initialAssignment>
40+
<initialAssignment symbol="b">
41+
<math xmlns="http://www.w3.org/1998/Math/MathML">
42+
<apply>
43+
<times/>
44+
<ci> initial_conc_B </ci>
45+
<ci> default_compartment </ci>
46+
</apply>
47+
</math>
48+
</initialAssignment>
49+
</listOfInitialAssignments>
50+
<listOfRules>
51+
<rateRule variable="A">
52+
<math xmlns="http://www.w3.org/1998/Math/MathML">
53+
<apply>
54+
<minus/>
55+
<apply>
56+
<times/>
57+
<ci> k2 </ci>
58+
<ci> B </ci>
59+
</apply>
60+
<apply>
61+
<times/>
62+
<ci> k1 </ci>
63+
<ci> A </ci>
64+
</apply>
65+
</apply>
66+
</math>
67+
</rateRule>
68+
<rateRule variable="a">
69+
<math xmlns="http://www.w3.org/1998/Math/MathML">
70+
<apply>
71+
<minus/>
72+
<apply>
73+
<times/>
74+
<ci> k2 </ci>
75+
<ci> b </ci>
76+
</apply>
77+
<apply>
78+
<times/>
79+
<ci> k1 </ci>
80+
<ci> a </ci>
81+
</apply>
82+
</apply>
83+
</math>
84+
</rateRule>
85+
<rateRule variable="B">
86+
<math xmlns="http://www.w3.org/1998/Math/MathML">
87+
<apply>
88+
<plus/>
89+
<apply>
90+
<times/>
91+
<apply>
92+
<minus/>
93+
<ci> k2 </ci>
94+
</apply>
95+
<ci> B </ci>
96+
</apply>
97+
<apply>
98+
<times/>
99+
<ci> k1 </ci>
100+
<ci> A </ci>
101+
</apply>
102+
</apply>
103+
</math>
104+
</rateRule>
105+
<rateRule variable="b">
106+
<math xmlns="http://www.w3.org/1998/Math/MathML">
107+
<apply>
108+
<plus/>
109+
<apply>
110+
<times/>
111+
<apply>
112+
<minus/>
113+
<ci> k2 </ci>
114+
</apply>
115+
<ci> b </ci>
116+
</apply>
117+
<apply>
118+
<times/>
119+
<ci> k1 </ci>
120+
<ci> a </ci>
121+
</apply>
122+
</apply>
123+
</math>
124+
</rateRule>
125+
</listOfRules>
126+
</model>
127+
</sbml>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
observableId observableFormula noiseFormula
2+
obs_a a 1
3+
obs_A A 1
4+
obs_b b 1
5+
obs_B B 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
parameterId estimate nominalValue parameterScale lowerBound upperBound
2+
k1 1 2 lin 0 10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
observableId experimentId time simulation
2+
obs_a 0 4.0
3+
obs_A 0 2.0
4+
obs_b 0 2.0
5+
obs_B 0 1.0
6+
obs_a 5 2.000000611804641
7+
obs_A 5 1.0000003059023206
8+
obs_b 5 3.999999388195359
9+
obs_B 5 1.9999996940976794
10+
obs_a 10 25.0
11+
obs_A 10 2.5
12+
obs_b 10 10.0
13+
obs_B 10 1.0
14+
obs_a 15 11.666670745364273
15+
obs_A 15 1.1666670745364274
16+
obs_b 15 23.33332925463573
17+
obs_B 15 2.333332925463573

0 commit comments

Comments
 (0)