Skip to content

Commit

Permalink
converted to Official
Browse files Browse the repository at this point in the history
  • Loading branch information
UnravelSports [JB] committed Dec 16, 2024
1 parent 3e45ef9 commit 44d2e7a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 deletions.
21 changes: 14 additions & 7 deletions 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, RefereeType
from .position import PositionType

from ...utils import deprecated

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


class OfficialType:
VideoAssistantReferee = "Video Assistant Referee"
MainReferee = "Main Referee"
AssistantReferee = "Assistant Referee"
FourthOfficial = "Fourth Official"


@dataclass(frozen=True)
class Referee:
referee_id: str
class Official:
official_id: str
name: Optional[str] = None
first_name: Optional[str] = None
last_name: Optional[str] = None
role: Optional[RefereeType] = None
role: Optional[OfficialType] = None

@property
def full_name(self):
Expand All @@ -134,8 +141,8 @@ def full_name(self):
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}"
return f"referee_{self.referee_id}"
return f"{self.role}_{self.official_id}"
return f"referee_{self.official_id}"


@dataclass(frozen=True)
Expand Down Expand Up @@ -1035,7 +1042,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)
officials: Optional[List] = field(default_factory=list)
attributes: Optional[Dict] = field(default_factory=dict, compare=False)

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


class RefereeType:
VideoReferee = "Video Referee"
Referee = "Referee"
Assistant = "Assistant"
FourthOfficial = "Fourth Official"
32 changes: 16 additions & 16 deletions kloppy/infra/serializers/event/sportec/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
CardType,
AttackingDirection,
PositionType,
Referee,
RefereeType,
Official,
OfficialType,
)
from kloppy.exceptions import DeserializationError
from kloppy.infra.serializers.event.deserializer import EventDataDeserializer
Expand All @@ -57,12 +57,12 @@
"LA": PositionType.LeftWing,
}

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

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -112,7 +112,7 @@ class SportecMetadata(NamedTuple):
fps: int
home_coach: str
away_coach: str
referees: List[Referee]
officials: List[Official]


def sportec_metadata_from_xml_elm(match_root) -> SportecMetadata:
Expand Down Expand Up @@ -227,7 +227,7 @@ def sportec_metadata_from_xml_elm(match_root) -> SportecMetadata:
if hasattr(match_root, "MatchInformation") and hasattr(
match_root.MatchInformation, "Referees"
):
referees = []
officials = []
referee_path = objectify.ObjectPath(
"PutDataRequest.MatchInformation.Referees"
)
Expand All @@ -237,17 +237,17 @@ def sportec_metadata_from_xml_elm(match_root) -> SportecMetadata:

for referee in referee_elms:
ref_attrib = referee.attrib
referees.append(
Referee(
referee_id=ref_attrib["PersonId"],
officials.append(
Official(
official_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 = []
officials = []

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


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

return EventDataset(
Expand Down
12 changes: 7 additions & 5 deletions kloppy/infra/serializers/tracking/sportec/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ 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]
official_ids = []
if sportec_metadata.officials:
official_ids = [
x.official_id for x in sportec_metadata.officials
]

with performance_logging("parse raw data", logger=logger):
date = parse(
Expand Down Expand Up @@ -199,7 +201,7 @@ def _iter():
)
for player_id, raw_player_data in frame_data.items()
if player_id != "ball"
and player_id not in referee_ids
and player_id not in official_ids
},
other_data={},
ball_coordinates=Point3D(
Expand Down Expand Up @@ -249,7 +251,7 @@ def _iter():
game_id=game_id,
home_coach=home_coach,
away_coach=away_coach,
referees=sportec_metadata.referees,
officials=sportec_metadata.officials,
)

return TrackingDataset(
Expand Down
4 changes: 2 additions & 2 deletions kloppy/tests/test_sportec.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_load_metadata(self, raw_data: Path, meta_data: Path):
assert dataset.metadata.periods[1].end_timestamp == timedelta(
seconds=4000 + 2996.68
)
assert len(dataset.metadata.referees) == 4
assert len(dataset.metadata.officials) == 4

def test_load_frames(self, raw_data: Path, meta_data: Path):
dataset = sportec.load_tracking(
Expand Down Expand Up @@ -251,4 +251,4 @@ def test_referees(self, raw_data_referee: Path, meta_data: Path):
coordinates="sportec",
only_alive=True,
)
assert len(dataset.metadata.referees) == 4
assert len(dataset.metadata.officials) == 4

0 comments on commit 44d2e7a

Please sign in to comment.