Skip to content

Commit

Permalink
Merge pull request #241 from my-game-plan/feature/opta-multiple-pass-…
Browse files Browse the repository at this point in the history
…qualifiers

Fix multiple pass qualifiers per pass event
  • Loading branch information
koenvo authored Dec 13, 2023
2 parents c056e0d + 9f7061c commit 8878305
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
49 changes: 27 additions & 22 deletions kloppy/infra/serializers/event/opta/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ def _parse_pass(raw_qualifiers: Dict[int, str], outcome: int) -> Dict:
else:
result = PassResult.INCOMPLETE
receiver_coordinates = _get_end_coordinates(raw_qualifiers)
qualifiers = _get_event_qualifiers(raw_qualifiers)
pass_qualifiers = _get_pass_qualifiers(raw_qualifiers)
overall_qualifiers = _get_event_qualifiers(raw_qualifiers)

qualifiers = pass_qualifiers + overall_qualifiers

return dict(
result=result,
Expand All @@ -275,7 +278,11 @@ def _parse_pass(raw_qualifiers: Dict[int, str], outcome: int) -> Dict:


def _parse_offside_pass(raw_qualifiers: Dict[int, str]) -> Dict:
qualifiers = _get_event_qualifiers(raw_qualifiers)
pass_qualifiers = _get_pass_qualifiers(raw_qualifiers)
overall_qualifiers = _get_event_qualifiers(raw_qualifiers)

qualifiers = pass_qualifiers + overall_qualifiers

return dict(
result=PassResult.OFFSIDE,
receiver_coordinates=_get_end_coordinates(raw_qualifiers),
Expand Down Expand Up @@ -518,32 +525,30 @@ def _get_event_qualifiers(raw_qualifiers: Dict[int, str]) -> List[Qualifier]:
qualifiers = []
qualifiers.extend(_get_event_setpiece_qualifiers(raw_qualifiers))
qualifiers.extend(_get_event_bodypart_qualifiers(raw_qualifiers))
qualifiers.extend(_get_event_pass_qualifiers(raw_qualifiers))
qualifiers.extend(_get_event_card_qualifiers(raw_qualifiers))
qualifiers.extend(_get_event_counter_attack_qualifiers(raw_qualifiers))
return qualifiers


def _get_event_pass_qualifiers(
raw_qualifiers: Dict[int, str]
) -> List[Qualifier]:
def _get_pass_qualifiers(raw_qualifiers: Dict[int, str]) -> List[Qualifier]:
qualifiers = []
if EVENT_QUALIFIER_CROSS in raw_qualifiers:
qualifiers.append(PassQualifier(value=PassType.CROSS))
elif EVENT_QUALIFIER_LONG_BALL in raw_qualifiers:
qualifiers.append(PassQualifier(value=PassType.LONG_BALL))
elif EVENT_QUALIFIER_CHIPPED_BALL in raw_qualifiers:
qualifiers.append(PassQualifier(value=PassType.CHIPPED_PASS))
elif EVENT_QUALIFIER_THROUGH_BALL in raw_qualifiers:
qualifiers.append(PassQualifier(value=PassType.THROUGH_BALL))
elif EVENT_QUALIFIER_LAUNCH in raw_qualifiers:
qualifiers.append(PassQualifier(value=PassType.LAUNCH))
elif EVENT_QUALIFIER_FLICK_ON in raw_qualifiers:
qualifiers.append(PassQualifier(value=PassType.FLICK_ON))
elif EVENT_QUALIFIER_ASSIST in raw_qualifiers:
qualifiers.append(PassQualifier(value=PassType.ASSIST))
elif EVENT_QUALIFIER_ASSIST_2ND in raw_qualifiers:
qualifiers.append(PassQualifier(value=PassType.ASSIST_2ND))
pass_qualifier_mapping = {
EVENT_QUALIFIER_CROSS: PassType.CROSS,
EVENT_QUALIFIER_LONG_BALL: PassType.LONG_BALL,
EVENT_QUALIFIER_CHIPPED_BALL: PassType.CHIPPED_PASS,
EVENT_QUALIFIER_THROUGH_BALL: PassType.THROUGH_BALL,
EVENT_QUALIFIER_LAUNCH: PassType.LAUNCH,
EVENT_QUALIFIER_FLICK_ON: PassType.FLICK_ON,
EVENT_QUALIFIER_ASSIST: PassType.ASSIST,
EVENT_QUALIFIER_ASSIST_2ND: PassType.ASSIST_2ND,
}
for (
sp_pass_qualifier,
pass_qualifier_value,
) in pass_qualifier_mapping.items():
if sp_pass_qualifier in raw_qualifiers:
qualifiers.append(PassQualifier(value=pass_qualifier_value))

return qualifiers


Expand Down
1 change: 1 addition & 0 deletions kloppy/tests/files/opta_f24.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Q id="1995938158" qualifier_id="140" value="45.5" />
<Q id="1933367545" qualifier_id="212" value="17.4" />
<Q id="1259500924" qualifier_id="3" />
<Q id="1959204963" qualifier_id="4" />
<Q id="2075266292" qualifier_id="213" value="6.1" />
<Q id="1595770571" qualifier_id="141" value="68.2" />
<Q id="1233258682" qualifier_id="56" value="Back" />
Expand Down
13 changes: 11 additions & 2 deletions kloppy/tests/test_opta.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
Point3D,
)

from kloppy.domain.models.event import EventType
from kloppy.domain.models.event import (
EventType,
PassQualifier,
BodyPartQualifier,
)

from kloppy import opta

Expand Down Expand Up @@ -95,7 +99,12 @@ def test_correct_deserialization(self, f7_data: str, f24_data: str):
dataset.events[0].qualifiers[0].value == SetPieceType.KICK_OFF
) # 1510681159
assert (
dataset.events[6].qualifiers[0].value == BodyPart.HEAD
BodyPartQualifier(value=BodyPart.HEAD)
in dataset.events[6].qualifiers
) # 1101592119
assert (
PassQualifier(value=PassType.THROUGH_BALL)
in dataset.events[6].qualifiers
) # 1101592119
assert (
dataset.events[5].qualifiers[0].value == PassType.CHIPPED_PASS
Expand Down

0 comments on commit 8878305

Please sign in to comment.