-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathone_split.py
285 lines (230 loc) · 10.6 KB
/
one_split.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
from sklearn.metrics import confusion_matrix
from sklearn.utils.extmath import density
import numpy as np
from features_processing import feature_extraction
# from features_processing.feature_scaler import FeatureScaler
from utils.evaluate import evalualte
from os.path import join, exists
from model.model_factory import get_model
from os import makedirs
import logging
import datetime
from data.data_access import Data
from preprocessing import pre
import scipy.sparse
import yaml
import pandas as pd
from matplotlib import pyplot as plt
import timeit
import time
from utils.plots import generate_plots, plot_roc, plot_confusion_matrix, plot_prc
# timeStamp = '_{0:%b}-{0:%d}_{0:%H}-{0:%M}'.format(datetime.datetime.now())
from utils.rnd import set_random_seeds
import torch
import gc
def elapsed_time(start_time, end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins, elapsed_secs
class OneSplitPipeline:
def __init__(self, data_params, pre_params, feature_params, model_params, pipeline_params, exp_name):
self.data_params = data_params
self.pre_params = pre_params
self.features_params = feature_params
self.model_params = model_params
self.exp_name = exp_name
self.pipeline_params = pipeline_params
if 'save_train' in pipeline_params['params']:
self.save_train = pipeline_params['params']['save_train']
else:
self.save_train = False
if 'eval_dataset' in pipeline_params['params']:
self.eval_dataset = pipeline_params['params']['eval_dataset']
else:
self.eval_dataset = 'validation'
self.prapre_saving_dir()
def prapre_saving_dir(self):
# self.directory = self.exp_name + timeStamp
self.directory = self.exp_name
if not exists(self.directory):
makedirs(self.directory)
def save_prediction(self,info, y_pred,y_pred_score, y_test, model_name, training= False):
if training:
file_name = join(self.directory , model_name+ '_traing.csv')
else:
file_name = join(self.directory , model_name+ '_testing.csv')
logging.info("saving : %s" % file_name)
info = info.copy()
info['pred'] = y_pred
info['pred_score'] = y_pred_score
info['y'] = y_test
info.to_csv(file_name)
def get_list(self,x, cols):
x_df = pd.DataFrame(x, columns=cols)
genes = cols.get_level_values(0).unique()
genes_list = []
input_shapes = []
for g in genes:
g_df = x_df.loc[:, g].as_matrix()
input_shapes.append(g_df.shape[1])
genes_list.append(g_df)
return genes_list
def run(self):
# logging
logging.info( 'loading data....')
test_scores = []
model_names = []
model_list = []
cnf_matrix_list = []
fig = plt.figure()
fig.set_size_inches((10, 6))
auc_fig = plt.figure()
prc_fig = plt.figure()
for data_params in self.data_params:
print ('data_params',self.data_params)
data_id = data_params['id']
data = Data(**data_params)
# get data
x_train, x_validate, x_test, y_train, y_validate, y_test, info_train, info_validate, info_test, columns = data.get_train_validate_test()
print( 'info', type(info_train), type(info_validate), info_test.shape)
logging.info( 'info {} {} {}'.format(info_train.shape, info_validate.shape, info_test.shape))
logging.info('x {} {} {}'.format( x_train.shape, x_validate.shape, x_test.shape))
logging.info('y {} {} {}'.format( y_train.shape, y_validate.shape, y_test.shape))
# pre-processing
logging.info('preprocessing....')
x_train, x_test = self.preprocess(x_train, x_test)
logging.info('feature extraction....')
x_train, x_validate, x_test = self.extract_features(x_train, x_validate, x_test)
for m in self.model_params:
# get model
set_random_seeds(random_seed=818)
model = get_model(m)
start = timeit.default_timer()
if m['type'] in[ 'nn', 'bert']:
model = model.fit(x_train, y_train, x_validate, y_validate)
else:
model = model.fit(x_train, y_train)
stop = timeit.default_timer()
mins, secs= elapsed_time(start, stop)
logging.info('predicting')
if self.eval_dataset == 'validation':
x_t = x_validate
y_t = y_validate
info_t = info_validate
else:
x_t = x_test
y_t = y_test
info_t= info_test
y_pred_test, y_pred_test_scores, test_score, cnf_matrix = self.predict(model, x_t, y_t)
cnf_matrix_list.append(cnf_matrix)
if 'id' in m:
model_name = m['id']
else:
model_name = m['type']
model_name = model_name+'_'+ data_id
test_score['time_in_minutes'] = mins +secs/60.
test_scores.append(test_score)
model_names.append(model_name)
logging.info(f'{model_name} Training Time: {mins}m {secs}s')
logging.info('saving results')
self.save_score(test_score, model_name)
self.save_prediction(info_t, y_pred_test,y_pred_test_scores, y_t, model_name)
plot_roc(auc_fig, y_t, y_pred_test_scores, self.directory, label=model_name)
plot_prc(prc_fig, y_t, y_pred_test_scores, self.directory, label=model_name)
# plt.savefig(join(self.directory, 'auprc_curves'))
if self.save_train:
y_pred_train, y_pred_train_scores, score, train_cnf_matrix= self.predict(model, x_train, y_train)
self.save_prediction(info_train, y_pred_train,y_pred_train_scores, y_train, model_name, training=True)
## clear memory
if m['id'] =='bert': ##TODO: clean this (encapsulate inside the model?)
model.model.cpu()
del model
gc.collect()
torch.cuda.empty_cache()
time.sleep(30)
classes = np.unique(y_train)
auc_fig.savefig(join(self.directory, 'auc_curves'))
prc_fig.savefig(join(self.directory, 'auprc_curves'))
test_scores = pd.DataFrame(test_scores, index=model_names)
generate_plots(test_scores, self.directory)
self.save_all_scores(test_scores)
# self.plot_coef(model_list)
self.save_cnf_matrix(cnf_matrix_list, model_names, classes)
return test_scores
def save_cnf_matrix(self, cnf_matrix_list, model_list, classes):
for cnf_matrix, model in zip(cnf_matrix_list, model_list):
plt.figure()
# plot_confusion_matrix(cnf_matrix, classes=['Primary','Metastatic'],
plot_confusion_matrix(cnf_matrix, classes=classes,
title='Confusion matrix, without normalization')
file_name = join(self.directory, 'confusion_' + model)
plt.savefig(file_name)
plt.figure()
plot_confusion_matrix(cnf_matrix, normalize=True, classes=classes,
title='Normalized confusion matrix')
file_name = join(self.directory, 'confusion_normalized_' + model)
plt.savefig(file_name)
# Plot normalized confusion matrix
# plt.figure()
# plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
# title='Normalized confusion matrix')
def plot_coef(self, model_list):
for model, model_name in model_list:
plt.figure()
file_name = join(self.directory, 'coef_'+model_name)
for coef in model.coef_:
plt.hist(coef, bins=20)
# plt.hist(model.coef_[1], bins=20)
plt.savefig(file_name)
def save_all_scores(self, scores):
file_name = join(self.directory , 'all_scores.csv')
scores.to_csv(file_name)
def save_score(self, score, model_name):
file_name = join(self.directory , model_name+ '_params.yml')
logging.info("saving yml : %s" % file_name)
with open(file_name, 'w') as yaml_file:
yaml_file.write(
yaml.dump([self.data_params, self.model_params, self.pre_params, str(score)], default_flow_style=False))
def predict(self, model, x_test, y_test ):
logging.info('predicitng ...')
y_pred_test = model.predict(x_test)
if hasattr(model, 'predict_proba'):
y_pred_test_scores = model.predict_proba(x_test)[:,1]
else:
y_pred_test_scores = y_pred_test
# y_pred_test_scores = model.predict_proba(x_test)[:,1]
logging.info('scoring ...')
score = evalualte(y_test, y_pred_test, y_pred_test_scores)
cnf_matrix = confusion_matrix(y_test, y_pred_test)
return y_pred_test, y_pred_test_scores, score, cnf_matrix
def preprocess(self, x_train, x_test):
logging.info('preprocessing....')
print(self.pre_params)
proc = pre.get_processor(self.pre_params)
if proc:
proc.fit(x_train)
x_train = proc.transform(x_train)
x_test = proc.transform(x_test)
# if scipy.sparse.issparse(x_train):
# x_train = x_train.todense()
# x_test = x_test.todense()
return x_train, x_test
def extract_features(self, x_train, x_validate, x_test):
if self.features_params== {}:
return x_train, x_test
logging.info('feature extraction ....')
print (self.features_params)
proc = feature_extraction.get_processor(self.features_params)
if proc:
proc.fit(x_train)
logging.info('x_train')
x_train = proc.transform(x_train)
logging.info('x_test')
x_test = proc.transform(x_test)
logging.info('x_validate')
x_validate = proc.transform(x_validate)
# if scipy.sparse.issparse(x_train):
# x_train = x_train.todense()
# x_test = x_test.todense()
return x_train,x_validate, x_test