Skip to content

Commit

Permalink
Merge pull request #170 from my-game-plan/feature/wyscout-v3-support
Browse files Browse the repository at this point in the history
Add support for Wyscout V3
  • Loading branch information
koenvo authored Mar 14, 2023
2 parents 5c6c568 + 2df5b67 commit d945f1e
Show file tree
Hide file tree
Showing 7 changed files with 51,895 additions and 48,003 deletions.
41 changes: 38 additions & 3 deletions kloppy/_providers/wyscout.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Union
import json
from typing import Union, Type

from kloppy.config import get_config
from kloppy.infra.serializers.event.wyscout import (
WyscoutDeserializer,
WyscoutDeserializerV3,
WyscoutDeserializerV2,
WyscoutInputs,
)
from kloppy.domain import EventDataset, Optional, List, EventFactory
Expand All @@ -14,6 +16,7 @@ def load(
event_types: Optional[List[str]] = None,
coordinates: Optional[str] = None,
event_factory: Optional[EventFactory] = None,
data_version: Optional[str] = None,
) -> EventDataset:
"""
Load Wyscout event data into a [`EventDataset`][kloppy.domain.models.event.EventDataset]
Expand All @@ -23,12 +26,21 @@ def load(
event_types:
coordinates:
event_factory:
data_version:
"""
deserializer = WyscoutDeserializer(
if data_version == "V2":
deserializer_class = WyscoutDeserializerV2
elif data_version == "V3":
deserializer_class = WyscoutDeserializerV3
else:
deserializer_class = identify_deserializer(event_data)

deserializer = deserializer_class(
event_types=event_types,
coordinate_system=coordinates,
event_factory=event_factory or get_config("event_factory"),
)

with open_as_file(event_data) as event_data_fp:
return deserializer.deserialize(
inputs=WyscoutInputs(event_data=event_data_fp),
Expand All @@ -47,3 +59,26 @@ def load_open_data(
coordinates=coordinates,
event_factory=event_factory,
)


def identify_deserializer(
event_data: FileLike,
) -> Union[Type[WyscoutDeserializerV3], Type[WyscoutDeserializerV2]]:
with open_as_file(event_data) as event_data_fp:
events_with_meta = json.load(event_data_fp)

events = events_with_meta["events"]
first_event = events[0]

deserializer = None
if "eventName" in first_event:
deserializer = WyscoutDeserializerV2
elif "primary" in first_event.get("type", {}):
deserializer = WyscoutDeserializerV3

if deserializer is None:
raise ValueError(
"Wyscout data version could not be recognized, please specify"
)

return deserializer
3 changes: 2 additions & 1 deletion kloppy/infra/serializers/event/wyscout/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .deserializer import WyscoutDeserializer, WyscoutInputs
from .deserializer_v3 import WyscoutDeserializerV3, WyscoutInputs
from .deserializer_v2 import WyscoutDeserializerV2
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class WyscoutInputs(NamedTuple):
event_data: IO[bytes]


class WyscoutDeserializer(EventDataDeserializer[WyscoutInputs]):
class WyscoutDeserializerV2(EventDataDeserializer[WyscoutInputs]):
@property
def provider(self) -> Provider:
return Provider.WYSCOUT
Expand Down
Loading

0 comments on commit d945f1e

Please sign in to comment.