赞
踩
简单写下毕设里用到的预处理步骤
数据集详细信息可查看:SEED数据集
预处理分为以下6步:
需要用到的python库
import numpy as np
import matplotlib.pyplot as plt
import mne
import os, sys
from scipy.io import loadmat
from pathlib import Path
from PyEMD import EMD
SEED数据集中的Preprocessed_EEG文件夹包含了原始的EEG数据,本文将针对这些原始EEG数据进行预处理。
由于SEED数据集中的EEG数据是.mat格式储存的,因此需要转换成MNE的Raw格式
file_path = '...' #数据集地址 raw_file = loadmat(file_path) #scipy.loadmat() std1020_eeglab = mne.channels.read_custom_montage("Standard-10-20-Cap81.locs",coord_frame='head') ch_names = ['Fp1','Fpz','Fp2','AF3','AF4','F7','F5','F3','F1','Fz','F2','F4','F6' ,'F8','FT7','FC5','FC3','FC1','FCz','FC2','FC4','FC6','FT8','T7','C5' ,'C3','C1','Cz','C2','C4','C6','T8','TP7','CP5','CP3','CP1','CPz','CP2' ,'CP4','CP6','TP8','P7','P5','P3','P1','Pz','P2','P4','P6','P8','PO7' ,'PO5','PO3','POz','PO4','PO6','PO8','CB1','O1','Oz','O2','CB2'] for key in raw_file.keys(): if key == '__header__' or key == '__version__' or key == '__globals__': print(key,' ',raw_file[key]) pass else: print(key, raw_file[key].shape, raw_file[key].ndim) print(raw_file[key].shape) info = mne.create_info( ch_names = ch_names, ch_types = ['eeg' for _ in range(62)], sfreq = 200 #采样频率 ) raw = mne.io.RawArray(raw_file[key], info) #生成raw
输出:
dict_keys(['__header__', '__version__', '__globals__', 'ww_eeg1', 'ww_eeg2', 'ww_eeg3', 'ww_eeg4', 'ww_eeg5', 'ww_eeg6', 'ww_eeg7', 'ww_eeg8', 'ww_eeg9', 'ww_eeg10', 'ww_eeg11', 'ww_eeg12', 'ww_eeg13', 'ww_eeg14', 'ww_eeg15'])
__header__ b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Sun Dec 01 11:40:07 2013'
__version__ 1.0
__globals__ []
ww_eeg1 (62, 47001) 2
(62, 47001)
Creating RawArray with float64 data, n_channels=62, n_times=47001
Range : 0 ... 47000 = 0.000 ... 235.000 secs
Ready.
首先要找出坏导(电极损坏或者其他原因),然后利用临近的电极数据对坏导进行插值处理
需要对上一步中生成的Raw数据进行可视化
scalings = {'eeg': 50}
raw.load_data()
raw.plot(scalings=scalings)
plt.show(block=True)
从下图中可与看出P1明显是坏导,直接点选P1通道进行坏导标记
选中之后关闭视窗
终端输出:
`Channels marked as bad:
['P1']`
之后进行插值处理
raw_pass = raw.copy()
raw_pass.load_data()
raw_pass.set_montage(std1020_eeglab,on_missing='warn')
raw_pass.interpolate_bads()
插值完成后效果如图所示
包括0.3 ~ 50Hz带通滤波与50Hz陷波滤波(0.3 ~ 49Hz带通滤波效果应当一致)
raw_pass = raw_pass.filter(l_freq=0.3, h_freq=50,method='fir')
raw_pass = raw_pass.notch_filter(freqs=50)
raw_pass.plot_psd(fmin=0, fmax=52,sphere=(0,0,0,0.11))
raw_pass.plot(scalings=scalings)
plt.show(block=True)
采用62导全脑平均参考
raw_ref = raw_pass.copy()
raw_ref.load_data()
raw_ref.set_eeg_reference(ref_channels='average')
利用ICA独立成分分析去除眼电和肌电伪迹
ica = mne.preprocessing.ICA(n_components=48, noise_cov=None, random_state=None, method='infomax', fit_params=None, max_iter='auto', allow_ref_meg=False, verbose=True) ica.fit(raws) eog_idx_auto, eog_scores = ica.find_bads_eog(raws,ch_name=['Fp1','Fpz','Fp2','AF3','AF4','F7','F5','F3','F1','Fz','F2','F4','F6','F8','FT7','FC5','FC3','FC1','FCz','FC2','FC4','FC6','FT8']) muscle_idx_auto, scores = ica.find_bads_muscle(raws) print(f"Automatically found eog artifact ICA components: {eog_idx_auto}") print(f"Automatically found muscle artifact ICA components: {muscle_idx_auto},{scores}") ica.plot_sources(raws, show_scrollbars=True) plt.show(block=True) #time.sleep(199) raw_recons = raws.copy() raw_recons = ica.apply(raw_recons)
检测出的眼电成分与肌电成分
Automatically found eog artifact ICA components: [0, 1]
Automatically found muscle artifact ICA components: [1, 2, 4, 6, 7, 14, 15, 16, 17, 18, 19, 21, 23, 24, 25, 26, 28, 29, 30, 32, 33, 34, 35, 36, 37, 39, 40, 41, 43, 44, 45]
选中所检测出的伪迹成分
关闭视窗后完成伪迹去除
…明天再继续写
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。