Diff of /preprocess_h5_smt.py [000000] .. [5d1c0a]

Switch to unified view

a b/preprocess_h5_smt.py
1
'''Preprocessor for KU Data.
2
'''
3
import argparse
4
from os.path import join as pjoin
5
6
import h5py
7
import numpy as np
8
from scipy.io import loadmat
9
from scipy.signal import decimate
10
from tqdm import tqdm
11
12
parser = argparse.ArgumentParser(
13
    description='Preprocessor for KU Data')
14
parser.add_argument('source', type=str, help='Path to raw KU data')
15
parser.add_argument('target', type=str, help='Path to pre-processed KU data')
16
parser.add_argument('--foldered-data', action='store_true',
17
                    help='DEPRECATED. DO NOT use this option ' +
18
                    'if the data is downloaded from GigaDB.')
19
args = parser.parse_args()
20
21
src = args.source
22
out = args.target
23
is_foldered = args.foldered_data
24
25
26
def get_data(sess, subj):
27
    if is_foldered:
28
        filename = pjoin('session' + str(sess), 's' + str(subj), 'EEG_MI.mat')
29
    else:
30
        filename = 'sess{:02d}_subj{:02d}_EEG_MI.mat'.format(sess, subj)
31
    filepath = pjoin(src, filename)
32
    raw = loadmat(filepath)
33
    # Obtain input, convert (time, epoch, chan) into (epoch, chan, time)
34
    X1 = np.moveaxis(raw['EEG_MI_train']['smt'][0][0], 0, -1)
35
    X1 = decimate(X1, 4)
36
    X2 = np.moveaxis(raw['EEG_MI_test']['smt'][0][0], 0, -1)
37
    X2 = decimate(X2, 4)
38
    X = np.concatenate((X1, X2), axis=0)
39
    # Obtain target: 0 -> right, 1 -> left
40
    Y1 = (raw['EEG_MI_train']['y_dec'][0][0][0] - 1)
41
    Y2 = (raw['EEG_MI_test']['y_dec'][0][0][0] - 1)
42
    Y = np.concatenate((Y1, Y2), axis=0)
43
    return X, Y
44
45
46
with h5py.File(pjoin(out, 'KU_mi_smt.h5'), 'w') as f:
47
    for subj in tqdm(range(1, 55)):
48
        X1, Y1 = get_data(1, subj)
49
        X2, Y2 = get_data(2, subj)
50
        X = np.concatenate((X1, X2), axis=0)
51
        X = X.astype(np.float32)
52
        Y = np.concatenate((Y1, Y2), axis=0)
53
        Y = Y.astype(np.int64)
54
        f.create_dataset('s' + str(subj) + '/X', data=X)
55
        f.create_dataset('s' + str(subj) + '/Y', data=Y)