點擊上面"腦機接口社區"關注我們
更多技術乾貨第一時間送達
Raw對象主要用來存儲連續型數據,核心數據爲n_channels和times,也包含Info對象。
下面可以通過幾個案例來說明Raw對象和相關用法。
Raw結構查看:
# 引入python庫
import mne
from mne.datasets import sample
import matplotlib.pyplot as plt
# sample的存放地址
data_path = sample.data_path()
# 該fif文件存放地址
fname = data_path + '/MEG/sample/sample_audvis_raw.fif'
"""
如果上述給定的地址中存在該文件,則直接加載本地文件,
如果不存在則在網上下載改數據
"""
raw = mne.io.read_raw_fif(fname)
通過打印raw:
print(raw)
<Raw | sample_audvis_raw.fif, n_channels x n_times : 376 x 166800 (277.7 sec), ~3.6 MB, data not loaded>
可以看出核心數據爲n_channels和n_times
raw.info
<Info | 24 non-empty fields
acq_pars : str | 13886 items
bads : list | MEG 2443, EEG 053
ch_names : list | MEG 0113, MEG 0112, MEG 0111, MEG 0122, MEG 0123, ...
chs : list | 376 items (GRAD: 204, MAG: 102, STIM: 9, EEG: 60, EOG: 1)
comps : list | 0 items
custom_ref_applied : bool | False
description : str | 49 items
dev_head_t : Transform | 3 items
dig : Digitization | 146 items (3 Cardinal, 4 HPI, 61 EEG, 78 Extra)
events : list | 1 items
experimenter : str | 3 items
file_id : dict | 4 items
highpass : float | 0.10000000149011612 Hz
hpi_meas : list | 1 items
hpi_results : list | 1 items
lowpass : float | 172.17630004882812 Hz
meas_date : tuple | 2002-12-03 19:01:10 GMT
meas_id : dict | 4 items
nchan : int | 376
proc_history : list | 0 items
proj_id : ndarray | 1 items
proj_name : str | 4 items
projs : list | PCA-v1: off, PCA-v2: off, PCA-v3: off
sfreq : float | 600.614990234375 Hz
acq_stim : NoneType
ctf_head_t : NoneType
dev_ctf_t : NoneType
device_info : NoneType
gantry_angle : NoneType
helium_info : NoneType
hpi_subsystem : NoneType
kit_system_id : NoneType
line_freq : NoneType
subject_info : NoneType
utc_offset : NoneType
xplotter_layout : NoneType
>
上面爲row中info的信息,從中可以看出info記錄了raw中有哪些是不良通道(bads),通道名稱:ch_names,sfreq:採樣頻率等。
通常raw的數據訪問方式如下:
data, times = raw[picks, time_slice]
picks:是根據條件挑選出來的索引;
time_slice:時間切片
想要獲取raw中所有數據,以下兩種方式均可:
data,times=raw[:]
data,times=raw[:,:]
"""
案例:
獲取10-20秒內的良好的MEG數據
# 根據type來選擇 那些良好的MEG信號(良好的MEG信號,通過設置exclude="bads") channel,
結果爲 channels所對應的的索引
"""
picks = mne.pick_types(raw.info, meg=True, exclude='bads')
t_idx = raw.time_as_index([10., 20.])
data, times = raw[picks, t_idx[0]:t_idx[1]]
plt.plot(times,data.T)
plt.title("Sample channels")
"""
sfreq:採樣頻率
raw返回所選信道以及時間段內的數據和時間點,
分別賦值給data以及times(即raw對象返回的是兩個array)
"""
sfreq=raw.info['sfreq']
data,times=raw[:5,int(sfreq*1):int(sfreq*3)]
plt.plot(times,data.T)
plt.title("Sample channels")
"""
繪製各通道的功率譜密度
"""
raw.plot_psd()
plt.show()
"""
繪製SSP矢量圖
"""
raw.plot_projs_topomap()
plt.show()
"""
繪製通道頻譜圖作爲topography
"""
raw.plot_psd_topo()
plt.show()
"""
繪製電極位置
"""
raw.plot_sensors()
plt.show()
MNE 從頭創建Raw對象
在實際過程中,有時需要從頭構建數據來創建Raw對象。
方式:通過mne.io.RawArray類來手動創建Raw
注:使用mne.io.RawArray創建Raw對象時,其構造函數只接受矩陣和info對象。
數據對應的單位:
V: eeg, eog, seeg, emg, ecg, bio, ecog
T: mag
T/m: grad
M: hbo, hbr
Am: dipole
AU: misc
構建一個Raw對象時,需要準備兩種數據,一種是data數據,一種是Info數據,
data數據是一個二維數據,形狀爲(n_channels,n_times)
案例1
import mne
import numpy as np
import matplotlib.pyplot as plt
"""
生成一個大小爲5x1000的二維隨機數據
其中5代表5個通道,1000代表times
"""
data = np.random.randn(5, 1000)
"""
創建info結構,
內容包括:通道名稱和通道類型
設置採樣頻率爲:sfreq=100
"""
info = mne.create_info(
ch_names=['MEG1', 'MEG2', 'EEG1', 'EEG2', 'EOG'],
ch_types=['grad', 'grad', 'eeg', 'eeg', 'eog'],
sfreq=100
)
"""
利用mne.io.RawArray類創建Raw對象
"""
custom_raw = mne.io.RawArray(data, info)
print(custom_raw)
從上面打印的信息可以看出
raw對象中n_channels=5, n_times=1000
"""
對圖形進行縮放
對於實際的EEG / MEG數據,應使用不同的比例因子。
對通道eeg、grad,eog的數據進行2倍縮放
"""
scalings = {'eeg': 2, 'grad': 2,'eog':2}
custom_raw.plot(n_channels=5,
scalings=scalings,
title='Data from arrays',
show=True, block=True)
plt.show()
案例2
import numpy as np
import neo
import mne
import matplotlib.pyplot as plt
構建正餘弦數據模擬mag,grad信號
其中採樣頻率爲1000Hz,時間爲0到10s.
# 創建任意數據
sfreq = 1000 # 採樣頻率
times = np.arange(0, 10, 0.001) # Use 10000 samples (10s)
sin = np.sin(times * 10) # 乘以 10 縮短週期
cos = np.cos(times * 10)
sinX2 = sin * 2
cosX2 = cos * 2
# 數組大小爲 4 X 10000.
data = np.array([sin, cos, sinX2, cosX2])
# 定義 channel types and names.
ch_types = ['mag', 'mag', 'grad', 'grad']
ch_names = ['sin', 'cos', 'sinX2', 'cosX2']
創建info對象
"""
創建info對象
"""
info = mne.create_info(ch_names=ch_names,
sfreq=sfreq,
ch_types=ch_types)
利用mne.io.RawArray創建raw對象
"""
利用mne.io.RawArray創建raw對象
"""
raw = mne.io.RawArray(data, info)
"""
對圖形進行縮放
對於實際的EEG / MEG數據,應使用不同的比例因子。
對通道mag的數據進行2倍縮小,對grad的數據進行1.7倍縮小
"""
scalings = {'mag': 2, 'grad':1.7}
raw.plot(n_channels=4, scalings=scalings, title='Data from arrays',
show=True, block=True)
"""
可以採用自動縮放比例
只要設置scalings='auto'即可
"""
scalings = 'auto'
raw.plot(n_channels=4, scalings=scalings,
title='Auto-scaled Data from arrays',
show=True, block=True)
plt.show()
文章來源於網絡,不用於商業行爲,若有侵權及疑問,請後臺留言!
更多閱讀
MNE-Python專輯 | MNE-Python詳細安裝與使用(更新)
EMD算法之Hilbert-Huang Transform原理詳解和案例分析
腦機接口BCI學習交流QQ羣:903290195
微信羣請掃碼添加,Rose拉你進羣
(請務必填寫備註,eg. 姓名+單位+專業/領域/行業)
長按加羣
歡迎點個在看鼓勵一下