diff --git a/examples/datasets/metrica.py b/examples/datasets/metrica.py index 6854134b..e7f70f0e 100644 --- a/examples/datasets/metrica.py +++ b/examples/datasets/metrica.py @@ -25,6 +25,12 @@ def main(): data_frame = dataset.to_pandas() print(data_frame) + # Also load dataset in new metrica format + dataset = metrica.load_open_data(limit=10_000, match_id="3") + data_frame = dataset.to_pandas() + df = data_frame[data_frame.ball_x.notnull()] + print(df) + if __name__ == "__main__": main() diff --git a/kloppy/_providers/metrica.py b/kloppy/_providers/metrica.py index 46c2921f..65e1d583 100644 --- a/kloppy/_providers/metrica.py +++ b/kloppy/_providers/metrica.py @@ -84,20 +84,36 @@ def load_open_data( coordinates: Optional[str] = None, ) -> TrackingDataset: if match_id == "1" or match_id == 1: - home_data = "https://raw.githubusercontent.com/metrica-sports/sample-data/master/data/Sample_Game_1/Sample_Game_1_RawTrackingData_Home_Team.csv" - away_data = "https://raw.githubusercontent.com/metrica-sports/sample-data/master/data/Sample_Game_1/Sample_Game_1_RawTrackingData_Away_Team.csv" + return load_tracking_csv( + home_data="https://raw.githubusercontent.com/metrica-sports/sample-data/" + "master/data/Sample_Game_1/Sample_Game_1_RawTrackingData_Home_Team.csv", + away_data="https://raw.githubusercontent.com/metrica-sports/sample-data/" + "master/data/Sample_Game_1/Sample_Game_1_RawTrackingData_Away_Team.csv", + sample_rate=sample_rate, + limit=limit, + coordinates=coordinates, + ) elif match_id == "2" or match_id == 2: - home_data = "https://raw.githubusercontent.com/metrica-sports/sample-data/master/data/Sample_Game_2/Sample_Game_2_RawTrackingData_Home_Team.csv" - away_data = "https://raw.githubusercontent.com/metrica-sports/sample-data/master/data/Sample_Game_2/Sample_Game_2_RawTrackingData_Away_Team.csv" + return load_tracking_csv( + home_data="https://raw.githubusercontent.com/metrica-sports/sample-data/" + "master/data/Sample_Game_2/Sample_Game_2_RawTrackingData_Home_Team.csv", + away_data="https://raw.githubusercontent.com/metrica-sports/sample-data/" + "master/data/Sample_Game_2/Sample_Game_2_RawTrackingData_Away_Team.csv", + sample_rate=sample_rate, + limit=limit, + coordinates=coordinates, + ) + elif match_id == "3" or match_id == 3: + return load_tracking_epts( + meta_data="https://raw.githubusercontent.com/metrica-sports/sample-data/" + "master/data/Sample_Game_3/Sample_Game_3_metadata.xml", + raw_data="https://raw.githubusercontent.com/metrica-sports/sample-data/" + "master/data/Sample_Game_3/Sample_Game_3_tracking.txt", + sample_rate=sample_rate, + limit=limit, + coordinates=coordinates, + ) else: raise KloppyError( f"Don't know where to fetch Metrica open data for {match_id}" ) - - return load_tracking_csv( - home_data=home_data, - away_data=away_data, - sample_rate=sample_rate, - limit=limit, - coordinates=coordinates, - ) diff --git a/kloppy/domain/models/tracking.py b/kloppy/domain/models/tracking.py index 125da8b1..7f45ca1b 100644 --- a/kloppy/domain/models/tracking.py +++ b/kloppy/domain/models/tracking.py @@ -81,15 +81,20 @@ def record_converter(frame: Frame) -> Dict: ball_y=frame.ball_coordinates.y if frame.ball_coordinates else None, - ball_z=frame.ball_coordinates.z + ball_z=getattr(frame.ball_coordinates, "z", None) if frame.ball_coordinates else None, ) for player, player_data in frame.players_data.items(): + row.update( { - f"{player.player_id}_x": player_data.coordinates.x, - f"{player.player_id}_y": player_data.coordinates.y, + f"{player.player_id}_x": player_data.coordinates.x + if player_data.coordinates + else None, + f"{player.player_id}_y": player_data.coordinates.y + if player_data.coordinates + else None, f"{player.player_id}_d": player_data.distance, f"{player.player_id}_s": player_data.speed, }