-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathq_traffic.py
200 lines (178 loc) · 7.21 KB
/
q_traffic.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
# link: https://github.com/JingqingZ/BaiduTraffic
import pandas as pd
from tqdm import tqdm
import json
import util
outputdir = 'output/Q_TRAFFIC'
util.ensure_dir(outputdir)
dataurl = 'input/Q-TRAFFIC/'
dataname = outputdir + '/Q_TRAFFIC'
def id_to_time(id):
year = 2017
month = 4
day = int(id / (24 * 4))
ori_day = day
if day >= 30:
month = 5
day -= 30
hour = int((id - 24 * 4 * ori_day) / 4)
min = int(id - 24 * 4 * ori_day - 4 * hour) * 15
sec = 0
day += 1
return str(year).zfill(4) + "-" + str(month).zfill(2) + "-" + str(day).zfill(2) + \
"T" + str(hour).zfill(2) + ":" + str(min).zfill(2) + ":" + str(sec).zfill(2) + "Z"
dataset = pd.read_csv(dataurl + 'road_network_sub-dataset.v2/road_network_sub-dataset.v2', sep='\t', header=None)
ori_columns_num = len(dataset.columns)
for i in range(8, ori_columns_num):
dataset = dataset.drop([i], axis=1)
firstline = [1562548955, 30, 3, 1520445066, 1549742690, 0.038, 6, 1]
for i in range(8):
dataset.at[0, i] = firstline[i]
linkset = pd.read_csv(dataurl + 'road_network_sub-dataset.v2/link_gps.v2', sep='\t', header=None)
idlist = linkset[0].values.tolist()
idset = set()
geo = []
node_inlink_dict = {}
node_outlink_dict = {}
datanum = dataset.shape[0]
for i in range(datanum):
id = dataset[0][i]
lat = linkset[2][i]
lon = linkset[1][i]
width = dataset[1][i]
direction = str(dataset[2][i])
snodeid = dataset[3][i]
enodeid = dataset[4][i]
length = dataset[5][i]
speedclass = str(dataset[6][i])
lanenum = dataset[7][i]
if id not in idset:
idset.add(id)
if direction == "0" or direction == "1":
if snodeid not in node_inlink_dict.keys():
node_inlink_dict[snodeid] = []
if enodeid not in node_inlink_dict.keys():
node_inlink_dict[enodeid] = []
node_inlink_dict[snodeid].append(id)
node_inlink_dict[enodeid].append(id)
if snodeid not in node_outlink_dict.keys():
node_outlink_dict[snodeid] = []
if enodeid not in node_outlink_dict.keys():
node_outlink_dict[enodeid] = []
node_outlink_dict[snodeid].append(id)
node_outlink_dict[enodeid].append(id)
elif direction == "2":
if enodeid not in node_inlink_dict.keys():
node_inlink_dict[enodeid] = []
node_inlink_dict[enodeid].append(id)
if snodeid not in node_outlink_dict.keys():
node_outlink_dict[snodeid] = []
node_outlink_dict[snodeid].append(id)
elif direction == "3":
if snodeid not in node_inlink_dict.keys():
node_inlink_dict[snodeid] = []
node_inlink_dict[snodeid].append(id)
if enodeid not in node_outlink_dict.keys():
node_outlink_dict[enodeid] = []
node_outlink_dict[enodeid].append(id)
geo.append([id, 'LineString', '[['+str(lon)+', '+str(lat)+']]',
width, direction, snodeid, enodeid,
length, speedclass, lanenum])
geo = pd.DataFrame(geo, columns=['geo_id', 'type', 'coordinates',
'width', 'direction', 'snodeid', 'enodeid',
'length', 'speedclass', 'lanenum'])
geo.to_csv(dataname+'.geo', index=False)
rel = []
reldict = dict()
for i in range(datanum):
sid = dataset[0][i]
direction = str(dataset[2][i])
snodeid = dataset[3][i]
enodeid = dataset[4][i]
relids = set()
if direction == "0" or direction == "1":
if enodeid in node_inlink_dict and enodeid in node_outlink_dict:
relids = relids.union(set(node_outlink_dict[enodeid]))
for eid in relids:
if eid == sid:
continue
link_weight = 1
if (sid, eid) not in reldict:
reldict[(sid, eid)] = len(reldict)
rel.append([len(reldict) - 1, 'geo', sid, eid, link_weight])
if snodeid in node_inlink_dict and snodeid in node_outlink_dict:
relids = relids.union(set(node_outlink_dict[snodeid]))
for eid in relids:
if eid == sid:
continue
link_weight = 1
if (sid, eid) not in reldict:
reldict[(sid, eid)] = len(reldict)
rel.append([len(reldict) - 1, 'geo', sid, eid, link_weight])
elif direction == "2":
if enodeid in node_inlink_dict and enodeid in node_outlink_dict:
relids = relids.union(set(node_outlink_dict[enodeid]))
for eid in relids:
if eid == sid:
continue
link_weight = 1
if (sid, eid) not in reldict:
reldict[(sid, eid)] = len(reldict)
rel.append([len(reldict) - 1, 'geo', sid, eid, link_weight])
elif direction == "3":
if snodeid in node_inlink_dict and snodeid in node_outlink_dict:
relids = relids.union(set(node_outlink_dict[snodeid]))
for eid in relids:
if eid == sid:
continue
link_weight = 1
if (sid, eid) not in reldict:
reldict[(sid, eid)] = len(reldict)
rel.append([len(reldict) - 1, 'geo', sid, eid, link_weight])
rel = pd.DataFrame(rel, columns=['rel_id', 'type', 'origin_id', 'destination_id', 'link_weight'])
rel.to_csv(dataname+'.rel', index=False)
dataset = pd.read_csv(dataurl + 'traffic_speed_sub-dataset.v2/traffic_speed_sub-dataset.v2', sep=', ', header=None)
dyna_id = 0
dyna_file = open(dataname+'.dyna', 'w')
dyna_file.write('dyna_id' + ',' + 'type' + ',' + 'time' + ',' + 'entity_id' + ',' + 'traffic_speed' + '\n')
datanum = dataset.shape[0]
for i in tqdm(range(datanum)):
entity_id = dataset[0][i]
time_id = dataset[1][i]
traffic_speed = dataset[2][i]
time = id_to_time(time_id)
dyna_file.write(str(dyna_id) + ',' + 'state' + ',' + str(time)
+ ',' + str(entity_id) + ',' + str(traffic_speed) + '\n')
dyna_id += 1
dyna_file.close()
config = dict()
config['geo'] = dict()
config['geo']['including_types'] = ['LineString']
config['geo']['LineString'] = {
'width': 'num',
'direction': 'enum',
'snodeid': 'num',
'enodeid': 'num',
'length': 'num',
'speedclass': 'enum',
'lanenum': 'num'
}
config['rel'] = dict()
config['rel']['including_types'] = ['geo']
config['rel']['geo'] = {'link_weight': 'num'}
config['dyna'] = dict()
config['dyna']['including_types'] = ['state']
config['dyna']['state'] = {'entity_id': 'geo_id', 'traffic_speed': 'num'}
config['info'] = dict()
config['info']['data_col'] = 'traffic_speed'
config['info']['weight_col'] = 'link_weight'
config['info']['data_files'] = ['Q_TRAFFIC']
config['info']['geo_file'] = 'Q_TRAFFIC'
config['info']['rel_file'] = 'Q_TRAFFIC'
config['info']['output_dim'] = 1
config['info']['time_intervals'] = 900
config['info']['init_weight_inf_or_zero'] = 'zero'
config['info']['set_weight_link_or_dist'] = 'link'
config['info']['calculate_weight_adj'] = False
config['info']['weight_adj_epsilon'] = 0.1
json.dump(config, open(outputdir+'/config.json', 'w', encoding='utf-8'), ensure_ascii=False)