File size: 3,777 Bytes
a0fd507 | 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 | import os
import random
import mne
import numpy as np
from tqdm import tqdm
import pickle
import lmdb
selected_channels = {
'01_tcp_ar': [
'EEG FP1-REF', 'EEG FP2-REF', 'EEG F3-REF', 'EEG F4-REF', 'EEG C3-REF', 'EEG C4-REF', 'EEG P3-REF',
'EEG P4-REF', 'EEG O1-REF', 'EEG O2-REF', 'EEG F7-REF', 'EEG F8-REF', 'EEG T3-REF', 'EEG T4-REF',
'EEG T5-REF', 'EEG T6-REF', 'EEG FZ-REF', 'EEG CZ-REF', 'EEG PZ-REF'
],
'02_tcp_le': [
'EEG FP1-LE', 'EEG FP2-LE', 'EEG F3-LE', 'EEG F4-LE', 'EEG C3-LE', 'EEG C4-LE', 'EEG P3-LE',
'EEG P4-LE', 'EEG O1-LE', 'EEG O2-LE', 'EEG F7-LE', 'EEG F8-LE', 'EEG T3-LE', 'EEG T4-LE',
'EEG T5-LE', 'EEG T6-LE', 'EEG FZ-LE', 'EEG CZ-LE', 'EEG PZ-LE'
],
'03_tcp_ar_a': [
'EEG FP1-REF', 'EEG FP2-REF', 'EEG F3-REF', 'EEG F4-REF', 'EEG C3-REF', 'EEG C4-REF', 'EEG P3-REF',
'EEG P4-REF', 'EEG O1-REF', 'EEG O2-REF', 'EEG F7-REF', 'EEG F8-REF', 'EEG T3-REF', 'EEG T4-REF',
'EEG T5-REF', 'EEG T6-REF', 'EEG FZ-REF', 'EEG CZ-REF', 'EEG PZ-REF'
]
}
def setup_seed(seed):
np.random.seed(seed)
random.seed(seed)
#遍历文件夹
def iter_files(rootDir):
#遍历根目录
file_path_list = []
for root,dirs,files in os.walk(rootDir):
for file in files:
file_name = os.path.join(root,file)
# print(file_name)
file_path_list.append(file_name)
return file_path_list
def preprocessing_recording(file_path, file_key_list: list, db: lmdb.open):
raw = mne.io.read_raw_edf(file_path, preload=True)
if '02_tcp_le' in file_path:
for ch in selected_channels['02_tcp_le']:
if ch not in raw.info['ch_names']:
return
raw.pick_channels(selected_channels['02_tcp_le'], ordered=True)
elif '01_tcp_ar' in file_path:
for ch in selected_channels['01_tcp_ar']:
if ch not in raw.info['ch_names']:
return
raw.pick_channels(selected_channels['01_tcp_ar'], ordered=True)
elif '03_tcp_ar_a' in file_path:
for ch in selected_channels['03_tcp_ar_a']:
if ch not in raw.info['ch_names']:
return
raw.pick_channels(selected_channels['03_tcp_ar_a'], ordered=True)
else:
return
# print(raw.info)
raw.resample(200)
raw.filter(l_freq=0.3, h_freq=75)
raw.notch_filter((60))
eeg_array = raw.to_data_frame().values
# print(raw.info)
eeg_array = eeg_array[:, 1:]
points, chs = eeg_array.shape
if points < 300 * 200:
return
a = points % (30 * 200)
eeg_array = eeg_array[60 * 200:-(a+60 * 200), :]
# print(eeg_array.shape)
eeg_array = eeg_array.reshape(-1, 30, 200, chs)
eeg_array = eeg_array.transpose(0, 3, 1, 2)
print(eeg_array.shape)
file_name = file_path.split('/')[-1][:-4]
for i, sample in enumerate(eeg_array):
# print(i, sample.shape)
if np.max(np.abs(sample)) < 100:
sample_key = f'{file_name}_{i}'
print(sample_key)
file_key_list.append(sample_key)
txn = db.begin(write=True)
txn.put(key=sample_key.encode(), value=pickle.dumps(sample))
txn.commit()
if __name__ == '__main__':
setup_seed(1)
file_path_list = iter_files('path...')
file_path_list = sorted(file_path_list)
random.shuffle(file_path_list)
# print(file_path_list)
db = lmdb.open(r'path...', map_size=1649267441664)
file_key_list = []
for file_path in tqdm(file_path_list):
preprocessing_recording(file_path, file_key_list, db)
txn = db.begin(write=True)
txn.put(key='__keys__'.encode(), value=pickle.dumps(file_key_list))
txn.commit()
db.close()
|