Skip to content

Commit

Permalink
fix(Wyscout v3): Handle unrecognized players gracefully to prevent cr…
Browse files Browse the repository at this point in the history
…ashes (#358)
  • Loading branch information
DriesDeprest authored Dec 18, 2024
1 parent 452efe7 commit 3b40d02
Show file tree
Hide file tree
Showing 4 changed files with 3,125 additions and 6 deletions.
4 changes: 4 additions & 0 deletions kloppy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ class UnknownEncoderError(KloppyError):

class KloppyParameterError(KloppyError):
pass


class DeserializationWarning(Warning):
pass
22 changes: 16 additions & 6 deletions kloppy/infra/serializers/event/wyscout/deserializer_v3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import warnings
from dataclasses import replace
from datetime import datetime, timedelta, timezone
from enum import Enum
Expand Down Expand Up @@ -37,7 +38,7 @@
TakeOnResult,
Team,
)
from kloppy.exceptions import DeserializationError
from kloppy.exceptions import DeserializationError, DeserializationWarning
from kloppy.utils import performance_logging

from ..deserializer import EventDataDeserializer
Expand Down Expand Up @@ -780,6 +781,19 @@ def deserialize(self, inputs: WyscoutInputs) -> EventDataset:
str(raw_event["possession"]["team"]["id"])
]

if player_id == INVALID_PLAYER:
player = None
elif player_id not in players[team_id]:
player = None
warnings.warn(
f"Event {raw_event['id']} was performed by player {player_id} and team {team_id}, "
f"but the player does not appear to be part of that team's lineup. "
f"Handled by setting the event's player to None.",
DeserializationWarning,
)
else:
player = players[team_id][player_id]

generic_event_args = {
"event_id": raw_event["id"],
"raw_event": raw_event,
Expand All @@ -792,11 +806,7 @@ def deserialize(self, inputs: WyscoutInputs) -> EventDataset:
else None
),
"team": team,
"player": (
players[team_id][player_id]
if player_id != INVALID_PLAYER
else None
),
"player": player,
"ball_owning_team": ball_owning_team,
"ball_state": None,
"period": periods[-1],
Expand Down
19 changes: 19 additions & 0 deletions kloppy/tests/prs/pr_358/test_pr_358.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from kloppy import wyscout
from kloppy.exceptions import DeserializationWarning


def test_ignore_unknown_player(base_dir):
with pytest.warns(
DeserializationWarning,
match="the player does not appear to be part of that team's lineup",
):
dataset = wyscout.load(
event_data=base_dir / "prs" / "pr_358" / "wyscout_events_v3.json",
coordinates="wyscout",
)

assert len(dataset.events) == 2

assert dataset.events[1].player is None
Loading

0 comments on commit 3b40d02

Please sign in to comment.