[21363a]: / lvl1 / genPreds.py

Download this file

290 lines (237 with data), 9.1 kB

  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
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 8 21:56:55 2015.
@author: alex, rc
This script contain code to generate lvl1 model prediction.
usage : python genPreds.py model_name mode
with mode = val for validation and val = test for test.
This script will read the model description from the yaml file, load
dependencies, create preprocessing and classification pipeline and apply them
on raw data independently on each subjects.
This script support caching of preprocessed data, in order to allow reuse of
preprocessing pipeline across model.
"""
import os
import sys
if __name__ == '__main__' and __package__ is None:
filePath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(filePath)
import numpy as np
import pandas as pd
from time import time
from copy import deepcopy
import yaml
from sklearn.pipeline import make_pipeline, Pipeline
from progressbar import Bar, ETA, Percentage, ProgressBar, RotatingMarker
from sklearn.metrics import roc_auc_score
from preprocessing.aux import getEventNames, load_raw_data
from multiprocessing import Pool
cols = getEventNames()
def _from_yaml_to_func(method, params):
"""go from yaml to method.
Need to be here for accesing local variables.
"""
prm = dict()
if params is not None:
for key, val in params.iteritems():
prm[key] = eval(str(val))
return eval(method)(**prm)
def doCols(col):
"""Train and Predict for one event."""
p = []
for clf in clfs:
clf.fit(trainPreprocessed, labels_train[:, col])
p.append(clf.predict_proba(testPreprocessed)[:, 1])
return p
yml = yaml.load(open(sys.argv[1]))
# Import package
for pkg, functions in yml['imports'].iteritems():
stri = 'from ' + pkg + ' import ' + ','.join(functions)
exec(stri)
# meta settings
fileName = yml['Meta']['file']
cores = yml['Meta']['cores']
subsample = yml['Meta']['subsample']
cache_preprocessed = yml['Meta']['cachePreprocessed']
if 'subsample_test' in yml['Meta'].keys():
subsample_test = yml['Meta']['subsample_test']
else:
subsample_test = 1
if 'addPreprocessed' in yml['Meta']:
addPreprocessed = yml['Meta']['addPreprocessed']
else:
addPreprocessed = []
# preprocessing pipeline
pipe = []
for item in yml['Preprocessing']:
for method, params in item.iteritems():
pipe.append(_from_yaml_to_func(method, params))
preprocess_base = make_pipeline(*pipe)
# post preprocessing
postpreprocess_base = None
if 'PostPreprocessing' in yml.keys():
pipe = []
for item in yml['PostPreprocessing']:
for method, params in item.iteritems():
pipe.append(_from_yaml_to_func(method, params))
postpreprocess_base = make_pipeline(*pipe)
# models
clfs = []
for mdl in yml['Models']:
clfs.append('Pipeline([ %s ])' % mdl)
for i, clf in enumerate(clfs):
clfs[i] = eval(clf)
# ## read arguments ###
mode = sys.argv[2]
if mode == 'val':
test = False
elif mode == 'test':
test = True
else:
raise('Invalid mode. Please specify either val or test')
if test:
folder = 'test/'
prefix = 'test_'
else:
folder = 'val/'
prefix = 'val_'
print 'Running %s, to be saved in file %s' % (mode, fileName)
saveFolder = folder + fileName
if not os.path.exists(saveFolder):
os.makedirs(saveFolder)
# #### define lists #####
subjects = range(1, 13)
widgets = ['Cross Val : ', Percentage(), ' ', Bar(marker=RotatingMarker()),
' ', ETA(), ' ']
pbar = ProgressBar(widgets=widgets, maxval=len(subjects))
pbar.start()
report = pd.DataFrame(index=[fileName])
start_time = time()
# #### generate predictions #####
for subject in subjects:
print 'Loading data for subject %d...' % subject
# ############### READ DATA ###############################################
data_train, labels_train, data_test, labels_test = load_raw_data(subject,
test)
trainPreprocessed = None
testPreprocessed = None
cacheFile = '%s/train_sub%d.npy' % (saveFolder, subject)
# copy processing pipeline to start fresh
preprocess = deepcopy(preprocess_base)
if postpreprocess_base is not None:
postpreprocess = deepcopy(postpreprocess_base)
else:
postpreprocess = None
# ### preprocessing ####
print 'Preprocessing Training data...'
if cache_preprocessed and os.path.isfile(cacheFile):
# if cache activated + file exist, load file
trainPreprocessed = np.load(cacheFile)
else:
# if not, do preprocessing
trainPreprocessed = preprocess.fit_transform(data_train, labels_train)
# if cache activated but no file, save
if cache_preprocessed:
np.save(cacheFile, trainPreprocessed)
trainPreprocessed = None
data_train = None
print 'Preprocessing Test data...'
cacheFile = '%s/test_sub%d.npy' % (saveFolder, subject)
# update subsampling for test Preprocessing
for name, step in preprocess.steps:
if hasattr(step, 'update_subsample'):
step.update_subsample(subsample, subsample_test)
if cache_preprocessed and os.path.isfile(cacheFile):
# if cache activated + file exist, load file
testPreprocessed = np.load(cacheFile)
else:
# if not, do preprocessing
testPreprocessed = preprocess.transform(data_test)
# if cache activated but no file, save
if cache_preprocessed:
np.save(cacheFile, testPreprocessed)
data_test = None
print 'Post Preprocessing data...'
if cache_preprocessed and (trainPreprocessed is None):
# if cache activated load file
cacheFile = '%s/train_sub%d.npy' % (saveFolder, subject)
trainPreprocessed = np.load(cacheFile)
# Add preprocessed feature if they have been set in the config file
for feat_name in addPreprocessed:
featFile = '%s/%s/train_sub%d.npy' % (folder, feat_name, subject)
if os.path.isfile(featFile):
feat = np.load(featFile)
if trainPreprocessed is None:
trainPreprocessed = feat
else:
trainPreprocessed = np.c_[trainPreprocessed, feat]
feat = None
else:
raise ValueError("File %s does not exist" % featFile)
# Add preprocessed feature if they have been set in the config file
for feat_name in addPreprocessed:
featFile = '%s/%s/test_sub%d.npy' % (folder, feat_name, subject)
if os.path.isfile(featFile):
feat = np.load(featFile)
if testPreprocessed is None:
testPreprocessed = feat
else:
testPreprocessed = np.c_[testPreprocessed, feat]
feat = None
else:
raise ValueError('File %s does not exist' % featFile)
trainPreprocessed[np.isnan(trainPreprocessed)] = 0
testPreprocessed[np.isnan(testPreprocessed)] = 0
if postpreprocess is not None:
trainPreprocessed = postpreprocess.fit_transform(trainPreprocessed,
labels_train)
for name, step in postpreprocess.steps:
if hasattr(step, 'update_subsample'):
step.update_subsample(subsample, subsample_test)
testPreprocessed = postpreprocess.transform(testPreprocessed)
print 'Training models...'
labels_train = labels_train[::subsample]
if cores == 1:
preds = [doCols(i) for i in range(len(cols))]
else:
pool = Pool(processes=cores)
preds = pool.map(doCols, range(len(cols)))
pool.close()
# ### results #####
print 'Aggregating results...'
for i in range(len(clfs)):
pred_i = [j[i] for j in preds]
pred_i = np.array(np.vstack(pred_i)).transpose()
np.save('%s/sub%d_clf%d.npy' % (saveFolder, subject, i), pred_i)
if not test:
auc = np.mean([roc_auc_score(trueVals, p) for trueVals, p in
zip(labels_test[::subsample_test].T, pred_i.T)])
print '%d, clf %d: %.5f' % (subject, i, auc)
# clear memory
preds = None
trainPreprocessed = None
testPreprocessed = None
# update progress Bar
pbar.update(subject)
if not test:
labels = np.load('../infos_val.npy')[:, :-2]
# ## AGGREGATE HERE
preds_tot = []
for i in range(len(clfs)):
preds_tot.append([])
for subject in subjects:
preds_tot[i].append(np.load('%s/sub%d_clf%d.npy' % (saveFolder,
subject, i)))
preds_tot[i] = np.concatenate(preds_tot[i])
if not test:
auc = [roc_auc_score(trueVals, p) for trueVals, p in
zip(labels[::subsample_test].T, preds_tot[i].T)]
report['AUC'] = np.mean(auc)
print np.mean(auc)
# ## save the model ###
np.save(folder + prefix + fileName + '.npy', preds_tot)
# ## save report
end_time = time()
report['Time'] = end_time - start_time
report.to_csv("report/%s_%s.csv" % (prefix, fileName))
print report