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 batch transform decorator #46

Merged
merged 3 commits into from
Dec 18, 2024
Merged

Add batch transform decorator #46

merged 3 commits into from
Dec 18, 2024

Conversation

ternaus
Copy link
Contributor

@ternaus ternaus commented Dec 18, 2024

Summary by Sourcery

Add a batch transform decorator to facilitate batch transformations for spatial and channel data, along with utility functions for reshaping and restoring data. Update project version and CI configuration, and introduce tests for the new functionality.

New Features:

  • Introduce a batch transform decorator to handle batch transformations for spatial and channel data.

Enhancements:

  • Add utility functions for reshaping and restoring 3D and batch data for spatial and channel transformations.

Build:

  • Update project version from 0.0.21 to 0.0.22 in pyproject.toml.

CI:

  • Upgrade Ruff linter version from v0.7.2 to v0.8.3 in pre-commit configuration.

Tests:

  • Add comprehensive tests for the new batch transform decorator, including tests for spatial and channel reshaping and restoration.

Copy link
Contributor

sourcery-ai bot commented Dec 18, 2024

Reviewer's Guide by Sourcery

This PR introduces a new batch transform decorator that enables efficient handling of batch transformations for image processing operations. The decorator supports different types of transformations (spatial, channel, and full) and handles various input tensor shapes including 2D and 3D data with optional batch and depth dimensions.

Class diagram for batch transform decorator

classDiagram
    class BatchTransformType {
        <<enumeration>>
        spatial
        channel
        full
    }

    class ShapeType {
        <<enumeration>>
        DHW
        DHWC
        NHW
        NHWC
        NDHW
        NDHWC
    }

    class Decorator {
        +batch_transform(transform_type: BatchTransformType, has_batch_dim: bool, has_depth_dim: bool, keep_depth_dim: bool): Callable[[F], F]
    }

    class ReshapeFunctions {
        +reshape_for_spatial(data: np.ndarray, has_batch_dim: bool, has_depth_dim: bool, keep_depth_dim: bool): tuple[np.ndarray, tuple[int, ...]]
        +reshape_for_channel(data: np.ndarray, has_batch_dim: bool, has_depth_dim: bool, keep_depth_dim: bool): tuple[np.ndarray, tuple[int, ...]]
    }

    class RestoreFunctions {
        +restore_from_spatial(data: np.ndarray, original_shape: tuple[int, ...], has_batch_dim: bool, has_depth_dim: bool, keep_depth_dim: bool): np.ndarray
        +restore_from_channel(data: np.ndarray, original_shape: tuple[int, ...], has_batch_dim: bool, has_depth_dim: bool, keep_depth_dim: bool): np.ndarray
    }

    Decorator --> BatchTransformType
    Decorator --> ReshapeFunctions
    Decorator --> RestoreFunctions
    ReshapeFunctions --> ShapeType
    RestoreFunctions --> ShapeType
Loading

File-Level Changes

Change Details Files
Implemented a new batch transform decorator with support for different transformation types
  • Added BatchTransformType enum with 'spatial', 'channel', and 'full' options
  • Created ShapeType enum to handle various tensor shapes (DHW, DHWC, NHW, NHWC, NDHW, NDHWC)
  • Implemented batch_transform decorator with configurable parameters for batch and depth dimensions
albucore/decorators.py
Added reshape and restore functions for different transformation types
  • Implemented reshape functions for spatial transformations (reshape_3d, reshape_batch, reshape_batch_3d)
  • Added reshape functions for channel transformations (reshape_3d_channel, reshape_batch_channel, reshape_batch_3d_channel)
  • Created corresponding restore functions to revert the transformations
  • Added support for keeping depth dimension during transformations
albucore/decorators.py
Added comprehensive test suite for the batch transform functionality
  • Created tests for spatial and channel transformations with various input shapes
  • Added tests for 3D volume handling and dimension preservation
  • Implemented roundtrip testing to ensure data integrity
  • Added edge case tests for empty arrays and non-contiguous inputs
tests/test_batch_decorator.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov-commenter
Copy link

codecov-commenter commented Dec 18, 2024

Codecov Report

Attention: Patch coverage is 90.96386% with 30 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@ef808a5). Learn more about missing BASE report.

Files with missing lines Patch % Lines
albucore/decorators.py 86.11% 30 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main      #46   +/-   ##
=======================================
  Coverage        ?   62.21%           
=======================================
  Files           ?       21           
  Lines           ?     2554           
  Branches        ?        0           
=======================================
  Hits            ?     1589           
  Misses          ?      965           
  Partials        ?        0           

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

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @ternaus - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@ternaus ternaus merged commit 8cf9f9e into main Dec 18, 2024
16 checks passed
@ternaus ternaus deleted the add_batch_transform branch December 18, 2024 22:25
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.

2 participants