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

[SPORTEC] Resolved Referee Issue #371

Merged
merged 9 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 21 additions & 1 deletion kloppy/domain/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Iterable,
)

from .position import PositionType
from .position import PositionType, RefereeType

from ...utils import deprecated

Expand Down Expand Up @@ -119,6 +119,25 @@ def __str__(self):
return self.value


@dataclass(frozen=True)
class Referee:
Copy link
Contributor

@probberechts probberechts Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Referee" or "Official"? I don't have a strong preference but one reason to prefer "Official" is that it covers more roles (e.g., the fourth official).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure here either, but I will update it to Official.

referee_id: str
name: str = None
first_name: str = None
last_name: str = None
UnravelSports marked this conversation as resolved.
Show resolved Hide resolved
role: Optional[RefereeType] = None

@property
def full_name(self):
if self.name:
return self.name
if self.first_name or self.last_name:
return f"{self.first_name} {self.last_name}"
if self.role:
return f"{self.role}_{self.referee_id}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For players, we use a lowercase string if the name is unknown, like home_{jersey_no} or away_{jersey_no}. For consistency, I would use lowercase for the role of the referee too.

return self.referee_id
UnravelSports marked this conversation as resolved.
Show resolved Hide resolved


@dataclass(frozen=True)
class Player:
"""
Expand Down Expand Up @@ -1016,6 +1035,7 @@ class Metadata:
game_id: Optional[str] = None
home_coach: Optional[str] = None
away_coach: Optional[str] = None
referees: Optional[List] = field(default_factory=list)
attributes: Optional[Dict] = field(default_factory=dict, compare=False)

def __post_init__(self):
Expand Down
7 changes: 7 additions & 0 deletions kloppy/domain/models/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,10 @@ def __str__(self):
@classmethod
def unknown(cls) -> "PositionType":
return cls.Unknown


class RefereeType:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably just put this into common.py. I see why you've put this here though.

VideoReferee = "Video Referee"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
VideoReferee = "Video Referee"
VideoReferee = "Video Assistant Referee"

Referee = "Referee"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are all a referee, hence using RefereeType="Referee" seems ambiguous.

Suggested change
Referee = "Referee"
Referee = "Main Referee"

Assistant = "Assistant"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Assistant = "Assistant"
Assistant = "Assistant Referee"

FourthOfficial = "Fourth Official"
38 changes: 38 additions & 0 deletions kloppy/infra/serializers/event/sportec/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
CardType,
AttackingDirection,
PositionType,
Referee,
RefereeType,
)
from kloppy.exceptions import DeserializationError
from kloppy.infra.serializers.event.deserializer import EventDataDeserializer
Expand All @@ -55,6 +57,14 @@
"LA": PositionType.LeftWing,
}

referee_types_mapping: Dict[str, RefereeType] = {
"referee": RefereeType.Referee,
"firstAssistant": RefereeType.Assistant,
"videoReferee": RefereeType.VideoReferee,
"secondAssistant": RefereeType.Assistant,
"fourthOfficial": RefereeType.FourthOfficial,
}

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -102,6 +112,7 @@ class SportecMetadata(NamedTuple):
fps: int
home_coach: str
away_coach: str
referees: List[Referee]


def sportec_metadata_from_xml_elm(match_root) -> SportecMetadata:
Expand Down Expand Up @@ -213,6 +224,31 @@ def sportec_metadata_from_xml_elm(match_root) -> SportecMetadata:
]
)

if hasattr(match_root, "MatchInformation") and hasattr(
match_root.MatchInformation, "Referees"
):
referees = []
referee_path = objectify.ObjectPath(
"PutDataRequest.MatchInformation.Referees"
)
referee_elms = referee_path.find(match_root).iterchildren(
tag="Referee"
)

for referee in referee_elms:
ref_attrib = referee.attrib
referees.append(
Referee(
referee_id=ref_attrib["PersonId"],
name=ref_attrib["Shortname"],
first_name=ref_attrib["FirstName"],
last_name=ref_attrib["LastName"],
role=referee_types_mapping[ref_attrib["Role"]],
)
)
else:
referees = []

return SportecMetadata(
score=score,
teams=teams,
Expand All @@ -222,6 +258,7 @@ def sportec_metadata_from_xml_elm(match_root) -> SportecMetadata:
fps=SPORTEC_FPS,
home_coach=home_coach,
away_coach=away_coach,
referees=referees,
)


Expand Down Expand Up @@ -673,6 +710,7 @@ def deserialize(self, inputs: SportecEventDataInputs) -> EventDataset:
game_id=game_id,
home_coach=home_coach,
away_coach=away_coach,
referees=sportec_metadata.referees,
)

return EventDataset(
Expand Down
8 changes: 8 additions & 0 deletions kloppy/infra/serializers/tracking/sportec/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def deserialize(
with performance_logging("parse metadata", logger=logger):
sportec_metadata = sportec_metadata_from_xml_elm(match_root)
teams = home_team, away_team = sportec_metadata.teams

periods = sportec_metadata.periods
transformer = self.get_transformer(
pitch_length=sportec_metadata.x_max,
Expand All @@ -130,6 +131,10 @@ def deserialize(
home_coach = sportec_metadata.home_coach
away_coach = sportec_metadata.away_coach

referee_ids = []
if sportec_metadata.referees:
referee_ids = [x.referee_id for x in sportec_metadata.referees]

with performance_logging("parse raw data", logger=logger):
date = parse(
match_root.MatchInformation.General.attrib["KickoffTime"]
Expand All @@ -156,6 +161,7 @@ def _iter():
for i, (frame_id, frame_data) in enumerate(
sorted(raw_frames.items())
):

if "ball" not in frame_data:
# Frames without ball data are corrupt.
continue
Expand Down Expand Up @@ -193,6 +199,7 @@ def _iter():
)
for player_id, raw_player_data in frame_data.items()
if player_id != "ball"
and player_id not in referee_ids
},
other_data={},
ball_coordinates=Point3D(
Expand Down Expand Up @@ -242,6 +249,7 @@ def _iter():
game_id=game_id,
home_coach=home_coach,
away_coach=away_coach,
referees=sportec_metadata.referees,
)

return TrackingDataset(
Expand Down
Loading
Loading