This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
data_utils.py
334 lines (262 loc) · 10.8 KB
/
data_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
import random
import pandas as pd
import numpy as np
import logging
import pickle
logger = logging.getLogger()
def batch_loader(iterable, batch_size, shuffle=False):
length = len(iterable)
if shuffle:
random.shuffle(iterable)
for idx in range(0, length, batch_size):
yield iterable[idx:min(length, idx + batch_size)]
def load_agg_data(
data_path='../data/aggregated_5min_scaled.csv',
x_len=10,
y_len=1,
ncells=20,
foresight=0,
dev_ratio=.1,
test_len=7,
seed=None,
):
data = pd.read_csv(data_path, index_col=0)
data.index = pd.to_datetime(data.index)
col_list = list(data.columns)
col_list.remove("CELL_NUM")
ndim_x = len(col_list)
ndim_y = ndim_x
full_x_ = []
full_y_ = []
full_dt_ = []
for cell_id in range(ncells): # config
cell_data = data[data['CELL_NUM']==cell_id]
grouped = cell_data.groupby(pd.Grouper(freq='D'))
cell_x = np.empty((0, x_len, ndim_x), dtype=np.float32)
cell_y = np.empty((0, y_len, ndim_y), dtype=np.float32)
cell_dt = np.empty((0, y_len), dtype=str)
for day, group in grouped:
if not group.shape[0]:
continue
else:
group_index = group.index.astype('str')
source_x = group[col_list].sort_index().values.reshape(-1, ndim_x).astype('float32')
source_y = group[col_list].sort_index().values.reshape(-1, ndim_y).astype('float32')
slided_x = np.array([source_x[i:i + x_len] for i in range(0, len(source_x) - x_len - foresight - y_len + 1)])
y_start_idx = x_len + foresight
slided_y = np.array([source_y[i:i + y_len] for i in range(y_start_idx, len(source_y) - y_len + 1)])
slided_dt = np.array([group_index[i:i + y_len] for i in range(y_start_idx, len(source_y) - y_len + 1)])
cell_x = np.concatenate([cell_x, slided_x],axis=0)
cell_y = np.concatenate([cell_y, slided_y],axis=0)
cell_dt = np.concatenate([cell_dt, slided_dt],axis=0)
full_x_.append(cell_x)
full_y_.append(cell_y)
full_dt_.append(cell_dt)
full_x = np.stack(full_x_, axis=1)
full_y = np.stack(full_y_, axis=1)
full_dt = np.stack(full_dt_, axis=1)
assert len(full_x) == len(full_y)
# squeeze second dim if y_len = 1
if y_len == 1:
full_y = np.squeeze(full_y, axis=2)
full_dt = np.squeeze(full_dt, axis=2)
full_dt_tmp = full_dt[:, 0]
end_dt = pd.to_datetime(full_dt_tmp[-1])
start_dt = end_dt - pd.Timedelta(days=test_len)
if y_len > 1:
start_dt = start_dt.astype('str')[0]
test_ind = -1
for i in range(len(full_dt_tmp)):
if y_len == 1:
d = full_dt_tmp[i]
else:
d = full_dt[i,0]
if d == str(start_dt):
test_ind = i+1
break
assert test_ind != -1
tr_x = full_x[:test_ind]
tr_y = full_y[:test_ind]
if seed :
np.random.seed(seed)
dev_len = int(len(tr_x) * dev_ratio)
dev_ind = np.random.permutation(len(tr_x))[:dev_len]
tr_ind = np.ones(len(tr_x), np.bool)
tr_ind[dev_ind] = 0
train_x, dev_x, test_x = tr_x[tr_ind], tr_x[dev_ind], full_x[test_ind:]
train_y, dev_y, test_y = tr_y[tr_ind], tr_y[dev_ind], full_y[test_ind:]
test_dt = full_dt[test_ind:]
logger.info('X : {}, {}, {}'.format(train_x.shape, dev_x.shape, test_x.shape))
logger.info('Y : {}, {}, {}'.format(train_y.shape, dev_y.shape, test_y.shape))
logger.info('Test set start time: {}'.format(test_dt[0, 0]))
return train_x, dev_x, test_x, train_y, dev_y, test_y, test_dt
def load_agg_selected_data_mem(
data_path='../data/aggregated_5min_scaled.csv',
x_len=10,
y_len=1,
mem_len=7,
foresight=0,
cell_ids=None,
dev_ratio=.1,
test_len=7,
seed=None,
):
data = pd.read_csv(data_path, index_col=0)
data.index = pd.to_datetime(data.index)
col_list = list(data.columns)
col_list.remove("CELL_NUM")
ndim_x = len(col_list)
ndim_y = ndim_x
full_m_lst=[]
full_y_lst = []
full_dt_lst = []
full_x_lst = []
full_cell_lst = []
for cell_id in cell_ids: # CELL_IDs from config
cell_data = data[data['CELL_NUM']==cell_id]
grouped = cell_data.groupby(pd.Grouper(freq='D'))
m_lst = []
y_lst = []
dt_lst = []
for day, group in grouped:
if not group.shape[0]:
continue
else:
group_index = group.index.astype('str')
source_x = group[col_list].sort_index().values.reshape(-1, ndim_x).astype('float32')
source_y = group[col_list].sort_index().values.reshape(-1, ndim_y).astype('float32')
slided_x = np.array([source_x[i:i + x_len] for i in range(0, len(source_x) - x_len - foresight - y_len + 1)])
y_start_idx = x_len + foresight
slided_y = np.array([source_y[i:i + y_len] for i in range(y_start_idx, len(source_y) - y_len+1)])
slided_dt = np.array([group_index[i:i + y_len] for i in range(y_start_idx, len(source_y) - y_len + 1)])
m_lst.append(slided_x)
y_lst.append(slided_y)
dt_lst.append(slided_dt)
# [slided, day, x_len, nf]
m_x = np.stack(m_lst, axis=1)
m_y = np.stack(y_lst, axis=1)
m_dt = np.stack(dt_lst, axis=1)
m = np.concatenate([m_x, m_y], axis=2)
cell = np.ones_like(m_dt)
cell = cell * cell_id
full_cell_lst.append(cell)
full_m_lst.append(m)
full_x_lst.append(m_x)
full_y_lst.append(m_y)
full_dt_lst.append(m_dt)
# [slided, ncells, day, nsteps, nfeatures]
logger.info("after day window sliding")
full_m = np.stack(full_m_lst, axis=1) # [slided, ncells, day, nsteps+1, nfeatures]
full_x = np.stack(full_x_lst, axis=1) # [slided, ncells, day, nsteps, nfeatures]
full_y = np.stack(full_y_lst, axis=1) # [slided, ncells, day, 1, nfeatures]
full_dt = np.stack(full_dt_lst, axis=1) # [slided, ncells, day, 1]
full_cell = np.stack(full_cell_lst, axis=1) # [slided, ncells, day, 1]
for arg in [full_m, full_x, full_y, full_dt, full_cell]:
logger.info(arg.shape)
# memory sliding for each cell
x_start_day = mem_len+1
total_m = []
total_x = []
total_y = []
total_dt = []
total_cell = []
for i in range(x_start_day, full_m.shape[2]):
total_m.append(full_m[:,:,i-mem_len:i,:,:])
total_x.append(full_x[:, :, i, :, :])
total_y.append(full_y[:, :, i, :, :])
total_dt.append(full_dt[:, :, i, :])
total_cell.append(full_cell[:, :, i, :])
del full_m, full_x, full_y, full_cell, full_dt, full_m_lst, full_x_lst, full_y_lst, full_cell_lst, full_dt_lst
# [slided, ncells, nsteps, nfeatures]
total_x = np.concatenate(total_x, axis=0)
total_y = np.concatenate(total_y, axis=0)
total_dt = np.concatenate(total_dt, axis=0)
total_cell = np.concatenate(total_cell, axis=0)
# total_m: [slided, ncells, mteps * (nsteps+1), nf]
total_m = np.concatenate(total_m, axis=0)
total_m = np.reshape(total_m, [total_m.shape[0], total_m.shape[1], -1, total_m.shape[-1]])
total_dt_cell0 = total_dt[:, 0, :]
# squeezing
total_y = np.squeeze(total_y)
if len(cell_ids) == 1:
total_y = np.expand_dims(total_y, axis=1) ## warning : only when using 1 cell !!
total_dt_cell0 = np.squeeze(total_dt_cell0)
total_dt = np.squeeze(total_dt)
total_cell= np.squeeze(total_cell)
logger.info("after memory sliding")
for arg in [total_x, total_y, total_dt, total_cell, total_m]:
logger.info(arg.shape)
# decide test_ind for test tuples
end_dt = pd.to_datetime(total_dt_cell0[-1])
start_dt = end_dt - pd.Timedelta(days=test_len)
if y_len > 1:
start_dt = start_dt.astype('str')[0]
test_ind = -1
for i in range(len(total_dt_cell0)):
if y_len == 1:
d = total_dt_cell0[i]
else:
d = total_dt_cell0[i,0]
if d == str(start_dt):
test_ind = i+1
break
assert test_ind != -1
logger.info("test ind: {}".format(test_ind))
tr_x = total_x[:test_ind]
tr_y = total_y[:test_ind]
tr_m = total_m[:test_ind]
tr_c = total_cell[:test_ind]
def _time_concat(arg):
'''making shape [slided * ncells, nsteps, nfeatures]'''
shapes = arg.shape
right = [shapes[i] for i in range(2, len(shapes))]
out = np.reshape(arg, [-1]+right)
return out
# [slided * ncells, nsteps, nf]
tr_x = _time_concat(tr_x)
tr_y = _time_concat(tr_y)
tr_c = _time_concat(tr_c)
tr_m = _time_concat(tr_m)
te_x = _time_concat(total_x[test_ind:])
te_y = _time_concat(total_y[test_ind:])
te_m = _time_concat(total_m[test_ind:])
te_c = _time_concat(total_cell[test_ind:])
if seed :
np.random.seed(seed)
dev_len = int(len(tr_x) * dev_ratio)
dev_ind = np.random.permutation(len(tr_x))[:dev_len]
tr_ind = np.ones(len(tr_x), np.bool)
tr_ind[dev_ind] = 0
train_x, dev_x = tr_x[tr_ind], tr_x[dev_ind]
logger.info(tr_y.shape)
train_y, dev_y = tr_y[tr_ind], tr_y[dev_ind]
train_m, dev_m = tr_m[tr_ind], tr_m[dev_ind]
train_c, dev_c = tr_c[tr_ind], tr_c[dev_ind]
test_dt = total_dt_cell0[test_ind:]
logger.info('X : {}, {}, {}'.format(train_x.shape, dev_x.shape, te_x.shape))
logger.info('Y : {}, {}, {}'.format(train_y.shape, dev_y.shape, te_y.shape))
logger.info('M : {}, {}, {}'.format(train_m.shape, dev_m.shape, te_m.shape))
logger.info('C : {}, {}, {}'.format(train_c.shape, dev_c.shape, te_c.shape))
logger.info('Test set start time: {}'.format(test_dt[0]))
return train_x, dev_x, te_x, train_y, dev_y, te_y, train_m, dev_m, te_m, test_dt
def load_agg_data_all(data_path='../data/aggregated_data_5min_scaled.csv', ncells=20, test_len=7):
data = pd.read_csv(data_path, index_col=0)
data.index = pd.to_datetime(data.index)
full_x = []
for cell_id in range(ncells): # config
cell_data = data[data['CELL_NUM']==cell_id]
# Find last test_len days to generate cell vectors
end_dt = cell_data.index.date[-1]
from datetime import timedelta
start_dt = end_dt-timedelta(days=6)
cell_x = cell_data[start_dt : end_dt+timedelta(days=1)]
full_x.append(cell_x)
full_x = np.stack(full_x, axis=0) # [ncells, t, d]
full_x = np.expand_dims(full_x, axis=0)
full_x = full_x[:, :, :, :-1]
print("-----------------------------")
print("input shape: {}".format(full_x.shape))
print("-----------------------------")
return full_x
if __name__ == "__main__":
full_x = load_agg_data_all()