U
    h$                     @  s   d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	m
Z
 d dlmZmZ d dlm  m  mZ d dlmZ d dlmZ d d	lmZmZmZ d d
lmZmZ dgZG dd deZdS )    )annotations)AnyLiteral)warn)AfterValidatorFieldmodel_validator)	AnnotatedSelfN)random_utils)BaseDropout)check_0pluscheck_1plusnondecreasing)MIN_UNIT_SIZE	ColorTypeGridDropoutc                      sz   e Zd ZdZG dd dejZdd	d
d
d
d
d
d
dddddddd	d fddZddddddZdd fddZ  Z	S )r   aj  Apply GridDropout augmentation to images, masks, bounding boxes, and keypoints.

    GridDropout drops out rectangular regions of an image and the corresponding mask in a grid fashion.
    This technique can help improve model robustness by forcing the network to rely on a broader context
    rather than specific local features.

    Args:
        ratio (float): The ratio of the mask holes to the unit size (same for horizontal and vertical directions).
            Must be between 0 and 1. Default: 0.5.
        unit_size_range (tuple[int, int] | None): Range from which to sample grid size. Default: None.
            Must be between 2 and the image's shorter edge. If None, grid size is calculated based on image size.
        holes_number_xy (tuple[int, int] | None): The number of grid units in x and y directions.
            First value should be between 1 and image width//2,
            Second value should be between 1 and image height//2.
            Default: None. If provided, overrides unit_size_range.
        random_offset (bool): Whether to offset the grid randomly between 0 and (grid unit size - hole size).
            If True, entered shift_xy is ignored and set randomly. Default: True.
        fill_value (int | float | Literal["random"] | tuple[int | float,...]): Value for the dropped pixels. Can be:
            - int or float: all channels are filled with this value.
            - tuple: tuple of values for each channel.
            - 'random': filled with random values.
            Default: 0.
        mask_fill_value (int | float | tuple[int | float,...] | None): Value for the dropped pixels in mask.
            If None, the mask is not modified. Default: None.
        shift_xy (tuple[int, int]): Offsets of the grid start in x and y directions from (0,0) coordinate.
            Only used when random_offset is False. Default: (0, 0).
        p (float): Probability of applying the transform. Default: 0.5.

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Note:
        - If both unit_size_range and holes_number_xy are None, the grid size is calculated based on the image size.
        - The actual number of dropped regions may differ slightly from holes_number_xy due to rounding.
        - This implementation includes deprecation warnings for older parameter names.

    Example:
        >>> import numpy as np
        >>> import albumentations as A
        >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
        >>> mask = np.random.randint(0, 2, (100, 100), dtype=np.uint8)
        >>> augmentation = A.GridDropout(ratio=0.3, unit_size_range=(10, 20), random_offset=True, p=1.0)
        >>> transformed = augmentation(image=image, mask=mask)
        >>> transformed_image, transformed_mask = transformed["image"], transformed["mask"]

    Reference:
        - Paper: https://arxiv.org/abs/2001.04086
    c                   @  s   e Zd ZU edddZded< eddZded	< eddZded
< eddZded< eddZ	ded< eddZ
ded< eddZded< ded< ded< ded< ded< ded< ded< edddddd Zd!S )"zGridDropout.InitSchemar      )gtlefloatratio   )ge
int | Noneunit_size_minunit_size_maxholes_number_xholes_number_yshift_xshift_yboolrandom_offsetColorType | Literal['random']
fill_valueColorType | Nonemask_fill_valuez]Annotated[tuple[int, int], AfterValidator(check_1plus), AfterValidator(nondecreasing)] | Noneunit_size_rangez7Annotated[tuple[int, int], AfterValidator(check_0plus)]shift_xyz>Annotated[tuple[int, int], AfterValidator(check_1plus)] | Noneholes_number_xyafter)moder
   returnc                 C  s   | j d k	r0| jd k	r0| j | jf| _tdtdd | jd k	r`| jd k	r`| j| jf| _tdtdd | jd k	r| j	d k	r| j| j	f| _
tdtdd | jrt| jd   kr| jd ksn td| S )	NzLunit_size_min and unit_size_max are deprecated. Use unit_size_range instead.r   )
stacklevelz9shift_x and shift_y are deprecated. Use shift_xy instead.zNholes_number_x and holes_number_y are deprecated. Use holes_number_xy instead.r   r   z<Max unit size should be >= min size, both at least 2 pixels.)r   r   r'   r   DeprecationWarningr   r    r(   r   r   r)   r   
ValueErrorself r3   U/tmp/pip-unpacked-wheel-e8onvpoz/albumentations/augmentations/dropout/grid_dropout.pyvalidate_normalization[   s(    (z-GridDropout.InitSchema.validate_normalizationN)__name__
__module____qualname__r   r   __annotations__r   r   r   r   r   r    r   r5   r3   r3   r3   r4   
InitSchemaG   s   
r:         ?NTr   r   r   r   r   r!   r#   r%   ztuple[int, int] | Noneztuple[int, int]zbool | None)r   r   r   r   r   r   r    r"   r$   r&   r'   r)   r(   always_applypc                   s6   t  j|	|
||d || _|| _|| _|| _|| _d S )N)r$   r&   r>   r=   )super__init__r   r'   r)   r"   r(   )r2   r   r   r   r   r   r   r    r"   r$   r&   r'   r)   r(   r=   r>   	__class__r3   r4   r@   v   s    zGridDropout.__init__zdict[str, Any])paramsdatar-   c                 C  sj   |d }| j r| j }n.t|| j| j \}}|d | |d | f}t||| j| jt | j	}d|iS )Nshaper   r   holes)
r)   fdropoutZcalculate_grid_dimensionsr'   Zgenerate_grid_holesr   r"   r   Zget_random_stater(   )r2   rC   rD   Zimage_shapeZgridZunit_heightZ
unit_widthrF   r3   r3   r4   get_params_dependent_on_data   s$    z(GridDropout.get_params_dependent_on_dataztuple[str, ...]r,   c                   s   t   dS )Nr   r'   r)   r(   r"   )r   r'   r)   r(   r"   )r?   get_transform_init_args_namesr1   rA   r3   r4   rI      s         z)GridDropout.get_transform_init_args_names)r;   NNNNNNTr   NNNr<   Nr;   )
r6   r7   r8   __doc__r   r:   r@   rH   rI   __classcell__r3   r3   rA   r4   r      s(   41               0) 
__future__r   typingr   r   warningsr   Zpydanticr   r   r   Ztyping_extensionsr	   r
   Z/albumentations.augmentations.dropout.functionalZaugmentationsZdropoutZ
functionalrG   Zalbumentationsr   Z/albumentations.augmentations.dropout.transformsr   Zalbumentations.core.pydanticr   r   r   Zalbumentations.core.typesr   r   __all__r   r3   r3   r3   r4   <module>   s   