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.49k stars 260 forks source link

[BUG] Legacy plotting cannot handle RB #644

Open Casper-Guo opened 2 days ago

Casper-Guo commented 2 days ago

Describe the issue:

The legacy team_color function throws a KeyError when rb is used as an input

Reproduce the code example:

session = f.get_session(2024, "US Grand Prix", "R")

laps = session.laps.pick_wo_box().pick_track_status("467", how="none")

laps["LapTime (s)"] = laps["LapTime"].dt.total_seconds()
team_order = (
    laps[["Team", "LapTime (s)"]]
    .groupby("Team")
    .median(numeric_only=True)["LapTime (s)"]
    .sort_values()
    .index
)
team_palette = {team: p.team_color(team) for team in team_order}

Error message:

File "/home/robery/Projects/Armchair-Strategist/f1_visualization/readme_machine.py", line 151, in main
    team_palette = {team: p.team_color(team) for team in team_order}
                          ^^^^^^^^^^^^^^^^^^
  File "/home/robery/Projects/Armchair-Strategist/env/lib/python3.12/site-packages/fastf1/plotting/_plotting.py", line 244, in team_color
    return LEGACY_TEAM_COLORS[LEGACY_TEAM_TRANSLATE[identifier.upper()]]
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'rb'
Casper-Guo commented 2 days ago

Values in LEGACY_TEAM_TRANSLATE are used as keys for LEGACY_TEAM_COLORS without checking:

return LEGACY_TEAM_COLORS[LEGACY_TEAM_TRANSLATE[identifier.upper()]]

There is no rb key in LEGACY_TEAM_COLORS:

LEGACY_TEAM_COLORS = {
    'mercedes': '#00d2be', 'ferrari': '#dc0000',
    'red bull': '#fcd700', 'mclaren': '#ff8700',
    'alpine': '#fe86bc', 'aston martin': '#006f62',
    'sauber': '#00e701', 'visa rb': '#1634cb',
    'haas': '#ffffff', 'williams': '#00a0dd'
}

LEGACY_TEAM_TRANSLATE: dict[str, str] = {
    'MER': 'mercedes',
    'FER': 'ferrari',
    'RBR': 'red bull',
    'MCL': 'mclaren',
    'APN': 'alpine',
    'AMR': 'aston martin',
    'SAU': 'sauber',
    'RB': 'rb',
    'HAA': 'haas',
    'WIL': 'williams'
}

Confirmed locally that this is the only change needed