【脑机接口社区】(2) 创建event和epochs对象

1. 前言

脑机接口社区和网上大部分博主所写的步骤和代码都是通过读取fif文件创建epoch对象,然而我并没有“具有event”的fif文件,也就是以下代码中的event_fname,所以我用的是第二个方法。


2. 读取fif文件创建Epoch对象

步骤:

  • 1)读取fif文件,构建raw对象;
  • 2)创建event对象;
  • 3)创建epoch对象;
  • 4)对epoch进行叠加平均得到evoked对象;
  • 5)绘制evoked。

代码

import mne
from mne import io
from 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'
event_id, tmin, tmax = 1, -0.2, 0.5

# 读取fif文件,创建raw对象
raw = io.read_raw_fif(raw_fname)
# 读取包含event的fif文件,创建event对象
events = mne.read_events(event_fname)

"""
 挑选通道:EEG + MEG - bad channels 
"""
raw.info['bads'] += ['MEG 2443', 'EEG 053']  # bads + 2 more
picks = mne.pick_types(raw.info, meg=True, eeg=False, stim=True, eog=True,
                       exclude='bads')

# 读取Epoch数据
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
                    picks=picks, baseline=(None, 0), preload=True,
                    reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6))
"""
对epochs数据进行求平均获取诱发响应
"""
evoked = epochs.average()

evoked.plot(time_unit='s')
plt.show()
Read a total of 4 projection items:
        PCA-v1 (1 x 102)  idle
        PCA-v2 (1 x 102)  idle
        PCA-v3 (1 x 102)  idle
        Average EEG reference (1 x 60)  idle
    Range : 6450 ... 48149 =     42.956 ...   320.665 secs
Ready.
Current compensation grade : 0
72 matching events found
Applying baseline correction (mode: mean)
Not setting metadata
Created an SSP operator (subspace dimension = 3)
4 projection items activated
Loading data for 72 events and 106 original time points ...
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on MAG : ['MEG 1711']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
17 bad epochs dropped

在这里插入图片描述


3. 无事件,创建Event和Epoch对象

主要是通过创建等距事件数组mne.make_fixed_length_events(),其中,参数:duration–持续时间,overlap–事件之间重叠时间,本项目中所用到的duration=4,overlap=2
 

(1)创建events

	#创建event对象,重叠时间overlap:2s
    # 创建等距事件,默认id为1
    events =  mne.make_fixed_length_events(raw, duration=4.,overlap=2)
    scalings = {'eeg': 'auto'}
    raw.plot(scalings=scalings)
    # 绘制事件
    fig = mne.viz.plot_events(events, sfreq=raw.info['sfreq'], first_samp=raw.first_samp )
    fig.subplots_adjust(right=0.7)  # make room for legend
    # 将事件和原始数据一起绘制
    plt.show()

 

(2)创建epochs

epochs = mne.Epochs(raw,new_events)
epochs.plot()
plt.show()


图最上面的1表示events_id,下面的则是epochs_id。
 

(3)查看分段后的数据

#原数据
print(raw.get_data().shape)
#分段后
for epoch in epochs[:3]:
    print(len(epoch))
    print(len(epoch[0]))
    print(epoch)

(8, 5218)
8
180
[[ -144.65019872  -685.67894627  -227.6085117  ... -1445.9131992
  -1523.38469579 -2072.20708348]
 [ -700.87143473  -501.35742716  -187.27423624 ... -1995.3743339
  -1903.86932901 -1643.93585733]
 [ -456.88209006  -372.8651528   -384.00986715 ... -2048.47870871
  -1663.99854513 -1792.61396261]
 ...
 [ -769.59106093  -702.97280532   -91.71081191 ... -1996.27233535
  -1679.9616664  -1546.97546035]
 [ -574.18893902  -369.95935528  -223.22617047 ... -1660.25699322
  -1806.17081158 -1719.39670269]
 [ -295.74407607  -518.31066542  -336.61639052 ... -1712.76458388
  -1929.28606826 -2012.93792372]]
8
180
[[ -707.20416935  -626.05258854  -612.95868595 ... -1481.36405094
  -1348.98813541 -1698.79764469]
 [ -458.14901968  -615.92480704  -187.62082144 ... -1430.62802359
  -1880.09933706 -1626.21939322]
 [ -526.66955332  -398.55254892  -653.84789804 ... -1420.9714332
  -1796.1278663  -2073.63830332]
 ...
 [ -374.16785548   -90.98960231  -568.29407619 ... -1606.54258082
  -1825.51584743 -1605.62674831]
 [ -255.34345128  -331.41754796  -533.958411   ... -1648.40839269
  -1646.59162023 -2106.65924718]
 [ -266.09665386   -78.22556011  -421.66559307 ... -1967.04214947
  -1982.07681744 -1555.67239849]]
8
180
[[ -437.02776043  -699.90239422  -515.13240521 ... -1759.97719281
  -1768.87514202 -1925.14613812]
 [ -664.01724096  -572.2451615   -232.91456457 ... -2027.06306311
  -1414.0014176  -1854.27375647]
 [ -253.44605431   -73.06336388  -662.15906701 ... -1913.50916466
  -1836.01087365 -1526.49732384]
 ...
 [  -66.20364908  -391.96851114    14.12264486 ... -1448.15469889
  -1935.92801432 -1539.35867838]
 [ -567.57007071  -414.43033072  -277.36804434 ... -1992.26941153
  -1866.29803702 -1592.40033194]
 [ -670.90289424  -546.58743404  -321.15170405 ... -1712.06051753
  -1547.58969234 -1725.77963374]]
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值