用mne绘制fNIRS脑地形图(topomap)
載入fNIRS數(shù)據(jù):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預(yù)處理:https://mne.tools/stable/auto_tutorials/preprocessing/plot_70_fnirs_processing.html#sphx-glr-auto-tutorials-preprocessing-plot-70-fnirs-processing-py
?
1.載入數(shù)據(jù),我的數(shù)據(jù)是snirf格式的,使用
# preload=False不加載數(shù)據(jù),preload=True加載數(shù)據(jù) raw_od= mne.io.read_raw_snirf("數(shù)據(jù)路徑", preload=True)載入數(shù)據(jù)。若數(shù)據(jù)格式是NIRx記錄的,可以用mne.io.read_raw_nirx()載入數(shù)據(jù),該函數(shù)只在NIRScout上測(cè)試過(guò)可行。
注:mne的0.20.7版本還無(wú)法使用read_raw_snirf函數(shù),conda install、update命令也無(wú)法升級(jí),直接用pip install -U mne進(jìn)行安裝或升級(jí)到最新版本。
?
讀取數(shù)據(jù)過(guò)程中出現(xiàn)TypeError: iteration over a 0-d array錯(cuò)誤
原因是,sources和detectors的Labels沒(méi)有成功讀入,可能SNIRF格式的種類(lèi)太多,我的數(shù)據(jù)中沒(méi)有這部分?jǐn)?shù)據(jù)。
sources = np.array(dat.get('nirs/probe/sourceLabels')) detectors = np.array(dat.get('nirs/probe/detectorLabels'))好在所有數(shù)據(jù)channel都是固定的,手動(dòng)將Labels設(shè)置上
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] # 根據(jù)探頭數(shù)量設(shè)置 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']數(shù)據(jù)格式如果有異常,可以根據(jù)官方提供的數(shù)據(jù)排查問(wèn)題,并手動(dòng)更改數(shù)據(jù)
?
2.根據(jù)https://mne.tools/stable/auto_tutorials/preprocessing/plot_70_fnirs_processing.html#sphx-glr-auto-tutorials-preprocessing-plot-70-fnirs-processing-py提供代碼對(duì)數(shù)據(jù)進(jìn)行預(yù)處理。我已經(jīng)在MATLAB中預(yù)處理過(guò)了,直接將數(shù)據(jù)進(jìn)行替換
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()發(fā)現(xiàn)標(biāo)簽數(shù)據(jù)(raw_haemo._annotations.description)異常,手動(dòng)讀入標(biāo)簽信息,進(jìn)行替換(參考: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() # 整行讀取數(shù)據(jù)if not lines:breakpassa, b, c, d, label, e = [(i) for i in lines.split(";")] #我的數(shù)據(jù)是按“;”分隔的labels.append(label) # 添加新讀取的數(shù)據(jù)passlabels = np.array(labels) # 將數(shù)據(jù)從list類(lèi)型轉(zhuǎn)換為array類(lèi)型。passreturn labelsraw_haemo._annotations.description = get_ananotation()?
4.繪圖所需數(shù)據(jù)準(zhǔn)備
#event_id根據(jù)raw_haemo._annotations.description的格式和事件數(shù)進(jìn)行更改 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 # 所需的時(shí)間段在標(biāo)簽打下前5s到標(biāo)簽打下后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分別表示行數(shù)和列數(shù),列數(shù)應(yīng)算上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時(shí)刻的腦地形圖 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后有個(gè)‘:’ 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.設(shè)置標(biāo)題
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.官方數(shù)據(jù)及全部代碼
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.官方繪制腦地形圖
?
總結(jié)
以上是生活随笔為你收集整理的用mne绘制fNIRS脑地形图(topomap)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2022春招前端最新面试题分享(牧原股份
- 下一篇: WOW模型导出到Unity3D使用教程