Skip to content

Commit

Permalink
Merge pull request #351 from my-game-plan/bugfix/wyscout-v3-penalties
Browse files Browse the repository at this point in the history
[Wyscout v3] Add support for extra time and penalties
  • Loading branch information
koenvo authored Oct 22, 2024
2 parents d70be8c + 0bfa659 commit 9063cbc
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions kloppy/infra/serializers/event/wyscout/deserializer_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@ def _parse_carry(raw_event: Dict, next_event: Dict, start_ts: Dict) -> Dict:
)

if next_event is not None:
period_id = int(next_event["matchPeriod"].replace("H", ""))
period_id = _parse_period_id(next_event["matchPeriod"])
end_timestamp = _create_timestamp_timedelta(
next_event, start_ts, period_id
)
else:
period_id = int(raw_event["matchPeriod"].replace("H", ""))
period_id = _parse_period_id(raw_event["matchPeriod"])
end_timestamp = _create_timestamp_timedelta(
raw_event, start_ts, period_id
)
Expand Down Expand Up @@ -511,6 +511,19 @@ def _players_to_dict(players: List[Player]):
return {player.player_id: player for player in players}


def _parse_period_id(raw_period: str) -> int:
if "H" in raw_period:
period_id = int(raw_period.replace("H", ""))
elif "E" in raw_period:
period_id = 2 + int(raw_period.replace("E", ""))
elif raw_period == "P":
period_id = 5
else:
raise DeserializationError(f"Unknown period {raw_period}")

return period_id


class WyscoutDeserializerV3(EventDataDeserializer[WyscoutInputs]):
@property
def provider(self) -> Provider:
Expand Down Expand Up @@ -572,14 +585,14 @@ def deserialize(self, inputs: WyscoutInputs) -> EventDataset:
next_period_id = None
if (idx + 1) < len(raw_events["events"]):
next_event = raw_events["events"][idx + 1]
next_period_id = int(
next_event["matchPeriod"].replace("H", "")
next_period_id = _parse_period_id(
next_event["matchPeriod"]
)

team_id = str(raw_event["team"]["id"])
team = teams[team_id]
player_id = str(raw_event["player"]["id"])
period_id = int(raw_event["matchPeriod"].replace("H", ""))
period_id = _parse_period_id(raw_event["matchPeriod"])

if len(periods) == 0 or periods[-1].id != period_id:
periods.append(
Expand Down

0 comments on commit 9063cbc

Please sign in to comment.