Skip to content

Conversation

@ternaus
Copy link
Contributor

@ternaus ternaus commented Jun 24, 2025

Summary by Sourcery

Unify and streamline LUT-based image processing functions by standardizing on float32 precision, consolidating dtype conversions, and simplifying per-channel processing loops.

Enhancements:

  • Standardize output dtype to float32 across LUT and normalization functions and remove redundant dtype casts
  • Replace cv2.merge calls with direct per-channel numpy assignments in apply_lut and multiply_add_lut
  • Switch OpenCV multiply and add operations to use CV_32F instead of CV_64F
  • Unify and simplify normalization routines by adjusting _normalize_mean_std_opencv signature and removing redundant casts
  • Incorporate clipping and casting into LUT creation for normalization methods
  • Clean up unused variables in multichannel flip function

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

sourcery-ai bot commented Jun 24, 2025

Reviewer's Guide

This PR refactors core LUT operations to use explicit per-channel loops with preallocated float32 buffers, standardizes use of float32 in OpenCV operations and LUT generation, and cleans up redundant conversions and unused code across the normalization and arithmetic functions.

Class diagram for refactored LUT and normalization functions

classDiagram
    class functions {
        +apply_lut(img, value, operation, inplace)
        +normalize_lut(img, mean, denominator, dtype, max_value)
        +power_lut(img, exponent, inplace)
        +multiply_add_lut(img, factor, value, inplace)
        +normalize_per_image_lut(img, normalization, eps, max_value)
        +to_float_lut(img, max_value)
        +_normalize_mean_std_opencv(img, mean, std)
        +normalize_per_image_opencv(img, normalization, eps)
    }
    functions : np.empty_like(img, dtype=np.float32)
    functions : per-channel for-loops for LUT application
    functions : standardize float32 dtype for OpenCV operations
    functions : remove redundant dtype conversions
    functions : remove unused code
    functions --> cv2.LUT
    functions --> cv2.multiply
    functions --> cv2.add
    functions --> cv2.normalize
    functions --> sz_lut
    functions --> clip
    functions --> create_lut_array
Loading

Flow diagram for per-channel LUT application refactor

flowchart TD
    A[Start: apply_lut or multiply_add_lut called] --> B[Preallocate result array as float32]
    B --> C{For each channel}
    C -->|i=0..num_channels-1| D[Apply LUT to channel i]
    D --> C
    C -->|All channels processed| E[Return result array]
Loading

File-Level Changes

Change Details Files
Replace cv2.merge list comprehensions with manual per-channel loops and np.empty_like results
  • Preallocate result array with np.empty_like and dtype=float32 in apply_lut
  • Loop over channels instead of cv2.merge in apply_lut and multiply_add_lut
  • Return the assembled result array directly
albucore/functions.py
Standardize float32 usage in arithmetic and LUT operations
  • Change cv2.multiply and cv2.add dtype arguments from CV_64F to CV_32F
  • Cast combined LUT expressions to float32 in multiply_add_opencv
  • Append .astype(np.float32) to all vectorized LUT computations
albucore/functions.py
Eliminate redundant dtype conversions and streamline normalization
  • Remove intermediate .astype(np.float32) calls where input arrays are already float32
  • Drop mean/denominator float32 casts in normalize_lut and consolidate into final astype
  • Move image-to-float32 conversion into lower-level helper and simplify upstream calls in normalize_per_image_opencv
albucore/functions.py
Cleanup unused code and minor refactors
  • Remove unused variables (height, width) in _flip_multichannel
  • Uncomment or remove a stale @preserve_channel_dim decorator line
  • Rename function parameters for consistency (_normalize_mean_std_opencv signature update)
albucore/functions.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

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 fixes various issues with LUT-based image processing functions to improve type consistency and normalization behavior. Key changes include replacing cv2.merge with manual per-channel processing, updating data types for OpenCV arithmetic, and modifying clipping behavior in normalization routines.

Comments suppressed due to low confidence (2)

albucore/functions.py:733

  • The removal of the explicit clipping (previously result.clip(-20, 20)) in the 'image_per_channel' normalization branch may change the expected output range. If this behavior is intended, consider updating the function documentation to clearly reflect the change.
        return result

albucore/functions.py:470

  • Changing the 'inplace' flag from True to False for the clip function in multiply_add_lut may alter the expected side-effect behavior. Verify that downstream code does not rely on in-place modifications of the LUT array.
    luts = clip(np.arange(0, max_value + 1, dtype=np.float32) * factor + value, dtype, inplace=False)

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 80.76923% with 5 lines in your changes missing coverage. Please review.

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

Files with missing lines Patch % Lines
albucore/functions.py 80.76% 5 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main      #66   +/-   ##
=======================================
  Coverage        ?   93.80%           
=======================================
  Files           ?       17           
  Lines           ?     1873           
  Branches        ?        0           
=======================================
  Hits            ?     1757           
  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
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.

@ternaus ternaus merged commit b87ebb0 into main Jun 24, 2025
14 checks passed
@ternaus ternaus deleted the fix_clip branch June 24, 2025 20:49
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