-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdo_featsets.py
524 lines (398 loc) · 18.4 KB
/
do_featsets.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#!/usr/bin/python
import os
import re
import sys
import argparse
import numpy as np
import nibabel as nib
import pickle
import scipy.stats as stats
import scipy.io as sio
#feature selection
from sklearn.feature_selection import f_classif
from sklearn.svm import SVC
from sklearn.feature_selection import RFE
from sklearn.feature_selection import SelectPercentile
from sklearn.feature_selection import SelectFdr
from sklearn.feature_selection import SelectFpr
from sklearn.feature_selection import RFECV
from sklearn.cross_validation import KFold
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import zero_one
#data preprocessing
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelEncoder
#other decompositions
from sklearn.decomposition import PCA
from sklearn.decomposition import RandomizedPCA
from sklearn.lda import LDA
from sklearn.grid_search import GridSearchCV
from sklearn.cross_validation import LeaveOneOut, StratifiedKFold
from sklearn.feature_selection import RFECV
from sklearn.pipeline import Pipeline, FeatureUnion
from IPython.core.debugger import Tracer; debug_here = Tracer()
sys.path.append('/home/alexandre/Dropbox/Documents/phd/work/aizkolari')
import aizkolari_utils as au
'''
import os
import sys
sys.path.append('/home/alexandre/Dropbox/Documents/phd/work/aizkolari')
import aizkolari_utils as au
sys.path.append('/home/alexandre/Dropbox/Documents/phd/work/oasis_aal')
from do_aal_featsets import *
hn = au.get_hostname()
if hn == 'azteca':
wd = '/media/data/oasis_aal'
roisdir = ''
outd = '/media/data/oasis_aal'
elif hn == 'corsair':
wd = '/media/alexandre/alextenso/work/oasis_svm'
roisdir = '/scratch/oasis_aal/aal_rois'
outd = '/scratch/oasis_aal'
elif hn == 'hpmed':
wd = '/media/alexandre/toshiba/work/oasis_svm'
roisdir = ''
outd = '/media/alexandre/toshiba/work/oasis_svm'
verbose = 2
au.setup_logger(verbose, logfname=None)
otype = '.npy'
smoothmm = 2
roilabsf = '/home/alexandre/Dropbox/Documents/phd/work/oasis_aal/aal_allvalues.txt'
#fsmethods = ['none', 'stats']
fsmethods = ['stats']
featstypes = ['jacs','smoothmodgm','modulatedgm','geodan','norms','trace']
for feats in featstypes:
datadir = os.path.join (wd, feats)
subjlstf = os.path.join (wd, feats + '_lst')
for fsmethod in fsmethods:
outfsdir = 'oasis_' + feats
if smoothmm > 0:
outfsdir += '_' + str(smoothmm) + 'mm'
outfsdir += '_' + fsmethod
outdir = os.path.join(outd, outfsdir)
print ('Creating ' + outdir)
main_do (datadir, subjlstf, smoothmm, feats, outdir, roisdir, roilabsf, fsmethod, otype)
'''
#-------------------------------------------------------------------------------
def set_parser():
fsmethods = ['none', 'stats', 'hist3d'] # ONLY UNSUPERVISED FEATURE EXTRACTION METHODS HERE, NOT 'rfe', 'rfecv', 'univariate', 'fdr', 'fpr', 'extratrees', 'pca', 'rpca', 'lda'] #svmweights
feats = ['jacs','smoothmodgm','modulatedgm', 'geodan', 'norms', 'trace']
outs = ['.npy','.mat']
parser = argparse.ArgumentParser(description='OASIS AAL classification experiment.')
parser.add_argument('-s', '--subjlstf', dest='subjlstf', default='', required=True, help='list file with the subjects for the analysis. Each line: <class_label>,<subject_file>')
parser.add_argument('-d', '--datadir', dest='datadir', default='', required=True, help='data directory path')
parser.add_argument('-r', '--roisdir', dest='roisdir', default='', required=True, help='rois directory path')
parser.add_argument('-o', '--outdir', dest='outdir', default='', required=False, help='output data directory path')
parser.add_argument('-l', '--roilabsf', dest='roilabsf', default='', required=True, help='path of file with ROI labels-values table, where each line has: ROIlabel ROIvalue')
parser.add_argument('-f', '--feats', dest='feats', default='jacs', choices=feats, required=False, help='deformation measure type')
parser.add_argument( '--otype', dest='otype', default='.npy', choices=outs, required=False, help='output file type.')
parser.add_argument('--bins', dest='bins', default=10, required=False, type=int, help='Number of bins in each dimension of the volume to calculate the histogram features.')
parser.add_argument('--smoothmm', dest='smoothmm', default=0, required=False, type=int, help='Size of a Gaussian smooth filter for each image before selecting features.')
parser.add_argument('--fsmethod', dest='fsmethod', default='rfe', choices=fsmethods, required=False, help='Feature selection method')
parser.add_argument('-v', '--verbosity', dest='verbosity', required=False, type=int, default=2, help='Verbosity level: Integer where 0 for Errors, 1 for Input/Output, 2 for Progression reports')
return parser
#-------------------------------------------------------------------------------
def get_aal_info(aal_data, roi_idx):
return aal_data[aal_data[:,3] == str(roi_idx)].flatten()
#-------------------------------------------------------------------------------
def list_filter (list, filter):
return [ (l) for l in list if filter(l) ]
#-------------------------------------------------------------------------------
def dir_search (regex, wd='.'):
ls = os.listdir(wd)
filt = re.compile(regex).search
return list_filter(ls, filt)
#-------------------------------------------------------------------------------
def dir_match (regex, wd='.'):
ls = os.listdir(wd)
filt = re.compile(regex).match
return list_filter(ls, filt)
#-------------------------------------------------------------------------------
def list_match (regex, list):
filt = re.compile(regex).match
return list_filter(list, filt)
#-------------------------------------------------------------------------------
def list_search (regex, list):
filt = re.compile(regex).search
return list_filter(list, filt)
#-------------------------------------------------------------------------------
def shelve_vars (ofname, varlist):
mashelf = shelve.open(ofname, 'n')
for key in varlist:
try:
mashelf[key] = globals()[key]
except:
log.error('ERROR shelving: {0}'.format(key))
mashelf.close()
#-------------------------------------------------------------------------------
def append_to_keys (mydict, preffix):
return {preffix + str(key) : (transform(value) if isinstance(value, dict) else value) for key, value in mydict.items()}
#-------------------------------------------------------------------------------
def append_to_list (mylist, preffix):
return list({preffix + str(item) for item in mylist})
#-------------------------------------------------------------------------------
def join_path_to_filelist (path, mylist):
return list({os.path.join(path, str(item)) for item in mylist})
#-------------------------------------------------------------------------------
def save_feats_file (feats, otype, outfname):
if otype == '.npy': np.save(outfname + '.npy', feats)
elif otype == '.mat': sio.savemat(outfname + '.mat', dict(feats = feats))
#-------------------------------------------------------------------------------
def get_fsmethod (fsmethod, n_feats, n_subjs):
n_jobs = 2
#Feature selection procedures
from sklearn.cross_validation import StratifiedKFold
#http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFE.html
fsmethods = { 'rfe' : RFE(estimator=SVC(kernel="linear", C=1), step=0.05, n_features_to_select=2),
#http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFE.html
'rfecv' : RFECV(estimator=SVC(kernel="linear"), step=0.05, loss_func=zero_one), #cv=3, default; cv=StratifiedKFold(n_subjs, 3)
#Univariate Feature selection: http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectPercentile.html
'univariate': SelectPercentile(f_classif, percentile=5),
#http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectFpr.html
'fpr' : SelectFpr (f_classif, alpha=0.05),
#http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectFdr.html
'fdr' : SelectFdr (f_classif, alpha=0.05),
#http://scikit-learn.org/stable/modules/feature_selection.html
'extratrees': ExtraTreesClassifier(n_estimators=50, max_features='auto', compute_importances=True, n_jobs=n_jobs, random_state=0),
'pca' : PCA(n_components='mle'),
'rpca' : RandomizedPCA(random_state=0),
'lda' : LDA(),
}
#feature selection parameter values for grid search
max_feats = ['auto']
if n_feats < 10:
feats_to_sel = range(2, n_feats, 2)
n_comps = range(1, n_feats, 2)
else:
feats_to_sel = range(2, 20, 4)
n_comps = range(1, 30, 4)
max_feats.extend(feats_to_sel)
fsgrid = { 'rfe' : dict(estimator__C = [0.1, 1, 10], n_features_to_select = feats_to_sel),
'rfecv' : dict(estimator__C = [0.1, 1, 10]),
'univariate': dict(percentile = [1, 3, 5, 10]),
'fpr' : dict(alpha = [1, 3, 5, 10]),
'fdr' : dict(alpha = [1, 3, 5, 10]),
'extratrees': dict(n_estimators = [1, 3, 5, 10, 30, 50], max_features = max_feats),
'pca' : dict(n_components = n_comps.extend(['mle']), whiten = [True, False]),
'rpca' : dict(n_components = n_comps, iterated_power = [3, 4, 5], whiten = [True, False]),
'lda' : dict(n_components = n_comps)
}
return fsmethods[fsmethod], fsgrid[fsmethod]
#-------------------------------------------------------------------------------
def create_rois_mask (roilst, roiflst):
au.log.info('Creating all ROIs common mask for data reading')
shape = nib.load(roiflst[0]).shape
mask = np.zeros(shape)
#create space for all features and read from subjects
for roi in roilst:
try:
roif = list_search('_' + roi + '.', roiflst)[0]
roivol = nib.load(roif).get_data()
mask += roivol
except:
debug_here()
return mask > 0
#-------------------------------------------------------------------------------
def parse_subjects_list (fname, datadir=''):
labels = []
subjs = []
if datadir:
datadir += os.path.sep
try:
f = open(fname, 'r')
for s in f:
line = s.strip().split(',')
labels.append(np.float(line[0]))
subjf = line[1].strip()
if not os.path.isabs(subjf):
subjs.append (datadir + subjf)
else:
subjs.append (subjf)
f.close()
except:
au.log.error( "Unexpected error: ", sys.exc_info()[0] )
debug_here()
sys.exit(-1)
return [labels, subjs]
#-------------------------------------------------------------------------------
def smooth_volume(imf, smoothmm):
from nipype.interfaces.fsl.maths import IsotropicSmooth
if smoothmm > 0:
omf = imf + '_smooth' + str(smoothmm) + 'mm.nii.gz'
isosmooth = IsotropicSmooth()
isosmooth.inputs.in_file = imf
isosmooth.inputs.fwhm = smoothmm
isosmooth.inputs.out_file = omf
isosmooth.run()
data = nib.load(omf).get_data()
os.remove(omf)
else:
data = nib.load(imf).get_data()
return data
#-------------------------------------------------------------------------------
def load_data (subjsf, datadir, msk, smoothmm=0):
#loading mask
nvox = np.sum (msk > 0)
indices = np.where(msk > 0)
#reading subjects list
[scores, subjs] = parse_subjects_list (subjsf, datadir)
scores = np.array(scores)
imgsiz = nib.load(subjs[0]).shape
dtype = nib.load(subjs[0]).get_data_dtype()
nsubjs = len(subjs)
#checking mask and first subject dimensions match
if imgsiz != msk.shape:
au.log.error ('Subject image and mask dimensions should coincide.')
exit(1)
#relabeling scores to integers, if needed
if not np.all(scores.astype(np.int) == scores):
# unis = np.unique(scores)
# scs = np.zeros (scores.shape, dtype=int)
# for k in np.arange(len(unis)):
# scs[scores == unis[k]] = k
# y = scs.copy()
le = LabelEncoder()
le.fit(scores)
y = le.transform(scores)
else:
y = scores.copy()
#loading data
au.log.info ('Loading data...')
X = np.zeros((nsubjs, nvox), dtype=dtype)
for f in np.arange(nsubjs):
imf = subjs[f]
au.log.info('Reading ' + imf)
if (smoothmm > 0):
img = smooth_volume(imf, smoothmm)
else:
img = nib.load(imf).get_data()
X[f,:] = img[msk > 0]
return X, y, scores, imgsiz, indices
#-------------------------------------------------------------------------------
def calculate_stats (data):
n_subjs = data.shape[0]
feats = np.zeros((n_subjs, 7))
feats[:,0] = fs.max (axis=1)
feats[:,1] = fs.min (axis=1)
feats[:,2] = fs.mean(axis=1)
feats[:,3] = fs.var (axis=1)
feats[:,4] = np.median (fs, axis=1)
feats[:,5] = stats.kurtosis (fs, axis=1)
feats[:,6] = stats.skew (fs, axis=1)
return feats
#-------------------------------------------------------------------------------
def calculate_hist3d (data, bins):
n_subjs = data.shape[0]
feats = np.zeros((n_subjs, bins*bins*bins))
for s in np.arange(n_subjs):
H, edges = np.histogramdd(data[s,], bins = (bins, bins, bins))
feats[s,:] = H.flatten()
return feats
#-------------------------------------------------------------------------------
def create_feature_sets (fsmethod, fsgrid, data, msk, y, outdir, outbasename, otype):
n_subjs = data.shape[0]
aalinfo = np.loadtxt (roilabsf, dtype=str)
np.savetxt (os.path.join(outdir, outbasename + '_labels.txt'), y, fmt="%.2f")
outfname = os.path.join(outdir, outbasename)
au.log.info('Creating ' + outfname)
fs = data[:, msk > 0]
if fsmethod == 'stats':
feats = calculate_stats (fs)
elif fsmethod == 'hist3d':
feats = calculate_hist3d (fs)
elif fsmethod == 'none':
feats = fs
#save file
save_feats_file (feats, otype, outfname)
#-------------------------------------------------------------------------------
def create_feature_sets (fsmethod, fsgrid, data, msk, y, roilst, roiflst, roilabsf, outdir, outbasename, otype):
n_subjs = data.shape[0]
aalinfo = np.loadtxt (roilabsf, dtype=str)
np.savetxt (os.path.join(outdir, outbasename + '_labels.txt'), y, fmt="%.2f")
for roi in roilst:
outfname = os.path.join(outdir, outbasename + '_' + roi)
au.log.info('Creating ' + outfname)
roif = list_search('_' + roi + '.', roiflst)[0]
aalidx = [i for i, x in enumerate(aalinfo[:,0]) if x == roi]
aalrow = aalinfo[aalidx,:]
#load roi
roivol = nib.load(roif).get_data()
roivol = roivol[msk > 0]
if fsmethod == 'stats':
feats = np.zeros((n_subjs, 7))
fs = data[:, roivol > 0]
feats[:,0] = fs.max (axis=1)
feats[:,1] = fs.min (axis=1)
feats[:,2] = fs.mean(axis=1)
feats[:,3] = fs.var (axis=1)
feats[:,4] = np.median(fs, axis=1)
feats[:,5] = stats.kurtosis (fs, axis=1)
feats[:,6] = stats.skew (fs, axis=1)
elif fsmethod == 'none':
feats = data[:, roivol > 0]
#save file
save_feats_file (feats, otype, outfname)
#-------------------------------------------------------------------------------
def main_do (datadir, subjlstf, bins, smoothmm, feats, outdir, roisdir, roilabsf, fsmethod, otype):
#create outdir if it does not exist
if not outdir:
outdir = os.path.join(datadir, 'oasis_' + feats + '_' + fsmethod + 'feats')
if not os.path.exists(outdir):
os.mkdir(outdir)
#output base file name
outbasename = 'oasis_' + feats + '_' + fsmethod
if smoothmm > 0:
outbasename += '_' + str(smoothmm) + 'mm'
#fsmethod
fsgrid = None
#if fsmethod != 'stats' and fsmethod != 'none' and fsmethod != 'hist3d':
# fsmethod, fsgrid = get_fsmethod (fsmethod, n_feats, n_subjs)
roilst = None
if roilabsf:
#get info from ROIs
aalinfo = np.loadtxt (roilabsf, dtype=str)
roilst = aalinfo[:,0]
n_rois = len(roilst)
#get a list of the aal roi volumes
roiflst = dir_search('aal.smooth*', roisdir)
roiflst = join_path_to_filelist (roisdir, roiflst)
#create roilst mask
msk = create_rois_mask (roilst, roiflst)
#load all data
data, y, scores, imgsiz, indices = load_data (subjlstf, datadir, msk, smoothmm)
#For now I'm doing only two class classification:
y = np.array(y)
y[y > 0] = 1
y = y.astype(int)
#create space for all features and read from subjects
if roilst:
create_feature_sets (fsmethod, fsgrid, data, msk, y, roilst, roiflst, aalinfo, outdir, outbasename, otype)
else:
create_feature_sets (fsmethod, fsgrid, data, msk, y, bins, outdir, outbasename, otype)
#-------------------------------------------------------------------------------
def main():
parser = set_parser()
try:
args = parser.parse_args ()
except argparse.ArgumentError, exc:
print (exc.message + '\n' + exc.argument)
parser.error(str(msg))
return -1
feats = args.feats.strip()
datadir = args.datadir.strip()
outdir = args.outdir.strip()
roisdir = args.roisdir.strip()
roilabsf = args.roilabsf.strip()
subjlstf = args.subjlstf.strip()
fsmethod = args.fsmethod.strip()
otype = args.otype.strip()
smoothmm = args.smoothmm
bins = args.bins
verbose = args.verbosity
#logging config
au.setup_logger(verbose)
return main_do (datadir, subjlstf, bins, smoothmm, feats, outdir, roisdir, roilabsf, fsmethod, otype)
#-------------------------------------------------------------------------------
if __name__ == "__main__":
sys.exit(main())