-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
154 lines (124 loc) · 5.25 KB
/
util.py
File metadata and controls
154 lines (124 loc) · 5.25 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
import numpy as np
import os
import scipy.sparse as sp
import torch
import pickle
import torch
import torch.nn.functional as F
class DataLoader(object):
def __init__(self, xs, ys, batch_size, pad_with_last_sample=True):
self.batch_size = batch_size
self.current_ind = 0
if pad_with_last_sample:
num_padding = (batch_size - (len(xs) % batch_size)) % batch_size
x_padding = np.repeat(xs[-1:], num_padding, axis=0)
y_padding = np.repeat(ys[-1:], num_padding, axis=0)
xs = np.concatenate([xs, x_padding], axis=0)
ys = np.concatenate([ys, y_padding], axis=0)
self.size = len(xs)
self.num_batch = int(self.size // self.batch_size)
self.xs = xs
self.ys = ys
def shuffle(self):
permutation = np.random.permutation(self.size)
xs, ys = self.xs[permutation], self.ys[permutation]
self.xs = xs
self.ys = ys
def get_iterator(self):
self.current_ind = 0
def _wrapper():
while self.current_ind < self.num_batch:
start_ind = self.batch_size * self.current_ind
end_ind = min(self.size, self.batch_size * (self.current_ind + 1))
x_i = self.xs[start_ind:end_ind, ...]
y_i = self.ys[start_ind:end_ind, ...]
yield (x_i, y_i)
self.current_ind += 1
return _wrapper()
class StandardScaler:
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
def load_dataset(dataset_dir, batch_size, valid_batch_size=None, test_batch_size=None):
data = {}
for category in ["train", "val", "test"]:
cat_data = np.load(os.path.join(dataset_dir, category + ".npz"))
data["x_" + category] = cat_data["x"]
data["y_" + category] = cat_data["y"]
scaler = StandardScaler(
mean=data["x_train"][..., 0].mean(), std=data["x_train"][..., 0].std()
)
# Data format
for category in ["train", "val", "test"]:
data["x_" + category][..., 0] = scaler.transform(data["x_" + category][..., 0])
print("Perform shuffle on the dataset")
random_train = torch.arange(int(data["x_train"].shape[0]))
random_train = torch.randperm(random_train.size(0))
data["x_train"] = data["x_train"][random_train, ...]
data["y_train"] = data["y_train"][random_train, ...]
random_val = torch.arange(int(data["x_val"].shape[0]))
random_val = torch.randperm(random_val.size(0))
data["x_val"] = data["x_val"][random_val, ...]
data["y_val"] = data["y_val"][random_val, ...]
# random_test = torch.arange(int(data['x_test'].shape[0]))
# random_test = torch.randperm(random_test.size(0))
# data['x_test'] = data['x_test'][random_test,...]
# data['y_test'] = data['y_test'][random_test,...]
data["train_loader"] = DataLoader(data["x_train"], data["y_train"], batch_size)
data["val_loader"] = DataLoader(data["x_val"], data["y_val"], valid_batch_size)
data["test_loader"] = DataLoader(data["x_test"], data["y_test"], test_batch_size)
data["scaler"] = scaler
return data
def MAE_torch(pred, true, mask_value=None):
if mask_value != None:
true = true[..., :pred.shape[-1]]
mask = torch.gt(true, mask_value)
pred = torch.masked_select(pred, mask)
true = torch.masked_select(true, mask)
return torch.mean(torch.abs(true - pred))
def MAPE_torch(pred, true, mask_value=None):
if mask_value != None:
true = true[..., :pred.shape[-1]]
mask = torch.gt(true, mask_value)
pred = torch.masked_select(pred, mask)
true = torch.masked_select(true, mask)
return torch.mean(torch.abs(torch.div((true - pred), true)))
def RMSE_torch(pred, true, mask_value=None):
if mask_value != None:
true = true[..., :pred.shape[-1]]
mask = torch.gt(true, mask_value)
pred = torch.masked_select(pred, mask)
true = torch.masked_select(true, mask)
return torch.sqrt(torch.mean((pred - true) ** 2))
def WMAPE_torch(pred, true, mask_value=None):
if mask_value != None:
true = true[..., :pred.shape[-1]]
mask = torch.gt(true, mask_value)
pred = torch.masked_select(pred, mask)
true = torch.masked_select(true, mask)
loss = torch.sum(torch.abs(pred - true)) / torch.sum(torch.abs(true))
return loss
def metric(pred, real):
mae = MAE_torch(pred, real, 0).item()
mape = MAPE_torch(pred, real,0).item()
wmape = WMAPE_torch(pred, real, 0).item()
rmse = RMSE_torch(pred, real, 0).item()
return mae, mape, rmse, wmape
def load_graph_data(pkl_filename):
sensor_ids, sensor_id_to_ind, adj_mx = load_pickle(pkl_filename)
return sensor_ids, sensor_id_to_ind, adj_mx
def load_pickle(pickle_file):
try:
with open(pickle_file, 'rb') as f:
pickle_data = pickle.load(f)
except UnicodeDecodeError as e:
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 e
return pickle_data