-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
380 lines (303 loc) · 10.8 KB
/
utils.py
File metadata and controls
380 lines (303 loc) · 10.8 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
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
import os
import sys
import torch
import numpy as np
import pickle as pk
import scipy.sparse as sp
from scipy.sparse import linalg
import pandas as pd
from scipy.sparse.linalg import eigs
import math
import logging
def missed_eval_torch(predict, true, mask):
mae = torch.sum(torch.absolute(predict - true) * (1 - mask)) / torch.sum(1 - mask)
rmse = torch.sqrt(
torch.sum((predict - true) ** 2 * (1 - mask)) / torch.sum(1 - mask)
)
mape_mask = torch.where(true > 5, 1, 0)
mape_mask = mape_mask * (1 - mask)
mape = torch.sum(torch.absolute((predict - true) / (true + 1e-5)) * mape_mask) / (
torch.sum(mape_mask) + 1e-5
)
return mae, rmse, mape
def missed_eval_np(predict, true, mask):
predict, true = np.asarray(predict), np.asarray(true)
mae = np.sum(np.absolute(predict - true) * (1 - mask)) / (np.sum(1 - mask) + 1e-5)
rmse = np.sqrt(
np.sum((predict - true) ** 2 * (1 - mask)) / (np.sum(1 - mask) + 1e-5)
)
mape_mask = np.where(true > 5, 1, 0)
mape_mask = mape_mask * (1 - mask)
mape = np.sum(np.absolute((predict - true) / (true + 1e-5)) * mape_mask) / np.sum(
mape_mask + 1e-5
)
return mae, rmse, mape
def unnormalization(data, mean, std):
return data * std + mean
def load_pkl(pickle_file: str) -> object:
"""Load pickle data.
Args:
pickle_file (str): file path
Returns:
object: loaded objected
"""
try:
with open(pickle_file, "rb") as f:
pickle_data = pickle.load(f)
except UnicodeDecodeError:
with open(pickle_file, "rb") as f:
pickle_data = pickle.load(f, encoding="latin1")
except Exception as e:
print("Unable to load data ", pickle_file, ":", e)
raise
return pickle_data
def sym_adj(adj):
"""Symmetrically normalize adjacency matrix."""
adj = sp.coo_matrix(adj)
rowsum = np.array(adj.sum(1))
d_inv_sqrt = np.power(rowsum, -0.5).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.0
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
return (
adj.dot(d_mat_inv_sqrt)
.transpose()
.dot(d_mat_inv_sqrt)
.astype(np.float32)
.todense()
)
def asym_adj(adj):
adj = sp.coo_matrix(adj)
rowsum = np.array(adj.sum(1)).flatten()
d_inv = np.power(rowsum, -1).flatten()
d_inv[np.isinf(d_inv)] = 0.0
d_mat = sp.diags(d_inv)
return d_mat.dot(adj).astype(np.float32).todense()
def calculate_normalized_laplacian(adj):
"""
# L = D^-1/2 (D-A) D^-1/2 = I - D^-1/2 A D^-1/2
# D = diag(A 1)
:param adj:
:return:
"""
adj = sp.coo_matrix(adj)
d = np.array(adj.sum(1))
d_inv_sqrt = np.power(d, -0.5).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.0
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
normalized_laplacian = (
sp.eye(adj.shape[0])
- adj.dot(d_mat_inv_sqrt).transpose().dot(d_mat_inv_sqrt).tocoo()
)
return normalized_laplacian
def calculate_scaled_laplacian(adj_mx, lambda_max=2, undirected=True):
if undirected:
adj_mx = np.maximum.reduce([adj_mx, adj_mx.T])
L = calculate_normalized_laplacian(adj_mx)
if lambda_max is None:
lambda_max, _ = linalg.eigsh(L, 1, which="LM")
lambda_max = lambda_max[0]
L = sp.csr_matrix(L)
M, _ = L.shape
I = sp.identity(M, format="csr", dtype=L.dtype)
L = (2 / lambda_max * L) - I
return L.astype(np.float32).todense()
def calculate_random_walk_matrix(adj_mx):
adj_mx = sp.coo_matrix(adj_mx)
d = np.array(adj_mx.sum(1))
d_inv = np.power(d, -1).flatten()
d_inv[np.isinf(d_inv)] = 0.0
d_mat_inv = sp.diags(d_inv)
random_walk_mx = d_mat_inv.dot(adj_mx).tocoo()
return random_walk_mx
def calculate_reverse_random_walk_matrix(adj_mx):
return calculate_random_walk_matrix(np.transpose(adj_mx))
def load_graph_missdata(adj_mx, adjtype):
if adjtype == "scalap":
adj = [calculate_scaled_laplacian(adj_mx)]
elif adjtype == "normlap":
adj = [calculate_normalized_laplacian(adj_mx).astype(np.float32).todense()]
elif adjtype == "symnadj":
adj = [sym_adj(adj_mx)]
elif adjtype == "transition":
adj = [asym_adj(adj_mx)]
elif adjtype == "doubletransition":
adj = [asym_adj(adj_mx), asym_adj(np.transpose(adj_mx))]
elif adjtype == "identity":
adj = [np.diag(np.ones(adj_mx.shape[0])).astype(np.float32)]
else:
error = 0
assert error, "adj type not defined"
return adj
def weight_matrix(adj_mx, sigma2=0.1, epsilon=0.5, scaling=True):
"""
Load weight matrix function.
:param file_path: str, the path of saved weight matrix file.
:param sigma2: float, scalar of matrix W.
:param epsilon: float, thresholds to control the sparsity of matrix W.
:param scaling: bool, whether applies numerical scaling on W.
:return: np.ndarray, [n_route, n_route].
"""
try:
W = adj_mx
except FileNotFoundError:
print(f"ERROR: input file was not found in {file_path}.")
# check whether W is a 0/1 matrix.
if set(np.unique(W)) == {0, 1}:
print('The input graph is a 0/1 matrix; set "scaling" to False.')
scaling = False
if scaling:
n = W.shape[0]
W = W / 10000.0
W2, W_mask = W * W, np.ones([n, n]) - np.identity(n)
# refer to Eq.10
return np.exp(-W2 / sigma2) * (np.exp(-W2 / sigma2) >= epsilon) * W_mask
else:
return W
def load_PA(file_path):
df = pd.read_csv(file_path, header=None)
df = df.to_numpy()
df = np.float64(df > 0)
return df
def scaled_Laplacian(W):
"""
compute \tilde{L}
Parameters
----------
W: np.ndarray, shape is (N, N), N is the num of vertices
Returns
----------
scaled_Laplacian: np.ndarray, shape (N, N)
"""
assert W.shape[0] == W.shape[1]
D = np.diag(np.sum(W, axis=1))
L = D - W
lambda_max = eigs(L, k=1, which="LR")[0].real
return (2 * L) / lambda_max - np.identity(W.shape[0])
def cheb_polynomial(L_tilde, K):
"""
compute a list of chebyshev polynomials from T_0 to T_{K-1}
Parameters
----------
L_tilde: scaled Laplacian, np.ndarray, shape (N, N)
K: the maximum order of chebyshev polynomials
Returns
----------
cheb_polynomials: list(np.ndarray), length: K, from T_0 to T_{K-1}
"""
N = L_tilde.shape[0]
cheb_polynomials = [np.identity(N), L_tilde.copy()]
for i in range(2, K):
cheb_polynomials.append(
2 * L_tilde * cheb_polynomials[i - 1] - cheb_polynomials[i - 2]
)
return cheb_polynomials
def load_weighted_adjacency_matrix(file_path, num_v):
df = pd.read_csv(file_path, header=None)
df = df.to_numpy()
df = np.float64(df > 0)
return df
def batch_cosine_similarity(x, y):
# 计算分母
l2_x = (
torch.norm(x, dim=2, p=2) + 1e-7
) # avoid 0, l2 norm, num_heads x batch_size x hidden_dim==>num_heads x batch_size
l2_y = (
torch.norm(y, dim=2, p=2) + 1e-7
) # avoid 0, l2 norm, num_heads x batch_size x hidden_dim==>num_heads x batch_size
l2_m = torch.matmul(l2_x.unsqueeze(dim=2), l2_y.unsqueeze(dim=2).transpose(1, 2))
l2_z = torch.matmul(x, y.transpose(1, 2))
# cos similarity affinity matrix
cos_affnity = l2_z / l2_m
adj = cos_affnity
return adj
def batch_dot_similarity(x, y):
QKT = torch.bmm(x, y.transpose(-1, -2)) / math.sqrt(x.shape[2])
W = torch.softmax(QKT, dim=-1)
return W
def read_pkl(pklfile):
with open(pklfile, "rb") as fb:
data = pk.load(fb)
fb.close()
return data
def save_to_pkl(variable, pklfile):
with open(pklfile, "wb") as fb:
pk.dump(variable, fb)
fb.close()
def get_normalized_adj(A):
"""
Returns the degree normalized adjacency matrix.
"""
A = A + np.diag(np.ones(A.shape[0], dtype=np.float32))
D = np.array(np.sum(A, axis=1)).reshape((-1,))
D[D <= 10e-5] = 10e-5 # Prevent infs
diag = np.reciprocal(np.sqrt(D))
A_wave = np.multiply(np.multiply(diag.reshape((-1, 1)), A), diag.reshape((1, -1)))
return A_wave
def get_logger(log_dir, name, log_filename="info.log", level=logging.INFO):
logger = logging.getLogger(name)
logger.setLevel(level)
# Add file handler and stdout handler
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
file_handler = logging.FileHandler(os.path.join(log_dir, log_filename))
file_handler.setFormatter(formatter)
# Add console handler.
console_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(console_formatter)
logger.addHandler(file_handler)
logger.addHandler(console_handler)
# Add google cloud log handler
logger.info("Log directory: %s", log_dir)
return logger
def get_randmask(observed_mask, min_miss_ratio=0.0, max_miss_ratio=1.0):
rand_for_mask = torch.rand_like(observed_mask) * observed_mask
rand_for_mask = rand_for_mask.reshape(-1)
sample_ratio = np.random.rand()
sample_ratio = sample_ratio * (max_miss_ratio - min_miss_ratio) + min_miss_ratio
num_observed = observed_mask.sum().item()
num_masked = round(num_observed * sample_ratio)
rand_for_mask[rand_for_mask.topk(num_masked).indices] = -1
cond_mask = (rand_for_mask > 0).reshape(observed_mask.shape).float()
return cond_mask
def get_block_mask(observed_mask, target_strategy="block"):
rand_sensor_mask = torch.rand_like(observed_mask)
randint = np.random.randint
sample_ratio = np.random.rand()
sample_ratio = sample_ratio * 0.15
mask = rand_sensor_mask < sample_ratio
min_seq = 12
max_seq = 24
for col in range(observed_mask.shape[1]):
idxs = np.flatnonzero(mask[:, col])
if not len(idxs):
continue
fault_len = min_seq
if max_seq > min_seq:
fault_len = fault_len + int(randint(max_seq - min_seq))
idxs_ext = np.concatenate([np.arange(i, i + fault_len) for i in idxs])
idxs = np.unique(idxs_ext)
idxs = np.clip(idxs, 0, observed_mask.shape[0] - 1)
mask[idxs, col] = True
rand_base_mask = torch.rand_like(observed_mask) < 0.05
reverse_mask = mask | rand_base_mask
block_mask = 1 - reverse_mask.to(torch.float32)
cond_mask = observed_mask.clone()
mask_choice = np.random.rand()
if target_strategy == "hybrid" and mask_choice > 0.7:
cond_mask = get_randmask(observed_mask, 0.0, 1.0)
else:
cond_mask = block_mask * cond_mask
return cond_mask
class StandardScaler:
"""
Standard the input
"""
def __init__(self, mean, std):
self.mean = mean
self.std = std
def transform(self, data):
return (data - self.mean) / self.std
def inverse_transform(self, data):
return (data * self.std) + self.mean