-
Notifications
You must be signed in to change notification settings - Fork 0
/
gMLV_ML.py
472 lines (350 loc) · 15.6 KB
/
gMLV_ML.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import numpy as np
import matplotlib.pyplot as plt
from numpy import linalg as la
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.linear_model import ElasticNet
from sklearn.model_selection import RepeatedKFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV
from scipy.integrate import odeint
from sklearn.base import BaseEstimator
from sklearn.base import RegressorMixin
#FIXME: this is a hack to stop the errors. Need to fix this properly
#from gMLV_sim import gLV, yobs, times, num_species, mu, M, y0, tX, tF, cRidge
class Ridge1(BaseEstimator, RegressorMixin):
"""Custom ridge regression class"""
def __init__(self, alphas=None, num_species=3):
self.coef_ = None
if alphas is None:
alphas = [0.1, 0.1]
self.alphas = alphas
self.num_species = num_species
def fit(self, X, y):
# print("calling fit")
self.coef_ = ridge_fit(X.T, y.T, self.alphas, self.num_species)
# return self
def predict(self, X):
return X @ self.coef_.T
def get_params(self, deep=True):
# suppose this estimator has parameters "alpha" and "recursive"
return {"alphas": self.alphas, "num_species": self.num_species}
# def set_params(self, **parameters):
# for parameter, value in parameters.items():
# setattr(self, parameter, value)
# return self
class Ridge2(BaseEstimator, RegressorMixin):
"""Custom ridge regression class"""
def __init__(self, alphas=None, num_species=3, num_pert=1):
self.coef_ = None
if alphas is None:
alphas = [0.1, 0.1, 0.1]
self.alphas = alphas
self.num_species = num_species
self.num_pert = num_pert
def fit(self, X, y):
# print("calling fit")
self.coef_ = ridge_fit_pert(X.T, y.T, self.alphas, self.num_species, self.num_pert)
# return self
def predict(self, X):
return X @ self.coef_.T
def get_params(self, deep=True):
# suppose this estimator has parameters "alpha" and "recursive"
return {"alphas": self.alphas, "num_species": self.num_species, "num_pert": self.num_pert}
def ridge_fit(X, F, alphas, num_species):
# To do: redo this with transpose X and Y
# standard least squares
# beta = np.dot( np.dot(F,np.transpose(X)), la.inv(np.dot(X,np.transpose(X))))
# compute ridge estimate
penalty = np.diagflat(np.hstack([np.repeat(alphas[0], num_species), alphas[1]]))
beta = (F @ X.T) @ la.inv(X @ X.T + penalty)
return beta
def ridge_fit_pert(X, F, alphas, num_species, num_pert):
# To do: redo this with transposed X and Y
# standard least squares
# beta = np.dot( np.dot(F,np.transpose(X)), la.inv(np.dot(X,np.transpose(X))))
# compute ridge estimate
penalty = np.diagflat(np.hstack([np.repeat(alphas[0], num_species), alphas[1], np.repeat(alphas[2], num_pert)]))
beta = np.dot(F, X.T) @ la.inv(X @ X.T + penalty)
return beta
# can use
# import importlib
# import gLV_ML
# importlib.reload(gLV_ML);
def ridge_fit_test(X, Y):
print("default ridge")
model = Ridge(alpha=0.01, fit_intercept=False)
model.fit(tX, tF)
print(model.coef_)
print(model.coef_.shape)
print(model.predict(tX))
print("custom ridge")
model = cRidge(alphas=[0.01, 0.01], num_species=num_species)
model.fit(tX, tF)
print(model.coef_)
print(model.coef_.shape)
print(model.predict(tX))
def linearize_time_course_16S(yobs, times):
num_species = yobs.shape[1]
nt = len(times)
# F = dlnX/dt
DlnX = np.diff(np.log(yobs), axis=0)
Dt = np.tile(np.diff(times), (num_species, 1))
# print(DlnX)
# print(Dt)
F = np.divide(DlnX, np.transpose(Dt))
# print(F)
# X matrix: stacked observed counts
X = np.vstack([np.transpose(yobs), np.ones(nt)])
# print(X)
# Get data into correct format for scikit-learn
tF = F
# print("tF:",np.shape(tF))
# remove last column of X and transpose to get design matrix
tX = np.transpose(X[:, 0:-1])
# print("tX:",np.shape(tX))
# plot data in one variable
# plt.scatter(tX[:,0], tF[:,0]);
return tX, tF
# here u should be of length timepoints
def linearize_time_course_16S_u(yobs, times, u):
num_species = yobs.shape[1]
nt = len(times)
# F = dlnX/dt
DlnX = np.diff(np.log(yobs), axis=0)
Dt = np.tile(np.diff(times), (num_species, 1))
F = np.divide(DlnX, np.transpose(Dt))
# X matrix: stacked observed counts
X = np.vstack([np.transpose(yobs), np.ones(nt), u])
# remove last column of X and transpose to get design matrix
tX = np.transpose(X[:, 0:-1])
# print("tX:",np.shape(tX))
return tX, F
def linearise_time_course_metabolites(sobs, yobs, times):
nm = sobs.shape[1]
ns = yobs.shape[1]
# S = ds/dt
DS = np.diff(sobs, axis=0)
Dt = np.tile(np.diff(times), (nm, 1))
S = np.divide(DS, np.transpose(Dt))
# X = dX/dt
DX = np.diff(yobs, axis=0)
Dt = np.tile(np.diff(times), (ns, 1))
X = np.divide(DX, np.transpose(Dt))
# X = yobs[0:-1, :]
return X, S
def linearize_time_course(yobs, times):
return linearize_time_course_16S(yobs, times)
def plot_coeffs():
n_alphas = 10
alphas = np.logspace(-5, 2, n_alphas)
print(alphas)
coefs = []
for a in alphas:
ridge = Ridge(alpha=a, fit_intercept=False)
ridge.fit(tX, tF)
# print( ridge.coef_.flatten() )
coefs.append(ridge.coef_.flatten())
ax = plt.gca()
ax.plot(alphas, coefs)
ax.set_xscale("log")
ax.set_xlim(ax.get_xlim()[::-1]) # reverse axis
plt.xlabel("alpha")
plt.ylabel("weights")
plt.title("Ridge coefficients as a function of the regularization")
plt.axis("tight")
plt.show()
def fit_alpha_Ridge1(X, F, num_species, n_a0, n_a1):
# use own ridge model
a0 = np.logspace(-2, 2, n_a0) # constraint on Mij matrix elements
a1 = np.logspace(-6, 0, n_a1) # constraint on mu
xv, yv = np.meshgrid(a0, a1, indexing='ij')
candidate_regressors = []
for i in range(n_a0):
for j in range(n_a1):
# print(i, j, xv[i,j], yv[i,j])
candidate_regressors.append(Ridge1(alphas=[xv[i, j], yv[i, j]], num_species=num_species))
cv = RepeatedKFold(n_splits=10, n_repeats=10)
cv_results = [-cross_val_score(r, X, F, scoring='neg_root_mean_squared_error', cv=cv) for r in candidate_regressors]
cv_means = np.array([np.mean(x) for x in cv_results])
cv_se = np.array([np.std(x) / np.sqrt(100) for x in cv_results])
min_i = np.argmin(cv_means)
inds = np.unravel_index(min_i, (n_a0, n_a1))
print("minimum found: a0/a1/error:", a0[inds[0]], a1[inds[1]], cv_means[min_i])
# unconstrained to compare
unc_model = Ridge1(alphas=[0, 0], num_species=num_species)
cv_results = -cross_val_score(unc_model, X, F, scoring='neg_root_mean_squared_error', cv=cv)
print("unconstrained error :", np.mean(cv_results))
return a0[inds[0]], a1[inds[1]]
def fit_alpha_Ridge2(X, F, num_species, num_pert, n_a0, n_a1, n_a2):
# use own ridge model
a0 = np.logspace(-6, 3, n_a0) # constraint on Mij matrix elements
a1 = np.logspace(-6, 3, n_a1) # constraint on mu
a2 = np.logspace(-6, 3, n_a2) # constraint on epsilon
xv, yv, zv = np.meshgrid(a0, a1, a2, indexing='ij')
candidate_regressors = []
for i in range(n_a0):
for j in range(n_a1):
for k in range(n_a2):
# print(i, j, xv[i,j], yv[i,j])
candidate_regressors.append(Ridge2(alphas=[xv[i, j, k], yv[i, j, k], zv[i, j, k]],
num_species=num_species,
num_pert=num_pert))
cv = RepeatedKFold(n_splits=10, n_repeats=10)
cv_results = [-cross_val_score(r, X, F, scoring='neg_root_mean_squared_error', cv=cv) for r in candidate_regressors]
cv_means = np.array([np.mean(x) for x in cv_results])
cv_se = np.array([np.std(x) / np.sqrt(100) for x in cv_results])
min_i = np.argmin(cv_means)
inds = np.unravel_index(min_i, (n_a0, n_a1, n_a2))
print("minimum found: a0/a1/a2/error:", a0[inds[0]], a1[inds[1]], a2[inds[2]], cv_means[min_i])
# unconstrained to compare
unc_model = Ridge2(alphas=[0, 0, 0], num_species=num_species, num_pert=num_pert)
cv_results = -cross_val_score(unc_model, X, F, scoring='neg_root_mean_squared_error', cv=cv)
print("unconstrained error :", np.mean(cv_results))
return a0[inds[0]], a1[inds[1]], a2[inds[2]]
def do_final_fit_Ridge1(X, F, num_species, a0, a1):
model = Ridge1(alphas=[a0, a1], num_species=num_species)
model.fit(X, F)
mu_h = [model.coef_[i][-1] for i in range(0, num_species)]
M_h = [model.coef_[i][0:num_species].tolist() for i in range(0, num_species)]
return mu_h, M_h
def do_final_fit_Ridge2(X, F, num_species, num_pert, a0, a1, a2):
model = Ridge2(alphas=[a0, a1, a2], num_species=num_species, num_pert=num_pert)
model.fit(X, F)
M_h = [model.coef_[i][0:num_species].tolist() for i in range(0, num_species)]
mu_h = [model.coef_[i][num_species] for i in range(0, num_species)]
e_h = [model.coef_[i][(num_species+1):] for i in range(0, num_species)]
return mu_h, M_h, e_h
def do_bootstrapping(X, F, num_species, a0, a1, nt, nboots=100):
# do some bootstrapping
model = Ridge1(alphas=[a0, a1], num_species=num_species)
mus = np.zeros([nboots, num_species])
mms = np.zeros([nboots, num_species * num_species])
for i in range(0, nboots):
sample_index = np.random.choice(range(0, nt - 1), nt - 1)
X_s = X[sample_index, :]
F_s = F[sample_index, :]
model.fit(X_s, F_s)
mu_h = [model.coef_[i][-1] for i in range(0, num_species)]
M_h = [model.coef_[i][0:num_species].tolist() for i in range(0, num_species)]
mus[i, :] = mu_h
mms[i, :] = np.array(M_h).flatten()
# print(np.array(mu_h))
# print(np.round(np.array(M_h),decimals=2))
print("examining mu_i")
mus_max = mus.max(axis=0)
mus_min = mus.min(axis=0)
for i in range(0, num_species):
star = ""
if np.abs(mus_min[i] - mus_max[i]) > 1e-4:
if mus_min[i] > 0 and mus_max[i] > 0:
star = "*"
elif mus_min[i] < 0 and mus_max[i] < 0:
star = "*"
print(i, np.round(mus_min[i], decimals=3), " - ", np.round(mus_max[i], decimals=3), star)
mms_max = mms.max(axis=0)
mms_min = mms.min(axis=0)
print("\nexamining Mij")
for i in range(0, num_species * num_species):
star = ""
if np.abs(mms_min[i] - mms_max[i]) > 1e-4:
if mms_min[i] > 0 and mms_max[i] > 0:
star = "*"
elif mms_min[i] < 0 and mms_max[i] < 0:
star = "*"
print(i + 1, np.unravel_index(i, (num_species, num_species)), np.round(mms_min[i], decimals=3), " - ",
np.round(mms_max[i], decimals=3), star)
def plot_alpha_lasso(X, S, n_a):
candidate_alpha = np.logspace(-1, 2, n_a)
candidate_regressors = [Lasso(alpha=a, fit_intercept=False, max_iter=10000, tol=1e-1) for a in candidate_alpha]
coefs = [r.fit(X, S).coef_.flatten() for r in candidate_regressors]
plt.figure()
ax = plt.gca()
ax.plot(candidate_alpha, coefs)
ax.set_xscale("log")
ax.set_xlim(ax.get_xlim()[::-1]) # reverse axis
plt.ylim(-1, 1)
plt.xlabel("alpha")
plt.ylabel("weights")
plt.title("Lasso coefficients as a function of the regularization")
plt.axis("tight")
plt.show()
def fit_alpha_lasso(X, S, n_a):
candidate_alpha = np.logspace(-1, 2, n_a)
candidate_regressors = [Lasso(alpha=a, fit_intercept=False, max_iter=10000, tol=1e-1) for a in candidate_alpha]
cv = RepeatedKFold(n_splits=10, n_repeats=10)
cv_results = [-cross_val_score(r, X, S, scoring='neg_root_mean_squared_error', cv=cv, n_jobs=-1) for r in
candidate_regressors]
n_est = np.array([len(x) for x in cv_results])
cv_means = np.array([np.mean(x) for x in cv_results])
cv_se = np.array([np.std(x) / np.sqrt(100) for x in cv_results])
min_i = np.argmin(cv_means)
cutoff = cv_means[min_i] + cv_se[min_i]
one_se_rule_i = np.argmax(candidate_alpha * (cv_means < cutoff))
print("minimum found: a/error:", candidate_alpha[min_i], cv_means[min_i])
print("min + se rule: a/error:", candidate_alpha[one_se_rule_i], cv_means[one_se_rule_i])
plt.figure()
plt.plot(candidate_alpha, cv_means)
plt.fill_between(candidate_alpha, cv_means + 1 * cv_se, cv_means - 1 * cv_se, alpha=.1)
plt.axhline(cutoff, linestyle='dotted', label='Best + One SE')
plt.scatter([candidate_alpha[one_se_rule_i]], [cv_means[one_se_rule_i]], marker='o', color='orange',
label='One SE Rule')
plt.scatter([candidate_alpha[min_i]], [cv_means[min_i]], marker='o', color='blue', label='Minimum rule')
plt.legend()
plt.xscale('log')
plt.xlabel('Log alpha')
plt.ylabel('MSE')
plt.show()
return candidate_alpha[min_i], candidate_alpha[one_se_rule_i]
###########################################################
# older function using other more standard methods. Might come back to these at some point
def fit_alpha_default():
# find the optimal penalisation terms
# model = Ridge(fit_intercept=False)
model = Lasso(fit_intercept=False, max_iter=10000)
# model = ElasticNet(fit_intercept=False, max_iter=100000, l1_ratio=0.9, tol=1e-2)
cv = RepeatedKFold(n_splits=5, n_repeats=3, random_state=1) # five fold
n_alphas = 100
grid = dict()
grid['alpha'] = np.logspace(-6, 0, n_alphas)
# define search
search = GridSearchCV(model, grid, scoring='neg_mean_squared_error', cv=cv, n_jobs=-1)
# perform the search
results = search.fit(tX, tF)
# summarize
print('MAE: %.3f' % results.best_score_)
print('Config: %s' % results.best_params_)
# fit using optimal alpha
# model = Ridge(alpha=results.best_params_['alpha'], fit_intercept=False)
model = Lasso(alpha=results.best_params_['alpha'], fit_intercept=False, max_iter=10000)
# model = ElasticNet(alpha=results.best_params_['alpha'], fit_intercept=False, max_iter=100000, l1_ratio=0.9, tol=1e-2)
# model = ElasticNet(alpha=0.01, fit_intercept=False, max_iter=100000, l1_ratio=0.9, tol=1e-2)
model.fit(tX, tF)
mu_h = [model.coef_[i][-1] for i in range(0, num_species)]
M_h = [model.coef_[i][0:num_species].tolist() for i in range(0, num_species)]
modelB = LinearRegression(fit_intercept=False)
modelB.fit(tX, tF)
mu_l = [modelB.coef_[i][-1] for i in range(0, num_species)]
M_l = [modelB.coef_[i][0:num_species].tolist() for i in range(0, num_species)]
print("\ninferred params:")
print("mu_hat/mu/mu_l:")
print(np.array(mu_h))
print(np.array(mu))
print(np.array(mu_l))
print("\nM_hat/M/M_l:")
print(np.round(np.array(M_h), decimals=2))
print("\n", np.array(M))
print("\n", np.round(np.array(M_l), decimals=2))
# plot the fit
yobs_pred = odeint(gLV, y0, times, args=(num_species, mu_h, M_h))
plt.plot(times, yobs)
plt.plot(times, yobs_pred, '--')
# plot the params
plt.figure()
plt.stem(np.arange(0, len(mu), dtype="int32"), np.array(mu_h), markerfmt="D")
plt.stem(np.arange(0, len(mu), dtype="int32"), np.array(mu), markerfmt="X")
plt.figure()
plt.stem(np.arange(0, num_species * num_species), np.array(M_h).flatten(), markerfmt="D")
plt.stem(np.arange(0, num_species * num_species), np.array(M).flatten(), markerfmt="X")