Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/skillcorner timestamp #338

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions kloppy/infra/serializers/tracking/skillcorner.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,24 @@ def _get_skillcorner_attacking_directions(cls, frames, periods):

return attacking_directions

def __load_json(self, file):
@staticmethod
def __replace_timestamp(obj):
if "timestamp" in obj:
obj["time"] = obj.pop("timestamp")
return obj

def __load_json_raw(self, file):
JanVanHaaren marked this conversation as resolved.
Show resolved Hide resolved
if Path(file.name).suffix == ".jsonl":
data = []
for line in file:
obj = json.loads(line)
# for each line rename timestamp to time to make it compatible with existing loader
if "timestamp" in obj:
obj["time"] = obj.pop("timestamp")
data.append(obj)
data.append(self.__replace_timestamp(obj))
return data
else:
return json.load(file)
data = json.load(file)
for line in data:
line = self.__replace_timestamp(line)
return data

@classmethod
def __get_periods(cls, tracking):
Expand Down Expand Up @@ -288,8 +294,8 @@ def __create_anon_player(cls, teams, frame_record):
)

def deserialize(self, inputs: SkillCornerInputs) -> TrackingDataset:
metadata = self.__load_json(inputs.meta_data)
raw_data = self.__load_json(inputs.raw_data)
metadata = json.load(inputs.meta_data)
raw_data = self.__load_json_raw(inputs.raw_data)

with performance_logging("Loading metadata", logger=logger):
periods = self.__get_periods(raw_data)
Expand Down

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions kloppy/tests/test_skillcorner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ def meta_data(self, base_dir) -> str:
def raw_data(self, base_dir) -> str:
return base_dir / "files/skillcorner_structured_data.json"

@pytest.fixture
def raw_data_timestamp(self, base_dir) -> str:
return base_dir / "files/skillcorner_structured_data_timestamp.json"

def test_correct_deserialization_timestamp(
self, raw_data_timestamp: Path, meta_data: Path
):
skillcorner.load(
meta_data=meta_data,
raw_data=raw_data_timestamp,
coordinates="skillcorner",
include_empty_frames=True,
)

def test_correct_deserialization(self, raw_data: Path, meta_data: Path):
dataset = skillcorner.load(
meta_data=meta_data,
Expand Down
Loading