Skip to content

Commit

Permalink
fix: uses function for v3 instead of resusing v2
Browse files Browse the repository at this point in the history
  • Loading branch information
fubininho committed May 23, 2024
1 parent fed94a0 commit aba5042
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
83 changes: 81 additions & 2 deletions kloppy/infra/serializers/event/wyscout/deserializer_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from dataclasses import replace
from datetime import timedelta
from typing import Dict, List, Tuple, NamedTuple, IO
from typing import Dict, List, Tuple, NamedTuple, IO, Optional

from kloppy.domain import (
BallOutEvent,
Expand Down Expand Up @@ -48,7 +48,8 @@
from kloppy.utils import performance_logging

from ..deserializer import EventDataDeserializer
from .deserializer_v2 import WyscoutInputs, _create_shot_result_coordinates
from .deserializer_v2 import WyscoutInputs
from . import wyscout_tags


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -99,6 +100,82 @@ def _parse_team(raw_events, wyId: str, ground: Ground) -> Team:
return team


def _create_shot_result_coordinates(raw_event: Dict) -> Optional[Point]:
"""Estimate the shot end location from the Wyscout tags.
Wyscout does not provide end-coordinates of shots. Instead shots on goal
are tagged with a zone. This function maps each of these zones to
a coordinate. The zones and corresponding y-coordinate are depicted below.
olt | ot | ort
--------------------------------
||=================||
-------------------------------
|| g;l | gt | grt ||
--------------------------------
ol || gcl | gc | gcr || or
--------------------------------
olb || glb | gb | gln || grb
40 45 50 55 60 (y-coordinate of zone)
44.62 55.38 (y-coordiante of post)
"""
if (
raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.GoalBottomCenter
or raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.GoalCenter
or raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.GoalTopCenter
):
return Point(100.0, 50.0)
if (
raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.GoalBottomRight
or raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.GoalCenterRight
or raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.GoalTopRight
):
return Point(100.0, 55.0)
if (
raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.GoalBottomLeft
or raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.GoalCenterLeft
or raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.GoalTopLeft
):
return Point(100.0, 45.0)
if raw_event["shot"]["goalZone"] == wyscout_tags.ShotZoneResults.OutTop:
return Point(100.0, 50.0)
if (
raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.OutRightTop
or raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.OutRight
or raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.OutBottomRight
):
return Point(100.0, 60.0)
if (
raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.OutLeftTop
or raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.OutLeft
or raw_event["shot"]["goalZone"]
== wyscout_tags.ShotZoneResults.OutBottomLeft
):
return Point(100.0, 40.0)
if raw_event["shot"]["goalZone"] == wyscout_tags.ShotZoneResults.Blocked:
return Point(
x=float(raw_event["location"]["x"]),
y=float(raw_event["positions"]["y"]),
)
return None


def _generic_qualifiers(raw_event: Dict) -> List[Qualifier]:
qualifiers: List[Qualifier] = []

Expand Down Expand Up @@ -540,6 +617,8 @@ def deserialize(self, inputs: WyscoutInputs) -> EventDataset:
ball_owning_team = teams[
str(raw_event["possession"]["team"]["id"])
]
else:
ball_owning_team = team # TODO: this solve the issue of ball owning team when transforming to spald, but it's not correct

generic_event_args = {
"event_id": raw_event["id"],
Expand Down
22 changes: 22 additions & 0 deletions kloppy/infra/serializers/event/wyscout/wyscout_tags.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import Enum

GOAL = 101
OWN_GOAL = 102

Expand Down Expand Up @@ -87,3 +89,23 @@
DANGEROUS_BALL_LOST = 2001

BLOCKED = 2101


class ShotZoneResults(Enum):
GoalBottomLeft = "glb"
GoalBottomRight = "gbr"
GoalBottomCenter = "gbc"
GoalCenterLeft = "gcl"
GoalCenter = "gc"
GoalCenterRight = "gcr"
GoalTopLeft = "gtl"
GoalTopRight = "gtr"
GoalTopCenter = "gtc"
OutBottomRight = "obr"
OutBottomLeft = "obl"
OutRight = "or"
OutLeft = "ol"
OutLeftTop = "olt"
OutTop = "ot"
OutRightTop = "ort"
Blocked = "bc"

0 comments on commit aba5042

Please sign in to comment.