-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_training_data.py
178 lines (150 loc) · 6.83 KB
/
generate_training_data.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
import os
import sys
import shutil
import pickle
import argparse
import numpy as np
import pandas as pd
# TODO: remove it when basicts can be installed by pip
sys.path.append(os.path.abspath(__file__ + "/../../../.."))
from basicts.data.transform import standard_transform
from basicts.utils import dump_pkl
def generate_data(args: argparse.Namespace):
"""Preprocess and generate train/valid/test datasets.
Args:
args (argparse): configurations of preprocessing
"""
target_channel = args.target_channel
future_seq_len = args.future_seq_len
history_seq_len = args.history_seq_len
add_time_of_day = args.tod
add_day_of_week = args.dow
add_day_of_month = args.dom
add_day_of_year = args.doy
output_dir = args.output_dir
train_ratio = args.train_ratio
valid_ratio = args.valid_ratio
data_file_path = args.data_file_path
graph_file_path = args.graph_file_path
norm_each_channel = args.norm_each_channel
# read data
df = pd.read_hdf(data_file_path)
data = np.expand_dims(df.values, axis=-1)
data = data[..., target_channel]
print("raw time series shape: {0}".format(data.shape))
# split data
l, n, f = data.shape
num_samples = l - (history_seq_len + future_seq_len) + 1
train_num = round(num_samples * train_ratio)
valid_num = round(num_samples * valid_ratio)
test_num = num_samples - train_num - valid_num
print("number of training samples:{0}".format(train_num))
print("number of validation samples:{0}".format(valid_num))
print("number of test samples:{0}".format(test_num))
index_list = []
for t in range(history_seq_len, num_samples + history_seq_len):
index = (t-history_seq_len, t, t+future_seq_len)
index_list.append(index)
train_index = index_list[:train_num]
valid_index = index_list[train_num: train_num + valid_num]
test_index = index_list[train_num +
valid_num: train_num + valid_num + test_num]
# normalize data
scaler = standard_transform
data_norm = scaler(data, output_dir, train_index, history_seq_len, future_seq_len, norm_each_channel=norm_each_channel)
# add temporal feature
feature_list = [data_norm]
if add_time_of_day:
# numerical time_of_day
tod = (
df.index.values - df.index.values.astype("datetime64[D]")) / np.timedelta64(1, "D")
tod_tiled = np.tile(tod, [1, n, 1]).transpose((2, 1, 0))
feature_list.append(tod_tiled)
if add_day_of_week:
# numerical day_of_week
dow = df.index.dayofweek / 7
dow_tiled = np.tile(dow, [1, n, 1]).transpose((2, 1, 0))
feature_list.append(dow_tiled)
if add_day_of_month:
# numerical day_of_month
dom = (df.index.day - 1 ) / 31 # df.index.day starts from 1. We need to minus 1 to make it start from 0.
dom_tiled = np.tile(dom, [1, n, 1]).transpose((2, 1, 0))
feature_list.append(dom_tiled)
if add_day_of_year:
# numerical day_of_year
doy = (df.index.dayofyear - 1) / 366 # df.index.month starts from 1. We need to minus 1 to make it start from 0.
doy_tiled = np.tile(doy, [1, n, 1]).transpose((2, 1, 0))
feature_list.append(doy_tiled)
processed_data = np.concatenate(feature_list, axis=-1)
# save data
index = {}
index["train"] = train_index
index["valid"] = valid_index
index["test"] = test_index
with open(output_dir + "/index_in{0}_out{1}.pkl".format(history_seq_len, future_seq_len), "wb") as f:
pickle.dump(index, f)
data = {}
data["processed_data"] = processed_data
with open(output_dir + "/data_in{0}_out{1}.pkl".format(history_seq_len, future_seq_len), "wb") as f:
pickle.dump(data, f)
# copy adj
# shutil.copyfile(graph_file_path, output_dir + "/adj_mx.pkl")
adj = pd.read_csv(graph_file_path, sep=" ", header=None, names=["from","to","a"]).pivot(index="from", columns="to", values="a")
dump_pkl(adj.values, output_dir + "/adj_mx.pkl")
if __name__ == "__main__":
# sliding window size for generating history sequence and target sequence
HISTORY_SEQ_LEN = 12
FUTURE_SEQ_LEN = 12
TRAIN_RATIO = 0.7
VALID_RATIO = 0.1
TARGET_CHANNEL = [0] # target channel(s)
DATASET_NAME = "BJ500"
TOD = True # if add time_of_day feature
DOW = True # if add day_of_week feature
DOM = True # if add day_of_month feature
DOY = True # if add day_of_year feature
NORM_EACH_CHANNEL = False
OUTPUT_DIR = "datasets/" + DATASET_NAME
DATA_FILE_PATH = "datasets/raw_data/{0}/{0}.h5".format(DATASET_NAME)
GRAPH_FILE_PATH = "datasets/raw_data/{0}/Adj({0}).txt".format(DATASET_NAME)
parser = argparse.ArgumentParser()
parser.add_argument("--output_dir", type=str,
default=OUTPUT_DIR, help="Output directory.")
parser.add_argument("--data_file_path", type=str,
default=DATA_FILE_PATH, help="Raw traffic readings.")
parser.add_argument("--graph_file_path", type=str,
default=GRAPH_FILE_PATH, help="Raw traffic readings.")
parser.add_argument("--history_seq_len", type=int,
default=HISTORY_SEQ_LEN, help="Sequence Length.")
parser.add_argument("--future_seq_len", type=int,
default=FUTURE_SEQ_LEN, help="Sequence Length.")
parser.add_argument("--tod", type=bool, default=TOD,
help="Add feature time_of_day.")
parser.add_argument("--dow", type=bool, default=DOW,
help="Add feature day_of_week.")
parser.add_argument("--dom", type=bool, default=DOM,
help="Add feature day_of_week.")
parser.add_argument("--doy", type=bool, default=DOY,
help="Add feature day_of_week.")
parser.add_argument("--target_channel", type=list,
default=TARGET_CHANNEL, help="Selected channels.")
parser.add_argument("--train_ratio", type=float,
default=TRAIN_RATIO, help="Train ratio")
parser.add_argument("--valid_ratio", type=float,
default=VALID_RATIO, help="Validate ratio.")
parser.add_argument("--norm_each_channel", type=float,
default=NORM_EACH_CHANNEL, help="Validate ratio.")
args_metr = parser.parse_args()
# print args
print("-"*(20+45+5))
for key, value in sorted(vars(args_metr).items()):
print("|{0:>20} = {1:<45}|".format(key, str(value)))
print("-"*(20+45+5))
if os.path.exists(args_metr.output_dir):
reply = str(input(
f"{args_metr.output_dir} exists. Do you want to overwrite it? (y/n)")).lower().strip()
if reply[0] != "y":
sys.exit(0)
else:
os.makedirs(args_metr.output_dir)
generate_data(args_metr)