File size: 1,560 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 | import h5py
import scipy
from scipy import signal
import os
import lmdb
import pickle
import numpy as np
import pandas as pd
data_dir = '/data/datasets/BigDownstream/SEED-VIG/mat/Raw_Data'
labels_dir = '/data/datasets/BigDownstream/SEED-VIG/mat/perclos_labels'
files = [file for file in os.listdir(data_dir)]
files = sorted(files)
files_dict = {
'train': files[:15],
'val': files[15:19],
'test': files[19:23],
}
print(files_dict)
dataset = {
'train': list(),
'val': list(),
'test': list(),
}
db = lmdb.open('/data/datasets/BigDownstream/SEED-VIG/processed', map_size=6000000000)
for files_key in files_dict.keys():
for file in files_dict[files_key]:
eeg = scipy.io.loadmat(os.path.join(data_dir, file))['EEG'][0][0][0]
labels = scipy.io.loadmat(os.path.join(labels_dir, file))['perclos']
print(eeg.shape, labels.shape)
eeg = eeg.reshape(885, 8, 200, 17)
eeg = eeg.transpose(0, 3, 1, 2)
labels = labels[:, 0]
print(eeg.shape, labels.shape)
for i, (sample, label) in enumerate(zip(eeg, labels)):
sample_key = f'{file[:-4]}-{i}'
print(sample_key)
data_dict = {
'sample': sample, 'label': label
}
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() |