Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Conversation

trylinka
Copy link
Contributor

@trylinka trylinka commented Jun 17, 2025

Summary by Sourcery

Expand noise augmentation support by introducing batch and volume apply methods, improving metadata handling in noise transforms, ensuring correct noise broadcasting in the functional API, and reinforcing reliability with extensive generate_noise tests.

New Features:

  • Add apply_to_images, apply_to_volume, and apply_to_volumes methods to the Gaussian noise transform to support batch and volumetric inputs
  • Enhance add_noise functional to broadcast noise maps to match input image shapes

Enhancements:

  • Refactor noise transforms to use get_image_data metadata for shape and dtype extraction
  • Simplify SaltAndPepper transform to use provided shape parameter and update its docstring

Build:

  • Bump albucore dependency to 0.0.28

Tests:

  • Add comprehensive tests for generate_noise covering multiple noise types, spatial modes, dimensionalities, approximation values, value range checks, and expected failure scenarios

Copy link
Contributor

sourcery-ai bot commented Jun 17, 2025

Reviewer's Guide

This PR refactors parameter handling and extends the Gaussian noise transform to support image batches and volumes, fixes noise broadcasting in add_noise, upgrades the albucore dependency, and dramatically expands generate_noise test coverage for various noise types, spatial modes, shapes, and failure scenarios.

Entity relationship diagram for noise map application to images and volumes

erDiagram
    IMAGE ||--o{ NOISE_MAP : "applies"
    IMAGES ||--o{ NOISE_MAP : "applies"
    VOLUME ||--o{ NOISE_MAP : "applies"
    VOLUMES ||--o{ NOISE_MAP : "applies"
Loading

Class diagram for extended Gaussian noise transform methods

classDiagram
    class GaussianNoiseTransform {
        +apply(img, noise_map, **params)
        +apply_to_images(images, noise_map, **params)
        +apply_to_volume(volume, noise_map, **params)
        +apply_to_volumes(volumes, noise_map, **params)
        +get_params_dependent_on_data(params, data)
    }
Loading

Class diagram for updated add_noise function

classDiagram
    class fpixel {
        +add_noise(img: np.ndarray, noise: np.ndarray) np.ndarray
    }
    fpixel : add_noise broadcasts noise to match img shape
Loading

Class diagram for get_image_data parameter extraction

classDiagram
    class get_image_data {
        +get_image_data(data: dict) -> dict
        """Returns metadata: dtype, height, width, num_channels"""
    }
    class GaussianNoiseTransform {
        +get_params_dependent_on_data(params, data)
    }
    GaussianNoiseTransform ..> get_image_data : uses
Loading

File-Level Changes

Change Details Files
Revamp generate_noise test coverage
  • Introduce get_noise_params helper for consistent parameter selection
  • Parameterize tests across noise types, spatial modes, and channel configurations
  • Cover grayscale, RGB, multi-channel, volume, and batch inputs
  • Validate approximation behavior and value ranges
  • Add xfail tests for known 4D failure cases
tests/functional/test_functional.py
Fix noise broadcasting in add_noise
  • Compute number of pixels in image and noise map
  • Stack and reshape noise array to match image dimensions
  • Switch from add() to add_array() for proper broadcasted addition
albumentations/augmentations/pixel/functional.py
Refactor and extend GaussianNoise transform
  • Import and use get_image_data for unified dtype and shape extraction
  • Replace manual image indexing with metadata-based height, width, channel handling
  • Update generate_noise calls to use consistent shape tuple
  • Add apply_to_images, apply_to_volume, and apply_to_volumes methods for batch/volume support
albumentations/augmentations/pixel/transforms.py
Upgrade albucore dependency
  • Bump albucore version from 0.0.26 to 0.0.28 in setup.py
setup.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. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the 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 exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

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

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 @trylinka - I've reviewed your changes - here's some feedback:

  • The new apply_to_images/apply_to_volume/apply_to_volumes methods duplicate existing batch/single transform logic; consider leveraging the batch_transform decorator or a common helper to reduce code duplication and ensure consistency.
  • Switching get_params_dependent_on_data to use params['shape'] and get_image_data(data) could break handling of different channel-order conventions—please verify that the shape and dtype extraction works correctly for both 2D images and 3D volumes.
  • Bumping albucore to 0.0.27 introduces the get_image_data function—double-check that v0.0.27 is backward-compatible and provides the expected API for this transform.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new apply_to_images/apply_to_volume/apply_to_volumes methods duplicate existing batch/single transform logic; consider leveraging the batch_transform decorator or a common helper to reduce code duplication and ensure consistency.
- Switching get_params_dependent_on_data to use params['shape'] and get_image_data(data) could break handling of different channel-order conventions—please verify that the shape and dtype extraction works correctly for both 2D images and 3D volumes.
- Bumping albucore to 0.0.27 introduces the get_image_data function—double-check that v0.0.27 is backward-compatible and provides the expected API for this transform.

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.

@trylinka
Copy link
Contributor Author

@sourcery-ai review

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 @trylinka - I've reviewed your changes and they look great!


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.

@trylinka
Copy link
Contributor Author

@sourcery-ai review

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 @trylinka - I've reviewed your changes - here's some feedback:

  • In functional.add_noise, consider using np.broadcast_to instead of manual stacking and reshaping to simplify the code and avoid extra memory copies.
  • The three new methods apply_to_images, apply_to_volume, and apply_to_volumes all just call fpixel.add_noise—refactor them to share a single implementation or delegate to apply to eliminate duplication.
  • Since generate_noise treats any 3D array as (H, W, C) rather than (D, H, W), add a clarifying note or validation to prevent confusion when users pass true volumetric data.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In functional.add_noise, consider using np.broadcast_to instead of manual stacking and reshaping to simplify the code and avoid extra memory copies.
- The three new methods apply_to_images, apply_to_volume, and apply_to_volumes all just call fpixel.add_noise—refactor them to share a single implementation or delegate to apply to eliminate duplication.
- Since generate_noise treats any 3D array as (H, W, C) rather than (D, H, W), add a clarifying note or validation to prevent confusion when users pass true volumetric data.

## Individual Comments

### Comment 1
<location> `albumentations/augmentations/pixel/functional.py:2304` </location>
<code_context>

     """
-    return add(img, noise, inplace=False)
+    num_pixels_in_image = np.prod(img.shape)
+    num_pixels_in_noise = np.prod(noise.shape)
+
+    noise = np.stack([noise] * (num_pixels_in_image // num_pixels_in_noise)).reshape(img.shape)
+
+    return add_array(img, noise, inplace=False)
</code_context>

<issue_to_address>
Noise tiling may not handle all broadcasting scenarios robustly.

Tiling assumes noise shape divides evenly into image shape, which may not always be true. Consider using np.broadcast_to or validating shapes to handle all scenarios robustly.
</issue_to_address>

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.

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 @trylinka - I've reviewed your changes and they look great!


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 3db139d into albumentations-team:main Jun 17, 2025
14 of 15 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants