-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilss.py
60 lines (47 loc) · 2.42 KB
/
utilss.py
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
import gumpy
import numpy as np
from datetime import datetime
import kapre
from keras.utils import np_utils
import keras
import keras.utils as ku
def load_preprocess_data(data, debug, lowcut, highcut, w0, Q, anti_drift, class_count, cutoff, axis, fs,T):
"""Load and preprocess data.
The routine loads data with the use of gumpy's Dataset objects, and
subsequently applies some post-processing filters to improve the data.
"""
# TODO: improve documentation
data_loaded = data.load(T)
if debug:
print('Band-pass filtering the data in frequency range from %.1f Hz to %.1f Hz... '
%(lowcut, highcut))
data_notch_filtered = gumpy.signal.notch(data_loaded.raw_data, cutoff, axis)
data_hp_filtered = gumpy.signal.butter_highpass(data_notch_filtered, anti_drift, axis)
data_bp_filtered = gumpy.signal.butter_bandpass(data_hp_filtered, lowcut, highcut, axis)
# Split data into classes.
# TODO: as soon as gumpy.utils.extract_trails2 is merged with the
# regular extract_trails, change here accordingly!
class1_mat, class2_mat = gumpy.utils.extract_trials2(data_bp_filtered, data_loaded.trials,
data_loaded.labels, data_loaded.trial_total,
fs, nbClasses = 2)
# concatenate data for training and create labels
x_train = np.concatenate((class1_mat, class2_mat))
labels_c1 = np.zeros((class1_mat.shape[0], ))
labels_c2 = np.ones((class2_mat.shape[0], ))
y_train = np.concatenate((labels_c1, labels_c2))
# for categorical crossentropy
y_train = ku.np_utils.to_categorical(y_train)
print("Data loaded and processed successfully!")
return x_train, y_train
# def print_version_info():
# now = datetime.now()
# print('%s/%s/%s' % (now.year, now.month, now.day))
# print('Keras version: {}'.format(keras.__version__))
# if keras.backend._BACKEND == 'tensorflow':
# import tensorflow
# print('Keras backend: {}: {}'.format(keras.backend._backend, tensorflow.__version__))
# else:
# import theano
# print('Keras backend: {}: {}'.format(keras.backend._backend, theano.__version__))
# print('Keras image dim ordering: {}'.format(keras.backend.image_dim_ordering()))
# print('Kapre version: {}'.format(kapre.__version__))