-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
146 lines (116 loc) · 4.89 KB
/
utils.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
import torch
from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np
from sklearn.metrics import precision_recall_fscore_support
def read_data(dataset):
if "SMD" == dataset:
data_path = "../Anomaly/SMD/"
scaler = StandardScaler()
data = np.load(data_path + "/SMD_train.npy")[:,:]
scaler.fit(data)
data = scaler.transform(data)
test_data = np.load(data_path + "/SMD_test.npy")[:,:]
test_data = scaler.transform(test_data)
train_data = data
data_len = len(train_data)
val_data = test_data#train_data[(int)(data_len * 0.8):]
test_labels = np.load(data_path + "/SMD_test_label.npy")[:]
elif "SWAT" == dataset:
data_path = "../Anomaly/SWAT/"
train_data = pd.read_csv( data_path + 'swat_train2.csv')
test_data = pd.read_csv(data_path + 'swat2.csv')
test_labels = test_data.values[:, -1]
train_data = train_data.values[:, :-1]
test_data = test_data.values[:, :-1]
scaler = StandardScaler()
scaler.fit(train_data)
train_data = scaler.transform(train_data)
test_data = scaler.transform(test_data)
data_len = len(train_data)
val_data = test_data
elif "MSL" == dataset:
data_path = "../Anomaly/MSL/"
scaler = StandardScaler()
train_data = np.load(data_path + "/MSL_train.npy")
scaler.fit(train_data)
train_data = scaler.transform(train_data)
test_data = np.load(data_path + "/MSL_test.npy")
test_data = scaler.transform(test_data)
test_labels = np.load(data_path + "/MSL_test_label.npy")
data_len = len(train_data)
val_data = test_data
elif "PSM" == dataset:
scaler = StandardScaler()
data_path = "../Anomaly/PSM/"
data = pd.read_csv(data_path + '/train.csv')
data = data.values[:, 1:]
data = np.nan_to_num(data)
scaler.fit(data)
data = scaler.transform(data)
test_data = pd.read_csv(data_path + '/test.csv')
test_data = test_data.values[:, 1:]
test_data = np.nan_to_num(test_data)
test_data = scaler.transform(test_data)
train_data = data
val_data = test_data
test_labels = pd.read_csv(data_path + '/test_label.csv').values[:, 1:]
elif "SMAP" == dataset:
scaler = StandardScaler()
data_path = "../Anomaly/SMAP/"
data = np.load(data_path + "/SMAP_train.npy")
scaler.fit(data)
data = scaler.transform(data)
test_data = np.load(data_path + "/SMAP_test.npy")
test_data = scaler.transform(test_data)
train_data = data
val_data = test_data
test_labels = np.load(data_path + "/SMAP_test_label.npy")
return train_data, test_data, val_data, test_labels
# to predict a single target value, not the entire window
def iterate_batches(data, window_size, batch_size, start_idx = 0):
for start in range(start_idx, len(data) - window_size, batch_size):
end = min(start + batch_size, len(data) - window_size)
batch_data = [data[i:i + window_size] for i in range(start, end)]
batch_targets = [data[i + window_size] for i in range(start, end)]
yield torch.stack(batch_data), torch.stack(batch_targets)
def apply_adjustment(gt_, pred_):
gt = gt_.copy()
pred = pred_.copy()
anomaly_state = False
for i in range(len(gt)):
if gt[i] == 1 and pred[i] == 1 and not anomaly_state:
anomaly_state = True
for j in range(i, 0, -1):
if gt[j] == 0:
break
else:
if pred[j] == 0:
pred[j] = 1
for j in range(i, len(gt)):
if gt[j] == 0:
break
else:
if pred[j] == 0:
pred[j] = 1
elif gt[i] == 0:
anomaly_state = False
if anomaly_state:
pred[i] = 1
return gt, pred
# end function
def sliding_window_anomaly_detection(mse_list, window_size, threshold_factor=3):
mse_series = pd.Series(mse_list)
# Calculate moving average and moving standard deviation
moving_avg = mse_series.rolling(window=window_size, min_periods=1).mean()
moving_std = mse_series.rolling(window=window_size, min_periods=1).std()
# Calculate dynamic threshold
dynamic_threshold = moving_avg + (threshold_factor * moving_std)
# Identify anomalies
anomalies = (mse_series > dynamic_threshold).astype(int)
# Convert to list for output
anomalies_list = anomalies.tolist()
return anomalies_list, dynamic_threshold.tolist()
def get_precision_recall_f1(true_labels, pred_y):
precision, recall, f1_score, _ = precision_recall_fscore_support(true_labels, pred_y, average='binary')
return round(precision, 4), round(recall, 4), round(f1_score, 4)