-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_stops_start.py
251 lines (205 loc) · 8.08 KB
/
get_stops_start.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
from concurrent.futures import ThreadPoolExecutor
import numpy as np
import matplotlib.pyplot as plt
import sqlite3
from scipy import interpolate
from tqdm import tqdm
from datetime import datetime
from random import choice
from glob import glob
from utils import (
get_route_details,
get_stops_details,
haversine_dist,
cartesian,
)
import os
from random import choice
from bisect import bisect_left
stops_data = get_stops_details()
routes_data = get_route_details()
def get_angle(coor1, coor2):
return np.dot(coor1, coor2) / (
np.linalg.norm(coor1) * np.linalg.norm(coor2)
)
def longest_subsequence(seq, return_index=False):
bisect = bisect_left
rank = list(seq)
if not rank:
return []
lastoflength = [0] # end position of subsequence with given length
predecessor = [
None
] # penultimate element of l.i.s. ending at given position
for i in range(1, len(seq)):
# seq[i] can extend a subsequence that ends with a lesser (or equal) element
j = bisect([rank[k] for k in lastoflength], rank[i])
# update existing subsequence of length j or extend the longest
try:
lastoflength[j] = i
except:
lastoflength.append(i)
# remember element before seq[i] in the subsequence
predecessor.append(lastoflength[j - 1] if j > 0 else None)
def trace(i):
if i is not None:
yield from trace(predecessor[i])
yield i
indices = trace(lastoflength[-1])
return list(indices) if return_index else [seq[i] for i in indices]
def task(tree_file):
if os.path.exists(
"assets/processed/stops_aligned/{}".format(tree_file.split("/")[-1])
):
print("passed", tree_file)
return
try:
tree = np.load(tree_file, allow_pickle=True)["arr_0"].item()
except:
print(tree_file)
stop_tree = {}
for route_id in tqdm(tree):
stops = routes_data[route_id]
stop_tree[route_id] = {}
directions = []
for e in range(1, len(stops)):
directions.append(
cartesian(*stops_data[stops[e]][:2])
- cartesian(*stops_data[stops[e - 1]][:2])
)
for each_trip in tree[route_id]:
stop_tree[route_id][each_trip] = [None] * len(stops)
for start_stop in range(0, len(stops)):
if tree[route_id][each_trip][start_stop] == None:
continue
trip_stop_data = np.array(
tree[route_id][each_trip][start_stop]
)
if (
len(trip_stop_data) > 1
and (np.diff([e[0] for e in trip_stop_data]) < 0).any()
):
if (
np.count_nonzero(
np.diff([e[0] for e in trip_stop_data]) < 0
)
> 1
):
continue
else:
trip_stop_data = sorted(
trip_stop_data, key=lambda e: e[0]
)
_, un_repeat_stops = np.unique(
[e[0] for e in trip_stop_data], return_index=True
)
trip_stop_data = np.array(trip_stop_data)[un_repeat_stops]
assert (np.diff([e[0] for e in trip_stop_data]) > 0).all()
distances = np.array(
[
haversine_dist(
*e[2:], *stops_data[stops[start_stop]][:2]
)
for e in trip_stop_data
]
)
time = np.array([e[0] for e in trip_stop_data])
close_time = np.argmin(distances)
close_time_val = time[close_time]
time -= time[close_time]
max_range = np.zeros(len(time), dtype=bool)
max_range[-15 + close_time : close_time] = True
max_range[close_time : close_time + 15] = True
useful_indices = np.logical_and(
np.logical_and(time < 5 * 60, time > -5 * 60), max_range
)
time = time[useful_indices]
distances = distances[useful_indices]
trip_stop_data = trip_stop_data[useful_indices]
if start_stop == 0:
prev_dir = -1 * directions[0]
next_dir = directions[0]
elif start_stop == len(stops) - 1:
prev_dir = -1 * directions[len(stops) - 2]
next_dir = directions[len(stops) - 2]
else:
prev_dir = -1 * directions[start_stop - 1]
next_dir = directions[start_stop]
prev_dir = np.array(
[
get_angle(
prev_dir,
cartesian(*e[2:])
- cartesian(*stops_data[stops[start_stop]][:2]),
)
for e in trip_stop_data
]
)
next_dir = np.array(
[
get_angle(
next_dir,
cartesian(*e[2:])
- cartesian(*stops_data[stops[start_stop]][:2]),
)
for e in trip_stop_data
]
)
backward = prev_dir > next_dir
displacement = distances * (-1 * (backward - 0.5) * 2)
useful_indices = longest_subsequence(
displacement, return_index=True
)
displacement = displacement[useful_indices]
time = time[useful_indices]
trip_stop_data = trip_stop_data[useful_indices]
if (
len(displacement) > 1
and -32 > displacement[0]
and -32 < displacement[-1]
):
stop_tree[route_id][each_trip][start_stop] = int(
close_time_val
+ interpolate.interp1d(
displacement, time, fill_value="extrapolate"
)(-32)
)
elif len(displacement) > 1:
dist_diff = np.diff(displacement)
drequired = (
displacement[0] + 32
if displacement[0] > -32
else displacement[-1] + 32
)
velocity_ind = np.argmin(np.abs(dist_diff - drequired))
velocity = dist_diff[velocity_ind] / (
time[velocity_ind + 1] - time[velocity_ind]
)
trequired = (
time[0] - drequired / velocity
if displacement[0] > -32
else time[-1] + drequired / velocity
)
stop_tree[route_id][each_trip][start_stop] = int(
close_time_val + trequired
)
else:
assert len(displacement) == 1
speed = trip_stop_data[0][1] * 3.6
drequired = displacement[0] + 32
if speed == 0:
speed = 2.7
trequired = (
time[0] - drequired / speed
if displacement[0] > -32
else time[-1] + drequired / speed
)
stop_tree[route_id][each_trip][start_stop] = int(
close_time_val + trequired
)
np.savez_compressed(
"assets/processed/stops_aligned/{}".format(tree_file.split("/")[-1]),
stop_tree,
)
executor = ThreadPoolExecutor(max_workers=8)
list(tqdm(map(task, glob("./assets/processed/stops_super/*"))))