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