From 9a08aa18cf5e85f32ffd918be2b3d135a6a8f131 Mon Sep 17 00:00:00 2001 From: driesdeprest Date: Tue, 19 Nov 2024 16:31:12 +0100 Subject: [PATCH] add post shot xg for opta --- .../event/statsperform/deserializer.py | 16 +++++++++++++--- kloppy/tests/files/opta_f24.xml | 1 + kloppy/tests/test_opta.py | 8 ++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/kloppy/infra/serializers/event/statsperform/deserializer.py b/kloppy/infra/serializers/event/statsperform/deserializer.py index 5d0f19d4..a003b662 100644 --- a/kloppy/infra/serializers/event/statsperform/deserializer.py +++ b/kloppy/infra/serializers/event/statsperform/deserializer.py @@ -35,6 +35,7 @@ GoalkeeperActionType, CounterAttackQualifier, ExpectedGoals, + PostShotExpectedGoals, ) from kloppy.exceptions import DeserializationError from kloppy.infra.serializers.event.deserializer import EventDataDeserializer @@ -134,6 +135,7 @@ EVENT_QUALIFIER_TEAM_FORMATION = 130 EVENT_QUALIFIER_XG = 321 +EVENT_QUALIFIER_POST_SHOT_XG = 322 event_type_names = { 1: "pass", @@ -357,9 +359,17 @@ def _parse_shot(raw_event: OptaEvent) -> Dict: qualifiers=qualifiers, ) - xg_value = raw_event.qualifiers.get(EVENT_QUALIFIER_XG) - if xg_value: - event_info["statistics"] = [ExpectedGoals(value=float(xg_value))] + statistics = [] + for event_qualifier, statistic in zip( + [EVENT_QUALIFIER_XG, EVENT_QUALIFIER_POST_SHOT_XG], + [ExpectedGoals, PostShotExpectedGoals], + ): + xg_value = raw_event.qualifiers.get(event_qualifier) + if xg_value: + statistics.append(statistic(value=float(xg_value))) + + if statistics: + event_info["statistics"] = statistics return event_info diff --git a/kloppy/tests/files/opta_f24.xml b/kloppy/tests/files/opta_f24.xml index e2ba3f70..93ede91a 100644 --- a/kloppy/tests/files/opta_f24.xml +++ b/kloppy/tests/files/opta_f24.xml @@ -362,6 +362,7 @@ + diff --git a/kloppy/tests/test_opta.py b/kloppy/tests/test_opta.py index 5d860597..28878945 100644 --- a/kloppy/tests/test_opta.py +++ b/kloppy/tests/test_opta.py @@ -409,6 +409,14 @@ def test_goal(self, dataset: EventDataset): ).value == 0.9780699610710144 ) + assert ( + next( + statistic + for statistic in goal.statistics + if statistic.name == "PSxG" + ).value + == 0.98 + ) class TestOptaDuelEvent: