From 1985b8fb5a684ff7775f0493e903082e7bc503b6 Mon Sep 17 00:00:00 2001 From: Casper Guo Date: Fri, 25 Oct 2024 12:52:00 -0400 Subject: [PATCH 1/3] Reorder fuzzy matching logic --- fastf1/plotting/_constants/__init__.py | 2 +- fastf1/plotting/_plotting.py | 41 ++++++++++++++++---------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/fastf1/plotting/_constants/__init__.py b/fastf1/plotting/_constants/__init__.py index f79bf1aa2..c888de832 100644 --- a/fastf1/plotting/_constants/__init__.py +++ b/fastf1/plotting/_constants/__init__.py @@ -38,7 +38,7 @@ 'APN': 'alpine', 'AMR': 'aston martin', 'SAU': 'sauber', - 'RB': 'rb', + 'RB' : 'visa rb', 'HAA': 'haas', 'WIL': 'williams' } diff --git a/fastf1/plotting/_plotting.py b/fastf1/plotting/_plotting.py index 2256a15b5..be3976308 100644 --- a/fastf1/plotting/_plotting.py +++ b/fastf1/plotting/_plotting.py @@ -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] @@ -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] From 4b5eef65ca65cf0677fe5b0222de05c3d6d71f78 Mon Sep 17 00:00:00 2001 From: Casper Guo Date: Fri, 25 Oct 2024 12:52:20 -0400 Subject: [PATCH 2/3] Add regression test for team_color --- fastf1/tests/test_plotting_deprecated.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/fastf1/tests/test_plotting_deprecated.py b/fastf1/tests/test_plotting_deprecated.py index 4beb77e96..9276e8c87 100644 --- a/fastf1/tests/test_plotting_deprecated.py +++ b/fastf1/tests/test_plotting_deprecated.py @@ -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(): From 1f633ebe2e9e61825d6a04c2500ecb1da5ccb93e Mon Sep 17 00:00:00 2001 From: theOehrly <23384863+theOehrly@users.noreply.github.com> Date: Sun, 27 Oct 2024 12:13:46 +0100 Subject: [PATCH 3/3] fix test failure caused by using incorrect team colors reference --- fastf1/plotting/__init__.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/fastf1/plotting/__init__.py b/fastf1/plotting/__init__.py index 1e6b6c568..9d1051feb 100644 --- a/fastf1/plotting/__init__.py +++ b/fastf1/plotting/__init__.py @@ -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 @@ -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).