File size: 3,260 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 | import numpy as np
import scipy
from scipy import signal
import os
import lmdb
import pickle
from scipy.signal import butter, lfilter, resample, filtfilt
def butter_bandpass(low_cut, high_cut, fs, order=5):
nyq = 0.5 * fs
low = low_cut / nyq
high = high_cut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
root_dir = '/data/datasets/BCICIV2a/data_mat'
files = [file for file in os.listdir(root_dir)]
files = sorted(files)
# files.remove('A04E.mat')
# files.remove('A04T.mat')
# files.remove('A06E.mat')
# files.remove('A06T.mat')
print(files)
files_dict = {
'train': ['A01E.mat', 'A01T.mat', 'A02E.mat', 'A02T.mat', 'A03E.mat', 'A03T.mat',
'A04E.mat', 'A04T.mat',
'A05E.mat', 'A05T.mat'],
'val': [
'A06E.mat', 'A06T.mat',
'A07E.mat', 'A07T.mat'
],
'test': ['A08E.mat', 'A08T.mat', 'A09E.mat', 'A09T.mat'],
}
dataset = {
'train': list(),
'val': list(),
'test': list(),
}
# for file in files:
# if 'E' in file:
# files_dict['train'].append(file)
# else:
# files_dict['test'].append(file)
#
# print(files_dict)
db = lmdb.open('/data/datasets/BCICIV2a/processed_inde_avg_filter', map_size=1610612736)
for files_key in files_dict.keys():
for file in files_dict[files_key]:
print(file)
data = scipy.io.loadmat(os.path.join(root_dir, file))
num = len(data['data'][0])
# print(num)
# print(data['data'][0, 8][0, 0][0].shape)
# print(data['data'][0, 8][0, 0][1].shape)
# print(data['data'][0, 8][0, 0][2].shape)
for j in range(3, num):
raw_data = data['data'][0, j][0, 0][0][:, :22]
events = data['data'][0, j][0, 0][1][:, 0]
labels = data['data'][0, j][0, 0][2][:, 0]
length = raw_data.shape[0]
events = events.tolist()
events.append(length)
# print(events)
annos = []
for i in range(len(events) - 1):
annos.append((events[i], events[i + 1]))
for i, (anno, label) in enumerate(zip(annos, labels)):
sample = raw_data[anno[0]:anno[1]].transpose(1, 0)
sample = sample - np.mean(sample, axis=0, keepdims=True)
# print(samples.shape)
b, a = butter_bandpass(0.3, 40, 250)
sample = lfilter(b, a, sample, -1)
# print(sample.shape)
sample = sample[:, 2 * 250:6 * 250]
sample = resample(sample, 800, axis=-1)
# print(sample.shape)
# print(i, sample.shape, label)
sample = sample.reshape(22, 4, 200)
sample_key = f'{file[:-4]}-{j}-{i}'
print(sample_key, label-1)
data_dict = {
'sample': sample, 'label': label - 1
}
# print(label-1)
txn = db.begin(write=True)
txn.put(key=sample_key.encode(), value=pickle.dumps(data_dict))
txn.commit()
dataset[files_key].append(sample_key)
txn = db.begin(write=True)
txn.put(key='__keys__'.encode(), value=pickle.dumps(dataset))
txn.commit()
db.close()
|