Skip to content

Commit

Permalink
Merge pull request #70 from bdagnino/expand-event-types
Browse files Browse the repository at this point in the history
Added improved standard events to Metrica serializer
  • Loading branch information
koenvo authored Nov 6, 2020
2 parents 17ccd75 + 454f031 commit 216b684
Show file tree
Hide file tree
Showing 11 changed files with 490 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/ambv/black
rev: stable
rev: 20.8b1
hooks:
- id: black
language_version: python3
72 changes: 72 additions & 0 deletions kloppy/domain/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import List, Union, Dict

from kloppy.domain.models.common import DatasetType
from kloppy.utils import camelcase_to_snakecase, removes_suffix

from .common import DataRecord, Dataset, Team, Player
from .pitch import Point
Expand Down Expand Up @@ -76,6 +77,51 @@ class EventType(Enum):
CARD = "CARD"
PLAYER_ON = "PLAYER_ON"
PLAYER_OFF = "PLAYER_OFF"
RECOVERY = "RECOVERY"
BALL_OUT = "BALL_OUT"
FOUL_COMMITTED = "FOUL_COMMITTED"


@dataclass
class Qualifier(ABC):
@abstractmethod
def to_dict(self):
pass

@property
def name(self):
return camelcase_to_snakecase(
removes_suffix(type(self).__name__, "Qualifier")
)


@dataclass
class BoolQualifier(Qualifier, ABC):
value: bool

def to_dict(self):
return {f"is_{self.name}": self.value}


class EnumQualifier(Qualifier, ABC):
value: Enum

def to_dict(self):
return {f"{self.name}_type": self.value.value}


class SetPieceType(Enum):
GOAL_KICK = "GOAL_KICK"
FREE_KICK = "FREE_KICK"
THROW_IN = "THROW_IN"
CORNER_KICK = "CORNER_KICK"
PENALTY = "PENALTY"
KICK_OFF = "KICK_OFF"


@dataclass
class SetPieceQualifier(EnumQualifier):
value: SetPieceType


@dataclass
Expand All @@ -90,6 +136,8 @@ class Event(DataRecord, ABC):
raw_event: Dict
state: Dict[str, any]

qualifiers: List[Qualifier]

@property
@abstractmethod
def event_type(self) -> EventType:
Expand Down Expand Up @@ -178,6 +226,24 @@ class CardEvent(Event):
event_name: str = "card"


@dataclass
class RecoveryEvent(Event):
event_type: EventType = EventType.RECOVERY
event_name: str = "recovery"


@dataclass
class BallOutEvent(Event):
event_type: EventType = EventType.BALL_OUT
event_name: str = "ball_out"


@dataclass
class FoulCommittedEvent(Event):
event_type: EventType = EventType.FOUL_COMMITTED
event_name: str = "foul_committed"


@dataclass
class EventDataset(Dataset):
records: List[
Expand Down Expand Up @@ -225,4 +291,10 @@ def add_state(self, *args, **kwargs):
"CardEvent",
"CardType",
"EventDataset",
"RecoveryEvent",
"FoulCommittedEvent",
"BallOutEvent",
"SetPieceType",
"Qualifier",
"SetPieceQualifier",
]
6 changes: 6 additions & 0 deletions kloppy/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
EventType,
Player,
DataRecord,
SetPieceType,
)


Expand Down Expand Up @@ -202,6 +203,11 @@ def _event_to_pandas_row_converter(event: Event) -> Dict:
"end_coordinates_y": event.end_coordinates.y,
}
)

if event.qualifiers:
for qualifier in event.qualifiers:
row.update(qualifier.to_dict())

return row


Expand Down
Loading

0 comments on commit 216b684

Please sign in to comment.