-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_interpret_distance_dsymb.py
983 lines (863 loc) · 30.9 KB
/
utils_interpret_distance_dsymb.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
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
import json
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from scipy.cluster.hierarchy import average, complete, dendrogram, single, ward
from scipy.signal import istft, stft
from scipy.stats import rankdata
from sklearn.cluster import AgglomerativeClustering, KMeans
from sklearn.metrics import (
adjusted_rand_score,
normalized_mutual_info_score,
rand_score,
silhouette_samples,
)
from sklearn.neighbors import NearestNeighbors
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.utils import Bunch
from symbolic_signal_distance import SymbolicSignalDistance
from symbolization import Symbolization
from utils import create_path
pairwise_dist = SymbolicSignalDistance.pairwise_dist
def load_json(filename: Path):
with open(file=filename) as fp:
res = json.load(fp)
return res
def scale_univariate_signal(univariate_signal):
"""Inputs a univariate signal."""
if univariate_signal.ndim != 1:
raise TypeError("The signal is not univariate.")
return (univariate_signal - np.mean(univariate_signal)) / (
np.std(univariate_signal)
)
def scale_univariate_signals(list_of_univariate_signals: list):
"""Inputs a list of univariate signals."""
return [
scale_univariate_signal(univariate_signal)
for univariate_signal in list_of_univariate_signals
]
def scale_multivariate_signal(multivariate_signal):
"""Inputs a multivariate signal.
Scale all the dimensions of a multivariate signal as if they were univariate.
"""
scaled_multivariate_signal = multivariate_signal.copy()
if multivariate_signal.ndim != 2:
raise TypeError("The signal is not multivariate.")
elif multivariate_signal.shape[0] < multivariate_signal.shape[1]:
raise TypeError(
"There are more dimensions than samples, which is weird."
)
for dim in range(multivariate_signal.shape[1]):
scaled_multivariate_signal[:, dim] = scale_univariate_signal(
multivariate_signal[:, dim]
)
return scaled_multivariate_signal
def permute_list(input_list, mapping_signal_indexes_new_to_raw):
"""Permute the rows so that each row is grouped by same label."""
output_list = [input_list[i] for i in mapping_signal_indexes_new_to_raw]
return output_list
def get_signal_index_of_label_change(df_metadata, str_label):
"""Get the signal indexes where the labels change"""
y_label = df_metadata[str_label].tolist()
le = LabelEncoder()
encoded_y_label = le.fit_transform(y_label)
pd_encoded_y_label = pd.Series(encoded_y_label)
label_changing_indexes = pd_encoded_y_label.index[
pd_encoded_y_label.diff() == 1
].tolist()
print(label_changing_indexes)
return label_changing_indexes
def get_spectrogram_from_signal(
univariate_signal, sampling_frequency, win_size, frequency_threshold
):
f, t, Zxx = stft(
univariate_signal,
fs=sampling_frequency,
nperseg=win_size,
noverlap=win_size - 1,
)
t = t[0:-1]
# By default, the last axis of Zxx corresponds to the segment times.
Zxx = Zxx[:, :-1]
frequency_threshold_index = list(f).index(frequency_threshold)
multivariate_spectrogram_signal = np.abs(Zxx).T
b_get_spectrogram_from_signal = Bunch(
f=f[0 : frequency_threshold_index + 1],
t=t,
Zxx=Zxx[0 : frequency_threshold_index + 1, :],
multivariate_spectrogram_signal=multivariate_spectrogram_signal[
:, 0 : frequency_threshold_index + 1
],
)
return b_get_spectrogram_from_signal
def plot_spectrogram_with_ruptures(
f, t, Zxx, s_plot, bkps=None, is_save=False, date_exp="unknown"
):
"""
Can add the true ruptures.
"""
cmap = sns.color_palette("viridis", as_cmap=True)
plt.figure(figsize=(6.4, 4.8))
plt.pcolormesh(t, f, np.abs(Zxx), cmap=cmap)
plt.colorbar()
if bkps is not None:
for i, bkp in enumerate(bkps):
if bkp != bkps[-1]: # the last breakpoint is the number of samples
bkp_seconds = bkp / max(bkps) * max(t)
if i == 0:
plt.axvline(
x=bkp_seconds,
linestyle="--",
linewidth=2,
label="segmentation bins",
color="red",
)
else:
plt.axvline(
x=bkp_seconds, linestyle="--", linewidth=2, color="red"
)
# plt.title("STFT")
plt.ylabel("Frequency [Hz]")
plt.xlabel("Time [sec]")
plt.tight_layout()
plt.margins(x=0)
if bkps is None:
str_ruptures = "without"
else:
str_ruptures = "with"
plt.legend()
if is_save:
print(
f"results/{date_exp}/img/spectrogram_{str_ruptures}_ruptures_{s_plot}.png"
)
plt.savefig(
f"results/{date_exp}/img/spectrogram_{str_ruptures}_ruptures_{s_plot}.png",
dpi=200,
)
plt.show()
def extend_array_with_last_element(array):
return np.array(list(array) + [array[-1]])
def filter_signal_using_stft(
univariate_signal: np.ndarray,
sampling_frequency: int,
win_size: int,
frequency_threshold: int,
):
"""Filter the frequencies of a univariate signal using STFT then inverse STFT.
Note that it might be easier to directly filter the signal.
"""
# Apply the STFT transformation
f, t, Zxx = stft(
univariate_signal,
fs=sampling_frequency,
nperseg=win_size,
noverlap=win_size - 1,
)
t = t[0:-1]
# By default, the last axis of Zxx corresponds to the segment times.
Zxx = Zxx[:, :-1]
# Get the inverse STFT (without any filtering)
_, reconstructed_univariate_signal = istft(
Zxx, fs=sampling_frequency, nperseg=win_size, noverlap=win_size - 1
)
# Apply the filtering
frequency_threshold_index = list(f).index(frequency_threshold)
f_filtered = f[0 : frequency_threshold_index + 1]
Zxx_filtered = Zxx.copy()
Zxx_filtered[frequency_threshold_index + 1 :, :] = 0
# Get the filtered inverse STFT
_, filtered_reconstructed_univariate_signal = istft(
Zxx_filtered,
fs=sampling_frequency,
nperseg=win_size,
noverlap=win_size - 1,
)
# Extending the inverse STFT signals so that they have the same length as the original signal
reconstructed_univariate_signal = extend_array_with_last_element(
reconstructed_univariate_signal
)
filtered_reconstructed_univariate_signal = extend_array_with_last_element(
filtered_reconstructed_univariate_signal
)
return (
t,
reconstructed_univariate_signal,
filtered_reconstructed_univariate_signal,
)
def plot_single_color_bar(
features_with_symbols_labels_df: pd.DataFrame,
signal_index: int,
n_symbols: int,
is_display_legend: bool = True,
is_display_border: bool = True,
sampling_frequency=100,
is_savefig=False,
date_exp="unknown",
):
"""
Plot the color bar of a single symbolic sequence.
"""
print(f"{signal_index = }")
if is_display_border:
edge_color_plot = "black"
else:
edge_color_plot = None
signal_features_with_symbols_labels_df = (
features_with_symbols_labels_df.query(f"signal_index == {signal_index}")
)
# list_colors = sns.color_palette("YlOrRd", n_colors=n_symbols)
list_colors = sns.color_palette("tab10", n_colors=n_symbols)
h = 1
fig, ax = plt.subplots(figsize=(10, 0.5))
for segment_index in range(len(signal_features_with_symbols_labels_df)):
a = signal_features_with_symbols_labels_df["segment_start"].iloc[
segment_index
]
b = signal_features_with_symbols_labels_df["segment_end"].iloc[
segment_index
]
symbol = signal_features_with_symbols_labels_df["segment_symbol"].iloc[
segment_index
]
ax.axvspan(
xmin=a,
xmax=b,
ymin=0,
ymax=1,
facecolor=list_colors[symbol],
edgecolor=edge_color_plot,
)
if is_display_legend:
# Display the legends of the symbols
for i in range(n_symbols):
ax.axvspan(
xmin=0,
xmax=0,
ymin=0,
ymax=0,
facecolor=list_colors[i],
label=i,
)
# ax.set_yticks([0.5])
# ax.set_yticklabels([signal_index])
if sampling_frequency is not None:
prev_xticks = ax.get_xticks()
new_xlabels = list(prev_xticks / sampling_frequency)
ax.set_xticks(prev_xticks)
ax.set_xticklabels(new_xlabels)
ax.set_yticks([])
ax.set_yticklabels([])
if is_display_legend:
ax.legend(
loc="upper center",
bbox_to_anchor=(0.5, 3),
fancybox=True,
shadow=True,
ncol=n_symbols,
title="Symbol",
)
# Title and labels
plt.margins(x=0)
if sampling_frequency is not None:
plt.xlabel("Time [sec]")
else:
plt.xlabel("Time stamp")
# plt.ylabel("signal index")
# plt.title("$d_{symb}$", loc="center")
if is_savefig:
plt.savefig(
f"results/{date_exp}/img/colorbar_{signal_index}.png",
bbox_inches="tight",
dpi=200,
)
plt.show()
def plot_color_bar(
features_with_symbols_labels_df: pd.DataFrame,
is_save=False,
date_exp=None,
pen_factor=None,
n_symbols=None,
data_source=None,
dataset_name=None,
specific_symbol=None,
is_display_legend: bool = False,
is_display_border: bool = False,
change_indexes: list = None,
y_label: list = None,
):
"""
Plot the color bars (symbolization) of several signals given the features and symbols per segment,
and the true signal class label.
"""
if is_display_border:
edge_color_plot = "black"
else:
edge_color_plot = None
# Get the smallest and largest signal indexes
bottom_signal = features_with_symbols_labels_df["signal_index"].min()
top_signal = features_with_symbols_labels_df["signal_index"].max()
# Do a translation of all signals indexes so that the first one is zero
if bottom_signal > 0:
features_with_symbols_labels_df["signal_index"] = (
features_with_symbols_labels_df["signal_index"].values
- features_with_symbols_labels_df["signal_index"].min()
)
# Get some meta data
n_signals = features_with_symbols_labels_df["signal_index"].nunique()
n_symbols = features_with_symbols_labels_df["segment_symbol"].max() + 1
n_samples = features_with_symbols_labels_df["segment_end"].max()
l_signal_indexes = sorted(
features_with_symbols_labels_df[["signal_index"]].drop_duplicates()[
"signal_index"
]
)
l_unique_symbols = sorted(
features_with_symbols_labels_df["segment_symbol"].unique()
)
h = 1 / n_signals
# list_colors = sns.color_palette("YlOrRd", n_colors=n_symbols)
list_colors = sns.color_palette("tab10", n_colors=n_symbols)
# Define the figure
fig, ax = plt.subplots(figsize=(10, n_signals // 5))
ax.set_ylim(bottom=bottom_signal, top=top_signal)
# ax.invert_yaxis()
# Plot the color bars per segment per signal
for signal_index in l_signal_indexes:
signal_features_with_symbols_labels_df = (
features_with_symbols_labels_df.query(
f"signal_index == {signal_index}"
)[["segment_start", "segment_end", "segment_symbol"]]
)
for segment_index in range(len(signal_features_with_symbols_labels_df)):
a = signal_features_with_symbols_labels_df["segment_start"].iloc[
segment_index
]
b = signal_features_with_symbols_labels_df["segment_end"].iloc[
segment_index
]
symbol = signal_features_with_symbols_labels_df[
"segment_symbol"
].iloc[segment_index]
if specific_symbol is None:
chosen_color = list_colors[symbol]
if specific_symbol is not None:
if symbol == specific_symbol:
chosen_color = list_colors[symbol]
else:
chosen_color = "lightgray"
ax.axvspan(
xmin=a,
xmax=b,
ymin=signal_index / n_signals,
ymax=signal_index / n_signals + h,
facecolor=chosen_color,
edgecolor=edge_color_plot,
)
# Display the change in indexes
if change_indexes is not None and y_label is not None:
l_labels = sorted(list(set(y_label)))
for i, change_index in enumerate(change_indexes):
ax.axhline(
y=change_index, linestyle="--", linewidth=2, color="lightgray"
)
plt.text(
0,
change_index - 1,
l_labels[i],
fontsize=12,
color="r",
style="italic",
bbox={"facecolor": "white", "alpha": 0.7, "pad": 5},
)
plt.text(
0,
max(change_indexes) + 1,
l_labels[-1],
fontsize=12,
color="r",
style="italic",
bbox={"facecolor": "white", "alpha": 0.7, "pad": 5},
)
# Display the legends of the symbols
for i in range(n_symbols):
ax.axvspan(
xmin=0, xmax=0, ymin=0, ymax=0, facecolor=list_colors[i], label=i
)
# l_signal_indexes_mid = list()
# for elem in l_signal_indexes:
# l_signal_indexes_mid.append(elem+h/2)
# ax.set_yticks(l_signal_indexes_mid)
# ax.set_yticklabels(l_signal_indexes)
if is_display_legend:
if n_signals < 20:
tuple_anchor = (0.5, -1)
else:
tuple_anchor = (0.5, 0)
ax.legend(
loc="lower center",
bbox_to_anchor=tuple_anchor,
fancybox=True,
shadow=True,
ncol=n_symbols,
title="symbol",
)
# Title and labels
plt.margins(x=0)
plt.xlabel("timestamp")
plt.ylabel("signal index")
# plt.legend(title="symbol")
plt.title(
f"MASTRIDE with penaly factor {pen_factor} and alphabet size {n_symbols}",
loc="center",
)
create_path(Path(Path.cwd() / f"results/{date_exp}/plots/"))
if is_save:
plt.savefig(
f"results/{date_exp}/plots/mastride_colorbars_{data_source}_{dataset_name}_pen{pen_factor}_nsymb{n_symbols}.png",
dpi=200,
)
plt.show()
def plot_color_bar_final(
features_with_symbols_labels_df: pd.DataFrame,
is_savefig=False,
date_exp=None,
specific_symbol=None,
sampling_frequency=None,
is_display_legend: bool = False,
is_display_border: bool = False,
change_indexes: list = None,
y_label: list = None,
):
"""
Plot the color bars (symbolization) of several signals given the features and symbols per segment,
and the true signal class label.
"""
if is_display_border:
edge_color_plot = "black"
else:
edge_color_plot = None
# Get the smallest and largest signal indexes
bottom_signal = features_with_symbols_labels_df["signal_index"].min()
top_signal = features_with_symbols_labels_df["signal_index"].max()
# Do a translation of all signals indexes so that the first one is zero
if bottom_signal > 0:
features_with_symbols_labels_df["signal_index"] = (
features_with_symbols_labels_df["signal_index"].values
- features_with_symbols_labels_df["signal_index"].min()
)
# Get some meta data
n_signals = features_with_symbols_labels_df["signal_index"].nunique()
n_symbols = features_with_symbols_labels_df["segment_symbol"].max() + 1
n_samples = features_with_symbols_labels_df["segment_end"].max()
l_signal_indexes = sorted(
features_with_symbols_labels_df[["signal_index"]].drop_duplicates()[
"signal_index"
]
)
l_unique_symbols = sorted(
features_with_symbols_labels_df["segment_symbol"].unique()
)
h = 1 / n_signals
# list_colors = sns.color_palette("YlOrRd", n_colors=n_symbols)
list_colors = sns.color_palette("tab10", n_colors=n_symbols)
# Define the figure
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_ylim(bottom=bottom_signal, top=top_signal)
ax.invert_yaxis()
# Plot the color bars per segment per signal
for signal_index in l_signal_indexes:
signal_features_with_symbols_labels_df = (
features_with_symbols_labels_df.query(
f"signal_index == {signal_index}"
)[["segment_start", "segment_end", "segment_symbol"]]
)
for segment_index in range(len(signal_features_with_symbols_labels_df)):
a = signal_features_with_symbols_labels_df["segment_start"].iloc[
segment_index
]
b = signal_features_with_symbols_labels_df["segment_end"].iloc[
segment_index
]
symbol = signal_features_with_symbols_labels_df[
"segment_symbol"
].iloc[segment_index]
if specific_symbol is None:
chosen_color = list_colors[symbol]
if specific_symbol is not None:
if symbol == specific_symbol:
chosen_color = list_colors[symbol]
else:
chosen_color = "lightgray"
ax.axvspan(
xmin=a,
xmax=b,
ymin=signal_index / n_signals,
ymax=signal_index / n_signals + h,
facecolor=chosen_color,
edgecolor=edge_color_plot,
)
# Display the change in indexes
if change_indexes is not None and y_label is not None:
set_y_labels_sub = set(y_label)
l_labels = list()
for str_name in y_label:
if str_name not in l_labels:
l_labels.append(str_name)
if len(l_labels) == 3:
break
for i, change_index in enumerate(change_indexes):
ax.axhline(
y=change_index, linestyle="--", linewidth=2, color="lightgray"
)
plt.text(
0,
change_index - 1,
l_labels[i],
fontsize=12,
color="r",
style="italic",
bbox={"facecolor": "white", "alpha": 0.7, "pad": 5},
)
plt.text(
0,
max(change_indexes) + 1,
l_labels[-1],
fontsize=12,
color="r",
style="italic",
bbox={"facecolor": "white", "alpha": 0.7, "pad": 5},
)
# Display the legends of the symbols
for i in range(n_symbols):
ax.axvspan(
xmin=0, xmax=0, ymin=0, ymax=0, facecolor=list_colors[i], label=i
)
if is_display_legend:
ax.legend(
loc="upper center",
bbox_to_anchor=(0.5, -0.1),
fancybox=True,
shadow=True,
ncol=n_symbols,
title="Symbol",
)
if sampling_frequency is not None:
existing_xticks, existing_xticklabels = plt.xticks()
new_xticklabels = list(existing_xticks / sampling_frequency)
plt.gca().set_xticklabels(new_xticklabels)
plt.gca().set_yticklabels([])
# Title and labels
plt.tight_layout()
# plt.margins(x=0)
plt.xlabel("Time [sec]")
plt.ylabel("symbolic sequences")
create_path(Path(Path.cwd() / f"results/{date_exp}/img/"))
if is_savefig:
plt.savefig(f"results/{date_exp}/img/color_bars_sub60.png", dpi=200)
plt.show()
def plot_segment_symbol(
signal_index,
symbol,
features_with_symbols_labels_df,
list_of_univariate_gait_signals,
):
l_columns = [
"signal_index",
"segment_start",
"segment_end",
"segment_length",
"segment_symbol",
]
df_temp = features_with_symbols_labels_df.query(
f"signal_index == {signal_index} and segment_symbol == {symbol}"
)[l_columns]
display(df_temp)
plt.figure(figsize=(8, 2))
for i, (a, b) in enumerate(
zip(df_temp["segment_start"].tolist(), df_temp["segment_end"].tolist())
):
plt.plot(
list_of_univariate_gait_signals[signal_index][a:b], label=f"{i}"
)
plt.title(f"Symbol {symbol} of signal index {signal_index}")
plt.legend(title="segment index")
plt.show()
def hierarchical_clustering(
distance_matrix, distance_name, method="complete", labels=None
):
"""Source: https://towardsdatascience.com/how-to-apply-hierarchical-clustering-to-time-series-a5fe2a7d8447"""
if method == "complete":
Z = complete(distance_matrix)
if method == "single":
Z = single(distance_matrix)
if method == "average":
Z = average(distance_matrix)
if method == "ward":
Z = ward(distance_matrix)
fig = plt.figure(figsize=(8, 3))
if labels is not None:
dn = dendrogram(Z, labels=labels)
else:
dn = dendrogram(Z)
plt.title(f"Dendrogram for {method}-linkage with {distance_name} distance")
plt.xticks(rotation=90, ha="center")
plt.show()
return Z
def get_cluster_centers(b_dsymb, n_symbols):
# Get the segment features
segment_features_df = b_dsymb.features_with_symbols_labels_df.copy()
# Retrieve features
only_features_df = Symbolization.get_feat_df(
segment_features_df=segment_features_df
)
# Scale the features before the clustering
scaler = StandardScaler().fit(only_features_df)
scaled_features = scaler.transform(only_features_df)
scaled_features_df = pd.DataFrame(
scaled_features, columns=scaler.feature_names_in_
)
# Launch clustering
clustering_model_ = KMeans(
n_clusters=n_symbols, init="k-means++", n_init=10, random_state=0
).fit(scaled_features_df)
# Get the cluster centers
b_dsymb.scaled_cluster_centers = clustering_model_.cluster_centers_
# Transform the cluster centers into data frames
b_dsymb.scaled_cluster_centers_df = pd.DataFrame(
b_dsymb.scaled_cluster_centers,
columns=clustering_model_.feature_names_in_,
)
b_dsymb.unscaled_cluster_centers = scaler.inverse_transform(
b_dsymb.scaled_cluster_centers
)
b_dsymb.unscaled_cluster_centers_df = pd.DataFrame(
b_dsymb.unscaled_cluster_centers,
columns=clustering_model_.feature_names_in_,
)
b_dsymb.inertia = clustering_model_.inertia_
return b_dsymb
def plot_dendrogram(
distance_matrix,
is_savefig,
date_exp,
x_label="Distance between symbols",
y_label="Symbol",
):
Z = complete(distance_matrix)
fig = plt.figure(figsize=(6, 3))
dn = dendrogram(Z)
# plt.xticks(rotation=90, ha="center")
plt.ylabel(x_label)
plt.xlabel(y_label)
plt.tight_layout()
if is_savefig:
plt.savefig(f"results/{date_exp}/img/dendrogram_symbols.png", dpi=200)
plt.show()
def plot_power_spectral_density(
b_dsymb,
f=None,
n_symbols=None,
is_savefig=False,
date_exp="unknown"
):
"""Power Spectral Density"""
# rename the feature names
unscaled_cluster_centers_df_plot = (
b_dsymb.unscaled_cluster_centers_df.copy()
)
unscaled_cluster_centers_df_plot.columns = [
str(elem)
for elem in list(
np.arange(0, len(unscaled_cluster_centers_df_plot.columns))
)
]
list_symbols = list(np.arange(0, n_symbols, 1))
plt.figure(figsize=(6, 4))
for symbol in range(n_symbols):
if symbol in list_symbols:
if f is not None:
plt.plot(
f, # list of frequencies from the spectrogram
unscaled_cluster_centers_df_plot.iloc[symbol],
label=f"{symbol}",
)
else:
plt.plot(
unscaled_cluster_centers_df_plot.iloc[symbol],
label=f"{symbol}",
)
plt.legend(title="Symbol")
plt.xlabel("Frequency [Hz]")
plt.ylabel("Power Spectral Density")
plt.tight_layout()
plt.margins(x=0)
if is_savefig:
plt.savefig(
f"results/{date_exp}/img/spectral_density_freq.png", dpi=200
)
plt.show()
def array2df(arr):
"""converts a 2-dimensional NumPy array into a Pandas DataFrame,
excluding the diagonal elements
"""
# Get the row and column indices of the array
row_indices, col_indices = np.indices(arr.shape)
# Flatten the array, row indices, and column indices
flattened_arr = arr.flatten()
flattened_row_indices = row_indices.flatten()
flattened_col_indices = col_indices.flatten()
# Create a dictionary with the data for the DataFrame
data = {
"i": flattened_row_indices,
"j": flattened_col_indices,
"arr": flattened_arr,
}
# Create the DataFrame
df = pd.DataFrame(data)
# Return the DataFrame without the diagonal
return df.query("i != j")
def compute_silhouette_score(b_distance, df_metadata):
b_distance.silhouette_samples = silhouette_samples(
b_distance.distance_matrix,
labels=df_metadata["meta_label"],
metric="precomputed",
)
b_distance.silhouette_mean = np.mean(b_distance.silhouette_samples)
b_distance.silhouette_median = np.median(b_distance.silhouette_samples)
# not orthopedic, thus healthy and neurological (hn)
signal_indexes_hn = df_metadata.query("meta_label != 'orthopedic'")[
"signal_index"
].tolist()
b_distance.silhouette_samples_hn = silhouette_samples(
b_distance.distance_matrix[signal_indexes_hn][:, signal_indexes_hn],
labels=df_metadata.query("meta_label != 'orthopedic'")[
"meta_label"
].values,
metric="precomputed",
)
b_distance.silhouette_mean_hn = np.mean(b_distance.silhouette_samples_hn)
b_distance.silhouette_median_hn = np.median(
b_distance.silhouette_samples_hn
)
return b_distance
def get_nearest_neighbors(distance_matrix):
# Create an instance of NearestNeighbors with a lot of neighbors
neighbors_model = NearestNeighbors(
n_neighbors=distance_matrix.shape[0], metric="precomputed"
)
# Fit the distance matrix to the model
neighbors_model.fit(distance_matrix)
# Find the nearest neighbors for each signal
old_distances, old_indices = neighbors_model.kneighbors(
distance_matrix, return_distance=True
)
# Exclude itself from the nearest neighbors
list_indices = list()
list_distances = list()
for i in range(old_indices.shape[0]):
row_list_indices = list(old_indices[i, :])
row_list_distances = list(old_distances[i, :])
if i in row_list_indices:
# itself is in the nearest neighbors
position_self = row_list_indices.index(i)
row_list_indices.pop(position_self)
row_list_distances.pop(position_self)
else:
# itself is not in the nearest neighbors
del row_list_indices[-1]
del row_list_distances[-1]
list_indices.append(row_list_indices)
list_distances.append(row_list_distances)
indices = np.array(list_indices)
distances = np.array(list_distances)
assert indices.shape == distances.shape, "Shape error"
assert indices.shape == (
distance_matrix.shape[0],
distance_matrix.shape[1] - 1,
), "Shape error"
return distances, indices
def retrieve_opposing_foot(signal_index_query, df_metadata, is_print=False):
"""If signal_index_query is left, then we find the corresponding right foot index."""
recording_index = df_metadata.query(
f"signal_index == {signal_index_query}"
)["recording_index"].values[0]
df_metadata_query = df_metadata.query(
f"recording_index == {recording_index}"
)
if is_print:
display(df_metadata_query)
foot_query = df_metadata_query.query(
f"signal_index == {signal_index_query}"
)["foot"].values[0]
if foot_query == "left":
foot_retrieval = "right"
else:
foot_retrieval = "left"
signal_index_retrieval = df_metadata_query.query(
f"foot == '{foot_retrieval}'"
)["signal_index"].values[0]
return signal_index_retrieval
def get_rank_of_opposing_foot(
signal_index_query, indices, distances, df_metadata, is_print=False
):
"""What is the rank of the opposing foot as the nearest neighbor?"""
row_indices = list(indices[signal_index_query])
row_distances = list(distances[signal_index_query])
ranks = list(
rankdata(row_distances, method="min")
) # rank starts at 1 (and not 0)
signal_index_retrieval = retrieve_opposing_foot(
signal_index_query, df_metadata, is_print
)
position_opposing_foot = row_indices.index(signal_index_retrieval)
rank_opposing_foot = ranks[position_opposing_foot]
if is_print:
print(f"{row_indices = }")
print(f"{row_distances = }")
print(f"{ranks = }")
print(f"{signal_index_retrieval = }")
print(f"{rank_opposing_foot = }")
return rank_opposing_foot
def get_ranks_of_opposing_feet(distance_matrix, df_metadata):
"""Main function"""
list_ranks_of_opposing_feet = list()
distances, indices = get_nearest_neighbors(distance_matrix)
for signal_index_query in range(distance_matrix.shape[0]):
rank_opposing_foot = get_rank_of_opposing_foot(
signal_index_query=signal_index_query,
indices=indices,
distances=distances,
df_metadata=df_metadata,
is_print=False,
)
list_ranks_of_opposing_feet.append(rank_opposing_foot)
return list_ranks_of_opposing_feet
def get_list_ranks_of_opposing_focus(b_distance, signal_indexes_focus):
b_distance.list_ranks_of_opposing_focus = [
b_distance.list_ranks_of_opposing_feet[signal_index]
for signal_index in signal_indexes_focus
]
b_distance.list_ranks_of_opposing_focus_mean = np.mean(
b_distance.list_ranks_of_opposing_focus
)
return b_distance
def get_clustering_labels(b_distance, n_clusters, true_labels):
clustering_model = AgglomerativeClustering(
n_clusters=n_clusters,
linkage="ward",
connectivity=b_distance.distance_matrix,
)
clustering_model.fit(b_distance.distance_matrix)
b_distance.cluster_labels = clustering_model.labels_
b_distance.rand_score = rand_score(
labels_pred=b_distance.cluster_labels, labels_true=true_labels
)
b_distance.adjusted_rand_score = adjusted_rand_score(
labels_pred=b_distance.cluster_labels, labels_true=true_labels
)
b_distance.norm_MI_score = normalized_mutual_info_score(
labels_pred=b_distance.cluster_labels, labels_true=true_labels
)
return b_distance