forked from Davidham3/STSGCN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
241 lines (199 loc) · 7.3 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
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
# -*- coding:utf-8 -*-
import os
import numpy as np
import mxnet as mx
def construct_model(config):
from models.stsgcn import stsgcn
module_type = config['module_type']
act_type = config['act_type']
temporal_emb = config['temporal_emb']
spatial_emb = config['spatial_emb']
use_mask = config['use_mask']
batch_size = config['batch_size']
num_of_vertices = config['num_of_vertices']
num_of_features = config['num_of_features']
points_per_hour = config['points_per_hour']
num_for_predict = config['num_for_predict']
adj_filename = config['adj_filename']
id_filename = config['id_filename']
if id_filename is not None:
if not os.path.exists(id_filename):
id_filename = None
adj = get_adjacency_matrix(adj_filename, num_of_vertices,
id_filename=id_filename)
adj_mx = construct_adj(adj, 3)
print("The shape of localized adjacency matrix: {}".format(
adj_mx.shape), flush=True)
data = mx.sym.var("data")
label = mx.sym.var("label")
adj = mx.sym.Variable('adj', shape=adj_mx.shape,
init=mx.init.Constant(value=adj_mx.tolist()))
adj = mx.sym.BlockGrad(adj)
mask_init_value = mx.init.Constant(value=(adj_mx != 0)
.astype('float32').tolist())
filters = config['filters']
first_layer_embedding_size = config['first_layer_embedding_size']
if first_layer_embedding_size:
data = mx.sym.Activation(
mx.sym.FullyConnected(
data,
flatten=False,
num_hidden=first_layer_embedding_size
),
act_type='relu'
)
else:
first_layer_embedding_size = num_of_features
net = stsgcn(
data, adj, label,
points_per_hour, num_of_vertices, first_layer_embedding_size,
filters, module_type, act_type,
use_mask, mask_init_value, temporal_emb, spatial_emb,
prefix="", rho=1, predict_length=12
)
assert net.infer_shape(
data=(batch_size, points_per_hour, num_of_vertices, 1),
label=(batch_size, num_for_predict, num_of_vertices)
)[1][1] == (batch_size, num_for_predict, num_of_vertices)
return net
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:
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 construct_adj(A, steps):
'''
construct a bigger adjacency matrix using the given matrix
Parameters
----------
A: np.ndarray, adjacency matrix, shape is (N, N)
steps: how many times of the does the new adj mx bigger than A
Returns
----------
new adjacency matrix: csr_matrix, shape is (N * steps, N * steps)
'''
N = len(A)
adj = np.zeros([N * steps] * 2)
for i in range(steps):
adj[i * N: (i + 1) * N, i * N: (i + 1) * N] = A
for i in range(N):
for k in range(steps - 1):
adj[k * N + i, (k + 1) * N + i] = 1
adj[(k + 1) * N + i, k * N + i] = 1
for i in range(len(adj)):
adj[i, i] = 1
return adj
def generate_from_train_val_test(data, transformer):
mean = None
std = None
for key in ('train', 'val', 'test'):
x, y = generate_seq(data[key], 12, 12)
if transformer:
x = transformer(x)
y = transformer(y)
if mean is None:
mean = x.mean()
if std is None:
std = x.std()
yield (x - mean) / std, y
def generate_from_data(data, length, transformer):
mean = None
std = None
train_line, val_line = int(length * 0.6), int(length * 0.8)
for line1, line2 in ((0, train_line),
(train_line, val_line),
(val_line, length)):
x, y = generate_seq(data['data'][line1: line2], 12, 12)
if transformer:
x = transformer(x)
y = transformer(y)
if mean is None:
mean = x.mean()
if std is None:
std = x.std()
yield (x - mean) / std, y
def generate_data(graph_signal_matrix_filename, transformer=None):
'''
shape is (num_of_samples, 12, num_of_vertices, 1)
'''
data = np.load(graph_signal_matrix_filename)
keys = data.keys()
if 'train' in keys and 'val' in keys and 'test' in keys:
for i in generate_from_train_val_test(data, transformer):
yield i
elif 'data' in keys:
length = data['data'].shape[0]
for i in generate_from_data(data, length, transformer):
yield i
else:
raise KeyError("neither data nor train, val, test is in the data")
def generate_seq(data, train_length, pred_length):
seq = np.concatenate([np.expand_dims(
data[i: i + train_length + pred_length], 0)
for i in range(data.shape[0] - train_length - pred_length + 1)],
axis=0)[:, :, :, 0: 1]
return np.split(seq, 2, axis=1)
def mask_np(array, null_val):
if np.isnan(null_val):
return (~np.isnan(null_val)).astype('float32')
else:
return np.not_equal(array, null_val).astype('float32')
def masked_mape_np(y_true, y_pred, null_val=np.nan):
with np.errstate(divide='ignore', invalid='ignore'):
mask = mask_np(y_true, null_val)
mask /= mask.mean()
mape = np.abs((y_pred - y_true) / y_true)
mape = np.nan_to_num(mask * mape)
return np.mean(mape) * 100
def masked_mse_np(y_true, y_pred, null_val=np.nan):
mask = mask_np(y_true, null_val)
mask /= mask.mean()
mse = (y_true - y_pred) ** 2
return np.mean(np.nan_to_num(mask * mse))
def masked_mae_np(y_true, y_pred, null_val=np.nan):
mask = mask_np(y_true, null_val)
mask /= mask.mean()
mae = np.abs(y_true - y_pred)
return np.mean(np.nan_to_num(mask * mae))