Skip to content

Commit

Permalink
Merge pull request #335 from my-game-plan/bugfix/statsperform-defende…
Browse files Browse the repository at this point in the history
…r-shot-block

[StatsPerform] Fix shot block event
  • Loading branch information
koenvo authored Oct 22, 2024
2 parents 30c262d + b918242 commit 42d9c25
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
22 changes: 16 additions & 6 deletions kloppy/infra/serializers/event/statsperform/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,12 +779,22 @@ def deserialize(self, inputs: StatsPerformInputs) -> EventDataset:
**generic_event_kwargs,
)
elif raw_event.type_id in KEEPER_EVENTS:
goalkeeper_event_kwargs = _parse_goalkeeper_events(
raw_event
)
event = self.event_factory.build_goalkeeper_event(
**goalkeeper_event_kwargs, **generic_event_kwargs
)
# Qualifier 94 means the "save" event is a shot block by a defender
if 94 in raw_event.qualifiers:
event = self.event_factory.build_generic(
**generic_event_kwargs,
result=None,
qualifiers=None,
event_name="block",
)
else:
goalkeeper_event_kwargs = _parse_goalkeeper_events(
raw_event
)
event = self.event_factory.build_goalkeeper_event(
**goalkeeper_event_kwargs,
**generic_event_kwargs,
)
elif (raw_event.type_id == EVENT_TYPE_BALL_TOUCH) & (
raw_event.outcome == 0
):
Expand Down
4 changes: 4 additions & 0 deletions kloppy/tests/files/opta_f24.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
<Q id="1149162593" qualifier_id="213" value="4.9" />
<Q id="1140451930" qualifier_id="141" value="7.4" />
</Event>
<Event id="1515097981" event_id="70" type_id="10" period_id="1" min="0" sec="10" player_id="164266" team_id="569" outcome="1" x="8.9" y="48.0" timestamp="2018-09-23T15:02:22.579" last_modified="2018-09-23T15:02:30" version="1537711350409">
<Q id="5001563785" qualifier_id="94" />
<Q id="5001563787" qualifier_id="56" value="Back" />
</Event>
<Event id="1444075194" event_id="8" type_id="1" period_id="1" min="0" sec="16" player_id="82830" team_id="569" outcome="0" x="47.3" y="10.7" timestamp="2018-09-23T15:02:30.404" last_modified="2018-09-23T15:02:32" version="1537711352109">
<Q id="2068850442" qualifier_id="56" value="Right" />
<Q id="2106816650" qualifier_id="213" value="0.2" />
Expand Down
2 changes: 1 addition & 1 deletion kloppy/tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ def read_to_stream(self, url: str, output: BinaryIO):
# Asserts borrowed from `test_opta.py`
assert dataset.metadata.provider == Provider.OPTA
assert dataset.dataset_type == DatasetType.EVENT
assert len(dataset.events) == 36
assert len(dataset.events) == 37
24 changes: 17 additions & 7 deletions kloppy/tests/test_opta.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def test_counter_attack_qualifiers(self, dataset: EventDataset):


class TestOptaPassEvent:
"""Tests related to deserialzing pass events"""
"""Tests related to deserializing pass events"""

def test_deserialize_all(self, dataset: EventDataset):
"""It should deserialize all pass events"""
Expand Down Expand Up @@ -280,7 +280,7 @@ def test_pass_qualifiers(self, dataset: EventDataset):


class TestOptaClearanceEvent:
"""Tests related to deserialzing clearance events"""
"""Tests related to deserializing clearance events"""

def test_deserialize_all(self, dataset: EventDataset):
"""It should deserialize all clearance events"""
Expand All @@ -294,7 +294,7 @@ def test_correct_deserialization(self, dataset: EventDataset):


class TestOptaShotEvent:
"""Tests related to deserialzing shot events"""
"""Tests related to deserializing shot events"""

def test_deserialize_all(self, dataset: EventDataset):
"""It should deserialize all shot events"""
Expand Down Expand Up @@ -404,7 +404,7 @@ def test_own_goal(self, dataset: EventDataset):


class TestOptaDuelEvent:
"""Tests related to deserialzing duel events"""
"""Tests related to deserializing duel events"""

def test_deserialize_all(self, dataset: EventDataset):
"""It should deserialize all duel events"""
Expand All @@ -424,7 +424,7 @@ def test_qualifiers(self, dataset: EventDataset):


class TestOptaGoalkeeperEvent:
"""Tests related to deserialzing goalkeeper events"""
"""Tests related to deserializing goalkeeper events"""

def test_deserialize_all(self, dataset: EventDataset):
"""It should deserialize all goalkeeper events"""
Expand Down Expand Up @@ -461,7 +461,7 @@ def test_qualifiers(self, dataset: EventDataset):


class TestOptaInterceptionEvent:
"""Tests related to deserialzing interception events"""
"""Tests related to deserializing interception events"""

def test_correct_deserialization(self, dataset: EventDataset):
"""Test if the interception event is correctly deserialized"""
Expand All @@ -470,9 +470,19 @@ def test_correct_deserialization(self, dataset: EventDataset):


class TestOptaMiscontrolEvent:
"""Tests related to deserialzing miscontrol events"""
"""Tests related to deserializing miscontrol events"""

def test_correct_deserialization(self, dataset: EventDataset):
"""Test if the miscontrol event is correctly deserialized"""
event = dataset.get_event_by_id("2509132175")
assert event.event_type == EventType.MISCONTROL


class TestOptaBlockEvent:
"""Tests related to deserializing block events"""

def test_correct_deserialization(self, dataset: EventDataset):
"""Test if the block event is correctly deserialized"""
event = dataset.get_event_by_id("1515097981")
assert event.event_type == EventType.GENERIC
assert event.event_name == "block"

0 comments on commit 42d9c25

Please sign in to comment.