Skip to content

Commit

Permalink
Merge pull request #151 from koenvo/bugfix/metrica-game-3
Browse files Browse the repository at this point in the history
Bugfix/metrica game 3
  • Loading branch information
koenvo authored Aug 24, 2022
2 parents 2641d41 + 8e6855f commit f1af145
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
6 changes: 6 additions & 0 deletions examples/datasets/metrica.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
40 changes: 28 additions & 12 deletions kloppy/_providers/metrica.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
11 changes: 8 additions & 3 deletions kloppy/domain/models/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit f1af145

Please sign in to comment.