10
10
def edata_fixture ():
11
11
"""edata is generated to test pre- and postequilibration"""
12
12
edata_pre = amici .ExpData (2 , 0 , 0 ,
13
- np .array ([0. , 0.1 , 0.2 , 0.5 , 1. , 2. , 5. , 10. ]))
13
+ np .array ([0. , 0.1 , 0.2 , 0.5 , 1. , 2. , 5. , 10. ]))
14
14
edata_pre .setObservedData ([1.5 ] * 16 )
15
15
edata_pre .fixedParameters = np .array ([5. , 20. ])
16
16
edata_pre .fixedParametersPreequilibration = np .array ([0. , 10. ])
17
17
edata_pre .reinitializeFixedParameterInitialStates = True
18
18
19
19
# edata for postequilibration
20
20
edata_post = amici .ExpData (2 , 0 , 0 ,
21
- np .array ([float ('inf' )] * 3 ))
21
+ np .array ([float ('inf' )] * 3 ))
22
22
edata_post .setObservedData ([0.75 ] * 6 )
23
23
edata_post .fixedParameters = np .array ([7.5 , 30. ])
24
24
25
25
# edata with both equilibrations
26
26
edata_full = amici .ExpData (2 , 0 , 0 ,
27
- np .array ([0. , 0. , 0. , 1. , 2. , 2. , 4. , float ('inf' ), float ('inf' )]))
27
+ np .array (
28
+ [0. , 0. , 0. , 1. , 2. , 2. , 4. , float ('inf' ),
29
+ float ('inf' )]))
28
30
edata_full .setObservedData ([3.14 ] * 18 )
29
31
edata_full .fixedParameters = np .array ([1. , 2. ])
30
32
edata_full .fixedParametersPreequilibration = np .array ([3. , 4. ])
@@ -42,7 +44,7 @@ def models():
42
44
sbml_importer = amici .SbmlImporter (sbml_file )
43
45
44
46
# Name of the model that will also be the name of the python module
45
- model_name = model_output_dir = 'model_constant_species'
47
+ model_name = model_output_dir = 'model_constant_species'
46
48
model_name_cl = model_output_dir_cl = 'model_constant_species_cl'
47
49
48
50
# Define constants, observables, sigmas
@@ -67,9 +69,11 @@ def models():
67
69
compute_conservation_laws = False )
68
70
69
71
# load both models
70
- model_without_cl_module = amici .import_model_module (model_name ,
72
+ model_without_cl_module = amici .import_model_module (
73
+ model_name ,
71
74
module_path = os .path .abspath (model_name ))
72
- model_with_cl_module = amici .import_model_module (model_name_cl ,
75
+ model_with_cl_module = amici .import_model_module (
76
+ model_name_cl ,
73
77
module_path = os .path .abspath (model_name_cl ))
74
78
75
79
# get the models and return
@@ -81,8 +85,8 @@ def models():
81
85
def get_results (model , edata = None , sensi_order = 0 ,
82
86
sensi_meth = amici .SensitivityMethod .forward ,
83
87
sensi_meth_preeq = amici .SensitivityMethod .forward ,
88
+ stst_sensi_mode = amici .SteadyStateSensitivityMode .newtonOnly ,
84
89
reinitialize_states = False ):
85
-
86
90
# set model and data properties
87
91
model .setReinitializeFixedParameterInitialStates (reinitialize_states )
88
92
@@ -92,6 +96,7 @@ def get_results(model, edata=None, sensi_order=0,
92
96
solver .setSensitivityOrder (sensi_order )
93
97
solver .setSensitivityMethodPreequilibration (sensi_meth_preeq )
94
98
solver .setSensitivityMethod (sensi_meth )
99
+ model .setSteadyStateSensitivityMode (stst_sensi_mode )
95
100
if edata is None :
96
101
model .setTimepoints (np .linspace (0 , 5 , 101 ))
97
102
else :
@@ -134,9 +139,6 @@ def test_compare_conservation_laws_sbml(models, edata_fixture):
134
139
assert np .isclose (rdata [field ], rdata_cl [field ]).all (), field
135
140
136
141
# ----- compare simulations wo edata, sensi = 0, states and sensis -------
137
- model_without_cl .setSteadyStateSensitivityMode (
138
- amici .SteadyStateSensitivityMode .simulationFSA
139
- )
140
142
141
143
# run simulations
142
144
edata , _ , _ = edata_fixture
@@ -154,7 +156,10 @@ def test_compare_conservation_laws_sbml(models, edata_fixture):
154
156
# run simulations
155
157
rdata_cl = get_results (model_with_cl , edata = edata , sensi_order = 1 )
156
158
assert rdata_cl ['status' ] == amici .AMICI_SUCCESS
157
- rdata = get_results (model_without_cl , edata = edata , sensi_order = 1 )
159
+ rdata = get_results (
160
+ model_without_cl , edata = edata , sensi_order = 1 ,
161
+ stst_sensi_mode = amici .SteadyStateSensitivityMode .integrateIfNewtonFails
162
+ )
158
163
assert rdata ['status' ] == amici .AMICI_SUCCESS
159
164
# check that steady state computation succeeded only by sim in full model
160
165
assert (rdata ['preeq_status' ] == np .array ([- 3 , 1 , 0 ])).all ()
@@ -178,43 +183,50 @@ def test_compare_conservation_laws_sbml(models, edata_fixture):
178
183
179
184
def test_adjoint_pre_and_post_equilibration (edata_fixture ):
180
185
# get both models
181
- model_module = amici .import_model_module ('model_constant_species' ,
186
+ model_module = amici .import_model_module (
187
+ 'model_constant_species' ,
182
188
module_path = os .path .abspath ('model_constant_species' ))
183
189
model = model_module .getModel ()
184
- model_module_cl = amici .import_model_module ('model_constant_species_cl' ,
190
+ model_module_cl = amici .import_model_module (
191
+ 'model_constant_species_cl' ,
185
192
module_path = os .path .abspath ('model_constant_species_cl' ))
186
193
model_cl = model_module_cl .getModel ()
187
194
188
195
# check gradient with and without state reinitialization
189
196
for edata in edata_fixture :
190
197
for reinit in [False , True ]:
191
- # --- compare different ways of preequilibration, full rank Jacobian --- ------
198
+ # --- compare different ways of preequilibration, full rank Jacobian ------
192
199
# forward preequilibration, forward simulation
193
- rff_cl = get_results (model_cl , edata = edata , sensi_order = 1 ,
194
- sensi_meth = amici .SensitivityMethod .forward ,
195
- sensi_meth_preeq = amici .SensitivityMethod .forward ,
196
- reinitialize_states = reinit )
200
+ rff_cl = get_results (
201
+ model_cl , edata = edata , sensi_order = 1 ,
202
+ sensi_meth = amici .SensitivityMethod .forward ,
203
+ sensi_meth_preeq = amici .SensitivityMethod .forward ,
204
+ reinitialize_states = reinit )
197
205
# forward preequilibration, adjoint simulation
198
- rfa_cl = get_results (model_cl , edata = edata , sensi_order = 1 ,
199
- sensi_meth = amici .SensitivityMethod .adjoint ,
200
- sensi_meth_preeq = amici .SensitivityMethod .forward ,
201
- reinitialize_states = reinit )
206
+ rfa_cl = get_results (
207
+ model_cl , edata = edata , sensi_order = 1 ,
208
+ sensi_meth = amici .SensitivityMethod .adjoint ,
209
+ sensi_meth_preeq = amici .SensitivityMethod .forward ,
210
+ reinitialize_states = reinit )
202
211
# adjoint preequilibration, adjoint simulation
203
- raa_cl = get_results (model_cl , edata = edata , sensi_order = 1 ,
204
- sensi_meth = amici .SensitivityMethod .adjoint ,
205
- sensi_meth_preeq = amici .SensitivityMethod .adjoint ,
206
- reinitialize_states = reinit )
212
+ raa_cl = get_results (
213
+ model_cl , edata = edata , sensi_order = 1 ,
214
+ sensi_meth = amici .SensitivityMethod .adjoint ,
215
+ sensi_meth_preeq = amici .SensitivityMethod .adjoint ,
216
+ reinitialize_states = reinit )
207
217
208
218
# assert all are close
209
219
assert np .isclose (rff_cl ['sllh' ], rfa_cl ['sllh' ]).all ()
210
220
assert np .isclose (rfa_cl ['sllh' ], raa_cl ['sllh' ]).all ()
211
221
assert np .isclose (raa_cl ['sllh' ], rff_cl ['sllh' ]).all ()
212
222
213
- # --- compare fully adjoint approach to simulation with singular Jacobian ----
214
- raa = get_results (model , edata = edata , sensi_order = 1 ,
215
- sensi_meth = amici .SensitivityMethod .adjoint ,
216
- sensi_meth_preeq = amici .SensitivityMethod .adjoint ,
217
- reinitialize_states = reinit )
223
+ # --- compare fully adjoint approach to simulation with singular Jacobian ----
224
+ raa = get_results (
225
+ model , edata = edata , sensi_order = 1 ,
226
+ sensi_meth = amici .SensitivityMethod .adjoint ,
227
+ sensi_meth_preeq = amici .SensitivityMethod .adjoint ,
228
+ stst_sensi_mode = amici .SteadyStateSensitivityMode .integrateIfNewtonFails ,
229
+ reinitialize_states = reinit )
218
230
219
231
# assert gradients are close (quadrature tolerances are laxer)
220
232
assert np .isclose (raa_cl ['sllh' ], raa ['sllh' ], 1e-5 , 1e-5 ).all ()
0 commit comments