Spaces:
Runtime error
Runtime error
File size: 14,218 Bytes
2d12f2a 3c52234 666d174 2d12f2a f77afce 2d12f2a f77afce 1415d69 f77afce 666d174 42d4711 666d174 88f8db5 68f7905 88f8db5 1ed6038 2d12f2a 1ed6038 2d12f2a 1ed6038 2d12f2a 4971268 2d12f2a 1ed6038 2d12f2a 0bd345a 2d12f2a 1ed6038 2d12f2a 0bd345a 2d12f2a f6a40e3 2d12f2a 1ed6038 2d12f2a 0bd345a 2d12f2a 1ed6038 2d12f2a 0bd345a 2d12f2a 666d174 42d4711 666d174 2d12f2a badfb4e 2d12f2a 666d174 2d12f2a 3f5181c 2d12f2a 666d174 2d12f2a 666d174 2d12f2a 666d174 87b5933 2d12f2a 8f5773e |
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
import datetime
import os
import streamlit as st
import numpy as np
import math
import fastf1
import pandas as pd
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, HTMLResponse
from pydantic import BaseModel
import available_data
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
import math
import numpy as np
def smooth_derivative(t_in, v_in):
#
# Function to compute a smooth estimation of a derivative.
# [REF: http://holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/]
#
# Configuration
#
# Derivative method: two options: 'smooth' or 'centered'. Smooth is more conservative
# but helps to supress the very noisy signals. 'centered' is more agressive but more noisy
method = "smooth"
t = t_in.copy()
v = v_in.copy()
# (0) Prepare inputs
# (0.1) Time needs to be transformed to seconds
try:
for i in range(0, t.size):
t.iloc[i] = t.iloc[i].total_seconds()
except:
pass
t = np.array(t)
v = np.array(v)
# (0.1) Assert they have the same size
assert t.size == v.size
# (0.2) Initialize output
dvdt = np.zeros(t.size)
# (1) Manually compute points out of the stencil
# (1.1) First point
dvdt[0] = (v[1] - v[0]) / (t[1] - t[0])
# (1.2) Second point
dvdt[1] = (v[2] - v[0]) / (t[2] - t[0])
# (1.3) Third point
dvdt[2] = (v[3] - v[1]) / (t[3] - t[1])
# (1.4) Last points
n = t.size
dvdt[n - 1] = (v[n - 1] - v[n - 2]) / (t[n - 1] - t[n - 2])
dvdt[n - 2] = (v[n - 1] - v[n - 3]) / (t[n - 1] - t[n - 3])
dvdt[n - 3] = (v[n - 2] - v[n - 4]) / (t[n - 2] - t[n - 4])
# (2) Compute the rest of the points
if method == "smooth":
c = [5.0 / 32.0, 4.0 / 32.0, 1.0 / 32.0]
for i in range(3, t.size - 3):
for j in range(1, 4):
dvdt[i] += (
2 * j * c[j - 1] * (v[i + j] - v[i - j]) /
(t[i + j] - t[i - j])
)
elif method == "centered":
for i in range(3, t.size - 2):
for j in range(1, 4):
dvdt[i] = (v[i + 1] - v[i - 1]) / (t[i + 1] - t[i - 1])
return dvdt
def truncated_remainder(dividend, divisor):
divided_number = dividend / divisor
divided_number = (
-int(-divided_number) if divided_number < 0 else int(divided_number)
)
remainder = dividend - divisor * divided_number
return remainder
def transform_to_pipi(input_angle):
pi = math.pi
revolutions = int((input_angle + np.sign(input_angle) * pi) / (2 * pi))
p1 = truncated_remainder(input_angle + np.sign(input_angle) * pi, 2 * pi)
p2 = (
np.sign(
np.sign(input_angle)
+ 2
* (
np.sign(
math.fabs(
(truncated_remainder(input_angle + pi, 2 * pi)) / (2 * pi)
)
)
- 1
)
)
) * pi
output_angle = p1 - p2
return output_angle, revolutions
def remove_acceleration_outliers(acc):
acc_threshold_g = 7.5
if math.fabs(acc[0]) > acc_threshold_g:
acc[0] = 0.0
for i in range(1, acc.size - 1):
if math.fabs(acc[i]) > acc_threshold_g:
acc[i] = acc[i - 1]
if math.fabs(acc[-1]) > acc_threshold_g:
acc[-1] = acc[-2]
return acc
def compute_accelerations(telemetry):
v = np.array(telemetry["Speed"]) / 3.6
lon_acc = smooth_derivative(telemetry["Time"], v) / 9.81
dx = smooth_derivative(telemetry["Distance"], telemetry["X"])
dy = smooth_derivative(telemetry["Distance"], telemetry["Y"])
theta = np.zeros(dx.size)
theta[0] = math.atan2(dy[0], dx[0])
for i in range(0, dx.size):
theta[i] = (
theta[i - 1] +
transform_to_pipi(math.atan2(dy[i], dx[i]) - theta[i - 1])[0]
)
kappa = smooth_derivative(telemetry["Distance"], theta)
lat_acc = v * v * kappa / 9.81
# Remove outliers
lon_acc = remove_acceleration_outliers(lon_acc)
lat_acc = remove_acceleration_outliers(lat_acc)
return np.round(lon_acc,2), np.round(lat_acc,2)
@st.cache_data
@app.get("/wdc", response_model=None)
def driver_standings() -> any:
YEAR = 2023 #datetime.datetime.now().year
df = pd.DataFrame(
pd.read_html(f"https://www.formula1.com/en/results.html/{YEAR}/drivers.html")[0]
)
df = df[["Driver", "PTS", "Car"]]
# reverse the order
df = df.sort_values(by="PTS", ascending=False)
# in Driver column only keep the last 3 characters
df["Driver"] = df["Driver"].str[:-3]
# add colors to the dataframe
car_colors = available_data.team_colors(YEAR)
df["fill"] = df["Car"].map(car_colors)
# remove rows where points is 0
df = df[df["PTS"] != 0]
df.reset_index(inplace=True, drop=True)
df.rename(columns={"PTS": "Points"}, inplace=True)
return {"WDC":df.to_dict("records")}
@st.cache_data
@app.get("/", response_model=None)
async def root():
return HTMLResponse(
content="""<iframe src="https://tracinginsights-f1-analysis.hf.space" frameborder="0" style="width:100%; height:100%;" scrolling="yes" allowfullscreen:"yes"></iframe>""",
status_code=200)
@st.cache_data
@app.get("/years", response_model=None)
def years_available() -> any:
# make a list from 2018 to current year
current_year = datetime.datetime.now().year
years = list(range(2018, current_year+1))
# reverse the list to get the latest year first
years.reverse()
years = [{"label": str(year), "value": year} for year in years]
return {"years": years}
# format for events {"events":[{"label":"Saudi Arabian Grand Prix","value":2},{"label":"Bahrain Grand Prix","value":1},{"label":"Pre-Season Testing","value":"t1"}]}
@st.cache_data
@app.get("/{year}", response_model=None)
def events_available(year: int) -> any:
# get events available for a given year
data = available_data.LatestData(year)
events = data.get_events()
events = [{"label": event, "value": event} for i, event in enumerate(events)]
events.reverse()
return {"events": events}
# format for sessions {"sessions":[{"label":"FP1","value":"FP1"},{"label":"FP2","value":"FP2"},{"label":"FP3","value":"FP3"},{"label":"Qualifying","value":"Q"},{"label":"Race","value":"R"}]}
@st.cache_data
@app.get("/{year}/{event}", response_model=None)
def sessions_available(year: int, event: str | int) -> any:
# get sessions available for a given year and event
data = available_data.LatestData(year)
sessions = data.get_sessions(event)
sessions = [{"label": session, "value": session} for session in sessions]
return {"sessions": sessions}
# format for drivers {"drivers":[{"color":"#fff500","label":"RIC","value":"RIC"},{"color":"#ff8700","label":"NOR","value":"NOR"},{"color":"#c00000","label":"VET","value":"VET"},{"color":"#0082fa","label":"LAT","value":"LAT"},{"color":"#787878","label":"GRO","value":"GRO"},{"color":"#ffffff","label":"GAS","value":"GAS"},{"color":"#f596c8","label":"STR","value":"STR"},{"color":"#787878","label":"MAG","value":"MAG"},{"color":"#0600ef","label":"ALB","value":"ALB"},{"color":"#ffffff","label":"KVY","value":"KVY"},{"color":"#fff500","label":"OCO","value":"OCO"},{"color":"#0600ef","label":"VER","value":"VER"},{"color":"#00d2be","label":"HAM","value":"HAM"},{"color":"#ff8700","label":"SAI","value":"SAI"},{"color":"#00d2be","label":"BOT","value":"BOT"},{"color":"#960000","label":"GIO","value":"GIO"}]}
@st.cache_data
@app.get("/{year}/{event}/{session}", response_model=None)
def session_drivers(year: int, event: str | int, session: str) -> any:
# get drivers available for a given year, event and session
f1session = fastf1.get_session(year, event, session)
api_path = f1session.api_path
drivers_raw = fastf1.api.driver_info(api_path)
drivers = []
for driver in drivers_raw.items():
drivers.append({
"color": available_data.team_colors(year)[driver[1]['TeamName']],
"label": driver[1]['Tla'],
"value": driver[1]['Tla']})
return {"drivers": drivers}
# format for chartData {"chartData":[{"lapnumber":1},{
# "VER":91.564,
# "VER_compound":"SOFT",
# "VER_compound_color":"#FF5733",
# "lapnumber":2
# },{"lapnumber":3},{"VER":90.494,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":4},{"lapnumber":5},{"VER":90.062,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":6},{"lapnumber":7},{"VER":89.815,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":8},{"VER":105.248,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":9},{"lapnumber":10},{"VER":89.79,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":11},{"VER":145.101,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":12},{"lapnumber":13},{"VER":89.662,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":14},{"lapnumber":15},{"VER":89.617,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":16},{"lapnumber":17},{"VER":140.717,"VER_compound":"SOFT","VER_compound_color":"#FF5733","lapnumber":18}]}
@st.cache_data
@app.get("/{year}/{event}/{session}/{driver}", response_model=None)
def laps_data(year: int, event: str | int, session: str, driver: str) -> any:
# get drivers available for a given year, event and session
f1session = fastf1.get_session(year, event, session)
f1session.load(telemetry=False, weather=False, messages=False)
laps = f1session.laps
team_colors = available_data.team_colors(year)
# add team_colors dict to laps on Team column
drivers = laps.Driver.unique()
# for each driver in drivers, get the Team column from laps and get the color from team_colors dict
drivers = [{"color": team_colors[laps[laps.Driver ==
driver].Team.iloc[0]], "label": driver, "value": driver} for driver in drivers]
driver_laps = laps.pick_driver(driver)
driver_laps['LapTime'] = driver_laps['LapTime'].dt.total_seconds()
compound_colors = {
"SOFT": "#FF0000",
"MEDIUM": "#FFFF00",
"HARD": "#FFFFFF",
"INTERMEDIATE": "#00FF00",
"WET": "#088cd0",
}
driver_laps_data = []
for _, row in driver_laps.iterrows():
if row['LapTime'] > 0:
lap = {f"{driver}": row['LapTime'],
f"{driver}_compound": row['Compound'],
f"{driver}_compound_color": compound_colors[row['Compound']],
"lapnumber": row['LapNumber']}
else:
lap = {"lapnumber": row['LapNumber']}
driver_laps_data.append(lap)
return {"chartData": driver_laps_data}
@st.cache_data
@app.get("/{year}/{event}/{session}/{driver}/{lap_number}", response_model=None)
def telemetry_data(year: int, event: str | int, session: str, driver: str, lap_number: int) -> any:
f1session = fastf1.get_session(year, event, session)
f1session.load(telemetry=True, weather=False, messages=False)
laps = f1session.laps
driver_laps = laps.pick_driver(driver)
driver_laps['LapTime'] = driver_laps['LapTime'].dt.total_seconds()
# get the telemetry for lap_number
selected_lap = driver_laps[driver_laps.LapNumber == lap_number]
telemetry = selected_lap.get_telemetry()
lon_acc, lat_acc = compute_accelerations(telemetry)
telemetry["lon_acc"] = lon_acc
telemetry["lat_acc"] = lat_acc
telemetry['Time'] = telemetry['Time'].dt.total_seconds()
laptime = selected_lap.LapTime.values[0]
data_key = f"{driver} - Lap {int(lap_number)} - {year} {session} [{int(laptime//60)}:{laptime%60}]"
telemetry['DRS'] = telemetry['DRS'].apply(lambda x: 1 if x in [10,12,14] else 0)
brake_tel = []
drs_tel = []
gear_tel = []
rpm_tel = []
speed_tel = []
throttle_tel = []
time_tel = []
track_map = []
lon_acc_tel = []
lat_acc_tel = []
for _, row in telemetry.iterrows():
brake = {"x": row['Distance'],
"y": row['Brake'],
}
brake_tel.append(brake)
drs = {"x": row['Distance'],
"y": row['DRS'],
}
drs_tel.append(drs)
gear = {"x": row['Distance'],
"y": row['nGear'],
}
gear_tel.append(gear)
rpm = {"x": row['Distance'],
"y": row['RPM'],
}
rpm_tel.append(rpm)
speed = {"x": row['Distance'],
"y": row['Speed'],
}
speed_tel.append(speed)
throttle = {"x": row['Distance'],
"y": row['Throttle'],
}
throttle_tel.append(throttle)
time = {"x": row['Distance'],
"y": row['Time'],
}
time_tel.append(time)
lon_acc = {"x": row['Distance'],
"y": row['lon_acc'],
}
lon_acc_tel.append(lon_acc)
lat_acc = {"x": row['Distance'],
"y": row['lat_acc'],
}
lat_acc_tel.append(lat_acc)
track = {"x": row['X'],
"y": row['Y'],
}
track_map.append(track)
telemetry_data = {
"telemetryData":{
"brake": brake_tel,
"dataKey": data_key,
"drs": drs_tel,
"gear": gear_tel,
"rpm": rpm_tel,
"speed": speed_tel,
"throttle": throttle_tel,
"time": time_tel,
"lon_acc": lon_acc_tel,
"lat_acc": lat_acc_tel,
"trackMap": track_map,
}
}
return telemetry_data
|