-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_covmatrix.py
250 lines (204 loc) · 12.2 KB
/
check_covmatrix.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 15 15:21:50 2023
@author: fm02
"""
import sys
import os
from os import path
import numpy as np
import pandas as pd
import mne
from mne.preprocessing import ICA, create_eog_epochs
import seaborn as sns
import matplotlib.pyplot as plt
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 preprocess, ica, snr_metrics, apply_ica
os.chdir("/home/fm02/MEG_NEOS/NEOS")
import pickle
# mne.viz.set_browser_backend("matplotlib")
# %%
max_list = dict(eeg=[], mag=[], grad=[])
min_list = dict(eeg=[], mag=[], grad=[])
avg_list = dict(eeg=[], mag=[], grad=[])
def compute_covariance(sbj_id):
subject = str(sbj_id)
ovr = config.ovr_procedure
sbj_path = path.join(config.data_path, config.map_subjects[sbj_id][0])
bad_eeg = config.bad_channels[sbj_id]['eeg']
if ovr[sbj_id] == 'ovrons':
over = '_ovrwonset'
elif ovr[sbj_id] == 'ovr':
over = '_ovrw'
elif ovr[sbj_id] == 'novr':
over = ''
condition = 'both'
raw_test = []
for i in range(1,6):
raw_test.append(mne.io.read_raw(path.join(sbj_path, f"block{i}_sss_f_ica{over}_{condition}_raw.fif")))
raw_test= mne.concatenate_raws(raw_test)
raw_test.load_data()
raw_test.info['bads'] = bad_eeg
picks = mne.pick_types(raw_test.info, meg=True, eeg=True, exclude='bads')
# raw_test.interpolate_bads(reset_bads=True)
raw_test.filter(l_freq=0.5, h_freq=None)
target_evts = mne.read_events(path.join(sbj_path, config.map_subjects[sbj_id][0][-3:] + \
'_target_events.fif'))
# manually checked for the shortest possible time the fixation cross was on
# this is
evt = pd.DataFrame(target_evts, columns=['time','n','trigger'])
# we need to consider 34ms delay for those triggers that reflect a change in the display
# note that we don't need to add the delay for the eye events (fixation/saccades)
# (i.e., they are not generated by a change on screen, so they are not affected by this delay)
# the events that are affected are:
# when fixation cross appears (TRIGGER 93)
# when the sentence appears (TRIGGER 94)
# when the sentence disappears (TRIGGER 95)
# when the calibration screen is presented (we ignore that by aligning the triggers)
# alternatively we might ignore this problem and get the stimuli even before, it should
# not matter for covariance matrix
evt.apply(lambda x: x['time']+34 if x['trigger'] in [93, 94,95] else x['time'], axis=1)
event_dict = {'Stim_on': 94}
epochs = mne.Epochs(raw_test, evt, tmin=-0.450, tmax=0.0, event_id=event_dict,
flat=None, picks=picks, reject_by_annotation=False,
reject=None, preload=True)
noise_cov_empirical = mne.compute_covariance(epochs, method='empirical',
tmax=0, rank='info', return_estimators=True)
noise_cov = mne.cov.regularize(noise_cov_empirical, raw_test.info, mag=0.1, grad=0.1,
eeg=0.1, rank='info')
fname_cov = path.join(sbj_path, config.map_subjects[sbj_id][0][-3:] + \
"_covariancematrix_empirical-cov.fif")
mne.write_cov(fname_cov, noise_cov)
# picks_eeg = mne.pick_types(epochs.info, exclude='bads', meg=False, eeg=True)
# picks_grad = mne.pick_types(epochs.info, exclude='bads', meg='grad', eeg=False)
# picks_mag = mne.pick_types(epochs.info, exclude='bads', meg='mag', eeg=False)
# for ch_type, picks in zip(['eeg', 'mag', 'grad'], [picks_eeg, picks_mag, picks_grad]):
# max_list[ch_type] = noise_cov['data'][picks,:][:,picks].max()
# min_list[ch_type] = noise_cov['data'][picks,:][:,picks].min()
# avg_list[ch_type] = noise_cov['data'][picks,:][:,picks].mean()
# noisy_channel = np.unravel_index(np.argmax(noise_cov['data'][picks_eeg,:][:,picks_eeg], axis=None), noise_cov['data'][picks_eeg,:][:,picks_eeg].shape)
# noisy_channel
# ch_name = epochs.info['ch_names'][noisy_channel[0]]
# noisy = dict()
# noisy[ch_name]=noisy_channel
# with open(f'/imaging/hauk/users/fm02/MEG_NEOS/data/misc/empirical_covariance/{subject}_noisy.py', 'wb') as f:
# pickle.dump(noisy, f)
# save_max = pd.DataFrame.from_dict(max_list, orient='index',
# columns=[subject])
# save_max.to_csv(f'/imaging/hauk/users/fm02/MEG_NEOS/data/misc/empirical_covariance/{subject}_max.csv',
# index=False)
# save_min = pd.DataFrame.from_dict(min_list, orient='index',
# columns=[subject])
# save_min.to_csv(f'/imaging/hauk/users/fm02/MEG_NEOS/data/misc/empirical_covariance/{subject}_min.csv',
# index=False)
# save_avg = pd.DataFrame.from_dict(avg_list, orient='index',
# columns=[subject])
# save_avg.to_csv(f'/imaging/hauk/users/fm02/MEG_NEOS/data/misc/empirical_covariance/{subject}_avg.csv',
# index=False)
# figs = noise_cov.plot(epochs.info, proj=True)
# for i, fig in zip(['matrix', 'eigenvalue_index'], figs):
# fname_fig = f'/imaging/hauk/users/fm02/MEG_NEOS/data/misc/empirical_covariance/{subject}_covariance_{i}.png'
# fig.savefig(fname_fig)
# evoked = epochs.average()
# fig = evoked.plot_white(noise_cov, time_unit='s')
# fname_fig = f'/imaging/hauk/users/fm02/MEG_NEOS/data/misc/empirical_covariance/{subject}_whitened_empirical_{i}.png'
# fig.savefig(fname_fig)
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:
compute_covariance(ss)
# %%
# MIN
# eeg mag grad
# 1 -1.75501642067792e-10 -1.6862153497477318e-26 -1.7159315785599152e-23
# 2 -1.8345533667094856e-11 -2.4276459908834612e-26 -1.5115480867122936e-23
# 3 -1.8750110446195348e-10 -3.5722708308069505e-26 -4.2522087805065284e-23
# 5 -1.3075780193284056e-11 -1.9205321006554769e-26 -9.409876192105225e-24
# 6 -1.1024172179649505e-11 -3.3287933811854563e-26 -1.2477094157686558e-23
# 8 -1.3733455031054284e-10 -2.137613021452824e-26 -1.861245160956176e-23
# 9 -3.167320780187942e-11 -1.53958513119721e-26 -1.9301346430541666e-23
# 10 -1.1649466857310516e-11 -4.3638869612750855e-26 -1.5353577928323452e-23
# 11 -2.2194329666960607e-10 -2.2038631697125415e-26 -2.5546083819877037e-23
# 12 -1.743160318017389e-10 -5.787068510248695e-26 -5.191084887852546e-23
# 13 -1.5290562663215128e-11 -2.0886638912430846e-26 -1.832189948367165e-23
# 14 -1.6142806606741554e-11 -2.340347482511508e-26 -2.3892034031336046e-23
# 15 -2.791362794743649e-10 -3.106089024236125e-26 -2.995656198031039e-23
# 16 -1.314928983885995e-11 -1.3725496275573339e-26 -1.117644830781397e-23
# 17 -1.0058462158951861e-10 -1.181841476667159e-25 -1.3527122008397758e-22
# 18 -3.4599044942093376e-11 -7.002107894079638e-26 -1.9667730513592522e-23
# 19 -8.232348870887106e-10 -4.677124067674186e-26 -2.383595223826922e-23
# 21 -1.6296479770794254e-09 -3.454569238041194e-26 -3.873273054821715e-23
# 22 -2.8697015623037708e-11 -2.667875421881819e-26 -3.030854078217991e-23
# 23 -2.3375527200215064e-11 -4.548340959499582e-26 -2.104944727723484e-23
# 24 -1.4934650798589382e-11 -4.688719924231467e-26 -2.3895672880841985e-23
# 25 -1.9697842440584646e-11 -3.551465494721085e-26 -2.850061765824906e-23
# 26 -3.115597242261508e-11 -2.3138379942188078e-26 -2.667954465235504e-23
# 27 -2.600487020651105e-11 -3.3566549120814124e-26 -2.91404015437878e-23
# 28 -2.7873563364929263e-09 -2.8393940433195703e-26 -1.695803566408207e-23
# 29 -4.2757358759021605e-11 -1.8942142436817542e-26 -1.710917535493442e-23
# 30 -3.7562106119120016e-11 -2.995362211759157e-26 -2.447061634395927e-23
# AVERAGE
# eeg mag grad
# 1 3.3526588471892996e-29 1.1158793247949657e-27 2.688686045658134e-25
# 2 -2.116667446772912e-29 1.4173563017051322e-27 3.4282380857899727e-25
# 3 2.2794880196015973e-29 1.2643808782559916e-27 5.433075354799293e-25
# 5 -1.4653851554581698e-29 9.52045130680653e-28 2.093443265487869e-25
# 6 3.4734472107339576e-30 3.1923070298020157e-27 2.3061241691111303e-25
# 8 -4.5589760392031946e-29 1.3314942343360251e-27 3.321024082699637e-25
# 9 -6.310887241768093e-29 4.498779599977704e-27 2.2948647727020857e-25
# 10 -3.944304526105059e-30 9.223997720932551e-28 1.298296021913253e-25
# 11 -2.1614788803055723e-28 4.166722612300233e-27 3.1969053104134408e-25
# 12 -4.0347649545705687e-29 1.1633970854997436e-27 3.1599507551474925e-25
# 13 6.946894421467915e-30 1.1612433354584449e-27 3.926919165553648e-25
# 14 -6.310887241768096e-30 4.1448194693252836e-27 3.4828598323506264e-25
# 15 1.5382787651809727e-29 2.1312963438730862e-27 5.222456449669478e-25
# 16 -7.326925777290849e-30 3.7530312913481805e-28 1.4562397467490023e-25
# 17 0.0 4.6158480139382996e-27 1.0411588776456016e-24
# 18 4.3387349787155656e-30 1.860649813546478e-27 3.9284254201465904e-25
# 19 -5.043456193213212e-29 1.752508485504884e-27 4.636765335976524e-25
# 21 -1.5078813249664568e-28 1.9872333889604647e-26 3.852400954557982e-25
# 22 4.457064114498717e-29 7.075761784497331e-28 2.6379142772092237e-25
# 23 2.2876966251409345e-29 3.965537856028765e-27 3.2152761780768363e-25
# 24 -2.353612890166166e-29 1.940911951342743e-27 3.616811452572849e-25
# 25 3.026073715927927e-29 1.3812655828536187e-27 4.261229001145336e-25
# 26 3.362304128808808e-29 3.9430158160162806e-27 4.775126072534689e-25
# 27 2.2088105346188328e-29 2.0186417719404997e-27 7.664386798947446e-25
# 28 -1.0420516661035875e-28 3.1228829013537784e-27 2.9512842670105527e-25
# 29 -6.946894421467915e-30 2.320613165567805e-27 3.619850872398274e-25
# 30 -2.721570123012491e-29 3.7211247490033066e-27 5.6495082676303805e-25
# MAX
# eeg mag grad
# 1 7.903154891635207e-10 5.455518163699204e-26 3.7714581189993394e-23
# 2 1.6053530410042707e-10 6.822059405267985e-26 4.292826341512933e-23
# 3 6.989589751183197e-10 1.156786607487545e-25 1.1862781814772758e-22
# 5 4.8165989896003925e-11 4.174947114839949e-26 2.0299085579652068e-23
# 6 5.7023997330790646e-11 7.921410819116076e-26 3.223804325898509e-23
# 8 3.2739039127835426e-10 6.026369465437634e-26 4.2091295266681363e-23
# 9 1.1731877975070501e-10 6.547174782042448e-26 4.6088973651935973e-23
# 10 4.9351302295225367e-11 6.486789788821183e-26 2.635977728181927e-23
# 11 2.9139824699795715e-09 9.171312669734236e-26 6.077144568189915e-23
# 12 1.27457919087997e-09 1.101327778379508e-25 1.364755249580363e-22
# 13 3.1341963335897155e-11 5.80752100272192e-26 4.889517925237507e-23
# 14 7.480799475116035e-11 9.315000148350824e-26 5.062147179788126e-23
# 15 9.31601056106069e-10 1.0850770476539942e-25 8.651336074652942e-23
# 16 4.8321426857882407e-11 3.1576775459493577e-26 2.423217857558083e-23
# 17 5.91294000468556e-10 3.0046776832447352e-25 2.6050375042048446e-22
# 18 4.429859046409571e-10 1.23741857772182e-25 4.7301833059832053e-23
# 19 1.388394620076279e-09 8.881881459252177e-26 5.306982925702441e-23
# 21 1.1588660751649149e-08 1.3582420379393665e-25 7.677069179378363e-23
# 22 4.5722016778995836e-10 5.471326648441462e-26 3.754727303881265e-23
# 23 5.377247611560112e-11 1.0182882966373266e-25 5.820513426560496e-23
# 24 6.979666499086529e-11 1.066311219622813e-25 4.755510910830583e-23
# 25 9.315299634446354e-11 1.0047723454731014e-25 6.722137640047347e-23
# 26 4.0500587608541555e-10 9.935419228201998e-26 5.2923322390738405e-23
# 27 7.456123676097631e-11 1.287967313747608e-25 7.433430909139791e-23
# 28 4.166657028942284e-09 7.131468607618308e-26 3.51365748551658e-23
# 29 5.076692981994262e-10 5.976860186021544e-26 4.543399107770078e-23
# 30 8.116468018848858e-11 1.0155632090946164e-25 7.294618490387473e-23