Skip to content

Conversation

@ternaus
Copy link
Contributor

@ternaus ternaus commented Jun 24, 2025

Summary by Sourcery

Preserve input immutability and correct output range in normalization routines, extend tests accordingly, and bump the package version.

Bug Fixes:

  • Prevent normalize, normalize_per_image, and their NumPy/OpenCV/LUT variants from mutating the original image arrays

Enhancements:

  • Clamp per-image normalization outputs to the [0,1] range instead of [-20,20]
  • Ensure normalize_numpy always operates on and returns a copy rather than modifying the input array

Tests:

  • Add parameterized tests to verify that all normalize and normalize_per_image variants preserve the original image for uint8 and float32 types

Chores:

  • Bump project version to 0.0.33

@ternaus ternaus requested a review from Copilot June 24, 2025 22:36
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jun 24, 2025

Reviewer's Guide

This PR adds immutability tests for all normalize and normalize_per_image variants, updates normalization functions to operate on copies and return new float32 arrays, tightens per-image clipping to the [0,1] range, and bumps the package version.

Class diagram for updated normalization functions

classDiagram
    class normalize_numpy {
        +np.ndarray normalize_numpy(img: np.ndarray, mean: float | np.ndarray, denominator: float | np.ndarray)
        Note: Now always returns a new float32 array (copy=True)
    }
    class normalize_per_image_numpy {
        +np.ndarray normalize_per_image_numpy(img: np.ndarray, normalization: str, eps: float)
        Note: Now clips output to [0, 1] for min_max and min_max_per_channel
    }
    class normalize_per_image_lut {
        +np.ndarray normalize_per_image_lut(img: np.ndarray, normalization: str, eps: float, max_value: int)
        Note: Now clips LUT output to [0, 1] for min_max and min_max_per_channel
    }
Loading

File-Level Changes

Change Details Files
Add tests to ensure normalization functions don’t modify input images
  • Parametrize tests over uint8/float32 and 1/3 channels
  • Verify normalize, normalize_numpy, normalize_opencv, normalize_lut preserve originals
  • Verify normalize_per_image, normalize_per_image_numpy/opencv/lut preserve originals
tests/test_normalize.py
tests/test_normalize_per_image.py
Ensure normalize_numpy works on a copy and returns a new float32 array
  • Use astype(copy=True) when converting input to float32
  • Return (img * denominator) with astype(copy=True) instead of in-place
albucore/functions.py
Restrict per-image normalization outputs to [0,1] and avoid in-place clipping
  • Compute normalized_img before clipping in normalize_per_image_numpy
  • Replace clip range from [-20,20] to [0,1] in numpy variants
  • Update LUT-based methods to clip output to [0,1] instead of [-20,20]
albucore/functions.py
Bump project version
  • Update version from 0.0.32 to 0.0.33
pyproject.toml

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 @ternaus - 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.

Comment on lines +244 to +247
if dtype == np.uint8:
original_img = np.random.randint(0, 256, size=shape, dtype=dtype)
else:
original_img = np.random.randn(*shape).astype(dtype)
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (code-quality): Avoid conditionals in tests. (no-conditionals-in-tests)

ExplanationAvoid complex code, like conditionals, in test functions.

Google's software engineering guidelines says:
"Clear tests are trivially correct upon inspection"
To reach that avoid complex code in tests:

  • loops
  • conditionals

Some ways to fix this:

  • Use parametrized tests to get rid of the loop.
  • Move the complex logic into helpers.
  • Move the complex part into pytest fixtures.

Complexity is most often introduced in the form of logic. Logic is defined via the imperative parts of programming languages such as operators, loops, and conditionals. When a piece of code contains logic, you need to do a bit of mental computation to determine its result instead of just reading it off of the screen. It doesn't take much logic to make a test more difficult to reason about.

Software Engineering at Google / Don't Put Logic in Tests

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

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

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #67   +/-   ##
=======================================
  Coverage        ?   93.96%           
=======================================
  Files           ?       17           
  Lines           ?     1921           
  Branches        ?        0           
=======================================
  Hits            ?     1805           
  Misses          ?      116           
  Partials        ?        0           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR ensures normalization functions do not modify their inputs, updates clipping behaviour for float-based normalization, and bumps the package version.

  • Added tests to confirm normalize* routines preserve the original image
  • Changed normalize_numpy and related functions to always copy arrays and adjust clipping to [0,1] for min-max
  • Bumped version from 0.0.32 to 0.0.33

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
tests/test_normalize_per_image.py Added test_normalize_per_image_preserves_original to cover all variants
tests/test_normalize.py Added test_normalize_preserves_original_image for main normalization
pyproject.toml Incremented project version to 0.0.33
albucore/functions.py Updated copy semantics in normalize_numpy, adjusted min-max clipping
Comments suppressed due to low confidence (2)

albucore/functions.py:284

  • [nitpick] astype(copy=True) always allocates a new array; consider using copy=False or restructuring the computation to minimize unnecessary copies and reduce memory pressure.
    img = img.astype(np.float32, copy=True)

albucore/functions.py:291

  • [nitpick] Calling astype(copy=True) here produces an extra array even when img is already float32; consider avoiding redundant copies by using copy=False or performing the operation in-place.
    return (img * denominator).astype(np.float32, copy=True)

@ternaus ternaus merged commit e849218 into main Jun 24, 2025
14 checks passed
@ternaus ternaus deleted the fix_in_normalize_float32 branch June 24, 2025 22:40
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.

3 participants