python与erp_Python-EEG处理和事件相关电位(ERP)
更多技術干貨第一時間送達
腦機接口社區推薦搜索
腦機接口
MNE
EEGLAB
算法
匯總
數據
有后臺留言問,代碼是在哪里運行的。這里說明一下,案例介紹的代碼均在jupyter notebook中運行的,當然這些代碼也可以在PyCharm等IDE中運行(不過可能存在再不同環境下代碼需要稍微改動的情況。)
Python腦電圖處理案例:
import mnefrom 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"# 這些EEG數據已經默認有平均參考(average reference)raw = mne.io.read_raw_fif(raw_fname, preload=True)
# 只篩選 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,可以使用以下方法更改其類型:raw.set_channel_types(mapping={"EOG 061": "eeg"})print(raw.info)
比較上面前后兩個打印中的紅框內容,可以發現,EOG已被EEG取代了。
# 更改EOG通道的名稱raw.rename_channels(mapping={"EOG 061": "EOG"})# 為了講述案例,我們將EOG頻道重設為EOG類型。raw.set_channel_types(mapping={"EOG": "eog"})"""樣本數據集中的EEG通道有對應通道位置。這些位置可在每個通道說明的"位置"中找到。下面是獲取第一個通道的位置"""print(raw.info["chs"][0]["loc"])
[-0.03737009 0.10568011 0.073338750.00235201 0.11096951 -0.03500458
0. 1. 0. 0. 0. 1. ]
"""可以使用mne.io.Raw.plot_sensors()繪制通道位置。如果您的數據沒有位置,則可以使用MNE隨附的Montages來設置"""raw.plot_sensors()raw.plot_sensors("3d") # in 3D
設置腦電圖參考
首先,從原始對象中刪除參考。
這顯示的移除了MNE默認的EEG平均參考。
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。reject = dict(eeg=180e-6, eog=150e-6)event_id, tmin, tmax = {"left/auditory": 1}, -0.2, 0.5events = 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 memorytitle = "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")
設置平均參考電極
"""平均參考:通常默認情況下添加,但也可以顯式添加。"""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 memorytitle =
"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的平均值作為參考raw_custom, _ = mne.set_eeg_reference(raw, [
"EEG 001",
"EEG 002"])evoked_custom = mne.Epochs(raw_custom, **epochs_params).average()del raw_custom
# save memorytitle =
"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對象。
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。left,
right = epochs[
"left"].average(), epochs[
"right"].average()# create and plot difference
ERPjoint_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)
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。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)
"""這可以通過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)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"]
[, , , ]
通常,在字典或列表中存儲誘發對象是有意義的——無論是不同的條件,還是不同的主題。
"""如果將它們存儲在一個列表中,就可以很容易地對它們求平均值,例如,跨主題(或條件)的總平均值。"""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)
總結
以上是生活随笔為你收集整理的python与erp_Python-EEG处理和事件相关电位(ERP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python: 测量代码运行时间
- 下一篇: MNE学习笔记(四):Evoked数据结