-
Notifications
You must be signed in to change notification settings - Fork 11
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
Labels.split
and Labels.make_training_splits
#98
Changes from 11 commits
e179103
c505f03
0a76e7a
99fcb6b
c11b1c8
c33abdd
df501a3
465951b
af3e508
7969207
4a9108e
d272c54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -144,13 +144,13 @@ extra_css: | |||||
- css/mkdocstrings.css | ||||||
|
||||||
copyright: > | ||||||
Copyright © 2024 - 2024 Talmo Lab – | ||||||
Copyright © 2022 - 2024 Talmo Lab – | ||||||
<a href="#__consent">Change cookie settings</a> | ||||||
|
||||||
nav: | ||||||
- Overview: index.md | ||||||
- Changelog: https://github.com/talmolab/sleap-io/releases | ||||||
- Core API: | ||||||
- Data model: model.md | ||||||
- Data formats: formats.md | ||||||
- Model: model.md | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct the indentation of the - - Model: model.md
- - Formats: formats.md
+ - Model: model.md
+ - Formats: formats.md Committable suggestion
Suggested change
Toolsyamllint
|
||||||
- Formats: formats.md | ||||||
- Full API: reference/ |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -329,6 +329,9 @@ def embed_frames( | |||||
to_embed_by_video[video] = [] | ||||||
to_embed_by_video[video].append(frame_idx) | ||||||
|
||||||
for video in to_embed_by_video: | ||||||
to_embed_by_video[video] = np.unique(to_embed_by_video[video]) | ||||||
|
||||||
replaced_videos = {} | ||||||
for video, frame_inds in to_embed_by_video.items(): | ||||||
video_ind = labels.videos.index(video) | ||||||
|
@@ -348,25 +351,40 @@ def embed_frames( | |||||
|
||||||
|
||||||
def embed_videos( | ||||||
labels_path: str, labels: Labels, embed: str | list[tuple[Video, int]] | ||||||
labels_path: str, labels: Labels, embed: bool | str | list[tuple[Video, int]] | ||||||
): | ||||||
"""Embed videos in a SLEAP labels file. | ||||||
|
||||||
Args: | ||||||
labels_path: A string path to the SLEAP labels file to save. | ||||||
labels: A `Labels` object to save. | ||||||
embed: One of `"user"`, `"suggestions"`, `"user+suggestions"`, `"source"` or | ||||||
list of tuples of `(video, frame_idx)` specifying the frames to embed. If | ||||||
`"source"` is specified, no images will be embedded and the source video | ||||||
embed: Frames to embed in the saved labels file. One of `None`, `True`, | ||||||
`"all"`, `"user"`, `"suggestions"`, `"user+suggestions"`, `"source"` or list | ||||||
of tuples of `(video, frame_idx)`. | ||||||
|
||||||
If `None` is specified (the default) and the labels contains embedded | ||||||
frames, those embedded frames will be re-saved to the new file. | ||||||
|
||||||
If `True` or `"all"`, all labeled frames and suggested frames will be | ||||||
embedded. | ||||||
|
||||||
If `"source"` is specified, no images will be embedded and the source video | ||||||
will be restored if available. | ||||||
|
||||||
This argument is only valid for the SLP backend. | ||||||
""" | ||||||
if embed == True: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify the truth check for better readability and performance. - if embed == True:
+ if embed: Committable suggestion
Suggested change
ToolsRuff
|
||||||
embed = "all" | ||||||
if embed == "user": | ||||||
embed = [(lf.video, lf.frame_idx) for lf in labels.user_labeled_frames] | ||||||
elif embed == "suggestions": | ||||||
embed = [(sf.video, sf.frame_idx) for sf in labels.suggestions] | ||||||
elif embed == "user+suggestions": | ||||||
embed = [(lf.video, lf.frame_idx) for lf in labels.user_labeled_frames] | ||||||
embed += [(sf.video, sf.frame_idx) for sf in labels.suggestions] | ||||||
elif embed == "all": | ||||||
embed = [(lf.video, lf.frame_idx) for lf in labels] | ||||||
embed += [(sf.video, sf.frame_idx) for sf in labels.suggestions] | ||||||
elif embed == "source": | ||||||
embed = [] | ||||||
elif isinstance(embed, list): | ||||||
|
@@ -711,6 +729,13 @@ def write_metadata(labels_path: str, labels: Labels): | |||||
"negative_anchors": {}, | ||||||
"provenance": labels.provenance, | ||||||
} | ||||||
|
||||||
# Custom encoding. | ||||||
for k in md["provenance"]: | ||||||
if isinstance(md["provenance"][k], Path): | ||||||
# Path -> str | ||||||
md["provenance"][k] = md["provenance"][k].as_posix() | ||||||
|
||||||
with h5py.File(labels_path, "a") as f: | ||||||
grp = f.require_group("metadata") | ||||||
grp.attrs["format_id"] = 1.2 | ||||||
|
@@ -942,7 +967,9 @@ def write_lfs(labels_path: str, labels: Labels): | |||||
|
||||||
# Link instances based on from_predicted field. | ||||||
for instance_id, from_predicted in to_link: | ||||||
instances[instance_id][5] = inst_to_id[id(from_predicted)] | ||||||
# Source instance may be missing if predictions were removed from the labels, in | ||||||
# which case, remove the link. | ||||||
instances[instance_id][5] = inst_to_id.get(id(from_predicted), -1) | ||||||
|
||||||
# Create structured arrays. | ||||||
points = np.array([tuple(x) for x in points], dtype=point_dtype) | ||||||
|
@@ -1013,23 +1040,35 @@ def read_labels(labels_path: str) -> Labels: | |||||
suggestions=suggestions, | ||||||
provenance=provenance, | ||||||
) | ||||||
labels.provenance["filename"] = labels_path | ||||||
|
||||||
return labels | ||||||
|
||||||
|
||||||
def write_labels( | ||||||
labels_path: str, labels: Labels, embed: str | list[tuple[Video, int]] | None = None | ||||||
labels_path: str, | ||||||
labels: Labels, | ||||||
embed: bool | str | list[tuple[Video, int]] | None = None, | ||||||
): | ||||||
"""Write a SLEAP labels file. | ||||||
|
||||||
Args: | ||||||
labels_path: A string path to the SLEAP labels file to save. | ||||||
labels: A `Labels` object to save. | ||||||
embed: One of `"user"`, `"suggestions"`, `"user+suggestions"`, `"source"`, | ||||||
`None` or list of tuples of `(video, frame_idx)` specifying the frames to | ||||||
embed. If `"source"` is specified, no images will be embedded and the source | ||||||
video will be restored if available. If `None` is specified (the default), | ||||||
existing embedded images will be re-embedded. | ||||||
embed: Frames to embed in the saved labels file. One of `None`, `True`, | ||||||
`"all"`, `"user"`, `"suggestions"`, `"user+suggestions"`, `"source"` or list | ||||||
of tuples of `(video, frame_idx)`. | ||||||
|
||||||
If `None` is specified (the default) and the labels contains embedded | ||||||
frames, those embedded frames will be re-saved to the new file. | ||||||
|
||||||
If `True` or `"all"`, all labeled frames and suggested frames will be | ||||||
embedded. | ||||||
|
||||||
If `"source"` is specified, no images will be embedded and the source video | ||||||
will be restored if available. | ||||||
|
||||||
This argument is only valid for the SLP backend. | ||||||
""" | ||||||
if Path(labels_path).exists(): | ||||||
Path(labels_path).unlink() | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update method call to match the new method signature as per the PR changes.
Committable suggestion