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 stain augment #2337

Merged
merged 7 commits into from
Feb 10, 2025
Merged

Add stain augment #2337

merged 7 commits into from
Feb 10, 2025

Conversation

ternaus
Copy link
Collaborator

@ternaus ternaus commented Feb 9, 2025

Summary by Sourcery

Add an H&E stain augmentation to simulate different staining conditions in histopathology images. The augmentation supports predefined stain matrices, Vahadane and Macenko stain extraction methods, and custom matrices. Control the intensity variations with intensity_scale_range and intensity_shift_range, and choose whether to augment background regions with augment_background.

New Features:

  • Added HEStain augmentation for histopathology images.

Tests:

  • Added tests for the new HEStain augmentation, including shape validation, comparison with scikit-learn NMF, stain separation accuracy, and background augmentation behavior.

Copy link
Contributor

sourcery-ai bot commented Feb 9, 2025

Reviewer's Guide by Sourcery

This PR introduces a new stain augmentation feature for histopathology images. The changes involve adding functions and classes for extracting and normalizing H&E stains using various methods (Vahadane, Macenko, presets), integrating a new HEStain transform into the augmentation pipeline, and expanding the test suite to validate the new functionality.

Updated class diagram for H&E Stain Augmentation

classDiagram
    class StainNormalizer {
        +stain_matrix_target
        +fit(img: np.ndarray)
    }

    class VahadaneNormalizer {
        +fit(img: np.ndarray)
    }

    class MacenkoNormalizer {
        -angular_percentile: float
        +fit(img: np.ndarray, angular_percentile: float = 99)
    }

    StainNormalizer <|-- VahadaneNormalizer
    StainNormalizer <|-- MacenkoNormalizer

    class SimpleNMF {
        -n_iter: int
        -initial_colors: np.array
        +fit_transform(optical_density: np.ndarray) : tuple[np.ndarray, np.ndarray]
    }

    class HEStain {
        -method: Literal["preset", "random_preset", "vahadane", "macenko"]
        -preset: Literal["ruifrok", "macenko", "standard", "high_contrast", "h_heavy", "e_heavy", "dark", "light"]
        -intensity_scale_range: tuple[float, float]
        -intensity_shift_range: tuple[float, float]
        -augment_background: bool
        -stain_normalizer
        -preset_names: list
        +get_stain_matrix(img: np.ndarray) : np.ndarray
        +apply(img: np.ndarray, stain_matrix: np.ndarray, scale_factors: np.ndarray, shift_values: np.ndarray) : np.ndarray
        +get_params_dependent_on_data(params: dict, data: dict) : dict
        +get_transform_init_args() : dict
    }

    class InitSchema {
        -method
        -preset
        -intensity_scale_range: tuple[float, float]
        -intensity_shift_range: tuple[float, float]
        -augment_background: bool
        +validate_matrix_selection() : Self
    }

    HEStain "1" *-- "1" InitSchema : uses

    %% Depending on the method, HEStain composes a stain extractor
    HEStain o-- VahadaneNormalizer : "stain_extractor (if method=='vahadane')"
    HEStain o-- MacenkoNormalizer : "stain_extractor (if method=='macenko')"
Loading

File-Level Changes

Change Details Files
Implemented core stain augmentation functions and classes
  • Added standard reference H&E stain matrices
  • Introduced get_normalizer function to select between Vahadane and Macenko methods
  • Defined base class StainNormalizer along with VahadaneNormalizer and MacenkoNormalizer implementations
  • Implemented SimpleNMF class for non-negative matrix factorization with iterative updates
  • Created order_stains_combined function for robust stain ordering using angular and spectral information
  • Implemented get_tissue_mask helper to identify tissue regions
  • Developed apply_he_stain_augmentation to adjust stain concentrations and reconstruct corrected images
albumentations/augmentations/functional.py
Expanded HE stain augmentation test coverage
  • Added tests for SimpleNMF, comparing shape, non-negativity, and unit norm constraints
  • Included tests to validate consistency against scikit-learn's NMF implementation
  • Created tests for normalizer output shape and stain separation using synthetic H&E images
  • Added tests for varying angular percentiles in MacenkoNormalizer
  • Tested augmentation effects on stain intensities with both background augmentation and non-background augmentation scenarios
tests/functional/test_functional.py
Integrated new HEStain transform into augmentation pipeline
  • Added HEStain class in the transforms module to wrap the stain augmentation logic
  • Implemented transform initialization schema with validation for method parameters and preset selection
  • Implemented get_stain_matrix to decide stain matrix selection (preset, random, or extraction via normalizers)
  • Integrated apply method to call apply_he_stain_augmentation and batch transformation methods for images and volumes
  • Modified get_params_dependent_on_data to generate random scale and shift factors
albumentations/augmentations/transforms.py
Updated augmentation test configurations to include the new HEStain transform
  • Added HEStain to lists of transforms in test augmentation files
  • Included HEStain configurations in augmentation definitions
tests/test_augmentations.py
tests/aug_definitions.py
tests/test_core.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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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 - here's some feedback:

Overall Comments:

  • Consider adding a usage example to the documentation for the new transform.
  • The SimpleNMF class could benefit from more detailed comments explaining the iterative update steps.
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.

albumentations/augmentations/functional.py Show resolved Hide resolved
tests/functional/test_functional.py Show resolved Hide resolved
albumentations/augmentations/functional.py Show resolved Hide resolved
tests/functional/test_functional.py Show resolved Hide resolved
tests/functional/test_functional.py Show resolved Hide resolved
tests/functional/test_functional.py Show resolved Hide resolved
tests/test_core.py Show resolved Hide resolved
albumentations/augmentations/transforms.py Show resolved Hide resolved
@ternaus ternaus merged commit 9c41743 into main Feb 10, 2025
14 checks passed
@ternaus ternaus deleted the add_stain_augment branch February 10, 2025 00:14
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