-
Notifications
You must be signed in to change notification settings - Fork 2
/
taxi_algorithm_parallel.py
319 lines (232 loc) · 7.49 KB
/
taxi_algorithm_parallel.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
# coding: utf-8
# In[ ]:
import pandas as pd
import networkx as nx
import geopandas as gpd
import shapely.geometry
import matplotlib
import glob
# In[ ]:
nx.__version__
# In[ ]:
import sys
HOUR = int(sys.argv[1])
df = pd.read_pickle("data/taxi_clean/FINAL_HR_{}.pkl".format(HOUR))
print("Hour {} has {} trips".format(HOUR, len(df)))
# In[ ]:
def uniform_str(x):
strd = str(x)
while len(strd) < 7:
strd = '0' + strd
return strd
df["NODEID_O"] = df["NODEID_O"].apply(uniform_str)
df["NODEID_D"] = df["NODEID_D"].apply(uniform_str)
df["pickup_datetime"] = pd.to_datetime(df["tpep_pickup_datetime"])
df["dropoff_datetime"] = pd.to_datetime(df["tpep_dropoff_datetime"])
del df['tpep_dropoff_datetime']
del df['tpep_pickup_datetime']
mn_nodes = gpd.read_file("data/mn_nodes.shp")
init_graph = nx.read_gpickle("data/final_graph_1st_pass_nx_2.1.pkl")
for a, b in init_graph.edges.items():
b['speed'] = 25.
b['weight'] = (b['dist'] / b['speed']) * 3600
# In[ ]:
import numpy as np
from itertools import chain
from collections import OrderedDict
def first_average(g):
travel_time = ((g["dropoff_datetime"] - g["pickup_datetime"]).mean()).total_seconds()
res = g.iloc[0]
res["tt_avg"] = travel_time
res["n_trips"] = len(g)
return res
# In[ ]:
import dask.dataframe as dd
from dask.multiprocessing import get
from dask.diagnostics import ProgressBar
ddata = dd.from_pandas(df, npartitions=30)
averaged = ddata.groupby(["NODEID_O", "NODEID_D"]) .apply(first_average) .reset_index(drop=True)
#averaged = averaged.reset_index()
# first trip filtering - > 2 mins, < 1 hour
first_filtered = averaged[(averaged["NODEID_O"] != averaged["NODEID_D"]) &
(averaged["tt_avg"] > 120) &
(averaged["tt_avg"] < 3600)].reset_index(drop=True)
with ProgressBar():
fa = first_filtered.compute(get=get)
fa["NODEID_O"] = fa["NODEID_O"].apply(uniform_str)
fa["NODEID_D"] = fa["NODEID_D"].apply(uniform_str)
# In[ ]:
import numpy as np
import toolz
S_trip = None
T_s = None
O_s = None
def trip_dist(trip):
try:
p = nx.algorithms.shortest_path(init_graph,
trip["NODEID_O"],
trip["NODEID_D"])
except Exception as e:
return np.nan
edges = ((p[i], p[i+1]) for i in range(len(p) - 1))
street_dist = sum(init_graph.get_edge_data(*e)['dist'] for e in edges)
return street_dist
@toolz.curry
def trip_path(trip, graph=None):
try:
p = nx.algorithms.shortest_path(graph,
trip["NODEID_O"],
trip["NODEID_D"])
edges = ((p[i], p[i+1]) for i in range(len(p) - 1))
except Exception as e:
return np.nan
return ",".join(p)
@toolz.curry
def trip_time(p,graph=None):
p = p.split(",")
edges = ((p[i], p[i+1]) for i in range(len(p) - 1))
t = 0.
for e in edges:
try:
w = graph.get_edge_data(*e)['weight']
except Exception as q:
bad = p
raise q
t += w
return t
# In[ ]:
import matplotlib.pyplot as plt
dfa = dd.from_pandas(fa, npartitions=30)
# second trip filtering
dfa["dist"] = dfa.apply(trip_dist, axis=1,
meta=("dist", "f8"))
dfa = dfa.dropna(subset=["dist"])
dfa["speed"] = dfa["dist"] / (dfa["tt_avg"])
dfa = dfa[((dfa["speed"] * 3600.) > 1) & (dfa["speed"] < (65 / (3600.)))] .reset_index(drop=True)
with ProgressBar():
sa = dfa.compute(get=get)
# In[ ]:
from collections import defaultdict
from datetime import datetime
# iterative steps
again = True
done = False
base_graph = init_graph.copy()
dp = sa
while again:
tt = trip_time(graph=base_graph)
path = trip_path(graph=base_graph)
again = False
S_trip = set() # all touched streets
T_s = defaultdict(set) # basically trips_by_street
O_s = defaultdict(np.float64) # offset_by_street
dsa = dd.from_pandas(dp, npartitions=30)
dsa["path"] = dsa.apply(path, axis=1, meta=("path", "O"))
dsa["et"] = dsa["path"].apply(tt, meta=("tt", "f8"))
dsa["rel_err"] = (dsa["et"] - dsa["tt_avg"]) / dsa["tt_avg"]
dsa["offset"] = ((dsa["et"] - dsa["tt_avg"]) * dsa["n_trips"])
with ProgressBar():
dp = dsa.compute(get=get)
print("time is {}".format(datetime.now()))
for name, p in zip(dp.index, dp["path"].str.split(",")):
edges = zip(p, p[1:])
for e in edges:
T_s[e].add(name)
S_trip.add(e)
print("time is {}".format(datetime.now()))
print("Sets computed")
for street, trips in T_s.items():
trips_df = dp.loc[trips]
O_s[street] = (trips_df["offset"]).sum()
print("time is {}".format(datetime.now()))
k = 1.2
print("rel_err sum is {}".format(np.abs(dp["rel_err"]).sum()))
while True:
g_c = base_graph.copy()
tt2 = trip_time(graph=g_c)
for street in S_trip:
a, b = street # street connects nodes a and b
e = base_graph.edges[street]
if O_s[street] < 0:
g_c[a][b]['weight'] = e["weight"] * k
else:
g_c[a][b]['weight'] = e["weight"] / k
dp["et_new"] = dp["path"].apply(tt2)
dp["new_rel_err"] = (dp["et_new"] - dp["tt_avg"]) / dp["tt_avg"]
print("new_rel_err sum is {}".format(np.abs(dp["new_rel_err"]).sum()))
if np.abs(dp["new_rel_err"]).sum() < np.abs(dp["rel_err"]).sum():
dp["et"] = dp["et_new"]
dp["rel_err"] = dp["new_rel_err"]
again = True
base_graph = g_c
break
else:
print("k updated to {}".format(k))
k = 1 + (k - 1) * .75
if k < 1.0001:
break
# In[ ]:
nx.write_gpickle(base_graph,"data/taxi_graphs/base_graph_hour_{}.pkl".format(HOUR))
# In[ ]:
for e, info in base_graph.edges.items():
print(info)
print("speed is {}".format(info['dist'] / (info['weight'] / 3600.)))
break
# In[ ]:
#speeds = pd.Series([info["dist"] / (info['weight'] / 3600.) for info in final_graph.edges.values()])
# In[ ]:
final_graph = base_graph.copy()
for e, attrs in final_graph.edges.items():
attrs["speed"] = attrs["dist"] / attrs["weight"]
# In[ ]:
S = set(final_graph.edges.keys())
ES = S_trip
NS = S - S_trip
N_S = nodes_by_street = {s: set(final_graph.edges(s)) for s in S}
n_s_i = n_by_street = sorted({s: len(N_S[s].intersection(S_trip)) for s in NS}.items(),
key=lambda x: x[1], reverse=True)
# In[ ]:
for s, n in n_s_i:
if n == 0:
continue
intersecting_speeds = [final_graph.edges[(e1,e2)]['speed'] for e1, e2 in N_S[s].intersection(S_trip)]
final_e = final_graph.edges[s]
v_s_i = sum(intersecting_speeds) / len(intersecting_speeds)
t_s_i = final_e["dist"] / v_s_i
final_e["speed"] = v_s_i
final_e["weight"] = t_s_i
ES.add(s)
NS = NS - set([s])
# In[ ]:
nx.write_gpickle(final_graph, "data/taxi_graphs/final_graph_hour_{}.pkl".format(HOUR))
# In[ ]:
#speeds = [(thing['ix'], thing['speed'] * 3600) for _, thing in final_graph.edges.items()]
#speeds = pd.Series((s[1] for s in speeds), index=(s[0] for s in speeds))
#speeds.name='speed'
#
#
## In[ ]:
#
#
#import geopandas as gpd
#mn_lines = gpd.read_file("data/mn_lines.shp")
#
#
## In[ ]:
#
#
#mn_speed_cols = mn_lines.loc[speeds.index]
#mn_speed_cols["speed"] = speeds
#
#
## In[ ]:
#
#
#mn_speed_cols.plot("speed", figsize=(16, 40))
#
#
## In[ ]:
#
#
#mn_lines._geometry_array
#