本教程為腦機學習者Rose發表於公眾號:腦機接口社區(微信號:Brain_Computer).QQ交流群:903290195
Epoch概念簡介
相信很多人第一次接觸epoch時,都會有疑惑,這個詞在EEG中到底指的是什么。
下面將詳細說明一下。
從連續的腦電圖信號中提取一些特定時間窗口的信號,這些時間窗口可以稱作為epochs.
由於EEG是連續收集的,要分析腦電事件相關的電位時,需要將信號"切分"成時間片段,這些時間片段被鎖定到某個事件(例如刺激)中的時間片段。
比如在EEGLAB分析中,EEGLAB將連續數據視為由一個較長的時期(long epoch)組成,而將數據切分后,它由多個較小的時期(small epoch)組成。
舉個例子
假設我們有一個長度為60s的信號x,采樣頻率為1 Hz.
腦電信號的矩陣表示為1x60矩陣,如果將信號划分成一些2s的信號,則將有30個peoch(信號中每2s就是一個epoch)
在MNE中,Epoch對象是一種把連續型數據作為時間段集合的表示方法,
形狀為(n_events,n_channels,n_times)的數組形式:
創建Epochs對象方式有三種:
(1)通過Raw對象和事件事件點(event times)
(2)通過讀取.fif文件數據生成Epoch對象
(3)通過mne.EpochsArray從頭創建Epoch對象
這里利用方式2和方式3創建Epochs對象
a. 讀取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()
b. 從頭創建Epoch對象
在實際過程中,有時需要從頭構建數據來創建Epochs對象,
方式:利用mne.EpochsArray創建Epochs對象,創建時直接構建numpy數組即可,數組的形狀必須是(n_epochs, n_chans, n_times)
數據對應的單位:
V: eeg, eog, seeg, emg, ecg, bio, ecog
T: mag
T/m: grad
M: hbo, hbr
Am: dipole
AU: misc
案例1
import mne
import numpy as np
import matplotlib.pyplot as plt
第一步:構建數據
構建一個大小為10x5x200的三維數組,數組中數據是隨機數;
第一維數據表示:10 epochs
第二維數據表示:5 channels
第三維數據表示:2 seconds per epoch
# 采樣頻率
sfreq = 100
data = np.random.randn(10, 5, sfreq * 2)
# 創建一個info結構
info = mne.create_info(
ch_names=['MEG1', 'MEG2', 'EEG1', 'EEG2', 'EOG'],
ch_types=['grad', 'grad', 'eeg', 'eeg', 'eog'],
sfreq=sfreq
)
第二步:構建events
在創建Epochs對象時,必須提供一個"events"數組,
事件(event)描述的是某一種波形(症狀)的起始點,其為一個三元組,形狀為(n_events,3):
第一列元素以整數來描述的事件起始采樣點;
第二列元素對應的是當前事件來源的刺激通道(stimulus channel)的先前值(previous value),該值大多數情況是0;
第三列元素表示的是該event的id。
events = np.array([
[0, 0, 1],
[1, 0, 2],
[2, 0, 1],
[3, 0, 2],
[4, 0, 1],
[5, 0, 2],
[6, 0, 1],
[7, 0, 2],
[8, 0, 1],
[9, 0, 2],
])
設置事件的id
如果是dict,則以后可以使用這些鍵訪問關聯的事件。示例:dict(聽覺=1,視覺=3)
如果是int,將創建一個id為string的dict。
如果是列表,則使用列表中指定ID的所有事件。
如果沒有,則所有事件都將與一起使用,並使用與事件id整數對應的字符串整數名稱創建dict。
# 創建event id,受試者或者微笑或者皺眉
event_id = dict(smiling=1, frowning=2)
"""
tmin:event開始前的時間,如果未指定,則默認為0
"""
# 設置事件開始前時間為-0.1s
tmin = -0.1
第三步:創建epochs對象
"""
利用mne.EpochsArray創建epochs對象
"""
custom_epochs = mne.EpochsArray(data, info, events, tmin, event_id)
print(custom_epochs)
# 繪制
_ = custom_epochs['smiling'].average().plot(time_unit='s')
案例2
import numpy as np
import neo
import mne
import matplotlib.pyplot as plt
"""
設置event id,用來識別events.
"""
event_id = 1
# 第一列表示樣本編號
events = np.array([[200, 0, event_id],
[1200, 0, event_id],
[2000, 0, event_id]]) # List of three arbitrary events
sfreq = 1000 # 采樣頻率
times = np.arange(0, 10, 0.001) # Use 10000 samples (10s)
sin = np.sin(times * 10) # 乘以 10 縮短周期
cos = np.cos(times * 10)
"""
利用sin和cos創建一個2個通道的700 ms epochs的數據集
只要是(n_epochs, n_channels, n_times)形狀的數據,都可以被用來創建
"""
epochs_data = np.array([[sin[:700], cos[:700]],
[sin[1000:1700], cos[1000:1700]],
[sin[1800:2500], cos[1800:2500]]])
ch_names = ['sin', 'cos']
ch_types = ['mag', 'mag']
info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)
epochs = mne.EpochsArray(epochs_data, info=info, events=events,
event_id={'arbitrary': 1})
epochs.plot(scalings='auto' )
plt.show()
本文章由腦機學習者Rose筆記分享,QQ交流群:903290195
更多分享,請關注公眾號