|
a |
|
b/visualization/topography.py |
|
|
1 |
# plot EEG topograpy with mne |
|
|
2 |
# https://mne.tools/stable/index.html |
|
|
3 |
|
|
|
4 |
import mne |
|
|
5 |
import numpy as np |
|
|
6 |
import scipy.io |
|
|
7 |
import matplotlib.pyplot as plt |
|
|
8 |
from matplotlib import mlab as mlab |
|
|
9 |
# from preprocess import import_data |
|
|
10 |
|
|
|
11 |
data = 0 |
|
|
12 |
label = 0 |
|
|
13 |
# get the data and label |
|
|
14 |
# data - (samples, channels, trials) |
|
|
15 |
# label - (label, 1) |
|
|
16 |
|
|
|
17 |
data = np.transpose(data, (2, 1, 0)) |
|
|
18 |
label = np.squeeze(np.transpose(label)) |
|
|
19 |
idx = np.where(label == 1) |
|
|
20 |
data_draw = data[idx] |
|
|
21 |
|
|
|
22 |
mean_trial = np.mean(data_draw, axis=0) # mean trial |
|
|
23 |
# use standardization or normalization to adjust |
|
|
24 |
mean_trial = (mean_trial - np.mean(mean_trial)) / np.std(mean_trial) |
|
|
25 |
|
|
|
26 |
|
|
|
27 |
mean_ch = np.mean(mean_trial, axis=1) # mean samples with channel dimension left |
|
|
28 |
|
|
|
29 |
|
|
|
30 |
# Draw topography |
|
|
31 |
biosemi_montage = mne.channels.make_standard_montage('biosemi64') # set a montage, see mne document |
|
|
32 |
index = [37, 9, 10, 46, 45, 44, 13, 12, 11, 47, 48, 49, 50, 17, 18, 31, 55, 54, 19, 30, 56, 29] # correspond channel |
|
|
33 |
biosemi_montage.ch_names = [biosemi_montage.ch_names[i] for i in index] |
|
|
34 |
biosemi_montage.dig = [biosemi_montage.dig[i+3] for i in index] |
|
|
35 |
info = mne.create_info(ch_names=biosemi_montage.ch_names, sfreq=250., ch_types='eeg') # sample rate |
|
|
36 |
|
|
|
37 |
evoked1 = mne.EvokedArray(mean_trial, info) |
|
|
38 |
evoked1.set_montage(biosemi_montage) |
|
|
39 |
plt.figure(1) |
|
|
40 |
# im, cn = mne.viz.plot_topomap(np.mean(mean_trial, axis=1), evoked1.info, show=False) |
|
|
41 |
im, cn = mne.viz.plot_topomap(mean_ch, evoked1.info, show=False) |
|
|
42 |
plt.colorbar(im) |
|
|
43 |
|
|
|
44 |
plt.savefig('./topo/test.png') |
|
|
45 |
print('the end') |
|
|
46 |
|