Skip to content

Commit

Permalink
Merge pull request #292 from my-game-plan/feature/add-pressure-event
Browse files Browse the repository at this point in the history
Add pressure event to data model & StatsBomb parser
  • Loading branch information
koenvo authored Apr 2, 2024
2 parents 714041f + 8fb129f commit cd3d116
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions kloppy/domain/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class EventType(Enum):
BALL_OUT (EventType):
FOUL_COMMITTED (EventType):
GOALKEEPER (EventType):
PRESSURE (EventType):
FORMATION_CHANGE (EventType):
"""

Expand All @@ -238,6 +239,7 @@ class EventType(Enum):
BALL_OUT = "BALL_OUT"
FOUL_COMMITTED = "FOUL_COMMITTED"
GOALKEEPER = "GOALKEEPER"
PRESSURE = "PRESSURE"
FORMATION_CHANGE = "FORMATION_CHANGE"

def __repr__(self):
Expand Down Expand Up @@ -1024,6 +1026,24 @@ class GoalkeeperEvent(Event):
event_name: str = "goalkeeper"


@dataclass(repr=False)
@docstring_inherit_attributes(Event)
class PressureEvent(Event):
"""
PressureEvent
Attributes:
event_type (EventType): `EventType.Pressure` (See [`EventType`][kloppy.domain.models.event.EventType])
event_name (str): `"pressure"`,
end_timestamp (float):
"""

end_timestamp: float

event_type: EventType = EventType.PRESSURE
event_name: str = "pressure"


@dataclass(repr=False)
class EventDataset(Dataset[Event]):
"""
Expand Down
4 changes: 4 additions & 0 deletions kloppy/domain/services/event_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
SubstitutionEvent,
GoalkeeperEvent,
)
from kloppy.domain.models.event import PressureEvent

T = TypeVar("T")

Expand Down Expand Up @@ -122,3 +123,6 @@ def build_substitution(self, **kwargs) -> SubstitutionEvent:

def build_goalkeeper_event(self, **kwargs) -> GoalkeeperEvent:
return create_event(GoalkeeperEvent, **kwargs)

def build_pressure_event(self, **kwargs) -> PressureEvent:
return create_event(PressureEvent, **kwargs)
19 changes: 19 additions & 0 deletions kloppy/infra/serializers/event/statsbomb/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,24 @@ def _create_events(
return [recovery_event]


class PRESSURE(EVENT):
"""StatsBomb 17/Pressure event."""

def _create_events(
self, event_factory: EventFactory, **generic_event_kwargs
) -> List[Event]:
end_timestamp = generic_event_kwargs["timestamp"] + self.raw_event.get(
"duration", 0.0
)
pressure_event = event_factory.build_pressure_event(
result=None,
qualifiers=None,
end_timestamp=end_timestamp,
**generic_event_kwargs,
)
return [pressure_event]


class TACTICAL_SHIFT(EVENT):
"""StatsBomb 36/Tactical shift event."""

Expand Down Expand Up @@ -1282,6 +1300,7 @@ def event_decoder(raw_event: Dict) -> Union[EVENT, Dict]:
EVENT_TYPE.PLAYER_ON: PLAYER_ON,
EVENT_TYPE.PLAYER_OFF: PLAYER_OFF,
EVENT_TYPE.BALL_RECOVERY: BALL_RECOVERY,
EVENT_TYPE.PRESSURE: PRESSURE,
EVENT_TYPE.TACTICAL_SHIFT: TACTICAL_SHIFT,
}
event_type = EVENT_TYPE(raw_event["type"])
Expand Down
9 changes: 9 additions & 0 deletions kloppy/tests/test_statsbomb.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,15 @@ def test_card(self, dataset: EventDataset):
assert foul_without_card.get_qualifier_value(CardQualifier) is None


class TestStatsBombPressureEvent:
"""Tests related to deserializing 17/Pressure events"""

def test_deserialize_all(self, dataset: EventDataset):
"""It should deserialize all pressure events"""
events = dataset.find_all("pressure")
assert len(events) == 203


class TestStatsBombPlayerOffEvent:
"""Tests related to deserializing 19/Player Off events"""

Expand Down

0 comments on commit cd3d116

Please sign in to comment.