File size: 3,342 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 | import h5py
import scipy
from scipy import signal
import os
import lmdb
import pickle
import numpy as np
import pandas as pd
train_dir = '/data/datasets/BigDownstream/Imagined speech/mat/Training set'
val_dir = '/data/datasets/BigDownstream/Imagined speech/mat/Validation set'
test_dir = '/data/datasets/BigDownstream/Imagined speech/mat/Test set'
files_dict = {
'train':sorted([file for file in os.listdir(train_dir)]),
'val':sorted([file for file in os.listdir(val_dir)]),
'test':sorted([file for file in os.listdir(test_dir)]),
}
print(files_dict)
dataset = {
'train': list(),
'val': list(),
'test': list(),
}
db = lmdb.open('/data/datasets/BigDownstream/Imagined speech/processed', map_size=3000000000)
for file in files_dict['train']:
data = scipy.io.loadmat(os.path.join(train_dir, file))
print(data['epo_train'][0][0][0])
eeg = data['epo_train'][0][0][4].transpose(2, 1, 0)
labels = data['epo_train'][0][0][5].transpose(1, 0)
eeg = eeg[:, :, -768:]
labels = np.argmax(labels, axis=1)
eeg = signal.resample(eeg, 600, axis=2).reshape(300, 64, 3, 200)
print(eeg.shape, labels.shape)
for i, (sample, label) in enumerate(zip(eeg, labels)):
sample_key = f'train-{file[:-4]}-{i}'
data_dict = {
'sample': sample, 'label': label,
}
txn = db.begin(write=True)
txn.put(key=sample_key.encode(), value=pickle.dumps(data_dict))
txn.commit()
print(sample_key)
dataset['train'].append(sample_key)
for file in files_dict['val']:
data = scipy.io.loadmat(os.path.join(val_dir, file))
eeg = data['epo_validation'][0][0][4].transpose(2, 1, 0)
labels = data['epo_validation'][0][0][5].transpose(1, 0)
eeg = eeg[:, :, -768:]
labels = np.argmax(labels, axis=1)
eeg = signal.resample(eeg, 600, axis=2).reshape(50, 64, 3, 200)
print(eeg.shape, labels.shape)
for i, (sample, label) in enumerate(zip(eeg, labels)):
sample_key = f'val-{file[:-4]}-{i}'
data_dict = {
'sample': sample, 'label': label,
}
txn = db.begin(write=True)
txn.put(key=sample_key.encode(), value=pickle.dumps(data_dict))
txn.commit()
print(sample_key)
dataset['val'].append(sample_key)
df = pd.read_excel("/data/datasets/BigDownstream/Imagined speech/mat/Track3_Answer Sheet_Test.xlsx")
df_=df.head(53)
all_labels=df_.values
print(all_labels.shape)
all_labels = all_labels[2:, 1:][:, 1:30:2].transpose(1, 0)
print(all_labels.shape)
print(all_labels)
for j, file in enumerate(files_dict['test']):
data = h5py.File(os.path.join(test_dir, file))
eeg = data['epo_test']['x'][:]
labels = all_labels[j]
eeg = eeg[:, :, -768:]
eeg = signal.resample(eeg, 600, axis=2).reshape(50, 64, 3, 200)
print(eeg.shape, labels.shape)
for i, (sample, label) in enumerate(zip(eeg, labels)):
sample_key = f'test-{file[:-4]}-{i}'
data_dict = {
'sample': sample, 'label': label-1,
}
txn = db.begin(write=True)
txn.put(key=sample_key.encode(), value=pickle.dumps(data_dict))
txn.commit()
print(sample_key)
dataset['test'].append(sample_key)
txn = db.begin(write=True)
txn.put(key='__keys__'.encode(), value=pickle.dumps(dataset))
txn.commit()
db.close() |