From 637fad9505328bc49ffd78ed8dab542d03141a4d Mon Sep 17 00:00:00 2001 From: driesdeprest Date: Fri, 30 Jun 2023 11:02:45 +0200 Subject: [PATCH 1/3] Add speed parsing for SecondSpectrum --- kloppy/domain/models/tracking.py | 1 + kloppy/domain/services/transformers/dataset.py | 1 + kloppy/infra/serializers/tracking/secondspectrum.py | 6 +++++- kloppy/tests/test_secondspectrum.py | 4 ++++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/kloppy/domain/models/tracking.py b/kloppy/domain/models/tracking.py index 794e8305..725a050c 100644 --- a/kloppy/domain/models/tracking.py +++ b/kloppy/domain/models/tracking.py @@ -24,6 +24,7 @@ class Frame(DataRecord): players_data: Dict[Player, PlayerData] other_data: Dict[str, Any] ball_coordinates: Point3D + ball_speed: int @property def record_id(self) -> int: diff --git a/kloppy/domain/services/transformers/dataset.py b/kloppy/domain/services/transformers/dataset.py index b2001895..52899b1d 100644 --- a/kloppy/domain/services/transformers/dataset.py +++ b/kloppy/domain/services/transformers/dataset.py @@ -190,6 +190,7 @@ def __change_frame_coordinate_system(self, frame: Frame): ball_coordinates=self.__change_point_coordinate_system( frame.ball_coordinates ), + ball_speed=frame.ball_speed, players_data={ key: PlayerData( coordinates=self.__change_point_coordinate_system( diff --git a/kloppy/infra/serializers/tracking/secondspectrum.py b/kloppy/infra/serializers/tracking/secondspectrum.py index 57277c23..a25e4146 100644 --- a/kloppy/infra/serializers/tracking/secondspectrum.py +++ b/kloppy/infra/serializers/tracking/secondspectrum.py @@ -64,8 +64,10 @@ def _frame_from_framedata(cls, teams, period, frame_data): ball_coordinates = Point3D( float(ball_x), float(ball_y), float(ball_z) ) + ball_speed = frame_data["ball"]["speed"] else: ball_coordinates = None + ball_speed = None ball_state = BallState.ALIVE if frame_data["live"] else BallState.DEAD ball_owning_team = ( @@ -78,6 +80,7 @@ def _frame_from_framedata(cls, teams, period, frame_data): jersey_no = player_data["number"] x, y, _ = player_data["xyz"] + speed = player_data["speed"] player = team.get_player_by_jersey_number(jersey_no) if not player: @@ -89,13 +92,14 @@ def _frame_from_framedata(cls, teams, period, frame_data): team.players.append(player) players_data[player] = PlayerData( - coordinates=Point(float(x), float(y)) + coordinates=Point(float(x), float(y)), speed=speed ) return Frame( frame_id=frame_id, timestamp=frame_timestamp, ball_coordinates=ball_coordinates, + ball_speed=ball_speed, ball_state=ball_state, ball_owning_team=ball_owning_team, players_data=players_data, diff --git a/kloppy/tests/test_secondspectrum.py b/kloppy/tests/test_secondspectrum.py index 197cf0e2..f556bff9 100644 --- a/kloppy/tests/test_secondspectrum.py +++ b/kloppy/tests/test_secondspectrum.py @@ -106,6 +106,10 @@ def test_correct_normalized_deserialization( assert dataset.records[0].players_coordinates[home_player] == Point( x=0.4146981051733674, y=0.9144718866065965 ) + assert ( + dataset.records[0].players_data[home_player].speed + == 6.578958220040129 + ) # Check normalised pitch dimensions pitch_dimensions = dataset.metadata.pitch_dimensions From e73e732ddeb54fb73a695067264da9fc83288733 Mon Sep 17 00:00:00 2001 From: driesdeprest Date: Fri, 30 Jun 2023 11:12:21 +0200 Subject: [PATCH 2/3] Make ball_speed optional --- kloppy/domain/models/tracking.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kloppy/domain/models/tracking.py b/kloppy/domain/models/tracking.py index 725a050c..f545142b 100644 --- a/kloppy/domain/models/tracking.py +++ b/kloppy/domain/models/tracking.py @@ -24,7 +24,7 @@ class Frame(DataRecord): players_data: Dict[Player, PlayerData] other_data: Dict[str, Any] ball_coordinates: Point3D - ball_speed: int + ball_speed: Optional[int] = None @property def record_id(self) -> int: From eda364cd38391bb1fd8d3af61957e8a7eda5a07b Mon Sep 17 00:00:00 2001 From: driesdeprest Date: Fri, 30 Jun 2023 11:38:08 +0200 Subject: [PATCH 3/3] ball_speed float type --- kloppy/domain/models/tracking.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kloppy/domain/models/tracking.py b/kloppy/domain/models/tracking.py index f545142b..ca6603dd 100644 --- a/kloppy/domain/models/tracking.py +++ b/kloppy/domain/models/tracking.py @@ -24,7 +24,7 @@ class Frame(DataRecord): players_data: Dict[Player, PlayerData] other_data: Dict[str, Any] ball_coordinates: Point3D - ball_speed: Optional[int] = None + ball_speed: Optional[float] = None @property def record_id(self) -> int: