查看原文
其他

脑电分析系列[MNE-Python-20]| 脑电图处理和事件相关电位(ERP)

Rose 脑机接口社区 2022-04-26

今天主要介绍一下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)

不用于商业行为,转载请联系后台

若有侵权,请后台留言,管理员即时删侵!

更多阅读

EEG公开数据集汇总

学习神经科学,发展人工智能

探索双耳助听器中的人体通信

手部的运动区域也与整个身体相连

他们想把广告植入你的梦境

EEG伪影详解和过滤工具的汇总(二)

EEG伪影类型详解和过滤工具的汇总(一)

MNE中数据结构Epoch及其创建方法

投稿通道

如何让你的工作让更多人知晓和受益?

脑机接口社区就是这样一个连接学界、

企业界和爱好者的平台渠道。


区鼓励高校实验室、企业或个人在我们平台上分享优质内容。


稿件要求

稿件系个人原创作品,若已在其他平台发表,请明确标注。

稿件一经录取,便提供稿费!

投稿通道

微信扫码,备注:投稿+姓名+单位

微信交流群,请扫码上方微信

(备注:姓名+单位+专业/领域行业)

QQ交流群:913607986

你的每一次在看,我都很在意!

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存