-
Notifications
You must be signed in to change notification settings - Fork 2
/
eeg_coherence_master.py
executable file
·193 lines (181 loc) · 7.29 KB
/
eeg_coherence_master.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# Copyright 2016 by Branislav Gerazov
#
# See the file LICENSE for the license associated with this software.
#
# Author(s):
# Branislav Gerazov, July 2016
"""
Analyse all coherence data.
"""
from __future__ import division
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as img
import cPickle
from matplotlib import cm
import matplotlib as mpl
#%% init
sens = ['Fp1', 'Fp2', 'F7', 'F3', 'Fz', 'F4', 'F8', 'T3', 'C3', 'Cz', 'C4',
'T4', 'T5', 'P3', 'Pz', 'P4', 'T6', 'O1', 'O2']
groups = ['Control goup', 'ADHD group']
eyes = ['closed eyes', 'open eyes']
bands = ['delta','theta','alpha','beta']
fs = 250
eeg_no_ch = len(sens)
#%% load
with open('coherence_master.pickle', "rb") as f:
coherence_master = cPickle.load(f)
# coherence_master = np.zeros((2,30,2,eeg_no_ch,eeg_no_ch, 5))
# these are: groups, people, eyes, ..., bands
# if "open" eyes = 1
# if "Norm" group = 0
#%% plot example coherence
fig, ax = plt.subplots(nrows=2, ncols=2)
for band in range(4):
plt.subplot(2,2,band+1)
plt.title(bands[band])
plt.imshow(coherence_master[0,7,1,:,:,band], aspect='auto', \
origin='lower', \
extent=[0, eeg_no_ch, 0, eeg_no_ch],\
cmap='plasma')
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.03, 0.7])
cmap = mpl.cm.plasma
norm = mpl.colors.Normalize(vmin=0.5, vmax=1)
mpl.colorbar.ColorbarBase(cbar_ax, cmap=cmap,
norm=norm, orientation='vertical',
ticks=[tick for tick in np.arange(0.5,1.1,0.1)])
#%% plot example topographic map
eeg_1020 = img.imread('21ch_eeg.png')
el_centers_dict = {"Cz": (197,181), "C3" : (134,181),
"C4" : (261,181), "T3" : (70,181), "T4" : (324,181),
"Fz" : (197,117), "F3" : (146,116), "F4" : (250,116),
"F7" : (95,107), "F8" : (300,107), "Fp1" : (156,61),
"Fp2" : (239,61), "O1" : (157,301),
"O2" : (238,301), "Pz" : (197,245),
"P3" : (146,245), "P4" : (250,245), "T5" : (95,255),
"T6" : (300,255)}
el_centers = np.array([el_centers_dict[item] for item in sens])
fig, ax = plt.subplots(nrows=2, ncols=2)
for band in range(4):
plt.subplot(2,2,band+1)
plt.title(bands[band])
for i in range(eeg_no_ch):
for j in range(i):
coh = coherence_master[0,7,1,i,j,band]
if coh > 0.5:
coh = (coh - 0.5)*2
col = cm.inferno(coh)
plt.plot([el_centers[i,0], el_centers[j,0]],
[el_centers[i,1], el_centers[j,1]],
color=col, alpha=coh*.99, linewidth=coh*8)
plt.imshow(eeg_1020)
ax = plt.gca()
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xticks([])
ax.set_yticks([])
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.03, 0.7])
cmap = mpl.cm.plasma
norm = mpl.colors.Normalize(vmin=0.5, vmax=1)
mpl.colorbar.ColorbarBase(cbar_ax, cmap=cmap,
norm=norm, orientation='vertical',
ticks=[tick for tick in np.arange(0.5,1.1,0.1)])
#%% average coherence
# example averaging
#for i in range(30):
# coherence_master[0,i,1,:,:,:] = coherence_master[0,7,1,:,:,:]
# coherence_master[0,i,0,:,:,:] = coherence_master[0,7,0,:,:,:]
coherence_avg = np.mean(coherence_master, axis=1)
# %% plot average coherence
for g in range(2):
for e in range(2):
plt.figure()
plt.suptitle(groups[g]+' '+eyes[e], fontsize=16)
for i in range(4):
plt.subplot(2,2,i+1)
plt.title(bands[i])
plt.imshow(coherence_avg[g,e,:,:,i], aspect='auto', \
origin='lower', \
extent=[0, eeg_no_ch, 0, eeg_no_ch],\
cmap='plasma')
plt.title(bands[i])
#%% hemispheres
hemisphere_l = [ind for ind,s in enumerate(sens) if s in 'Fp1F7F3T3C3T5P3O1']
hemisphere_lz = [ind for ind,s in enumerate(sens) if s in 'Fp1F7F3T3C3T5P3O1CzFzPz']
hemisphere_r = [ind for ind,s in enumerate(sens) if s in 'Fp2F8F2T4C4T6P4O2']
hemisphere_rz = [ind for ind,s in enumerate(sens) if s in 'Fp2F8F2T4C4T6P4O2CzFzPz']
#%% plot intra heisphere coherence
for g in range(2):
for e in range(2):
fig, ax = plt.subplots(nrows=2, ncols=2)
plt.suptitle(groups[g]+', '+eyes[e], fontsize=16)
for band in range(4):
plt.subplot(2,2,band+1)
plt.title(bands[band])
for i in hemisphere_l:
for j in hemisphere_l:
coh = coherence_avg[g,e,i,j,band]
if coh > 0.5:
coh = (coh - 0.5)*2
col = cm.inferno(coh)
plt.plot([el_centers[i,0], el_centers[j,0]],
[el_centers[i,1], el_centers[j,1]],
color=col, alpha=coh*.99, linewidth=coh*8)
for i in hemisphere_r:
for j in hemisphere_r:
coh = coherence_avg[g,e,i,j,band]
if coh > 0.5:
coh = (coh - 0.5)*2
col = cm.inferno(coh)
plt.plot([el_centers[i,0], el_centers[j,0]],
[el_centers[i,1], el_centers[j,1]],
color=col, alpha=coh*.99, linewidth=coh*8)
plt.imshow(eeg_1020)
ax = plt.gca()
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xticks([])
ax.set_yticks([])
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.03, 0.7])
cmap = mpl.cm.plasma
norm = mpl.colors.Normalize(vmin=0.5, vmax=1)
mpl.colorbar.ColorbarBase(cbar_ax, cmap=cmap,
norm=norm, orientation='vertical',
ticks=[tick for tick in np.arange(0.5,1.1,0.1)])
#%% inter heisphere coherence
for g in range(2):
for e in range(2):
fig, ax = plt.subplots(nrows=2, ncols=2)
plt.suptitle(groups[g]+', '+eyes[e], fontsize=16)
for band in range(4):
plt.subplot(2,2,band+1)
plt.title(bands[band])
for i in hemisphere_lz:
for j in hemisphere_rz:
coh = coherence_avg[g,e,i,j,band]
if coh > 0.5:
coh = (coh - 0.5)*2
col = cm.inferno(coh)
plt.plot([el_centers[i,0], el_centers[j,0]],
[el_centers[i,1], el_centers[j,1]],
color=col, alpha=coh*.99, linewidth=coh*8)
plt.imshow(eeg_1020)
ax = plt.gca()
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xticks([])
ax.set_yticks([])
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.03, 0.7])
cmap = mpl.cm.plasma
norm = mpl.colors.Normalize(vmin=0.5, vmax=1)
mpl.colorbar.ColorbarBase(cbar_ax, cmap=cmap,
norm=norm, orientation='vertical',
ticks=[tick for tick in np.arange(0.5,1.1,0.1)])