-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
562 lines (466 loc) · 20.8 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
from __future__ import division
import os
import zipfile
import numpy as np
import scipy.sparse as sp
import pandas as pd
from math import radians, cos, sin, asin, sqrt
#from sklearn.externals import joblib
import joblib
import scipy.io
import torch
from torch import nn
"""
Geographical information calculation
"""
def get_long_lat(sensor_index,loc = None):
"""
Input the index out from 0-206 to access the longitude and latitude of the nodes
"""
if loc is None:
locations = pd.read_csv('data/metr/graph_sensor_locations.csv')
else:
locations = loc
lng = locations['longitude'].loc[sensor_index]
lat = locations['latitude'].loc[sensor_index]
return lng.to_numpy(),lat.to_numpy()
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371
return c * r * 1000
"""
Load datasets
"""
def load_metr_la_rdata():
if (not os.path.isfile("data/metr/adj_mat.npy")
or not os.path.isfile("data/metr/node_values.npy")):
with zipfile.ZipFile("data/metr/METR-LA.zip", 'r') as zip_ref:
zip_ref.extractall("data/metr/")
A = np.load("data/metr/adj_mat.npy")
X = np.load("data/metr/node_values.npy").transpose((1, 2, 0))
X = X.astype(np.float32)
return A, X
def generate_nerl_data():
# %% Obtain all the file names
filepath = 'data/nrel/al-pv-2006'
files = os.listdir(filepath)
# %% Begin parse the file names and store them in a pandas Dataframe
tp = [] # Type
lat = [] # Latitude
lng =[] # Longitude
yr = [] # Year
pv_tp = [] # PV_type
cap = [] # Capacity MW
time_itv = [] # Time interval
file_names =[]
for _file in files:
parse = _file.split('_')
if parse[-2] == '5':
tp.append(parse[0])
lat.append(np.double(parse[1]))
lng.append(np.double(parse[2]))
yr.append(np.int(parse[3]))
pv_tp.append(parse[4])
cap.append(np.int(parse[5].split('MW')[0]))
time_itv.append(parse[6])
file_names.append(_file)
else:
pass
files_info = pd.DataFrame(
np.array([tp,lat,lng,yr,pv_tp,cap,time_itv,file_names]).T,
columns=['type','latitude','longitude','year','pv_type','capacity','time_interval','file_name']
)
# %% Read the time series into a numpy 2-D array with 137x105120 size
X = np.zeros((len(files_info),365*24*12))
for i in range(files_info.shape[0]):
f = filepath + '/' + files_info['file_name'].loc[i]
d = pd.read_csv(f)
assert d.shape[0] == 365*24*12, 'Data missing!'
X[i,:] = d['Power(MW)']
print(i/files_info.shape[0]*100,'%')
np.save('data/nrel/nerl_X.npy',X)
files_info.to_pickle('data/nrel/nerl_file_infos.pkl')
# %% Get the adjacency matrix based on the inverse of distance between two nodes
A = np.zeros((files_info.shape[0],files_info.shape[0]))
for i in range(files_info.shape[0]):
for j in range(i+1,files_info.shape[0]):
lng1 = lng[i]
lng2 = lng[j]
lat1 = lat[i]
lat2 = lat[j]
d = haversine(lng1,lat1,lng2,lat2)
A[i,j] = d
A = A/7500 # distance / 7.5 km
A += A.T + np.diag(A.diagonal())
A = np.exp(-A)
np.save('data/nrel/nerl_A.npy',A)
def load_nerl_data():
if (not os.path.isfile("data/nrel/nerl_X.npy")
or not os.path.isfile("data/nrel/nerl_A.npy")):
with zipfile.ZipFile("data/nrel/al-pv-2006.zip", 'r') as zip_ref:
zip_ref.extractall("data/nrel/al-pv-2006")
generate_nerl_data()
X = np.load('data/nrel/nerl_X.npy')
A = np.load('data/nrel/nerl_A.npy')
files_info = pd.read_pickle('data/nrel/nerl_file_infos.pkl')
X = X.astype(np.float32)
# X = (X - X.mean())/X.std()
return A,X,files_info
def generate_ushcn_data():
pos = []
Utensor = np.zeros((1218, 120, 12, 2))
Omissing = np.ones((1218, 120, 12, 2))
with open("data/ushcn/Ulocation", "r") as f:
loc = 0
for line in f.readlines():
poname = line[0:11]
pos.append(line[13:30])
with open("data/ushcn/ushcn.v2.5.5.20191231/"+ poname +".FLs.52j.prcp", "r") as fp:
temp = 0
for linep in fp.readlines():
if int(linep[12:16]) > 1899:
for i in range(12):
str_temp = linep[17 + 9*i:22 + 9*i]
p_temp = int(str_temp)
if p_temp == -9999:
Omissing[loc, temp, i, 0] = 0
else:
Utensor[loc, temp, i, 0] = p_temp
temp = temp + 1
with open("data/ushcn/ushcn.v2.5.5.20191231/"+ poname +".FLs.52j.tavg", "r") as ft:
temp = 0
for linet in ft.readlines():
if int(linet[12:16]) > 1899:
for i in range(12):
str_temp = linet[17 + 9*i:22 + 9*i]
t_temp = int(str_temp)
if t_temp == -9999:
Omissing[loc, temp, i, 1] = 0
else:
Utensor[loc, temp, i, 1] = t_temp
temp = temp + 1
loc = loc + 1
latlon =np.loadtxt("data/ushcn/latlon.csv",delimiter=",")
sim = np.zeros((1218,1218))
for i in range(1218):
for j in range(1218):
sim[i,j] = haversine(latlon[i, 1], latlon[i, 0], latlon[j, 1], latlon[j, 0]) #RBF
sim = np.exp(-sim/10000/10)
joblib.dump(Utensor,'data/ushcn/Utensor.joblib')
joblib.dump(Omissing,'data/ushcn/Omissing.joblib')
joblib.dump(sim,'data/ushcn/sim.joblib')
def load_udata():
if (not os.path.isfile("data/ushcn/Utensor.joblib")
or not os.path.isfile("data/ushcn/sim.joblib")):
with zipfile.ZipFile("data/ushcn/ushcn.v2.5.5.20191231.zip", 'r') as zip_ref:
zip_ref.extractall("data/ushcn/ushcn.v2.5.5.20191231/")
generate_ushcn_data()
X = joblib.load('data/ushcn/Utensor.joblib')
A = joblib.load('data/ushcn/sim.joblib')
Omissing = joblib.load('data/ushcn/Omissing.joblib')
X = X.astype(np.float32)
return A,X,Omissing
def load_sedata():
assert os.path.isfile('data/sedata/A.mat')
assert os.path.isfile('data/sedata/mat.csv')
A_mat = scipy.io.loadmat('data/sedata/A.mat')
A = A_mat['A']
X = pd.read_csv('data/sedata/mat.csv',index_col=0)
X = X.to_numpy()
return A,X
def load_pems_data():
assert os.path.isfile('data/pems/pems-bay.h5')
assert os.path.isfile('data/pems/distances_bay_2017.csv')
df = pd.read_hdf('data/pems/pems-bay.h5')
transfer_set = df.as_matrix()
distance_df = pd.read_csv('data/pems/distances_bay_2017.csv', dtype={'from': 'str', 'to': 'str'})
normalized_k = 0.1
dist_mx = np.zeros((325, 325), dtype=np.float32)
dist_mx[:] = np.inf
sensor_ids = df.columns.values.tolist()
sensor_id_to_ind = {}
for i, sensor_id in enumerate(sensor_ids):
sensor_id_to_ind[sensor_id] = i
for row in distance_df.values:
if row[0] not in sensor_id_to_ind or row[1] not in sensor_id_to_ind:
continue
dist_mx[sensor_id_to_ind[row[0]], sensor_id_to_ind[row[1]]] = row[2]
distances = dist_mx[~np.isinf(dist_mx)].flatten()
std = distances.std()
adj_mx = np.exp(-np.square(dist_mx / std))
adj_mx[adj_mx < normalized_k] = 0
A_new = adj_mx
return transfer_set,A_new
"""
Dynamically construct the adjacent matrix
"""
def get_Laplace(A):
"""
Returns the laplacian adjacency matrix. This is for C_GCN
"""
if A[0, 0] == 1:
A = A - np.diag(np.ones(A.shape[0], dtype=np.float32)) # if the diag has been added by 1s
D = np.array(np.sum(A, axis=1)).reshape((-1,))
D[D <= 10e-5] = 10e-5
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_normalized_adj(A):
"""
Returns the degree normalized adjacency matrix. This is for K_GCN
"""
if A[0, 0] == 0:
A = A + np.diag(np.ones(A.shape[0], dtype=np.float32)) # if the diag has been added by 1s
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 calculate_random_walk_matrix(adj_mx):
"""
Returns the random walk adjacency matrix. This is for D_GCN
"""
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.
d_mat_inv = sp.diags(d_inv)
random_walk_mx = d_mat_inv.dot(adj_mx).tocoo()
return random_walk_mx.toarray()
def test_error_missing(STmodel, unknow_set, test_data, A_s, E_maxvalue, Missing0,test_truth):
"""
:param STmodel: The graph neural networks
:unknow_set: The unknow locations for spatial prediction
:test_data: The true value test_data of shape (test_num_timesteps, num_nodes)
:A_s: The full adjacent matrix
:Missing0: True: 0 in original datasets means missing data
:return: NAE, MAPE and RMSE
"""
unknow_set = set(unknow_set)
time_dim = STmodel.time_dimension
test_omask = np.ones(test_data.shape)
if Missing0 == True:
test_omask[test_data == 0] = 0
test_inputs = (test_data * test_omask).astype('float32')
test_inputs_s = test_inputs
missing_index = np.ones(np.shape(test_data))
missing_index[:, list(unknow_set)] = 0
missing_index_s = missing_index
o = np.zeros([test_truth.shape[0]//time_dim*time_dim, test_inputs_s.shape[1]]) #Separate the test data into several h period
for i in range(0, test_truth.shape[0]//time_dim*time_dim, time_dim):
inputs = test_inputs_s[i:i+time_dim, :]
missing_inputs = missing_index_s[i:i+time_dim, :]
T_inputs = inputs*missing_inputs
T_inputs = T_inputs/E_maxvalue
T_inputs = np.expand_dims(T_inputs, axis = 0)
T_inputs = torch.from_numpy(T_inputs.astype('float32'))
A_q = torch.from_numpy((calculate_random_walk_matrix(A_s).T).astype('float32'))
A_h = torch.from_numpy((calculate_random_walk_matrix(A_s.T).T).astype('float32'))
imputation = STmodel(T_inputs, A_q, A_h)
imputation = imputation.data.numpy()
o[i:i+time_dim, :] = imputation[0, :, :]
o = o*E_maxvalue
truth = test_truth[0:test_data.shape[0]//time_dim*time_dim]
o[missing_index_s[0:test_data.shape[0]//time_dim*time_dim] == 1] = truth[missing_index_s[0:test_data.shape[0]//time_dim*time_dim] == 1]
test_mask = 1 - missing_index_s[0:test_data.shape[0]//time_dim*time_dim]
if Missing0 == True:
test_mask[truth == 0] = 0
o[truth == 0] = 0
o_ = o[:,list(unknow_set)]
truth_ = truth[:,list(unknow_set)]
test_mask_ = test_mask[:,list(unknow_set)]
MAE = np.sum(np.abs(o_ - truth_))/np.sum( test_mask_)
RMSE = np.sqrt(np.sum((o_ - truth_)*(o_ - truth_))/np.sum( test_mask_) )
# MAPE = np.sum(np.abs(o - truth)/(truth + 1e-5))/np.sum( test_mask)
R2 = 1 - np.sum( (o_ - truth_)*(o_ - truth_) )/np.sum( (truth_ - truth_.mean())*(truth_-truth_.mean() ) )
return MAE, RMSE, R2, o
def test_error(STmodel, unknow_set, test_data, A_s, E_maxvalue, Missing0):
"""
:param STmodel: The graph neural networks
:unknow_set: The unknow locations for spatial prediction
:test_data: The true value test_data of shape (test_num_timesteps, num_nodes)
:A_s: The full adjacent matrix
:Missing0: True: 0 in original datasets means missing data
:return: NAE, MAPE and RMSE
"""
unknow_set = set(unknow_set)
time_dim = STmodel.time_dimension
test_omask = np.ones(test_data.shape)
if Missing0 == True:
test_omask[test_data == 0] = 0
test_inputs = (test_data * test_omask).astype('float32')
test_inputs_s = test_inputs
missing_index = np.ones(np.shape(test_data))
missing_index[:, list(unknow_set)] = 0
missing_index_s = missing_index
o = np.zeros([test_data.shape[0]//time_dim*time_dim, test_inputs_s.shape[1]]) #Separate the test data into several h period
for i in range(0, test_data.shape[0]//time_dim*time_dim, time_dim):
inputs = test_inputs_s[i:i+time_dim, :]
missing_inputs = missing_index_s[i:i+time_dim, :]
T_inputs = inputs*missing_inputs
T_inputs = T_inputs/E_maxvalue
T_inputs = np.expand_dims(T_inputs, axis = 0)
T_inputs = torch.from_numpy(T_inputs.astype('float32'))
A_q = torch.from_numpy((calculate_random_walk_matrix(A_s).T).astype('float32'))
A_h = torch.from_numpy((calculate_random_walk_matrix(A_s.T).T).astype('float32'))
imputation = STmodel(T_inputs, A_q, A_h)
imputation = imputation.data.numpy()
o[i:i+time_dim, :] = imputation[0, :, :]
o = o*E_maxvalue
truth = test_inputs_s[0:test_data.shape[0]//time_dim*time_dim]
o[missing_index_s[0:test_data.shape[0]//time_dim*time_dim] == 1] = truth[missing_index_s[0:test_data.shape[0]//time_dim*time_dim] == 1]
test_mask = 1 - missing_index_s[0:test_data.shape[0]//time_dim*time_dim]
if Missing0 == True:
test_mask[truth == 0] = 0
o[truth == 0] = 0
o_ = o[:,list(unknow_set)]
truth_ = truth[:,list(unknow_set)]
test_mask_ = test_mask[:,list(unknow_set)]
MAE = np.sum(np.abs(o_ - truth_))/np.sum( test_mask_)
RMSE = np.sqrt(np.sum((o_ - truth_)*(o_ - truth_))/np.sum( test_mask_) )
# MAPE = np.sum(np.abs(o - truth)/(truth + 1e-5))/np.sum( test_mask)
R2 = 1 - np.sum( (o_ - truth_)*(o_ - truth_) )/np.sum( (truth_ - truth_.mean())*(truth_-truth_.mean() ) )
return MAE, RMSE, R2, o
def rolling_test_error(STmodel, unknow_set, test_data, A_s, E_maxvalue,Missing0):
"""
:It only calculates the last time points' prediction error, and updates inputs each time point
:param STmodel: The graph neural networks
:unknow_set: The unknow locations for spatial prediction
:test_data: The true value test_data of shape (test_num_timesteps, num_nodes)
:A_s: The full adjacent matrix
:Missing0: True: 0 in original datasets means missing data
:return: NAE, MAPE and RMSE
"""
unknow_set = set(unknow_set)
time_dim = STmodel.time_dimension
test_omask = np.ones(test_data.shape)
if Missing0 == True:
test_omask[test_data == 0] = 0
test_inputs = (test_data * test_omask).astype('float32')
test_inputs_s = test_inputs
missing_index = np.ones(np.shape(test_data))
missing_index[:, list(unknow_set)] = 0
missing_index_s = missing_index
o = np.zeros([test_data.shape[0] - time_dim, test_inputs_s.shape[1]])
for i in range(0, test_data.shape[0] - time_dim):
inputs = test_inputs_s[i:i+time_dim, :]
missing_inputs = missing_index_s[i:i+time_dim, :]
MF_inputs = inputs * missing_inputs
MF_inputs = np.expand_dims(MF_inputs, axis = 0)
MF_inputs = torch.from_numpy(MF_inputs.astype('float32'))
A_q = torch.from_numpy((calculate_random_walk_matrix(A_s).T).astype('float32'))
A_h = torch.from_numpy((calculate_random_walk_matrix(A_s.T).T).astype('float32'))
imputation = STmodel(MF_inputs, A_q, A_h)
imputation = imputation.data.numpy()
o[i, :] = imputation[0, time_dim-1, :]
truth = test_inputs_s[time_dim:test_data.shape[0]]
o[missing_index_s[time_dim:test_data.shape[0]] == 1] = truth[missing_index_s[time_dim:test_data.shape[0]] == 1]
o = o*E_maxvalue
truth = test_inputs_s[0:test_data.shape[0]//time_dim*time_dim]
test_mask = 1 - missing_index_s[time_dim:test_data.shape[0]]
if Missing0 == True:
test_mask[truth == 0] = 0
o[truth == 0] = 0
MAE = np.sum(np.abs(o - truth))/np.sum( test_mask)
RMSE = np.sqrt(np.sum((o - truth)*(o - truth))/np.sum( test_mask) )
MAPE = np.sum(np.abs(o - truth)/(truth + 1e-5))/np.sum( test_mask) #avoid x/0
return MAE, RMSE, MAPE, o
def test_error_cap(STmodel, unknow_set, full_set, test_set, A,time_dim,capacities):
unknow_set = set(unknow_set)
test_omask = np.ones(test_set.shape)
test_omask[test_set == 0] = 0
test_inputs = (test_set * test_omask).astype('float32')
test_inputs_s = test_inputs#[:, list(proc_set)]
missing_index = np.ones(np.shape(test_inputs))
missing_index[:, list(unknow_set)] = 0
missing_index_s = missing_index#[:, list(proc_set)]
A_s = A#[:, list(proc_set)][list(proc_set), :]
o = np.zeros([test_set.shape[0]//time_dim*time_dim, test_inputs_s.shape[1]])
for i in range(0, test_set.shape[0]//time_dim*time_dim, time_dim):
inputs = test_inputs_s[i:i+time_dim, :]
missing_inputs = missing_index_s[i:i+time_dim, :]
MF_inputs = inputs*missing_inputs
MF_inputs = MF_inputs
MF_inputs = np.expand_dims(MF_inputs, axis = 0)
MF_inputs = torch.from_numpy(MF_inputs.astype('float32'))
A_q = torch.from_numpy((calculate_random_walk_matrix(A_s).T).astype('float32'))
A_h = torch.from_numpy((calculate_random_walk_matrix(A_s.T).T).astype('float32'))
imputation = STmodel(MF_inputs, A_q, A_h)
imputation = imputation.data.numpy()
o[i:i+time_dim, :] = imputation[0, :, :]
o = o*capacities
truth = test_inputs_s[0:test_set.shape[0]//time_dim*time_dim]
truth = truth*capacities
o[missing_index_s[0:test_set.shape[0]//time_dim*time_dim] == 1] = truth[missing_index_s[0:test_set.shape[0]//time_dim*time_dim] == 1]
o[truth == 0] = 0
test_mask = 1 - missing_index_s[0:test_set.shape[0]//time_dim*time_dim]
test_mask[truth == 0] = 0
o_ = o[:,list(unknow_set)]
truth_ = truth[:,list(unknow_set)]
test_mask_ = test_mask[:,list(unknow_set)]
MAE = np.sum(np.abs(o_ - truth_))/np.sum( test_mask_)
RMSE = np.sqrt(np.sum((o_ - truth_)*(o_ - truth_))/np.sum( test_mask_) )
# MAPE = np.sum(np.abs(o - truth)/(truth + 1e-5))/np.sum( test_mask)
R2 = 1 - np.sum( (o_ - truth_)*(o_ - truth_) )/np.sum( (truth_ - truth_.mean())*(truth_-truth_.mean() ) )
return MAE, RMSE, R2, o
def compute_MAE(X_masked,X_true,X_hat): #Only calculate the errors on the masked and nonzero positions
pos_test = np.where((X_true != 0) & (X_masked == 0))
MAE = np.sum(abs(X_true[pos_test]-X_hat[pos_test]))/X_true[pos_test].shape[0]
return MAE
def compute_RMSE(X_masked,X_true,X_hat):
pos_test = np.where((X_true != 0) & (X_masked == 0))
RMSE = np.sqrt(((X_true[pos_test]-X_hat[pos_test])**2).sum()/X_true[pos_test].shape[0])
return RMSE
def compute_MAPE(X_masked,X_true,X_hat):
pos_test = np.where((X_true != 0) & (X_masked == 0))
MAPE = np.sum(np.abs(X_true[pos_test]-X_hat[pos_test]) / X_true[pos_test]) / X_true[pos_test].shape[0]
return MAPE
def compute_WMAPE(X_masked,X_true,X_hat):
pos_test = np.where((X_true != 0) & (X_masked == 0))
WMAPE = np.sum(np.abs(X_true[pos_test]-X_hat[pos_test])) / np.sum(X_true[pos_test])
return WMAPE
def get_missing_rate(X_lost):
#统计完整数据集中流量为0的通道数
o_channel_num = (X_lost == 0).astype(int).sum().sum()
matrix_miss_rate = o_channel_num/(X_lost.size)
return matrix_miss_rate
#构造拉普拉斯矩阵,注意需要为对称正定矩阵
def construct_Laplacian(adj):
degree = np.diag(np.sum(adj,axis=1))
temp = degree-adj
if np.allclose(temp,temp.transpose()) and np.linalg.eigvals(temp).all()>=0:
Lap = temp.copy()
else:
print('Error Construction')
Lap = None
return Lap
def TensorFromMat(mat,dim):
#Construct a 3D tensor from a matrix
days_slice = [(start_i,start_i + dim[0]) for start_i in list(range(0,dim[0]*dim[2],dim[0]))]
array_list = []
for day_slice in days_slice:
start_i,end_i = day_slice[0],day_slice[1]
array_slice = mat[start_i:end_i,:]
array_list.append(array_slice)
tensor3d = np.array(np.stack(array_list,axis = 0).astype('float64'))
tensor3d = np.moveaxis(tensor3d,0,-1)
return tensor3d
def Tensor2Mat(tensor):
#convert a tensor into a matrix by flattening the 'day' mode to 'time interval'.
for k in range(np.shape(tensor)[-1]):
if k == 0:
stacked = np.vstack(tensor[:,:,k])
else:
stacked = np.vstack((stacked,tensor[:,:,k]))
return stacked