theOehrly / Fast-F1

FastF1 is a python package for accessing and analyzing Formula 1 results, schedules, timing data and telemetry
https://docs.fastf1.dev
MIT License
2.29k stars 239 forks source link

[BUG] DriverAhead not working properly #553

Open foxyskippy opened 3 months ago

foxyskippy commented 3 months ago

Describe the issue:

I noticed when Norris was leading behind safetycar Verstappen "overtook" several times Norris.

Reproduce the code example:

Date                                DriverAhead DistanceToDriverAhead   Time Distance   RelativeDistance Status Driver
2024-03-09 17:20:17.2220000 0   1.42639 0 days 00:01:41.705000  3905.45 0.635869    OnTrack NOR
2024-03-09 17:20:17.3820000 0   1.42639 0 days 00:01:41.865000  3913.27 0.637142    OnTrack NOR
2024-03-09 17:20:17.3910000 0   1.42639 0 days 00:01:41.874000  3913.7  0.637213    OnTrack NOR
2024-03-09 17:20:17.5420000 0   1.42639 0 days 00:01:42.025000  3920.91 0.638387    OnTrack NOR
2024-03-09 17:20:17.6510000 0   1.42639 0 days 00:01:42.134000  3926.07 0.639227    OnTrack NOR
2024-03-09 17:20:17.7020000 0   1.42639 0 days 00:01:42.185000  3928.47 0.639617    OnTrack NOR
2024-03-09 17:20:17.9510000 0   1.42639 0 days 00:01:42.434000  3940    0.641494    OnTrack NOR
2024-03-09 17:20:18.0620000 0   1.42639 0 days 00:01:42.545000  3945.07 0.64232 OnTrack NOR
2024-03-09 17:20:18.1710000 0   1.42639 0 days 00:01:42.654000  3950.02 0.643126    OnTrack NOR
2024-03-09 17:20:18.3310000 0   1.42639 0 days 00:01:42.814000  3957.22 0.644299    OnTrack NOR
2024-03-09 17:20:18.3310000 4   27.6003 0 days 00:01:41.715000  3929.31 0.638692    OnTrack VER
2024-03-09 17:20:18.4220000 0   1.42639 0 days 00:01:42.905000  3961.27 0.644957    OnTrack NOR
2024-03-09 17:20:18.4220000 4   27.0003 0 days 00:01:41.806000  3934    0.639454    OnTrack VER
2024-03-09 17:20:19.1710000 0   1.42639 0 days 00:01:43.654000  3993.33 0.650177    OnTrack NOR
2024-03-09 17:20:19.1710000 4   21.6603 0 days 00:01:42.555000  3975.95 0.646274    OnTrack VER
2024-03-09 17:20:19.2230000 0   1.42639 0 days 00:01:43.706000  3995.53 0.650536    OnTrack NOR
2024-03-09 17:20:19.2230000 4   16.3203 0 days 00:01:42.607000  3978.94 0.64676 OnTrack VER
2024-03-09 17:20:19.3510000 0   1.42639 0 days 00:01:43.834000  4000.96 0.65142 OnTrack NOR
2024-03-09 17:20:19.3510000 4   14.4869 0 days 00:01:42.735000  3986.3  0.647956    OnTrack VER
2024-03-09 17:20:19.4630000 0   1.42639 0 days 00:01:43.946000  4005.73 0.652197    OnTrack NOR
2024-03-09 17:20:19.4630000 4   12.6536 0 days 00:01:42.847000  3992.81 0.649014    OnTrack VER
2024-03-09 17:20:19.5510000 0   1.42639 0 days 00:01:44.034000  4009.5  0.65281 OnTrack NOR
2024-03-09 17:20:19.5510000 4   10.6536 0 days 00:01:42.935000  3998.01 0.649859    OnTrack VER
2024-03-09 17:20:19.7030000 0   1.42639 0 days 00:01:44.186000  4016.07 0.653879    OnTrack NOR
2024-03-09 17:20:19.7030000 4   8.65361 0 days 00:01:43.087000  4007.14 0.651344    OnTrack VER
2024-03-09 17:20:19.8310000 0   1.42639 0 days 00:01:44.314000  4021.69 0.654794    OnTrack NOR
2024-03-09 17:20:19.8310000 4   6.93139 0 days 00:01:43.215000  4014.97 0.652616    OnTrack VER
2024-03-09 17:20:19.9030000 0   1.42639 0 days 00:01:44.386000  4024.9  0.655318    OnTrack NOR
2024-03-09 17:20:19.9030000 4   5.20917 0 days 00:01:43.287000  4019.42 0.65334 OnTrack VER
2024-03-09 17:20:20.0710000 0   1.42639 0 days 00:01:44.554000  4032.59 0.65657 OnTrack NOR
2024-03-09 17:20:20.0710000 4   3.30917 0 days 00:01:43.455000  4029.88 0.65504 OnTrack VER
2024-03-09 17:20:20.1430000 0   1.42639 0 days 00:01:44.626000  4035.97 0.65712 OnTrack NOR
2024-03-09 17:20:20.1430000 4   1.40917 0 days 00:01:43.527000  4034.29 0.655756    OnTrack VER
2024-03-09 17:20:20.2110000 0   1.42639 0 days 00:01:44.694000  4039.19 0.657645    OnTrack NOR
2024-03-09 17:20:20.2110000 4   1.33361 0 days 00:01:43.595000  4038.37 0.656419    OnTrack VER
2024-03-09 17:20:20.3910000 0   1.42639 0 days 00:01:44.874000  4047.91 0.659063    OnTrack NOR
2024-03-09 17:20:20.3910000 4   1.25806 0 days 00:01:43.775000  4048.72 0.658102    OnTrack VER
2024-03-09 17:20:20.4620000 1   1.42639 0 days 00:01:44.945000  4051.39 0.65963 OnTrack NOR
2024-03-09 17:20:20.4620000 0   1.1825  0 days 00:01:43.846000  4052.54 0.658723    OnTrack VER
2024-03-09 17:20:20.6110000 1   1.35972 0 days 00:01:45.094000  4058.76 0.66083 OnTrack NOR
2024-03-09 17:20:20.6110000 0   1.06917 0 days 00:01:43.995000  4060.03 0.65994 OnTrack VER
2024-03-09 17:20:20.7020000 1   1.29306 0 days 00:01:45.185000  4063.32 0.661573    OnTrack NOR
2024-03-09 17:20:20.7020000 0   0.955833    0 days 00:01:44.086000  4064.34 0.660641    OnTrack VER
2024-03-09 17:20:20.8110000 1   1.31337 0 days 00:01:45.294000  4068.86 0.662475    OnTrack NOR
2024-03-09 17:20:20.8110000 0   0.8425  0 days 00:01:44.195000  4069.33 0.661452    OnTrack VER
2024-03-09 17:20:20.9820000 0   1.33369 0 days 00:01:45.465000  4077.79 0.663928    OnTrack NOR
2024-03-09 17:20:20.9820000 4   0.729167    0 days 00:01:44.366000  4076.79 0.662664    OnTrack VER
2024-03-09 17:20:21.0910000 0   1.35401 0 days 00:01:45.574000  4083.69 0.664889    OnTrack NOR
2024-03-09 17:20:21.0910000 4   3.47917 0 days 00:01:44.475000  4081.34 0.663405    OnTrack VER
2024-03-09 17:20:21.3420000 0   1.37433 0 days 00:01:45.825000  4097.79 0.667185    OnTrack NOR

Error message:

No response

pesaventofilippo commented 3 months ago

This may be due to another bug, which causes the lap alignment to be skipped for the Saudi Arabian race (see #549)

theOehrly commented 3 months ago

@pesaventofilippo that's a good assumption, especially if this problem is not present in other races. I haven't investigated the problem here yet, though.

In general, given the very short distances in the safety car train and the fairly low resolution source data, this may always be tricky.

In case someone wants to try and fix #549, I'm happy to give some guidance. But the math behind that can make your head spin quite a bit 😅

foxyskippy commented 3 months ago

@pesaventofilippo @theOehrly not sure but I checked Bahrain Grand Prix and got a bit of the issues. As example; Data is from lap 4 Gridposition issue

pesaventofilippo commented 3 months ago

Uhm, I tried testing for HAM and PIA on lap 4, here's what I found:

import fastf1

session = fastf1.get_session(2024, "Bahrain", "R")
session.load()

ham_lap = session.laps.pick_driver("HAM").pick_lap(4)
ham_car = ham_lap.get_car_data().add_driver_ahead()

pia_lap = session.laps.pick_driver("PIA").pick_lap(4)
pia_car = pia_lap.get_car_data().add_driver_ahead()

# Print all instances in which the DriverAhead for HAM isn't PIA (as it should be)
print(ham_car[ham_car.DriverAhead != "81"])

# Print all instances in which the DriverAhead for PIA isn't ALO (as it should be)
print(pia_car[pia_car.DriverAhead != "14"])

Output:

                     Date    RPM  ...  DriverAhead  DistanceToDriverAhead
0 2024-03-02 15:08:42.470  10410  ...                                 NaN

                    Date    RPM  ...  DriverAhead  DistanceToDriverAhead
0 2024-03-02 15:08:41.910  10448  ...                                 NaN

In both cases, the only instance in which the DriverAhead isn't correct is the first row in the lap data, where DriverAhead is 0 (which is to be expected for the first row).

Maybe there's a problem in the way you determine the position during a lap? I could have missed something or maybe I'm not understanding, correct me if I'm wrong!

foxyskippy commented 3 months ago

I used Distance and RelativeDistance since DriverAhead is not giving proper info. As example Bahrain;

can it be the source of data (car, pos, interpolation)??? Issue GridPosition2

theOehrly commented 3 months ago

@foxyskippy could you please share a code example to reproduce the problem? I took a bit more of an in depth look at this problem now. But I can't find anything wrong. The values that I get are different from yours, and they look correct. I have no idea why you get different results. But looking at the screenshot above, you seem to merge the data somehow?

I won't dig into this any more unless you can show me how to reproduce the problem. Doing guess work is just a waste of time for everybody. My results match those of @pesaventofilippo.