-
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
Add high level APIs #80
Conversation
WalkthroughThe latest update to the Changes
Recent Review DetailsConfiguration used: CodeRabbit UI Files selected for processing (2)
Files skipped from review as they are similar to previous changes (1)
Additional comments not posted (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #80 +/- ##
==========================================
+ Coverage 94.79% 95.16% +0.37%
==========================================
Files 13 13
Lines 1498 1551 +53
==========================================
+ Hits 1420 1476 +56
+ Misses 78 75 -3 ☔ View full report in Codecov by Sentry. |
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.
Actionable comments posted: 1
Actionable comments outside the diff hunks (1)
sleap_io/model/video.py (1)
Line range hint
97-97
: Avoid using a bareexcept
statement. It's better to specify the exceptions you expect to handle to avoid masking other unexpected issues.- except: + except ExpectedExceptionType:
save_labelstudio, | ||
load_jabs, | ||
save_jabs, | ||
load_video, | ||
load_file, | ||
) |
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.
📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [7-28]
Several imports in this file are marked as unused. If these are not intended for external use, consider adding them to the __all__
list to explicitly define what is exported by this module, or remove them if they are not needed.
- from sleap_io.model.skeleton import Node, Edge, Skeleton, Symmetry
- from sleap_io.model.video import Video
- from sleap_io.model.instance import (
- Point,
- PredictedPoint,
- Track,
- Instance,
- PredictedInstance,
- )
- from sleap_io.model.labeled_frame import LabeledFrame
- from sleap_io.model.labels import Labels
- from sleap_io.io.main import (
- load_slp,
- save_slp,
- load_nwb,
- save_nwb,
- load_labelstudio,
- save_labelstudio,
- load_jabs,
- save_jabs,
- )
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.
Actionable comments posted: 2
def load_file( | ||
filename: str | Path, format: Optional[str] = None, **kwargs | ||
) -> Union[Labels, Video]: | ||
"""Load a file and return the appropriate object. | ||
|
||
Args: | ||
filename: Path to a file. | ||
format: Optional format to load as. If not provided, will be inferred from the | ||
file extension. Available formats are: "slp", "nwb", "labelstudio", "jabs" | ||
and "video". | ||
|
||
Returns: | ||
A `Labels` or `Video` object. | ||
""" | ||
if isinstance(filename, Path): | ||
filename = str(filename) | ||
|
||
if format is None: | ||
if filename.endswith(".slp"): | ||
format = "slp" | ||
elif filename.endswith(".nwb"): | ||
format = "nwb" | ||
elif filename.endswith(".json"): | ||
format = "json" | ||
elif filename.endswith(".h5"): | ||
format = "jabs" | ||
else: | ||
for vid_ext in Video.EXTS: | ||
if filename.endswith(vid_ext): | ||
format = "video" | ||
break | ||
if format is None: | ||
raise ValueError(f"Could not infer format from filename: '{filename}'.") | ||
|
||
if filename.endswith(".slp"): | ||
return load_slp(filename, **kwargs) | ||
elif filename.endswith(".nwb"): | ||
return load_nwb(filename, **kwargs) | ||
elif filename.endswith(".json"): | ||
return load_labelstudio(filename, **kwargs) | ||
elif filename.endswith(".h5"): | ||
return load_jabs(filename, **kwargs) | ||
elif format == "video": | ||
return load_video(filename, **kwargs) | ||
|
||
|
||
def save_file( | ||
labels: Labels, filename: str | Path, format: Optional[str] = None, **kwargs | ||
): | ||
"""Save a file based on the extension. | ||
|
||
Args: | ||
labels: A SLEAP `Labels` object (see `load_slp`). | ||
filename: Path to save labels to. | ||
format: Optional format to save as. If not provided, will be inferred from the | ||
file extension. Available formats are: "slp", "nwb", "labelstudio" and | ||
"jabs". | ||
""" | ||
if isinstance(filename, Path): | ||
filename = str(filename) | ||
|
||
if format is None: | ||
if filename.endswith(".slp"): | ||
format = "slp" | ||
elif filename.endswith(".nwb"): | ||
format = "nwb" | ||
elif filename.endswith(".json"): | ||
format = "labelstudio" | ||
elif "pose_version" in kwargs: | ||
format = "jabs" | ||
|
||
if format == "slp": | ||
save_slp(labels, filename, **kwargs) | ||
elif format == "nwb": | ||
save_nwb(labels, filename, **kwargs) | ||
elif format == "labelstudio": | ||
save_labelstudio(labels, filename, **kwargs) | ||
elif format == "jabs": | ||
pose_version = kwargs.pop("pose_version", 5) | ||
save_jabs(labels, pose_version, filename, **kwargs) | ||
else: | ||
raise ValueError(f"Unknown format '{format}' for filename: '{filename}'.") |
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.
Improve the error message in load_file
to list supported formats.
- raise ValueError(f"Could not infer format from filename: '{filename}'.")
+ raise ValueError(f"Could not infer format from filename: '{filename}'. Supported formats are: 'slp', 'nwb', 'labelstudio', 'jabs', 'video'.")
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.
def load_file( | |
filename: str | Path, format: Optional[str] = None, **kwargs | |
) -> Union[Labels, Video]: | |
"""Load a file and return the appropriate object. | |
Args: | |
filename: Path to a file. | |
format: Optional format to load as. If not provided, will be inferred from the | |
file extension. Available formats are: "slp", "nwb", "labelstudio", "jabs" | |
and "video". | |
Returns: | |
A `Labels` or `Video` object. | |
""" | |
if isinstance(filename, Path): | |
filename = str(filename) | |
if format is None: | |
if filename.endswith(".slp"): | |
format = "slp" | |
elif filename.endswith(".nwb"): | |
format = "nwb" | |
elif filename.endswith(".json"): | |
format = "json" | |
elif filename.endswith(".h5"): | |
format = "jabs" | |
else: | |
for vid_ext in Video.EXTS: | |
if filename.endswith(vid_ext): | |
format = "video" | |
break | |
if format is None: | |
raise ValueError(f"Could not infer format from filename: '{filename}'.") | |
if filename.endswith(".slp"): | |
return load_slp(filename, **kwargs) | |
elif filename.endswith(".nwb"): | |
return load_nwb(filename, **kwargs) | |
elif filename.endswith(".json"): | |
return load_labelstudio(filename, **kwargs) | |
elif filename.endswith(".h5"): | |
return load_jabs(filename, **kwargs) | |
elif format == "video": | |
return load_video(filename, **kwargs) | |
def save_file( | |
labels: Labels, filename: str | Path, format: Optional[str] = None, **kwargs | |
): | |
"""Save a file based on the extension. | |
Args: | |
labels: A SLEAP `Labels` object (see `load_slp`). | |
filename: Path to save labels to. | |
format: Optional format to save as. If not provided, will be inferred from the | |
file extension. Available formats are: "slp", "nwb", "labelstudio" and | |
"jabs". | |
""" | |
if isinstance(filename, Path): | |
filename = str(filename) | |
if format is None: | |
if filename.endswith(".slp"): | |
format = "slp" | |
elif filename.endswith(".nwb"): | |
format = "nwb" | |
elif filename.endswith(".json"): | |
format = "labelstudio" | |
elif "pose_version" in kwargs: | |
format = "jabs" | |
if format == "slp": | |
save_slp(labels, filename, **kwargs) | |
elif format == "nwb": | |
save_nwb(labels, filename, **kwargs) | |
elif format == "labelstudio": | |
save_labelstudio(labels, filename, **kwargs) | |
elif format == "jabs": | |
pose_version = kwargs.pop("pose_version", 5) | |
save_jabs(labels, pose_version, filename, **kwargs) | |
else: | |
raise ValueError(f"Unknown format '{format}' for filename: '{filename}'.") | |
def load_file( | |
filename: str | Path, format: Optional[str] = None, **kwargs | |
) -> Union[Labels, Video]: | |
"""Load a file and return the appropriate object. | |
Args: | |
filename: Path to a file. | |
format: Optional format to load as. If not provided, will be inferred from the | |
file extension. Available formats are: "slp", "nwb", "labelstudio", "jabs" | |
and "video". | |
Returns: | |
A `Labels` or `Video` object. | |
""" | |
if isinstance(filename, Path): | |
filename = str(filename) | |
if format is None: | |
if filename.endswith(".slp"): | |
format = "slp" | |
elif filename.endswith(".nwb"): | |
format = "nwb" | |
elif filename.endswith(".json"): | |
format = "json" | |
elif filename.endswith(".h5"): | |
format = "jabs" | |
else: | |
for vid_ext in Video.EXTS: | |
if filename.endswith(vid_ext): | |
format = "video" | |
break | |
if format is None: | |
raise ValueError(f"Could not infer format from filename: '{filename}'. Supported formats are: 'slp', 'nwb', 'labelstudio', 'jabs', 'video'.") | |
if filename.endswith(".slp"): | |
return load_slp(filename, **kwargs) | |
elif filename.endswith(".nwb"): | |
return load_nwb(filename, **kwargs) | |
elif filename.endswith(".json"): | |
return load_labelstudio(filename, **kwargs) | |
elif filename.endswith(".h5"): | |
return load_jabs(filename, **kwargs) | |
elif format == "video": | |
return load_video(filename, **kwargs) | |
def save_file( | |
labels: Labels, filename: str | Path, format: Optional[str] = None, **kwargs | |
): | |
"""Save a file based on the extension. | |
Args: | |
labels: A SLEAP `Labels` object (see `load_slp`). | |
filename: Path to save labels to. | |
format: Optional format to save as. If not provided, will be inferred from the | |
file extension. Available formats are: "slp", "nwb", "labelstudio" and | |
"jabs". | |
""" | |
if isinstance(filename, Path): | |
filename = str(filename) | |
if format is None: | |
if filename.endswith(".slp"): | |
format = "slp" | |
elif filename.endswith(".nwb"): | |
format = "nwb" | |
elif filename.endswith(".json"): | |
format = "labelstudio" | |
elif "pose_version" in kwargs: | |
format = "jabs" | |
if format == "slp": | |
save_slp(labels, filename, **kwargs) | |
elif format == "nwb": | |
save_nwb(labels, filename, **kwargs) | |
elif format == "labelstudio": | |
save_labelstudio(labels, filename, **kwargs) | |
elif format == "jabs": | |
pose_version = kwargs.pop("pose_version", 5) | |
save_jabs(labels, pose_version, filename, **kwargs) | |
else: | |
raise ValueError(f"Unknown format '{format}' for filename: '{filename}'.") |
def save_file( | ||
labels: Labels, filename: str | Path, format: Optional[str] = None, **kwargs | ||
): | ||
"""Save a file based on the extension. | ||
|
||
Args: | ||
labels: A SLEAP `Labels` object (see `load_slp`). | ||
filename: Path to save labels to. | ||
format: Optional format to save as. If not provided, will be inferred from the | ||
file extension. Available formats are: "slp", "nwb", "labelstudio" and | ||
"jabs". | ||
""" | ||
if isinstance(filename, Path): | ||
filename = str(filename) | ||
|
||
if format is None: | ||
if filename.endswith(".slp"): | ||
format = "slp" | ||
elif filename.endswith(".nwb"): | ||
format = "nwb" | ||
elif filename.endswith(".json"): | ||
format = "labelstudio" | ||
elif "pose_version" in kwargs: | ||
format = "jabs" | ||
|
||
if format == "slp": | ||
save_slp(labels, filename, **kwargs) | ||
elif format == "nwb": | ||
save_nwb(labels, filename, **kwargs) | ||
elif format == "labelstudio": | ||
save_labelstudio(labels, filename, **kwargs) | ||
elif format == "jabs": | ||
pose_version = kwargs.pop("pose_version", 5) | ||
save_jabs(labels, pose_version, filename, **kwargs) | ||
else: | ||
raise ValueError(f"Unknown format '{format}' for filename: '{filename}'.") |
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.
Refactor save_file
to handle pose_version
more consistently.
- elif "pose_version" in kwargs:
- format = "jabs"
+ # Move pose_version handling to the jabs-specific block
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.
def save_file( | |
labels: Labels, filename: str | Path, format: Optional[str] = None, **kwargs | |
): | |
"""Save a file based on the extension. | |
Args: | |
labels: A SLEAP `Labels` object (see `load_slp`). | |
filename: Path to save labels to. | |
format: Optional format to save as. If not provided, will be inferred from the | |
file extension. Available formats are: "slp", "nwb", "labelstudio" and | |
"jabs". | |
""" | |
if isinstance(filename, Path): | |
filename = str(filename) | |
if format is None: | |
if filename.endswith(".slp"): | |
format = "slp" | |
elif filename.endswith(".nwb"): | |
format = "nwb" | |
elif filename.endswith(".json"): | |
format = "labelstudio" | |
elif "pose_version" in kwargs: | |
format = "jabs" | |
if format == "slp": | |
save_slp(labels, filename, **kwargs) | |
elif format == "nwb": | |
save_nwb(labels, filename, **kwargs) | |
elif format == "labelstudio": | |
save_labelstudio(labels, filename, **kwargs) | |
elif format == "jabs": | |
pose_version = kwargs.pop("pose_version", 5) | |
save_jabs(labels, pose_version, filename, **kwargs) | |
else: | |
raise ValueError(f"Unknown format '{format}' for filename: '{filename}'.") | |
def save_file( | |
labels: Labels, filename: str | Path, format: Optional[str] = None, **kwargs | |
): | |
"""Save a file based on the extension. | |
Args: | |
labels: A SLEAP `Labels` object (see `load_slp`). | |
filename: Path to save labels to. | |
format: Optional format to save as. If not provided, will be inferred from the | |
file extension. Available formats are: "slp", "nwb", "labelstudio" and | |
"jabs". | |
""" | |
if isinstance(filename, Path): | |
filename = str(filename) | |
if format is None: | |
if filename.endswith(".slp"): | |
format = "slp" | |
elif filename.endswith(".nwb"): | |
format = "nwb" | |
elif filename.endswith(".json"): | |
format = "labelstudio" | |
# Move pose_version handling to the jabs-specific block | |
if format == "slp": | |
save_slp(labels, filename, **kwargs) | |
elif format == "nwb": | |
save_nwb(labels, filename, **kwargs) | |
elif format == "labelstudio": | |
save_labelstudio(labels, filename, **kwargs) | |
elif format == "jabs": | |
pose_version = kwargs.pop("pose_version", 5) | |
save_jabs(labels, pose_version, filename, **kwargs) | |
else: | |
raise ValueError(f"Unknown format '{format}' for filename: '{filename}'.") |
load_video
andload_file
high level APIsSummary by CodeRabbit