-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.py
186 lines (142 loc) · 5.16 KB
/
features.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import numpy as np
from scipy.stats import entropy
from sklearn.base import BaseEstimator, TransformerMixin
import pywt
class Quantiles(BaseEstimator, TransformerMixin):
def __init__(self, quantiles=[.50]):
self.quantiles = quantiles
def get_quantiles(self, arr, quantiles):
"""
function gets quantiles from 3d array and returns 2d array
parameters
----------
arr : np.ndarray
3d array where fist dimension its timedelta records
second dimension its frequency intervals
third dimension its channelwise data
quantiles : list
python list with percentiles
f.e. [0.25, 0.50, 0.75]
returns
-------
out: ndarray
2d ndarray with columns - len(quantiles) percentiles for each channel
"""
out = np.quantile(arr, quantiles, axis=1, interpolation='midpoint')
out = out.transpose(1, 2, 0).reshape(-1, len(quantiles) * arr.shape[2])
return out
def fit(self, X, y=None, **fit_params):
return self
def fit_transform(self, X, y=None, **fit_params):
return self.get_quantiles(X, self.quantiles)
def transform(self, X, y=None):
return self.get_quantiles(X, self.quantiles)
class STD(BaseEstimator, TransformerMixin):
def __init__(self):
pass
def get_std(self, arr):
"""
function get std feature from 3d array and convert its into 2d array
Parameters
----------
arr : np.ndarray
3d array where fist dimension its timedelta records
second dimension its frequency intervals
third dimension its channelwise data
Returns
-------
out : ndarray
"""
out = np.std(arr, axis=1)
return out
def fit(self, X, y=None, **fit_params):
return self
def fit_transform(self, X, y=None, **fit_params):
return self.get_std(X)
def transform(self, X, y=None):
return self.get_std(X)
class Wavelets(BaseEstimator, TransformerMixin):
def __init__(self, last_n=None, level=None, wavelet='haar', mode='symmetric'):
self.wavelet = wavelet
self.mode = mode
self.level = level
self.last_n = last_n
def get_wavelets(self, arr):
out = pywt.wavedec(arr, wavelet=self.wavelet, level=self.level, mode=self.mode, axis=1)
if self.last_n:
out = out[:self.last_n]
for coef in out:
print(coef.shape, end=' ')
print()
out = np.concatenate(out, axis=1)
out = out.reshape(-1, out.shape[1] * out.shape[2])
return out
def fit(self, X, y=None, **fit_params):
return self
def fit_transform(self, X, y=None, **fit_params):
return self.get_wavelets(X)
def transform(self, X, y=None):
return self.get_wavelets(X)
class Entropy(BaseEstimator, TransformerMixin):
def __init__(self):
pass
def get_entropy(self, arr):
"""
function count entropy in 3d array and return 2d array
Parameters
----------
arr : np.ndarray
3d array where fist dimension its timedelta records
second dimension its frequency intervals
third dimension its channelwise data
Returns
-------
out : ndarray
"""
out = entropy(arr, axis=1)
return out
def fit(self, X, y=None, **fit_params):
return self
def fit_transform(self, X, y=None, **fit_params):
self.fit(X, y)
self.transform(X, y)
def transform(self, X, y=None):
return self.get_entropy(X)
if __name__ == '__main__':
import utils
import classifiers
import pandas as pd
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import FeatureUnion
data_folder = './data/'
file_name = 'data/emotions.csv'
SEED = 1
data = pd.read_csv(data_folder + file_name)
data['class'], class_dict = utils.encode_column(data['class'])
train, test = utils.eeg_train_test_split(data.to_numpy(), chunk_size=5 * 128, test_size=0.2, random_state=SEED)
print('Train.shape:', train.shape)
print('Test.shape:', test.shape)
X_test, Y_test = utils.create_x_y(test, dt=128, shift=64, verbose=0)
X_train, Y_train = utils.create_x_y(train, dt=128, shift=64)
print('X_test.shape:', X_test.shape)
print('Y_test.shape:', Y_test.shape)
print('X_train.shape:', X_train.shape)
print('Y_train.shape:', Y_train.shape)
print('\nTesting features:\n')
std = STD()
entrop = Entropy()
quantiles = Quantiles(quantiles=[0.5, 0.25, 0.75])
test_std = std.fit_transform(X_test)
print('test_std.shape:', test_std.shape)
test_entrop = entrop.fit_transform(X_test)
print('test_entrop.shape:', test_entrop.shape)
test_quantiles = quantiles.fit_transform(X_test)
print('test_quantiles.shape:', test_quantiles.shape)
union = FeatureUnion([
('STD', STD()),
('Entropy', Entropy()),
('Quantiles', Quantiles(quantiles=[0.5])),
])
result = union.fit_transform(X_test)
print('All in one:', result.shape)