Skip to content

Commit

Permalink
dynamically update official team colors + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
theOehrly committed Jun 30, 2024
1 parent 7fa7344 commit a7d102a
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 268 deletions.
18 changes: 17 additions & 1 deletion fastf1/plotting/_backend.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dataclasses
import warnings
from typing import (
Dict,
Expand Down Expand Up @@ -52,9 +53,24 @@ def _load_drivers_from_f1_livetiming(

if team not in teams:
normalized_full_team_name = _normalize_string(team_name).lower()
for ref_team_name in Constants[year].Teams.keys():
for ref_team_name, team_consts in Constants[year].Teams.items():
if ref_team_name in normalized_full_team_name:
team.normalized_value = ref_team_name

# copy team constants, update the official color if it
# is available from the API and add the constants to the
# team
if team_color := driver_entry.get('TeamColour'):
replacements = {'Official': f"#{team_color}"}
else:
replacements = {}
colors = dataclasses.replace(
team_consts.TeamColor, **replacements
)
team.constants = dataclasses.replace(
team_consts, TeamColor=colors
)

break
else:
warnings.warn(f"Encountered unknown team '{team_name}' while "
Expand Down
2 changes: 2 additions & 0 deletions fastf1/plotting/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rapidfuzz import fuzz

from fastf1.logger import get_logger
from fastf1.plotting._constants.base import TeamConst


_logger = get_logger(__package__)
Expand All @@ -24,6 +25,7 @@ class _Driver:
class _Team:
value: str = ''
normalized_value: str = ''
constants: TeamConst = None

def __init__(self):
super().__init__()
Expand Down
10 changes: 6 additions & 4 deletions fastf1/plotting/_constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
season2023,
season2024
)
from fastf1.plotting._constants.base import BaseSeason
from fastf1.plotting._constants.base import BaseSeasonConst


Constants: Dict[str, BaseSeason] = dict()
Constants: Dict[str, BaseSeasonConst] = dict()

for year in range(2018, 2025):
season = globals()[f"season{year}"]
Constants[str(year)] = BaseSeason(CompoundColors=season.CompoundColors,
Teams=season.Teams)
Constants[str(year)] = BaseSeasonConst(
CompoundColors=season.CompoundColors,
Teams=season.Teams
)


# Deprecated, will be removed for 2025
Expand Down
12 changes: 6 additions & 6 deletions fastf1/plotting/_constants/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Dict


class Compounds:
class CompoundsConst:
HyperSoft = "HYPERSOFT"
UltraSoft = "ULTRASOFT"
SuperSoft = "SUPERSOFT"
Expand All @@ -17,18 +17,18 @@ class Compounds:


@dataclass(frozen=True)
class TeamColors:
class TeamColorsConst:
Official: str
FastF1: str


@dataclass(frozen=True)
class Team:
class TeamConst:
ShortName: str
TeamColor: TeamColors
TeamColor: TeamColorsConst


@dataclass(frozen=True)
class BaseSeason:
class BaseSeasonConst:
CompoundColors: Dict[str, str]
Teams: Dict[str, Team]
Teams: Dict[str, TeamConst]
80 changes: 42 additions & 38 deletions fastf1/plotting/_constants/season2018.py
Original file line number Diff line number Diff line change
@@ -1,102 +1,106 @@
from typing import Dict

from fastf1.plotting._constants.base import (
Compounds,
Team,
TeamColors
CompoundsConst,
TeamColorsConst,
TeamConst
)


Teams: Dict[str, Team] = {
'ferrari': Team(
# NOTE: the team constants are copied when loading the driver-team-mapping
# and values may be modified there, it the used API provides different values


Teams: Dict[str, TeamConst] = {
'ferrari': TeamConst(
ShortName='Ferrari',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#dc0000',
FastF1='#dc0000'
)
),
'force india': Team(
'force india': TeamConst(
ShortName='Force India',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#f596c8',
FastF1='#ff87bc'
)
),
'haas': Team(
'haas': TeamConst(
ShortName='Haas',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#828282',
FastF1='#b6babd'
)
),
'mclaren': Team(
'mclaren': TeamConst(
ShortName='McLaren',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#ff8000',
FastF1='#ff8000'
)
),
'mercedes': Team(
'mercedes': TeamConst(
ShortName='Mercedes',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#00d2be',
FastF1='#00f5d0'
)
),
'racing point': Team(
'racing point': TeamConst(
ShortName='Racing Point',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#f596c8',
FastF1='#ff87bc'
)
),
'red bull': Team(
'red bull': TeamConst(
ShortName='Red Bull',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#1e41ff',
FastF1='#1e41ff'
)
),
'renault': Team(
'renault': TeamConst(
ShortName='Renault',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#fff500',
FastF1='#fff500'
)
),
'sauber': Team(
'sauber': TeamConst(
ShortName='Sauber',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#9b0000',
FastF1='#900000'
)
),
'toro rosso': Team(
'toro rosso': TeamConst(
ShortName='Toro Rosso',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#469bff',
FastF1='#2b4562'
)
),
'williams': Team(
'williams': TeamConst(
ShortName='Williams',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#ffffff',
FastF1='#00a0dd'
)
)
}

CompoundColors: Dict[Compounds, str] = {
Compounds.HyperSoft: "#feb1c1",
Compounds.UltraSoft: "#b24ba7",
Compounds.SuperSoft: "#fc2b2a",
Compounds.Soft: "#ffd318",
Compounds.Medium: "#f0f0f0",
Compounds.Hard: "#00a2f5",
Compounds.SuperHard: "#fd7d3c",
Compounds.Intermediate: "#43b02a",
Compounds.Wet: "#0067ad",
Compounds.Unknown: "#00ffff",
Compounds.TestUnknown: "#434649"
CompoundColors: Dict[CompoundsConst, str] = {
CompoundsConst.HyperSoft: "#feb1c1",
CompoundsConst.UltraSoft: "#b24ba7",
CompoundsConst.SuperSoft: "#fc2b2a",
CompoundsConst.Soft: "#ffd318",
CompoundsConst.Medium: "#f0f0f0",
CompoundsConst.Hard: "#00a2f5",
CompoundsConst.SuperHard: "#fd7d3c",
CompoundsConst.Intermediate: "#43b02a",
CompoundsConst.Wet: "#0067ad",
CompoundsConst.Unknown: "#00ffff",
CompoundsConst.TestUnknown: "#434649"
}
68 changes: 36 additions & 32 deletions fastf1/plotting/_constants/season2019.py
Original file line number Diff line number Diff line change
@@ -1,91 +1,95 @@
from typing import Dict

from fastf1.plotting._constants.base import (
Compounds,
Team,
TeamColors
CompoundsConst,
TeamColorsConst,
TeamConst
)


Teams: Dict[str, Team] = {
'alfa romeo': Team(
# NOTE: the team constants are copied when loading the driver-team-mapping
# and values may be modified there, it the used API provides different values


Teams: Dict[str, TeamConst] = {
'alfa romeo': TeamConst(
ShortName='Alfa Romeo',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#9b0000',
FastF1='#900000'
)
),
'haas': Team(
'haas': TeamConst(
ShortName='Haas',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#bd9e57',
FastF1='#bd9e57'
)
),
'ferrari': Team(
'ferrari': TeamConst(
ShortName='Ferrari',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#dc0000',
FastF1='#da291c'
)
),
'mclaren': Team(
'mclaren': TeamConst(
ShortName='McLaren',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#ff8700',
FastF1='#ff8000'
)
),
'mercedes': Team(
'mercedes': TeamConst(
ShortName='Mercedes',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#00d2be',
FastF1='#00d2be'
)
),
'racing point': Team(
'racing point': TeamConst(
ShortName='Racing Point',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#f596c8',
FastF1='#ff87bc'
)
),
'red bull': Team(
'red bull': TeamConst(
ShortName='Red Bull',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#1e41ff',
FastF1='#1e41ff'
)
),
'renault': Team(
'renault': TeamConst(
ShortName='Renault',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#fff500',
FastF1='#fff500'
)
),
'toro rosso': Team(
'toro rosso': TeamConst(
ShortName='Toro Rosso',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#469bff',
FastF1='#2b4562'
)
),
'williams': Team(
'williams': TeamConst(
ShortName='Williams',
TeamColor=TeamColors(
TeamColor=TeamColorsConst(
Official='#ffffff',
FastF1='#00a0dd'
)
)
}

CompoundColors: Dict[Compounds, str] = {
Compounds.Soft: "#da291c",
Compounds.Medium: "#ffd12e",
Compounds.Hard: "#f0f0ec",
Compounds.Intermediate: "#43b02a",
Compounds.Wet: "#0067ad",
Compounds.Unknown: "#00ffff",
Compounds.TestUnknown: "#434649"
CompoundColors: Dict[CompoundsConst, str] = {
CompoundsConst.Soft: "#da291c",
CompoundsConst.Medium: "#ffd12e",
CompoundsConst.Hard: "#f0f0ec",
CompoundsConst.Intermediate: "#43b02a",
CompoundsConst.Wet: "#0067ad",
CompoundsConst.Unknown: "#00ffff",
CompoundsConst.TestUnknown: "#434649"
}
Loading

0 comments on commit a7d102a

Please sign in to comment.