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 suggestions deserialization #95

Merged
merged 2 commits into from
May 22, 2024
Merged
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
8 changes: 5 additions & 3 deletions sleap_io/io/slp.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,11 @@ def read_suggestions(labels_path: str, videos: list[Video]) -> list[SuggestionFr
Returns:
A list of `SuggestionFrame` objects.
"""
suggestions = [
json.loads(x) for x in read_hdf5_dataset(labels_path, "suggestions_json")
]
try:
suggestions = read_hdf5_dataset(labels_path, "suggestions_json")
except KeyError:
return []
suggestions = [json.loads(x) for x in suggestions]
suggestions_objects = []
for suggestion in suggestions:
suggestions_objects.append(
Expand Down
2 changes: 1 addition & 1 deletion sleap_io/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

# Define package version.
# This is read dynamically by setuptools in pyproject.toml to determine the release version.
__version__ = "0.1.1"
__version__ = "0.1.2"
20 changes: 20 additions & 0 deletions tests/io/test_slp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
PredictedPoint,
PredictedInstance,
Labels,
SuggestionFrame,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unused import SuggestionFrame.

-    SuggestionFrame,

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
SuggestionFrame,

)
from sleap_io.io.slp import (
read_videos,
Expand All @@ -29,6 +30,8 @@
write_lfs,
read_labels,
write_labels,
read_suggestions,
write_suggestions,
)
from sleap_io.io.utils import read_hdf5_dataset
import numpy as np
Expand Down Expand Up @@ -237,3 +240,20 @@ def test_slp_imgvideo(tmpdir, slp_imgvideo):
assert type(videos[0].backend) == ImageVideo
assert len(videos[0].filename) == 2
assert videos[0].shape is None


def test_suggestions(tmpdir):
labels = Labels()
labels.videos.append(Video.from_filename("fake.mp4"))
labels.suggestions.append(SuggestionFrame(video=labels.video, frame_idx=0))

write_suggestions(tmpdir / "test.slp", labels.suggestions, labels.videos)
loaded_suggestions = read_suggestions(tmpdir / "test.slp", labels.videos)
assert len(loaded_suggestions) == 1
assert loaded_suggestions[0].video.filename == "fake.mp4"
assert loaded_suggestions[0].frame_idx == 0

# Handle missing suggestions dataset
write_videos(tmpdir / "test2.slp", labels.videos)
loaded_suggestions = read_suggestions(tmpdir / "test2.slp", labels.videos)
assert len(loaded_suggestions) == 0
Loading