Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add owngoal event for SB #219

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions kloppy/domain/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class EventType(Enum):
BALL_OUT (EventType):
FOUL_COMMITTED (EventType):
FORMATION_CHANGE (EventType):
OWN_GOAL_AGAINST (EventType):
"""

GENERIC = "generic"
Expand All @@ -206,6 +207,7 @@ class EventType(Enum):
BALL_OUT = "BALL_OUT"
FOUL_COMMITTED = "FOUL_COMMITTED"
FORMATION_CHANGE = "FORMATION_CHANGE"
OWN_GOAL_AGAINST = "OWN_GOAL_AGAINST"

def __repr__(self):
return self.value
Expand Down Expand Up @@ -879,6 +881,22 @@ class FoulCommittedEvent(Event):
event_name: str = "foul_committed"


@dataclass(repr=False)
@docstring_inherit_attributes(Event)
class OwnGoalAgainstEvent(Event):
"""
OwnGoalAgainstEvent

Attributes:
event_type (EventType): `EventType.OWN_GOAL_AGAINST` (See [`EventType`][kloppy.domain.models.event.EventType])
event_name (str): `"own_goal_against"`

"""

event_type: EventType = EventType.OWN_GOAL_AGAINST
event_name: str = "own_goal_against"


@dataclass(repr=False)
class EventDataset(Dataset[Event]):
"""
Expand Down Expand Up @@ -992,4 +1010,5 @@ def generic_record_converter(event: Event):
"DuelType",
"DuelQualifier",
"DuelResult",
"OwnGoalAgainstEvent",
]
4 changes: 4 additions & 0 deletions kloppy/domain/services/event_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
FoulCommittedEvent,
CardEvent,
SubstitutionEvent,
OwnGoalAgainstEvent,
)

T = TypeVar("T")
Expand Down Expand Up @@ -110,3 +111,6 @@ def build_foul_committed(self, **kwargs) -> FoulCommittedEvent:

def build_substitution(self, **kwargs) -> SubstitutionEvent:
return create_event(SubstitutionEvent, **kwargs)

def build_own_goal_against(self, **kwargs) -> OwnGoalAgainstEvent:
return create_event(OwnGoalAgainstEvent, **kwargs)
8 changes: 8 additions & 0 deletions kloppy/infra/serializers/event/statsbomb/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
SB_EVENT_TYPE_CLEARANCE = 9
SB_EVENT_TYPE_DRIBBLE = 14
SB_EVENT_TYPE_SHOT = 16
SB_EVENT_OWN_GOAL_AGAINST = 20
SB_EVENT_TYPE_PASS = 30
SB_EVENT_TYPE_50_50 = 33
SB_EVENT_TYPE_CARRY = 43
Expand Down Expand Up @@ -914,6 +915,13 @@ def deserialize(self, inputs: StatsBombInputs) -> EventDataset:
)
)
new_events.append(formation_change_event)
elif event_type == SB_EVENT_OWN_GOAL_AGAINST:
own_goal_event = self.event_factory.build_own_goal_against(
result=None,
qualifiers=None,
**generic_event_kwargs,
)
new_events.append(own_goal_event)
# rest: generic
else:
generic_event = self.event_factory.build_generic(
Expand Down
48 changes: 45 additions & 3 deletions kloppy/tests/files/statsbomb_event.json
Original file line number Diff line number Diff line change
Expand Up @@ -170248,9 +170248,51 @@
"miscontrol" : {
"aerial_won" : true
}
}, {
},{
"id" : "89dd4f4b-0a70-48d8-a0e7-ac4c",
"index" : 4001,
"period" : 2,
"timestamp" : "00:47:31.053",
"minute" : 92,
"second" : 31,
"type" : {
"id" : 20,
"name" : "Own Goal Against"
},
"possession" : 101,
"possession_team" : {
"id" : 217,
"name" : "Barcelona"
},
"play_pattern" : {
"id" : 2,
"name" : "From Corner"
},
"obv_for_after" : null,
"obv_for_before" : null,
"obv_for_net" : null,
"obv_against_after" : null,
"obv_against_before" : null,
"obv_against_net" : null,
"obv_total_net" : null,
"team" : {
"id" : 217,
"name" : "Barcelona"
},
"player" : {
"id" : 5503,
"name" : "Lionel Andrés Messi Cuccittini"
},
"position" : {
"id" : 24,
"name" : "Left Center Forward"
},
"location" : [ 4.4, 40.2 ],
"duration" : 0.0,
"related_events" : []
}, {
"id" : "e1cc4d5e-ba55-4b6b-88cc-dae13311c1d9",
"index" : 4001,
"index" : 4002,
"period" : 2,
"timestamp" : "00:47:32.053",
"minute" : 92,
Expand All @@ -170276,7 +170318,7 @@
"related_events" : [ "cd767526-5f64-4600-acbf-e0a2608f0d00" ]
}, {
"id" : "cd767526-5f64-4600-acbf-e0a2608f0d00",
"index" : 4002,
"index" : 4003,
"period" : 2,
"timestamp" : "00:47:32.053",
"minute" : 92,
Expand Down
3 changes: 2 additions & 1 deletion kloppy/tests/test_statsbomb.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_correct_deserialization(

assert dataset.metadata.provider == Provider.STATSBOMB
assert dataset.dataset_type == DatasetType.EVENT
assert len(dataset.events) == 4039
assert len(dataset.events) == 4040
assert len(dataset.metadata.periods) == 2
assert (
dataset.metadata.orientation == Orientation.ACTION_EXECUTING_TEAM
Expand Down Expand Up @@ -160,6 +160,7 @@ def test_correct_deserialization(
== DuelType.GROUND
)
assert dataset.events[272].event_type == EventType.CLEARANCE
assert dataset.events[4037].event_type == EventType.OWN_GOAL_AGAINST

def test_correct_normalized_deserialization(
self, lineup_data: Path, event_data: Path
Expand Down
2 changes: 1 addition & 1 deletion kloppy/tests/test_to_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def dataset(self, event_data: Path, lineup_data: Path) -> EventDataset:

def test_default_columns(self, dataset: EventDataset):
records = dataset.to_records()
assert len(records) == 4039
assert len(records) == 4040
assert list(records[0].keys()) == [
"event_id",
"event_type",
Expand Down