MNE官网:https://mne.tools/stable/auto_examples/index.html
conda install mne
导入库
import mne
import matplotlib.pyplot as plt
#读入数据
raw = mne.io.read_raw_bdf('./Part1_IAPS_SES1_EEG_fNIRS_03082006.bdf')
#查看核心数据
raw.info
不同的文件里面的数据不同,几个关键文件是:
bads:不良通道
ch_names:通道名称
sfreq:采样频率
#直接获取原始数据,(原始数据,时间序列)
data,times=raw[:]
获取原始数据小例子
#获取10-20s内的EEG数据例子
#根据type来选择 那些良好的MEG信号(良好的MEG信号,通过设置exclude="bads") channel,结果为 channels所对应的的索引
picks = mne.pick_types(raw.info, eeg=True, exclude=["F5","F8","AF7","AF8","AFz","Fp1","Fp2","Fpz","F7","F6","EXG1","EXG2","EXG3","EXG4","EXG5","EXG6","EXG7","EXG8","Status"]) #可以自己设置不要的通道
#picks = mne.pick_types(raw.info, meg=True, exclude='bads')#挑出非坏道的通道,第二个参数是设置数据类型,脑电数据设为eeg,近红外数据设为fnirs
t_id = raw.time_as_index([10.,20.])#获取10到20s的时间序号
data,times = raw[picks,t_id[0]:t_id[1]] #raw[通道,时间序列]
plt.plot(times,data.T)
plt.title("Sample channels")
另一种方式
# sfreq:采样频率
# raw返回所选信道以及时间段内的数据和时间点,
# 分别赋值给data以及times(即raw对象返回的是两个array)
#选择前5个通道,1-3s的数据
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()
对于某些数据可以通过自己创建raw数据来进行EEG分析
#导入库
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)
自己数据的实例
创建数据,此处直接使用上面读取文件里的data,也可以自己创建data
#读入数据
raw = mne.io.read_raw_bdf('./Part1_IAPS_SES1_EEG_fNIRS_03082006.bdf')
picks = mne.pick_types(raw.info, eeg=True, exclude=["F5","F8","AF7","AF8","AFz","Fp1","Fp2","Fpz","F7","F6","EXG1","EXG2","EXG3","EXG4","EXG5","EXG6","EXG7","EXG8","Status"]) #可以自己设置不要的通道
data,times = raw[picks,:]
#创建info结构,
# 内容包括:通道名称和通道类型
# 设置采样频率为:sfreq=256
exclude=["F5","F8","AF7","AF8","AFz","Fp1","Fp2","Fpz","F7","F6","EXG1","EXG2","EXG3","EXG4","EXG5","EXG6","EXG7","EXG8","Status"]#设置我想要删除的通道
# for x in exclude:
# ch_names.remove(x) #移除
#设置通道类型
ch_types = ['eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg','eeg']
info = mne.create_info(
ch_names=ch_names,
ch_types=ch_types,
sfreq=256
)
整合数据,创建raw对象
#利用mne.io.RawArray类创建Raw对象
custom_raw = mne.io.RawArray(data, info)
print(custom_raw)