mne使用教程(三)

03 epoching_and_evoked_responses

env:

  • matplotlib 3.4.3
  • pathlib 1.0.1
  • mne 0.23.0
  • mne-bids 0.8

Creating epochs and generating evoked responses(ERP/ERF)

import matplotlib
import pathlib

import mne
import mne_bids

import warnings
warnings.filterwarnings("ignore")

matplotlib.use("Qt5Agg")
bids_root = pathlib.Path('out_data/sample_BIDS')

bids_path = mne_bids.BIDSPath(subject='01',
                             session='01',
                             task='audiovisual',
                             run='01',
                             datatype='meg',
                             root=bids_root)

raw = mne_bids.read_raw_bids(bids_path)
raw.load_data()
raw.filter(l_freq=0.1, h_freq=40)
events, event_id = mne.events_from_annotations(raw)
event_id

{‘Auditory/Left’: 1,
‘Auditory/Right’: 2,
‘Button’: 3,
‘Smiley’: 4,
‘Visual/Left’: 5,
‘Visual/Right’: 6}

tmin = -0.3
tmax = 0.5
baseline = (None, 0)

epochs = mne.Epochs(raw,
                   events=events,
                   event_id=event_id,
                   tmin=tmin,
                   tmax=tmax,
                   baseline=baseline,
                   preload=True)
epochs

Number of events 320
Events Auditory/Left: 72
Auditory/Right: 73
Button: 16
Smiley: 15
Visual/Left: 73
Visual/Right: 71
Time range -0.300 – 0.499 sec
Baseline -0.300 – 0.000 sec

epochs.plot();
epochs

Number of events 320
Events Auditory/Left: 72
Auditory/Right: 73
Button: 16
Smiley: 15
Visual/Left: 73
Visual/Right: 71
Time range -0.300 – 0.499 sec
Baseline -0.300 – 0.000 sec

Selecting epochs based on experimental conditions

epochs['Auditory/Right']

Number of events 73
Events Auditory/Right: 73
Time range -0.300 – 0.499 sec
Baseline -0.300 – 0.000 sec

epochs['Auditory']

Number of events 145
Events Auditory/Left: 72
Auditory/Right: 73
Time range -0.300 – 0.499 sec
Baseline -0.300 – 0.000 sec

epochs['Left']

Number of events 145
Events Auditory/Left: 72
Visual/Left: 73
Time range -0.300 – 0.499 sec
Baseline -0.300 – 0.000 sec

epochs['Visual'].plot_image();
epochs['Right'].copy().pick_types(meg=False, eeg=True).plot_image();
epochs['Right'].plot_image(picks=['eeg']);

Saving epochs

epochs.save(pathlib.Path('out_data') / 'epochs_epo.fif',
           overwrite=True)

Creating evoked data

evoked_auditory = epochs['Auditory'].average()
evoked_visual = epochs['Visual'].average()
evoked_auditory

<Evoked | ‘0.50 × Auditory/Left + 0.50 × Auditory/Right’ (average, N=145), -0.29969 – 0.49949 sec, baseline -0.299693 – 0 sec, 366 ch, ~4.6 MB>

evoked_auditory.plot(spatial_colors=True);
evoked_auditory.plot_topomap(ch_type='mag', times=[0, 0.050, 0.100, 0.150, 0.200]);
evoked_auditory.plot_joint(picks='mag');
mne.viz.plot_compare_evokeds([evoked_auditory, evoked_visual], picks='mag');
evoked_visual_Left = epochs['Visual/Left'].average()
evoked_visual_Right = epochs['Visual/Right'].average()
mne.viz.plot_compare_evokeds([evoked_visual_Left, evoked_visual_Right], picks='eeg');

Saving evoked data

mne.write_evokeds(fname=pathlib.Path('out_data') / 'evokeds_ave.fif',
                 evoked=[evoked_auditory, evoked_visual])

Reading evoked data

evokeds = mne.read_evokeds(fname=pathlib.Path('out_data') / 'evokeds_ave.fif')
evokeds

[<Evoked | ‘0.50 × Auditory/Left + 0.50 × Auditory/Right’ (average, N=145), -0.29969 – 0.49949 sec, baseline -0.299693 – 0 sec, 366 ch, ~4.6 MB>,
<Evoked | ‘0.51 × Visual/Left + 0.49 × Visual/Right’ (average, N=144), -0.29969 – 0.49949 sec, baseline -0.299693 – 0 sec, 366 ch, ~4.6 MB>]

evokeds[-1]

<Evoked | ‘0.51 × Visual/Left + 0.49 × Visual/Right’ (average, N=144), -0.29969 – 0.49949 sec, baseline -0.299693 – 0 sec, 366 ch, ~4.6 MB>

evoked = mne.read_evokeds(fname=pathlib.Path('out_data') / 'evokeds_ave.fif',
                         condition='0.51 × Visual/Left + 0.49 × Visual/Right')
evoked

<Evoked | ‘0.51 × Visual/Left + 0.49 × Visual/Right’ (average, N=144), -0.29969 – 0.49949 sec, baseline -0.299693 – 0 sec, 366 ch, ~4.6 MB>

摘自B站:https://www.bilibili.com/video/BV1zK411g7WM/?spm_id_from=333.788.recommend_more_video.-1

2021/8/22

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值