-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathdataset.py
153 lines (126 loc) · 5.21 KB
/
dataset.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
import os
import sys
proj_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(proj_dir)
from util import config, file_dir
from datetime import datetime
import numpy as np
import arrow
import metpy.calc as mpcalc
from metpy.units import units
from torch.utils import data
class HazeData(data.Dataset):
def __init__(self, graph,
hist_len=1,
pred_len=24,
dataset_num=1,
flag='Train',
):
if flag == 'Train':
start_time_str = 'train_start'
end_time_str = 'train_end'
elif flag == 'Val':
start_time_str = 'val_start'
end_time_str = 'val_end'
elif flag == 'Test':
start_time_str = 'test_start'
end_time_str = 'test_end'
else:
raise Exception('Wrong Flag!')
self.start_time = self._get_time(config['dataset'][dataset_num][start_time_str])
self.end_time = self._get_time(config['dataset'][dataset_num][end_time_str])
self.data_start = self._get_time(config['dataset']['data_start'])
self.data_end = self._get_time(config['dataset']['data_end'])
self.knowair_fp = file_dir['knowair_fp']
self.graph = graph
self._load_npy()
self._gen_time_arr()
self._process_time()
self._process_feature()
self.feature = np.float32(self.feature)
self.pm25 = np.float32(self.pm25)
self._calc_mean_std()
seq_len = hist_len + pred_len
self._add_time_dim(seq_len)
self._norm()
def _norm(self):
self.feature = (self.feature - self.feature_mean) / self.feature_std
self.pm25 = (self.pm25 - self.pm25_mean) / self.pm25_std
def _add_time_dim(self, seq_len):
def _add_t(arr, seq_len):
t_len = arr.shape[0]
assert t_len > seq_len
arr_ts = []
for i in range(seq_len, t_len):
arr_t = arr[i-seq_len:i]
arr_ts.append(arr_t)
arr_ts = np.stack(arr_ts, axis=0)
return arr_ts
self.pm25 = _add_t(self.pm25, seq_len)
self.feature = _add_t(self.feature, seq_len)
self.time_arr = _add_t(self.time_arr, seq_len)
def _calc_mean_std(self):
self.feature_mean = self.feature.mean(axis=(0,1))
self.feature_std = self.feature.std(axis=(0,1))
self.wind_mean = self.feature_mean[-2:]
self.wind_std = self.feature_std[-2:]
self.pm25_mean = self.pm25.mean()
self.pm25_std = self.pm25.std()
def _process_feature(self):
metero_var = config['data']['metero_var']
metero_use = config['experiments']['metero_use']
metero_idx = [metero_var.index(var) for var in metero_use]
self.feature = self.feature[:,:,metero_idx]
u = self.feature[:, :, -2] * units.meter / units.second
v = self.feature[:, :, -1] * units.meter / units.second
speed = 3.6 * mpcalc.wind_speed(u, v)._magnitude
direc = mpcalc.wind_direction(u, v)._magnitude
h_arr = []
w_arr = []
for i in self.time_arrow:
h_arr.append(i.hour)
w_arr.append(i.isoweekday())
h_arr = np.stack(h_arr, axis=-1)
w_arr = np.stack(w_arr, axis=-1)
h_arr = np.repeat(h_arr[:, None], self.graph.node_num, axis=1)
w_arr = np.repeat(w_arr[:, None], self.graph.node_num, axis=1)
self.feature = np.concatenate([self.feature, h_arr[:, :, None], w_arr[:, :, None],
speed[:, :, None], direc[:, :, None]
], axis=-1)
def _process_time(self):
start_idx = self._get_idx(self.start_time)
end_idx = self._get_idx(self.end_time)
self.pm25 = self.pm25[start_idx: end_idx+1, :]
self.feature = self.feature[start_idx: end_idx+1, :]
self.time_arr = self.time_arr[start_idx: end_idx+1]
self.time_arrow = self.time_arrow[start_idx: end_idx + 1]
def _gen_time_arr(self):
self.time_arrow = []
self.time_arr = []
for time_arrow in arrow.Arrow.interval('hour', self.data_start, self.data_end.shift(hours=+3), 3):
self.time_arrow.append(time_arrow[0])
self.time_arr.append(time_arrow[0].timestamp)
self.time_arr = np.stack(self.time_arr, axis=-1)
def _load_npy(self):
self.knowair = np.load(self.knowair_fp)
self.feature = self.knowair[:,:,:-1]
self.pm25 = self.knowair[:,:,-1:]
def _get_idx(self, t):
t0 = self.data_start
return int((t.timestamp - t0.timestamp) / (60 * 60 * 3))
def _get_time(self, time_yaml):
arrow_time = arrow.get(datetime(*time_yaml[0]), time_yaml[1])
return arrow_time
def __len__(self):
return len(self.pm25)
def __getitem__(self, index):
return self.pm25[index], self.feature[index], self.time_arr[index]
if __name__ == '__main__':
from graph import Graph
graph = Graph()
train_data = HazeData(graph, flag='Train')
val_data = HazeData(graph, flag='Val')
test_data = HazeData(graph, flag='Test')
print(len(train_data))
print(len(val_data))
print(len(test_data))