當前位置:
首頁 >
基于python的脑电地形图显示
發布時間:2023/12/31
30
豆豆
生活随笔
收集整理的這篇文章主要介紹了
基于python的脑电地形图显示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
做了個腦地形圖,里面涉及到了一些關于matplotlib子圖繪制、colorbar設置以及自定義colormap等問題,列在這里了先
文章目錄
- 讀取.mat文件
- 拓撲圖顯示函數
- 顯示每個頻率腦地形圖
- 各個頻帶平均功率
- 前后相減平均值
- 配對檢驗并顯示
- P值
- T值
- 每一部分的圖
- 每個人各個頻帶
- 各頻帶平均
- 前后相減平均值
- 配對檢驗-p值
- 配對檢驗-t值
讀取.mat文件
import scipy.io as sio # 這里根據具體的結構體結構來分析 bp_result = sio.loadmat(r"bp_result.mat")["result"][0][0].tolist() bp_result = np.array(bp_result).reshape(11) events_name = ["EC_before" , "EO_before" , "EC_after" , "EO_after"] band_name = ["δ" , "θ" , "α" , "β" , "γ"] # result 索引層級: event 人 通道 頻帶 # 歸一化 for event in events_name:for i in range(len(bp_result[event])):bp_result[event][i] /= np.sum(bp_result[event][i] , axis = 1).reshape(32 , 1)拓撲圖顯示函數
p.s. 這里用了mne庫
根據我自己的需要選擇了一些輸入參數。
def show_topo(data , vmin = 0 , vmax = 1 , contours = 6 , cmap = 'jet'):# plt.figure(figsize = (10 , 10))# 后面的操作不接受一維變量data = data.reshape(32 , 1)# 創建信息info = mne.create_info(ch_names = ch_names , sfreq = 1000 , ch_types = 'eeg')# 創建evoke對象evoked = mne.EvokedArray(data , info)# 設置電極evoked.set_montage(biosemi_montage)# 顯示拓撲圖ax , countour = mne.viz.plot_topomap(evoked.data[: , 0] ,evoked.info , show = False , cmap = cmap , vmin = vmin , vmax = vmax , contours = contours) return ax , countour顯示每個頻率腦地形圖
# 支持中文 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用來正常顯示中文標簽 plt.rcParams['axes.unicode_minus'] = False # 用來正常顯示負號for event in events_name:fig = plt.figure(figsize = (10 , 20) , dpi = 300)for j in range(11):for i in range(5):data = bp_result[event][j][: , i]ax = plt.subplot(11 , 5 , j * 5 + i + 1 )aximage , countour = show_topo(data , vmin = 0 , vmax = 0.5)if j == 0:ax.set_title(band_name[i] , fontsize = 15)#colorbar 左 下 寬 高 l = 0.92b = 0.2w = 0.015h = 0.6#對應 l,b,w,h;設置colorbar位置;rect = [l,b,w,h] cbar_ax = fig.add_axes(rect) plt.colorbar(aximage, cax=cbar_ax)fig.suptitle(event + " 每個人各個頻帶腦地形圖" , fontsize="x-large")plt.savefig("result/" + event + "_每個人各個頻帶腦地形圖.png")各個頻帶平均功率
fig = plt.figure(figsize = (10 , 7) , dpi = 300) for j , event in enumerate(events_name):for i in range(5):data = np.average(bp_result[event])[: , i]ax = plt.subplot(4 , 5 ,j * 5 + i + 1 )aximage , countour = show_topo(data , vmin = 0 , vmax = 0.4)if j == 1:ax.set_title(band_name[i] , fontsize = 15)if i == 0:ax.set_ylabel(event , rotation = 90 , size = 'large') #colorbar 左 下 寬 高 l = 0.92 b = 0.2 w = 0.015 h = 0.6 #對應 l,b,w,h;設置colorbar位置; rect = [l,b,w,h] cbar_ax = fig.add_axes(rect) plt.colorbar(aximage, cax=cbar_ax) fig.suptitle("各頻帶平均" , fontsize="x-large") plt.savefig("result/" + "各個頻帶腦地形圖_平均.png")前后相減平均值
fig = plt.figure(figsize = (10 , 5) , dpi = 300) for j in [0 , 1]:for i in range(5):# 計算前后差值data = np.average(bp_result[events_name[j + 2]] - bp_result[events_name[j]])[: , i]ax = plt.subplot(2 , 5 ,j * 5 + i + 1 )aximage , countour = show_topo(data , vmin = -0.15, vmax = 0.15)if j == 0:ax.set_title(band_name[i] , fontsize = 15)if i == 0:ax.set_ylabel("%s"%(events_name[j + 2][:2]) , rotation = 90 , size = 'large') #colorbar 左 下 寬 高 l = 0.92 b = 0.2 w = 0.015 h = 0.6 #對應 l,b,w,h;設置colorbar位置; rect = [l,b,w,h] cbar_ax = fig.add_axes(rect) plt.colorbar(aximage, cax=cbar_ax) fig.suptitle("after - before" , fontsize="x-large") plt.savefig("result/" + "各個頻帶腦地形圖_平均差異.png")配對檢驗并顯示
P值
import matplotlib as mpl fig = plt.figure(figsize = (10 , 5) , dpi = 300) for j in [0 , 1]:d = np.zeros((11 , 32 , 5))temp = bp_result[events_name[j + 2]] - bp_result[events_name[j]]for i in range(len(bp_result[events_name[j]])):d[i , : , :] = temp[i]for i in range(5):# 計算前后差值t , p_two = stats.ttest_1samp(d[: , : , i] , 0)data = p_twoax = plt.subplot(2 , 5 ,j * 5 + i + 1 )# 自行創建colormap,第一個參數是16進制RGB顏色, 第二個參數是分成幾個顏色塊C = mpl.colors.LinearSegmentedColormap.from_list('cmap', ['#FF0000', '#FFFF00', '#00F5FF', '#FFFFFF'], 6)aximage , countour = show_topo(data , vmin = 0, vmax = 0.06 ,cmap = C)if j == 0:ax.set_title(band_name[i] , fontsize = 15)if i == 0:ax.set_ylabel("%s"%(events_name[j + 2][:2]) , rotation = 90 , size = 'large') #colorbar 左 下 寬 高 l = 0.92 b = 0.2 w = 0.015 h = 0.6 #對應 l,b,w,h;設置colorbar位置; rect = [l,b,w,h] cbar_ax = fig.add_axes(rect) plt.colorbar(aximage, cax=cbar_ax) fig.suptitle("after - before p_val" , fontsize="x-large") plt.savefig("result/" + "各個頻帶腦地形圖_平均差異_p.png")T值
fig = plt.figure(figsize = (10 , 5) , dpi = 300) for j in [0 , 1]:d = np.zeros((11 , 32 , 5))temp = bp_result[events_name[j + 2]] - bp_result[events_name[j]]for i in range(len(bp_result[events_name[j]])):d[i , : , :] = temp[i]for i in range(5):# 計算前后差值t , p_two = stats.ttest_1samp(d[: , : , i] , 0)data = tax = plt.subplot(2 , 5 ,j * 5 + i + 1 )aximage , countour = show_topo(data , vmin = -3 , vmax = 3 ,cmap = 'bwr')if j == 0:ax.set_title(band_name[i] , fontsize = 15)if i == 0:ax.set_ylabel("%s"%(events_name[j + 2][:2]) , rotation = 90 , size = 'large') #colorbar 左 下 寬 高 l = 0.92 b = 0.2 w = 0.015 h = 0.6 #對應 l,b,w,h;設置colorbar位置; rect = [l,b,w,h] cbar_ax = fig.add_axes(rect) plt.colorbar(aximage, cax=cbar_ax) fig.suptitle("after - before t_val" , fontsize="x-large") plt.savefig("result/" + "各個頻帶腦地形圖_平均差異_t.png")每一部分的圖
每個人各個頻帶
各頻帶平均
前后相減平均值
配對檢驗-p值
配對檢驗-t值
總結
以上是生活随笔為你收集整理的基于python的脑电地形图显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于如何获取复选框选中行的数据
- 下一篇: 赛元芯片使用