-
-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathvisualization.py
234 lines (186 loc) · 8.51 KB
/
visualization.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
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
import pandas as pd
import os
import fastf1 as ff1
def show_deviation_minima_on_track(res, track, sector_number):
mad_x_stats = res['mad_x{}'.format(sector_number)]
mad_y_stats = res['mad_y{}'.format(sector_number)]
mean_x_stats = res['mean_x{}'.format(sector_number)]
mean_y_stats = res['mean_y{}'.format(sector_number)]
mean_x_stats = np.array(mean_x_stats)
mean_y_stats = np.array(mean_y_stats)
mad_x_stats = np.array(mad_x_stats)
mad_y_stats = np.array(mad_y_stats)
x_minima = np.r_[True, mad_x_stats[1:] < mad_x_stats[:-1]] & np.r_[mad_x_stats[:-1] < mad_x_stats[1:], True]
y_minima = np.r_[True, mad_y_stats[1:] < mad_y_stats[:-1]] & np.r_[mad_y_stats[:-1] < mad_y_stats[1:], True]
ax_main = plt.subplot(label='Track Map')
plt.plot(track.sorted_x, track.sorted_y)
ax_main.set_aspect('equal')
ax_main.set_xlabel('X')
ax_main.set_ylabel('Y')
ax_main.yaxis.set_tick_params(labelleft=False, labelright=True)
ax_main.yaxis.set_label_position("right")
# x deviation minima
for x_min in mean_x_stats[x_minima]:
ax_main.axvline(x_min, color='r')
# y deviation minima
for y_min in mean_y_stats[y_minima]:
ax_main.axhline(y_min, color='r')
divider = make_axes_locatable(ax_main)
ax_mad_x = divider.append_axes("top", 1.2, pad=0.1, sharex=ax_main)
ax_mad_y = divider.append_axes("left", 1.2, pad=0.1, sharey=ax_main)
ax_mad_x.plot(mean_x_stats, mad_x_stats)
ax_mad_x.grid(which='both')
ax_mad_x.set_ylabel('Y MAD {}'.format(sector_number))
ax_mad_x.xaxis.set_tick_params(labelbottom=False)
ax_mad_y.plot(mad_y_stats, mean_y_stats)
ax_mad_y.grid(which='both')
ax_mad_y.invert_xaxis()
ax_mad_y.set_xlabel('X MAD {}'.format(sector_number))
ax_mad_y.yaxis.set_tick_params(labelleft=False)
def plot_lap_time_integrity(laps_data, suffix=''):
drivers_series = laps_data.DriverResult
drivers = list(drivers_series.drop_duplicates())
deltas = list()
ref = list() # scatter plots need an x and y value therefore ref is created by counting up
n = 0
if 'Date' in laps_data.columns:
for driver in drivers:
i_max = len(laps_data[laps_data.DriverResult == driver])
for i in range(1, i_max):
delta = (laps_data.iloc[i].Date - laps_data.iloc[i].LapTime - laps_data.iloc[i-1].Date).total_seconds()
deltas.append(delta)
ref.append(n)
n += 1
else:
for driver in drivers:
i_max = len(laps_data[laps_data.DriverResult == driver])
for i in range(1, i_max):
delta = (laps_data.iloc[i].Time - laps_data.iloc[i].LapTime - laps_data.iloc[i-1].Time).total_seconds()
deltas.append(delta)
ref.append(n)
n += 1
fig1 = plt.figure()
fig1.suptitle("Lap Time Scatter {}".format(suffix))
plt.scatter(ref, deltas)
fig2 = plt.figure()
fig2.suptitle("Lap Time Histogram {}".format(suffix))
plt.hist(deltas, bins=50)
def plot_lap_position_integrity(laps_data, pos_data, suffix=''):
vals = dict()
for _, lap in laps_data.pick_wo_box().pick_track_status('1').iterlaps(require=['LapStartDate', 'DriverNumber']):
if not lap['IsAccurate']:
continue
if lap['LapStartDate'] not in pos_data[lap['DriverNumber']]['Date']:
tmp_add = pd.DataFrame({}, index=[lap['LapStartDate'], ])
tmp_df = pos_data[lap['DriverNumber']].set_index('Date').append(tmp_add).sort_index()
tmp_df.loc[:, ('X', 'Y')] = tmp_df.loc[:, ('X', 'Y')].interpolate(method='quadratic')
row = tmp_df[tmp_df.index == lap['LapStartDate']].iloc[0]
else:
row = pos_data[lap['DriverNumber']][pos_data[lap['DriverNumber']]['Date'] == lap['LapStartDate']].iloc[0]
if lap['DriverNumber'] not in vals.keys():
vals[lap['DriverNumber']] = {'x': list(), 'y': list(), 't': list()}
vals[lap['DriverNumber']]['x'].append(row['X'])
vals[lap['DriverNumber']]['y'].append(row['Y'])
vals[lap['DriverNumber']]['t'].append(lap['Time'].total_seconds())
x_vals, y_vals = list(), list()
for drv in vals.keys():
x_vals.extend(vals[drv]['x'])
y_vals.extend(vals[drv]['y'])
y_mean_tot = np.mean(y_vals)
fig0y = plt.figure()
fig0y.suptitle("Time Scatter {}".format(suffix))
for drv in vals.keys():
plt.plot(vals[drv]['t'], vals[drv]['y'], label=drv)
plt.legend()
fig0cy = plt.figure()
fig0cy.suptitle("Time Scatter Mean correction {}".format(suffix))
for drv in vals.keys():
plt.plot(vals[drv]['t'], np.array(vals[drv]['y']) + (y_mean_tot - np.mean(vals[drv]['y'])), label=drv)
plt.legend()
fig1 = plt.figure()
fig1.suptitle("Position Scatter {}".format(suffix))
for drv in vals.keys():
plt.scatter(vals[drv]['x'], vals[drv]['y'], label=drv)
plt.axis('equal')
plt.legend()
fig2 = plt.figure()
fig2 .suptitle("Position X Histogram {}".format(suffix))
plt.hist(x_vals, bins=30)
fig3 = plt.figure()
fig3.suptitle("Position Y Histogram {}".format(suffix))
plt.hist(y_vals, bins=30)
def all_sectors_result_plots(result, track, suffix, workdir=None):
r = result
# mean absolute deviation x
plt.figure(figsize=(15, 8))
plt.suptitle('MAD X | {}'.format(suffix))
plt.plot(r['mad_x1'], label="mad_x1")
plt.plot(r['mad_x2'], label="mad_x2")
plt.plot(r['mad_x3'], label="mad_x3")
plt.legend()
plt.grid(which='both')
if workdir:
plt.savefig(os.path.join(workdir, 'mad x {}.png'.format(suffix)), dpi=300)
# mean absolute deviation y
plt.figure(figsize=(15, 8))
plt.suptitle('MAD Y | {}'.format(suffix))
plt.plot(r['mad_y1'], label="mad_y1")
plt.plot(r['mad_y2'], label="mad_y2")
plt.plot(r['mad_y3'], label="mad_y3")
plt.legend()
plt.grid(which='both')
if workdir:
plt.savefig(os.path.join(workdir, 'mad y {}.png'.format(suffix)), dpi=300)
# track + mad plot one for each sector
plt.figure(figsize=(15, 8))
plt.suptitle('Track + MAD 1 | {}'.format(suffix))
show_deviation_minima_on_track(r, track, 1)
if workdir:
plt.savefig(os.path.join(workdir, 'track plus mad 1 {}.png'.format(suffix)), dpi=300)
plt.figure(figsize=(15, 8))
plt.suptitle('Track + MAD 2 | {}'.format(suffix))
show_deviation_minima_on_track(r, track, 2)
if workdir:
plt.savefig(os.path.join(workdir, 'track plus mad 2 {}.png'.format(suffix)), dpi=300)
plt.figure(figsize=(15, 8))
plt.suptitle('Track + MAD 3 | {}'.format(suffix))
show_deviation_minima_on_track(r, track, 3)
if workdir:
plt.savefig(os.path.join(workdir, 'track plus mad 3 {}.png'.format(suffix)), dpi=300)
# delta between test value and mean result
dx = list()
for test, tres in zip(r['tx'], r['mean_x1']):
dx.append(test-tres)
dy = list()
for test, tres in zip(r['ty'], r['mean_y1']):
dy.append(test-tres)
plt.figure(figsize=(15, 8))
plt.suptitle('Difference In - Out Coordinate | {}'.format(suffix))
plt.plot(dx, label='dx')
plt.plot(dy, label='dy')
plt.legend()
plt.grid(which='both')
if workdir:
plt.savefig(os.path.join(workdir, 'coord diff {}.png'.format(suffix)), dpi=300)
plt.show()
def delta_time_validation(ref_lap, comp_lap):
from fastf1 import plotting
plotting.setup_mpl()
plt.figure()
dt, ref_tel, compare_tel = ff1.utils.delta_time(ref_lap, comp_lap)
sec12 = ref_tel['Distance'].iloc[np.argmin(abs(ref_tel['SessionTime'] - ref_lap['Sector1SessionTime']))]
sec23 = ref_tel['Distance'].iloc[np.argmin(abs(ref_tel['SessionTime'] - ref_lap['Sector2SessionTime']))]
plt.vlines(sec12, min(dt), max(dt), colors='yellow')
plt.vlines(sec23, min(dt), max(dt), colors='yellow')
plt.vlines(1.0, min(dt), max(dt), colors='yellow')
plt.plot(ref_tel['Distance'], dt, label='Gap')
dts1 = comp_lap['Sector1Time'] - ref_lap['Sector1Time']
dts2 = (comp_lap['Sector1Time'] + comp_lap['Sector2Time']) - (ref_lap['Sector1Time'] + ref_lap['Sector2Time'])
dtsL = comp_lap['LapTime'] - ref_lap['LapTime']
plt.hlines(dts1.total_seconds(), 0, max(ref_tel['Distance']), label='Sec1', colors='blue')
plt.hlines(dts2.total_seconds(), 0, max(ref_tel['Distance']), label='Sec2', colors='pink')
plt.hlines(dtsL.total_seconds(), 0, max(ref_tel['Distance']), label='Sec3', colors='grey')
plt.legend()