-
Notifications
You must be signed in to change notification settings - Fork 3
/
interpret.py
306 lines (275 loc) · 7.35 KB
/
interpret.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
#!/usr/bin/python
import os
import argparse
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from nilearn import plotting, image, masking
import deeplight
import hcprep
def main():
np.random.seed(24091)
ap = argparse.ArgumentParser()
ap.add_argument(
"--architecture",
required=False,
default='3D',
help="DeepLight architecture (2D or 3D) (default: 3D)"
)
ap.add_argument(
"--pretrained",
required=False,
default=1,
help="use pre-trained model? (1: True or 0_ False) (default: 1)"
)
ap.add_argument(
"--task",
required=False,
default='MOTOR',
help="data of which task to interpret"\
"(EMOTION, GAMBLING, LANGUAGE, SOCIAL,"\
"MOTOR, RELATIONAL, WM)? (default: MOTOR)"
)
ap.add_argument(
"--subject",
required=False,
default=100307,
help="data of which subject to interpret? (default: 100307)"
)
ap.add_argument(
"--run",
required=False,
default='LR',
help="data of which run to interpret (LR or RL)? (default: LR)"
)
ap.add_argument(
"--data",
required=False,
default='../data/',
help="path to TFR data files"
)
ap.add_argument(
"--out",
required=False,
help="path where DeepLight maps are saved"
)
ap.add_argument(
"--verbose",
required=False,
default=1,
help="comment current program steps"\
"(0: no or 1: yes) (default: 1)"
)
args = ap.parse_args()
architecture = str(args.architecture)
pretrained = bool(int(args.pretrained))
task = str(args.task)
subject = str(args.subject)
run = str(args.run)
verbose = bool(int(args.verbose))
data_path = str(args.data)
if args.out is not None:
out_path = str(args.out)
print(
'Path: {}'.format(out_path)
)
else:
out_path = '../results/relevances/DeepLight/{}/brainmaps/'.format(
architecture)
print(
'"out" not defined. Defaulting to: {}'.format(
out_path
)
)
hcp_info = hcprep.info.basics()
sub_out_path = out_path+'sub-{}/'.format(subject)
os.makedirs(sub_out_path, exist_ok=True)
if verbose:
print(
'\nSaving results to: {}'.format(sub_out_path)
)
sub_bold_img = image.load_img(
hcprep.paths.path_bids_func_mni(
subject=subject,
task=task,
run=run,
path=data_path
)
)
sub_bold_mask = image.load_img(
hcprep.paths.path_bids_func_mask_mni(
subject=subject,
task=task,
run=run,
path=data_path
)
)
tfr_file = hcprep.paths.path_bids_tfr(
subject=subject,
task=task,
run=run,
path=data_path
)
dataset = deeplight.data.io.make_dataset(
files=[tfr_file],
n_onehot=20, # there are 20 cognitive states in the HCP data (so 20 total onehot entries)
# we neglect the last 4 onehot entries, as these belong to the WM task,
# which is not part of the pre-training data (see hcp_info.onehot_idx_per_task):
onehot_idx=np.arange(16),
batch_size=1, # to save memory, we process 1 sample at a time!
repeat=False,
n_workers=2
)
iterator = dataset.make_initializable_iterator()
iterator_features = iterator.get_next()
if architecture == '3D':
deeplight_variant = deeplight.three.model(
batch_size=1,
n_states=16, # pre-trained DeepLight has 16 output states
pretrained=pretrained,
verbose=verbose
)
elif architecture == '2D':
deeplight_variant = deeplight.two.model(
batch_size=1,
n_states=16, # pre-trained DeepLight has 16 output states
pretrained=pretrained,
verbose=verbose
)
else:
raise ValueError(
"Invalid value for DeepLight architecture."\
"Must be 2D or 3D."
)
# we need to setup the LRP copmutation before calling .interpret
deeplight_variant.setup_lrp()
sess = tf.Session()
sess.run(iterator.initializer)
if verbose:
print(
'\nInterpreting predictions for task: {}, subject: {}, run: {}'.format(
task, subject, run
)
)
states = []
relevances = []
trs = []
acc = 0
n = 0
while True:
try:
(
batch_volume,
batch_trs,
batch_state,
batch_onehot
) = sess.run(
[
iterator_features['volume'],
iterator_features['tr'],
iterator_features['state'],
iterator_features['onehot']
]
)
batch_pred = deeplight_variant.decode(
volume=batch_volume
)
batch_relevances = deeplight_variant.interpret(
volume=batch_volume
)
for i in range(batch_state.shape[0]):
relevances.append(batch_relevances[i])
states.append(batch_state[i])
trs.append(batch_trs[i])
acc += np.sum(batch_pred[i].argmax() == batch_onehot[i].argmax())
n += 1
if verbose and (n%10) == 0:
print(
'\tDecoding accuracy after {} batches: {} %'.format(
n, (acc/n)*100
)
)
except tf.errors.OutOfRangeError:
break
if verbose:
print('..done.')
trs = np.concatenate(trs)
states = np.concatenate(states)
relevances = np.concatenate(
[
np.expand_dims(r, -1)
for r in relevances
],
axis=-1
)
# sort relevances / states by their TR
tr_idx = np.argsort(trs)
relevances = relevances[...,tr_idx]
states = states[tr_idx]
sub_relevance_img = image.new_img_like(
ref_niimg=sub_bold_img,
data=relevances
)
sub_relevance_img.to_filename(
sub_out_path+'sub-{}_task-{}_run-{}_desc-relevances.nii.gz'.format(
subject, task, run))
if verbose:
print(
'\nPlotting brainmaps to: {}'.format(sub_out_path)
)
for si, state in enumerate(hcp_info.states_per_task[task]):
# subset imgs to cognitive state
state_relevance_img = image.index_img(
imgs=sub_relevance_img,
index=states==si
)
state_relevance_img = image.smooth_img(
imgs=state_relevance_img,
fwhm=6
)
mean_state_relevance_img = image.mean_img(
imgs=state_relevance_img
)
mean_state_relevance_img.to_filename(
sub_out_path+'sub-{}_task-{}_run-{}_desc-{}_avg_relevances.nii.gz'.format(
subject, task, run, state
)
)
# 95 percentile threshold for plotting
threshold = np.percentile(
masking.apply_mask(
imgs=mean_state_relevance_img,
mask_img=sub_bold_mask
),
95
)
# surface
plotting.plot_img_on_surf(
stat_map=mean_state_relevance_img,
views=['lateral', 'medial', 'ventral'],
hemispheres=['left', 'right'],
title='State: {}'.format(state),
colorbar=True,
cmap='inferno',
threshold=threshold
);
plt.savefig(
sub_out_path+'sub-{}_task-{}_run-{}_desc-{}_avg_relevance_surf_brainmap.png'.format(
subject, task, run, state), dpi=200)
plt.clf()
# axial slices
plotting.plot_stat_map(
stat_map_img=mean_state_relevance_img,
display_mode='z',
cut_coords=30,
cmap=plt.cm.seismic,
threshold=threshold
)
plt.savefig(
sub_out_path+'sub-{}_task-{}_run-{}_desc-{}_avg_relevance_axial_slices.png'.format(
subject, task, run, state), dpi=200)
plt.clf()
if verbose:
print('\n\n..done.')
if __name__ == '__main__':
main()