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

Add better support for exporting and loading RGB videos from .pkg.slp files #125

Merged
merged 7 commits into from
Oct 3, 2024

Conversation

talmo
Copy link
Contributor

@talmo talmo commented Oct 2, 2024

  • greyscale attribute is now stored in the backend metadata
  • Video backend creation now uses the greyscale setting in the backend metadata if available
  • Better handling of grayscale serialization in the imageio backend during export

Summary by CodeRabbit

  • New Features

    • Enhanced video handling with improved grayscale detection and metadata serialization.
    • Robust frame processing for embedding videos.
  • Bug Fixes

    • Adjusted error handling in video creation to prevent unhandled states.
  • Tests

    • Added new tests for verifying RGB and grayscale video data handling.
    • Improved code readability through indentation corrections in existing tests.

Copy link
Contributor

coderabbitai bot commented Oct 2, 2024

Warning

Rate limit exceeded

@talmo has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 11 minutes and 17 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Files that changed from the base of the PR and between 3b759b4 and 8518843.

Walkthrough

The pull request introduces several updates to the sleap_io/io/slp.py file, enhancing video handling and metadata management. Notable changes include the implementation of grayscale detection in the make_video function, updates to video metadata serialization to accurately reflect the grayscale attribute, and improvements in frame processing within the embed_video function. Additionally, error handling has been refined, and minor formatting adjustments have been made. New test functions for RGB and grayscale video handling were added in tests/io/test_slp.py, along with corrections to existing test function indentation.

Changes

File Change Summary
sleap_io/io/slp.py - Enhanced make_video to include grayscale detection.
- Updated video_to_dict for grayscale serialization.
- Improved embed_video for robust frame processing.
- Adjusted error handling in make_video.
- Minor code formatting changes.
sleap_io/model/video.py - Updated grayscale property logic for better accuracy.
- Refined open method for adaptive grayscale handling.
tests/io/test_slp.py - Added test_embed_rgb to verify RGB video handling.
- Added test_embed_grayscale for grayscale video verification.
- Corrected indentation in test_write_videos.

Possibly related PRs

  • Grayscale property passthrough #99: The changes in this PR enhance the handling of the grayscale property in the Video class, which is directly related to the grayscale handling introduced in the main PR's make_video and video_to_dict functions.
  • Safer video loading from SLP #119: This PR modifies the make_video function to include an open_backend parameter, which aligns with the changes made in the main PR regarding video handling and metadata management.
  • Fix split calculation and allow for not embedding #120: The updates to the write_labels function in this PR refine the logic for embedding videos, which is relevant to the changes made in the main PR regarding video metadata serialization and embedding.
  • Fix embedded video lookup #122: This PR includes modifications to the make_video function that streamline video handling, which is closely related to the enhancements made in the main PR regarding video processing and metadata management.

Poem

🐰 In the meadow where videos play,
Grayscale whispers in a new way.
Frames are snug, no more a fright,
With tests to ensure they shine bright.
Hooray for changes, let’s hop and cheer,
For video magic is finally here! 🎥✨


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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

🧹 Outside diff range and nitpick comments (1)
tests/io/test_slp.py (1)

359-370: LGTM! Consider enhancing the test coverage.

The new test function test_embed_rgb effectively verifies the basic functionality of embedding RGB video data. It aligns well with the PR objective of improving support for RGB videos. However, consider the following enhancements to make the test more robust:

  1. Verify that the actual RGB data is preserved correctly after embedding and reading back.
  2. Test different embedding options (e.g., 'all', 'suggestions') to ensure they work correctly with RGB data.
  3. Check if the grayscale attribute is preserved after reading the embedded data.
  4. Add assertions to verify that the video backend type changes as expected (from MediaVideo to HDF5Video).

Here's a suggested enhancement to the test function:

def test_embed_rgb(tmpdir, slp_real_data):
    base_labels = read_labels(slp_real_data)
    base_labels.video.grayscale = False
    assert base_labels.video.shape == (1100, 384, 384, 3)
    assert base_labels.video[0].shape == (384, 384, 3)
    
    # Store original data for comparison
    original_frame = base_labels.video[0].copy()

    for embed_option in ['user', 'all', 'suggestions']:
        labels_path = str(tmpdir / f"labels_{embed_option}.pkg.slp")
        write_labels(labels_path, base_labels, embed=embed_option)
        labels = read_labels(labels_path)
        
        assert labels.video[0].shape == (384, 384, 3)
        assert labels.video.grayscale == False
        assert type(labels.video.backend) == HDF5Video
        
        # Check if the RGB data is preserved
        np.testing.assert_array_equal(labels.video[0], original_frame)

    # Test source embedding
    source_labels_path = str(tmpdir / "labels_source.slp")
    write_labels(source_labels_path, labels, embed='source')
    source_labels = read_labels(source_labels_path)
    assert type(source_labels.video.backend) == MediaVideo

This enhanced version tests multiple embedding options, verifies data preservation, and checks backend types.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7024b54 and 6e2f4cd.

📒 Files selected for processing (2)
  • sleap_io/io/slp.py (3 hunks)
  • tests/io/test_slp.py (1 hunks)
🔇 Additional comments (1)
sleap_io/io/slp.py (1)

273-274: Ensure proper handling of single-channel frames before saving

The code squeezes the last axis of the frame if frame.shape[-1] == 1 to handle single-channel images appropriately. Verify that this operation behaves correctly for all expected frame shapes and does not inadvertently alter multi-channel images.

Run the following script to confirm that frames with different shapes are handled correctly:

✅ Verification successful

Proper handling of single-channel frames confirmed

The squeeze operation is correctly applied only to single-channel frames (frame.shape[-1] == 1), ensuring that multi-channel images are not inadvertently altered.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check frame shapes after squeezing operation.

# Test: List unique frame shapes in the dataset. Expect: Correct shapes without unintended squeezing.
ast-grep --lang python --pattern $'for frame_idx in $_:\n    frame = video[$_]\n    if frame.shape[-1] == 1:\n        frame = frame.squeeze(axis=-1)\n    $$$\n' \
| xargs -I{} echo "Found squeezing operation in {}"

Length of output: 210


Script:

#!/bin/bash
# Description: Search for squeeze operations in sleap_io/io/slp.py and ensure proper handling.

# Search for lines where squeeze is called with axis=-1
rg 'frame\.squeeze\s*\(\s*axis\s*=\s*-1\s*\)' sleap_io/io/slp.py -C 2

Length of output: 304

sleap_io/io/slp.py Outdated Show resolved Hide resolved
sleap_io/io/slp.py Show resolved Hide resolved
Copy link

codecov bot commented Oct 2, 2024

Codecov Report

Attention: Patch coverage is 93.75000% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.48%. Comparing base (7024b54) to head (8518843).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
sleap_io/io/slp.py 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #125      +/-   ##
==========================================
+ Coverage   96.21%   96.48%   +0.26%     
==========================================
  Files          15       15              
  Lines        2033     2046      +13     
==========================================
+ Hits         1956     1974      +18     
+ Misses         77       72       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

🧹 Outside diff range and nitpick comments (1)
sleap_io/model/video.py (1)

Line range hint 9-9: Remove duplicate import of Optional from typing module

The Optional type is imported twice in the import statement. This is redundant and can be removed to clean up the code.

Apply this diff to remove the duplicate import:

-from typing import Tuple, Optional, Optional
+from typing import Tuple, Optional
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6e2f4cd and 3b759b4.

📒 Files selected for processing (3)
  • sleap_io/io/slp.py (3 hunks)
  • sleap_io/model/video.py (2 hunks)
  • tests/io/test_slp.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • sleap_io/io/slp.py
  • tests/io/test_slp.py

Comment on lines 146 to 151
grayscale = None
if "grayscale" in self.backend_metadata:
grayscale = self.backend_metadata["grayscale"]
elif "shape" in self.backend_metadata:
grayscale = self.backend_metadata["shape"][-1] == 1
return grayscale
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor grayscale determination logic into a helper method

The logic for determining grayscale from backend_metadata is duplicated in both the grayscale property and the open method. To adhere to the DRY (Don't Repeat Yourself) principle and improve maintainability, consider refactoring this logic into a private helper method.

Apply this diff to lines 146-151 to use the helper method:

            else:
                grayscale = None
-               if "grayscale" in self.backend_metadata:
-                   grayscale = self.backend_metadata["grayscale"]
-               elif "shape" in self.backend_metadata:
-                   grayscale = self.backend_metadata["shape"][-1] == 1
+               grayscale = self._determine_grayscale_from_metadata()
                return grayscale

Add the following helper method to the class:

    def _determine_grayscale_from_metadata(self) -> Optional[bool]:
        """Helper method to determine grayscale from backend_metadata."""
        if "grayscale" in self.backend_metadata:
            return self.backend_metadata["grayscale"]
        elif "shape" in self.backend_metadata:
            return self.backend_metadata["shape"][-1] == 1
        return None

Comment on lines +279 to +283
if grayscale is None:
if "grayscale" in self.backend_metadata:
grayscale = self.backend_metadata["grayscale"]
elif "shape" in self.backend_metadata:
grayscale = self.backend_metadata["shape"][-1] == 1
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor grayscale determination logic into a helper method

As previously mentioned, the logic for determining grayscale from backend_metadata is duplicated. Reusing the helper method improves code readability and maintainability.

Apply this diff to lines 279-283 to use the helper method:

            else:
                if grayscale is None:
-                   if "grayscale" in self.backend_metadata:
-                       grayscale = self.backend_metadata["grayscale"]
-                   elif "shape" in self.backend_metadata:
-                       grayscale = self.backend_metadata["shape"][-1] == 1
+                   grayscale = self._determine_grayscale_from_metadata()

Committable suggestion was skipped due to low confidence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant