-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_analysis.py
276 lines (244 loc) · 12.5 KB
/
sample_analysis.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
#!/usr/bin/python
#coding:utf-8
from __future__ import division
import automatic_test
import numpy as np
import extract_features
from extract_features import *
from extract_features import _feature_types
# import matplotlib.pyplot as plt
import seaborn as sns
from shapely.geometry import LineString, Point, MultiPoint, GeometryCollection
import pandas
from constants import INT_DIST
import plot_helper
import sys
import pickle
# matplotlib.rcParams.update({'font.size': 28})
sns.set_context("paper", font_scale=1.8)
sns.plt.rc("figure", figsize=[12,6])
def get_average_speed_and_dist_of_sample(sample):
fn = sample['pickled_filename']
with open(fn, 'r') as f:
int_sit = pickle.load(f)
track = extract_features.transform_track_to_cartesian(int_sit['track'])
track_line = sample['geometry']['track_line']
entry_line = sample['geometry']['entry_line']
exit_line = sample['geometry']['exit_line']
dist_p1 = extended_interpolate(entry_line, entry_line.length-INT_DIST)
normal1 = extend_line(get_normal_to_line(entry_line, entry_line.length-INT_DIST), 1000.0, direction="both")
track_p1 = find_closest_intersection(normal1, dist_p1, track_line)
track_i1 = find_nearest_coord_index(track_line, track_p1)
dist_p2 = extended_interpolate(exit_line, INT_DIST)
normal2 = extend_line(get_normal_to_line(exit_line, INT_DIST), 1000.0, direction="both")
track_p2 = find_closest_intersection(normal2, dist_p2, track_line)
track_i2 = find_nearest_coord_index(track_line, track_p2)
time_delta = (track[track_i2][2] - track[track_i1][2]).total_seconds()
min_i, max_i = min(track_i1, track_i2), max(track_i1, track_i2)
dist = np.sum(np.linalg.norm(np.diff(np.array([(x, y) for (x, y, _) in track[min_i: max_i+1]]), axis=0), axis=1))
return abs(dist/time_delta*3.6), dist
def get_array_from_feature(samples, feature):
feature_i = extract_features._feature_types.index(feature)
return np.array([s['X'][feature_i] for s in samples])
def plot_label_heatmap(samples, bars_y = 30):
angles = np.linspace(0., np.pi, len(samples[0]['y']))
min_x = np.amin(angles)
max_x = np.amax(angles)
radii = np.zeros((len(samples), len(angles)))
for i, s in enumerate(samples):
radii[i] = s['y']
min_y = np.amin(radii)
max_y = np.amax(radii)
heatmap_array = np.zeros((bars_y, len(angles)))
for i in xrange(np.shape(radii)[0]):
for j in xrange(np.shape(radii)[1]):
dest_j = j
dest_i = round((radii[i,j] - min_y) / (max_y - min_y) * bars_y)-1
heatmap_array[dest_i, dest_j] += 1
indices = np.linspace(min_y, max_y, bars_y)
indices = ["%.1f" % i for i in indices]
columns = np.linspace(0., 180., len(angles))
columns = ["%.1f" % i for i in columns]
heatmap_array = np.flipud(heatmap_array)
heatmap_frame = pandas.DataFrame(data=heatmap_array, index=reversed(indices), columns=columns)
f = sns.heatmap(heatmap_frame)
plt.subplots_adjust(top=0.9)
plt.title("Labels Heatmap")
sns.axlabel("Angle", "Radius")
sns.plt.show(f)
def find_closest_point_along_vec(line, p, vec):
SEARCH_LENGTH = 1000.0
pos_ruler = LineString([p.coords[0], list(np.array(p.coords[0]) + vec*SEARCH_LENGTH)])
neg_ruler = LineString([p.coords[0], list(np.array(p.coords[0]) - vec*SEARCH_LENGTH)])
pos_p = extract_features.find_closest_intersection(pos_ruler, p, line)
neg_p = extract_features.find_closest_intersection(neg_ruler, p, line)
if pos_p != None:
dist_n = p.distance(pos_p)
else:
dist_n = None
if neg_p != None:
dist_nn = p.distance(neg_p)
else:
dist_nn = None
if dist_n != None and dist_nn != None:
if dist_n <= dist_nn:
return pos_p
else:
return neg_p
if dist_n == dist_nn == None:
raise extract_features.NoIntersectionError("No intersection of normals with track found")
else:
if dist_n != None: return pos_p
else: return neg_p
def split_path_at_line_dist(path, way_line, vec, dist):
"""Split a path at the projected point along vec at dist of way_line"""
way_p = way_line.interpolate(dist)
path_p = find_closest_point_along_vec(path, way_p, vec)
path_split_dist = path.project(path_p)
return extract_features.split_line(path, path_split_dist)
def plot_sample_intersection_curvature(samples, title="Sample curvature over intersection coordinates", ax=None, color=None):
"""Plot each sample's curvature relative to the intersection distances coordinate system"""
print "Curvature calculation..."
sample_steps = 100
curvatures = np.zeros((len(samples), sample_steps))
line_dists = np.array(curvatures)
for i, s in enumerate(samples):
track_line = s['geometry']['track_line']
entry_line = s['geometry']['entry_line']
exit_line = s['geometry']['exit_line']
try:
half_angle_vec = extract_features.get_half_angle_vec(exit_line, s['X'][_feature_types.index('intersection_angle')])
# Limit path to a set s_di interval at intersection
# _, track_line = split_path_at_line_dist(track_line, entry_line, half_angle_vec, entry_line.length-36.0)
# track_line, _ = split_path_at_line_dist(track_line, exit_line, half_angle_vec, 36.0)
curvature_sample_coords = [track_line.interpolate(dist).coords[0] for dist in np.linspace(0, track_line.length, sample_steps)]
X, Y = zip(*curvature_sample_coords)
way_line, dists = extract_features.set_up_way_line_and_distances(entry_line, exit_line)
way_line = extract_features.extend_line(way_line, 1000.0, direction="both") # Make sure the way_line is not too short to cover the whole track
LineDistances, _ = extract_features.get_distances_from_cartesian(X, Y, way_line, half_angle_vec)
line_dists[i] = LineDistances - 1000.0 - INT_DIST # Shift to the actual coordinate system
curvatures[i] = extract_features.get_line_curvature(track_line, sample_steps)
except extract_features.NoIntersectionError as e:
#plot_helper.plot_intersection(s, additional_lines=[way_line])
print e
continue
# fig = plt.figure()
# sns.plt.hold(True)
for i in range(curvatures.shape[0]):
handle, = ax.plot(line_dists[i], np.degrees(curvatures[i]), color=color, linestyle='-')
return handle # Only need one
# plt.title(title)
# sns.plt.show()
def show_bar_plot():
pass
if __name__ == "__main__":
dataset_samples = [("KITTI + Karlsruhe", automatic_test.load_samples("data/training_data/samples_analysis/samples_kitti.pickle")),
("Darmstadt", automatic_test.load_samples("data/training_data/samples_analysis/samples_darmstadt.pickle"))]
sns.set_style("whitegrid", {"legend.frameon":True})
# Intersection angles
figure1, axes1 = sns.plt.subplots(1, 2, sharey=True)
for i, (name, samples) in enumerate(dataset_samples):
ax = axes1[i]
# sns.set_style("whitegrid")
intersection_angles = list(get_array_from_feature(samples, 'intersection_angle')/(np.pi)*180.0)
left_turn_count = len([ia for ia in intersection_angles if ia >= 0.])
right_turn_count = len([ia for ia in intersection_angles if ia < 0.])
print "%s: links: %d/%d rechts: %d/%d" % (name, left_turn_count, len(intersection_angles), right_turn_count, len(intersection_angles))
sns.distplot(intersection_angles, bins=20, kde=False, rug=True, ax=ax)
# sns.plt.bar(intersection_angles, bins=20)
# ax.set_xlabel(u"Kreuzungswinkel [°]")
# if i == 0:
# ax.set_ylabel("Anzahl der Kreuzungen [-]")
# sns.plt.show(figure1)
# Oneways
# figure2, axes2 = sns.plt.subplots(1, 2, sharey=True)
for i, (name, samples) in enumerate(dataset_samples):
# ax = axes2[i]
oneways_entry = [extract_features.float_to_boolean(f) for f in get_array_from_feature(samples, 'oneway_entry')]
oneways_exit = [extract_features.float_to_boolean(f) for f in get_array_from_feature(samples, 'oneway_exit')]
oneways = [en and ex for en, ex in zip(oneways_entry, oneways_exit)]
print "%s: Kreuzungen mit Einbahnstraßen: %d/%d" % (name, oneways.count(True), len(samples))
# oneway = {True:u"Einbahnstraße", False:u"Gegenverkehrsstraße"}
# sns.set_style("whitegrid")
# sns.countplot([oneway[o] for o in oneways], order=[u"Einbahnstraße", u"Gegenverkehrsstraße"], ax=ax)
# if i == 0:
# ax.set_ylabel("Kreuzungen mit entsprechenden Armen")
# else:
# ax.set_ylabel("")
# sns.plt.show(figure2)
def label_int(ms):
if ms != 0.0:
return str(int(ms))
else:
return "n/a"
def get_order(l):
return [label_int(ms) for ms in sorted(list(set(l)))]
# Maxspeeds
figure3, axes3 = sns.plt.subplots(1, 2, sharey=True)
for i, (name, samples) in enumerate(dataset_samples):
ax = axes3[i]
maxspeeds = list(get_array_from_feature(samples, 'maxspeed_entry')) + list(get_array_from_feature(samples, 'maxspeed_exit'))
maxspeeds_labels = [label_int(ms) for ms in maxspeeds]
maxspeed_plot = sns.countplot(maxspeeds_labels, order=["n/a","30","50"], ax=ax)
# ax.set_xlabel(u"Erlaubte Höchstgeschwindigkeit [km/h]")
# if i == 0:
# ax.set_ylabel("Anzahl der Kreuzungsarme [-]")
ax.set_ylabel("")
# sns.plt.show(figure3)
# Lanes
figure4, axes4 = sns.plt.subplots(1, 2, sharey=True)
for i, (name, samples) in enumerate(dataset_samples):
ax = axes4[i]
lanes = list(get_array_from_feature(samples, 'lane_count_entry')) + list(get_array_from_feature(samples, 'lane_count_exit'))
lanes_labels = [label_int(ms) for ms in lanes]
lanes_plot = sns.countplot(lanes_labels, order=["n/a", "1", "2", "4"], ax=ax)
# ax.set_xlabel(u"Fahrstreifenanzahl")
# if i == 0:
# ax.set_ylabel("Anzahl der Kreuzungsarme [-]")
# sns.plt.title("KITTI + Karlsruhe")
ax.set_ylabel("")
sns.plt.show(figure4)
# Track curvatures
figure5, axes5 = sns.plt.subplots(1, 2)
for i, (name, samples) in enumerate(dataset_samples):
l_samples = [s for s in samples if s['X'][_feature_types.index('intersection_angle')] >= 0.]
r_samples = [s for s in samples if s['X'][_feature_types.index('intersection_angle')] < 0.]
ax = axes5[i]
ax.hold(True)
l_handle = plot_sample_intersection_curvature(l_samples, ax=ax, color=(0,0,1,0.5))
r_handle = plot_sample_intersection_curvature(r_samples, ax=ax, color=(1,0,0,0.5))
ax.legend([l_handle, r_handle], ['Linksabb.', 'Rechtsabb.'])
ax.set_xlim([-30,30])
ax.set_ylim([-15,15])
ax.set_ylabel("")
sns.plt.show(figure5)
figure6, axes6 = sns.plt.subplots(2, 2, sharey=True, sharex=True)
for i, (name, samples) in enumerate(dataset_samples):
speed, dist = zip(*[get_average_speed_and_dist_of_sample(s) for s in samples])
print "%s: Durchschnittsgeschwindigkeit %d km/h" % (name, np.mean(speed))
print "%s: Weglänge %.2f m" % (name, np.sum(dist))
maxspeed_entry = get_array_from_feature(samples, 'maxspeed_entry')
maxspeed_exit = get_array_from_feature(samples, 'maxspeed_exit')
vehicle_speed_entry = get_array_from_feature(samples, 'vehicle_speed_entry')
vehicle_speed_exit = get_array_from_feature(samples, 'vehicle_speed_exit')
diff_speed_entry = [vs - ms for ms, vs in zip(maxspeed_entry, vehicle_speed_entry) if ms != 0.0]
diff_speed_exit = [vs - ms for ms, vs in zip(maxspeed_exit, vehicle_speed_exit) if ms != 0.0]
ax = axes6[0,i]
sns.distplot(diff_speed_entry, bins=20, kde=False, rug=True, ax=ax)
ax = axes6[1,i]
sns.distplot(diff_speed_exit, color="red", bins=20, kde=False, rug=True, ax=ax)
sns.plt.show(figure6)
figure7, axes7 = sns.plt.subplots(2, 2)
for i, (name, samples) in enumerate(dataset_samples):
sns.plt.hold(True)
_, s_di = set_up_way_line_and_distances(samples[0]['geometry']['entry_line'], samples[0]['geometry']['exit_line'])
for s in samples:
if s['X'][_feature_types.index('intersection_angle')] >= 0.:
linestyle = 'r-'
else:
linestyle = 'b-'
ax = axes7[0,i]
ax.plot(s_di, s['label']['y_distances'], linestyle)
ax = axes7[1,i]
ax.plot(s_di, s['label']['y_radii'], linestyle)
sns.plt.show(figure7)