-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
260 lines (223 loc) · 8.91 KB
/
util.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
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
import os
import random
from scipy.sparse.linalg import eigs
import numpy as np
import torch
from componenets.normalization import NScaler, MinMax01Scaler, MinMax11Scaler, StandardScaler, ColumnMinMaxScaler
def normalize_dataset(data, normalizer, column_wise=False):
if normalizer == 'max01':
if column_wise:
minimum = data.min(axis=0, keepdims=True)
maximum = data.max(axis=0, keepdims=True)
else:
minimum = data.min()
maximum = data.max()
scaler = MinMax01Scaler(minimum, maximum)
data = scaler.transform(data)
print('Normalize the dataset by MinMax01 Normalization')
elif normalizer == 'max11':
if column_wise:
minimum = data.min(axis=0, keepdims=True)
maximum = data.max(axis=0, keepdims=True)
else:
minimum = data.min()
maximum = data.max()
scaler = MinMax11Scaler(minimum, maximum)
data = scaler.transform(data)
print('Normalize the dataset by MinMax11 Normalization')
elif normalizer == 'std':
if column_wise:
mean = data.mean(axis=0, keepdims=True)
std = data.std(axis=0, keepdims=True)
else:
mean = data.mean()
std = data.std()
scaler = StandardScaler(mean, std)
data = scaler.transform(data)
print('Normalize the dataset by Standard Normalization')
elif normalizer == 'None':
scaler = NScaler()
data = scaler.transform(data)
print('Does not normalize the dataset')
elif normalizer == 'cmax':
# column min max, to be depressed
# note: axis must be the spatial dimension, please check !
scaler = ColumnMinMaxScaler(data.min(axis=0), data.max(axis=0))
data = scaler.transform(data)
print('Normalize the dataset by Column Min-Max Normalization')
else:
raise ValueError
return data, scaler
def load_st_dataset(dataset):
# output B, N, D
if dataset == 'PEMS4':
data_path = os.path.join('../data/PEMS04/PEMS04.npz')
data = np.load(data_path)['data'][:, :, 0]
elif dataset == 'PEMS8':
data_path = os.path.join('../data/PEMS08/PEMS08.npz')
data = np.load(data_path)['data'][:, :, 0]
elif dataset == 'PEMS3':
data_path = os.path.join('../data/PEMS03/PEMS03.npz')
data = np.load(data_path)['data'][:, :, 0]
elif dataset == 'PEMS7':
data_path = os.path.join('../data/PEMS07/PEMS07.npz')
data = np.load(data_path)['data'][:, :, 0]
else:
raise ValueError
if len(data.shape) == 2:
data = np.expand_dims(data, axis=-1)
print('Load %s Dataset shaped: ' % dataset, data.shape, data.max(), data.min(), data.mean(), np.median(data))
return data
def Add_Window_Horizon(data, window=3, horizon=1, single=False):
'''
:param data: shape [B, ...]
:param window:
:param horizon:
:return: X is [B, W, ...], Y is [B, H, ...]
'''
length = len(data)
end_index = length - horizon - window + 1
X = [] # windows
Y = [] # horizon
index = 0
if single:
while index < end_index:
X.append(data[index:index + window])
Y.append(data[index + window + horizon - 1:index + window + horizon])
index = index + 1
else:
while index < end_index:
X.append(data[index:index + window])
Y.append(data[index + window:index + window + horizon])
index = index + 1
X = np.array(X)
Y = np.array(Y)
return X, Y
def split_data_by_ratio(data, val_ratio, test_ratio):
data_len = data.shape[0]
test_data = data[-int(data_len * test_ratio):]
val_data = data[-int(data_len * (test_ratio + val_ratio)):-int(data_len * test_ratio)]
train_data = data[:-int(data_len * (test_ratio + val_ratio))]
return train_data, val_data, test_data
def get_dataloader(args, normalizer='std', single=False):
data = load_st_dataset(args.data) # B, N, D
# normalize st data
data, scaler = normalize_dataset(data, normalizer, args.column_wise)
# spilit dataset by days or by ratio
data_train, data_val, data_test = split_data_by_ratio(data, args.val_ratio, args.test_ratio)
# add time window
x_tra, y_tra = Add_Window_Horizon(data_train, window=args.lag, horizon=args.horizon, single=False)
x_val, y_val = Add_Window_Horizon(data_val, window=args.lag, horizon=args.horizon, single=False)
x_test, y_test = Add_Window_Horizon(data_test, window=args.lag, horizon=args.horizon, single=False)
print('Train: ', x_tra.shape, y_tra.shape)
print('Val: ', x_val.shape, y_val.shape)
print('Test: ', x_test.shape, y_test.shape)
print('Y_true min', scaler.inverse_transform(data).min())
##############get dataloader######################
train_dataloader = data_loader(x_tra, y_tra, args.batch_size, shuffle=True, drop_last=True)
val_dataloader = data_loader(x_val, y_val, args.batch_size, shuffle=False, drop_last=True)
test_dataloader = data_loader(x_test, y_test, args.batch_size, shuffle=False, drop_last=False)
return train_dataloader, val_dataloader, test_dataloader, scaler
def data_loader(X, Y, batch_size, shuffle=True, drop_last=True):
cuda = True if torch.cuda.is_available() else False
TensorFloat = torch.cuda.FloatTensor if cuda else torch.FloatTensor
X, Y = TensorFloat(X), TensorFloat(Y)
data = torch.utils.data.TensorDataset(X, Y)
dataloader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=shuffle, drop_last=drop_last)
return dataloader
def print_model_parameters(model, only_num=True):
print('*****************Model Parameter*****************')
if not only_num:
for name, param in model.named_parameters():
print(name, param.shape, param.requires_grad)
total_num = sum([param.nelement() for param in model.parameters()])
print('Total params num: {}'.format(total_num))
print('*****************Finish Parameter****************')
def get_adjacency_matrix(distance_df_filename, num_of_vertices,
type_='connectivity', id_filename=None):
'''
Parameters
----------
distance_df_filename: str, path of the csv file contains edges information
num_of_vertices: int, the number of vertices
type_: str, {connectivity, distance}
Returns
----------
A: np.ndarray, adjacency matrix
'''
import csv
A = np.zeros((int(num_of_vertices), int(num_of_vertices)),
dtype=np.float32)
if id_filename != 'None':
with open(id_filename, 'r') as f:
id_dict = {int(i): idx
for idx, i in enumerate(f.read().strip().split('\n'))}
with open(distance_df_filename, 'r') as f:
f.readline()
reader = csv.reader(f)
for row in reader:
if len(row) != 3:
continue
i, j, distance = int(row[0]), int(row[1]), float(row[2])
A[id_dict[i], id_dict[j]] = 1
A[id_dict[j], id_dict[i]] = 1
return A
# Fills cells in the matrix with distances.
with open(distance_df_filename, 'r') as f:
f.readline()
reader = csv.reader(f)
for row in reader:
if len(row) != 3:
continue
i, j, distance = int(row[0]), int(row[1]), float(row[2])
if type_ == 'connectivity':
A[i, j] = 1
A[j, i] = 1
elif type_ == 'distance':
A[i, j] = 1 / distance
A[j, i] = 1 / distance
else:
raise ValueError("type_ error, must be "
"connectivity or distance!")
return A
def init_seed(seed):
'''
Disable cudnn to maximize reproducibility
'''
torch.cuda.cudnn_enabled = False
torch.backends.cudnn.deterministic = True
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
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 masked_mae(preds, labels, null_val=np.nan):
if np.isnan(null_val):
mask = ~torch.isnan(labels)
else:
mask = (labels != null_val)
mask = mask.float()
mask /= torch.mean((mask))
mask = torch.where(torch.isnan(mask), torch.zeros_like(mask), mask)
loss = torch.abs(preds - labels)
loss = loss * mask
loss = torch.where(torch.isnan(loss), torch.zeros_like(loss), loss)
return torch.mean(loss)
def reparameterize(mu, logvar):
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std