-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils_process.py
More file actions
1327 lines (1080 loc) · 48.1 KB
/
Copy pathutils_process.py
File metadata and controls
1327 lines (1080 loc) · 48.1 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# In[] Import all the libraries
import numpy as np
import pandas as pd
import pickle
import numba
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import copy
import gc
import os
import sys
import warnings
from numpy.fft import rfft, rfftfreq, irfft
from scipy.signal import savgol_filter
import pywt
from tqdm import tqdm
# =============================================================================
# ################## Utils for Preprocessing Waveforms ########################
# =============================================================================
def low_pass(s, threshold=1e4):
fourier = rfft(s)
frequencies = rfftfreq(s.size, d=2e-2/s.size)
fourier[frequencies > threshold] = 0
return irfft(fourier)
def get_crossing(x, phase=0):
x_1 = low_pass(x, threshold=100)
x = x_1.reshape((-1, ))
zero_crossing = np.where(np.diff(np.sign(x)))[0]
up_crossing = -1
for zc in zero_crossing:
if x[zc] < 0 and x[zc + 1] > 0:
up_crossing = zc
return up_crossing
def phase_shift(x, cross):
if cross > 0:
x = np.hstack([x[cross:], x[:cross]])
return x
def noise_estimation_fixed(x):
NUM_PATCH = 1000
LEN_PATCH = 1000
#indexes = np.random.uniform(low=0, high=799000, size=NUM_PATCH)
indexes = np.linspace(0, 799000, NUM_PATCH)
patches = np.zeros((NUM_PATCH, LEN_PATCH))
for i in range(NUM_PATCH):
patches[i, :] = x[int(indexes[i]): int(indexes[i]) + LEN_PATCH]
coverage = 0
diff = []
over_th = 0
for index, i in enumerate(np.linspace(0, 15, 31)):
num_cover = np.sum(i > np.max(patches, axis=-1))
diff.append(num_cover - coverage)
if num_cover - coverage > 80:
over_th = index
coverage = num_cover
loc_max_diff = np.argmax(diff)
#print(diff)
#plt.plot(diff)
return max(loc_max_diff, over_th) * 0.5 + 0.5
def spike_detection_ori5_fast(x, size=250, noise_level=3):
length = x.shape[0]
x_abs = abs(x)
NUM_PART = 20
LEN_PART = int(length / NUM_PART)
large_points = []
for i in range(NUM_PART):
large_points.append(np.argpartition(x_abs[i*LEN_PART: (i+1)*LEN_PART], 40000-100)[-100:] + i*LEN_PART)
large_points = np.concatenate(large_points)
x_clear = abs(x_abs)
for point in large_points:
if point - 50 > 0 and point + 50 < 800000:
x_clear[point - 50: point + 50] = 0
large_points_2 = []
for i in range(NUM_PART):
#large_points_2.append(np.argsort(x_clear[i*LEN_PART: (i+1)*LEN_PART])[-50:] + i*LEN_PART)
#large_points_2.append(bottleneck.argpartition(-x_clear[i*LEN_PART: (i+1)*LEN_PART], 100)[:100] + i*LEN_PART)
large_points_2.append(np.argpartition(x_clear[i*LEN_PART: (i+1)*LEN_PART], 40000-100)[-100:] + i*LEN_PART)
large_points_2 = np.concatenate(large_points_2)
large_points = np.concatenate([large_points, large_points_2])
x_clear_2 = abs(x_clear)
for point in large_points_2:
if point - 50 > 0 and point + 50 < 800000:
x_clear_2[point - 50: point + 50] = 0
large_points_3 = []
for i in range(NUM_PART):
#large_points_3.append(np.argsort(x_clear_2[i*LEN_PART: (i+1)*LEN_PART])[-50:] + i*LEN_PART)
#large_points_3.append(bottleneck.argpartition(-x_clear_2[i*LEN_PART: (i+1)*LEN_PART], 100)[:100] + i*LEN_PART)
large_points_3.append(np.argpartition(x_clear_2[i*LEN_PART: (i+1)*LEN_PART], 40000-100)[-100:] + i*LEN_PART)
large_points_3 = np.concatenate(large_points_3)
large_points = np.concatenate([large_points, large_points_3])
#large_points = np.argsort(x)[-2000:]
points = []
for point in large_points:
if point - 25 > 0 and point + 25 < 800000:
window = x_abs[max(0, point - 25): min(length, point + 25)]
if x_abs[point] == np.max(window):
if x_abs[point] > 4 and x_abs[point] < 50 and x_abs[point] > noise_level * 1.155:
FLAG = True
while FLAG == True:
if np.sign(x[point-1]) * np.sign(x[point]) == -1 and x_abs[point-1] > 0.5 * x_abs[point]:
point = point - 1
else:
FLAG = False
if np.sign(x[point-2]) * np.sign(x[point]) == -1 and x_abs[point-2] > 0.5 * x_abs[point]:
points.append([point-2, x[point]])
elif np.sign(x[point-3]) * np.sign(x[point]) == -1 and x_abs[point-3] > 0.5 * x_abs[point]:
points.append([point-3, x[point]])
else:
points.append([point, x[point]])
return points
def RMSE(x1, x2):
rmse = np.sqrt(np.mean(np.power(x1 - x2, 2)))
return rmse
def peaks_on_flatten(train_df, signal_ids, visualization=False):
start_time = time.time()
all_aligned_signals = []
all_flat_signals = []
all_points = []
for index in signal_ids:
if np.mod(index, 100) == 0:
print(index)
print('Elapsed time: {}'.format(time.time() - start_time))
signal = train_df[str(index)].values
crossing = get_crossing(signal)
signal = phase_shift(signal, crossing)
yhat = savgol_filter(signal, 99, 3)
flat = signal - yhat
noise_level = noise_estimation_fixed(flat)
points = spike_detection_ori5_fast(flat, noise_level=noise_level)
points = np.array(points)
all_aligned_signals.append(signal)
all_flat_signals.append(flat)
all_points.append(points)
if visualization:
plt.plot(signal)
plt.plot(yhat)
plt.plot(flat)
plt.axhline(noise_level, color='y')
plt.axhline(-noise_level, color='y')
plt.scatter(points[:,0], points[:,1], color='red')
plt.legend(['aligned', 'high pass', 'flatten','noise'])
plt.xlim([0,800000])
plt.show()
return all_aligned_signals, all_flat_signals, all_points
def choose_chunk_peak(all_flat_signals, all_points, window_size=5000, wave_len=15):
num_window = all_flat_signals[0].shape[0] // window_size
waves_all = np.zeros([len(all_flat_signals), num_window, 2*wave_len])
start_time = time.time()
for index in range(len(all_flat_signals)):
if np.mod(index, 100) == 0:
print(index)
print('Elapsed time: {}'.format(time.time() - start_time))
flat = all_flat_signals[index]
points = all_points[index]
if len(points) > 0:
for i in range(num_window):
flat_interval = flat[(i*window_size) : (i+1)*window_size]
loc = (points[:,0] >= i*window_size) & (points[:,0] <= (i+1)*window_size)
points_interval = points[loc]
# points_interval = points[(points[:,0] >= i*window_size) & (points[:,0] <= (i+1)*window_size)]
if len(points_interval) > 0:
point_keep = points_interval[np.argmax(np.abs(points_interval[:,1]))]
start = int(point_keep[0] - 15)
end = int(point_keep[0] + 15)
f = flat[start:end]
waves_all[index, i, :] = f
return waves_all
# =============================================================================
# ################## Utils for Extracting Peaks #####################
# =============================================================================
@numba.jit(nopython=True)
def flatiron(x, alpha=100., beta=1):
"""
Flatten signal
Creator: Michael Kazachok
Source: https://www.kaggle.com/miklgr500/flatiron
"""
new_x = np.zeros_like(x)
zero = x[0]
for i in range(1, len(x)):
zero = zero*(alpha-beta)/alpha + beta*x[i]/alpha
new_x[i] = x[i] - zero
return new_x
@numba.jit(nopython=True)
def drop_missing(intersect,sample):
"""
Find intersection of sorted numpy arrays
Since intersect1d sort arrays each time, it's effectively inefficient.
Here you have to sweep intersection and each sample together to build
the new intersection, which can be done in linear time, maintaining order.
Source: https://stackoverflow.com/questions/46572308/intersection-of-sorted-numpy-arrays
Creator: B. M.
"""
i=j=k=0
new_intersect=np.empty_like(intersect)
while i< intersect.size and j < sample.size:
if intersect[i]==sample[j]: # the 99% case
new_intersect[k]=intersect[i]
k+=1
i+=1
j+=1
elif intersect[i]<sample[j]:
i+=1
else :
j+=1
return new_intersect[:k]
@numba.jit(nopython=True)
def _local_maxima_1d_window_single_pass(x, w):
midpoints = np.empty(x.shape[0] // 2, dtype=np.intp)
left_edges = np.empty(x.shape[0] // 2, dtype=np.intp)
right_edges = np.empty(x.shape[0] // 2, dtype=np.intp)
m = 0 # Pointer to the end of valid area in allocated arrays
i = 1 # Pointer to current sample, first one can't be maxima
i_max = x.shape[0] - 1 # Last sample can't be maxima
while i < i_max:
# Test if previous sample is smaller
if x[i - 1] < x[i]:
i_ahead = i + 1 # Index to look ahead of current sample
# Find next sample that is unequal to x[i]
while i_ahead < i_max and x[i_ahead] == x[i]:
i_ahead += 1
i_right = i_ahead - 1
f = False
i_window_end = i_right + w
while i_ahead < i_max and i_ahead < i_window_end:
if x[i_ahead] > x[i]:
f = True
break
i_ahead += 1
# Maxima is found if next unequal sample is smaller than x[i]
if x[i_ahead] < x[i]:
left_edges[m] = i
right_edges[m] = i_right
midpoints[m] = (left_edges[m] + right_edges[m]) // 2
m += 1
# Skip samples that can't be maximum
i = i_ahead - 1
i += 1
# Keep only valid part of array memory.
midpoints = midpoints[:m]
left_edges = left_edges[:m]
right_edges = right_edges[:m]
return midpoints, left_edges, right_edges
@numba.jit(nopython=True)
def local_maxima_1d_window(x, w=1):
"""
Find local maxima in a 1D array.
This function finds all local maxima in a 1D array and returns the indices
for their midpoints (rounded down for even plateau sizes).
It is a modified version of scipy.signal._peak_finding_utils._local_maxima_1d
to include the use of a window to define how many points on each side to use in
the test for a point being a local maxima.
Parameters
----------
x : ndarray
The array to search for local maxima.
w : np.int
How many points on each side to use for the comparison to be True
Returns
-------
midpoints : ndarray
Indices of midpoints of local maxima in `x`.
Notes
-----
- Compared to `argrelmax` this function is significantly faster and can
detect maxima that are more than one sample wide. However this comes at
the cost of being only applicable to 1D arrays.
"""
fm, fl, fr = _local_maxima_1d_window_single_pass(x, w)
bm, bl, br = _local_maxima_1d_window_single_pass(x[::-1], w)
bm = np.abs(bm - x.shape[0] + 1)[::-1]
bl = np.abs(bl - x.shape[0] + 1)[::-1]
br = np.abs(br - x.shape[0] + 1)[::-1]
m = drop_missing(fm, bm)
return m
@numba.jit(nopython=True)
def plateau_detection(grad, threshold, plateau_length=5):
"""Detect the point when the gradient has reach a plateau"""
count = 0
loc = 0
for i in range(grad.shape[0]):
if grad[i] > threshold:
count += 1
if count == plateau_length:
loc = i - plateau_length
break
return loc
#@numba.jit(nopython=True)
def get_peaks(x, window=25,visualise=False,visualise_color=None):
"""
Find the peaks in a signal trace.
Parameters
----------
x : ndarray
The array to search.
window : np.int
How many points on each side to use for the local maxima test
Returns
-------
peaks_x : ndarray
Indices of midpoints of peaks in `x`.
peaks_y : ndarray
Absolute heights of peaks in `x`.
x_hp : ndarray
An absolute flattened version of `x`.
"""
x_hp = flatiron(x, 100, 1)
x_dn = np.abs(x_hp)
peaks = local_maxima_1d_window(x_dn, window)
heights = x_dn[peaks]
ii = np.argsort(heights)[::-1]
peaks = peaks[ii]
heights = heights[ii]
ky = heights
kx = np.arange(1, heights.shape[0]+1)
conv_length = 9
grad = np.diff(ky, 1)/np.diff(kx, 1)
grad = np.convolve(grad, np.ones(conv_length)/conv_length)#, mode='valid')
grad = grad[conv_length-1:-conv_length+1]
knee_x = plateau_detection(grad, -0.01, plateau_length=1000)
knee_x -= conv_length//2
if visualise:
plt.plot(grad, color=visualise_color)
plt.axvline(knee_x, ls="--", color=visualise_color)
peaks_x = peaks[:knee_x]
peaks_y = heights[:knee_x]
ii = np.argsort(peaks_x)
peaks_x = peaks_x[ii]
peaks_y = peaks_y[ii]
return peaks_x, peaks_y, x_hp
@numba.jit(nopython=True)
def clip(v, l, u):
"""Numba helper function to clip a value"""
if v < l:
v = l
elif v > u:
v = u
return v
@numba.jit(nopython=True)
def create_sawtooth_template(sawtooth_length, pre_length, post_length):
"""Generate sawtooth template"""
l = pre_length+post_length+1
st = np.zeros(l)
for i in range(sawtooth_length+1):
j = pre_length+i
if j < l:
st[j] = 1 - ((2./sawtooth_length) * i)
return st
@numba.jit(nopython=True)
def create_sawtooth_template1(pre_length, post_length):
"""Generate sawtooth template"""
l = pre_length+post_length+1
st = np.zeros(l)
start = pre_length
st[start] = 1
st[start + 1] = -1
st[start + 2] = 1
return st
@numba.jit(nopython=True)
def create_sawtooth_template2(pre_length, post_length):
"""Generate sawtooth template"""
l = pre_length+post_length+1
st = np.zeros(l)
start = pre_length
st[start] = 1
st[start + 1] = -1
st[start + 2] = 0
st[start + 3] = 0.5
return st
@numba.jit(nopython=True)
def create_sawtooth_template3(pre_length, post_length):
"""Generate sawtooth template"""
l = pre_length+post_length+1
st = np.zeros(l)
start = pre_length
st[start] = 1
st[start + 1] = 0
st[start + 2] = -1
return st
@numba.jit(nopython=True)
def create_sawtooth_template4(pre_length, post_length):
"""Generate sawtooth template"""
l = pre_length+post_length+1
st = np.zeros(l)
start = pre_length
st[start] = 1
return st
@numba.jit(nopython=True)
def create_sawtooth_template5(pre_length, post_length):
"""Generate sawtooth template"""
l = pre_length+post_length+1
st = np.zeros(l)
start = pre_length
st[start] = 1
st[start + 1] = -1
st[start + 2] = 1
st[start + 3] = -1
return st
@numba.jit(nopython=True)
def create_sawtooth_template6(pre_length, post_length):
"""Generate sawtooth template"""
l = pre_length+post_length+1
st = np.zeros(l)
start = pre_length
st[start] = 1
st[start + 1] = 0
st[start + 2] = -1
st[start + 3] = 1
return st
@numba.jit(nopython=True)
def create_sawtooth_template7(pre_length, post_length):
"""Generate sawtooth template"""
l = pre_length+post_length+1
st = np.zeros(l)
start = pre_length
st[start] = 1
st[start + 1] = -1
return st
@numba.jit(nopython=True)
def calculate_peak_features(px, x_hp0, ws=5, wl=25):
"""
Calculate features for peaks.
Parameters
----------
px : ndarray
Indices of peaks.
x_hp0 : ndarray
The array to search.
ws : np.int
How many points on each side to use for small window features
wl : np.int
How many points on each side to use for large window features
Returns
-------
features : ndarray
Features calculate for each peak in `x_hp0`.
"""
peak_features_names = [
'ratio_next',
'ratio_prev',
'small_dist_to_min',
'sawtooth_rmse',
'sawtooth_rmse1',
'sawtooth_rmse2',
'sawtooth_rmse3',
'sawtooth_rmse4',
'sawtooth_rmse5',
'sawtooth_rmse6',
'sawtooth_rmse7',
]
num_peak_features = len(peak_features_names)
features = np.ones((px.shape[0], num_peak_features), dtype=np.float64) * np.nan
for i in range(px.shape[0]):
feature_number = 0
x = px[i]
x_next = x+1
x_prev = x-1
h0 = x_hp0[x]
ws_s = clip(x-ws, 0, 800000-1)
ws_e = clip(x+ws, 0, 800000-1)
wl_s = clip(x-wl, 0, 800000-1)
wl_e = clip(x+wl, 0, 800000-1)
ws_pre = x - ws_s
ws_post = ws_e - x
wl_pre = x - wl_s
wl_post = wl_e - x
if x_next < 800000:
h0_next = x_hp0[x_next]
features[i, feature_number] = np.abs(h0_next)/np.abs(h0)
feature_number += 1
if x_prev >= 0:
h0_prev = x_hp0[x_prev]
features[i, feature_number] = np.abs(h0_prev)/np.abs(h0)
feature_number += 1
x_hp_ws0 = x_hp0[ws_s:ws_e+1]
x_hp_wl0 = x_hp0[wl_s:wl_e+1]
x_hp_wl0_norm = (x_hp_wl0/np.abs(h0))
x_hp_ws0_norm = (x_hp_ws0/np.abs(h0))
x_hp_abs_wl0 = np.abs(x_hp_wl0)
wl_max_0 = np.max(x_hp_abs_wl0)
ws_opp_peak_i = np.argmin(x_hp_ws0*np.sign(h0))
features[i, feature_number] = ws_opp_peak_i - ws
feature_number += 1
x_hp_wl0_norm_sign = x_hp_wl0_norm * np.sign(h0)
sawtooth_length = 3
st = create_sawtooth_template(sawtooth_length, wl_pre, wl_post)
assert np.argmax(st) == np.argmax(x_hp_wl0_norm_sign)
assert st.shape[0] == x_hp_wl0_norm_sign.shape[0]
features[i, feature_number] = np.mean(np.power(x_hp_wl0_norm_sign - st, 2))
feature_number += 1
st = create_sawtooth_template1(wl_pre, wl_post)
features[i, feature_number] = np.mean(np.power(x_hp_wl0_norm_sign - st, 2))
feature_number += 1
st = create_sawtooth_template2(wl_pre, wl_post)
features[i, feature_number] = np.mean(np.power(x_hp_wl0_norm_sign - st, 2))
feature_number += 1
st = create_sawtooth_template3(wl_pre, wl_post)
features[i, feature_number] = np.mean(np.power(x_hp_wl0_norm_sign - st, 2))
feature_number += 1
st = create_sawtooth_template4(wl_pre, wl_post)
features[i, feature_number] = np.mean(np.power(x_hp_wl0_norm_sign - st, 2))
feature_number += 1
st = create_sawtooth_template5(wl_pre, wl_post)
features[i, feature_number] = np.mean(np.power(x_hp_wl0_norm_sign - st, 2))
feature_number += 1
st = create_sawtooth_template6(wl_pre, wl_post)
features[i, feature_number] = np.mean(np.power(x_hp_wl0_norm_sign - st, 2))
feature_number += 1
st = create_sawtooth_template7(wl_pre, wl_post)
features[i, feature_number] = np.mean(np.power(x_hp_wl0_norm_sign - st, 2))
feature_number += 1
if i == 0:
assert feature_number == num_peak_features
return features
def process_signal(data,window=25):
"""
Process a signal trace to find the peaks and calculate features for each peak.
Parameters
----------
data : ndarray
The array to search.
window : np.int
How many points on each side to use for the local maxima test
Returns
-------
px0 : ndarray
Indices for each peak in `data`.
height0 : ndarray
Absolute heaight for each peak in `data`.
f0 : ndarray
Features calculate for each peak in `data`.
"""
px0, height0, x_hp0 = get_peaks(
data.astype(np.float),
window=window,
)
f0 = calculate_peak_features(px0, x_hp0)
return px0, height0, f0
def process_measurement_peaks(data, signal_ids):
"""
Process three signal traces in measurment to find the peaks
and calculate features for each peak.
Parameters
----------
data : ndarray
Signal traces.
signal_ids : ndarray
Signal IDs for each of the signal traces in measurment
Returns
-------
res : ndarray
Data for each peak in the three traces in `data`.
sigid_res : ndarray
Signal ID for each row in `res`.
"""
res = []
sigid_res = []
assert data.shape[1] % 3 == 0
N = data.shape[1]//3
for i in range(N):
sigids = signal_ids[i*3:(i+1)*3]
x = data[:, i*3:(i+1)*3].astype(np.float)
px0, height0, f0 = process_signal(x[:, 0])
px1, height1, f1 = process_signal(x[:, 1])
px2, height2, f2 = process_signal(x[:, 2])
if px0.shape[0] != 0:
res.append(np.hstack([
px0[:, np.newaxis],
height0[:, np.newaxis],
f0,
]))
sigid_res.append(np.ones(px0.shape[0], dtype=np.int) * sigids[0])
if px1.shape[0] != 0:
res.append(np.hstack([
px1[:, np.newaxis],
height1[:, np.newaxis],
f1,
]))
sigid_res.append(np.ones(px1.shape[0], dtype=np.int) * sigids[1])
if px2.shape[0] != 0:
res.append(np.hstack([
px2[:, np.newaxis],
height2[:, np.newaxis],
f2,
]))
sigid_res.append(np.ones(px2.shape[0], dtype=np.int) * sigids[2])
return res, sigid_res
def process_measurement(data_array, meta_df, fft_data):
"""
Process three signal traces in measurment to find the peaks
and calculate features for each peak.
Parameters
----------
# data_df : pandas.DataFrame
# Signal traces.
data_array : ndarray
Signal traces.
meta_df : pandas.DataFrame
Meta data for measurement
fft_data : ndarray
50Hz fourier coefficient for three traces
Returns
-------
peaks : pandas.DataFrame
Data for each peak in the three traces in `data`.
"""
peaks, sigids = process_measurement_peaks(
# data_df.values, # [:, :100*3],
data_array,
meta_df['signal_id'].values, # [:100*3]
)
peaks = np.concatenate(peaks)
peaks = pd.DataFrame(
peaks,
columns=['px', 'height'] + peak_features_names
)
peaks['signal_id'] = np.concatenate(sigids)
# Calculate the phase resolved location of each peak
phase_50hz = np.angle(fft_data, deg=False) # fft_data[:, 1]
phase_50hz = pd.DataFrame(
phase_50hz,
columns=['phase_50hz']
)
phase_50hz['signal_id'] = meta_df['signal_id'].values
peaks = pd.merge(peaks, phase_50hz, on='signal_id', how='left')
dt = (20e-3/(800000))
f1 = 50
w1 = 2*np.pi*f1
peaks['phase_aligned_x'] = (np.degrees(
(w1*peaks['px'].values*dt) + peaks['phase_50hz'].values
) + 90) % 360
# Calculate the phase resolved quarter for each peak
peaks['Q'] = pd.cut(peaks['phase_aligned_x'], [0, 90, 180, 270, 360], labels=[0, 1, 2, 3])
return peaks
@numba.jit(nopython=True, parallel=True)
def calculate_50hz_fourier_coefficient(data):
"""Calculate the 50Hz Fourier coefficient of a signal.
Assumes the signal is 800000 data points long and covering 20ms.
"""
n = 800000
assert data.shape[0] == n
omegas = np.exp(-2j * np.pi * np.arange(n) / n).reshape(n, 1)
m_ = omegas ** np.arange(1, 2)
m = m_.flatten()
res = np.zeros(data.shape[1], dtype=m.dtype)
for i in numba.prange(data.shape[1]):
res[i] = m.dot(data[:, i].astype(m.dtype))
return res
def process(peaks_df, meta_df):
results = pd.DataFrame(index=meta_df['id_measurement'].unique())
results.index.rename('id_measurement', inplace=True)
################################################################################
if not USE_SIMPLIFIED_VERSION:
# Filter peaks using ratio_next and height features
# Note: may not be all that important
peaks_df = peaks_df[~(
(peaks_df['ratio_next'] > 0.33333)
& (peaks_df['height'] > 50)
)]
################################################################################
# Count peaks in phase resolved quarters 0 and 2
p = peaks_df[peaks_df['Q'].isin([0, 2])].copy()
res = p.groupby('id_measurement').agg(
{
'px': ['count'],
})
res.columns = ["peak_count_Q02"]
results = pd.merge(results, res, on='id_measurement', how='left')
################################################################################
# Count total peaks for each measurement id
res = peaks_df.groupby('id_measurement').agg(
{
'px': ['count'],
})
res.columns = ["peak_count_total"]
results = pd.merge(results, res, on='id_measurement', how='left')
################################################################################
# Count peaks in phase resolved quarters 1 and 3
p = peaks_df[peaks_df['Q'].isin([1, 3])].copy()
res = p.groupby('id_measurement').agg(
{
'px': ['count'],
})
res.columns = ['peak_count_Q13']
results = pd.merge(results, res, on='id_measurement', how='left')
################################################################################
# Calculate additional features using phase resolved quarters 0 and 2
feature_quarters = [0, 2]
p = peaks_df[peaks_df['Q'].isin(feature_quarters)].copy()
p['abs_small_dist_to_min'] = np.abs(p['small_dist_to_min'])
res = p.groupby('id_measurement').agg(
{
'height': ['mean', 'std'],
'ratio_prev': ['mean'],
'ratio_next': ['mean'],
'abs_small_dist_to_min': ['mean'],
'sawtooth_rmse': ['mean'],
'sawtooth_rmse1': ['mean'],
'sawtooth_rmse2': ['mean'],
'sawtooth_rmse3': ['mean'],
'sawtooth_rmse4': ['mean'],
'sawtooth_rmse5': ['mean'],
'sawtooth_rmse6': ['mean'],
'sawtooth_rmse7': ['mean'],
})
res.columns = ["_".join(f) + '_Q02' for f in res.columns]
results = pd.merge(results, res, on='id_measurement', how='left')
return results
def get_features(signal_ids, kmeans, kmeans_A, kmeans_B, kmeans_C):
signal_to_peak_dict = {}
waves_all = []
waves_all_unnorm = []
point_count = 0
signal_to_peak_dict_A = {}
waves_all_A = []
waves_all_unnorm_A = []
point_count_A = 0
signal_to_peak_dict_B = {}
waves_all_B = []
waves_all_unnorm_B = []
point_count_B = 0
signal_to_peak_dict_C = {}
waves_all_C = []
waves_all_unnorm_C = []
point_count_C = 0
num_types_all = 15
num_types_sep = 6
NUM_SEGS = 20
NUM_SIGNALS = len(signal_ids)
hist_types = np.zeros((NUM_SIGNALS, num_types_all, NUM_SEGS))
hist_types_A = np.zeros((NUM_SIGNALS // 3, num_types_sep, NUM_SEGS))
hist_types_B = np.zeros((NUM_SIGNALS // 3, num_types_sep, NUM_SEGS))
hist_types_C = np.zeros((NUM_SIGNALS // 3, num_types_sep, NUM_SEGS))
for index in signal_ids:
if np.mod(index, 100) == 0:
print(index)
# signal = praq_train[str(index)].values
signal = pq.read_pandas('train.parquet', columns = str(index)).to_pandas().values.reshape(-1)
crossing = get_crossing(signal)
signal = phase_shift(signal, crossing)
yhat = savgol_filter(signal, 99, 3)
flat = signal - yhat
noise_level = noise_estimation_fixed(flat)
points = spike_detection_ori5_fast(flat, noise_level=noise_level)
points = np.array(points)
if len(points) > 0:
for point in points:
#peak_dict[point_count] = (index, point, meta_train.iloc[index].target)
start = int(point[0] - 15)
end = int(point[0] + 15)
f = flat[start: end]
f = f * np.sign(f[15])
waves_all_unnorm.append(f)
f = f / np.max(abs(f))
waves_all.append(f)
if index in signal_to_peak_dict.keys():
signal_to_peak_dict[index].append(point_count)
else:
signal_to_peak_dict[index] = [point_count]
point_count += 1
pred = kmeans.predict(f[None, :])
pos = point[0]
pos_id = int(pos // 40000)
hist_types[index, pred, pos_id] += 1
if np.mod(index, 3) == 0:
for point in points:
start = int(point[0] - 15)
end = int(point[0] + 15)
f = flat[start: end]
f = f * np.sign(f[15])
waves_all_unnorm_A.append(f)
f = f / np.max(abs(f))
waves_all_A.append(f)
if index in signal_to_peak_dict_A.keys():
signal_to_peak_dict_A[index].append(point_count_A)
else:
signal_to_peak_dict_A[index] = [point_count_A]
point_count_A += 1
pred = kmeans_A.predict(f[None, :])
pos = point[0]
pos_id = int(pos // 40000)
hist_types_A[index // 3, pred, pos_id] += 1
elif np.mod(index, 3) == 1:
for point in points:
start = int(point[0] - 15)
end = int(point[0] + 15)
f = flat[start: end]
f = f * np.sign(f[15])
waves_all_unnorm_B.append(f)
f = f / np.max(abs(f))
waves_all_B.append(f)
if index in signal_to_peak_dict_B.keys():
signal_to_peak_dict_B[index].append(point_count_B)
else:
signal_to_peak_dict_B[index] = [point_count_B]
point_count_B += 1
pred = kmeans_B.predict(f[None, :])
pos = point[0]
pos_id = int(pos // 40000)
hist_types_B[index // 3, pred, pos_id] += 1
else:
for point in points: