-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
236 lines (194 loc) · 7.87 KB
/
utils.py
File metadata and controls
236 lines (194 loc) · 7.87 KB
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
import os
import pickle
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import torch
from sklearn.preprocessing import MinMaxScaler, RobustScaler
from torch.utils.data import DataLoader, Dataset, SubsetRandomSampler
def plot_losses(loss_list, folder):
os.makedirs(f'plots/{folder}/', exist_ok=True)
for i in loss_list: # Ensure consistent indentation
Training_Acc = loss_list[i]
plt.xlabel('Epochs')
plt.ylabel(f'Average training Loss')
plt.plot(range(len(Training_Acc)), Training_Acc, label=f'Average training Loss', linewidth=1, linestyle='-', marker='.')
plt.savefig(f'plots/{folder}/training-graph.pdf')
plt.clf()
def normalize_data(data, scaler=None):
data = np.asarray(data, dtype=np.float32)
if np.any(sum(np.isnan(data))):
data = np.nan_to_num(data)
if scaler is None:
scaler = MinMaxScaler()
scaler.fit(data)
data = scaler.transform(data)
print("Data normalized")
return data, scaler
def get_data_dim(dataset):
"""
:param dataset: Name of dataset
:return: Number of dimensions in data
"""
if dataset == "SMAP":
return 25
elif dataset == "MSL":
return 55
elif str(dataset).startswith("machine"):
return 38
else:
raise ValueError("unknown dataset " + str(dataset))
def get_target_dims(dataset):
"""
:param dataset: Name of dataset
:return: index of data dimension that should be modeled (forecasted and reconstructed),
returns None if all input dimensions should be modeled
"""
if dataset == "SMAP":
return 25
elif dataset == "MSL":
return 55
elif dataset == "SMD":
return None
else:
raise ValueError("unknown dataset " + str(dataset))
def get_data(dataset, max_train_size=None, max_test_size=None,
normalize=False, spec_res=False, train_start=0, test_start=0):
"""
Get data from pkl files
return shape: (([train_size, x_dim], [train_size] or None), ([test_size, x_dim], [test_size]))
Method from OmniAnomaly (https://github.com/NetManAIOps/OmniAnomaly)
"""
prefix = "datasets"
if str(dataset).startswith("machine"):
prefix += "/ServerMachineDataset/processed"
elif dataset in ["MSL", "SMAP"]:
prefix += "/data/processed"
if max_train_size is None:
train_end = None
else:
train_end = train_start + max_train_size
if max_test_size is None:
test_end = None
else:
test_end = test_start + max_test_size
print("load data of:", dataset)
print("train: ", train_start, train_end)
print("test: ", test_start, test_end)
x_dim = get_data_dim(dataset)
f = open(os.path.join(prefix, dataset + "_train.pkl"), "rb")
train_data = pickle.load(f).reshape((-1, x_dim))[train_start:train_end, :]
f.close()
try:
f = open(os.path.join(prefix, dataset + "_test.pkl"), "rb")
test_data = pickle.load(f).reshape((-1, x_dim))[test_start:test_end, :]
f.close()
except (KeyError, FileNotFoundError):
test_data = None
try:
f = open(os.path.join(prefix, dataset + "_test_label.pkl"), "rb")
test_label = pickle.load(f).reshape((-1))[test_start:test_end]
f.close()
except (KeyError, FileNotFoundError):
test_label = None
if normalize:
train_data, scaler = normalize_data(train_data, scaler=None)
test_data, _ = normalize_data(test_data, scaler=scaler)
print("train set shape: ", train_data.shape)
print("test set shape: ", test_data.shape)
print("test set label shape: ", None if test_label is None else test_label.shape)
return (train_data, None), (test_data, test_label)
class SlidingWindowDataset(Dataset):
def __init__(self, data, window_size=100, horizon=1, stride=1):
"""
Args:
data (torch.Tensor): The multivariate time series data of shape (num_samples, num_features).
window_size (int): The size of the sliding window.
horizon (int): The number of future steps to predict.
stride (int): The stride of the sliding window.
"""
self.data = data
self.window_size = window_size if window_size is not None else 100
self.horizon = horizon if horizon is not None else 1
self.stride = stride
self.windows, self.targets = self._create_windows_and_targets()
def _create_windows_and_targets(self):
windows = []
targets = []
num_samples, num_features = self.data.shape
for i in range(0, num_samples - self.window_size - self.horizon + 1, self.stride):
window = self.data[i:i + self.window_size]
target = self.data[i + self.window_size:i + self.window_size + self.horizon]
windows.append(window)
targets.append(target)
return torch.stack(windows), torch.stack(targets)
def __len__(self):
return len(self.windows)
def __getitem__(self, idx):
return self.windows[idx], self.targets[idx]
def create_data_loaders(train_dataset, batch_size, shuffle=False, test_dataset=None):
train_loader, test_loader = None, None
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=shuffle,drop_last=True)
print(f"train_size:{len(train_dataset)}")
if test_dataset is not None:
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False,drop_last=True)
print(f"test_size: {len(test_dataset)}")
return train_loader, test_loader
def load(model, PATH, device="cpu"):
"""
Loads the model's parameters from the path mentioned
:param PATH: Should contain pickle file
"""
model.load_state_dict(torch.load(PATH, map_location=device))
def get_series_color(y):
if np.average(y) >= 0.95:
return "black"
elif np.average(y) == 0.0:
return "black"
else:
return "black"
def get_y_height(y):
if np.average(y) >= 0.95:
return 1.5
elif np.average(y) == 0.0:
return 0.1
else:
return max(y) + 0.1
def adjust_anomaly_scores(scores, dataset, is_train, lookback):
"""
Method for MSL and SMAP where channels have been concatenated as part of the preprocessing
:param scores: anomaly_scores
:param dataset: name of dataset
:param is_train: if scores is from train set
:param lookback: lookback (window size) used in model
"""
# Remove errors for time steps when transition to new channel (as this will be impossible for model to predict)
if dataset.upper() not in ['SMAP', 'MSL']:
return scores
adjusted_scores = scores.copy()
if is_train:
md = pd.read_csv(f'./datasets/data/{dataset.lower()}_train_md.csv')
else:
md = pd.read_csv('./datasets/data/labeled_anomalies.csv')
md = md[md['spacecraft'] == dataset.upper()]
md = md[md['chan_id'] != 'P-2']
# Sort values by channel
md = md.sort_values(by=['chan_id'])
# Getting the cumulative start index for each channel
sep_cuma = np.cumsum(md['num_values'].values) - lookback
sep_cuma = sep_cuma[:-1]
buffer = np.arange(1, 20)
i_remov = np.sort(np.concatenate((sep_cuma, np.array([i+buffer for i in sep_cuma]).flatten(),
np.array([i-buffer for i in sep_cuma]).flatten())))
i_remov = i_remov[(i_remov < len(adjusted_scores)) & (i_remov >= 0)]
i_remov = np.sort(np.unique(i_remov))
if len(i_remov) != 0:
adjusted_scores[i_remov] = 0
# Normalize each concatenated part individually
sep_cuma = np.cumsum(md['num_values'].values) - lookback
s = [0] + sep_cuma.tolist()
for c_start, c_end in [(s[i], s[i+1]) for i in range(len(s)-1)]:
e_s = adjusted_scores[c_start: c_end+1]
e_s = (e_s - np.min(e_s))/(np.max(e_s) - np.min(e_s))
adjusted_scores[c_start: c_end+1] = e_s
return adjusted_scores