-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathnyc_tod.py
159 lines (137 loc) · 4.77 KB
/
nyc_tod.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
#link: https://github.com/liulingbo918/CSTN
import csv
import json
import os.path
import numpy as np
def gen_config_gridod(row_num, column_num):
gridod = {
"including_types": [
"state"
], "state": {
"row_id": row_num,
"column_id": column_num,
"flow": "num",
}
}
return gridod
def gen_config_geo():
geo = {"including_types": [
"Polygon"
],
"Polygon": {
"row_id": "num",
"column_id": "num"
}
}
return geo
def gen_config_info(file_name, interval):
info = \
{
"data_col": [
"flow"
],
"data_files": [
file_name
],
"geo_file": file_name,
"ext_file": file_name,
"output_dim": 1,
"init_weight_inf_or_zero": "inf",
"set_weight_link_or_dist": "dist",
"calculate_weight_adj": False,
"weight_adj_epsilon": 0.1,
"time_intervals": interval,
}
return info
def gen_config_ext():
ext = \
{
"including_types": [
""
]
}
return ext
def gen_config(output_dir_flow, file_name, row_num, column_num, interval):
config = {}
data = json.loads(json.dumps(config))
data["geo"] = gen_config_geo()
data["ext"] = gen_config_ext()
data["gridod"] = gen_config_gridod(row_num, column_num)
data["info"] = gen_config_info(file_name, interval)
config = json.dumps(data)
with open(output_dir_flow + "/config.json", "w") as f:
json.dump(data, f, ensure_ascii=False, indent=1)
print(config)
if __name__ == '__main__':
dataset = 'NYC_TOD'
output_dir = 'output/'
if not os.path.exists(output_dir):
os.mkdir(output_dir)
# 处理 OD 数据流信息
print("Start processing OD data...")
od_data_npy = 'oddata.npy'
od_data = np.load(od_data_npy, allow_pickle=True, encoding='latin1')[()][0] # (T, R*C, R, C)
row_num = od_data.shape[2]
column_num = od_data.shape[3]
dyna_file = open(output_dir + '/' + dataset + '.gridod', 'w')
writer = csv.writer(dyna_file)
writer.writerow(
['dyna_id', 'type', 'time',
'origin_row_id', 'origin_column_id', 'destination_row_id', 'destination_column_id',
'flow'])
<<<<<<< HEAD
cnt = 0
=======
idx = 0
>>>>>>> 1921e440a269abdeef7af7f73a5271ecda7419c2
for origin_row in range(row_num):
for origin_column in range(column_num):
for destination_row in range(row_num):
for destination_column in range(column_num):
<<<<<<< HEAD
for idx, line in enumerate(od_data):
writer.writerow([cnt,
=======
for line in od_data:
idx += 1
writer.writerow([idx,
>>>>>>> 1921e440a269abdeef7af7f73a5271ecda7419c2
'state',
'',
origin_row,
origin_column,
destination_row,
destination_column,
line[origin_row * column_num + origin_column][destination_row][
destination_column]
])
cnt += 1
dyna_file.close()
print("Finish processing OD data.")
# 生成 GEO 信息
print('Start generating geo file...')
geo_file = open(output_dir + '/' + dataset + '.geo', 'w')
writer = csv.writer(geo_file)
writer.writerow(['geo_id', 'type', 'coordinates', 'row_id', 'column_id'])
for r in range(row_num):
for c in range(column_num):
writer.writerow([r * row_num + c, 'Polygon', '[]', r, c])
geo_file.close()
print('Finish generating geo file.')
# 处理外部数据特征
print("Start processing ext data...")
weather_npy = 'weather.npy'
weather = np.load(weather_npy, allow_pickle=True, encoding='latin1')[()][0] # (T, F)
ext_file = open(output_dir + '/' + dataset + '.ext', 'w')
writer = csv.writer(ext_file)
writer.writerow(
['ext_id', 'time',
0, 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])
for idx, line in enumerate(weather):
writer.writerow([idx, ''] + list(line))
ext_file.close()
print("Finish processing ext data.")
# 生成信息文件
print("Start processing config file...")
gen_config(output_dir, file_name=dataset, row_num=row_num, column_num=column_num, interval=1800)
print("Finish processing config file.")