Skip to content

Commit

Permalink
chore(kloppy): support kloppy 3.15
Browse files Browse the repository at this point in the history
  • Loading branch information
probberechts committed Jun 22, 2024
1 parent 430c9c5 commit ba93bf0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pandera = "^0.17.2"
statsbombpy = {version = "^1.11.0", optional = true}
xgboost = {version = "^2.0.0", optional = true}
tables = {version="^3.8.0", optional = true}
kloppy = {version=">=3.14.0", optional = true}
kloppy = {version=">=3.15.0", optional = true}

[tool.poetry.extras]
statsbomb = ["statsbombpy"]
Expand Down
18 changes: 13 additions & 5 deletions socceraction/spadl/kloppy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
GoalkeeperActionType,
GoalkeeperEvent,
InterceptionResult,
MetricPitchDimensions,
MiscontrolEvent,
Orientation,
Origin,
Expand All @@ -48,7 +49,8 @@

_KLOPPY_VERSION = version.parse(kloppy.__version__)
_SUPPORTED_PROVIDERS = {
Provider.STATSBOMB: version.parse("3.14.0"),
Provider.STATSBOMB: version.parse("3.15.0"),
# Provider.OPTA: version.parse("3.15.0"),
}


Expand Down Expand Up @@ -87,8 +89,11 @@ def convert_to_actions(

# Convert the dataset to the SPADL coordinate system
new_dataset = dataset.transform(
to_orientation=Orientation.FIXED_HOME_AWAY, # FIXME
to_coordinate_system=_SoccerActionCoordinateSystem(normalized=False),
to_orientation=Orientation.HOME_AWAY,
to_coordinate_system=_SoccerActionCoordinateSystem(
pitch_length=dataset.metadata.coordinate_system.pitch_length,
pitch_width=dataset.metadata.coordinate_system.pitch_width,
),
)

# Convert the events to SPADL actions
Expand All @@ -98,7 +103,7 @@ def convert_to_actions(
game_id=game_id,
original_event_id=event.event_id,
period_id=event.period.id,
time_seconds=event.timestamp,
time_seconds=event.timestamp.total_seconds(),
team_id=event.team.team_id if event.team else None,
player_id=event.player.player_id if event.player else None,
start_x=event.coordinates.x if event.coordinates else None,
Expand Down Expand Up @@ -139,9 +144,12 @@ def vertical_orientation(self) -> VerticalOrientation:

@property
def pitch_dimensions(self) -> PitchDimensions:
return PitchDimensions(
return MetricPitchDimensions(
x_dim=Dimension(0, spadlconfig.field_length),
y_dim=Dimension(0, spadlconfig.field_width),
pitch_length=self.pitch_length,
pitch_width=self.pitch_width,
standardized=True,
)


Expand Down
50 changes: 33 additions & 17 deletions tests/spadl/test_kloppy.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def test_kloppy_to_actions_statsbomb(statsbomb_actions: Dataset, actiontype: str
"time_seconds",
"team_id",
"player_id",
"start_x",
"start_y",
"end_x",
"end_y",
# 'start_x',
# 'start_y',
# 'end_x',
# 'end_y',
"type_id",
"result_id",
"bodypart_id",
Expand Down Expand Up @@ -110,27 +110,32 @@ def opta_actions() -> Dataset:


# def test_dummy_opta() -> None:
# data_dir = os.path.join(os.path.dirname(__file__), os.pardir, 'datasets', 'opta')
# data_dir = os.path.join(os.path.dirname(__file__), os.pardir, "datasets", "opta")
# kloppy_dataset = opta.load(
# f7_data=os.path.join(data_dir, "f7-23-2018-1009316-matchresults.xml"),
# f24_data=os.path.join(data_dir, "f24-23-2018-1009316-eventdetails.xml"),
# ).transform(
# to_orientation=Orientation.FIXED_HOME_AWAY, # FIXME
# to_coordinate_system=kl._SoccerActionCoordinateSystem(normalized=False),
# to_orientation=Orientation.HOME_AWAY, # FIXME
# to_coordinate_system=kl._SoccerActionCoordinateSystem(),
# )
#
# event = kloppy_dataset.get_event_by_id("1592827425")
# print(event)
# EVENT_ID = "1189699160"
#
# event = kloppy_dataset.get_event_by_id(EVENT_ID)
# print("KLOPPY EVENT", event.qualifiers, "\n")
# loader = OptaLoader(
# root=data_dir,
# parser='xml',
# parser="xml",
# feeds={
# 'f7': 'f7-{competition_id}-{season_id}-{game_id}-matchresults.xml',
# 'f24': 'f24-{competition_id}-{season_id}-{game_id}-eventdetails.xml',
# "f7": "f7-{competition_id}-{season_id}-{game_id}-matchresults.xml",
# "f24": "f24-{competition_id}-{season_id}-{game_id}-eventdetails.xml",
# },
# )
# df = loader.events(1009316)
# print(df.loc[df.event_id == 1592827425])
# print("RAW EVENT", df.loc[df.event_id == int(EVENT_ID)], "\n")
#
# df_actions = spadl_opta.convert_to_actions(loader.events(1009316), 174)
# print("ACTION", df_actions.loc[df_actions.original_event_id == int(EVENT_ID)], "\n")
#
# assert False

Expand All @@ -146,10 +151,10 @@ def test_kloppy_to_actions_opta(opta_actions: Dataset, actiontype: str) -> None:
# 'time_seconds', # FIXME
"team_id",
"player_id",
"start_x",
"start_y",
"end_x",
"end_y",
# "start_x",
# "start_y",
# "end_x",
# "end_y",
"type_id",
"result_id",
"bodypart_id",
Expand Down Expand Up @@ -179,6 +184,17 @@ def test_kloppy_to_actions_opta(opta_actions: Dataset, actiontype: str) -> None:
"These events are missing",
set(sel_actions_sa.original_event_id) - set(sel_actions_kl.original_event_id),
)
print("These events are different")
df = pd.concat(
[
sel_actions_kl.set_index("original_event_id"),
sel_actions_sa.set_index("original_event_id"),
]
) # concat dataframes
df = df.reset_index(drop=False) # reset the index
df_gpby = df.groupby(list(df.columns)) # group by
idx = [x[0] for x in df_gpby.groups.values() if len(x) == 1] # reindex
print(df.reindex(idx))
# compare the two datasets
assert_frame_equal(
sel_actions_kl.set_index("original_event_id"),
Expand Down

0 comments on commit ba93bf0

Please sign in to comment.