This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6_Theta_Congruncy.py
428 lines (279 loc) · 14.3 KB
/
6_Theta_Congruncy.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
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 25 10:19:01 2021
@author: Carlos
"""
import mne
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import os
import os.path as op
from mne.datasets import fetch_fsaverage
import seaborn as sns
from mne import stats
from mne.stats import permutation_cluster_test, spatio_temporal_cluster_test, summarize_clusters_stc
from mne.stats import f_mway_rm, f_threshold_mway_rm, ttest_1samp_no_p, permutation_cluster_1samp_test, spatio_temporal_cluster_1samp_test
import pickle
from scipy import stats as stats
base_path = 'D:/BindEEG/'
os.chdir(base_path)
###############################################################################################################
## Importing anatomy template and specific functions
###############################################################################################################
# Download fsaverage files
fs_dir = fetch_fsaverage(verbose=True)
subjects_dir = op.dirname(fs_dir)
# The files live in:
subject = 'fsaverage'
trans = 'fsaverage' # MNE has a built-in fsaverage transformation
src_name = op.join(fs_dir, 'bem', 'fsaverage-oct-6-src.fif')
src = mne.read_source_spaces(src_name, verbose = True)
###############################################################################################################
# Loading and plotting results
###############################################################################################################
os.chdir(base_path + '/Sources/theta_whole_brain')
subjlist = [ 1, 2, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]
Impl_LL = list()
Impl_LR = list()
Impl_RL = list()
Impl_RR = list()
Memo_LL = list()
Memo_LR = list()
Memo_RL = list()
Memo_RR = list()
for idx, val in enumerate(subjlist):
Impl_LL.append(mne.read_source_estimate('Impl_LL_%s' % val))
Impl_LR.append(mne.read_source_estimate('Impl_LR_%s' % val))
Impl_RL.append(mne.read_source_estimate('Impl_RL_%s' % val))
Impl_RR.append(mne.read_source_estimate('Impl_RR_%s' % val))
Memo_LL.append(mne.read_source_estimate('Memo_LL_%s' % val))
Memo_LR.append(mne.read_source_estimate('Memo_LR_%s' % val))
Memo_RL.append(mne.read_source_estimate('Memo_RL_%s' % val))
Memo_RR.append(mne.read_source_estimate('Memo_RR_%s' % val))
###############################################################################################################
# STATS on sources
###############################################################################################################
Impl_CuedLeft_RespLeft = list()
Impl_CuedLeft_RespRight = list()
Impl_CuedRight_RespLeft = list()
Impl_CuedRight_RespRight = list()
Memo_CuedLeft_RespLeft = list()
Memo_CuedLeft_RespRight = list()
Memo_CuedRight_RespLeft = list()
Memo_CuedRight_RespRight = list()
for idx, val in enumerate(subjlist):
Impl_CuedLeft_RespLeft.append(Impl_LL[idx].copy().crop(0, 1.8).data)
Impl_CuedLeft_RespRight.append(Impl_LR[idx].copy().crop(0, 1.8).data)
Impl_CuedRight_RespLeft.append(Impl_RL[idx].copy().crop(0, 1.8).data)
Impl_CuedRight_RespRight.append(Impl_RR[idx].copy().crop(0, 1.8).data)
Memo_CuedLeft_RespLeft.append(Memo_LL[idx].copy().crop(0, 1.8).data)
Memo_CuedLeft_RespRight.append(Memo_LR[idx].copy().crop(0, 1.8).data)
Memo_CuedRight_RespLeft.append(Memo_RL[idx].copy().crop(0, 1.8).data)
Memo_CuedRight_RespRight.append(Memo_RR[idx].copy().crop(0, 1.8).data)
# as we only have one hemisphere we need only need half the adjacency
print('Computing adjacency.')
adjacency = mne.spatial_src_adjacency(src)
dist = 0.02 # need to find the right threshold!
from scipy import sparse
from scipy.spatial.distance import cdist
adj = cdist(src[0]['rr'][src[0]['vertno']],
src[1]['rr'][src[1]['vertno']])
adj = sparse.csr_matrix(adj <= dist, dtype=int)
empties = [sparse.csr_matrix((nv, nv), dtype=int) for nv in adj.shape]
adj = sparse.vstack([sparse.hstack([empties[0], adj]),
sparse.hstack([adj.T, empties[1]])])
adj_tot = sparse.coo_matrix(adjacency + adj)
###############################################################################################################
# Congruent vs incongruent Impl
###############################################################################################################
# Data: for the function permutation_cluster_test() it needs to be a list of arrays
# each array contains data for one group/condition of observations
I_congr = np.stack([Impl_CuedLeft_RespLeft, Impl_CuedRight_RespRight])
I_congr = np.mean(I_congr, axis = 0)
I_incongr = np.stack([Impl_CuedLeft_RespRight, Impl_CuedRight_RespLeft])
I_incongr = np.mean(I_incongr, axis = 0)
I_contr = I_incongr - I_congr
# Note that X needs to be a multi-dimensional array of shape
# samples (subjects) x time x space, so we permute dimensions
I_contr = np.transpose(I_contr, [0, 2, 1])
# Now let's actually do the clustering. This can take a long time...
# Here we set the threshold quite high to reduce computation.
p_threshold = 0.001
t_threshold = -stats.distributions.t.ppf(p_threshold / 2., len(subjlist) - 1)
tail = 1 # f-test, so tail > 0
n_permutations = 500 # Save some time (the test won't be too sensitive ...)
print('Clustering.')
T_obs, clusters, cluster_p_values, H0 = clu_I = \
spatio_temporal_cluster_1samp_test(I_contr, adjacency=adj_tot, n_jobs=4, n_permutations = n_permutations,
tail = tail,
threshold=t_threshold, buffer_size=None,
verbose=True)
###############################################################################################################
# Congruent vs incongruent Memo
###############################################################################################################
M_congr = np.stack([Memo_CuedLeft_RespLeft, Memo_CuedRight_RespRight])
M_congr = np.mean(M_congr, axis = 0)
M_incongr = np.stack([Memo_CuedLeft_RespRight, Memo_CuedRight_RespLeft])
M_incongr = np.mean(M_incongr, axis = 0)
M_contr = M_incongr - M_congr
# Note that X needs to be a multi-dimensional array of shape
# samples (subjects) x time x space, so we permute dimensions
M_contr = np.transpose(M_contr, [0, 2, 1])
# Now let's actually do the clustering. This can take a long time...
# Here we set the threshold quite high to reduce computation.
p_threshold = 0.001
t_threshold = -stats.distributions.t.ppf(p_threshold / 2., len(subjlist) - 1)
tail = 1 # f-test, so tail > 0
n_permutations = 500 # Save some time (the test won't be too sensitive ...)
print('Clustering.')
T_obs, clusters, cluster_p_values, H0 = clu_M = \
spatio_temporal_cluster_1samp_test(M_contr, adjacency=adj_tot, n_jobs=4, n_permutations = n_permutations,
tail = tail,
threshold=t_threshold, buffer_size=None,
verbose=True)
###############################################################################################################
# Between Tasks
###############################################################################################################
Diff = I_contr - M_contr
# Now let's actually do the clustering. This can take a long time...
# Here we set the threshold quite high to reduce computation.
p_threshold = 0.001
t_threshold = -stats.distributions.t.ppf(p_threshold / 2., len(subjlist) - 1)
tail = 1 # f-test, so tail > 0
n_permutations = 500 # Save some time (the test won't be too sensitive ...)
print('Clustering.')
T_obs, clusters, cluster_p_values, H0 = clu_IM = \
spatio_temporal_cluster_1samp_test(Diff, adjacency=adj_tot, n_jobs=4, n_permutations = n_permutations,
tail = tail,
threshold=t_threshold, buffer_size=None,
verbose=True)
###############################################################################################################
# Impl vs Memo only in congruent trials
###############################################################################################################
# Data: for the function permutation_cluster_test() it needs to be a list of arrays
# each array contains data for one group/condition of observations
I_congr = np.stack([Impl_CuedLeft_RespLeft, Impl_CuedRight_RespRight])
I_congr = np.mean(I_congr, axis = 0)
M_congr = np.stack([Memo_CuedLeft_RespLeft, Memo_CuedRight_RespRight])
M_congr = np.mean(M_congr, axis = 0)
Diff = I_congr - M_congr
Diff = np.transpose(Diff, [0, 2, 1])
p_threshold = 0.001
t_threshold = -stats.distributions.t.ppf(p_threshold / 2., len(subjlist) - 1)
tail = 1 # f-test, so tail > 0
n_permutations = 500 # Save some time (the test won't be too sensitive ...)
print('Clustering.')
T_obs, clusters, cluster_p_values, H0 = clu_IvsM_congr = \
spatio_temporal_cluster_1samp_test(Diff, adjacency=adj_tot, n_jobs=4, n_permutations = n_permutations,
tail = tail,
threshold=t_threshold, buffer_size=None,
verbose=True)
###############################################################################################################
# Impl vs Memo only in incongruent trials
###############################################################################################################
I_incongr = np.stack([Impl_CuedLeft_RespRight, Impl_CuedRight_RespLeft])
I_incongr = np.mean(I_incongr, axis = 0)
M_incongr = np.stack([Memo_CuedLeft_RespRight, Memo_CuedRight_RespLeft])
M_incongr = np.mean(M_incongr, axis = 0)
Diff = I_incongr - M_incongr
Diff = np.transpose(Diff, [0, 2, 1])
p_threshold = 0.001
t_threshold = -stats.distributions.t.ppf(p_threshold / 2., len(subjlist) - 1)
tail = 1 # f-test, so tail > 0
n_permutations = 500 # Save some time (the test won't be too sensitive ...)
print('Clustering.')
T_obs, clusters, cluster_p_values, H0 = clu_IvsM_incongr = \
spatio_temporal_cluster_1samp_test(Diff, adjacency=adj_tot, n_jobs=4, n_permutations = n_permutations,
tail = tail,
threshold=t_threshold, buffer_size=None,
verbose=True)
###############################################################################################################
# Congruent vs INcongruent across tasks
###############################################################################################################
congr = np.stack([Impl_CuedLeft_RespLeft, Impl_CuedRight_RespRight, Memo_CuedLeft_RespLeft, Memo_CuedRight_RespRight])
congr = np.mean(congr, axis = 0)
incongr = np.stack([Impl_CuedLeft_RespRight, Impl_CuedRight_RespLeft, Memo_CuedLeft_RespRight, Memo_CuedRight_RespLeft])
incongr = np.mean(incongr, axis = 0)
Diff = incongr - congr
Diff = np.transpose(Diff, [0, 2, 1])
p_threshold = 0.001
t_threshold = -stats.distributions.t.ppf(p_threshold / 2., len(subjlist) - 1)
tail = 1 # f-test, so tail > 0
n_permutations = 500 # Save some time (the test won't be too sensitive ...)
print('Clustering.')
T_obs, clusters, cluster_p_values, H0 = clu_congr_vs_incongr = \
spatio_temporal_cluster_1samp_test(Diff, adjacency=adj_tot, n_jobs=4, n_permutations = n_permutations,
tail = tail,
threshold= t_threshold, buffer_size=None,
verbose=True)
###############################################################################################################
# saving
###############################################################################################################
with open('clu_I.pickle', 'wb') as f:
pickle.dump(clu_I, f)
with open('clu_M.pickle', 'wb') as f:
pickle.dump(clu_M, f)
with open('clu_IM.pickle', 'wb') as f:
pickle.dump(clu_IM, f)
with open('clu_IvsM_congr.pickle', 'wb') as f:
pickle.dump(clu_IvsM_congr, f)
with open('clu_IvsM_incongr.pickle', 'wb') as f:
pickle.dump(clu_IvsM_incongr, f)
with open('clu_congr_vs_incongr.pickle', 'wb') as f:
pickle.dump(clu_congr_vs_incongr, f)
###############################################################################################################
# Checking significance and plotting
###############################################################################################################
# Loading Clusters
with open('clu_I.pickle', 'rb') as f:
clu_I = pickle.load(f)
with open('clu_M.pickle', 'rb') as f:
clu_M = pickle.load(f)
with open('clu_IM.pickle', 'rb') as f:
clu_IM = pickle.load(f)
with open('clu_IvsM_congr.pickle', 'rb') as f:
clu_IvsM_congr = pickle.load(f)
with open('clu_IvsM_incongr.pickle', 'rb') as f:
clu_IvsM_incongr = pickle.load(f)
with open('clu_congr_vs_incongr.pickle', 'rb') as f:
clu_congr_vs_incongr = pickle.load(f)
np.sort(clu_I[2])
np.sort(clu_M[2])
np.sort(clu_IM[2])
np.sort(clu_IvsM_congr[2])
np.sort(clu_IvsM_incongr[2])
np.sort(clu_congr_vs_incongr[2])
print('Visualizing clusters.')
fsave_vertices = src
tstep = 7.8125
stc_all_cluster_vis = summarize_clusters_stc(clu_IvsM_congr, tstep=tstep, p_thresh=0.05,
vertices=fsave_vertices,
subject='fsaverage')
# Let's actually plot the first "time point" in the SourceEstimate, which
# shows all the clusters, weighted by duration
mne.viz.set_3d_backend("pyvista")
brain = stc_all_cluster_vis.plot(
subject='fsaverage',
hemi='split',
background='white',
foreground='black',
time_label='',
initial_time=1,
smoothing_steps=10,
alpha = 1,
transparent = True,
clim=dict(kind='value', lims=[0, 1, np.max(stc_all_cluster_vis.data)]))
# time points of the almost significant cluster
time_points_clu = np.unique(clu_IvsM_congr[1][6][0])
times = Impl_LL[0].copy().crop(0, 1.8).times
times_clu = times[time_points_clu]
times_bi = np.isin(times, times_clu, assume_unique = True)
series = pd.Series(times_bi)
data = pd.DataFrame()
data['value'] = series
data['timepoint'] = times*1000
fig, ax = plt.subplots()
sns.lineplot(x="timepoint", y="value",
data=data)
plt.xlabel('Time (ms)')