1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| import os import mne import numpy as np
channels = ['Fp1', 'Fp2', 'F7', 'F3', 'Fz', 'F4', 'F8', 'T3', 'C3', 'Cz', 'C4', 'T4', 'T5', 'P3', 'Pz', 'P4', 'T6', 'O1', 'O2']
input_folders = ['公开数据集/AD', '公开数据集/CN'] output_folders = [ 'guding_channl/AD', 'guding_channl/CN']
def process_edf_file(file_path, output_folder): raw = mne.io.read_raw_edf(file_path, preload=True) if not all(ch in raw.ch_names for ch in channels): print(f"文件 {file_path} 不包含所有所需通道,跳过处理。") return raw.pick_channels(channels) raw.reorder_channels(channels) Delta_data = raw.copy().filter(0.5, 4, fir_design='firwin').get_data() theta_data = raw.copy().filter(4, 8, fir_design='firwin').get_data()
alpha_data = raw.copy().filter(8, 13, fir_design='firwin').get_data() Beta_data = raw.copy().filter(13, 25, fir_design='firwin').get_data()
Gamma_data = raw.copy().filter(25, 45, fir_design='firwin').get_data() stacked_data = np.stack((Delta_data, theta_data,alpha_data,Beta_data,Gamma_data),axis=0) print(stacked_data.shape) file_name = os.path.splitext(os.path.basename(file_path))[0] save_path = os.path.join(output_folder, f'{file_name}_processed.npy') np.save(save_path, stacked_data) print(f"处理并保存文件: {save_path}")
def process_set_file(file_path, output_folder): raw = mne.io.read_raw_eeglab(file_path, preload=True) raw.resample(512) if not all(ch in raw.ch_names for ch in channels): print(f"文件 {file_path} 不包含所有所需通道,跳过处理。") return raw.pick_channels(channels) raw.reorder_channels(channels) Delta_data = raw.copy().filter(0.5, 4, fir_design='firwin').get_data() theta_data = raw.copy().filter(4, 8, fir_design='firwin').get_data()
alpha_data = raw.copy().filter(8, 13, fir_design='firwin').get_data() Beta_data = raw.copy().filter(13, 25, fir_design='firwin').get_data()
Gamma_data = raw.copy().filter(25, 45, fir_design='firwin').get_data() stacked_data = np.stack((Delta_data, theta_data,alpha_data,Beta_data,Gamma_data),axis=0) print(stacked_data.shape) file_name = os.path.splitext(os.path.basename(file_path))[0] save_path = os.path.join(output_folder, f'{file_name}_processed.npy') np.save(save_path, stacked_data) print(f"处理并保存文件: {save_path}")
def process_file(file_path, output_folder): file_extension = os.path.splitext(file_path)[1] if file_extension == '.edf': process_edf_file(file_path, output_folder) elif file_extension == '.set': process_set_file(file_path, output_folder) else: print(f"文件 {file_path} 格式不支持,跳过处理。")
assert len(input_folders) == len(output_folders), "输入和输出文件夹数量不匹配"
for output_folder in output_folders: os.makedirs(output_folder, exist_ok=True)
for input_folder, output_folder in zip(input_folders, output_folders): for file_name in os.listdir(input_folder): file_path = os.path.join(input_folder, file_name) process_file(file_path, output_folder)
|