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

Fix input types #2344

Merged
merged 8 commits into from
Feb 16, 2025
Merged

Fix input types #2344

merged 8 commits into from
Feb 16, 2025

Conversation

ternaus
Copy link
Collaborator

@ternaus ternaus commented Feb 16, 2025

Summary by Sourcery

Refine type hints across various augmentations, functions, and schemas for enhanced type safety and clarity. Update tests accordingly to maintain type consistency.

Bug Fixes:

  • Correct type hints for parameters fill, fill_mask, scale, translate_percent, translate_px, rotate, shear, shift_limit, shift_limit_x, shift_limit_y, scale_limit, rotate_limit, nb_rows, nb_cols, mean, std, p_replace, n_segments, distort_limit, multiplier, brightness, contrast, saturation, hue, gamma_limit, r_shift_limit, g_shift_limit, b_shift_limit, max_part_shift, clip_limit, quality_range, img_weight, beta_limit, radius, alias_blur, max_factor, step_factor, scale_limit, padding, rain_type, drop_color, blur_value, brightness_coefficient, hue_shift_limit, sat_shift_limit, val_shift_limit, brightness_limit, contrast_limit, position, font_color, operation, group_element across multiple augmentations and schemas.
  • Remove unnecessary cast operations and correct type annotations for improved type safety.
  • Update tests to reflect the type hint changes and ensure type consistency.
  • Fix type annotations in Perspective, Affine, ShiftScaleRotate, PiecewiseAffine, Pad, PadIfNeeded, Normalize, RandomRain, RandomCropFromBorders, CLAHE, RandomBrightnessContrast, ImageCompression, ColorJitter, TemplateTransform, RingingOvershoot, UnsharpMask, PixelDropout, AdvancedBlur, RandomFog, ChromaticAberration, Morphological, ChannelShuffle, Pad3D, PadIfNeeded3D, CenterCrop3D, RandomCrop3D, CoarseDropout3D, BaseCropAndPad, RandomCrop, CenterCrop, Crop, CropAndPad, RandomScale, FDA, SafeRotate, Rotate, TextImage, Erasing, CoarseDropout, ConstrainedCoarseDropout, Blur, MotionBlur, MedianBlur, GaussianBlur, GlassBlur, ZoomBlur, MaskDropout, GridDropout, XYMasking classes and their respective schemas.
  • Correct type annotations for d4, bboxes_d4, keypoints_d4, warp_affine_with_value_extension, warp_affine, keypoints_affine, create_affine_transformation_matrix, pad, extend_value, copy_make_border_with_value_extension, pad_with_params, remap, validate_rotate_method, is_rgb_image, is_grayscale_image, is_multispectral_image, _maybe_process_in_chunks, clip, clip_image, clip_bboxes_v2, get_random_crop_coords, random_crop, center_crop, crop_bbox_by_coords, bbox_rot90, keypoint_rot90, try_add_depth, adjust_rotation_matrix, safe_rotate, keypoints_rotation_matrix, affine, adjust_keypoint_by_scale, flip_keypoints, from_distance_maps, get_num_channels, get_center_crop_coords, get_random_crop_coords3d, center_crop3d, random_crop3d, generate_holes, cutout3d, generate_binary_mask, get_multichannel_color, cutout, generate_gaussian_noise_xy, generate_grid_dropout_mask, grid_dropout, channel_dropout, random_shadow functions to ensure accurate type information and prevent potential runtime errors.

Copy link
Contributor

sourcery-ai bot commented Feb 16, 2025

Reviewer's Guide by Sourcery

This pull request focuses on improving type safety and code clarity by updating type hints throughout the albumentations library. It introduces more specific type annotations using Literal for parameters with a limited set of allowed values and uses tuple[float, ...] | float for parameters that can accept either a single number or a tuple of numbers. These changes enhance the library's usability and reduce the risk of type-related errors.

Updated class diagram for Perspective transform

classDiagram
    class Perspective {
        -border_mode (OpenCV flag)
        -fill (tuple[float, ...] | float)
        -fill_mask (tuple[float, ...] | float)
    }
Loading

Updated class diagram for Affine transform

classDiagram
    class Affine {
        -scale: tuple[float, float] | float | dict[str, float | tuple[float, float]]
        -translate_percent: tuple[float, float] | float | dict[str, float | tuple[float, float]] | None
        -translate_px: tuple[float, float] | float | dict[str, float | tuple[float, float]] | None
        -rotate: tuple[float, float] | float
        -shear: tuple[float, float] | float | dict[str, float | tuple[float, float]]
        -fill (tuple[float, ...] | float)
        -fill_mask (tuple[float, ...] | float)
    }
Loading

Updated class diagram for ShiftScaleRotate transform

classDiagram
    class ShiftScaleRotate {
        -fill (tuple[float, ...] | float)
        -fill_mask (tuple[float, ...] | float)
        -shift_limit_x: tuple[float, float] | float | None
        -shift_limit_y: tuple[float, float] | float | None
    }
Loading

Updated class diagram for PiecewiseAffine transform

classDiagram
    class PiecewiseAffine {
        -nb_rows: tuple[int, int] | int
        -nb_cols: tuple[int, int] | int
    }
Loading

Updated class diagram for D4 transform

classDiagram
    class D4 {
        -group_element: Literal["e", "r90", "r180", "r270", "v", "hvt", "h", "t"]
    }
Loading

Updated class diagram for Pad transform

classDiagram
    class Pad {
        -fill (tuple[float, ...] | float)
        -fill_mask (tuple[float, ...] | float)
    }
Loading

Updated class diagram for PadIfNeeded transform

classDiagram
    class PadIfNeeded {
        -position: Literal["center", "top_left", "top_right", "bottom_left", "bottom_right", "random"]
        -fill (tuple[float, ...] | float)
        -fill_mask (tuple[float, ...] | float)
    }
Loading

Updated class diagram for Normalize transform

classDiagram
    class Normalize {
        -mean (tuple[float, ...] | float | None)
        -std (tuple[float, ...] | float | None)
    }
Loading

Updated class diagram for Rain transform

classDiagram
    class Rain {
        -rain_type: Literal["drizzle", "heavy", "torrential", "default"]
    }
Loading

Updated class diagram for HueSaturationValue transform

classDiagram
    class HueSaturationValue {
        -hue_shift_limit: tuple[float, float] | float
        -sat_shift_limit: tuple[float, float] | float
        -val_shift_limit: tuple[float, float] | float
    }
Loading

Updated class diagram for RandomBrightnessContrast transform

classDiagram
    class RandomBrightnessContrast {
        -brightness_limit: tuple[float, float] | float
        -contrast_limit: tuple[float, float] | float
    }
Loading

Updated class diagram for CLAHE transform

classDiagram
    class CLAHE {
        -clip_limit: tuple[float, float] | float
    }
Loading

Updated class diagram for RandomGamma transform

classDiagram
    class RandomGamma {
        -gamma_limit: tuple[float, float] | float
    }
Loading

Updated class diagram for MultiplicativeNoise transform

classDiagram
    class MultiplicativeNoise {
        -multiplier: tuple[float, float] | float
    }
Loading

Updated class diagram for ColorJitter transform

classDiagram
    class ColorJitter {
        -brightness: tuple[float, float] | float
        -contrast: tuple[float, float] | float
        -saturation: tuple[float, float] | float
        -hue: tuple[float, float] | float
    }
Loading

Updated class diagram for GridReplace transform

classDiagram
    class GridReplace {
        -p_replace: tuple[float, float] | float
        -n_segments: tuple[int, int] | int
    }
Loading

Updated class diagram for RingingOvershoot transform

classDiagram
    class RingingOvershoot {
        -blur_limit: tuple[int, int] | int
    }
Loading

Updated class diagram for UnsharpMask transform

classDiagram
    class UnsharpMask {
        -blur_limit: tuple[int, int] | int
        -alpha: tuple[float, float] | float
    }
Loading

Updated class diagram for PixelDropout transform

classDiagram
    class PixelDropout {
        -drop_value: tuple[float, ...] | float | None
        -mask_drop_value: tuple[float, ...] | float | None
    }
Loading

Updated class diagram for GaussNoise transform

classDiagram
    class GaussNoise {
        -mean: tuple[float, float] | float
        -std: tuple[float, float] | float
        -gauss_sigma: tuple[float, float] | float
        -cutout_threshold: tuple[float, float] | float
        -intensity: tuple[float, float] | float
    }
Loading

Updated class diagram for ChromaticAberration transform

classDiagram
    class ChromaticAberration {
        -primary_distortion_limit: tuple[float, float] | float
        -secondary_distortion_limit: tuple[float, float] | float
        -mode: Literal["green_purple", "red_blue", "random"]
    }
Loading

Updated class diagram for Morphological transform

classDiagram
    class Morphological {
        -scale: tuple[int, int] | int
        -operation: Literal["erosion", "dilation"]
    }
Loading

Updated class diagram for RGBShift transform

classDiagram
    class RGBShift {
        -r_shift_limit: tuple[float, float] | float
        -g_shift_limit: tuple[float, float] | float
        -b_shift_limit: tuple[float, float] | float
    }
Loading

Updated class diagram for Pad3D transform

classDiagram
    class Pad3D {
        -fill: tuple[float, ...] | float
        -fill_mask: tuple[float, ...] | float
    }
Loading

Updated class diagram for PadIfNeeded3D transform

classDiagram
    class PadIfNeeded3D {
        -fill: tuple[float, ...] | float
        -fill_mask: tuple[float, ...] | float
    }
Loading

Updated class diagram for CenterCrop3D transform

classDiagram
    class CenterCrop3D {
        -fill: tuple[float, ...] | float
        -fill_mask: tuple[float, ...] | float
    }
Loading

Updated class diagram for RandomCrop3D transform

classDiagram
    class RandomCrop3D {
        -fill: tuple[float, ...] | float
        -fill_mask: tuple[float, ...] | float
    }
Loading

Updated class diagram for CoarseDropout3D transform

classDiagram
    class CoarseDropout3D {
        -fill: tuple[float, ...] | float
        -fill_mask: tuple[float, ...] | float | None
    }
Loading

Updated class diagram for RandomCrop transform

classDiagram
    class RandomCrop {
        -fill: tuple[float, ...] | float
        -fill_mask: tuple[float, ...] | float
    }
Loading

Updated class diagram for CenterCrop transform

classDiagram
    class CenterCrop {
        -fill: tuple[float, ...] | float
        -fill_mask: tuple[float, ...] | float
    }
Loading

Updated class diagram for Crop transform

classDiagram
    class Crop {
        -fill: tuple[float, ...] | float
        -fill_mask: tuple[float, ...] | float
    }
Loading

Updated class diagram for CropAndPad transform

classDiagram
    class CropAndPad {
        -fill: tuple[float, ...] | float
        -fill_mask: tuple[float, ...] | float
    }
Loading

Updated class diagram for RandomScale transform

classDiagram
    class RandomScale {
        -scale_limit: tuple[float, float] | float
    }
Loading

Updated class diagram for FDA transform

classDiagram
    class FDA {
        -beta_limit: tuple[float, float] | float
    }
Loading

Updated class diagram for TemplateMatching transform

classDiagram
    class TemplateMatching {
        -img_weight: tuple[float, float] | float
    }
Loading

Updated class diagram for CoarseDropout transform

classDiagram
    class CoarseDropout {
        -hole_height_range: tuple[float, float] | tuple[int, int]
        -hole_width_range: tuple[float, float] | tuple[int, int]
        -fill: tuple[float, ...] | float | Literal["random", "random_uniform", "inpaint_telea", "inpaint_ns"]
        -fill_mask: tuple[float, ...] | float | None
    }
Loading

Updated class diagram for Erasing transform

classDiagram
    class Erasing {
        -fill: tuple[float, ...] | float | Literal["random", "random_uniform", "inpaint_telea", "inpaint_ns"]
        -fill_mask: tuple[float, ...] | float | None
    }
Loading

Updated class diagram for ConstrainedCoarseDropout transform

classDiagram
    class ConstrainedCoarseDropout {
        -fill: tuple[float, ...] | float | Literal["random", "random_uniform", "inpaint_telea", "inpaint_ns"]
        -fill_mask: tuple[float, ...] | float | None
    }
Loading

Updated class diagram for MaskDropout transform

classDiagram
    class MaskDropout {
        -fill: float | Literal["inpaint_telea", "inpaint_ns"]
        -fill_mask: float
    }
Loading

Updated class diagram for XYMasking transform

classDiagram
    class XYMasking {
        -fill: tuple[float, ...] | float | Literal["random", "random_uniform", "inpaint_telea", "inpaint_ns"]
        -fill_mask: tuple[float, ...] | float | None
    }
Loading

Updated class diagram for GridDropout transform

classDiagram
    class GridDropout {
        -fill: tuple[float, ...] | float | Literal["random", "random_uniform", "inpaint_telea", "inpaint_ns"]
        -fill_mask: tuple[float, ...] | float | None
    }
Loading

Updated class diagram for Blur transform

classDiagram
    class Blur {
        -blur_limit: tuple[int, int] | int
    }
Loading

Updated class diagram for Defocus transform

classDiagram
    class Defocus {
        -blur_limit: tuple[int, int] | int
    }
Loading

Updated class diagram for MedianBlur transform

classDiagram
    class MedianBlur {
        -blur_limit: tuple[int, int] | int
    }
Loading

Updated class diagram for GaussianBlur transform

classDiagram
    class GaussianBlur {
        -sigma_limit: tuple[float, float] | float
        -blur_limit: tuple[int, int] | int
    }
Loading

Updated class diagram for ZoomBlur transform

classDiagram
    class ZoomBlur {
        -blur_limit: tuple[int, int] | int
        -sigma_x_limit: tuple[float, float] | float
        -sigma_y_limit: tuple[float, float] | float
        -rotate_limit: tuple[int, int] | int
        -beta_limit: tuple[float, float] | float
        -noise_limit: tuple[float, float] | float
    }
Loading

Updated class diagram for RicianNoise transform

classDiagram
    class RicianNoise {
        -radius: tuple[int, int] | int
        -alias_blur: tuple[float, float] | float
    }
Loading

Updated class diagram for MotionBlur transform

classDiagram
    class MotionBlur {
        -max_factor: tuple[float, float] | float
        -step_factor: tuple[float, float] | float
    }
Loading

Updated class diagram for Rotate transform

classDiagram
    class Rotate {
        -limit: tuple[float, float] | float
        -fill: tuple[float, ...] | float
        -fill_mask: tuple[float, ...] | float | None
    }
Loading

Updated class diagram for SafeRotate transform

classDiagram
    class SafeRotate {
        -limit: tuple[float, float] | float
        -fill: tuple[float, ...] | float
        -fill_mask: tuple[float, ...] | float
    }
Loading

Updated class diagram for TextImage transform

classDiagram
    class TextImage {
        -fraction_range: tuple[float, float]
        -font_size_fraction_range: tuple[float, float]
        -font_color: list[tuple[float, ...] | float | str] | tuple[float, ...] | float | str
    }
Loading

File-Level Changes

Change Details Files
Updated type hints for fill and fill_mask parameters in various transforms to accept either a single float/int or a tuple of floats/ints.
  • Changed type hints for fill and fill_mask to `tuple[float, ...]
floatinPerspective, Affine, ShiftScaleRotate, Pad, PadIfNeeded, CenterCrop3D, RandomCrop3D, CoarseDropout3D, Pad3D, Crop, and CropAndPadtransforms.</li><li>Updated theInitSchemaclasses to reflect the new type hints forfillandfill_mask.</li><li>Updated the applymethods inPad3DandCropAndPadto correctly handle the new type hints forfillandfill_mask`.
Updated type hints for parameters related to ranges and limits to accept either a single float/int or a tuple of floats/ints.
  • Changed type hints for scale, translate_percent, translate_px, rotate, and shear in Affine transform.
  • Changed type hints for shift_limit, scale_limit, rotate_limit, shift_limit_x, and shift_limit_y in ShiftScaleRotate transform.
  • Changed type hints for nb_rows and nb_cols in PiecewiseAffine transform.
  • Changed type hints for distort_limit in OpticalDistortion and ElasticTransform transforms.
  • Changed type hints for hue_shift_limit, sat_shift_limit, and val_shift_limit in HueSaturationValue transform.
  • Changed type hints for brightness_limit and contrast_limit in RandomBrightnessContrast transform.
  • Changed type hints for clip_limit in CLAHE transform.
  • Changed type hints for gamma_limit in RandomGamma transform.
  • Changed type hints for multiplier in MultiplicativeNoise transform.
  • Changed type hints for brightness, contrast, saturation, and hue in ColorJitter transform.
  • Changed type hints for p_replace and n_segments in Superpixels transform.
  • Changed type hints for blur_limit in RingingOvershoot transform.
  • Changed type hints for blur_limit, sigma_limit, and alpha in ISONoise transform.
  • Updated the InitSchema classes to reflect the new type hints.
albumentations/augmentations/geometric/transforms.py
albumentations/augmentations/transforms.py
albumentations/augmentations/blur/transforms.py
albumentations/core/pydantic.py
Updated type hints for mean and std parameters in Normalize transform to accept either a single float or a tuple of floats.
  • Changed type hints for mean and std to `tuple[float, ...]
float
Updated type hints for rain_type parameter in Rain transform to use Literal for allowed values.
  • Changed type hint for rain_type to Literal["drizzle", "heavy", "torrential", "default"] in Rain transform.
albumentations/augmentations/transforms.py
Updated type hints for group_element parameter in D4 transform to use Literal for allowed values.
  • Changed type hint for group_element to Literal["e", "r90", "r180", "r270", "v", "hvt", "h", "t"] in D4 transform.
  • Updated the get_params method in D4 transform to return the correct type hint.
albumentations/augmentations/geometric/transforms.py
albumentations/augmentations/geometric/functional.py
Updated type hints for position parameter in PadIfNeeded and related transforms to use Literal for allowed values.
  • Changed type hint for position to Literal["center", "top_left", "top_right", "bottom_left", "bottom_right", "random"] in PadIfNeeded and related transforms.
  • Updated the InitSchema classes to reflect the new type hints for position.
albumentations/augmentations/geometric/transforms.py
albumentations/augmentations/crops/transforms.py
Updated type hints for mode parameter in ChromaticAberration transform to use Literal for allowed values.
  • Changed type hint for mode to Literal["green_purple", "red_blue", "random"] in ChromaticAberration transform.
albumentations/augmentations/transforms.py
Updated type hints for operation parameter in Morphological transform to use Literal for allowed values.
  • Changed type hint for operation to Literal["erosion", "dilation"] in Morphological transform.
albumentations/augmentations/transforms.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 changelog entry to document the changes to the input types.
  • It looks like you've replaced ColorType with tuple[float, ...] | float in many places, which is good, but you might want to double-check that you've covered all instances where ColorType was used.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟡 Complexity: 2 issues 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 51e8db5 into main Feb 16, 2025
14 checks passed
@ternaus ternaus deleted the fix_input_types branch February 16, 2025 22:28
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