Skip to content

Commit

Permalink
# This is a combination of 23 commits.
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
axeloh authored and wQvaale committed Mar 29, 2021
1 parent ca6bf70 commit b8a042b
Show file tree
Hide file tree
Showing 135 changed files with 239,178 additions and 218 deletions.
11 changes: 3 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
__pycache__/
.idea/
.DS_Store
datasets/ecg_data.txt
datasets/data/
datasets/ServerMachineDataset/processed/
/datasets/data/processed/
/datasets/data/test/
/datasets/data/train/
eda/.ipynb_checkpoints/
visualization/.ipynb_checkpoints/
plots/
*.pkl
models/
output/smd/*/logs
output/smd/*/preds.pkl
output/smd/*/anomaly_preds.pkl
output/
40 changes: 19 additions & 21 deletions args.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import argparse


def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("Boolean value expected.")


def get_parser():
parser = argparse.ArgumentParser()

# Data params
parser.add_argument("--dataset", type=str.upper, default="SMD")
parser.add_argument(
"--group",
type=str,
default="1-1",
help="Required for smd dataset. <group_index>-<index>",
)
parser.add_argument("--group", type=str, default="1-1", help="Required for SMD dataset. <group_index>-<index>")
parser.add_argument("--lookback", type=int, default=100)
parser.add_argument("--horizon", type=int, default=1)
parser.add_argument("--target_col", type=int, default=None)
parser.add_argument("--do_preprocess", type=str2bool, default=False)

# Model params
parser.add_argument("--kernel_size", type=int, default=7)
Expand All @@ -31,27 +37,19 @@ def get_parser():
parser.add_argument("--bs", type=int, default=256)
parser.add_argument("--init_lr", type=float, default=1e-3)
parser.add_argument("--val_split", type=float, default=0.1)
parser.add_argument("--shuffle_dataset", type=bool, default=True)
parser.add_argument("--shuffle_dataset", type=str2bool, default=True)
parser.add_argument("--dropout", type=float, default=0.3)
parser.add_argument("--use_cuda", type=bool, default=True)
parser.add_argument("--use_cuda", type=str2bool, default=True)
parser.add_argument("--model_path", type=str, default="models")
parser.add_argument("--print_every", type=int, default=1)

# Predictor args
parser.add_argument(
"--save_scores",
type=bool,
default=True,
help="To save anomaly scores predicted.",
)
parser.add_argument(
"--load_scores",
type=bool,
default=False,
help="To use already computed anomaly scores",
)
parser.add_argument("--save_scores", type=str2bool, default=True, help="To save anomaly scores predicted.")
parser.add_argument("--load_scores", type=str2bool, default=False, help="To use already computed anomaly scores")
parser.add_argument("--gamma", type=float, default=1)
parser.add_argument("--level", type=float, default=None)
parser.add_argument("--q", type=float, default=1e-3)
parser.add_argument("--use_mov_av", type=str2bool, default=False)

# Other
parser.add_argument("--comment", type=str, default="")
Expand Down
21 changes: 21 additions & 0 deletions bash_scripts/copy_files_from_ssh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

port=11726
[email protected]

scp -P $port -r $ssh:~/MTAD-GAT/models ./

for i in {1..8}
do
scp -P $port -r $ssh:~/MTAD-GAT/output/SMD/1-$i ./output/SMD/
done

for i in {1..9}
do
scp -P $port -r $ssh:~/MTAD-GAT/output/SMD/2-$i ./output/SMD/
done

for i in {1..11}
do
scp -P $port -r $ssh:~/MTAD-GAT/output/SMD/3-$i ./output/SMD/
done
30 changes: 30 additions & 0 deletions bash_scripts/predict_smd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

python predict.py --dataset SMD --group 1-1 --model 17032021_010924 --load_scores True
python predict.py --dataset SMD --group 1-2 --model 17032021_012546 --load_scores True
python predict.py --dataset SMD --group 1-3 --model 17032021_013914 --load_scores True
python predict.py --dataset SMD --group 1-4 --model 17032021_015244 --load_scores True
python predict.py --dataset SMD --group 1-5 --model 17032021_020613 --load_scores True
python predict.py --dataset SMD --group 1-6 --model 17032021_021942 --load_scores True
python predict.py --dataset SMD --group 1-7 --model 17032021_023323 --load_scores True
python predict.py --dataset SMD --group 1-8 --model 17032021_024657 --load_scores True
python predict.py --dataset SMD --group 2-1 --model 17032021_030025 --load_scores True
python predict.py --dataset SMD --group 2-2 --model 17032021_031410 --load_scores True
python predict.py --dataset SMD --group 2-3 --model 17032021_032905 --load_scores True
python predict.py --dataset SMD --group 2-4 --model 17032021_034448 --load_scores True
python predict.py --dataset SMD --group 2-5 --model 17032021_035857 --load_scores True
python predict.py --dataset SMD --group 2-6 --model 17032021_041234 --load_scores True
python predict.py --dataset SMD --group 2-7 --model 17032021_042906 --load_scores True
python predict.py --dataset SMD --group 2-8 --model 17032021_044240 --load_scores True
python predict.py --dataset SMD --group 2-9 --model 17032021_045614 --load_scores True
python predict.py --dataset SMD --group 3-1 --model 17032021_051238 --load_scores True
python predict.py --dataset SMD --group 3-2 --model 17032021_052901 --load_scores True
python predict.py --dataset SMD --group 3-3 --model 17032021_054227 --load_scores True
python predict.py --dataset SMD --group 3-4 --model 17032021_055608 --load_scores True
python predict.py --dataset SMD --group 3-5 --model 17032021_060937 --load_scores True
python predict.py --dataset SMD --group 3-6 --model 17032021_062314 --load_scores True
python predict.py --dataset SMD --group 3-7 --model 17032021_063934 --load_scores True
python predict.py --dataset SMD --group 3-8 --model 17032021_065554 --load_scores True
python predict.py --dataset SMD --group 3-9 --model 17032021_071215 --load_scores True
python predict.py --dataset SMD --group 3-10 --model 17032021_072910 --load_scores True
python predict.py --dataset SMD --group 3-11 --model 17032021_074315 --load_scores True
File renamed without changes.
21 changes: 0 additions & 21 deletions copy_files_from_ssh.sh

This file was deleted.

54 changes: 36 additions & 18 deletions eval_methods.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from spot import SPOT
from spot import SPOT, dSPOT


def adjust_predicts(score, label, threshold, advance=1, pred=None, calc_latency=False):
Expand Down Expand Up @@ -87,28 +87,33 @@ def pot_eval(init_score, score, label, q=1e-3, level=0.99):
s = SPOT(q) # SPOT object
s.fit(init_score, score) # data import
s.initialize(level=level, min_extrema=False) # initialization step
# ret = s.run(dynamic=True) # run
ret = s.run(dynamic=False, with_alarm=False) # much faster

# s = dSPOT(q, depth=300) # SPOT object
# s.fit(init_score, score) # data import
# s.initialize() # initialization step
# ret = s.run() # much faster

print(len(ret["alarms"]))
print(len(ret["thresholds"]))

pot_th = np.mean(ret["thresholds"])
pred, p_latency = adjust_predicts(score, label, pot_th, calc_latency=True)
# pred = adjust_predicts(score, label, pot_th, advance=1, delay=30)
p_t = calc_point2point(pred, label)
print("POT result: ", p_t, pot_th)
# print("POT result: ", p_t, pot_th)
return {
"pot-f1": p_t[0],
"pot-precision": p_t[1],
"pot-recall": p_t[2],
"pot-TP": p_t[3],
"pot-TN": p_t[4],
"pot-FP": p_t[5],
"pot-FN": p_t[6],
"pot-threshold": pot_th,
"pot-latency": p_latency,
"f1": p_t[0],
"precision": p_t[1],
"recall": p_t[2],
"TP": p_t[3],
"TN": p_t[4],
"FP": p_t[5],
"FN": p_t[6],
"threshold": pot_th,
"latency": p_latency,
"pred": pred,
"pot_thresholds": ret["thresholds"],
"thresholds": ret["thresholds"],
}


Expand All @@ -119,6 +124,7 @@ def bf_search(score, label, start, end=None, step_num=1, display_freq=1, verbose
list: list for results
float: the `threshold` for best-f1
"""
print(f"Finding best f1-score by searching for threshold..")
if step_num is None or end is None:
end = start
step_num = 1
Expand All @@ -128,21 +134,33 @@ def bf_search(score, label, start, end=None, step_num=1, display_freq=1, verbose
threshold = search_lower_bound
m = (-1.0, -1.0, -1.0)
m_t = 0.0
m_l = 0
for i in range(search_step):
threshold += search_range / float(search_step)
target = calc_seq(score, label, threshold)
target, latency = calc_seq(score, label, threshold)
if target[0] > m[0]:
m_t = threshold
m = target
m_l = latency
if verbose and i % display_freq == 0:
print("cur thr: ", threshold, target, m, m_t)
print(m, m_t)
return m, m_t

return {
"f1": m[0],
"precision": m[1],
"recall": m[2],
"TP": m[3],
"TN": m[4],
"FP": m[5],
"FN": m[6],
"threshold": m_t,
"latency": m_l,
}


def calc_seq(score, label, threshold):
"""
Calculate f1 score for a score sequence
"""
predict = adjust_predicts(score, label, threshold)
return calc_point2point(predict, label)
predict, latency = adjust_predicts(score, label, threshold, calc_latency=True)
return calc_point2point(predict, label), latency
2 changes: 1 addition & 1 deletion mtad_gat.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(
gru_hid_dim,
autoenc_n_layers,
autoenc_hid_dim,
n_features,
out_dim,
dropout,
device,
)
Expand Down
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
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
Binary file added output/SMD/2-9/train_losses.png
Binary file added output/SMD/2-9/validation_losses.png
Binary file added output/SMD/3-1/train_losses.png
Binary file added output/SMD/3-1/validation_losses.png
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
Binary file added output/SMD/3-5/test_scores.npy
Binary file not shown.
Binary file added output/SMD/3-5/train_losses.png
Binary file added output/SMD/3-5/train_scores.npy
Binary file not shown.
Binary file added output/SMD/3-5/validation_losses.png
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
Binary file removed output/smd/2-9/train_losses.png
Diff not rendered.
Binary file removed output/smd/2-9/validation_losses.png
Diff not rendered.
Binary file removed output/smd/3-1/train_losses.png
Diff not rendered.
Binary file removed output/smd/3-1/validation_losses.png
Diff not rendered.
Binary file removed output/smd/3-11/test_scores.npy
Binary file not shown.
Binary file removed output/smd/3-11/train_losses.png
Diff not rendered.
Binary file removed output/smd/3-11/train_scores.npy
Binary file not shown.
Binary file removed output/smd/3-11/validation_losses.png
Diff not rendered.
Binary file removed output/smd/3-5/test_scores.npy
Binary file not shown.
Binary file removed output/smd/3-5/train_losses.png
Diff not rendered.
Binary file removed output/smd/3-5/train_scores.npy
Binary file not shown.
Binary file removed output/smd/3-5/validation_losses.png
Diff not rendered.
Loading

0 comments on commit b8a042b

Please sign in to comment.