Skip to content

Commit

Permalink
Fix issues around legacy team color plotting (#645)
Browse files Browse the repository at this point in the history
* Fix internal team name mismatch for "visa rb"

* Reorder fuzzy matching logic

* Add regression test for team_color

* fix test failure caused by using incorrect team colors reference

---------

Co-authored-by: theOehrly <[email protected]>
(cherry picked from commit 958576e)
  • Loading branch information
Casper-Guo authored and theOehrly committed Oct 27, 2024
1 parent 116c57b commit 1363421
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
9 changes: 3 additions & 6 deletions fastf1/plotting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
LEGACY_DRIVER_COLORS as _LEGACY_DRIVER_COLORS
from fastf1.plotting._constants import \
LEGACY_DRIVER_TRANSLATE as _LEGACY_DRIVER_TRANSLATE
from fastf1.plotting._constants import \
LEGACY_TEAM_COLORS as _LEGACY_TEAM_COLORS
from fastf1.plotting._constants import \
LEGACY_TEAM_TRANSLATE as _LEGACY_TEAM_TRANSLATE
from fastf1.plotting._constants import Constants as _Constants
Expand Down Expand Up @@ -134,12 +136,7 @@ def __getattr__(name):
future version. Use :func:`~fastf1.plotting.get_driver_name` instead.
"""

_DEPR_TEAM_COLORS: dict[str, str] = {
# str(key.value): val for key, val
# in _Constants['2024'].Colormaps[_Colormaps.Default].items()
name.replace("kick ", ""): team.TeamColor.FastF1 for name, team
in _Constants['2024'].Teams.items()
}
_DEPR_TEAM_COLORS: dict[str, str] = _LEGACY_TEAM_COLORS.copy()
TEAM_COLORS: dict[str, str]
"""
Mapping of team names to team colors (hex color codes).
Expand Down
2 changes: 1 addition & 1 deletion fastf1/plotting/_constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
'APN': 'alpine',
'AMR': 'aston martin',
'SAU': 'sauber',
'RB': 'rb',
'RB' : 'visa rb',
'HAA': 'haas',
'WIL': 'williams'
}
Expand Down
41 changes: 25 additions & 16 deletions fastf1/plotting/_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,23 @@ def driver_color(identifier: str) -> str:
ratio = fuzz.ratio(identifier, existing_key)
key_ratios.append((ratio, existing_key))
key_ratios.sort(reverse=True)
if key_ratios[0][0] != 100:
_logger.warning(
"Correcting invalid user input "
f"'{identifier}' to '{key_ratios[0][1]}'."

)
if ((key_ratios[0][0] < 35)
or (key_ratios[0][0] / key_ratios[1][0] < 1.2)):
if (key_ratios[0][0] < 35) or (
key_ratios[0][0] / key_ratios[1][0] < 1.2
):
# ensure that the best match has a minimum accuracy (35 out of
# 100) and that it has a minimum confidence (at least 20% better
# than second best)
raise KeyError
raise KeyError(
f"Cannot find a good match for user input: {identifier}"
)

if key_ratios[0][0] != 100:
_logger.warning(
"Correcting invalid user input "
f"'{identifier}' to '{key_ratios[0][1]}'."
)

best_matched_key = key_ratios[0][1]
return LEGACY_DRIVER_COLORS[best_matched_key]

Expand Down Expand Up @@ -263,18 +268,22 @@ def team_color(identifier: str) -> str:
ratio = fuzz.ratio(identifier, existing_key)
key_ratios.append((ratio, existing_key))
key_ratios.sort(reverse=True)
if key_ratios[0][0] != 100:
_logger.warning(
"Correcting invalid user input "
f"'{identifier}' to '{key_ratios[0][1]}'."

)
if ((key_ratios[0][0] < 35)
or (key_ratios[0][0] / key_ratios[1][0] < 1.2)):
if (key_ratios[0][0] < 35) or (
key_ratios[0][0] / key_ratios[1][0] < 1.2
):
# ensure that the best match has a minimum accuracy (35 out of
# 100) and that it has a minimum confidence (at least 20% better
# than second best)
raise KeyError
raise KeyError(
f"Cannot find a good match for user input: {identifier}"
)

if key_ratios[0][0] != 100:
_logger.warning(
"Correcting invalid user input "
f"'{identifier}' to '{key_ratios[0][1]}'."
)
best_matched_key = key_ratios[0][1]
return LEGACY_TEAM_COLORS[best_matched_key]

Expand Down
17 changes: 13 additions & 4 deletions fastf1/tests/test_plotting_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,20 @@ def test_driver_color():

def test_team_color():
with pytest.warns(FutureWarning, match="is deprecated"):
color = fastf1.plotting.team_color('ferrari')
color_ferrari = fastf1.plotting.team_color('ferrari')

assert color.startswith('#')
assert len(color) == 7
_ = int(color[1:], base=16) # ensure that it's a valid hex color
assert color_ferrari.startswith('#')
assert len(color_ferrari) == 7
_ = int(color_ferrari[1:], base=16) # ensure that it's a valid hex color

with pytest.warns(FutureWarning, match="is deprecated"):
color_visa = fastf1.plotting.team_color("visa")
color_rb = fastf1.plotting.team_color("rb")
color_visa_rb = fastf1.plotting.team_color("visa rb")
color_rbr = fastf1.plotting.team_color("RBR")

assert color_visa == color_rb == color_visa_rb
assert color_visa_rb != color_rbr


def test_lapnumber_axis():
Expand Down

0 comments on commit 1363421

Please sign in to comment.