-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
161 lines (135 loc) · 4.92 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
import os
import re
import glob
import torch
import random
import logging
import datetime
import numpy as np
from scipy import io
def initial_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
os.environ["CUBLAS_WORKSPACE_CONFIG"]=":4096:8"
torch.cuda.manual_seed(seed)
random.seed(seed)
'''
# --------------------------------------------
# Data Loading
# --------------------------------------------
'''
def load_data(dataset_name, dataroot):
'''
Load the dataset.
Input:
dataset_name: the name of dataset
dataroot: the path of dataset
Output:
tensor: the original data with the shape of `(locations, days, time_intervals)`
'''
dataset_name += "-data-set"
if dataset_name == "Hangzhou-data-set":
file = os.path.join(dataroot, dataset_name, "tensor.mat")
tensor = io.loadmat(file)['tensor']
elif dataset_name == "PeMS-data-set":
file = os.path.join(dataroot, dataset_name, "pems.npy")
tensor = np.load(file).reshape(228, -1, 288)
elif dataset_name == "Portland-data-set":
file = os.path.join(dataroot, dataset_name, "volume.npy") # occupancy, speed and volume
tensor = np.load(file).reshape(1156, -1, 96)
elif dataset_name == "Seattle-data-set":
file = os.path.join(dataroot, dataset_name, "tensor.npz")
tensor = np.load(file)["arr_0"]
try:
tensor = torch.Tensor(tensor)
except:
tensor = torch.Tensor(tensor.astype(np.int16))
return tensor
'''
# --------------------------------------------
# Missing Pattern Generation
# --------------------------------------------
'''
def missing_pattern(dense_tensor, ms, kind="random", block_window=12, seed=1000):
initial_seed(seed)
if kind == "random":
binary_tensor = torch.round(torch.Tensor(np.random.rand(*dense_tensor.shape)) + 0.5 - ms)
elif kind == "non-random":
dim1, dim2, _ = dense_tensor.shape
binary_tensor = torch.round(torch.Tensor(np.random.rand(dim1, dim2)) + 0.5 - ms)[:, :, None]
elif kind == "blackout":
dense_mat = dense_tensor.reshape(dense_tensor.shape[0], -1)
T = dense_mat.shape[1]
binary_blocks = np.round(np.random.rand(T // block_window) + 0.5 - ms)
binary_mat = np.array([binary_blocks] * block_window).reshape(T, order="F")[None, :]
binary_tensor = torch.Tensor(binary_mat.reshape(dense_tensor.shape[1], -1))[None, :, :]
else:
raise ValueError("Only 'random', 'non-random', and 'blackout' 3 kinds of missing patterns.")
if kind == "blackout":
# binary blocks used for showing the missing pattern
return binary_tensor, binary_blocks
else:
return binary_tensor
'''
# --------------------------------------------
# Metrics
# --------------------------------------------
'''
def compute_rmse(var, var_hat):
return torch.sqrt(torch.sum((var - var_hat) ** 2) / var.shape[0])
def compute_mape(var, var_hat):
return torch.sum(torch.abs(var - var_hat) / var) / var.shape[0]
'''
# --------------------------------------------
# logger
# --------------------------------------------
'''
def log(*args, **kwargs):
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S:"), *args, **kwargs)
def logger_info(logger_name, log_path='default_logger.log'):
''' set up logger
modified by Kai Zhang (github: https://github.com/cszn)
'''
log = logging.getLogger(logger_name)
if log.hasHandlers():
print('LogHandlers exist!')
else:
print('LogHandlers setup!')
level = logging.INFO
formatter = logging.Formatter('%(asctime)s.%(msecs)03d : %(message)s', datefmt='%y-%m-%d %H:%M:%S')
fh = logging.FileHandler(log_path, mode='a')
fh.setFormatter(formatter)
log.setLevel(level)
log.addHandler(fh)
# print(len(log.handlers))
sh = logging.StreamHandler()
sh.setFormatter(formatter)
log.addHandler(sh)
def logger_close(logger):
# close the logger
handlers = logger.handlers[:]
for handler in handlers:
logger.removeHandler(handler)
handler.close()
def find_last_checkpoint(params_dir, pretrained_path=None):
"""
Args:
params_dir: model folder
pretrained_path: pretrained model path. If params_dir does not have any model, load from pretrained_path
Return:
init_iter: iteration number
init_path: model path
"""
file_list = glob.glob(os.path.join(params_dir, '*_G.pth'))
if file_list:
iter_exist = []
for file_ in file_list:
iter_current = re.findall(r"(\d+)_G.pth", file_)
iter_exist.append(int(iter_current[0]))
init_iter = max(iter_exist)
init_path = os.path.join(params_dir, '{}_G.pth'.format(init_iter))
else:
init_iter = 0
init_path = pretrained_path
return init_iter, init_path