有后台留言问,代码是在哪里运行的。这里说明一下,案例介绍的代码均在jupyter notebook中运行的,当然这些代码也可以在PyCharm等IDE中运行(不过可能存在再不同环境下代码需要稍微改动的情况。)
Python脑电图处理案例:
代码语言:javascript复制import mne
from mne.datasets import sample
代码语言:javascript复制# 加载数据文件
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)
代码语言:javascript复制# 只筛选 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,可以使用以下方法更改其类型:
代码语言:javascript复制raw.set_channel_types(mapping={'EOG 061': 'eeg'})
print(raw.info)
比较上面前后两个打印中的红框内容,可以发现,EOG已被EEG取代了。
代码语言:javascript复制# 更改EOG通道的名称
raw.rename_channels(mapping={'EOG 061': 'EOG'})
# 为了讲述案例,我们将EOG频道重设为EOG类型。
raw.set_channel_types(mapping={'EOG': 'eog'})
代码语言:javascript复制"""
样本数据集中的EEG通道有对应通道位置。这些位置可在每个通道说明的"位置"中找到。
下面是获取第一个通道的位置
"""
print(raw.info['chs'][0]['loc'])
代码语言:javascript复制[-0.03737009 0.10568011 0.07333875
代码语言:javascript复制 0.00235201 0.11096951 -0.03500458
0. 1. 0. 0. 0. 1. ]
代码语言:javascript复制"""
可以使用mne.io.Raw.plot_sensors()绘制通道位置。
如果您的数据没有位置,则可以使用MNE随附的Montages来设置
"""
raw.plot_sensors()
代码语言:javascript复制raw.plot_sensors('3d') # in 3D
设置脑电图参考
首先,从原始对象中删除参考。
这显示的移除了MNE默认的EEG平均参考。
代码语言:javascript复制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。
代码语言:javascript复制reject = dict(eeg=180e-6, eog=150e-6)
event_id, tmin, tmax = {'left/auditory': 1}, -0.2, 0.5
events = 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')
设置平均参考电极
代码语言:javascript复制"""
平均参考:通常默认情况下添加,但也可以显式添加。
"""
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的平均值作为参考
代码语言:javascript复制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对象。
代码语言:javascript复制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。
代码语言:javascript复制left, right = epochs["left"].average(), epochs["right"].average()
# create and plot difference ERP
joint_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)
代码语言:javascript复制 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。
代码语言:javascript复制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)
代码语言:javascript复制"""
这可以通过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)
代码语言:javascript复制 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>]
通常,在字典或列表中存储诱发对象是有意义的——无论是不同的条件,还是不同的主题。
代码语言:javascript复制"""
如果将它们存储在一个列表中,就可以很容易地对它们求平均值,例如,跨主题(或条件)的总平均值。
"""
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)
文章来源于网络,仅用于学术交流,不用于商业行为,
若有侵权及疑问,请后台留言,管理员即时删侵!