Spaces:
Runtime error
Runtime error
tracinginsights
commited on
Commit
•
2d12f2a
1
Parent(s):
71172b2
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datetime
|
2 |
+
import os
|
3 |
+
|
4 |
+
import fastf1
|
5 |
+
import pandas as pd
|
6 |
+
from fastapi import FastAPI
|
7 |
+
from fastapi.responses import FileResponse, HTMLResponse
|
8 |
+
from pydantic import BaseModel
|
9 |
+
|
10 |
+
import available_data
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
|
15 |
+
@app.get("/", response_model=None)
|
16 |
+
async def root():
|
17 |
+
return HTMLResponse(
|
18 |
+
content="""<iframe src="https://tracinginsights-f1-analysis.hf.space" frameborder="0" style="width:100%; height:100%;" scrolling="yes" allowfullscreen:"yes"></iframe>""",
|
19 |
+
status_code=200)
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
@app.get("/years", response_model=None)
|
24 |
+
def years_available() -> any:
|
25 |
+
# make a list from 2018 to current year
|
26 |
+
current_year = datetime.datetime.now().year
|
27 |
+
years = list(range(2018, current_year+1))
|
28 |
+
# reverse the list to get the latest year first
|
29 |
+
years.reverse()
|
30 |
+
years = [{"label": str(year), "value": year} for year in years]
|
31 |
+
return {"years": years}
|
32 |
+
|
33 |
+
|
34 |
+
# format for events {"events":[{"label":"Saudi Arabian Grand Prix","value":2},{"label":"Bahrain Grand Prix","value":1},{"label":"Pre-Season Testing","value":"t1"}]}
|
35 |
+
|
36 |
+
|
37 |
+
@app.get("/{year}", response_model=None)
|
38 |
+
def events_available(year: int) -> any:
|
39 |
+
# get events available for a given year
|
40 |
+
data = available_data.LatestData(year)
|
41 |
+
events = data.get_events()
|
42 |
+
events = [{"label": event, "value": i} for i, event in enumerate(events)]
|
43 |
+
events.reverse()
|
44 |
+
|
45 |
+
return {"events": events}
|
46 |
+
|
47 |
+
# format for sessions {"sessions":[{"label":"FP1","value":"FP1"},{"label":"FP2","value":"FP2"},{"label":"FP3","value":"FP3"},{"label":"Qualifying","value":"Q"},{"label":"Race","value":"R"}]}
|
48 |
+
|
49 |
+
|
50 |
+
@app.get("/{year}/{event}", response_model=None)
|
51 |
+
def sessions_available(year: int, event: str) -> any:
|
52 |
+
# get sessions available for a given year and event
|
53 |
+
data = available_data.LatestData(year)
|
54 |
+
sessions = data.get_sessions(event)
|
55 |
+
sessions = [{"label": session, "value": session} for session in sessions]
|
56 |
+
|
57 |
+
return {"sessions": sessions}
|
58 |
+
|
59 |
+
# 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"}]}
|
60 |
+
|
61 |
+
|
62 |
+
@app.get("/{year}/{event}/{session}", response_model=None)
|
63 |
+
def session_drivers(year: int, event: str, session: str) -> any:
|
64 |
+
# get drivers available for a given year, event and session
|
65 |
+
f1session = fastf1.get_session(year, event, session)
|
66 |
+
f1session.load(telemetry=False, weather=False, messages=False)
|
67 |
+
laps = f1session.laps
|
68 |
+
team_colors = available_data.team_colors(year)
|
69 |
+
# add team_colors dict to laps on Team column
|
70 |
+
|
71 |
+
drivers = laps.Driver.unique()
|
72 |
+
# for each driver in drivers, get the Team column from laps and get the color from team_colors dict
|
73 |
+
drivers = [{"color": team_colors[laps[laps.Driver ==
|
74 |
+
driver].Team.iloc[0]], "label": driver, "value": driver} for driver in drivers]
|
75 |
+
|
76 |
+
return {"drivers": drivers}
|
77 |
+
|
78 |
+
|
79 |
+
# format for chartData {"chartData":[{"lapnumber":1},{
|
80 |
+
# "VER":91.564,
|
81 |
+
# "VER_compound":"SOFT",
|
82 |
+
# "VER_compound_color":"#FF5733",
|
83 |
+
# "lapnumber":2
|
84 |
+
# },{"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}]}
|
85 |
+
|
86 |
+
|
87 |
+
@app.get("/{year}/{event}/{session}/{driver}", response_model=None)
|
88 |
+
def laps_data(year: int, event: str, session: str, driver: str) -> any:
|
89 |
+
|
90 |
+
# get drivers available for a given year, event and session
|
91 |
+
f1session = fastf1.get_session(year, event, session)
|
92 |
+
f1session.load(telemetry=False, weather=False, messages=False)
|
93 |
+
laps = f1session.laps
|
94 |
+
team_colors = available_data.team_colors(year)
|
95 |
+
# add team_colors dict to laps on Team column
|
96 |
+
|
97 |
+
drivers = laps.Driver.unique()
|
98 |
+
# for each driver in drivers, get the Team column from laps and get the color from team_colors dict
|
99 |
+
drivers = [{"color": team_colors[laps[laps.Driver ==
|
100 |
+
driver].Team.iloc[0]], "label": driver, "value": driver} for driver in drivers]
|
101 |
+
|
102 |
+
driver_laps = laps.pick_driver(driver)
|
103 |
+
driver_laps['LapTime'] = driver_laps['LapTime'].dt.total_seconds()
|
104 |
+
compound_colors = {
|
105 |
+
"SOFT": "#FF0000",
|
106 |
+
"MEDIUM": "#FFFF00",
|
107 |
+
"HARD": "#FFFFFF",
|
108 |
+
"INTERMEDIATE": "#00FF00",
|
109 |
+
"WET": "#088cd0",
|
110 |
+
|
111 |
+
}
|
112 |
+
|
113 |
+
driver_laps_data = []
|
114 |
+
|
115 |
+
for _, row in driver_laps.iterrows():
|
116 |
+
|
117 |
+
if row['LapTime'] > 0:
|
118 |
+
lap = {f"{driver}": row['LapTime'],
|
119 |
+
f"{driver}_compound": row['Compound'],
|
120 |
+
f"{driver}_compound_color": compound_colors[row['Compound']],
|
121 |
+
"lapnumber": row['LapNumber']}
|
122 |
+
else:
|
123 |
+
lap = {"lapnumber": row['LapNumber']}
|
124 |
+
|
125 |
+
driver_laps_data.append(lap)
|
126 |
+
|
127 |
+
return {"chartData": driver_laps_data}
|
128 |
+
|
129 |
+
|
130 |
+
@app.get("/{year}/{event}/{session}/{driver}/{lap_number}", response_model=None)
|
131 |
+
def telemetry_data(year: int, event: str, session: str, driver: str, lap_number: int) -> any:
|
132 |
+
|
133 |
+
f1session = fastf1.get_session(year, event, session)
|
134 |
+
f1session.load(telemetry=True, weather=False, messages=False)
|
135 |
+
laps = f1session.laps
|
136 |
+
|
137 |
+
driver_laps = laps.pick_driver(driver)
|
138 |
+
driver_laps['LapTime'] = driver_laps['LapTime'].dt.total_seconds()
|
139 |
+
|
140 |
+
# get the telemetry for lap_number
|
141 |
+
selected_lap = driver_laps[driver_laps.LapNumber == lap_number]
|
142 |
+
|
143 |
+
telemetry = selected_lap.get_telemetry()
|
144 |
+
telemetry['Time'] = telemetry['Time'].dt.total_seconds()
|
145 |
+
|
146 |
+
laptime = selected_lap.LapTime.values[0]
|
147 |
+
data_key = f"{driver} - Lap {int(lap_number)} - {year} {session} [{int(laptime//60)}:{laptime%60}]"
|
148 |
+
|
149 |
+
brake_tel = []
|
150 |
+
drs_tel = []
|
151 |
+
gear_tel = []
|
152 |
+
rpm_tel = []
|
153 |
+
speed_tel = []
|
154 |
+
throttle_tel = []
|
155 |
+
time_tel = []
|
156 |
+
track_map = []
|
157 |
+
|
158 |
+
for _, row in telemetry.iterrows():
|
159 |
+
|
160 |
+
brake = {"x": row['Distance'],
|
161 |
+
"y": row['Brake'],
|
162 |
+
}
|
163 |
+
brake_tel.append(brake)
|
164 |
+
|
165 |
+
drs = {"x": row['Distance'],
|
166 |
+
"y": row['DRS'],
|
167 |
+
}
|
168 |
+
drs_tel.append(drs)
|
169 |
+
|
170 |
+
gear = {"x": row['Distance'],
|
171 |
+
"y": row['nGear'],
|
172 |
+
}
|
173 |
+
gear_tel.append(gear)
|
174 |
+
|
175 |
+
rpm = {"x": row['Distance'],
|
176 |
+
"y": row['RPM'],
|
177 |
+
}
|
178 |
+
rpm_tel.append(rpm)
|
179 |
+
|
180 |
+
speed = {"x": row['Distance'],
|
181 |
+
"y": row['Speed'],
|
182 |
+
}
|
183 |
+
speed_tel.append(speed)
|
184 |
+
|
185 |
+
throttle = {"x": row['Distance'],
|
186 |
+
"y": row['Throttle'],
|
187 |
+
}
|
188 |
+
throttle_tel.append(throttle)
|
189 |
+
|
190 |
+
time = {"x": row['Distance'],
|
191 |
+
"y": row['Time'],
|
192 |
+
}
|
193 |
+
time_tel.append(time)
|
194 |
+
|
195 |
+
track = {"x": row['X'],
|
196 |
+
"y": row['Y'],
|
197 |
+
}
|
198 |
+
track_map.append(track)
|
199 |
+
|
200 |
+
telemetry_data = {
|
201 |
+
"telemetryData":{
|
202 |
+
"brake": brake_tel,
|
203 |
+
"dataKey": data_key,
|
204 |
+
"drs": drs_tel,
|
205 |
+
"gear": gear_tel,
|
206 |
+
"rpm": rpm_tel,
|
207 |
+
"speed": speed_tel,
|
208 |
+
"throttle": throttle_tel,
|
209 |
+
"time": time_tel,
|
210 |
+
"trackMap": track_map,
|
211 |
+
}
|
212 |
+
}
|
213 |
+
|
214 |
+
return telemetry_data
|