-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathanalysis_FeaturePrediction.py
306 lines (224 loc) · 10.3 KB
/
analysis_FeaturePrediction.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
'''Generic Object Decoding: Feature prediction
Analysis summary
----------------
- Learning method: Sparse linear regression
- Preprocessing: Normalization and voxel selection
- Data: GenericDecoding_demo
- Results format: Pandas dataframe
'''
from __future__ import print_function
import os
import sys
import pickle
from itertools import product
from time import time
import numpy as np
import pandas as pd
from scipy import stats
from slir import SparseLinearRegression
from sklearn.linear_model import LinearRegression # For quick demo
import bdpy
from bdpy.bdata import concat_dataset
from bdpy.ml import add_bias
from bdpy.preproc import select_top
from bdpy.stats import corrcoef
from bdpy.util import makedir_ifnot, get_refdata
from bdpy.dataform import append_dataframe
from bdpy.distcomp import DistComp
import god_config as config
# Main #################################################################
def main():
# Settings ---------------------------------------------------------
# Data settings
subjects = config.subjects
rois = config.rois
num_voxel = config.num_voxel
image_feature = config.image_feature_file
features = config.features
n_iter = 200
results_dir = config.results_dir
# Misc settings
analysis_basename = os.path.basename(__file__)
# Load data --------------------------------------------------------
print('----------------------------------------')
print('Loading data')
data_all = {}
for sbj in subjects:
if len(subjects[sbj]) == 1:
data_all[sbj] = bdpy.BData(subjects[sbj][0])
else:
# Concatenate data
suc_cols = ['Run', 'Block']
data_all[sbj] = concat_dataset([bdpy.BData(f) for f in subjects[sbj]],
successive=suc_cols)
data_feature = bdpy.BData(image_feature)
# Add any additional processing to data here
# Initialize directories -------------------------------------------
makedir_ifnot(results_dir)
makedir_ifnot('tmp')
# Analysis loop ----------------------------------------------------
print('----------------------------------------')
print('Analysis loop')
for sbj, roi, feat in product(subjects, rois, features):
print('--------------------')
print('Subject: %s' % sbj)
print('ROI: %s' % roi)
print('Num voxels: %d' % num_voxel[roi])
print('Feature: %s' % feat)
# Distributed computation
analysis_id = analysis_basename + '-' + sbj + '-' + roi + '-' + feat
results_file = os.path.join(results_dir, analysis_id + '.pkl')
if os.path.exists(results_file):
print('%s is already done. Skipped.' % analysis_id)
continue
dist = DistComp(lockdir='tmp', comp_id=analysis_id)
if dist.islocked():
print('%s is already running. Skipped.' % analysis_id)
continue
dist.lock()
# Prepare data
print('Preparing data')
dat = data_all[sbj]
x = dat.select(rois[roi]) # Brain data
datatype = dat.select('DataType') # Data type
labels = dat.select('stimulus_id') # Image labels in brain data
y = data_feature.select(feat) # Image features
y_label = data_feature.select('ImageID') # Image labels
# For quick demo, reduce the number of units from 1000 to 100
y = y[:, :100]
y_sorted = get_refdata(y, y_label, labels) # Image features corresponding to brain data
# Get training and test dataset
i_train = (datatype == 1).flatten() # Index for training
i_test_pt = (datatype == 2).flatten() # Index for perception test
i_test_im = (datatype == 3).flatten() # Index for imagery test
i_test = i_test_pt + i_test_im
x_train = x[i_train, :]
x_test = x[i_test, :]
y_train = y_sorted[i_train, :]
y_test = y_sorted[i_test, :]
# Feature prediction
pred_y, true_y = feature_prediction(x_train, y_train,
x_test, y_test,
n_voxel=num_voxel[roi],
n_iter=n_iter)
# Separate results for perception and imagery tests
i_pt = i_test_pt[i_test] # Index for perception test within test
i_im = i_test_im[i_test] # Index for imagery test within test
pred_y_pt = pred_y[i_pt, :]
pred_y_im = pred_y[i_im, :]
true_y_pt = true_y[i_pt, :]
true_y_im = true_y[i_im, :]
# Get averaged predicted feature
test_label_pt = labels[i_test_pt, :].flatten()
test_label_im = labels[i_test_im, :].flatten()
pred_y_pt_av, true_y_pt_av, test_label_set_pt \
= get_averaged_feature(pred_y_pt, true_y_pt, test_label_pt)
pred_y_im_av, true_y_im_av, test_label_set_im \
= get_averaged_feature(pred_y_im, true_y_im, test_label_im)
# Get category averaged features
catlabels_pt = np.vstack([int(n) for n in test_label_pt]) # Category labels (perception test)
catlabels_im = np.vstack([int(n) for n in test_label_im]) # Category labels (imagery test)
catlabels_set_pt = np.unique(catlabels_pt) # Category label set (perception test)
catlabels_set_im = np.unique(catlabels_im) # Category label set (imagery test)
y_catlabels = data_feature.select('CatID') # Category labels in image features
ind_catave = (data_feature.select('FeatureType') == 3).flatten()
y_catave_pt = get_refdata(y[ind_catave, :], y_catlabels[ind_catave, :], catlabels_set_pt)
y_catave_im = get_refdata(y[ind_catave, :], y_catlabels[ind_catave, :], catlabels_set_im)
# Prepare result dataframe
results = pd.DataFrame({'subject' : [sbj, sbj],
'roi' : [roi, roi],
'feature' : [feat, feat],
'test_type' : ['perception', 'imagery'],
'true_feature': [true_y_pt, true_y_im],
'predicted_feature': [pred_y_pt, pred_y_im],
'test_label' : [test_label_pt, test_label_im],
'test_label_set' : [test_label_set_pt, test_label_set_im],
'true_feature_averaged' : [true_y_pt_av, true_y_im_av],
'predicted_feature_averaged' : [pred_y_pt_av, pred_y_im_av],
'category_label_set' : [catlabels_set_pt, catlabels_set_im],
'category_feature_averaged' : [y_catave_pt, y_catave_im]})
# Save results
makedir_ifnot(os.path.dirname(results_file))
with open(results_file, 'wb') as f:
pickle.dump(results, f)
print('Saved %s' % results_file)
dist.unlock()
# Functions ############################################################
def feature_prediction(x_train, y_train, x_test, y_test, n_voxel=500, n_iter=200):
'''Run feature prediction
Parameters
----------
x_train, y_train : array_like [shape = (n_sample, n_voxel)]
Brain data and image features for training
x_test, y_test : array_like [shape = (n_sample, n_unit)]
Brain data and image features for test
n_voxel : int
The number of voxels
n_iter : int
The number of iterations
Returns
-------
predicted_label : array_like [shape = (n_sample, n_unit)]
Predicted features
ture_label : array_like [shape = (n_sample, n_unit)]
True features in test data
'''
n_unit = y_train.shape[1]
# Normalize brian data (x)
norm_mean_x = np.mean(x_train, axis=0)
norm_scale_x = np.std(x_train, axis=0, ddof=1)
x_train = (x_train - norm_mean_x) / norm_scale_x
x_test = (x_test - norm_mean_x) / norm_scale_x
# Feature prediction for each unit
print('Running feature prediction')
y_true_list = []
y_pred_list = []
for i in range(n_unit):
print('Unit %03d' % (i + 1))
start_time = time()
# Get unit features
y_train_unit = y_train[:, i]
y_test_unit = y_test[:, i]
# Normalize image features for training (y_train_unit)
norm_mean_y = np.mean(y_train_unit, axis=0)
std_y = np.std(y_train_unit, axis=0, ddof=1)
norm_scale_y = 1 if std_y == 0 else std_y
y_train_unit = (y_train_unit - norm_mean_y) / norm_scale_y
# Voxel selection
corr = corrcoef(y_train_unit, x_train, var='col')
x_train_unit, voxel_index = select_top(x_train, np.abs(corr), n_voxel, axis=1, verbose=False)
x_test_unit = x_test[:, voxel_index]
# Add bias terms
x_train_unit = add_bias(x_train_unit, axis=1)
x_test_unit = add_bias(x_test_unit, axis=1)
# Setup regression
# For quick demo, use linaer regression
model = LinearRegression()
#model = SparseLinearRegression(n_iter=n_iter, prune_mode=1)
# Training and test
try:
model.fit(x_train_unit, y_train_unit) # Training
y_pred = model.predict(x_test_unit) # Test
except:
# When SLiR failed, returns zero-filled array as predicted features
y_pred = np.zeros(y_test_unit.shape)
# Denormalize predicted features
y_pred = y_pred * norm_scale_y + norm_mean_y
y_true_list.append(y_test_unit)
y_pred_list.append(y_pred)
print('Time: %.3f sec' % (time() - start_time))
# Create numpy arrays for return values
y_predicted = np.vstack(y_pred_list).T
y_true = np.vstack(y_true_list).T
return y_predicted, y_true
def get_averaged_feature(pred_y, true_y, labels):
'''Return category-averaged features'''
labels_set = np.unique(labels)
pred_y_av = np.array([np.mean(pred_y[labels == c, :], axis=0) for c in labels_set])
true_y_av = np.array([np.mean(true_y[labels == c, :], axis=0) for c in labels_set])
return pred_y_av, true_y_av, labels_set
# Run as a scirpt ######################################################
if __name__ == '__main__':
# To avoid any use of global variables,
# do nothing except calling main() here
main()