用mne绘制fNIRS脑地形图(topomap)
載入fNIRS數據:https://mne.tools/stable/auto_tutorials/io/plot_30_reading_fnirs_data.html#sphx-glr-auto-tutorials-io-plot-30-reading-fnirs-data-py
fNIRS預處理:https://mne.tools/stable/auto_tutorials/preprocessing/plot_70_fnirs_processing.html#sphx-glr-auto-tutorials-preprocessing-plot-70-fnirs-processing-py
?
1.載入數據,我的數據是snirf格式的,使用
# preload=False不加載數據,preload=True加載數據 raw_od= mne.io.read_raw_snirf("數據路徑", preload=True)載入數據。若數據格式是NIRx記錄的,可以用mne.io.read_raw_nirx()載入數據,該函數只在NIRScout上測試過可行。
注:mne的0.20.7版本還無法使用read_raw_snirf函數,conda install、update命令也無法升級,直接用pip install -U mne進行安裝或升級到最新版本。
?
讀取數據過程中出現TypeError: iteration over a 0-d array錯誤
原因是,sources和detectors的Labels沒有成功讀入,可能SNIRF格式的種類太多,我的數據中沒有這部分數據。
sources = np.array(dat.get('nirs/probe/sourceLabels')) detectors = np.array(dat.get('nirs/probe/detectorLabels'))好在所有數據channel都是固定的,手動將Labels設置上
read_raw_snirf()# sources = np.array(dat.get('nirs/probe/sourceLabels')) # detectors = np.array(dat.get('nirs/probe/detectorLabels')) # sources = [s.decode('UTF-8') for s in sources] # detectors = [d.decode('UTF-8') for d in detectors] # 根據探頭數量設置 sources = ['S1','S2','S3','S4','S5','S6','S7','S8','S9','S10','S11','S12','S13','S14','S15','S16'] detectors = ['D1','D2','D3','D4','D5','D6','D7','D8','D9','D10','D11','D12','D13','D14','D15','D16']數據格式如果有異常,可以根據官方提供的數據排查問題,并手動更改數據
?
2.根據https://mne.tools/stable/auto_tutorials/preprocessing/plot_70_fnirs_processing.html#sphx-glr-auto-tutorials-preprocessing-plot-70-fnirs-processing-py提供代碼對數據進行預處理。我已經在MATLAB中預處理過了,直接將數據進行替換
from scipy.io import loadmatraw_haemo = mne.preprocessing.nirs.beer_lambert_law(raw_od) nirs_HbO = loadmat("HbO路徑") nirs_HbR = loadmat("HbR路徑") raw_haemo._data[list(range(0,96,2)), :] = nirs_HbO['oxyData'].transpose() raw_haemo._data[list(range(1,96,2)), :] = nirs_HbR['dxyData'].transpose()發現標簽數據(raw_haemo._annotations.description)異常,手動讀入標簽信息,進行替換(參考:https://www.cnblogs.com/hackpig/p/8215786.html)
import numpy as npdef get_ananotation ():filename = 'trigger信息文件路徑' labels = []with open(filename, 'r') as file_to_read:while True:lines = file_to_read.readline() # 整行讀取數據if not lines:breakpassa, b, c, d, label, e = [(i) for i in lines.split(";")] #我的數據是按“;”分隔的labels.append(label) # 添加新讀取的數據passlabels = np.array(labels) # 將數據從list類型轉換為array類型。passreturn labelsraw_haemo._annotations.description = get_ananotation()?
4.繪圖所需數據準備
#event_id根據raw_haemo._annotations.description的格式和事件數進行更改 events, _ = mne.events_from_annotations(raw_haemo, event_id={'1': 1,'2': 2,'3': 3,'4': 4}) event_dict = {'hand': 1, 'wrist': 2, 'shoulder': 3, 'rest': 4} tmin, tmax = -5, 30 # 所需的時間段在標簽打下前5s到標簽打下后30sepochs = mne.Epochs(raw_haemo, events, event_id=event_dict,tmin=tmin, tmax=tmax,reject_by_annotation=True,proj=True, baseline=(None, 0), preload=True,detrend=None, verbose=True)# nrows和ncols分別表示行數和列數,列數應算上colorbar的那一列,gridspec_kw最后的0.1為colorbar的比重 fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(9, 5),gridspec_kw=dict(width_ratios=[1, 1, 1, 1, 0.1])) vmin, vmax, ts = -8, 8, 9.0 # vmin, vmax表示colorbar的范圍,ts表示繪制的是9.0s時刻的腦地形圖 topomap_args = dict(extrapolate='local')evoked_hand = epochs['hand'].average() evoked_wrist = epochs['wrist'].average() evoked_shoulder = epochs['shoulder'].average() evoked_rest = epochs['rest'].average()?
5.繪制腦地形圖
evoked_hand.plot_topomap(ch_type='hbo', times=ts, axes=axes[0, 0],vmin=vmin, vmax=vmax, colorbar=False,**topomap_args) evoked_hand.plot_topomap(ch_type='hbr', times=ts, axes=axes[1, 0],vmin=vmin, vmax=vmax, colorbar=False,**topomap_args) evoked_wrist.plot_topomap(ch_type='hbo', times=ts, axes=axes[0, 1],vmin=vmin, vmax=vmax, colorbar=False,**topomap_args) evoked_wrist.plot_topomap(ch_type='hbr', times=ts, axes=axes[1, 1],vmin=vmin, vmax=vmax, colorbar=False,**topomap_args) evoked_shoulder.plot_topomap(ch_type='hbo', times=ts, axes=axes[0, 2],vmin=vmin, vmax=vmax, colorbar=False,**topomap_args) evoked_shoulder.plot_topomap(ch_type='hbr', times=ts, axes=axes[1, 2],vmin=vmin, vmax=vmax, colorbar=False,**topomap_args) # 最后一列的兩張圖,colorbar=True,axes后有個‘:’ evoked_rest.plot_topomap(ch_type='hbo', times=ts, axes=axes[0, 3:],vmin=vmin, vmax=vmax, colorbar=True,**topomap_args) evoked_rest.plot_topomap(ch_type='hbr', times=ts, axes=axes[1, 3:],vmin=vmin, vmax=vmax, colorbar=True,**topomap_args)?
6.設置標題
for column, condition in enumerate(['hand', 'wrist', 'shoulder', 'rest']):for row, chroma in enumerate(['HbO', 'HbR']):axes[row, column].set_title('{}: {}'.format(chroma, condition)) fig.tight_layout()?
7.官方數據及全部代碼
import os import numpy as np import matplotlib.pyplot as plt from itertools import compress import mnefnirs_data_folder = mne.datasets.fnirs_motor.data_path() fnirs_cw_amplitude_dir = os.path.join(fnirs_data_folder, 'Participant-1') raw_intensity = mne.io.read_raw_nirx(fnirs_cw_amplitude_dir, verbose=True) raw_intensity.load_data()picks = mne.pick_types(raw_intensity.info, meg=False, fnirs=True) dists = mne.preprocessing.nirs.source_detector_distances(raw_intensity.info, picks=picks) raw_intensity.pick(picks[dists > 0.01])raw_od = mne.preprocessing.nirs.optical_density(raw_intensity)sci = mne.preprocessing.nirs.scalp_coupling_index(raw_od)raw_od.info['bads'] = list(compress(raw_od.ch_names, sci < 0.5))raw_haemo = mne.preprocessing.nirs.beer_lambert_law(raw_od)events, _ = mne.events_from_annotations(raw_haemo, event_id={'1.0': 1,'2.0': 2,'3.0': 3}) event_dict = {'Control': 1, 'Tapping/Left': 2, 'Tapping/Right': 3}reject_criteria = dict(hbo=80e-6) tmin, tmax = -5, 15epochs = mne.Epochs(raw_haemo, events, event_id=event_dict,tmin=tmin, tmax=tmax,reject=reject_criteria, reject_by_annotation=True,proj=True, baseline=(None, 0), preload=True,detrend=None, verbose=True)fig, axes = plt.subplots(nrows=2, ncols=4, figsize=(9, 5),gridspec_kw=dict(width_ratios=[1, 1, 1, 0.1])) vmin, vmax, ts = -8, 8, 9.0evoked_left = epochs['Tapping/Left'].average() evoked_right = epochs['Tapping/Right'].average()evoked_left.plot_topomap(ch_type='hbo', times=ts, axes=axes[0, 0],vmin=vmin, vmax=vmax, colorbar=False,**topomap_args) evoked_left.plot_topomap(ch_type='hbr', times=ts, axes=axes[1, 0],vmin=vmin, vmax=vmax, colorbar=False,**topomap_args) evoked_right.plot_topomap(ch_type='hbo', times=ts, axes=axes[0, 1],vmin=vmin, vmax=vmax, colorbar=False,**topomap_args) evoked_right.plot_topomap(ch_type='hbr', times=ts, axes=axes[1, 1],vmin=vmin, vmax=vmax, colorbar=False,**topomap_args)evoked_diff = mne.combine_evoked([evoked_left, evoked_right], weights=[1, -1])evoked_diff.plot_topomap(ch_type='hbo', times=ts, axes=axes[0, 2:],vmin=vmin, vmax=vmax, colorbar=True,**topomap_args) evoked_diff.plot_topomap(ch_type='hbr', times=ts, axes=axes[1, 2:],vmin=vmin, vmax=vmax, colorbar=True,**topomap_args)for column, condition in enumerate(['Tapping Left', 'Tapping Right', 'Left-Right']):for row, chroma in enumerate(['HbO', 'HbR']):axes[row, column].set_title('{}: {}'.format(chroma, condition)) fig.tight_layout()?
8.官方繪制腦地形圖
?
總結
以上是生活随笔為你收集整理的用mne绘制fNIRS脑地形图(topomap)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022春招前端最新面试题分享(牧原股份
- 下一篇: chrome浏览器广告屏蔽插件adblo