-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNEOS_MakeStcsROIs_SingleEpochs.py
409 lines (307 loc) · 15.3 KB
/
NEOS_MakeStcsROIs_SingleEpochs.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 16 17:40:24 2023
@author: fm02
"""
import sys
import os
from os import path
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
import pickle
import mne
from mne.minimum_norm import apply_inverse_epochs
os.chdir("/home/fm02/MEG_NEOS/NEOS")
import NEOS_config as config
#os.chdir("/home/fm02/MEG_NEOS/NEOS/my_eyeCA")
from my_eyeCA import apply_ica
import seaborn as sns
import matplotlib.pyplot as plt
mne.viz.set_browser_backend("matplotlib")
snr = 1.0 # snr should be 1 for single epoch inversion
lambda2 = 1.0 / snr ** 2
loose = 0.2
depth = None
reject_criteria = config.epo_reject
flat_criteria = config.epo_flat
def ovr_sub(ovr):
if ovr in ['nover', 'novr', 'novrw']:
ovr = ''
elif ovr in ['ovrw', 'ovr', 'over', 'overw']:
ovr = '_ovrw'
elif ovr in ['ovrwonset', 'ovrons', 'overonset']:
ovr = '_ovrwonset'
return ovr
labels_path = path.join(config.data_path, "my_ROIs")
stc_path = path.join(config.data_path, "stcs")
lATL = mne.read_label(path.join(labels_path, 'l_ATL_fsaverage-lh.label'),
subject='fsaverage')
rATL = mne.read_label(path.join(labels_path, 'r_ATL_fsaverage-rh.label'),
subject='fsaverage')
PVA = mne.read_label(path.join(labels_path, 'PVA_fsaverage-lh.label'),
subject='fsaverage')
IFG = mne.read_label(path.join(labels_path, 'IFG_fsaverage-lh.label'),
subject='fsaverage')
AG = mne.read_label(path.join(labels_path, 'AG_fsaverage-lh.label'),
subject='fsaverage')
PTC = mne.read_label(path.join(labels_path, 'PTC_fsaverage-lh.label'),
subject='fsaverage')
rois = [lATL,
rATL,
PVA,
IFG,
AG,
PTC]
meta = pd.read_csv('/imaging/hauk/users/fm02/MEG_NEOS/stim/meg_metadata.csv', header=0)
pred = ['ID', 'Word', 'ConcM', 'LEN', 'LogFreq(Zipf)', 'Position', 'Sim']
meta = meta[pred]
scaler = StandardScaler()
meta[['ConcM', 'LEN', 'LogFreq(Zipf)', 'Position', 'Sim']] = scaler.fit_transform(
meta[['ConcM', 'LEN', 'LogFreq(Zipf)', 'Position', 'Sim']])
# %%
def make_stcsEpochs(sbj_id, method='eLORETA', inv_suf='shrunk_dropbads'):
"""In this function stcs are computed as signed soure activity"""
subject = str(sbj_id)
sbj_path = path.join(config.data_path, config.map_subjects[sbj_id][0])
bad_eeg = config.bad_channels_all[sbj_id]['eeg']
ovr = config.ovr_procedure[sbj_id]
ovr = ovr_sub(ovr)
raw_test = apply_ica.get_ica_raw(sbj_id,
condition='both',
overweighting=ovr,
interpolate=False,
drop_EEG_4_8=False)
raw_test = raw_test.set_eeg_reference(ref_channels='average', projection=True)
raw_test.load_data()
raw_test.info['bads'] = bad_eeg
picks = mne.pick_types(raw_test.info, meg=True, eeg=True, exclude='bads')
target_evts = mne.read_events(path.join(sbj_path, config.map_subjects[sbj_id][0][-3:] + \
'_target_events.fif'))
rows = np.where(target_evts[:,2]==999)[0]
event_dict = {'FRP': 999}
tmin, tmax = -.3, .7
epochs = mne.Epochs(raw_test, target_evts, event_dict, tmin=tmin, tmax=tmax,
picks=picks, reject=None, preload=True)
metadata = pd.DataFrame(columns=meta.columns)
for row in rows:
index = target_evts[row-2, 2]*100 + target_evts[row-1, 2]
metadata = pd.concat([metadata,
meta[meta['ID']==index]])
epochs.metadata = metadata
epochs.resample(250, npad='auto')
sbj_path = path.join(config.data_path, config.map_subjects[sbj_id][0])
inv_fname = path.join(sbj_path, subject + f'_EEGMEG{inv_suf}-inv.fif')
inverse_operator = mne.minimum_norm.read_inverse_operator(inv_fname)
rois_subject = mne.morph_labels(rois, subject_to=subject,
subject_from='fsaverage',
subjects_dir=config.subjects_dir)
rois_lab = ['lATL',
'rATL',
'PVA',
'IFG',
'AG',
'PTC']
# we are doing normal orientation because intensities do not seem reliable
stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, method,
pick_ori="normal", nave=len(epochs))
stc_epochs = dict()
epoch_rois = dict()
for i, roi in enumerate(rois_subject):
stc_epochs[rois_lab[i]] = [epoch.extract_label_time_course(roi,
inverse_operator['src'],
mode='mean_flip').squeeze() for epoch in stc]
epoch_rois[rois_lab[i]] = np.array(stc_epochs[rois_lab[i]])
times = epochs.times
for roi in rois_lab:
sns.lineplot(x=times, y=epoch_rois[roi].mean(axis=0), label=roi)
plt.axvline(0, color='k');
plt.axhline(0, color='k', alpha=0.3, linewidth = 0.5);
plt.legend()
plt.savefig(path.join(config.data_path, "plots", "stcs",
f"{sbj_id}_data_for_mixed.png"))
one_subj = dict()
for j, t in enumerate(times):
df_t = pd.DataFrame(columns=['ID', 'Word', 'ConcM', 'LEN', 'LogFreq(Zipf)', 'Position', 'Sim', 'sbj', 'activity', 'roi'])
for i, roi in enumerate(rois_subject):
df = metadata.copy().reset_index(drop=True)
df['sbj'] = subject
rois_act = pd.DataFrame(epoch_rois[rois_lab[i]][:, j], columns=['activity'])
rois_act['roi'] = rois_lab[i]
df = pd.concat([df, rois_act], axis=1)
df_t = pd. concat([df_t, df])
one_subj[round(t*10e2)] = df_t
with open(f'/imaging/hauk/users/fm02/MEG_NEOS/data/data_for_mixed_models/sbj_{subject}.P', 'wb') as handle:
pickle.dump(one_subj, handle, protocol=pickle.HIGHEST_PROTOCOL)
def make_stcsEpochs_intensities(sbj_id, method='eLORETA', inv_suf='shrunk_dropbads'):
"""In this function, stcs for mixed models are computed as intensities."""
subject = str(sbj_id)
sbj_path = path.join(config.data_path, config.map_subjects[sbj_id][0])
bad_eeg = config.bad_channels_all[sbj_id]['eeg']
ovr = config.ovr_procedure[sbj_id]
ovr = ovr_sub(ovr)
raw_test = apply_ica.get_ica_raw(sbj_id,
condition='both',
overweighting=ovr,
interpolate=False,
drop_EEG_4_8=False)
raw_test = raw_test.set_eeg_reference(ref_channels='average', projection=True)
raw_test.load_data()
raw_test.info['bads'] = bad_eeg
picks = mne.pick_types(raw_test.info, meg=True, eeg=True, exclude='bads')
target_evts = mne.read_events(path.join(sbj_path, config.map_subjects[sbj_id][0][-3:] + \
'_target_events.fif'))
rows = np.where(target_evts[:,2]==999)[0]
event_dict = {'FRP': 999}
tmin, tmax = -.3, .7
epochs = mne.Epochs(raw_test, target_evts, event_dict, tmin=tmin, tmax=tmax,
picks=picks, reject=None, preload=True)
metadata = pd.DataFrame(columns=meta.columns)
for row in rows:
index = target_evts[row-2, 2]*100 + target_evts[row-1, 2]
metadata = pd.concat([metadata,
meta[meta['ID']==index]])
epochs.metadata = metadata
epochs.resample(250, npad='auto')
sbj_path = path.join(config.data_path, config.map_subjects[sbj_id][0])
inv_fname = path.join(sbj_path, subject + f'_EEGMEG{inv_suf}-inv.fif')
inverse_operator = mne.minimum_norm.read_inverse_operator(inv_fname)
rois_subject = mne.morph_labels(rois, subject_to=subject,
subject_from='fsaverage',
subjects_dir=config.subjects_dir)
rois_lab = ['lATL',
'rATL',
'PVA',
'IFG',
'AG',
'PTC']
# we are doing normal orientation because intensities do not seem reliable
stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, method,
pick_ori=None, nave=len(epochs))
stc_epochs = dict()
epoch_rois = dict()
for i, roi in enumerate(rois_subject):
stc_epochs[rois_lab[i]] = [epoch.extract_label_time_course(roi,
inverse_operator['src'],
mode='mean').squeeze() for epoch in stc]
epoch_rois[rois_lab[i]] = np.array(stc_epochs[rois_lab[i]])
times = epochs.times
for roi in rois_lab:
sns.lineplot(x=times, y=epoch_rois[roi].mean(axis=0), label=roi)
plt.axvline(0, color='k');
plt.axhline(0, color='k', alpha=0.3, linewidth = 0.5);
plt.legend()
plt.savefig(path.join(config.data_path, "plots", "stcs",
f"{sbj_id}_data_for_mixed_intensities.png"))
one_subj = dict()
for j, t in enumerate(times):
df_t = pd.DataFrame(columns=['ID', 'Word', 'ConcM', 'LEN', 'LogFreq(Zipf)', 'Position', 'Sim', 'sbj', 'activity', 'roi'])
for i, roi in enumerate(rois_subject):
df = metadata.copy().reset_index(drop=True)
df['sbj'] = subject
rois_act = pd.DataFrame(epoch_rois[rois_lab[i]][:, j], columns=['activity'])
rois_act['roi'] = rois_lab[i]
df = pd.concat([df, rois_act], axis=1)
df_t = pd. concat([df_t, df])
one_subj[round(t*10e2)] = df_t
with open(f'/imaging/hauk/users/fm02/MEG_NEOS/data/data_for_mixed_models/sbj_{subject}_intensities.P', 'wb') as handle:
pickle.dump(one_subj, handle, protocol=pickle.HIGHEST_PROTOCOL)
def make_stcsEpochs_factorial(sbj_id, method='eLORETA', inv_suf='shrunk_dropbads'):
"""In this function, stcs for mixed models are computed as signed activity."""
subject = str(sbj_id)
sbj_path = path.join(config.data_path, config.map_subjects[sbj_id][0])
bad_eeg = config.bad_channels_all[sbj_id]['eeg']
ovr = config.ovr_procedure[sbj_id]
ovr = ovr_sub(ovr)
raw_test = apply_ica.get_ica_raw(sbj_id,
condition='both',
overweighting=ovr,
interpolate=False,
drop_EEG_4_8=False)
raw_test = raw_test.set_eeg_reference(ref_channels='average', projection=True)
raw_test.load_data()
raw_test.info['bads'] = bad_eeg
picks = mne.pick_types(raw_test.info, meg=True, eeg=True, exclude='bads')
target_evts = mne.read_events(path.join(sbj_path, config.map_subjects[sbj_id][0][-3:] + \
'_target_events.fif'))
rows = np.where(target_evts[:,2]==999)[0]
for row in rows:
if target_evts[row-2, 2] == 1:
target_evts[row, 2] = 991
elif target_evts[row-2, 2] == 2:
target_evts[row, 2] = 992
elif target_evts[row-2, 2] == 3:
target_evts[row, 2] = 993
elif target_evts[row-2, 2] == 4:
target_evts[row, 2] = 994
elif target_evts[row-2, 2] == 5:
target_evts[row, 2] = 995
event_dict = {'Abstract/Predictable': 991,
'Concrete/Predictable': 992,
'Abstract/Unpredictable': 993,
'Concrete/Unpredictable': 994}
tmin, tmax = -.3, .7
tmin, tmax = -.3, .7
epochs = mne.Epochs(raw_test, target_evts, event_dict, tmin=tmin, tmax=tmax,
picks=picks, reject=None, preload=True)
epochs.resample(250, npad='auto')
sbj_path = path.join(config.data_path, config.map_subjects[sbj_id][0])
inv_fname = path.join(sbj_path, subject + f'_EEGMEG{inv_suf}-inv.fif')
inverse_operator = mne.minimum_norm.read_inverse_operator(inv_fname)
rois_subject = mne.morph_labels(rois, subject_to=subject,
subject_from='fsaverage',
subjects_dir=config.subjects_dir)
rois_lab = ['lATL',
'rATL',
'PVA',
'IFG',
'AG',
'PTC']
stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, method,
pick_ori="normal", nave=len(epochs))
conditions = [epochs[i].event_id for i in range(len(epochs))]
conditions = np.array([list(conditions[i].keys()) for i in range(len(epochs))]).squeeze()
info = pd.DataFrame(conditions, columns=['Interaction'])
Conc = [inf.split('/')[0] for inf in info['Interaction']]
Pred = [inf.split('/')[1] for inf in info['Interaction']]
info['Conc'] = Conc
info['Pred'] = Pred
stc_epochs = dict()
epoch_rois = dict()
for i, roi in enumerate(rois_subject):
stc_epochs[rois_lab[i]] = [epoch.extract_label_time_course(roi,
inverse_operator['src'],
mode='mean_flip').squeeze() for epoch in stc]
epoch_rois[rois_lab[i]] = np.array(stc_epochs[rois_lab[i]])
avg = np.array([epoch_rois[key] for key in epoch_rois.keys()]).mean(axis=0)
epoch_rois['avg'] = avg
times = epochs.times
for roi in epoch_rois.keys():
sns.lineplot(x=times, y=epoch_rois[roi].mean(axis=0), label=roi)
plt.axvline(0, color='k');
plt.axhline(0, color='k', alpha=0.3, linewidth = 0.5);
plt.legend()
plt.savefig(path.join(config.data_path, "plots", "stcs",
f"{sbj_id}_data_for_mixed_factorial.png"))
one_subj = dict()
for j, t in enumerate(times):
df_t = pd.DataFrame(columns=['Conc', 'Pred', 'Interaction', 'sbj', 'activity', 'roi'])
for i, roi in enumerate(epoch_rois.keys()):
df = info.copy().reset_index(drop=True)
df['sbj'] = subject
rois_act = pd.DataFrame(epoch_rois[roi][:, j], columns=['activity'])
rois_act['roi'] = roi
df = pd.concat([df, rois_act], axis=1)
df_t = pd. concat([df_t, df])
one_subj[round(t*10e2)] = df_t
with open(f'/imaging/hauk/users/fm02/MEG_NEOS/data/data_for_mixed_models/sbj_{subject}_factorial.P', 'wb') as handle:
pickle.dump(one_subj, handle, protocol=pickle.HIGHEST_PROTOCOL)
# if len(sys.argv) == 1:
# sbj_ids = [1,2,3,5,6,8,9,10,11,12,13,14,15,16,17,18,19,
# 21,22,23,24,25,26,27,28,29,30]
# else:
# # get list of subjects IDs to process
# sbj_ids = [int(aa) for aa in sys.argv[1:]]
# for ss in sbj_ids:
# make_InverseOperator(ss)