mne官方提供绘制地形图的例子:https://mne.tools/stable/auto_examples/visualization/evoked_topomap.html#sphx-glr-auto-examples-visualization-evoked-topomap-py
跟着教程绘制即可,下面是我绘制地形图的代码
def plot_topomap(evoke):
'''
:param evoke: evoked data
:return:
'''
# 设置想要绘制的地形图的指定时间
times = np.arange(0.1, 0.5, 0.02)
# ncols是指每一行显示的个数,contours是指地形图的等高线数
evoke.plot_topomap(times, ncols=4, nrows='auto', contours=9, ch_type='eeg')
这边补充一个python小知识:np.linspace和np.arange有什么区别
https://stackoverflow.com/questions/62106028/what-is-the-difference-between-np-linspace-and-np-arange
在绘制完之后的图片是这样的:
感觉与官网上的图有点差别,一是大脑太小了,二是电极在地形图中的位置也不匹配。一开始我通过设置参数sphere来调整中间大脑的直径,有效果但毕竟是自己设置的,这个直径的值把握不好。
参考:https://mne.tools/stable/auto_tutorials/intro/40_sensor_locations.html
Channel positions in 2D space are obtained by projecting their actual 3D positions onto a sphere, then projecting the sphere onto a plane. By default, a sphere with origin at (0, 0, 0) (x, y, z coordinates) and radius of 0.095 meters (9.5 cm) is used. You can use a different sphere radius by passing a single value as the sphere argument in any function that plots channels in 2D (like plot that we use here, but also for example mne.viz.plot_topomap):
对于电极在地形图中位置不匹配问题,我采用重新定位电极方法
# 采用EEG内置脑电图电极standard_1020,不过还有其他很多标准,具体看上面的链接
montage = mne.channels.make_standard_montage('standard_1020')
raw.set_montage(montage, on_missing='ignore')
这里有一个问题,就是电极通道名字不匹配问题,我原先的电极通道中PZ、OZ电极等的Z是大写的,然后"standard_1020"电极的名字中z是小写的,所以会报错,找了很久的方法,终于解决了
具体看:https://mne.discourse.group/t/channel-positions-not-present-in-digmontage/4119/2
简而言之就是自己换一下通道的名字
new_chan_names = ['Fp1', 'Fpz', 'Fp2', 'AF3', 'AF4', 'F7', 'F5', 'F3', 'F1', 'Fz', 'F2',
'F4', 'F6', 'F8', 'FT7', 'FC5',
'FC3', 'FC1', 'FCz', 'FC2', 'FC4', 'FC6', 'FT8', 'T7', 'C5', 'C3', 'C1',
'Cz', 'C2', 'C4', 'C6', 'T8',
'M1', 'TP7', 'CP5', 'CP3', 'CP1', 'CPz', 'CP2', 'CP4', 'CP6', 'TP8', 'M2', 'P7', 'P5', 'P3', 'P1', 'Pz',
'P2', 'P4', 'P6', 'P8', 'PO7', 'PO5', 'PO3', 'POz', 'PO4', 'PO6', 'PO8', 'O9', 'O1', 'Oz', 'O2', 'O10']
def read_plot(file, name):
raw = mne.io.read_raw_cnt(file, preload=True)
# 去掉坏的电极
raw.info['bads'].append('M1')
raw.info['bads'].append('M2')
old_chan_names = raw.ch_names
chan_names_dict = {old_chan_names[i]: new_chan_names[i] for i in range(64)}
raw.rename_channels(chan_names_dict)
montage = mne.channels.make_standard_montage('standard_1020')
raw.set_montage(montage, on_missing='ignore')
raw.plot_sensors(show_names=True)
print(raw.ch_names)
然后就解决了,神奇的是上面第一个大脑太小的问题也自动解决了。
附图: