-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeatures.py
480 lines (435 loc) · 26.2 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import numpy as np
import concurrent.futures
from DSPAnalyzer import Signal_Analyzer
from KDEstimator import KernelDensityEstimator
def foo(bar):
print('hello {}'.format(bar))
return bar, bar
class Features_Extraction(object):
def __init__(self, data, data_grid):
self.__data__ = data
self.__datagrid__ = data_grid
self.__rawprocessings__ = []
self.__features__ = []
@staticmethod
def extract(data, signals):
# Create two threads as follows
try:
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
futures = []
extraction_results = []
try:
#futures.append(executor.submit(Features_Extraction.extract_correlation_features, data, signals))
#futures.append(executor.submit(Features_Extraction.extract_fft_features, data, signals))
futures.append(executor.submit(Features_Extraction.extract_statistical_features_from_signals, data, signals))
#futures.append(executor.submit(Features_Extraction.extract_eigen_features, data, signals))
except:
print('Error: unable to start threads')
i = 0
while futures.__len__() > 0:
f = futures[i]
try:
fresult = f.result(timeout=30)
if f.done() and f.cancelled():
futures.pop(i)
elif f.done() and not f.cancelled() and not f.running():
extraction_results.extend(fresult)
futures.pop(i)
elif f.done() and not f.running():
futures.pop(i)
if futures.__len__() != 0:
i = (i + 1) % futures.__len__()
except:
if f.done() and f.cancelled():
futures.pop(i)
elif f.done() and not f.cancelled() and not f.running():
extraction_results.extend(fresult)
futures.pop(i)
elif f.done() and not f.running():
futures.pop(i)
if futures.__len__() != 0:
i = (i + 1) % futures.__len__()
return extraction_results
except:
print('Error: unable to extract features')
return extraction_results
@staticmethod
def extract_fft_features(signal_data, signals, poly_deg=10):
preprocessings = []
features = []
raw_features = []
for data, signal in zip(signal_data, signals):
try:
data_grid = np.linspace(np.min(data), np.max(data), data.__len__())
sp, freq, freq_sp, peaks, recostructed_data = Signal_Analyzer.frequency_peaks_analysis(data=data,
data_grid=data_grid,
signal=signal)
poly_sp = Signal_Analyzer.polyfit(data=[sp.real], poly_deg=poly_deg)[0]
poly_data = Signal_Analyzer.polyfit(data=[data], poly_deg=poly_deg)[0]
except:
print('Error: unable to compute fft for signal %s' %signal)
continue
fft_preprocessing = {'Signal': signal,
'Data': data,
'PolyData': poly_sp,
'FFT': sp,
'PolyFFT': poly_sp,
'FFT_map': freq_sp,
'FFT_peaks_map': np.max(peaks.values()),
'FFT_npeaks': peaks.__len__(),
'Method': 'Peaks Recostruction',
'Recostructed_Signal': recostructed_data}
preprocessing = {}
preprocessing['Type'] = 'PP5'
preprocessing['Sub_To'] = signal
preprocessing['Data'] = fft_preprocessing
preprocessings.append(preprocessing)
info_fft_features = {'MaxFFTModulePeak': np.max(list(peaks.values())),
'MaxFFTModulePeakFreq': freq[np.argmax(list(peaks.values()))],
'MinFFTModulePeak': np.min(list(peaks.values())),
'MinFFTModulePeakFreq': freq[np.argmin(list(peaks.values()))],
'MaxFreq': np.max(freq),
'MinFreq': np.min(freq),
'FFTNPeaks': peaks.__len__(),
}
raw_fft_features = list(info_fft_features.values())
raw_fft_features.extend(list(poly_data))
raw_fft_features.extend(list(poly_sp))
info_fft_features['Poly_Data'] = list(poly_data)
info_fft_features['Poly_FFT'] = list(poly_sp)
feature = {}
feature['Type'] = 'FP5'
feature['Sub_To'] = signal
raw_feature = dict(feature)
feature['Data'] = info_fft_features
features.append(feature)
raw_feature['Data'] = raw_fft_features
raw_features.append(raw_feature)
return preprocessings, features, raw_features
@staticmethod
def extract_eigen_features(data, signals):
try:
feature = {}
raw_feature = {}
sig_analysis = Signal_Analyzer.correlation(data=data, signals=signals)
eigvals_kendall, eigvects_kendall = np.linalg.eig(sig_analysis.correlation_kendalls_matrix())
eigvals_pearson, eigvects_pearson = np.linalg.eig(sig_analysis.correlation_pearsons_matrix())
eigvals_spearman, eigvects_spearman = np.linalg.eig(sig_analysis.correlation_spearmans_matrix())
eigen_preprocessing = {'EigenValues_kendall': eigvals_kendall,
'EigenVectors_kendall': eigvects_kendall,
'EigenValues_Pearson': eigvals_pearson,
'EigenVectors_Pearson': eigvects_pearson,
'EigenValues_Spearman': eigvals_spearman,
'EigenVectors_Spearman': eigvects_spearman,
'Binary_Covariance_Matrixes': sig_analysis.covariance_matrixes(),
'Spearman_Matrix': sig_analysis.correlation_spearmans_matrix(),
'Kendall_Matrix': sig_analysis.correlation_kendalls_matrix(),
'Pearson_Matrix': sig_analysis.correlation_pearsons_matrix()}
preprocessing = {}
preprocessing['Type'] = 'PP4'
preprocessing['Sub_To'] = str([str(signals[i]) + ' ' for i in range(signals.__len__())])
preprocessing['Data'] = eigen_preprocessing
info_eigen_features = {'EigenValues_kendall': eigvals_kendall,
'EigenValues_Pearson': eigvals_pearson,
'EigenValues_Spearman': eigvals_spearman
}
raw_eigen_features = list(np.concatenate([np.array(eigvals_kendall).ravel(), np.array(eigvals_pearson).ravel(), np.array(eigvals_spearman).ravel()]))
feature = {}
feature['Type'] = 'FP4'
feature['Sub_To'] = str([str(signals[i]) + ' ' for i in range(signals.__len__())])
feature['Data'] = info_eigen_features
raw_feature = dict(feature)
raw_feature['Data'] = raw_eigen_features
return [preprocessing], [feature], [raw_feature]
except:
return [{}], [{}], [{}]
@staticmethod
def extract_correlation_features(data, signals):
preprocessings = []
features = []
raw_features = []
sig_analysis = Signal_Analyzer.correlation(data=data, signals=signals)
multiple_correlation_preprocessing = []
multiple_correlation_features = []
for icouple, spearman, pvalue, pearson, correlation_coeff, correlation in zip(sig_analysis.couples(), \
sig_analysis.spearmans(), \
sig_analysis.pvalues(), \
sig_analysis.pearsons(), \
sig_analysis.correlation_coeff(), \
sig_analysis.correlation()):
signalAindex = icouple[0]
signalBindex = icouple[1]
single_correlation_preprocessing = {'SignaL1': signals[signalAindex],
'Signal2': signals[signalBindex],
signals[signalAindex]: data[signalAindex],
signals[signalBindex]: data[signalBindex],
'Mean' + signals[signalAindex]: np.mean(data[signalAindex]),
'Stdv' + signals[signalAindex]: np.std(data[signalAindex]),
'Mean' + signals[signalBindex]: np.mean(data[signalBindex]),
'Stdv' + signals[signalBindex]: np.std(data[signalBindex]),
'Spearman': spearman,
'Pearson': pearson,
'PValue': pvalue,
'Correlation_Coefficients': correlation_coeff,
'Correlation': correlation}
single_preprocessing = {}
single_preprocessing['Type'] = 'PP2'
single_preprocessing['Sub_To'] = str(signals[signalAindex]) + ' ' + str(signals[signalBindex])
single_preprocessing['Data'] = single_correlation_preprocessing
preprocessings.append(single_preprocessing)
info_corr_features = { 'Spearman': spearman,
'Pearson': pearson,
'PValue': pvalue,
'Mean' + signals[signalAindex]: np.mean(data[signalAindex]),
'Stdv' + signals[signalAindex]: np.std(data[signalAindex]),
'Mean' + signals[signalBindex]: np.mean(data[signalBindex]),
'Stdv' + signals[signalBindex]: np.std(data[signalBindex])
}
single_feature = {}
single_feature['Type'] = 'FP2'
single_feature['Sub_To'] = str(signals[signalAindex]) + ' ' + str(signals[signalBindex])
single_raw_feature = dict(single_feature)
single_raw_corr_features = list(np.reshape(correlation_coeff, correlation_coeff.__len__() * correlation_coeff.__len__()))
single_raw_corr_features.extend(np.reshape(correlation, correlation.__len__() * correlation.__len__()))
single_raw_corr_features.extend(info_corr_features.values())
info_corr_features['Correlation_Coefficients'] = list(np.reshape(correlation_coeff, newshape=(1, -1)))
info_corr_features['Correlation'] = list(np.reshape(correlation, newshape=(1, -1)))
single_feature['Data'] = info_corr_features
single_raw_feature['Data'] = single_raw_corr_features
features.append(single_feature)
raw_features.append(single_raw_feature)
binary_tuple_index = sig_analysis.couples()
binary_covariance_matrixes = sig_analysis.covariance_matrixes()
spearman_matrix = sig_analysis.correlation_spearmans_matrix()
kendall_matrix = sig_analysis.correlation_kendalls_matrix()
pearson_matrix = sig_analysis.correlation_pearsons_matrix()
correlation_preprocessing = {'Binary_Covariancion_Matrixes': binary_covariance_matrixes,
'Binary_Tuple_Index': binary_tuple_index,
'Single_Correlations': multiple_correlation_preprocessing,
'Spearman_Matrix': spearman_matrix,
'Kendall_Matrix': kendall_matrix,
'Pearson_Matrix': pearson_matrix}
preprocessing = {}
preprocessing['Type'] = 'PP3'
preprocessing['Sub_To'] = str([str(signals[i]) + ' ' for i in range(signals.__len__())])
preprocessing['Data'] = correlation_preprocessing
preprocessings.append(preprocessing)
info_corr_features = {}
raveled_matrix = np.matrix.ravel(np.array(spearman_matrix))
one_filtered_matrix = raveled_matrix[[int(v) != 1 for v in raveled_matrix]]
duplicate_filtered_matrix = set(one_filtered_matrix)
raveled_matrix = list(duplicate_filtered_matrix)
info_corr_features['SpearmanCorr'] = raveled_matrix
raveled_matrix = np.matrix.ravel(np.array(kendall_matrix))
one_filtered_matrix = raveled_matrix[[int(v) != 1 for v in raveled_matrix]]
duplicate_filtered_matrix = set(one_filtered_matrix)
raveled_matrix = list(duplicate_filtered_matrix)
info_corr_features['KendallCorr'] = raveled_matrix
raveled_matrix = np.matrix.ravel(np.array(pearson_matrix))
one_filtered_matrix = raveled_matrix[[int(v) != 1 for v in raveled_matrix]]
duplicate_filtered_matrix = set(one_filtered_matrix)
raveled_matrix = list(duplicate_filtered_matrix)
info_corr_features['PearsonCorr'] = raveled_matrix
feature = {}
feature['Type'] = 'FP3'
feature['Sub_To'] = str([str(signals[i]) + ' ' for i in range(signals.__len__())])
feature['Data'] = info_corr_features
features.append(feature)
raw_feature = dict(feature)
raw_corr_features = list()
for value in info_corr_features.values():
raw_corr_features.extend(value)
raw_feature['Data'] = raw_corr_features
raw_features.append(raw_feature)
return preprocessings, features, raw_features
@staticmethod
def extract_statistical_features(data, signal, poly_deg=3):
data_grid = np.linspace(np.min(data), np.max(data), data.__len__())
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
futures = list([object() for i in range(4)])
extraction_results = list([{} for i in range(4)])
scikit_stat_preprocessing = {}
statu_stat_preprocessing = {}
statm_stat_preprocessing = {}
scipy_stat_preprocessing = {}
try:
futures[0] = executor.submit(KernelDensityEstimator.kde_explore_scipy, data, data_grid)
futures[1] = executor.submit(KernelDensityEstimator.kde_explore_statsmodels_u, data, data_grid)
futures[2] = executor.submit(KernelDensityEstimator.kde_explore_statsmodels_m, data, data_grid)
futures[3] = executor.submit(KernelDensityEstimator.kde_explore_scikitlearn, data, data_grid)
except:
print('Error: unable to start threads')
wait_futures = list(futures)
i = 0
while wait_futures.__len__() > 0:
f = wait_futures[i]
try:
fresult = f.result(timeout=20)
if f.done() and f.cancelled():
wait_futures.pop(i)
elif f.done() and not f.cancelled() and not f.running():
extraction_results[futures.index(f)] = fresult
wait_futures.pop(i)
elif f.done() and not f.running():
wait_futures.pop(i)
if wait_futures.__len__() != 0:
i = (i + 1) % wait_futures.__len__()
except:
if f.done() and f.cancelled():
wait_futures.pop(i)
elif f.done() and not f.cancelled() and not f.running():
extraction_results[futures.index(f)] = fresult
wait_futures.pop(i)
elif f.done() and not f.running():
wait_futures.pop(i)
if wait_futures.__len__() != 0:
i = (i + 1) % wait_futures.__len__()
whole_pdf_polycoeffs = []
whole_cdf_polycoeffs = []
try:
if extraction_results[0].__len__() > 0:
pdfs, bws, params = extraction_results[0][0], extraction_results[0][1], \
extraction_results[0][2]
try:
pdf_polycoeffs = Signal_Analyzer.polyfit(data=pdfs, poly_deg=poly_deg)
whole_pdf_polycoeffs.extend(pdf_polycoeffs)
except:
pdf_polycoeffs = [np.zeros(poly_deg + 1)]
scipy_stat_preprocessing = {'Method': 'SciPy', 'PDFPolyfits': pdf_polycoeffs, 'PolyfitDeg': poly_deg, 'PDFs': pdfs,
'Bandwidths': bws, 'Parameters': params}
if extraction_results[1].__len__() > 0:
pdfs, cdfs, bws, cumhazards, sfs, params = extraction_results[1][0], \
extraction_results[1][1], \
extraction_results[1][2], \
extraction_results[1][3], \
extraction_results[1][4], \
extraction_results[1][5]
try:
pdf_polycoeffs = Signal_Analyzer.polyfit(data=pdfs, poly_deg=poly_deg)
whole_pdf_polycoeffs.extend(pdf_polycoeffs)
except:
pdf_polycoeffs = [np.zeros(poly_deg + 1)]
try:
cdf_polycoeffs = Signal_Analyzer.polyfit(data=list(cdfs), poly_deg=poly_deg)
whole_cdf_polycoeffs.extend(cdf_polycoeffs)
except:
cdf_polycoeffs = [np.zeros(poly_deg + 1)]
statu_stat_preprocessing = {'Method': 'StatsmodelU',
'PDFPolyfits': pdf_polycoeffs,
'CDFPolyfits': cdf_polycoeffs,
'PolyfitDeg': poly_deg,
'PDFs': pdfs,
'CDFs': cdfs,
'Cumulative-Hazards': cumhazards,
'SFs': sfs,
'Bandwidths': bws,
'Parameters': params}
if extraction_results[2].__len__() > 0:
pdfs, cdfs, bws, loo_likelyhoods, params = extraction_results[2][0], \
extraction_results[2][1], \
extraction_results[2][2], \
extraction_results[2][3], \
extraction_results[2][4]
try:
pdf_polycoeffs = Signal_Analyzer.polyfit(data=pdfs, poly_deg=poly_deg)
whole_pdf_polycoeffs.extend(pdf_polycoeffs)
except:
pdf_polycoeffs = [np.zeros(poly_deg + 1)]
try:
cdf_polycoeffs = Signal_Analyzer.polyfit(data=cdfs, poly_deg=poly_deg)
whole_cdf_polycoeffs.extend(cdf_polycoeffs)
except:
cdf_polycoeffs = [np.zeros(poly_deg + 1)]
statm_stat_preprocessing = {'Method': 'StatsmodelM',
'PDFPolyfits': pdf_polycoeffs,
'CDFPolyfits': cdf_polycoeffs,
'PolyfitDeg': poly_deg,
'PDFs': pdfs,
'CDFs': cdfs,
'LeaveOneOut-Likelyhoods': loo_likelyhoods,
'Bandwidths': bws,
'Parameters': params}
if extraction_results[3].__len__() > 0:
pdfs, bws, log_likelyhoods, params = extraction_results[3][0], \
extraction_results[3][1], \
extraction_results[3][2], \
extraction_results[3][3]
try:
pdf_polycoeffs = Signal_Analyzer.polyfit(data=pdfs, poly_deg=poly_deg)
whole_pdf_polycoeffs.extend(pdf_polycoeffs)
except:
pdf_polycoeffs = np.zeros(poly_deg + 1)
scikit_stat_preprocessing = {'Method': 'ScikitLearn',
'PDFPolyfits': pdf_polycoeffs,
'PolyfitDeg': poly_deg,
'PDFs': pdfs,
'Bandwidths': bws,
'Parameters': params,
'Log-Likelyhoods': log_likelyhoods}
except:
print('Error: unable to get result from threads')
statistic_preprocessing = {'SciPy': scipy_stat_preprocessing,
'StatsmodelU': statu_stat_preprocessing,
'StatsmodelM': statm_stat_preprocessing,
'ScikitLearn': scikit_stat_preprocessing}
preprocessing = {}
preprocessing['Type'] = 'PP6'
preprocessing['Sub_To'] = signal
preprocessing['Data'] = statistic_preprocessing
info_stat_features = {'SciPy_PDFPolyfits': statistic_preprocessing['SciPy']['PDFPolyfits'],
'StatsmodelU_PDFPolyfits': statistic_preprocessing['StatsmodelU']['PDFPolyfits'],
'StatsmodelM_PDFPolyfits': statistic_preprocessing['StatsmodelM']['PDFPolyfits'],
'ScikitLearn_PDFPolyfits': statistic_preprocessing['ScikitLearn']['PDFPolyfits'],
'StatsmodelU_CDFPolyfits': statistic_preprocessing['StatsmodelU']['CDFPolyfits'],
'StatsmodelM_CDFPolyfits': statistic_preprocessing['StatsmodelM']['CDFPolyfits']}
feature = {}
feature['Type'] = 'FP6'
feature['Sub_To'] = signal
raw_feature = dict(feature)
raw_stat_features = list([poly_deg])
for values in list(info_stat_features.values()):
for value in list(values):
if str(value) != 'nan' and list(value).__len__() == poly_deg + 1:
raw_stat_features.extend(value)
break
raw_feature['Data'] = raw_stat_features
info_stat_features['Poly_deg'] = poly_deg
feature['Data'] = info_stat_features
return preprocessing, feature, raw_feature
@staticmethod
def extract_statistical_features_from_signals(data, signals):
extraction_results = []
with concurrent.futures.ThreadPoolExecutor(max_workers=signals.__len__()) as executor:
futures = []
extraction_results = []
for samples, signal in zip(data, signals):
try:
futures.append(executor.submit(Features_Extraction.extract_statistical_features, samples, signal))
break
except:
print('Error: unable to start thread on signal %s' % signal)
i = 0
while futures.__len__() > 0:
f = futures[i]
try:
fresult = f.result(timeout=20)
if f.done() and f.cancelled():
futures.pop(i)
elif f.done() and not f.cancelled() and not f.running():
extraction_results.append(fresult)
futures.pop(i)
elif f.done() and not f.running():
futures.pop(i)
if futures.__len__() != 0:
i = (i + 1) % futures.__len__()
except:
if f.done() and f.cancelled():
futures.pop(i)
elif f.done() and not f.cancelled() and not f.running():
extraction_results.append(fresult)
futures.pop(i)
elif f.done() and not f.running():
futures.pop(i)
if futures.__len__() != 0:
i = (i + 1) % futures.__len__()
return extraction_results[0], extraction_results[1], extraction_results[2]