Python-脑电图处理和事件相关电位(ERP)

更多技术干货第一时间送达

Hello,大家好!

Rose小哥今天主要介绍一下MNE-Python中进行脑电图处理和事件相关电位(ERP)。

有后台留言问,代码是在哪里运行的。这里说明一下,案例介绍的代码均在jupyter notebook中运行的,当然这些代码也可以在PyCharm等IDE中运行(不过可能存在再不同环境下代码需要稍微改动的情况。)

Python脑电图处理案例:

import mnefrom mne.datasets import sample
# 加载数据文件data_path = sample.data_path()raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'# 这些EEG数据已经默认有平均参考(average reference)raw = mne.io.read_raw_fif(raw_fname, preload=True)
# 只筛选 EEG 和 EOG数据raw.pick_types(meg=False, eeg=True, eog=True)
# 查看 raw 信息, 可以发现有59个EEG通道和1个EOG通道print(raw.info)

实际上,拥有一些EOG通道的EEG通道是很普遍的。在实际获取EEG数据时,会受到眼动等活动的影响。

如果要更改通道类型,可以使用mne.io.Raw.set_channel_types()方法。
例如,要将EOG通道视为EEG,可以使用以下方法更改其类型:

raw.set_channel_types(mapping={'EOG 061': 'eeg'})print(raw.info)

比较上面前后两个打印中的红框内容,可以发现,EOG已被EEG取代了。

# 更改EOG通道的名称raw.rename_channels(mapping={'EOG 061': 'EOG'})
# 为了讲述案例,我们将EOG频道重设为EOG类型。raw.set_channel_types(mapping={'EOG': 'eog'})
"""样本数据集中的EEG通道有对应通道位置。这些位置可在每个通道说明的"位置"中找到。
下面是获取第一个通道的位置"""
print(raw.info['chs'][0]['loc'])

[-0.03737009 0.10568011 0.07333875 0.00235201 0.11096951 -0.03500458
0. 1. 0. 0. 0. 1. ]

"""可以使用mne.io.Raw.plot_sensors()绘制通道位置。如果您的数据没有位置,则可以使用MNE随附的Montages来设置"""raw.plot_sensors()
raw.plot_sensors('3d') # in 3D

设置脑电图参考

首先,从原始对象中删除参考。

这显示的移除了MNE默认的EEG平均参考。

raw_no_ref, _ = mne.set_eeg_reference(raw, [])

EEG channel type selected for re-referencing

EEG data marked as already having the desired reference. Preventing automatic future re-referencing to an average reference.

Removing existing average EEG reference projection.

然后,定义了Epochs,并计算了左听觉状态的ERP。

reject = dict(eeg=180e-6, eog=150e-6)event_id, tmin, tmax = {'left/auditory': 1}, -0.2, 0.5events = mne.read_events(event_fname)epochs_params = dict(events=events, event_id=event_id, tmin=tmin, tmax=tmax, reject=reject)
evoked_no_ref = mne.Epochs(raw_no_ref, **epochs_params).average()del raw_no_ref # save memory
title = 'EEG Original reference'evoked_no_ref.plot(titles=dict(eeg=title), time_unit='s')evoked_no_ref.plot_topomap(times=[0.1], size=3., title=title, time_unit='s')

设置平均参考电极

"""平均参考:通常默认情况下添加,但也可以显式添加。"""
raw.del_proj()raw_car, _ = mne.set_eeg_reference(raw, 'average', projection=True)evoked_car = mne.Epochs(raw_car, **epochs_params).average()del raw_car # save memory
title = 'EEG Average reference'evoked_car.plot(titles=dict(eeg=title), time_unit='s')evoked_car.plot_topomap(times=[0.1], size=3., title=title, time_unit='s')

自定义参考:使用通道EEG 001和EEG 002的平均值作为参考

raw_custom, _ = mne.set_eeg_reference(raw, ['EEG 001', 'EEG 002'])evoked_custom = mne.Epochs(raw_custom, **epochs_params).average()del raw_custom # save memory
title = 'EEG Custom reference'evoked_custom.plot(titles=dict(eeg=title), time_unit='s')evoked_custom.plot_topomap(times=[0.1], size=3., title=title, time_unit='s')

可以使用' / '分隔的'标记'来选择Epochs中的试验子集。

首先,我们创建一个包含4个条件的Epochs对象。

event_id = {'left/auditory': 1, 'right/auditory': 2, 'left/visual': 3, 'right/visual': 4}epochs_params = dict(events=events, event_id=event_id, tmin=tmin, tmax=tmax, reject=reject)epochs = mne.Epochs(raw, **epochs_params)
print(epochs)

接下来,我们创建左刺激和右刺激试验的平均值。

我们可以使用基本的操作,例如,构建和绘制不同的ERP。

left, right = epochs["left"].average(), epochs["right"].average()
# create and plot difference ERPjoint_kwargs = dict(ts_args=dict(time_unit='s'), topomap_args=dict(time_unit='s'))mne.combine_evoked([left, -right], weights='equal').plot_joint(**joint_kwargs)

Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EEG : ['EEG 001']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']

这是权重相等的差异。 如果试验编号不平衡,则也可以考虑使每个条件的事件数相等(使用epochs.equalize_event_counts)。 例如,首先,我们为每种情况创建单独的ERP。

aud_l = epochs["auditory", "left"].average()aud_r = epochs["auditory", "right"].average()vis_l = epochs["visual", "left"].average()vis_r = epochs["visual", "right"].average()
all_evokeds = [aud_l, aud_r, vis_l, vis_r]print(all_evokeds)
"""这可以通过Python列表理解来简化"""
all_evokeds = [epochs[cond].average() for cond in sorted(event_id.keys())]print(all_evokeds)
# 然后,我们也以这种方式构造和绘制左右试验的未加权平均值:mne.combine_evoked( [aud_l, -aud_r, vis_l, -vis_r], weights='equal').plot_joint(**joint_kwargs)

Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EEG : ['EEG 001']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
Rejecting epoch based on EOG : ['EOG']
[<Evoked | 'left/auditory' (average, N=56), [-0.1998, 0.49949] sec, 59 ch, ~3.1 MB>, <Evoked | 'left/visual' (average, N=67), [-0.1998, 0.49949] sec, 59 ch, ~3.1 MB>, <Evoked | 'right/auditory' (average, N=62), [-0.1998, 0.49949] sec, 59 ch, ~3.1 MB>, <Evoked | 'right/visual' (average, N=56), [-0.1998, 0.49949] sec, 59 ch, ~3.1 MB>]

通常,在字典或列表中存储诱发对象是有意义的——无论是不同的条件,还是不同的主题。

"""如果将它们存储在一个列表中,就可以很容易地对它们求平均值,例如,跨主题(或条件)的总平均值。"""grand_average = mne.grand_average(all_evokeds)mne.write_evokeds('tmp\\tmp-ave.fif', all_evokeds)
# 如果“诱发对象”对象存储在词典中,则可以按名称检索它们。all_evokeds = dict((cond, epochs[cond].average()) for cond in event_id)print(all_evokeds['left/auditory'])
# 除了显式访问外,还可以用于设置标题。for cond in all_evokeds: all_evokeds[cond].plot_joint(title=cond, **joint_kwargs)

更多阅读

小脑的功能解剖

眼动追踪与VR

DEAP数据集--一个重要的情绪脑电研究数据集(更新)

EEG数据、伪影的查看与清洗

脑电数据收集,处理和分析的基础

什么是EEG以及如何解释EEG?

EEG vs MRI vs fMRI vs fNIRS简介

大脑区域与功能对应

北京脑中心罗敏敏教授:解析奖赏神经环路 | 视频课程

Python-Evoked地形图可视化

Python-可视化Evoked数据

关于事件相关电位SSVEP应用于视频游戏的研究

关于事件相关电位P300应用于视频游戏的研究

Python-生成模拟原始脑电数据

头皮脑电图源空间的计算建模为癫痫的术前评估提供依据

测试干式EEG传感器的有效性--使用游戏

Python-使用多种滤波器对脑电数据去除伪影

长文阅读 | 人机融合智能:人工智能3.0

脑机接口基础之神经科学(更新)

BCI比赛数据集简介-BCI competition IV 2b

EEG信号特征提取算法

EEMD算法原理与实现

EMD算法原理与实现
EEGNet: 神经网络应用于脑电信号

信号处理之倒频谱原理与python实现

信号处理之功率谱原理与python实现

脑电信号滤波-代码实现

应用深度学习EEGNet来处理脑电信号

(0)

相关推荐