Skip to content

Commit b8a042b

Browse files
axelohwQvaale
authored andcommitted
# This is a combination of 23 commits.
# This is the 1st commit message: feat: possibility to specify target dim # The commit message ML4ITS#2 will be skipped: # feat: possibility to specify target dim # The commit message ML4ITS#3 will be skipped: # feat: possibility to specify target dim # The commit message ML4ITS#4 will be skipped: # feat: possibility to specify target dim # The commit message ML4ITS#5 will be skipped: # feat: possibility to specify target dim # The commit message ML4ITS#6 will be skipped: # feat: possibility to specify target dim # The commit message ML4ITS#7 will be skipped: # feat: possibility to specify target dim # The commit message ML4ITS#8 will be skipped: # feat: possibility to specify target dim # The commit message ML4ITS#9 will be skipped: # feat: possibility to specify target dim # The commit message ML4ITS#10 will be skipped: # fix # The commit message ML4ITS#11 will be skipped: # fix # The commit message ML4ITS#12 will be skipped: # feat: writing results to txt file # The commit message ML4ITS#13 will be skipped: # feat: writing results to txt file # The commit message ML4ITS#14 will be skipped: # .. # The commit message ML4ITS#15 will be skipped: # .. # The commit message ML4ITS#16 will be skipped: # .. # The commit message ML4ITS#17 will be skipped: # trying new anomaly score # The commit message ML4ITS#18 will be skipped: # trying new anomaly score # The commit message ML4ITS#19 will be skipped: # trying new anomaly score # The commit message ML4ITS#20 will be skipped: # trying new anomaly score # The commit message ML4ITS#21 will be skipped: # fix # The commit message ML4ITS#22 will be skipped: # results from all experiments, plotting, ++ # The commit message ML4ITS#23 will be skipped: # added plotter class and jupyter notebook file to visualize results
1 parent ca6bf70 commit b8a042b

File tree

135 files changed

+239178
-218
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+239178
-218
lines changed

.gitignore

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22
__pycache__/
33
.idea/
44
.DS_Store
5-
datasets/ecg_data.txt
5+
datasets/data/
66
datasets/ServerMachineDataset/processed/
7-
/datasets/data/processed/
8-
/datasets/data/test/
9-
/datasets/data/train/
10-
eda/.ipynb_checkpoints/
7+
visualization/.ipynb_checkpoints/
118
plots/
129
*.pkl
1310
models/
14-
output/smd/*/logs
15-
output/smd/*/preds.pkl
16-
output/smd/*/anomaly_preds.pkl
11+
output/

args.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import argparse
22

33

4+
def str2bool(v):
5+
if isinstance(v, bool):
6+
return v
7+
if v.lower() in ("yes", "true", "t", "y", "1"):
8+
return True
9+
elif v.lower() in ("no", "false", "f", "n", "0"):
10+
return False
11+
else:
12+
raise argparse.ArgumentTypeError("Boolean value expected.")
13+
14+
415
def get_parser():
516
parser = argparse.ArgumentParser()
617

718
# Data params
819
parser.add_argument("--dataset", type=str.upper, default="SMD")
9-
parser.add_argument(
10-
"--group",
11-
type=str,
12-
default="1-1",
13-
help="Required for smd dataset. <group_index>-<index>",
14-
)
20+
parser.add_argument("--group", type=str, default="1-1", help="Required for SMD dataset. <group_index>-<index>")
1521
parser.add_argument("--lookback", type=int, default=100)
1622
parser.add_argument("--horizon", type=int, default=1)
17-
parser.add_argument("--target_col", type=int, default=None)
23+
parser.add_argument("--do_preprocess", type=str2bool, default=False)
1824

1925
# Model params
2026
parser.add_argument("--kernel_size", type=int, default=7)
@@ -31,27 +37,19 @@ def get_parser():
3137
parser.add_argument("--bs", type=int, default=256)
3238
parser.add_argument("--init_lr", type=float, default=1e-3)
3339
parser.add_argument("--val_split", type=float, default=0.1)
34-
parser.add_argument("--shuffle_dataset", type=bool, default=True)
40+
parser.add_argument("--shuffle_dataset", type=str2bool, default=True)
3541
parser.add_argument("--dropout", type=float, default=0.3)
36-
parser.add_argument("--use_cuda", type=bool, default=True)
42+
parser.add_argument("--use_cuda", type=str2bool, default=True)
3743
parser.add_argument("--model_path", type=str, default="models")
3844
parser.add_argument("--print_every", type=int, default=1)
3945

4046
# Predictor args
41-
parser.add_argument(
42-
"--save_scores",
43-
type=bool,
44-
default=True,
45-
help="To save anomaly scores predicted.",
46-
)
47-
parser.add_argument(
48-
"--load_scores",
49-
type=bool,
50-
default=False,
51-
help="To use already computed anomaly scores",
52-
)
47+
parser.add_argument("--save_scores", type=str2bool, default=True, help="To save anomaly scores predicted.")
48+
parser.add_argument("--load_scores", type=str2bool, default=False, help="To use already computed anomaly scores")
5349
parser.add_argument("--gamma", type=float, default=1)
5450
parser.add_argument("--level", type=float, default=None)
51+
parser.add_argument("--q", type=float, default=1e-3)
52+
parser.add_argument("--use_mov_av", type=str2bool, default=False)
5553

5654
# Other
5755
parser.add_argument("--comment", type=str, default="")

bash_scripts/copy_files_from_ssh.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
port=11726
4+
5+
6+
scp -P $port -r $ssh:~/MTAD-GAT/models ./
7+
8+
for i in {1..8}
9+
do
10+
scp -P $port -r $ssh:~/MTAD-GAT/output/SMD/1-$i ./output/SMD/
11+
done
12+
13+
for i in {1..9}
14+
do
15+
scp -P $port -r $ssh:~/MTAD-GAT/output/SMD/2-$i ./output/SMD/
16+
done
17+
18+
for i in {1..11}
19+
do
20+
scp -P $port -r $ssh:~/MTAD-GAT/output/SMD/3-$i ./output/SMD/
21+
done

bash_scripts/predict_smd.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
python predict.py --dataset SMD --group 1-1 --model 17032021_010924 --load_scores True
4+
python predict.py --dataset SMD --group 1-2 --model 17032021_012546 --load_scores True
5+
python predict.py --dataset SMD --group 1-3 --model 17032021_013914 --load_scores True
6+
python predict.py --dataset SMD --group 1-4 --model 17032021_015244 --load_scores True
7+
python predict.py --dataset SMD --group 1-5 --model 17032021_020613 --load_scores True
8+
python predict.py --dataset SMD --group 1-6 --model 17032021_021942 --load_scores True
9+
python predict.py --dataset SMD --group 1-7 --model 17032021_023323 --load_scores True
10+
python predict.py --dataset SMD --group 1-8 --model 17032021_024657 --load_scores True
11+
python predict.py --dataset SMD --group 2-1 --model 17032021_030025 --load_scores True
12+
python predict.py --dataset SMD --group 2-2 --model 17032021_031410 --load_scores True
13+
python predict.py --dataset SMD --group 2-3 --model 17032021_032905 --load_scores True
14+
python predict.py --dataset SMD --group 2-4 --model 17032021_034448 --load_scores True
15+
python predict.py --dataset SMD --group 2-5 --model 17032021_035857 --load_scores True
16+
python predict.py --dataset SMD --group 2-6 --model 17032021_041234 --load_scores True
17+
python predict.py --dataset SMD --group 2-7 --model 17032021_042906 --load_scores True
18+
python predict.py --dataset SMD --group 2-8 --model 17032021_044240 --load_scores True
19+
python predict.py --dataset SMD --group 2-9 --model 17032021_045614 --load_scores True
20+
python predict.py --dataset SMD --group 3-1 --model 17032021_051238 --load_scores True
21+
python predict.py --dataset SMD --group 3-2 --model 17032021_052901 --load_scores True
22+
python predict.py --dataset SMD --group 3-3 --model 17032021_054227 --load_scores True
23+
python predict.py --dataset SMD --group 3-4 --model 17032021_055608 --load_scores True
24+
python predict.py --dataset SMD --group 3-5 --model 17032021_060937 --load_scores True
25+
python predict.py --dataset SMD --group 3-6 --model 17032021_062314 --load_scores True
26+
python predict.py --dataset SMD --group 3-7 --model 17032021_063934 --load_scores True
27+
python predict.py --dataset SMD --group 3-8 --model 17032021_065554 --load_scores True
28+
python predict.py --dataset SMD --group 3-9 --model 17032021_071215 --load_scores True
29+
python predict.py --dataset SMD --group 3-10 --model 17032021_072910 --load_scores True
30+
python predict.py --dataset SMD --group 3-11 --model 17032021_074315 --load_scores True
File renamed without changes.

copy_files_from_ssh.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

eval_methods.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22

3-
from spot import SPOT
3+
from spot import SPOT, dSPOT
44

55

66
def adjust_predicts(score, label, threshold, advance=1, pred=None, calc_latency=False):
@@ -87,28 +87,33 @@ def pot_eval(init_score, score, label, q=1e-3, level=0.99):
8787
s = SPOT(q) # SPOT object
8888
s.fit(init_score, score) # data import
8989
s.initialize(level=level, min_extrema=False) # initialization step
90-
# ret = s.run(dynamic=True) # run
9190
ret = s.run(dynamic=False, with_alarm=False) # much faster
91+
92+
# s = dSPOT(q, depth=300) # SPOT object
93+
# s.fit(init_score, score) # data import
94+
# s.initialize() # initialization step
95+
# ret = s.run() # much faster
96+
9297
print(len(ret["alarms"]))
9398
print(len(ret["thresholds"]))
9499

95100
pot_th = np.mean(ret["thresholds"])
96101
pred, p_latency = adjust_predicts(score, label, pot_th, calc_latency=True)
97102
# pred = adjust_predicts(score, label, pot_th, advance=1, delay=30)
98103
p_t = calc_point2point(pred, label)
99-
print("POT result: ", p_t, pot_th)
104+
# print("POT result: ", p_t, pot_th)
100105
return {
101-
"pot-f1": p_t[0],
102-
"pot-precision": p_t[1],
103-
"pot-recall": p_t[2],
104-
"pot-TP": p_t[3],
105-
"pot-TN": p_t[4],
106-
"pot-FP": p_t[5],
107-
"pot-FN": p_t[6],
108-
"pot-threshold": pot_th,
109-
"pot-latency": p_latency,
106+
"f1": p_t[0],
107+
"precision": p_t[1],
108+
"recall": p_t[2],
109+
"TP": p_t[3],
110+
"TN": p_t[4],
111+
"FP": p_t[5],
112+
"FN": p_t[6],
113+
"threshold": pot_th,
114+
"latency": p_latency,
110115
"pred": pred,
111-
"pot_thresholds": ret["thresholds"],
116+
"thresholds": ret["thresholds"],
112117
}
113118

114119

@@ -119,6 +124,7 @@ def bf_search(score, label, start, end=None, step_num=1, display_freq=1, verbose
119124
list: list for results
120125
float: the `threshold` for best-f1
121126
"""
127+
print(f"Finding best f1-score by searching for threshold..")
122128
if step_num is None or end is None:
123129
end = start
124130
step_num = 1
@@ -128,21 +134,33 @@ def bf_search(score, label, start, end=None, step_num=1, display_freq=1, verbose
128134
threshold = search_lower_bound
129135
m = (-1.0, -1.0, -1.0)
130136
m_t = 0.0
137+
m_l = 0
131138
for i in range(search_step):
132139
threshold += search_range / float(search_step)
133-
target = calc_seq(score, label, threshold)
140+
target, latency = calc_seq(score, label, threshold)
134141
if target[0] > m[0]:
135142
m_t = threshold
136143
m = target
144+
m_l = latency
137145
if verbose and i % display_freq == 0:
138146
print("cur thr: ", threshold, target, m, m_t)
139-
print(m, m_t)
140-
return m, m_t
147+
148+
return {
149+
"f1": m[0],
150+
"precision": m[1],
151+
"recall": m[2],
152+
"TP": m[3],
153+
"TN": m[4],
154+
"FP": m[5],
155+
"FN": m[6],
156+
"threshold": m_t,
157+
"latency": m_l,
158+
}
141159

142160

143161
def calc_seq(score, label, threshold):
144162
"""
145163
Calculate f1 score for a score sequence
146164
"""
147-
predict = adjust_predicts(score, label, threshold)
148-
return calc_point2point(predict, label)
165+
predict, latency = adjust_predicts(score, label, threshold, calc_latency=True)
166+
return calc_point2point(predict, label), latency

mtad_gat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(
5151
gru_hid_dim,
5252
autoenc_n_layers,
5353
autoenc_hid_dim,
54-
n_features,
54+
out_dim,
5555
dropout,
5656
device,
5757
)
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

output/SMD/2-9/train_losses.png

28.7 KB

output/SMD/2-9/validation_losses.png

29.8 KB

output/SMD/3-1/train_losses.png

30.9 KB

output/SMD/3-1/validation_losses.png

30.8 KB
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

output/SMD/3-5/test_scores.npy

92.3 KB
Binary file not shown.

output/SMD/3-5/train_losses.png

32.7 KB

output/SMD/3-5/train_scores.npy

92.3 KB
Binary file not shown.

output/SMD/3-5/validation_losses.png

29.7 KB
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

output/smd/2-9/train_losses.png

-29.9 KB
Binary file not shown.

output/smd/2-9/validation_losses.png

-38.2 KB
Binary file not shown.

output/smd/3-1/train_losses.png

-34.4 KB
Binary file not shown.

output/smd/3-1/validation_losses.png

-34.9 KB
Binary file not shown.

output/smd/3-11/test_scores.npy

-112 KB
Binary file not shown.

output/smd/3-11/train_losses.png

-29.6 KB
Binary file not shown.

output/smd/3-11/train_scores.npy

-112 KB
Binary file not shown.

output/smd/3-11/validation_losses.png

-40.3 KB
Binary file not shown.

output/smd/3-5/test_scores.npy

-92.3 KB
Binary file not shown.

output/smd/3-5/train_losses.png

-33.5 KB
Binary file not shown.

output/smd/3-5/train_scores.npy

-92.3 KB
Binary file not shown.

output/smd/3-5/validation_losses.png

-38.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)