-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.py
274 lines (231 loc) · 11.1 KB
/
main.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
import argparse
import logging
import os
# Keras
from keras.callbacks import EarlyStopping
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense
from keras.regularizers import l1, l2
# Numpy
import numpy as np
#Pandas
import pandas as pd
# sklearn and scipy
from sklearn.model_selection import train_test_split
from scipy.stats import pearsonr as r
from GA.utils.utils import retrieve_data
from GA.evolve_cnn.train import compile_model_cnn
from GA.evolve.train import compile_model_mlp
from GA.utils.utils import convert_to_individual_alleles
early_stopper = EarlyStopping(monitor='val_loss', min_delta=0.1, patience=2, verbose=0, mode='auto')
def CNN(traits=['height', 'BMI', 'WHR', 'BHMD', 'SBP'], verbose=0, unif=False, nbsnps=10000, p=None, reps=1):
#cnn1
param = list({'optimizer': 'nadam', 'size_window': 2, 'activation': 'softplus', 'nb_neurons': 64, 'stride': 'one',
'nb_cnn_layers': 1, 'filters': 16, 'weight_decay': 0.0, 'nb_layers': 3,
'dropout': 0.01, 'batch_norm': True})
#cnn2
param.append({'optimizer': 'nadam', 'size_window': 2, 'activation': 'elu', 'nb_neurons': 32, 'stride': 'one',
'nb_cnn_layers': 1, 'filters': 32, 'weight_decay': 0.0, 'nb_layers': 3,
'dropout': 0.01, 'batch_norm': False})
#cnn3
param.append({'optimizer': 'rmsprop', 'size_window': 3, 'activation': 'linear', 'nb_neurons': 32, 'stride': 'one',
'nb_cnn_layers': 1, 'filters': 16, 'weight_decay': 0.0, 'nb_layers': 1,
'dropout': 0.01, 'batch_norm': False})
R = {}
for t in traits:
best = 0
print(t)
x_tr, x_tst, y_tr, y_tst = retrieve_data(t, nbsnps, unif=unif)
x_tr, x_val, y_tr, y_val = train_test_split(x_tr, y_tr, test_size=0.33)
n_snps = x_tr.shape[1]
x_tr = np.expand_dims(x_tr, axis=2)
x_val = np.expand_dims(x_val, axis=2)
x_tst = np.expand_dims(x_tst, axis=2)
f = os.path.join(os.path.expanduser("~"), 'Code/genomic_cnn/models', "Model_" + t + "_cnn_"
+ str(n_snps / 1000) + "k" + ("_unif" if unif else "_best") + ".h5")
n = 0
if p is None:
res = np.zeros((len(param), 2))
for g in param:
print(g)
for x in range(0, reps):
m = compile_model_cnn(g, (n_snps, 1))
m.fit(x_tr, y_tr, epochs=1200, verbose=verbose, validation_data=(x_val, y_val),
callbacks = [early_stopper])
if r(m.predict(x_val).ravel(), y_val)[0] > res[n, 0]:
print(r(m.predict(x_val).ravel(), y_val)[0])
print(x)
res[n, 0] = r(m.predict(x_val).ravel(), y_val)[0]
res[n, 1] = r(m.predict(x_tst).ravel(), y_tst)[0]
if res[n, 0] > best:
print("A better network was found with r: %.3f" % res[n, 0])
print(g)
m.save(f)
best = res[n, 0]
n = n + 1
else:
res = np.zeros((reps, 2))
g = param[p]
for i in range(0, reps):
m = compile_model_cnn(g, (n_snps, 1))
m.fit(x_tr, y_tr, epochs=1200, verbose=verbose, validation_data=(x_val, y_val),callbacks=[early_stopper])
res[i, :] = (r(m.predict(x_val).ravel(), y_val)[0], r(m.predict(x_tst).ravel(), y_tst)[0])
R[t+"_tr"] = res[:, 0]
R[t+"_tst"] = res[:, 1]
print(pd.DataFrame(R).to_csv(float_format='%.3f', index=False))
logging.info(pd.DataFrame(R).to_csv(float_format='%.3f', index=False))
def MLP(traits=['height', 'BMI', 'WHR', 'BHMD', 'SBP'], verbose=0, unif=False, nbsnps=10000, p=None, reps=1, hot=False):
#mlp1
geneparam = list({'optimizer': 'rmsprop', 'activation': 'elu', 'nb_neurons': 32,
'weight_decay': 0.01, 'nb_layers': 1, 'dropout': 0.02})
# mlp2
geneparam.append({'optimizer': 'adagrad', 'activation': 'elu', 'nb_neurons': 64, 'weight_decay': 0.01,
'nb_layers': 2, 'dropout': 0.03})
# mlp3
geneparam.append({'optimizer': 'adam', 'activation': 'softplus', 'nb_neurons': 32,
'weight_decay': 0.01, 'nb_layers': 5, 'dropout': 0.02})
R = {}
for t in traits:
print(t)
best = 0
x_tr, x_tst, y_tr, y_tst = retrieve_data(t, nbsnps, unif=unif)
x_tr, x_val, y_tr, y_val = train_test_split(x_tr, y_tr, test_size=0.33)
if hot:
x_tr = convert_to_individual_alleles(x_tr)
x_val = convert_to_individual_alleles(x_val)
x_tst = convert_to_individual_alleles(x_tst)
n_snps = x_tr.shape[1]
f = os.path.join(os.path.expanduser("~"), 'Code/genomic_cnn/models',
"Model_" + t + "_mlp_" + str(n_snps / 1000) \
+ "kHot" + ("_unif" if unif else "_best") + ".h5")
else:
n_snps = x_tr.shape[1]
f = os.path.join(os.path.expanduser("~"), 'Code/genomic_cnn/models', "Model_" + t + "_mlp_"
+ str(n_snps / 1000) + "k" + ("_unif" if unif else "_best") + ".h5")
n = 0
if p is None:
res = np.zeros((len(geneparam), 2))
for g in geneparam:
print(g)
for x in range(0, reps):
m = compile_model_mlp(g, n_snps)
m.fit(x_tr, y_tr, epochs=1200, validation_data=(x_val, y_val), callbacks=[early_stopper], verbose=verbose)
if r(m.predict(x_val).ravel(), y_val)[0] > res[n, 0]:
print(r(m.predict(x_val).ravel(), y_val)[0])
print(x)
res[n, 0] = r(m.predict(x_val).ravel(), y_val)[0]
res[n, 1] = r(m.predict(x_tst).ravel(), y_tst)[0]
if res[n, 0] > best:
print("A better network was found with r: %.3f" % res[n,0])
print(g)
m.save(f)
best = res[n, 0]
K.clear_session()
n = n + 1
else:
res = np.zeros((reps, 2))
g = geneparam[p]
for i in range(0, reps):
m = compile_model_mlp(g, n_snps)
m.fit(x_tr, y_tr, epochs=1200, verbose=verbose, validation_data=(x_val, y_val),
callbacks=[early_stopper])
res[i, :] = (r(m.predict(x_val).ravel(), y_val)[0], r(m.predict(x_tst).ravel(), y_tst)[0])
R[t + "_tr"] = res[:, 0]
R[t + "_tst"] = res[:, 1]
print(pd.DataFrame(R).to_csv(float_format='%.3f', index=False))
logging.info(pd.DataFrame(R).to_csv(float_format='%.3f', index=False))
def lin_models(lasso=True, traits=['height', 'BMI', 'WHR', 'BHMD', 'SBP'], nbsnps=10000,verbose=0, hot=False, unif=False, reps=1):
alpha = [0.01]
R = {}
for t in traits:
print(t)
x_tr, x_tst, y_tr, y_tst = retrieve_data(t, nbsnps, unif=unif)
x_tr, x_val, y_tr, y_val = train_test_split(x_tr, y_tr, test_size=0.33)
if hot:
x_tr = convert_to_individual_alleles(x_tr)
x_val = convert_to_individual_alleles(x_val)
x_tst = convert_to_individual_alleles(x_tst)
nb_snps = x_tr.shape[1]
res = np.zeros((len(alpha), 3))
n = 0
for a in alpha:
print(a)
for i in range(0,reps):
m = Sequential()
if lasso:
m.add(Dense(1, input_dim=nb_snps,kernel_regularizer=l1(a)))
else:
m.add(Dense(1, input_dim=nb_snps, kernel_regularizer=l2(a)))
m.compile(loss='mse', optimizer='adam')
m.fit(x_tr, y_tr, epochs=1000, callbacks=[EarlyStopping()], validation_data=(x_val, y_val), verbose=verbose)
if r(m.predict(x_val).ravel(), y_val)[0] > res[n, 0]:
print(r(m.predict(x_val).ravel(), y_val)[0])
print(i)
res[n, 0] = r(m.predict(x_val).ravel(), y_val)[0]
res[n, 1] = r(m.predict(x_tst).ravel(), y_tst)[0]
K.clear_session()
print(res[n, 1])
n += 1
R[t+"val"] = res[:, 0]
R[t+"tst"] = res[:, 1]
R["alpha"] = alpha
print(pd.DataFrame(R).to_csv(float_format='%.3f', index=False))
logging.info(pd.DataFrame(R).to_csv(float_format='%.3f', index=False))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--trait", help="Trait to run optimization", default="height")
parser.add_argument("-k", "--num_snps", help="Number of SNPs", default=10000, type=int)
parser.add_argument("-v", "--verbose", help="Verbose", default=0, type=int)
parser.add_argument("--unif", help="Use uniformly spaced spns", action='store_true')
parser.add_argument("-f", "--file", help="filename", default=None)
parser.add_argument("--hot", help="Use 1 hot encoding", action="store_true")
parser.add_argument("-r", "--rep", help="Repetitions", default=1, type=int)
parser.add_argument("-s","--specific", help="Train specific mlp/cnn model (int)", default=None, type=int)
group = parser.add_mutually_exclusive_group()
group.add_argument('--lasso', action='store_true')
group.add_argument('--ridge', action='store_true')
group.add_argument('--mlp', action='store_true')
group.add_argument('--cnn', action='store_true')
args = parser.parse_args()
print(args)
if args.lasso:
method = "_lasso_"
if args.ridge:
method = "_ridge_"
if args.mlp:
method = "_mlp_"
if args.cnn:
method = "_cnn_"
if args.hot:
method = method + "1hot_"
if args.file is None:
filename = "Opt_" + args.trait + method + str(args.num_snps / 1000)
if args.unif:
filename = filename + "unifk.txt"
else:
filename = filename + "k.txt"
else:
filename = args.file
print("logging to " + filename)
# Setup logging.
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
level=logging.INFO,
filename=filename
)
logging.info("***Evaluating %s for %s trait with %d snps***" % (method, args.trait, args.num_snps))
logging.info(args)
if args.lasso:
lin_models(lasso=True, traits=[args.trait], nbsnps=args.num_snps,verbose=args.verbose,
hot=args.hot, unif=args.unif, reps=args.rep)
if args.ridge:
lin_models(lasso=False, traits=[args.trait], nbsnps=args.num_snps,verbose=args.verbose,
hot=args.hot, unif=args.unif, reps=args.rep)
if args.mlp:
MLP(traits=[args.trait], nbsnps=args.num_snps, verbose=args.verbose, hot=args.hot, unif=args.unif,
reps=args.rep, p=args.specific)
if args.cnn:
CNN(traits=[args.trait], nbsnps=args.num_snps,verbose=args.verbose,unif=args.unif,
reps=args.rep, p=args.specific)