-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathprepareData.py
More file actions
328 lines (276 loc) · 11.9 KB
/
Copy pathprepareData.py
File metadata and controls
328 lines (276 loc) · 11.9 KB
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
import pandas as pd
from datetime import datetime
import os
import six.moves.cPickle as pickle
import numpy as np
import h5py
import time
from utils import *
def remove_incomplete_days(data, timestamps, T=48):
# remove a certain day which has not 48 timestamps
days = [] # available days: some day only contain some seqs
days_incomplete = []
i = 0
while i < len(timestamps):
if int(timestamps[i][8:]) != 1:
i += 1
elif i+T-1 < len(timestamps) and int(timestamps[i+T-1][8:]) == T:
days.append(timestamps[i][:8])
i += T
else:
days_incomplete.append(timestamps[i][:8])
i += 1
print("incomplete days: ", days_incomplete)
days = set(days)
idx = []
for i, t in enumerate(timestamps):
if t[:8] in days:
idx.append(i)
data = data[idx]
timestamps = [timestamps[i] for i in idx]
return data, timestamps
def load_stdata(fname):
f = h5py.File(fname, 'r')
data = f['data'].value
timestamps = f['date'].value
f.close()
return data, timestamps
def string2timestamp(strings, T=48):
'''
strings: list, eg. ['2017080912','2017080913']
return: list, eg. [Timestamp('2017-08-09 05:30:00'), Timestamp('2017-08-09 06:00:00')]
'''
timestamps = []
time_per_slot = 24.0 / T
num_per_T = T // 24
for t in strings:
year, month, day, slot = int(t[:4]), int(t[4:6]), int(t[6:8]), int(t[8:])-1
timestamps.append(pd.Timestamp(datetime(year, month, day, hour=int(slot * time_per_slot), minute=(slot % num_per_T) * int(60.0 * time_per_slot))))
return timestamps
class STMatrix(object):
"""docstring for STMatrix"""
def __init__(self, data, timestamps, T=48, CheckComplete=True):
super(STMatrix, self).__init__()
assert len(data) == len(timestamps)
self.data = data
self.data_1 = data[:, 0, :, :]
self.data_2 = data[:, 1, :, :]
self.timestamps = timestamps
self.T = T
self.pd_timestamps = string2timestamp(timestamps, T=self.T)
if CheckComplete:
self.check_complete()
# index
self.make_index()
def make_index(self):
self.get_index = dict()
for i, ts in enumerate(self.pd_timestamps):
self.get_index[ts] = i
def check_complete(self):
missing_timestamps = []
offset = pd.DateOffset(minutes=24 * 60 // self.T)
pd_timestamps = self.pd_timestamps
i = 1
while i < len(pd_timestamps):
if pd_timestamps[i - 1] + offset != pd_timestamps[i]:
missing_timestamps.append("(%s -- %s)" % (pd_timestamps[i - 1], pd_timestamps[i]))
i += 1
for v in missing_timestamps:
print(v)
assert len(missing_timestamps) == 0
def get_matrix(self, timestamp):
return self.data[self.get_index[timestamp]]
def get_matrix_1(self, timestamp): # in_flow
ori_matrix = self.data_1[self.get_index[timestamp]]
new_matrix = ori_matrix[np.newaxis, :]
# print("new_matrix shape:",new_matrix.shape) #(1, 32, 32)
return new_matrix
def get_matrix_2(self, timestamp): # out_flow
ori_matrix = self.data_2[self.get_index[timestamp]]
new_matrix = ori_matrix[np.newaxis, :]
# print("new_matrix shape:",new_matrix.shape) #(1, 32, 32)
return new_matrix
def save(self, fname):
pass
def check_it(self, depends):
for d in depends:
if d not in self.get_index.keys():
return False
return True
def create_dataset_3D(self, len_closeness=3, len_trend=3, TrendInterval=7, len_period=3, PeriodInterval=1):
offset_frame = pd.DateOffset(minutes=24 * 60 // self.T)
XC = []
XP = []
XT = []
Y = []
timestamps_Y = []
depends = [range(1, len_closeness + 1),
[PeriodInterval * self.T * j for j in range(1, len_period + 1)],
[TrendInterval * self.T * j for j in range(1, len_trend + 1)]]
i = max(self.T * TrendInterval * len_trend, self.T * PeriodInterval * len_period, len_closeness)
while i < len(self.pd_timestamps):
Flag = True
for depend in depends:
if Flag is False:
break
Flag = self.check_it([self.pd_timestamps[i] - j * offset_frame for j in depend])
if Flag is False:
i += 1
continue
# closeness
c_1_depends = list(depends[0]) # in_flow
c_1_depends.sort(reverse=True)
# print('----- c_1_depends:',c_1_depends)
c_2_depends = list(depends[0]) # out_flow
c_2_depends.sort(reverse=True)
# print('----- c_2_depends:',c_2_depends)
x_c_1 = [self.get_matrix_1(self.pd_timestamps[i] - j * offset_frame) for j in
c_1_depends] # [(1,32,32),(1,32,32),(1,32,32)] in
x_c_2 = [self.get_matrix_2(self.pd_timestamps[i] - j * offset_frame) for j in
c_2_depends] # [(1,32,32),(1,32,32),(1,32,32)] out
x_c_1_all = np.vstack(x_c_1) # x_c_1_all.shape (3, 32, 32)
x_c_2_all = np.vstack(x_c_2) # x_c_1_all.shape (3, 32, 32)
x_c_1_new = x_c_1_all[np.newaxis, :] # (1, 3, 32, 32)
x_c_2_new = x_c_2_all[np.newaxis, :] # (1, 3, 32, 32)
x_c = np.vstack([x_c_1_new, x_c_2_new]) # (2, 3, 32, 32)
# period
p_depends = list(depends[1])
if (len(p_depends) > 0):
p_depends.sort(reverse=True)
# print('----- p_depends:',p_depends)
x_p_1 = [self.get_matrix_1(self.pd_timestamps[i] - j * offset_frame) for j in p_depends]
x_p_2 = [self.get_matrix_2(self.pd_timestamps[i] - j * offset_frame) for j in p_depends]
x_p_1_all = np.vstack(x_p_1) # [(3,32,32),(3,32,32),...]
x_p_2_all = np.vstack(x_p_2) # [(3,32,32),(3,32,32),...]
x_p_1_new = x_p_1_all[np.newaxis, :] # (1, 3, 32, 32)
x_p_2_new = x_p_2_all[np.newaxis, :] # (1, 3, 32, 32)
x_p = np.vstack([x_p_1_new, x_p_2_new]) # (2, 3, 32, 32)
# trend
t_depends = list(depends[2])
if (len(t_depends) > 0):
t_depends.sort(reverse=True)
x_t_1 = [self.get_matrix_1(self.pd_timestamps[i] - j * offset_frame) for j in t_depends]
x_t_2 = [self.get_matrix_2(self.pd_timestamps[i] - j * offset_frame) for j in t_depends]
x_t_1_all = np.vstack(x_t_1) # [(3,32,32),(3,32,32),...]
x_t_2_all = np.vstack(x_t_2) # [(3,32,32),(3,32,32),...]
x_t_1_new = x_t_1_all[np.newaxis, :] # (1, 3, 32, 32)
x_t_2_new = x_t_2_all[np.newaxis, :] # (1, 3, 32, 32)
x_t = np.vstack([x_t_1_new, x_t_2_new]) # (2, 3, 32, 32)
y = self.get_matrix(self.pd_timestamps[i])
if len_closeness > 0:
XC.append(x_c)
if len_period > 0:
XP.append(x_p)
if len_trend > 0:
XT.append(x_t)
Y.append(y)
timestamps_Y.append(self.timestamps[i])
i += 1
XC = np.asarray(XC)
XP = np.asarray(XP)
XT = np.asarray(XT)
Y = np.asarray(Y)
print("3D matrix - XC shape: ", XC.shape, "XP shape: ", XP.shape, "XT shape: ", XT.shape, "Y shape:", Y.shape)
return XC, XP, XT, Y, timestamps_Y
def timestamp2vec(timestamps):
# tm_wday range [0, 6], Monday is 0
vec = [time.strptime(str(t[:8], encoding='utf-8'), '%Y%m%d').tm_wday for t in timestamps] # python3
ret = []
for i in vec:
v = [0 for _ in range(7)]
v[i] = 1
if i >= 5:
v.append(0) # weekend
else:
v.append(1) # weekday
ret.append(v)
return np.asarray(ret)
def load_data(filename, T=24, nb_flow=2, len_closeness=None, len_period=None, len_trend=None, len_test=None, meta_data=True):
assert (len_closeness + len_period + len_trend > 0)
# load data
data, timestamps = load_stdata(os.path.join("data",filename))
# remove a certain day which does not have 48 timestamps
data, timestamps = remove_incomplete_days(data, timestamps, T)
data = data[:, :nb_flow]
data[data < 0] = 0.
data_all = [data]
timestamps_all = [timestamps]
# minmax_scale
data_train = data[:-len_test]
print('train_data shape: ', data_train.shape)
mmn = MinMaxNormalization()
mmn.fit(data_train)
data_all_mmn = []
for d in data_all:
data_all_mmn.append(mmn.transform(d))
XC, XP, XT = [], [], []
Y = []
timestamps_Y = []
for data, timestamps in zip(data_all_mmn, timestamps_all):
# instance-based dataset --> sequences with format as (X, Y) where X is a sequence of images and Y is an image.
st = STMatrix(data, timestamps, T, CheckComplete=False)
_XC, _XP, _XT, _Y, _timestamps_Y = st.create_dataset_3D(len_closeness=len_closeness, len_period=len_period,
len_trend=len_trend)
XC.append(_XC)
XP.append(_XP)
XT.append(_XT)
Y.append(_Y)
timestamps_Y += _timestamps_Y
XC = np.vstack(XC)
XP = np.vstack(XP)
XT = np.vstack(XT)
Y = np.vstack(Y)
print("XC shape: ", XC.shape, "XP shape: ", XP.shape, "XT shape: ", XT.shape, "Y shape:", Y.shape)
XC_train, XP_train, XT_train, Y_train = XC[:-len_test], XP[:-len_test], XT[:-len_test], Y[:-len_test]
XC_test, XP_test, XT_test, Y_test = XC[-len_test:], XP[-len_test:], XT[-len_test:], Y[-len_test:]
timestamp_train, timestamp_test = timestamps_Y[:-len_test], timestamps_Y[-len_test:]
X_train = []
X_test = []
for l, X_ in zip([len_closeness, len_period, len_trend], [XC_train, XP_train, XT_train]):
if l > 0:
X_train.append(X_)
for l, X_ in zip([len_closeness, len_period, len_trend], [XC_test, XP_test, XT_test]):
if l > 0:
X_test.append(X_)
print('train shape:', XC_train.shape, Y_train.shape, 'test shape: ', XC_test.shape, Y_test.shape)
# load meta feature
if meta_data:
meta_feature = timestamp2vec(timestamps_Y)
metadata_dim = meta_feature.shape[1]
meta_feature_train, meta_feature_test = meta_feature[:-len_test], meta_feature[-len_test:]
X_train.append(meta_feature_train)
X_test.append(meta_feature_test)
else:
metadata_dim = None
for _X in X_train:
print(_X.shape, )
print()
for _X in X_test:
print(_X.shape, )
print()
return X_train, Y_train, X_test, Y_test, mmn, metadata_dim, timestamp_train, timestamp_test
T = 24 # number of time intervals in one day
lr = 0.0002 # learning rate
len_closeness = 6 # length of closeness dependent sequence
len_period = 0 # length of peroid dependent sequence
len_trend = 4 # length of trend dependent sequence
nb_residual_unit = 4 # number of residual units
nb_flow = 2 # there are two types of flows: new-flow and end-flow
days_test = 10 # divide data into two subsets: Train & Test, of which the test set is the last 10 days
len_test = T * days_test
map_height, map_width = 16, 8 # grid size
original_filename = 'NYC14_M16x8_T60_NewEnd.h5'
X_train, Y_train, X_test, Y_test, mmn, external_dim, timestamp_train, timestamp_test = \
load_data(original_filename, T=T, nb_flow=nb_flow, len_closeness=len_closeness, len_period=len_period,
len_trend=len_trend, len_test=len_test, meta_data=False)
filename = os.path.join("data", 'NYC_c%d_p%d_t%d_noext'%(len_closeness, len_period, len_trend))
f = open(filename, 'wb')
pickle.dump(X_train, f)
pickle.dump(Y_train, f)
pickle.dump(X_test, f)
pickle.dump(Y_test, f)
pickle.dump(mmn, f)
pickle.dump(external_dim, f)
pickle.dump(timestamp_train, f)
pickle.dump(timestamp_test, f)
f.close()