File size: 10,247 Bytes
d7745f2
bcf020e
c46cec1
 
 
 
 
84b4aa4
c46cec1
d7745f2
17976c1
d7745f2
fb11f75
b8df987
35e393c
 
ce9721c
bdf1ad8
6706c02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75b2bb3
2171ad9
84b4aa4
2171ad9
6a0c8a8
75b2bb3
 
84b4aa4
beb64c6
12c311f
beb64c6
 
75b2bb3
beb64c6
 
 
 
75b2bb3
beb64c6
 
 
 
41aaaa6
db1622b
41aaaa6
75b2bb3
217dd35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44864e0
b3cc530
 
217dd35
 
 
44864e0
b3cc530
 
217dd35
 
bb00581
 
75b2bb3
c46cec1
bb00581
217dd35
 
db1622b
 
 
6706c02
db1622b
6706c02
db1622b
217dd35
 
db1622b
217dd35
 
 
 
 
 
 
db1622b
 
44864e0
bcf020e
217dd35
6706c02
147b75e
cb7b7fd
6706c02
 
c46cec1
6706c02
217dd35
6706c02
 
217dd35
6706c02
 
 
 
c46cec1
6706c02
 
 
 
 
 
 
b3cc530
 
6706c02
 
12c311f
 
44864e0
 
 
 
163292a
44864e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3cc530
 
44864e0
 
 
 
beb64c6
 
217dd35
b3cc530
beb64c6
217dd35
44864e0
 
 
 
b3cc530
44864e0
 
b3cc530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46cec1
bcf020e
b3344b3
217dd35
6706c02
b3cc530
6706c02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217dd35
ed4ba7d
44864e0
 
 
 
b3cc530
44864e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d0ff81
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
import os
from shiny import App, ui, render, reactive
import fastf1 as ff1
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib import cm
import numpy as np
import shinyswatch

# Define cache folder path
cache_path = os.getcwd() + "/cache"
print(f"Cache path: {cache_path}")

ff1.Cache.enable_cache(cache_path)

# Offline mode to prevent F1 API crashes on Hugging Face
ff1.Cache.offline_mode(enabled=True)

# Define drivers
drivers_2023 = {'Fastest driver': 'Fastest driver', 'VER': 'Max Verstappen', 
                'NOR': 'Lando Norris', 'GAS': 'Pierre Gasly', 'PER': 'Sergio Perez', 
                'ALO': 'Fernando Alonso', 'LEC': 'Charles Leclerc', 'STR': 'Lance Stroll', 
                'MAG': 'Kevin Magnussen', 'TSU': 'Yuki Tsunoda', 'ALB': 'Alexander Albon',
                'ZHO': 'Guanyu Zhou', 'HUL': 'Nico Hülkenberg', 'OCO': 'Esteban Ocon',
                'HAM': 'Lewis Hamilton', 'SAI': 'Carlos Sainz', 'RUS': 'George Russel',
                'BOT': 'Valteri Bottas', 'PIA': 'Oscar Piastri', 'VRI': 'Nyck de Vries',
                'SAR': 'Logan Sargeant', 'RIC': 'Daniel Ricciardo'}

drivers_2022 = {'Fastest driver': 'Fastest driver', 'VER': 'Max Verstappen', 
                'NOR': 'Lando Norris', 'GAS': 'Pierre Gasly', 'PER': 'Sergio Perez', 
                'ALO': 'Fernando Alonso', 'LEC': 'Charles Leclerc', 'STR': 'Lance Stroll', 
                'MAG': 'Kevin Magnussen', 'TSU': 'Yuki Tsunoda', 'ALB': 'Alexander Albon',
                'ZHO': 'Guanyu Zhou', 'HUL': 'Nico Hülkenberg', 'OCO': 'Esteban Ocon',
                'HAM': 'Lewis Hamilton', 'SAI': 'Carlos Sainz', 'RUS': 'George Russel',
                'BOT': 'Valteri Bottas', 'VRI': 'Nyck de Vries', 'VET': 'Sebastian Vettel',
                'RIC': 'Daniel Ricciardo', 'MSC': 'Mick Schumacher',
                'LAT': 'Nicolas Latifi'}

app_ui = ui.page_fluid(
    {"style": "padding: 10px"},
    shinyswatch.theme.minty(),
    ui.include_css("styles.css"),
    ui.panel_title("Gear usage in fastest lap"),
    ui.layout_sidebar(
        ui.panel_sidebar(
            ui.input_select(
                "track_select", "Select track:",
                choices = ["Austria", "Hungary", "Spain", "Bahrain", "United-Kingdom"],
                selected = "Austria"
            ),
            ui.input_radio_buttons(
                "session_type", "Session type:", 
                choices = {"R": "Race", "Q": "Qualification"}, 
                selected = "R"
            ),
            ui.input_radio_buttons(
                "year", "Year:", 
                choices = ["2023", "2022"], 
                selected = "2023"
            ),
            width=2
        ),
        
        ui.panel_main(
            ui.row(
                ui.column(
                    6,
                    ui.input_select(
                        "driver1_select", label="Select driver 1:",
                        choices = ["Fastest driver"],
                        selected = "Fastest driver"
                    )
                ),
                ui.column(
                    6,
                    ui.input_select(
                        "driver2_select", label="Select driver 2:",
                        choices = ["Fastest driver"],
                        selected = "Fastest driver"
                    )
                )
            ),
            ui.row(
                ui.column(
                    6,
                    ui.output_plot("gear_1"),
                    ui.output_text("fastest_driver_1"),
                    ui.output_text("laptime_1")
                ),
                ui.column(
                    6,
                    ui.output_plot("gear_2"),
                    ui.output_text("fastest_driver_2"),
                    ui.output_text("laptime_2")
                )
            ),
        ),
    ),
)   

def server(input, output, session):

    # Updating driver selection list
    @reactive.Effect()
    def _():
        if input.year() == "2023":
            driver_options = drivers_2023
        elif input.year() == "2022":
            driver_options = drivers_2022
            
        ui.update_select("driver1_select",
            label="Select driver 1:",
            choices=driver_options,
            selected=input.driver1_select()
        )

        ui.update_select("driver2_select",
            label="Select driver 2:",
            choices=driver_options,
            selected=input.driver2_select()
        )
    
    # Get required data for driver 2 based on selection
    @reactive.Calc
    def get_data_1():
        try:
            ui.notification_show("Data takes a couple seconds to load.", duration=3, type = 'default')
            
            f1_session = ff1.get_session(int(input.year()), input.track_select(), input.session_type())
            f1_session.load()

            # Check if user input == fastest driver
            if input.driver1_select() == "Fastest driver":
                lap = f1_session.laps.pick_fastest()
            else:
                laps_driver = f1_session.laps.pick_driver(input.driver1_select())
                lap = laps_driver.pick_fastest()
            
            tel = lap.get_telemetry()
            driver = lap['Driver']

            #converting data to numpy data tables
            x = np.array(tel['X'].values)
            y = np.array(tel['Y'].values)

            points = np.array([x, y]).T.reshape(-1, 1, 2)
            segments = np.concatenate([points[:-1], points[1:]], axis=1)
            gear = tel['nGear'].to_numpy().astype(float)
            lap_time = lap['LapTime']
            return segments, gear, driver, lap_time

        except Exception:
            ui.notification_show("Data not available. Select another track or driver.", duration=10, type = 'error')

    # Get required data for driver 2 based on selection
    @reactive.Calc
    def get_data_2():
        try:
            ui.notification_show("Data takes a couple seconds to load.", duration=3, type = 'default')
            f1_session = ff1.get_session(int(input.year()), input.track_select(), input.session_type())
            f1_session.load()

            # Check if user input == fastest driver
            if input.driver2_select() == "Fastest driver":
                lap = f1_session.laps.pick_fastest()
            else:
                laps_driver = f1_session.laps.pick_driver(input.driver2_select())
                lap = laps_driver.pick_fastest()
            
            tel = lap.get_telemetry()
            driver = lap['Driver']

            #converting data to numpy data tables
            x = np.array(tel['X'].values)
            y = np.array(tel['Y'].values)

            points = np.array([x, y]).T.reshape(-1, 1, 2)
            segments = np.concatenate([points[:-1], points[1:]], axis=1)
            gear = tel['nGear'].to_numpy().astype(float)
            lap_time = lap['LapTime']
            return segments, gear, driver, lap_time

        except Exception:
            ui.notification_show("Data not available. Select another track or driver.", duration=10, type = 'error')
    
    @output
    @render.text
    def fastest_driver_1():
        segments, gear, driver, lap_time = get_data_1()
        #print(f"The driver of the fastest lap this session is: {driver}")
        return f"Graph shows the fastest lap of: {driver}"
    
    @output
    @render.text
    def fastest_driver_2():
        segments, gear, driver, lap_time = get_data_2()
        #print(f"The driver of the fastest lap this session is: {driver}")
        return f"Graph shows the fastest lap of: {driver}"
    
    @output
    @render.text
    def laptime_1():
        segments, gear, driver, lap_time = get_data_1()
        delta_str= str(lap_time)
        # Split the time delta string to extract hours, minutes, and seconds
        time_parts = delta_str.split(" ")[-1].split(":")
        hours, minutes, seconds = map(float, time_parts)

        # Convert the extracted values to the desired format
        formatted_time = "{:02d}:{:06.3f}".format(int(hours * 60 + minutes), seconds)

        return f"The lap time is: {formatted_time}"
    
    @output
    @render.text
    def laptime_2():
        segments, gear, driver, lap_time = get_data_2()
        delta_str= str(lap_time)
        # Split the time delta string to extract hours, minutes, and seconds
        time_parts = delta_str.split(" ")[-1].split(":")
        hours, minutes, seconds = map(float, time_parts)

        # Convert the extracted values to the desired format
        formatted_time = "{:02d}:{:06.3f}".format(int(hours * 60 + minutes), seconds)

        return f"The lap time is: {formatted_time}"

    @output
    @render.plot
    def gear_1():
        try:
            segments, gear, driver, lap_time = get_data_1()
            
            cmap = cm.get_cmap('Paired')
            lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N+1), cmap=cmap)
            lc_comp.set_array(gear)
            lc_comp.set_linewidth(4)

            plt.gca().add_collection(lc_comp)
            plt.axis('equal')
            plt.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)

            cbar = plt.colorbar(mappable=lc_comp, label="Gear", boundaries=np.arange(1, 10))
            cbar.set_ticks(np.arange(1.5, 9.5))
            cbar.set_ticklabels(np.arange(1, 9))
    
            plt

        except Exception:
            pass

    @output
    @render.plot
    def gear_2():
        try:
            segments, gear, driver, lap_time = get_data_2()
            
            cmap = cm.get_cmap('Paired')
            lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N+1), cmap=cmap)
            lc_comp.set_array(gear)
            lc_comp.set_linewidth(4)

            plt.gca().add_collection(lc_comp)
            plt.axis('equal')
            plt.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)

            cbar = plt.colorbar(mappable=lc_comp, label="Gear", boundaries=np.arange(1, 10))
            cbar.set_ticks(np.arange(1.5, 9.5))
            cbar.set_ticklabels(np.arange(1, 9))
    
            plt

        except Exception:
            pass

app = App(app_ui, server)