-
Notifications
You must be signed in to change notification settings - Fork 21
/
NYC_RISK.py
224 lines (185 loc) · 6.37 KB
/
NYC_RISK.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
# link: https://github.com/Echohhhhhh/GSNet
import os
import numpy as np
import pandas as pd
import datetime
import json
import pickle
import sys
dataurl, outputdir, prefix = 'input/NYC_RISK', 'output/NYC_RISK', 'output/NYC_RISK/NYC_RISK'
row_count, column_count = 20, 20
graph_node_count = 243
geo_columns = ['geo_id', 'type', 'coordinates', 'row_id', 'column_id', 'risk_mask']
rel_columns = ['rel_id', 'type', 'origin_id', 'destination_id', 'road_adj', 'risk_adj', 'poi_adj']
dyna_columns = ['dyna_id', 'type', 'time', 'row_id', 'column_id', 'risk', 'holiday', *[f'poi_type_{i}' for i in range(7)],
'temperature', *[f'weather_{n}' for n in ['clear', 'cloudy', 'rain', 'snow', 'mist']],
'inflow', 'outflow']
geo_type = 'Polygon'
rel_type = 'geo'
dyna_type = 'state'
def write_geo() -> None:
# write graph nodes instead of grids
geo_file = open(prefix + '.geo', 'w')
with open(dataurl + '/grid_node_map.pkl', 'rb') as f:
grid_node_map = pickle.load(f)
with open(dataurl + '/risk_mask.pkl', 'rb') as f:
rm = pickle.load(f)
effective_node = []
for i in range(grid_node_map.shape[0]):
grid = grid_node_map[i]
if np.sum(grid) != 0:
effective_node.append(i)
geo_file.write(','.join(geo_columns))
geo_file.write('\n')
i = 0
for nid in range(graph_node_count):
row_id = effective_node[i] // 20
column_id = effective_node[i] % 20
row = [nid, geo_type, [], row_id, column_id, rm[column_id, row_id]]
geo_file.write(','.join(map(str, row)))
geo_file.write('\n')
i += 1
geo_file.close()
def write_rel() -> None:
rel_file = open(prefix + '.rel', 'w')
rel_file.write(','.join(rel_columns))
rel_file.write('\n')
adjs = {}
for k in ['road_adj', 'risk_adj', 'poi_adj']:
with open(dataurl + '/' + k + '.pkl', 'rb') as f:
adjs[k] = pickle.load(f)
rel_id = 0
# orig_id points to the first dimension of the grid
for i in range(graph_node_count):
for j in range(graph_node_count):
row = [
rel_id,
rel_type,
i,
j,
adjs['road_adj'][i][j],
adjs['risk_adj'][i][j],
adjs['poi_adj'][i][j]
]
rel_file.write(','.join(map(str, row)))
rel_file.write('\n')
rel_id += 1
del adjs
rel_file.close()
def write_dyna() -> None:
dyna_file = open(prefix + '.grid', 'w')
with open(dataurl + '/all_data.pkl', 'rb') as f:
ad = pickle.load(f)
dyna_file.write(','.join(dyna_columns))
dyna_file.write('\n')
dyna_id = 0
time_slots, num_features, num_cols, num_rows = ad.shape
for r in range(num_rows):
for c in range(num_cols):
for ts in range(time_slots):
dt = datetime.datetime(2013, 1, 1, 0, 0) + datetime.timedelta(days=ts//24, hours=ts%24)
dt: str = dt.isoformat() + 'Z'
curr_row_raw = ad[ts, :, c, r]
row = [
dyna_id,
dyna_type,
dt,
r,
c,
curr_row_raw[0],
curr_row_raw[32],
*[curr_row_raw[33+i] for i in range(7)],
curr_row_raw[40],
*[curr_row_raw[41+i] for i in range(5)],
curr_row_raw[46],
curr_row_raw[47]
]
for i in range(len(row)):
if isinstance(row[i], str):
continue
elif isinstance(row[i], int):
row[i] = repr(row[i])
elif isinstance(row[i], float):
row[i] = repr(row[i])
else:
raise TypeError()
dyna_file.write(','.join(row))
dyna_file.write('\n')
dyna_id += 1
if dyna_id % 16384 == 0:
print(f'{dyna_id}/{time_slots * num_cols * num_rows}', file=sys.stderr)
del ad
dyna_file.close()
def write_config() -> None:
config = {
'geo': {
'including_types': geo_type,
geo_type: {
'row_id': 'num',
'column_id': 'num',
'risk_mask': 'num'
}
},
'rel': {
'including_types': rel_type,
rel_type: {
'road_adj': 'num',
'risk_adj': 'num',
'poi_adj': 'num'
}
},
'grid': {
'including_types': dyna_type,
dyna_type: {
'row_id': 20,
'column_id': 20,
'risk': 'num',
'holiday': 'num',
'poi_type_0': 'num',
'poi_type_1': 'num',
'poi_type_2': 'num',
'poi_type_3': 'num',
'poi_type_4': 'num',
'poi_type_5': 'num',
'poi_type_6': 'num',
'temperature': 'num',
'weather_clear': 'num',
'weather_cloudy': 'num',
'weather_rain': 'num',
'weather_snow': 'num',
'weather_mist': 'num',
'inflow': 'num',
'outflow': 'num'
}
},
'info': {
'data_col': dyna_columns[5:],
'weight_col': rel_columns[4:],
'data_files': ['NYC_RISK'],
'graph_input_col': [
'risk',
'inflow',
'outflow'
],
'target_time_col': [
'holiday'
],
'geo_file': 'NYC_RISK',
'rel_file': 'NYC_RISK',
'grid_len_row': row_count,
'grid_len_column': column_count,
'output_dim': 1,
'time_intervals': 3600,
'risk_thresholds': [0, 0.04, 0.08],
'risk_weights': [0.05, 0.2, 0.25, 0.5]
}
}
with open(outputdir + '/config.json', 'w', encoding='UTF-8') as f:
json.dump(config, f, indent=4, ensure_ascii=False)
if __name__ == '__main__':
if not os.path.exists(outputdir):
os.mkdir(outputdir)
write_geo()
write_rel()
write_dyna()
write_config()