forked from bit-magpie/EEG-processing-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtopograph.py
More file actions
104 lines (83 loc) · 3.34 KB
/
topograph.py
File metadata and controls
104 lines (83 loc) · 3.34 KB
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
import numpy as np
import scipy.interpolate
from scipy import signal
from matplotlib import patches
import matplotlib.pyplot as plt
def get_psds(data, fs=128, f_range=[0.5, 30]):
'''
Calculate signal power using Welch method.
Input: data- mxn matrix (m: number of channels, n: samples of signals)
fs- Sampling frequency (default 128Hz)
f_range- Frequency range (default 0.5Hz to 30Hz)
Output: Power values and PSD values
'''
powers = []
psds = list()
for sig in data:
freq, psd = signal.welch(sig, fs)
idx = np.logical_and(freq >= f_range[0], freq <= f_range[1])
powers = np.append(powers, sum(psd[idx]))
psds.append(psd[idx])
return powers, psds
def plot_topomap(data, ax, fig):
'''
Plot topographic plot of EEG data. This specialy design for Emotiv 14 electrode data.
This can be change for any other arrangement by changing ch_pos (channel position array)
Input: data- 1D array 14 data values
ax- Matplotlib subplot object to be plotted every thing
fig- Matplot lib figure object to draw colormap
'''
N = 300
xy_center = [2,2]
radius = 2
# AF3, F7, F3, FC5, T7, P7, O1, O2, P8, T8, FC6, F4, F8, AF4
ch_pos = [[1,4],[0.1,3], [1.5,3.5], [0.5,2.5],
[-0.1,2], [0.4,0.4], [1.5,0], [2.5,0],
[3.6,0.4], [4.1,2], [3.5,2.5], [2.5,3.5],
[3.9,3], [3,4]]
x,y = [],[]
for i in ch_pos:
x.append(i[0])
y.append(i[1])
xi = np.linspace(-2, 6, N)
yi = np.linspace(-2, 6, N)
zi = scipy.interpolate.griddata((x, y), data, (xi[None,:], yi[:,None]), method='cubic')
dr = xi[1] - xi[0]
for i in range(N):
for j in range(N):
r = np.sqrt((xi[i] - xy_center[0])**2 + (yi[j] - xy_center[1])**2)
if (r - dr/2) > radius:
zi[j,i] = "nan"
#fig, ax = plt.subplots()
dist = ax.contourf(xi, yi, zi, 60, cmap = plt.get_cmap('coolwarm'), zorder = 1)
ax.contour(xi, yi, zi, 15, linewidths = 0.5,colors = "grey", zorder = 2)
cbar = fig.colorbar(dist, ax=ax, format='%.1e')
cbar.ax.tick_params(labelsize=8)
ax.scatter(x, y, marker = 'o', c = 'b', s = 15, zorder = 3)
circle = patches.Circle(xy = xy_center, radius = radius, edgecolor = "k", facecolor = "none", zorder=4)
ax.add_patch(circle)
for loc, spine in ax.spines.items():
spine.set_linewidth(0)
ax.set_xticks([])
ax.set_yticks([])
circle = patches.Ellipse(xy = [0,2], width = 0.4, height = 1.0, angle = 0, edgecolor = "k", facecolor = "w", zorder = 0)
ax.add_patch(circle)
circle = patches.Ellipse(xy = [4,2], width = 0.4, height = 1.0, angle = 0, edgecolor = "k", facecolor = "w", zorder = 0)
ax.add_patch(circle)
xy = [[1.6,3.6], [2,4.3],[2.4,3.6]]
polygon = patches.Polygon(xy = xy, edgecolor = "k", facecolor = "w", zorder = 0)
ax.add_patch(polygon)
ax.set_xlim(-0.5, 4.5)
ax.set_ylim(-0.5, 4.5)
return ax
if __name__ == "__main__":
import mne
import matplotlib.pyplot as plt
data = mne.io.read_raw_edf('1.edf')
raw_data = data.get_data()
ch_data = raw_data[2:16,:]
pwrs, _ = get_psds(ch_data)
fig, ax = plt.subplots(figsize=(10,8))
plot_topomap(pwrs, ax, fig)
plt.show()
fig.savefig("topograph.png", bbox_inches='tight')