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 own goals in StatsBomb deserializer #229

Merged
merged 3 commits into from
Nov 15, 2023
Merged
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
5 changes: 5 additions & 0 deletions kloppy/domain/services/state_builder/builders/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ def reduce_after(self, state: Score, event: Event) -> Score:
state = replace(state, home=state.home + 1)
else:
state = replace(state, away=state.away + 1)
elif event.result == ShotResult.OWN_GOAL:
if event.team.ground == Ground.HOME:
state = replace(state, away=state.away + 1)
else:
state = replace(state, home=state.home + 1)
return state
13 changes: 12 additions & 1 deletion kloppy/infra/serializers/event/statsbomb/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
SB_EVENT_TYPE_CLEARANCE = 9
SB_EVENT_TYPE_DRIBBLE = 14
SB_EVENT_TYPE_SHOT = 16
SB_EVENT_TYPE_OWN_GOAL_AGAINST = 20
SB_EVENT_TYPE_OWN_GOAL_FOR = 25
SB_EVENT_TYPE_GOALKEEPER_EVENT = 23
SB_EVENT_TYPE_PASS = 30
SB_EVENT_TYPE_50_50 = 33
Expand Down Expand Up @@ -856,6 +858,15 @@ def deserialize(self, inputs: StatsBombInputs) -> EventDataset:
**generic_event_kwargs,
)
new_events.append(shot_event)
elif event_type == SB_EVENT_TYPE_OWN_GOAL_AGAINST:
shot_event = self.event_factory.build_shot(
result=ShotResult.OWN_GOAL,
qualifiers=[],
**generic_event_kwargs,
)
new_events.append(shot_event)
elif event_type == SB_EVENT_TYPE_OWN_GOAL_FOR:
pass
elif event_type == SB_EVENT_TYPE_CLEARANCE:
clearance_event_kwargs = _parse_clearance(
raw_event=raw_event, events=events
Expand Down Expand Up @@ -1063,7 +1074,7 @@ def deserialize(self, inputs: StatsBombInputs) -> EventDataset:
# Last step is to add freeze_frame information
for event in events:
if event.event_type == EventType.SHOT:
if "freeze_frame" in event.raw_event["shot"]:
if "freeze_frame" in event.raw_event.get("shot", {}):
event.freeze_frame = transformer.transform_frame(
_parse_freeze_frame(
freeze_frame=event.raw_event["shot"][
Expand Down
78 changes: 77 additions & 1 deletion kloppy/tests/files/statsbomb_event.json
Original file line number Diff line number Diff line change
Expand Up @@ -170249,6 +170249,82 @@
"aerial_won" : true
}
}, {
"id" : "f942c5b5-df4b-4ee4-9e90-ed5f5",
"index" : 4005,
"period" : 2,
"timestamp" : "00:44:51.456",
"minute" : 44,
"second" : 51,
"type" : {
"id" : 25,
"name" : "Own Goal For"
},
"possession" : 101,
"possession_team" : {
"id" : 206,
"name" : "Deportivo Alavés"
},
"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" : 206,
"name" : "Deportivo Alavés"
},
"location" : [ 115.7, 39.9 ],
"duration" : 0.0,
"related_events" : [ "89dd4f4b-0a70-48d8-a0e7-ac4c" ]
}, {
"id" : "89dd4f4b-0a70-48d8-a0e7-ac4c",
"index" : 4006,
"period" : 2,
"timestamp" : "00:44:51.456",
"minute" : 44,
"second" : 51,
"type" : {
"id" : 20,
"name" : "Own Goal Against"
},
"possession" : 101,
"possession_team" : {
"id" : 206,
"name" : "Deportivo Alavés"
},
"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" : 6629,
"name" : "Fernando Pacheco Flores"
},
"position" : {
"id" : 5,
"name" : "Left Center Back"
},
"location" : [ 4.4, 40.2 ],
"duration" : 0.0,
"related_events" : [ "f942c5b5-df4b-4ee4-9e90-ed5f5" ]
}, {
"id" : "e1cc4d5e-ba55-4b6b-88cc-dae13311c1d9",
"index" : 4001,
"period" : 2,
Expand Down Expand Up @@ -170388,4 +170464,4 @@
}
}
}
]
]
2 changes: 1 addition & 1 deletion kloppy/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def test_event_dataset_to_polars(self, base_dir):
import polars as pl

c = df.select(pl.col("event_id").count())[0, 0]
assert c == 4041
assert c == 4042

def test_tracking_dataset_to_polars(self):
"""
Expand Down
5 changes: 3 additions & 2 deletions kloppy/tests/test_state_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def test_score_state_builder(self, base_dir):
"0-0": 2909,
"1-0": 717,
"2-0": 405,
"3-0": 10,
"3-0": 7,
"3-1": 4,
}

def test_sequence_state_builder(self, base_dir):
Expand Down Expand Up @@ -93,7 +94,7 @@ def test_formation_state_builder(self, base_dir):

# inspect FormationChangeEvent usage and formation state_builder
assert events_per_formation_change["4-1-4-1"] == 3085
assert events_per_formation_change["4-4-2"] == 956
assert events_per_formation_change["4-4-2"] == 957

assert dataset.metadata.teams[0].starting_formation == FormationType(
"4-4-2"
Expand Down
38 changes: 34 additions & 4 deletions kloppy/tests/test_statsbomb.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
Point,
Provider,
FormationType,
Frame,
Position,
ShotResult,
)

from kloppy import statsbomb
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_correct_deserialization(

assert dataset.metadata.provider == Provider.STATSBOMB
assert dataset.dataset_type == DatasetType.EVENT
assert len(dataset.events) == 4041
assert len(dataset.events) == 4042
assert len(dataset.metadata.periods) == 2
assert (
dataset.metadata.orientation == Orientation.ACTION_EXECUTING_TEAM
Expand Down Expand Up @@ -170,12 +170,12 @@ def test_correct_deserialization(
)

assert (
dataset.events[4039].get_qualifier_value(GoalkeeperQualifier)
dataset.events[4040].get_qualifier_value(GoalkeeperQualifier)
== GoalkeeperActionType.SMOTHER
)

assert (
dataset.events[4040].get_qualifier_value(GoalkeeperQualifier)
dataset.events[4041].get_qualifier_value(GoalkeeperQualifier)
== GoalkeeperActionType.PUNCH
)

Expand Down Expand Up @@ -254,6 +254,36 @@ def test_foul_committed(self, lineup_data: Path, event_data: Path):

assert len(dataset.events) == 23

def test_own_goal(self, lineup_data: Path, event_data: Path):
JanVanHaaren marked this conversation as resolved.
Show resolved Hide resolved
"""
Test own goal events.

The StatsBomb "Own Goal For" (id = 25) and one "Own Goal Against" (id = 20) events
should be converted to a single shot event with ShotResult.OWN_GOAL.
"""
dataset = statsbomb.load(
lineup_data=lineup_data,
event_data=event_data,
)

# The Own Goal For event should be removed
own_goal_for_event = [
event
for event in dataset.events
if event.event_id == "f942c5b5-df4b-4ee4-9e90-ed5f5"
]
assert len(own_goal_for_event) == 0

# The Own Goal Against event should be converted to a shot event
own_goal_against_event = [
event
for event in dataset.events
if event.event_id == "89dd4f4b-0a70-48d8-a0e7-ac4c"
]
assert len(own_goal_against_event) == 1
assert own_goal_against_event[0].event_type == EventType.SHOT
assert own_goal_against_event[0].result == ShotResult.OWN_GOAL

def test_related_events(self, lineup_data: Path, event_data: Path):
dataset = statsbomb.load(
lineup_data=lineup_data, event_data=event_data
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) == 4041
assert len(records) == 4042
assert list(records[0].keys()) == [
"event_id",
"event_type",
Expand Down