arre99 commited on
Commit
4d56b75
·
1 Parent(s): cd4ae79

modified WCC and WDC functions

Browse files
Files changed (2) hide show
  1. api_playground.ipynb +173 -1
  2. fastf1_tools.py +11 -23
api_playground.ipynb CHANGED
@@ -2,7 +2,7 @@
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
- "execution_count": 10,
6
  "id": "b7d2515e",
7
  "metadata": {},
8
  "outputs": [
@@ -32,6 +32,178 @@
32
  "# FastF1"
33
  ]
34
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  {
36
  "cell_type": "code",
37
  "execution_count": 16,
 
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
+ "execution_count": 1,
6
  "id": "b7d2515e",
7
  "metadata": {},
8
  "outputs": [
 
32
  "# FastF1"
33
  ]
34
  },
35
+ {
36
+ "cell_type": "code",
37
+ "execution_count": 2,
38
+ "id": "62bf0598",
39
+ "metadata": {},
40
+ "outputs": [
41
+ {
42
+ "name": "stderr",
43
+ "output_type": "stream",
44
+ "text": [
45
+ "req WARNING \tDEFAULT CACHE ENABLED! (941.49 MB) /home/arre/.cache/fastf1\n"
46
+ ]
47
+ },
48
+ {
49
+ "name": "stdout",
50
+ "output_type": "stream",
51
+ "text": [
52
+ " position positionText points wins driverId driverNumber \\\n",
53
+ "0 1 1 186.0 5 piastri 81 \n",
54
+ "1 2 2 176.0 2 norris 4 \n",
55
+ "2 3 3 137.0 2 max_verstappen 33 \n",
56
+ "3 4 4 111.0 0 russell 63 \n",
57
+ "4 5 5 94.0 0 leclerc 16 \n",
58
+ "5 6 6 71.0 0 hamilton 44 \n",
59
+ "6 7 7 48.0 0 antonelli 12 \n",
60
+ "7 8 8 42.0 0 albon 23 \n",
61
+ "8 9 9 21.0 0 hadjar 6 \n",
62
+ "9 10 10 20.0 0 ocon 31 \n",
63
+ "10 11 11 16.0 0 hulkenberg 27 \n",
64
+ "11 12 12 14.0 0 stroll 18 \n",
65
+ "12 13 13 12.0 0 sainz 55 \n",
66
+ "13 14 14 11.0 0 gasly 10 \n",
67
+ "14 15 15 10.0 0 tsunoda 22 \n",
68
+ "15 16 16 6.0 0 bearman 87 \n",
69
+ "16 17 17 4.0 0 lawson 30 \n",
70
+ "17 18 18 2.0 0 alonso 14 \n",
71
+ "18 19 19 0.0 0 bortoleto 5 \n",
72
+ "19 20 20 0.0 0 doohan 7 \n",
73
+ "20 21 21 0.0 0 colapinto 43 \n",
74
+ "\n",
75
+ " driverCode driverUrl givenName \\\n",
76
+ "0 PIA http://en.wikipedia.org/wiki/Oscar_Piastri Oscar \n",
77
+ "1 NOR http://en.wikipedia.org/wiki/Lando_Norris Lando \n",
78
+ "2 VER http://en.wikipedia.org/wiki/Max_Verstappen Max \n",
79
+ "3 RUS http://en.wikipedia.org/wiki/George_Russell_(r... George \n",
80
+ "4 LEC http://en.wikipedia.org/wiki/Charles_Leclerc Charles \n",
81
+ "5 HAM http://en.wikipedia.org/wiki/Lewis_Hamilton Lewis \n",
82
+ "6 ANT https://en.wikipedia.org/wiki/Andrea_Kimi_Anto... Andrea Kimi \n",
83
+ "7 ALB http://en.wikipedia.org/wiki/Alexander_Albon Alexander \n",
84
+ "8 HAD https://en.wikipedia.org/wiki/Isack_Hadjar Isack \n",
85
+ "9 OCO http://en.wikipedia.org/wiki/Esteban_Ocon Esteban \n",
86
+ "10 HUL http://en.wikipedia.org/wiki/Nico_H%C3%BClkenberg Nico \n",
87
+ "11 STR http://en.wikipedia.org/wiki/Lance_Stroll Lance \n",
88
+ "12 SAI http://en.wikipedia.org/wiki/Carlos_Sainz_Jr. Carlos \n",
89
+ "13 GAS http://en.wikipedia.org/wiki/Pierre_Gasly Pierre \n",
90
+ "14 TSU http://en.wikipedia.org/wiki/Yuki_Tsunoda Yuki \n",
91
+ "15 BEA http://en.wikipedia.org/wiki/Oliver_Bearman Oliver \n",
92
+ "16 LAW http://en.wikipedia.org/wiki/Liam_Lawson Liam \n",
93
+ "17 ALO http://en.wikipedia.org/wiki/Fernando_Alonso Fernando \n",
94
+ "18 BOR https://en.wikipedia.org/wiki/Gabriel_Bortoleto Gabriel \n",
95
+ "19 DOO http://en.wikipedia.org/wiki/Jack_Doohan Jack \n",
96
+ "20 COL http://en.wikipedia.org/wiki/Franco_Colapinto Franco \n",
97
+ "\n",
98
+ " familyName dateOfBirth driverNationality constructorIds \\\n",
99
+ "0 Piastri 2001-04-06 Australian [mclaren] \n",
100
+ "1 Norris 1999-11-13 British [mclaren] \n",
101
+ "2 Verstappen 1997-09-30 Dutch [red_bull] \n",
102
+ "3 Russell 1998-02-15 British [mercedes] \n",
103
+ "4 Leclerc 1997-10-16 Monegasque [ferrari] \n",
104
+ "5 Hamilton 1985-01-07 British [ferrari] \n",
105
+ "6 Antonelli 2006-08-25 Italian [mercedes] \n",
106
+ "7 Albon 1996-03-23 Thai [williams] \n",
107
+ "8 Hadjar 2004-09-28 French [rb] \n",
108
+ "9 Ocon 1996-09-17 French [haas] \n",
109
+ "10 Hülkenberg 1987-08-19 German [sauber] \n",
110
+ "11 Stroll 1998-10-29 Canadian [aston_martin] \n",
111
+ "12 Sainz 1994-09-01 Spanish [williams] \n",
112
+ "13 Gasly 1996-02-07 French [alpine] \n",
113
+ "14 Tsunoda 2000-05-11 Japanese [rb, red_bull] \n",
114
+ "15 Bearman 2005-05-08 British [haas] \n",
115
+ "16 Lawson 2002-02-11 New Zealander [red_bull, rb] \n",
116
+ "17 Alonso 1981-07-29 Spanish [aston_martin] \n",
117
+ "18 Bortoleto 2004-10-14 Brazilian [sauber] \n",
118
+ "19 Doohan 2003-01-20 Australian [alpine] \n",
119
+ "20 Colapinto 2003-05-27 Argentine [alpine] \n",
120
+ "\n",
121
+ " constructorUrls constructorNames \\\n",
122
+ "0 [http://en.wikipedia.org/wiki/McLaren] [McLaren] \n",
123
+ "1 [http://en.wikipedia.org/wiki/McLaren] [McLaren] \n",
124
+ "2 [http://en.wikipedia.org/wiki/Red_Bull_Racing] [Red Bull] \n",
125
+ "3 [http://en.wikipedia.org/wiki/Mercedes-Benz_in... [Mercedes] \n",
126
+ "4 [http://en.wikipedia.org/wiki/Scuderia_Ferrari] [Ferrari] \n",
127
+ "5 [http://en.wikipedia.org/wiki/Scuderia_Ferrari] [Ferrari] \n",
128
+ "6 [http://en.wikipedia.org/wiki/Mercedes-Benz_in... [Mercedes] \n",
129
+ "7 [http://en.wikipedia.org/wiki/Williams_Grand_P... [Williams] \n",
130
+ "8 [http://en.wikipedia.org/wiki/RB_Formula_One_T... [RB F1 Team] \n",
131
+ "9 [http://en.wikipedia.org/wiki/Haas_F1_Team] [Haas F1 Team] \n",
132
+ "10 [http://en.wikipedia.org/wiki/Sauber_Motorsport] [Sauber] \n",
133
+ "11 [http://en.wikipedia.org/wiki/Aston_Martin_in_... [Aston Martin] \n",
134
+ "12 [http://en.wikipedia.org/wiki/Williams_Grand_P... [Williams] \n",
135
+ "13 [http://en.wikipedia.org/wiki/Alpine_F1_Team] [Alpine F1 Team] \n",
136
+ "14 [http://en.wikipedia.org/wiki/RB_Formula_One_T... [RB F1 Team, Red Bull] \n",
137
+ "15 [http://en.wikipedia.org/wiki/Haas_F1_Team] [Haas F1 Team] \n",
138
+ "16 [http://en.wikipedia.org/wiki/Red_Bull_Racing,... [Red Bull, RB F1 Team] \n",
139
+ "17 [http://en.wikipedia.org/wiki/Aston_Martin_in_... [Aston Martin] \n",
140
+ "18 [http://en.wikipedia.org/wiki/Sauber_Motorsport] [Sauber] \n",
141
+ "19 [http://en.wikipedia.org/wiki/Alpine_F1_Team] [Alpine F1 Team] \n",
142
+ "20 [http://en.wikipedia.org/wiki/Alpine_F1_Team] [Alpine F1 Team] \n",
143
+ "\n",
144
+ " constructorNationalities \n",
145
+ "0 [British] \n",
146
+ "1 [British] \n",
147
+ "2 [Austrian] \n",
148
+ "3 [German] \n",
149
+ "4 [Italian] \n",
150
+ "5 [Italian] \n",
151
+ "6 [German] \n",
152
+ "7 [British] \n",
153
+ "8 [Italian] \n",
154
+ "9 [American] \n",
155
+ "10 [Swiss] \n",
156
+ "11 [British] \n",
157
+ "12 [British] \n",
158
+ "13 [French] \n",
159
+ "14 [Italian, Austrian] \n",
160
+ "15 [American] \n",
161
+ "16 [Austrian, Italian] \n",
162
+ "17 [British] \n",
163
+ "18 [Swiss] \n",
164
+ "19 [French] \n",
165
+ "20 [French] \n"
166
+ ]
167
+ }
168
+ ],
169
+ "source": [
170
+ "ergast = fastf1.ergast.Ergast()\n",
171
+ "driver_standings = ergast.get_driver_standings(2025).content[0]\n",
172
+ "print(driver_standings)\n",
173
+ "#driver_standing = driver_standings[[\"position\", \"points\", \"wins\", \"driverCode\"]].reset_index(drop=True)\n",
174
+ "#driver_standing = driver_standing[driver_standing[\"driverCode\"] == \"HAM\"]"
175
+ ]
176
+ },
177
+ {
178
+ "cell_type": "code",
179
+ "execution_count": 14,
180
+ "id": "bd15c64a",
181
+ "metadata": {},
182
+ "outputs": [
183
+ {
184
+ "name": "stdout",
185
+ "output_type": "stream",
186
+ "text": [
187
+ "{'Ferrari',\n",
188
+ " 'Force India',\n",
189
+ " 'Haas F1 Team',\n",
190
+ " 'McLaren',\n",
191
+ " 'Mercedes',\n",
192
+ " 'Red Bull',\n",
193
+ " 'Renault',\n",
194
+ " 'Sauber',\n",
195
+ " 'Toro Rosso',\n",
196
+ " 'Williams'}\n"
197
+ ]
198
+ }
199
+ ],
200
+ "source": [
201
+ "ergast = fastf1.ergast.Ergast()\n",
202
+ "constructor_standings = ergast.get_constructor_standings(2018).content[0]\n",
203
+ "pprint(set(constructor_standings[\"constructorName\"]))\n",
204
+ "#constructor_standing = constructor_standings[[\"position\", \"points\", \"wins\", \"constructorName\"]].reset_index(drop=True)"
205
+ ]
206
+ },
207
  {
208
  "cell_type": "code",
209
  "execution_count": 16,
fastf1_tools.py CHANGED
@@ -19,7 +19,8 @@ from utils import parser_utils, track_utils
19
  from utils.constants import (
20
  AVAILABLE_SESSION_TYPES,
21
  DRIVER_DETAILS,
22
- CONSTRUCTOR_DETAILS
 
23
  )
24
 
25
  # Custom types
@@ -123,15 +124,15 @@ def driver_championship_standings(year: int, driver_name: str) -> str:
123
  str: Formatted string with driver's position, points, and wins
124
  """
125
 
126
- with open("assets/driver_abbreviations.json") as f:
127
- driver_abbreviations = json.load(f)
128
- driver_abbreviation = driver_abbreviations[driver_name]
129
  ergast = fastf1.ergast.Ergast()
130
  driver_standings = ergast.get_driver_standings(year).content[0]
131
- driver_standing = driver_standings[["position", "points", "wins", "driverCode"]].reset_index(drop=True)
132
- driver_standing = driver_standing[driver_standing["driverCode"] == driver_abbreviation]
 
 
133
  suffix = "st" if driver_standing['position'].iloc[0] == 1 else "nd" if driver_standing['position'].iloc[0] == 2 else "rd" if driver_standing['position'].iloc[0] == 3 else "th"
134
- standings_string = f"{driver_name} is {driver_standing['position'].iloc[0]}{suffix} with {driver_standing['points'].iloc[0]} points and {driver_standing['wins'].iloc[0]} wins"
 
135
  return standings_string
136
 
137
  def constructor_championship_standings(year: int, constructor_name: str) -> str:
@@ -145,26 +146,13 @@ def constructor_championship_standings(year: int, constructor_name: str) -> str:
145
  str: Formatted string with constructor's position, points, and wins
146
  """
147
 
148
- team_mapping = {
149
- "McLaren": "McLaren",
150
- "Ferrari": "Ferrari",
151
- "Red Bull Racing": "Red Bull",
152
- "Mercedes": "Mercedes",
153
- "Aston Martin": "Aston Martin",
154
- "Alpine": "Alpine F1 Team",
155
- "Haas": "Haas F1 Team",
156
- "Racing Bulls": "RB F1 Team",
157
- "Williams": "Williams",
158
- "Kick Sauber": "Sauber"
159
- }
160
-
161
  ergast = fastf1.ergast.Ergast()
162
  constructor_standings = ergast.get_constructor_standings(year).content[0]
163
  constructor_standing = constructor_standings[["position", "points", "wins", "constructorName"]].reset_index(drop=True)
164
- mapped_name = team_mapping[constructor_name]
165
- constructor_standing = constructor_standing[constructor_standing["constructorName"] == mapped_name]
166
  suffix = "st" if constructor_standing['position'].iloc[0] == 1 else "nd" if constructor_standing['position'].iloc[0] == 2 else "rd" if constructor_standing['position'].iloc[0] == 3 else "th"
167
- standings_string = f"{constructor_name} are {constructor_standing['position'].iloc[0]}{suffix} with {constructor_standing['points'].iloc[0]} points and {constructor_standing['wins'].iloc[0]} wins"
 
168
  return standings_string
169
 
170
  def track_visualization(year: int, round: gp, visualization_type: str, driver_name: str) -> Image.Image:
 
19
  from utils.constants import (
20
  AVAILABLE_SESSION_TYPES,
21
  DRIVER_DETAILS,
22
+ CONSTRUCTOR_DETAILS,
23
+ CURRENT_YEAR
24
  )
25
 
26
  # Custom types
 
124
  str: Formatted string with driver's position, points, and wins
125
  """
126
 
 
 
 
127
  ergast = fastf1.ergast.Ergast()
128
  driver_standings = ergast.get_driver_standings(year).content[0]
129
+ driver_standing = driver_standings[["position", "points", "wins", "givenName", "familyName"]].reset_index(drop=True)
130
+ driver_standing = driver_standing[(driver_standing["givenName"] + " " + driver_standing["familyName"]) == driver_name]
131
+ if driver_standing.empty:
132
+ return f"Could not find stats for {driver_name}"
133
  suffix = "st" if driver_standing['position'].iloc[0] == 1 else "nd" if driver_standing['position'].iloc[0] == 2 else "rd" if driver_standing['position'].iloc[0] == 3 else "th"
134
+ is_was = "is" if year == CURRENT_YEAR else "was"
135
+ standings_string = f"{driver_name} {is_was} {driver_standing['position'].iloc[0]}{suffix} with {driver_standing['points'].iloc[0]} points and {driver_standing['wins'].iloc[0]} wins"
136
  return standings_string
137
 
138
  def constructor_championship_standings(year: int, constructor_name: str) -> str:
 
146
  str: Formatted string with constructor's position, points, and wins
147
  """
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  ergast = fastf1.ergast.Ergast()
150
  constructor_standings = ergast.get_constructor_standings(year).content[0]
151
  constructor_standing = constructor_standings[["position", "points", "wins", "constructorName"]].reset_index(drop=True)
152
+ constructor_standing = constructor_standing[constructor_standing["constructorName"] == constructor_name]
 
153
  suffix = "st" if constructor_standing['position'].iloc[0] == 1 else "nd" if constructor_standing['position'].iloc[0] == 2 else "rd" if constructor_standing['position'].iloc[0] == 3 else "th"
154
+ are_were = "are" if year == CURRENT_YEAR else "were"
155
+ standings_string = f"{constructor_name} {are_were} {constructor_standing['position'].iloc[0]}{suffix} with {constructor_standing['points'].iloc[0]} points and {constructor_standing['wins'].iloc[0]} wins"
156
  return standings_string
157
 
158
  def track_visualization(year: int, round: gp, visualization_type: str, driver_name: str) -> Image.Image: