-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate_model.py
268 lines (178 loc) · 6.11 KB
/
validate_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
'''
Validates the dynamical viability of a set of estimated parameter values. Call
pattern:
python validate_model.py <output directory>
'''
from __future__ import division
import os.path as pa
from itertools import izip
import sys
import numpy as np
# np.random.seed(33)
import matplotlib.pyplot as plt
import scipy.integrate
import constants
import equations
import structure
from utils.linalg import approx_jac
# Execution options
FORCE = True
DRY_RUN = False
# Criterion
EQU_CONC_THRESHOLD = 1.001
CONC_FOLD_PERTURBATION = 2
CONC_FOLD_CONVERGENCE = 1.01
# PERTURBATION_SCALE = constants.RT * np.log(CONC_FOLD_PERTURBATION)
# CONVERGENCE_SCALE = constants.RT * np.log(CONC_FOLD_CONVERGENCE)
# N_PERTURBATIONS = 30
APPROX_JAC_RADIUS = 1e-5
PERTURBATION_RECOVERY_TIME_TOLERANCE = 3
EXPECTED_RECOVERY_EPOCHS = np.log((CONC_FOLD_PERTURBATION - 1)/(CONC_FOLD_CONVERGENCE - 1))
PERTURBATION_RECOVERY_EPOCHS = PERTURBATION_RECOVERY_TIME_TOLERANCE * EXPECTED_RECOVERY_EPOCHS
TARGET_PYRUVATE_PRODUCTION = 0.14e-3
FLUX_FIT_THRESHOLD = 1e-3
# ODE integration parameters
DT = 1e1
T_INIT = 0
INTEGRATOR = 'lsoda'
INTEGRATOR_OPTIONS = dict(
atol = 1e-6 # Default absolute tolerance is way too low (1e-12)
)
def load_pars(target_dir):
obj = np.load(pa.join(target_dir, 'obj.npy'))
pars = np.load(pa.join(target_dir, 'pars.npy'))
return obj, pars
conc_ind = [
structure.parameters.index(structure.GLC.format(compound))
for compound in structure.DYNAMIC_COMPOUNDS
]
is_dynamic = np.zeros(structure.n_parameters, np.bool)
is_dynamic[conc_ind] = True
is_static = ~is_dynamic
def dg_dt(glc, pars):
x = structure.glc_association_matrix.T.dot(glc)
x[is_static] = pars[is_static]
return equations.dglc_dt(x, *equations.args)
def init_dg_dt(pars):
return structure.glc_association_matrix.dot(pars)
target_dir = sys.argv[1]
# stable_path = pa.join(target_dir, 'stable.npy')
equ_path = pa.join(target_dir, 'equ.npy')
lre_path = pa.join(target_dir, 'lre.npy')
valid_path = pa.join(target_dir, 'valid.npy')
paths = [
# stable_path,
equ_path,
lre_path,
valid_path
]
if not FORCE and all(pa.exists(path) for path in paths):
print 'Skipping {}, output already exists'.format(target_dir)
else:
print 'Validating {} parameters'.format(target_dir)
(all_obj, all_pars) = load_pars(target_dir)
equ = []
lre = []
# stable = []
for (i, pars) in enumerate(all_pars.T):
dx_dt = lambda t, x: dg_dt(x, pars)
x_start = init_dg_dt(pars)
t_final = PERTURBATION_RECOVERY_EPOCHS / constants.MU
ode = scipy.integrate.ode(dx_dt)
ode.set_initial_value(x_start, T_INIT)
ode.set_integrator(
INTEGRATOR,
**INTEGRATOR_OPTIONS
)
# x_hist = [x_start.copy()]
while ode.successful() and ode.t < t_final:
x_curr = ode.integrate(ode.t + DT)
# TODO: terminate when x_curr stops changing
# x_hist.append(ode.integrate(ode.t + DT))
# x_curr = x_hist[-1]
# all_x = np.array(x_hist)
# f = plt.figure()
# plt.plot(all_x - x_curr[None, :])
# plt.savefig('ode.pdf')
x_eq = x_curr
pars_final = structure.glc_association_matrix.T.dot(x_eq)
pars_final[is_static] = pars[is_static]
v = equations.reaction_rates(pars_final, *equations.args)
net_pyruvate_production = v[-2] - v[-1]
flux_fit = (net_pyruvate_production / TARGET_PYRUVATE_PRODUCTION - 1)**2
flux_is_fit = (flux_fit < FLUX_FIT_THRESHOLD)
normed_log_conc_deviation = np.linalg.norm(x_eq - x_start, 2) / constants.RT
if not ode.successful() or not (normed_log_conc_deviation < np.log(EQU_CONC_THRESHOLD)):
equ.append(False)
lre.append(None)
# stable.append(False)
if not ode.successful():
print 'ODE integration failure'
else:
print 'Initial concentrations too far from equilibrium ({:0.2e}, should be < 1)'.format(
normed_log_conc_deviation / np.log(EQU_CONC_THRESHOLD)
)
continue
else:
equ.append(flux_is_fit)
if not flux_is_fit:
v_init = equations.reaction_rates(pars, *equations.args)
print 'flux error:', net_pyruvate_production, v_init[-2] - v_init[-1]
# if not flux_is_fit: print 'bad flux: {}'.format(net_pyruvate_production)
# import ipdb; ipdb.set_trace()
# x_eq = init_dg_dt(pars)
jac = approx_jac(lambda x: dx_dt(T_INIT, x), x_eq, APPROX_JAC_RADIUS)
(eigvals, eigvecs) = np.linalg.eig(jac)
largest_real_eigenvalue = np.max(np.real(eigvals))
# print largest_real_eigenvalue / constants.MU
lre.append(largest_real_eigenvalue)
print i, np.mean(equ)
# if largest_real_eigenvalue >= 0:
# stable.append(False)
# continue
# t_final = -PERTURBATION_RECOVERY_EPOCHS / largest_real_eigenvalue
# # x_final = []
# for p in xrange(N_PERTURBATIONS):
# # x_init = x_eq + (np.random.uniform(size = x_eq.size) - 0.5) * PERTURBATION_SCALE
# perturbation = np.random.normal(size = x_eq.size)
# perturbation /= np.linalg.norm(perturbation, 2)
# perturbation *= PERTURBATION_SCALE
# x_init = x_eq + perturbation
# ode = scipy.integrate.ode(dx_dt)
# ode.set_initial_value(x_init, T_INIT)
# ode.set_integrator(
# INTEGRATOR,
# # **INTEGRATOR_OPTIONS # TODO
# )
# x_curr = x_init.copy()
# while ode.successful() and ode.t < t_final and not np.linalg.norm(x_curr - x_eq, 2) < CONVERGENCE_SCALE:
# x_curr = ode.integrate(ode.t + DT)
# # print np.linalg.norm(x_curr - x_eq, 2) / CONVERGENCE_SCALE
# # x_final.append(x_curr)
# if not ode.successful():
# # print 'integration failed'
# stable.append(False)
# break
# elif ode.t >= t_final:
# # print 'failed to converge'
# # print ode.t
# # print np.abs(x_curr - x_eq)
# # print constants.RT * np.log(CONC_FOLD_CONVERGENCE)
# # print dx_dt(0, x_curr)
# stable.append(False)
# break
# # else:
# # print 'recovered by {} (expected ~{})'.format(ode.t, -EXPECTED_RECOVERY_EPOCHS/largest_real_eigenvalue)
# else:
# stable.append(True)
# print i, np.mean(equ), np.mean(stable)
# stable = np.array(stable, np.bool)
equ = np.array(equ, np.bool)
lre = np.array(lre, np.float64)
valid = ((lre < 0) & equ)
if not DRY_RUN:
# np.save(stable_path, stable)
np.save(equ_path, equ)
np.save(lre_path, lre)
np.save(valid_path, valid)
print '{:0.2%}'.format(valid.mean())