-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathencoding_model.py
More file actions
306 lines (236 loc) · 13 KB
/
Copy pathencoding_model.py
File metadata and controls
306 lines (236 loc) · 13 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import json
from os.path import exists
import numpy as np
from sklearn.linear_model import Ridge
from sklearn.metrics import make_scorer
from sklearn.model_selection import GridSearchCV, KFold
from scipy.stats import rankdata, zscore
from scipy.spatial.distance import cdist
from gifti_io import read_gifti, write_gifti
from split_stories import check_keys, load_split_data, split_models
from brainiak.utils.utils import array_correlation
# Function for selecting and aggregating subjects
def aggregate_subjects(datasets, models, story_list, subject_list,
hemi='lh', aggregation='average'):
# Allow for easy single subject input
if type(story_list) == str:
story_list = [story_list]
# Loop through requested stories
data_stack, model_stack = [], []
for story in story_list:
# Broadcast model if concatenating subjects
if aggregation == 'concatenate':
model = np.tile(models[story], (len(subject_list), 1))
else:
model = models[story]
# Allow for easy single subject input
if type(subject_list) == str:
subject = subject_list
data = datasets[story][subject][hemi]
else:
if len(subject_list) == 1:
data = datasets[story][subject_list[0]][hemi]
else:
n_subjects = len(subject_list)
assert n_subjects > 1
# Check subjects are in the data
for subject in subject_list:
assert subject in datasets[story].keys()
# Compile test subjects
data_list = []
for subject in subject_list:
data_list.append(datasets[story][subject][hemi])
# Average time data across subjects
if aggregation == 'average' and n_subjects > 1:
data = np.mean(data_list, axis=0)
elif aggregation == 'concatenate' and n_subjects > 1:
data = np.vstack(data_list)
data_stack.append(data)
model_stack.append(model)
# Stack for multiple stories
data_stack = np.vstack(data_stack)
model_stack = np.vstack(model_stack)
if len(model_stack) != len(data_stack):
raise ValueError("Model and data have mismatching shape! "
f"model: {model_stack.shape}, data: {data_stack.shape}")
return data_stack, model_stack
# Function to compute correlation-based rank accuracy
def rank_accuracy(predicted_model, test_model, mean=True):
n_predictions = test_model.shape[0]
# Get correlations between pairs
correlations = 1 - cdist(predicted_model, test_model,
'correlation')
# Get rank of matching prediction for each
ranks = []
for index in np.arange(n_predictions):
ranks.append(rankdata(correlations[index])[index])
# Normalize ranks by number of choices
ranks = (np.array(ranks) - 1) / (n_predictions - 1)
if mean:
ranks = np.mean(ranks)
return ranks
# Function to run grid search over alphas across voxels
def grid_search(train_model, train_data, alphas, scorer, n_splits=10):
# Get number of voxels
n_voxels = train_data.shape[1]
# Set up ridge regression
ridge = Ridge(fit_intercept=True, normalize=False,
copy_X=True, tol=0.001)
# Set up grid search
grid = GridSearchCV(ridge, {'alpha': alphas}, iid=False,
scoring=scorer,
cv=KFold(n_splits=n_splits), refit=False,
return_train_score=False)
# Loop through voxels
best_alphas, best_scores, all_scores = [], [], []
for voxel in np.arange(n_voxels):
# Perform grid search over alphas for voxel
grid.fit(train_model, train_data[:, voxel]);
best_alphas.append(grid.best_params_['alpha'])
best_scores.append(grid.best_score_)
# Get all scores across folds
split_scores = []
for split in np.arange(n_splits):
split_score = grid.cv_results_[f'split{split}_test_score']
split_scores.append(split_score)
all_scores.append(np.mean(split_scores, axis=0))
best_alphas = np.array(best_alphas)
best_scores = np.array(best_scores)
all_scores = np.column_stack(all_scores)
assert (best_alphas.shape[0] == best_scores.shape[0]
== all_scores.shape[1] == n_voxels)
return best_alphas, best_scores, all_scores
# Name guard for actually running encoding model analysis
if __name__ == '__main__':
# Load dictionary of input filenames and parameters
with open('data/metadata.json') as f:
metadata = json.load(f)
# Create story and subject lists
stories = ['black', 'forgot']
# Set ROIs, spaces, and hemispheres
rois = ['EAC', 'AAC', 'TPOJ', 'PMC']
prefixes = [('no SRM', 'noSRM', 'noSRM'),
('no SRM (average)', 'noSRM', 'noSRM'),
('no SRM (within-subject)', 'noSRM', 'noSRM'),
('cPCA (k = 100)', 'parcel-mean_k-100_cPCA-train', 'parcel-mean_k-100_cPCA-test'),
('cPCA (k = 50)', 'parcel-mean_k-50_cPCA-train', 'parcel-mean_k-50_cPCA-test'),
('cPCA (k = 10)', 'parcel-mean_k-10_cPCA-train', 'parcel-mean_k-10_cPCA-test'),
('cSRM (k = 100)', 'parcel-mean_k-100_cSRM-train', 'parcel-mean_k-100_cSRM-test'),
('cSRM (k = 50)', 'parcel-mean_k-50_cSRM-train', 'parcel-mean_k-50_cSRM-test'),
('cSRM (k = 10)', 'parcel-mean_k-10_cSRM-train', 'parcel-mean_k-10_cSRM-test')]
stories = ['black', 'forgot']
hemis = ['lh', 'rh']
# Set some parameters for encoding model
delays = [2, 3, 4, 5]
aggregation = 'average'
story_train = 'all'
alpha = 100
# Make custom correlation scorer
correlation_scorer = make_scorer(array_correlation)
# Populate results file if it already exists
results_fn = f'data/encoding_{story_train}-story_avg_inv_results.npy'
if exists(results_fn):
results = np.load(results_fn, allow_pickle=True).item()
else:
results = {}
# Loop through keys without replacing existing ones
for story in stories:
if story not in results:
results[story] = {}
if story_train == 'within':
train_stories, test_stories = story, story
elif story_train == 'across':
test_stories = story
train_stories = [st for st in stories if st is not test_story]
elif story_train == 'all':
test_stories = story
train_stories = stories
# By default just grab all subjects
subject_list = check_keys(metadata[story]['data'])
# Split models and load in data splits
train_model_dict = split_models(metadata, stories=stories,
subjects=None, half=1,
delays=delays)
test_model_dict = split_models(metadata, stories=stories,
subjects=None, half=2,
delays=delays)
for roi in rois:
if roi not in results[story]:
results[story][roi] = {}
for prefix in prefixes:
if prefix[0] not in results[story][roi]:
results[story][roi][prefix[0]] = {}
# Load in split cSRM data for train and test
train_dict = load_split_data(metadata, stories=stories,
subjects=None, hemisphere=hemis,
half=1, prefix=f'{roi}_' + prefix[1])
test_dict = load_split_data(metadata, stories=stories,
subjects=None, hemisphere=hemis,
half=2, prefix=f'{roi}_' + prefix[2])
for s in range(len(subject_list)):
test_subjects = [subject_list[s]]
test_subject = test_subjects[0]
# Use leave-one-subject-out cross-validation (unless within-subject)
if prefix[0] == 'no SRM (within-subject)':
train_subjects = test_subjects
else:
train_subjects = [sub for sub in subject_list
if sub is not test_subjects[0]]
if test_subject not in results[story][roi][prefix[0]]:
results[story][roi][prefix[0]][test_subject] = {}
for hemi in hemis:
if hemi not in results[story][roi][prefix[0]][test_subject]:
results[story][roi][prefix[0]][test_subject][hemi] = {}
# Aggregate data and model across subjects
train_data, train_model = aggregate_subjects(train_dict,
train_model_dict,
train_stories,
train_subjects,
hemi=hemi,
aggregation=aggregation)
test_data, test_model = aggregate_subjects(test_dict,
test_model_dict,
test_stories,
test_subjects,
hemi=hemi,
aggregation=aggregation)
# Get the regional average as well
if prefix[0] == 'no SRM (average)':
train_data = np.expand_dims(np.mean(train_data,
axis=1), 1)
test_data = np.expand_dims(np.mean(test_data,
axis=1), 1)
# Declare ridge regression model
ridge = Ridge(alpha=alpha, fit_intercept=True, normalize=False,
copy_X=True, tol=0.001, solver='auto')
# Fit training data
ridge.fit(train_model, train_data)
# Get coefficients of trained model
coefficients = ridge.coef_
# Use trained model to predict response for test data
predicted_data = ridge.predict(test_model)
# Compute correlation between predicted and test response
performance = array_correlation(predicted_data,
test_data)
results[story][roi][prefix[0]][test_subject][hemi]['encoding'] = performance
print(f"Finished forwarding encoding analysis for "
f"{story}, {roi}, {prefix[0]}, {test_subjects}, "
f"performance = {np.mean(performance):.4f}")
# Decoding via dot product between test samples and coefficients
if prefix[0] != 'no SRM (average)':
# Collapse coefficients across delays for decoding
collapse_coef = np.mean(np.split(ridge.coef_, len(delays),
axis=1), axis=0)
collapse_test_model = np.mean(np.split(test_model, len(delays),
axis=1), axis=0)
predicted_model = Ridge(alpha=alpha,
fit_intercept=False).fit(collapse_coef,
test_data.T).coef_
raise
accuracy = rank_accuracy(predicted_model, collapse_test_model)
results[story][roi][prefix[0]][test_subject][hemi]['decoding'] = accuracy
print(f"Finished decoding analysis for "
f"{story}, {roi}, {prefix[0]}, {test_subjects}, "
f"accuracy = {accuracy:.4f} ({rank})")
accuracies.append(accuracy)
np.save(results_fn, results)