-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml_cwt_interpret.py
1497 lines (1364 loc) · 52.4 KB
/
ml_cwt_interpret.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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import glob
import json
from datetime import datetime, timedelta
from pathlib import Path
from typing import List
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pycwt as wavelet
import typer
from skfeature.function.similarity_based import fisher_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from cwt._cwt import CWT, DWT
from model.data_loader import load_activity_data
from preprocessing.preprocessing import apply_preprocessing_steps
from scipy.stats import mannwhitneyu
def report_mannwhitney(array1, array2):
U1, p = mannwhitneyu(list(array1), list(array2), method="exact")
nx, ny = len(array1), len(array2)
U2 = nx * ny - U1
print(U2, U1, p)
res = f"p={p:.3f}"
print(res)
return res
def elemwise_cwt(X, preprocessing_steps, output_dir):
f_transform = CWT(
step_slug="_".join(preprocessing_steps),
wavelet_f0=6,
out_dir=output_dir,
n_scales=8,
sub_sample_scales=1,
enable_coi=False,
enable_graph_out=False,
)
X_cwt, _, _ = f_transform.transform(X)
cwt_list_0 = []
for cwt in X_cwt:
cwt_list_0.append(np.reshape(cwt, (f_transform.shape[0], f_transform.shape[1])))
coefs_class0_mean = np.mean(cwt_list_0, axis=0)
return coefs_class0_mean
def datetime_range(start, end, delta):
current = start
while current < end:
yield current
current += delta
def get_imp_stat_cat(
farmname,
preprocessing_steps,
date_list,
imp,
activity,
X_train,
output_dir,
n_peaks=1
):
# print(date_list)
HALF_PEAKLENGHT = 120
days_median = []
nights_median = []
days_std = []
nights_std = []
all_days = []
all_nights = []
idxs = []
for i in range(n_peaks):
start = i * HALF_PEAKLENGHT
end = start + HALF_PEAKLENGHT
a = activity[start:end]
imp_ = imp[start:end]
date = date_list[start:end]
mask = np.array([0 if x < len(a)/2 else 1 for x in range(len(a))])
light = np.zeros(mask.shape).astype(str)
light[mask == 0] = "before"
light[mask == 1] = "after"
day_imp = imp_[light == "before"]
night_imp = imp_[light == "after"]
all_days.extend(day_imp)
all_nights.extend(night_imp)
# report_mannwhitney(day_imp, night_imp)
std = [day_imp.std(), night_imp.std()]
mean = [day_imp.mean(), night_imp.mean()]
median = [np.median(day_imp), np.median(night_imp)]
days_median.append(np.median(day_imp))
nights_median.append(np.median(night_imp))
days_std.append(np.std(day_imp))
nights_std.append(np.std(night_imp))
index = f"Peak {i}"
idxs.append(index)
# dfs.append(df)
df = pd.DataFrame(
{
"before peak median": nights_median,
"after peak median": days_median,
"before peak std": nights_std,
"after peak std": days_std,
},
index=idxs,
)
# df = pd.concat(dfs)
fig_ = df.plot.barh(
rot=0, title="Feature importance for each", figsize=(6, 6)
).get_figure()
ax = fig_.gca()
for j in range(len(ax.get_yticklabels())):
ax.get_yticklabels()[j].set_weight("bold")
ax.set_xlabel("Median of feature importance")
ax.legend(loc="upper right")
# for j in range(n_activity_days*2):
# if j % 2 == 0:
# continue
# ax.get_yticklabels()[j].set_weight("bold")
filename = f"{-1}_per_{X_train.shape[1]}.png"
filepath = output_dir / filename
print(filepath)
fig_.tight_layout()
fig_.savefig(filepath)
# dot plot
plt.clf()
plt.cla()
df_ = pd.DataFrame({"before": days_median, "after": nights_median})
fig_box = df_.boxplot().get_figure()
ax = fig_box.gca()
ax.set_title(
f"Feature importance before vs after\n{report_mannwhitney(days_median, nights_median)}"
)
ax.set_ylabel("Mean of feature importance")
for i, d in enumerate(df_):
y = df_[d]
x = np.random.normal(i + 1, 0.04, len(y))
ax.plot(x, y, marker="o", linestyle="None", mfc="none")
steps = "_".join(preprocessing_steps).lower()
filename = f"{farmname}_{-1}_box_{X_train.shape[1]}_{steps}.png"
filepath = output_dir / filename
print(filepath)
fig_box.set_size_inches(4, 4)
fig_box.tight_layout()
fig_box.savefig(filepath)
mask = np.array([1 for _ in range(len(activity))])
cpt = 0
for n, v in enumerate(mask):
if cpt < HALF_PEAKLENGHT:
mask[n] = 0
if cpt > HALF_PEAKLENGHT*2:
cpt = 0
cpt+=1
light = np.zeros(mask.shape).astype(str)
light[mask == 0] = "before"
light[mask == 1] = "after"
fig_, ax_ = plt.subplots(figsize=(8, 8))
filename = f"{-1}_period_{X_train.shape[1]}.png"
filepath = output_dir / filename
print(filepath)
ax_.plot(activity)
ax_.plot(mask)
fig_.savefig(filepath)
day_imp = imp[light == "before"]
night_imp = imp[light == "after"]
std = [day_imp.std(), night_imp.std()]
mean = [day_imp.mean(), night_imp.mean()]
median = [np.median(day_imp), np.median(night_imp)]
index = ["before peak", "after peak"]
df = pd.DataFrame({"Std": std, "Mean": mean, "Median": median}, index=index)
fig_ = df.plot.barh(rot=0, title="Feature importance", figsize=(8, 8)).get_figure()
ax = fig_.gca()
ax.set_xlabel("Feature importance")
filename = f"{farmname}_{-1}_{X_train.shape[1]}_{steps}.png"
filepath = output_dir / filename
print(filepath)
fig_.tight_layout()
fig_.savefig(filepath)
def get_imp_stat(
farmname,
preprocessing_steps,
date_list,
imp,
activity,
n_activity_days,
X_train,
output_dir,
sunrise_max,
sunset_min,
sunrise_min,
sunset_max,
):
# print(date_list)
dfs = []
days_median = []
nights_median = []
days_std = []
nights_std = []
all_days = []
all_nights = []
idxs = []
for i in range(n_activity_days):
start = i * 1440
end = start + 1440
a = activity[start:end]
imp_ = imp[start:end]
date = date_list[start:end]
date = pd.to_datetime(date).time
m1 = np.array([1 if x >= sunrise_max and x < sunset_min else 0 for x in date])
m2 = np.array([1 if x >= sunrise_min and x < sunset_max else 0 for x in date])
mask = m1 + m2
light = np.zeros(mask.shape).astype(str)
light[mask == 0] = "night"
light[mask == 1] = "transit"
light[mask == 2] = "day"
day_imp = imp_[light == "day"]
night_imp = imp_[light == "night"]
all_days.extend(day_imp)
all_nights.extend(night_imp)
# report_mannwhitney(day_imp, night_imp)
std = [day_imp.std(), night_imp.std()]
mean = [day_imp.mean(), night_imp.mean()]
median = [np.median(day_imp), np.median(night_imp)]
days_median.append(np.median(day_imp))
nights_median.append(np.median(night_imp))
days_std.append(np.std(day_imp))
nights_std.append(np.std(night_imp))
index = f"FTest day + {abs(i - n_activity_days)}"
idxs.append(index)
# dfs.append(df)
df = pd.DataFrame(
{
"Night median": nights_median,
"Day median": days_median,
"Night std": nights_std,
"Day std": days_std,
},
index=idxs,
)
# df = pd.concat(dfs)
fig_ = df.plot.barh(
rot=0, title="Feature importance for each day"
).get_figure()
ax = fig_.gca()
for j in range(len(ax.get_yticklabels())):
ax.get_yticklabels()[j].set_weight("bold")
ax.set_xlabel("Median of feature importance")
ax.legend(loc="upper right")
# for j in range(n_activity_days*2):
# if j % 2 == 0:
# continue
# ax.get_yticklabels()[j].set_weight("bold")
filename = f"{n_activity_days}_per_day_night_{X_train.shape[1]}.png"
filepath = output_dir / filename
print(filepath)
fig_.tight_layout()
fig_.savefig(filepath)
# dot plot
plt.clf()
plt.cla()
df_ = pd.DataFrame({"Daytime": days_median, "Nighttime": nights_median})
fig_box = df_.boxplot().get_figure()
ax = fig_box.gca()
ax.set_title(
f"Feature importance Day vs Night\n{report_mannwhitney(days_median, nights_median)}"
)
ax.set_ylabel("Mean of feature importance")
for i, d in enumerate(df_):
y = df_[d]
x = np.random.normal(i + 1, 0.04, len(y))
ax.plot(x, y, marker="o", linestyle="None", mfc="none")
steps = "_".join(preprocessing_steps).lower()
filename = f"{farmname}_{n_activity_days}_box_day_night_{X_train.shape[1]}_{steps}.png"
filepath = output_dir / filename
print(filepath)
fig_box.set_size_inches(4, 4)
fig_box.tight_layout()
fig_box.savefig(filepath)
dates = np.array([x.time() for x in date_list])
m1 = np.array([1 if x >= sunrise_max and x < sunset_min else 0 for x in dates])
m2 = np.array([1 if x >= sunrise_min and x < sunset_max else 0 for x in dates])
mask = m1 + m2
light = np.zeros(mask.shape).astype(str)
light[mask == 0] = "night"
light[mask == 1] = "transit"
light[mask == 2] = "day"
fig_, ax_ = plt.subplots(figsize=(8, 8))
filename = f"{n_activity_days}_period_{X_train.shape[1]}.png"
filepath = output_dir / filename
print(filepath)
ax_.plot(activity)
ax_.plot(mask)
fig_.savefig(filepath)
day_imp = imp[light == "day"]
night_imp = imp[light == "night"]
std = [day_imp.std(), night_imp.std()]
mean = [day_imp.mean(), night_imp.mean()]
median = [np.median(day_imp), np.median(night_imp)]
max = [np.max(day_imp), np.max(night_imp)]
min = [np.min(day_imp), np.min(night_imp)]
index = ["Daytime", "Nighttime"]
df = pd.DataFrame({"Std": std, "Mean": mean, "Median": median}, index=index)
fig_ = df.plot.barh(rot=0, title="Feature importance", figsize=(8, 8)).get_figure()
ax = fig_.gca()
ax.set_xlabel("Feature importance")
filename = f"{farmname}_{n_activity_days}_day_night_{X_train.shape[1]}_{steps}.png"
filepath = output_dir / filename
print(filepath)
fig_.tight_layout()
fig_.savefig(filepath)
def main(
output_dir: Path = typer.Option(
..., exists=False, file_okay=False, dir_okay=True, resolve_path=True
),
dataset_folder: Path = typer.Option(
..., exists=True, file_okay=False, dir_okay=True, resolve_path=True
),
class_healthy_label: List[str] = ["1To1"],
class_unhealthy_label: List[str] = ["2To2"],
preprocessing_steps: List[str] = ["None"],
n_activity_days: int = 7,
n_imputed_days: int = 7,
meta_columns: List[str] = [
"label",
"id",
"imputed_days",
"date",
"health",
"target",
],
meta_col_str: List[str] = ["health", "label", "date"],
r_avg: int = 60,
prct: int = 90,
_size: int = 3,
transform: str = "cwt",
enable_graph_out: bool = True,
# distance: bool = True,
random_forest: bool = True,
individual_to_ignore: List[str] = [],
sampling: str = "T",
resolution: float = None,
plot_high_dimension_db: bool = False,
p: bool = typer.Option(False, "--p"),
farmname: str = '',
sunrise_max: int = 9,
sunset_min: int =17,
sunrise_min: int =9,
sunset_max: int =17,
width: int = 11,
height: int = 5,
n_peaks=1
):
"""This script builds the graphs for cwt interpretation\n
Args:\n
output_dir: Output directory
dataset_folder: Dataset input directory
class_healthy: Label for healthy class
class_unhealthy: Label for unhealthy class
p: analyse famacha impact over time up to test date
"""
files = glob.glob(str(dataset_folder / "*.csv")) # find datset files
print("found %d files." % len(files))
print(files)
for file in files:
# days, farm_id, option, sampling = parse_param_from_filename(file)
print(f"loading dataset file {file} ...")
(
data_frame,
meta_data,
meta_data_short,
_,
_,
label_series,
samples,
_,
) = load_activity_data(
output_dir,
meta_columns,
file,
n_activity_days,
class_healthy_label,
class_unhealthy_label,
imputed_days=n_imputed_days,
preprocessing_steps=preprocessing_steps,
meta_cols_str=meta_col_str,
individual_to_ignore=individual_to_ignore,
sampling=sampling,
resolution=resolution,
)
print(data_frame)
data_frame_time, _, _ = apply_preprocessing_steps(
meta_columns,
None,
None,
None,
None,
None,
None,
None,
None,
None,
data_frame.copy(),
output_dir,
preprocessing_steps,
class_healthy_label,
class_unhealthy_label,
clf_name="SVM",
n_scales=None,
farm_name="FARMS",
keep_meta=False,
)
print(data_frame_time)
data_frame_time_o, _, _ = apply_preprocessing_steps(
meta_columns,
None,
None,
None,
None,
None,
None,
None,
None,
None,
data_frame.copy(),
output_dir,
["QN", "ANSCOMBE", "LOG"],
class_healthy_label,
class_unhealthy_label,
clf_name="SVM",
n_scales=None,
farm_name="FARMS",
keep_meta=False,
)
base = datetime(day=1, month=1, year=2022, hour=0, minute=0, second=0)
xaxis = [base - timedelta(minutes=x) for x in range(1440 * n_activity_days)]
# for n_activity_days in range(1, n_activity_days):
print(n_activity_days)
meta_ = meta_data_short[data_frame_time["health"].isin([0, 1])]
data_frame_time = data_frame_time.loc[data_frame_time["health"].isin([0, 1])]
data_frame = data_frame.loc[data_frame["health"].isin([0, 1])]
X_train_o, y_train_o = (
data_frame_time_o.iloc[:, :-2].iloc[:, ::-1].values,
data_frame_time_o["health"].values,
)
if n_activity_days > 0:
X_train, y_train = (
data_frame_time.iloc[:, :-2].iloc[:, ::-1].values,
data_frame_time["health"].values,
)
else:
# for cats peak samples
X_train, y_train = (
data_frame_time.iloc[:, :-2].values,
data_frame_time["health"].values,
)
n_activity_days = 1
clf = SVC(kernel="linear", probability=True)
# clf = LinearRegression()
clf = LogisticRegression(n_jobs=-1)
if random_forest:
clf = RandomForestClassifier(random_state=0)
print("fit...")
#fisher_s = fisher_score.fisher_score(X_train, y_train)
clf.fit(X_train, y_train)
if plot_high_dimension_db:
plot_high_dimension_db(
output_dir / "training",
X_train,
y_train,
None,
meta_,
clf,
n_activity_days,
preprocessing_steps,
0,
)
if random_forest:
imp = clf.feature_importances_
else:
imp = abs(clf.coef_[0])
y = clf.decision_function(X_train)
w_norm = np.linalg.norm(clf.coef_)
dist = abs(y / w_norm)
df_dist = pd.DataFrame(X_train)
df_dist["y"] = y_train
df_dist["dist"] = dist
df_dist = df_dist.sort_values("dist")
df_dist_healthy = df_dist[df_dist["y"] == 0]
df_dist_unhealthy = df_dist[df_dist["y"] == 1]
d_m = max([df_dist_healthy.shape[0], df_dist_unhealthy.shape[0]])
r = int(np.ceil(d_m / _size))
fig, axs = plt.subplots(_size, 4, facecolor="white", figsize=(42.80, 12.80))
cpt = 0
min_a = []
max_a = []
for i in range(0, d_m, r):
end = int(i + r)
start = i
d_h = df_dist_healthy[start:end]
d_uh = df_dist_unhealthy[start:end]
activity_h = np.mean(d_h.iloc[:, :-2]).values
activity_uh = np.mean(d_uh.iloc[:, :-2]).values
min_a.append(np.min(activity_h))
min_a.append(np.min(activity_uh))
max_a.append(np.max(activity_h))
max_a.append(np.max(activity_uh))
min_a = np.min(min_a)
max_a = np.max(max_a)
for i in range(0, d_m, r):
end = int(i + r)
start = i
d = df_dist[start:end]
min_d = d["dist"].values[0]
max_d = d["dist"].values[-1]
d_h = df_dist_healthy[start:end]
d_uh = df_dist_unhealthy[start:end]
activity_h = np.mean(d_h.iloc[:, :-2]).values
activity_uh = np.mean(d_uh.iloc[:, :-2]).values
cwt_h = elemwise_cwt(
d_h.iloc[:, :-2].values, preprocessing_steps, output_dir
)
cwt_uh = elemwise_cwt(
d_uh.iloc[:, :-2].values, preprocessing_steps, output_dir
)
if i == 0:
mat_max = max([np.nanmax(cwt_h), np.nanmax(cwt_uh)])
mat_min = min([np.nanmin(cwt_h), np.nanmin(cwt_uh)])
axs[cpt, 0].plot(activity_h)
axs[cpt, 0].set_title(
f"Healthy samples distance range [{min_d:.3f} {max_d:.3f}] {d_h.iloc[:, :-2].shape}"
)
axs[cpt, 0].set_xlabel("Time")
axs[cpt, 0].set_ylabel("Activity")
axs[cpt, 0].set_ylim([min_a, max_a])
axs[cpt, 1].plot(activity_uh)
axs[cpt, 1].set_title(
f"Unhealthy samples distance range [{min_d:.3f} {max_d:.3f} {d_uh.iloc[:, :-2].shape}]"
)
axs[cpt, 1].set_xlabel("Time")
axs[cpt, 1].set_ylabel("Activity")
axs[cpt, 1].set_ylim([min_a, max_a])
origin = "upper"
im = axs[cpt, 2].imshow(
cwt_h,
origin=origin,
extent=[0, len(activity_h), 1, cwt_h.shape[0]],
interpolation="nearest",
aspect="auto",
vmin=mat_min,
vmax=mat_max,
)
axs[cpt, 2].set_title(f"Element wise mean of cwt coefficients healthy")
axs[cpt, 2].set_xlabel("Time")
axs[cpt, 2].set_ylabel("Scales")
fig.colorbar(im, ax=axs[cpt, 2])
im = axs[cpt, 3].imshow(
cwt_uh,
origin=origin,
extent=[0, len(activity_h), 1, cwt_h.shape[0]],
interpolation="nearest",
aspect="auto",
vmin=mat_min,
vmax=mat_max,
)
axs[cpt, 3].set_title(f"Element wise mean of cwt coefficients unhealthy")
axs[cpt, 3].set_xlabel("Time")
axs[cpt, 3].set_ylabel("Scales")
fig.colorbar(im, ax=axs[cpt, 3])
cpt += 1
filename = (
f"{n_activity_days}_{transform}_{r}_distance_from_db_{X_train.shape[1]}.png"
)
filepath = output_dir / filename
fig.tight_layout()
print(filepath)
fig.savefig(filepath)
intercept = clf.intercept_
mean_time = np.mean(X_train, axis=0)
mean_time_o = np.mean(X_train_o, axis=0)
date_list = list(range(0, len(mean_time)))
if farmname != 'cats':
date_list = [
datetime(2016, 9, 1, 0) + timedelta(minutes=1 * x)
for x in range(0, len(mean_time))
]
get_imp_stat(
farmname,
preprocessing_steps,
date_list,
imp,
mean_time_o,
n_activity_days,
X_train,
output_dir,
sunrise_max,
sunset_min,
sunrise_min,
sunset_max,
)
else:
get_imp_stat_cat(
farmname,
preprocessing_steps,
date_list,
imp,
mean_time_o,
X_train,
output_dir,
n_peaks=n_peaks
)
fig, ax = plt.subplots(figsize=(width, height))
ax2 = ax.twinx()
ax.plot(
date_list,
mean_time,
label=f"mean activity of all samples({class_healthy_label}, {class_unhealthy_label}) after {preprocessing_steps}",
)
# ax.plot(date_list, mean_time_o, label=f"mean activity of all samples({class_healthy_label}, {class_unhealthy_label}) after {['QN', 'ANSCOMBE', 'LOG']}")
# ax.plot(imp*mean, label="mean activity of all samples * feature importance")
# ax2.plot(date_list, imp, color="red", label="weight", alpha=0.3)
# ax2.plot(date_list, fisher_s, color="green", label="Fisher score", alpha=0.3)
# ax2.plot(date_list, intercept, color="purple", label="intercept", alpha=0.3)
df_imp = pd.DataFrame(imp, columns=["imp"])
roll_avg = df_imp.imp.rolling(r_avg).mean()
n_b = len(roll_avg)
roll_avg = roll_avg.dropna()
n_a = len(roll_avg)
pad = int(np.ceil((n_b - n_a)/2))
roll_avg = [np.nan]*pad + roll_avg.to_list() + [np.nan]*pad
roll_avg = np.array(roll_avg[0:len(date_list)])
ax2.plot(
date_list,
roll_avg,
color="black",
label=f"feature importance rolling avg ({r_avg} points)",
alpha=0.9,
)
if farmname != 'cats':
# ax.axvline(sunrise_max, color="r", ls="--")
# ax.axvline(sunset_min, color="r", ls="--")
#
# ax.axvline(sunrise_min, color="b", ls="--")
# ax.axvline(sunset_max, color="b", ls="--")
for item in date_list:
if (
item.hour == sunrise_max.hour and item.minute == sunrise_max.minute
) or (item.hour == sunset_min.hour and item.minute == sunset_min.minute):
ax.axvline(item, color="r", ls="--")
if (item.hour == sunrise_min.hour and item.minute == sunrise_min.minute) or (
item.hour == sunset_max.hour and item.minute == sunset_max.minute
):
ax.axvline(item, color="b", ls="--")
# ax2.axvline(date_list[720], color='r', ls='--')
else:
for item in date_list:
if item % 240 == 0:
if item == 0:
continue
ax.axvline(item, color="r", ls="--")
ax.legend(loc="upper left")
ax2.legend(loc="upper right")
ax.set_title(
f"Feature importance {type(clf).__name__} w={n_activity_days} \n daytime(max:{sunrise_max},min:{sunset_min}) nightime(max:{sunset_max},min:{sunrise_min})"
)
ax.set_xlabel("Time")
ax.set_ylabel("Activity")
ax2.set_ylabel("abs(Coefficients)", color="red")
filename = f"{n_activity_days}_feature_importance_{X_train.shape[1]}.png"
filepath = output_dir / filename
print(filepath)
if farmname != 'cats':
T = 60 * 1
ax2.xaxis.set_major_formatter(mdates.DateFormatter("%dT%H"))
#ax2.xaxis.set_major_locator(mdates.MinuteLocator(interval=T * n_activity_days))
ax.xaxis.set_major_formatter(mdates.DateFormatter("%dT%H"))
#ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=T * n_activity_days))
fig.autofmt_xdate()
fig.savefig(filepath)
if transform == "dwt":
f_transform = DWT(
dwt_window="coif1",
step_slug="_".join(preprocessing_steps),
out_dir=output_dir,
enable_graph_out=enable_graph_out,
)
_, X_train = f_transform.transform(X_train)
if transform == "cwt":
f_transform = CWT(
step_slug="_".join(preprocessing_steps),
wavelet_f0=6,
out_dir=output_dir,
n_scales=8,
sub_sample_scales=1,
enable_coi=False,
enable_graph_out=enable_graph_out,
)
X_train, _, _ = f_transform.transform(X_train)
X_train_o = X_train.copy()
X_train[np.isnan(X_train)] = -1
# scales = CWT_Transform.get_scales()
clf = SVC(kernel="linear", probability=True)
# clf = LinearRegression()
if random_forest:
clf = RandomForestClassifier(random_state=0)
print("fit...")
clf.fit(X_train, y_train)
if random_forest:
imp = clf.feature_importances_
else:
imp = abs(clf.coef_[0])
# if distance:
# y = clf.decision_function(X_train)
# w_norm = np.linalg.norm(clf.coef_)
# dist = y / w_norm
# imp = dist
#
intercept = clf.intercept_
imp_top_n_perct = imp.copy()
imp_top_n_perct[
imp_top_n_perct <= np.percentile(imp_top_n_perct, prct)
] = np.nan
mean_ = np.mean(X_train, axis=0)
cwt_0 = X_train[y_train == 0]
cwt_1 = X_train[y_train == 1]
cwt_list_0 = []
for cwt in cwt_0:
cwt_list_0.append(
np.reshape(cwt, (f_transform.shape[0], f_transform.shape[1]))
)
cwt_list_1 = []
for cwt in cwt_1:
cwt_list_1.append(
np.reshape(cwt, (f_transform.shape[0], f_transform.shape[1]))
)
coefs_class0_mean = np.mean(cwt_list_0, axis=0)
coefs_class1_mean = np.mean(cwt_list_1, axis=0)
cwt_imp = np.reshape(imp, (f_transform.shape[0], f_transform.shape[1]))
cwt_imp_top = np.reshape(
imp_top_n_perct, (f_transform.shape[0], f_transform.shape[1])
)
fig, axs = plt.subplots(3, 1, facecolor="white", figsize=(12.80, 18.80))
axs = axs.ravel()
fig, ax = plt.subplots(figsize=(12.80, 7.20))
ax2 = ax.twinx()
ax.plot(mean_, label=f"mean {transform}(flatten) of all samples")
# ax.plot(imp*mean, label="mean activity of all samples * feature importance")
ax2.plot(imp, color="red", label="feature importance", alpha=0.3)
df_imp = pd.DataFrame(imp, columns=["imp"])
roll_avg = df_imp.imp.rolling(1000).mean()
ax2.plot(
roll_avg,
color="black",
label=f"feature importance rolling avg ({1000} points)",
alpha=0.9,
)
ax.legend(loc="upper left")
ax2.legend(loc="upper right")
ax.set_title(f"Feature importance {type(clf).__name__} days={n_activity_days}")
ax.set_xlabel(f"{transform} (features)")
ax.set_ylabel("Activity")
ax2.set_ylabel("abs(Coefficients)", color="red")
filename = (
f"{n_activity_days}_{transform}_feature_importance_{X_train.shape[1]}.png"
)
filepath = output_dir / filename
print(filepath)
fig.savefig(filepath)
cwt_0 = X_train[y_train == 0]
cwt_1 = X_train[y_train == 1]
cwt_list_0 = []
for cwt in cwt_0:
# iwave_test = wavelet.icwt(np.reshape(cwt, (f_transform.shape[0], f_transform.shape[1])), f_transform.scales, f_transform.delta_t,
# wavelet=f_transform.wavelet_type.lower()).real
# plt.plot(iwave_test)
# plt.show()
cwt_list_0.append(
np.reshape(cwt, (f_transform.shape[0], f_transform.shape[1]))
)
cwt_list_1 = []
for cwt in cwt_1:
cwt_list_1.append(
np.reshape(cwt, (f_transform.shape[0], f_transform.shape[1]))
)
coi_mask = np.reshape(
X_train_o[0], (f_transform.shape[0], f_transform.shape[1])
)
coefs_class0_mean = np.mean(cwt_list_0, axis=0)
coefs_class0_mean[np.isnan(coi_mask)] = np.nan
coefs_class1_mean = np.mean(cwt_list_1, axis=0)
coefs_class1_mean[np.isnan(coi_mask)] = np.nan
cwt_imp = np.reshape(imp, (f_transform.shape[0], f_transform.shape[1]))
# cwt_intercept = np.reshape(intercept, (f_transform.shape[0], f_transform.shape[1]))
# cwt_imp[np.isnan(coi_mask)] = np.nan
cwt_imp_top = np.reshape(
imp_top_n_perct, (f_transform.shape[0], f_transform.shape[1])
)
cwt_imp_top[np.isnan(coi_mask)] = np.nan
fig, axs = plt.subplots(5, 2, facecolor="white", figsize=(28.60, 26.80))
origin = "upper"
if transform == "dwt":
fig, axs = plt.subplots(3, 2, facecolor="white", figsize=(28.60, 12.80))
origin = "lower"
axs = axs.ravel()
# axs[0].pcolormesh(
# np.arange(coefs_class0_mean.shape[1]),
# scales,
# coefs_class0_mean,
# cmap="viridis"
# )
mat_max = max([np.nanmax(coefs_class0_mean), np.nanmax(coefs_class1_mean)])
mat_min = min([np.nanmin(coefs_class0_mean), np.nanmin(coefs_class1_mean)])
date_list = mdates.date2num(date_list)
im = axs[0].imshow(
coefs_class0_mean,
origin=origin,
extent=[date_list[0], date_list[-1], 1, coefs_class0_mean.shape[0]],
interpolation="nearest",
aspect="auto",
vmin=mat_min,
vmax=mat_max,
)
fig.colorbar(im, ax=axs[0])
if farmname != 'cats':
date_format = "%dT%H"
if n_activity_days < 0:
date_format = "00:%H"
axs[0].xaxis.set_major_formatter(mdates.DateFormatter(date_format))
axs[0].xaxis.set_major_locator(
mdates.MinuteLocator(interval=T * n_activity_days)
)
axs[0].set_title(f"Element wise mean of {transform} coefficients healthy")
axs[0].set_xlabel("Time")
axs[0].set_ylabel("Scales")
im = axs[1].imshow(
coefs_class1_mean,
origin=origin,
extent=[date_list[0], date_list[-1], 1, coefs_class0_mean.shape[0]],
interpolation="nearest",
aspect="auto",
vmin=mat_min,
vmax=mat_max,
)
fig.colorbar(im, ax=axs[1])
if farmname != 'cats':
axs[1].xaxis.set_major_formatter(mdates.DateFormatter(date_format))
axs[1].xaxis.set_major_locator(
mdates.MinuteLocator(interval=T * n_activity_days)
)
axs[1].set_title(f"Element wise mean of {transform} coefficients unhealthy")
axs[1].set_xlabel("Time")
axs[1].set_ylabel("Scales")
mat_max = max([np.nanmax(cwt_imp), np.nanmax(cwt_imp_top)])
mat_min = min([np.nanmin(cwt_imp), np.nanmin(cwt_imp_top)])
im = axs[2].imshow(
cwt_imp,
origin=origin,
extent=[date_list[0], date_list[-1], 1, coefs_class0_mean.shape[0]],
interpolation="nearest",
aspect="auto",
vmin=mat_min,
vmax=mat_max,
)
fig.colorbar(im, ax=axs[2])
if farmname != 'cats':
axs[2].xaxis.set_major_formatter(mdates.DateFormatter(date_format))
axs[2].xaxis.set_major_locator(
mdates.MinuteLocator(interval=T * n_activity_days)
)
axs[2].set_title(f"{transform} Features importance {type(clf).__name__}")
axs[2].set_xlabel("Time")
axs[2].set_ylabel("Scales")
im = axs[3].imshow(
cwt_imp_top,
origin=origin,
extent=[date_list[0], date_list[-1], 1, coefs_class0_mean.shape[0]],
interpolation="nearest",
aspect="auto",
vmin=mat_min,
vmax=mat_max,
)
fig.colorbar(im, ax=axs[3])
if farmname != 'cats':
axs[3].xaxis.set_major_formatter(mdates.DateFormatter(date_format))
axs[3].xaxis.set_major_locator(
mdates.MinuteLocator(interval=T * n_activity_days)
)
axs[3].set_title(
f"{transform} Features importance top 10% {type(clf).__name__} days={n_activity_days}"
)
axs[3].set_xlabel("Time")
axs[3].set_ylabel("Scales")
a = (cwt_imp * coefs_class0_mean) - intercept
b = (cwt_imp * coefs_class1_mean) - intercept
# if distance:
# a = (cwt_imp * coefs_class0_mean)
# b = (cwt_imp * coefs_class1_mean)
mat_max = max([np.nanmax(a), np.nanmax(b)])