主要内容
- 1、数据提取
- 2、制作数据样本-数据分快
- 3、过程展示
1、数据提取
本次教程以chb01患者的数据为例:
首先提取该患者的eeg数据:
在数据提取中就完成滤波:(0~50Hz)
from mne import Epochs, pick_types, events_from_annotations
from mne.io import concatenate_raws
from mne.io import read_raw_edf
from mne.datasets import eegbci
import mne
import numpy as np
import pandas as pd
import glob
import numpy as np
import os
from scipy import signal, fft
import matplotlib.pyplot as pltpath_time = "ttt.csv" # 患者发病发病起止时间表
file_dir = "chb01"
path_save = "data"
# 选择患者共有的通道
ch = ['FP1-F7', 'F7-T7', 'T7-P7', 'P7-O1', 'FP1-F3', 'F3-C3', 'C3-P3', 'P3-O1', 'FP2-F4', 'F4-C4', 'C4-P4', 'P4-O2', 'FP2-F8', 'F8-T8', 'T8-P8-0', 'P8-O2', 'FZ-CZ', 'CZ-PZ', 'P7-T7', 'T7-FT9', 'FT9-FT10', 'FT10-T8']
time = pd.read_csv(path_time,index_col="chb")
files = sorted(os.listdir(file_dir))
for file in files:if os.path.splitext(file)[1] == '.edf':f = os.path.splitext(file)[0]f_str = str(os.path.splitext(os.path.splitext(file)[0])[0])if i == 0:raws = mne.io.read_raw_edf(file_dir+"/" + file,preload=True,verbose=False)raws.pick_channels(ch)raws.filter(0.1,50.,method='iir')raw_d,raw_t = raws[:,:]i+=1else:i+=1if f_str in time.index:time.loc[f_str]['start'] = time.loc[f_str]['start'] * 256 + len(raw_t)time.loc[f_str]['end'] = time.loc[f_str]['end']*256 + len(raw_t)raw = mne.io.read_raw_edf(file_dir+"/" + file, preload=True,verbose=False)raw.pick_channels(ch)raw.filter(0.1,50.,method='iir')raws = concatenate_raws([raws,raw])raws_d, raw_t = raws[:,:]
d, t = raws[:,:]
data = d*1e6
np.save(path_save+"/"+file_dir+".npy",data)
其中,ttt.csv文件的部分内容如下:
2、制作数据样本-数据分快
详细代码如下:
fbq = 3600*256fbjq = 3600*256*6fbjh = 3600*256*5time_window = 1024 # 每秒256个采样数据,这里的1024则是4秒的窗口的数据,可以根据实验需求调整!start = times.iloc[0]['start'] end = times.iloc[0]['end']d = np.load(path_save+"/"+file_dir)if start >= fbq : fbqd = d[:,start-fbq:start]fbqd = fbqd.transpose(1,0)fbqd = fbqd.reshape(-1,time_window,22)fbql = np.zeros(fbqd.shape[0], dtype = int)fbql[:] = 1print(fbqd.shape,fbql.shape)np.save(path_save+"/"+"/d/"+file_dir+"_q.npy",fbqd)np.save(path_save+"/"+"/l/"+file_dir+"_q.npy",fbql)if start >= fbjq:fbjd = d[:,start-fbjq:start-fbjh]fbjd = fbjd.transpose(1,0)fbjd = fbjd.reshape(-1,time_window,22)fbjl = np.zeros(fbjd.shape[0], dtype = int)print(fbjd.shape,fbjl.shape)np.save(path_save+"/"+"/d/"+file_dir+"_j.npy",fbjd)np.save(path_save+"/"+"/l/"+file_dir+"_j.npy",fbjl)
3、过程展示
数据提取的过程如下:
样本制作与数据分块过程如下:
900为样本数量,1024为4s时间窗口内的数据,22为eeg通道数量!
接下来就可以进入具体的研究内容了!!!