U
    h@&                     @  s   d dl mZ d dlZd dlmZ d dlmZ d dlZd dl	m
Z
mZmZ d dlmZmZmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZmZmZ dgZG dd deZdS )    )annotationsN)Any)warn)AfterValidatorFieldmodel_validator)	AnnotatedLiteralSelf)random_utils)BaseDropout)check_1plusnondecreasing)	ColorTypeNumericType
ScalarTypeCoarseDropoutc                      s   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 fddZeddddddddZddddddZ	dd fdd Z
  ZS )"r   aL  CoarseDropout randomly drops out rectangular regions from the image and optionally,
    the corresponding regions in an associated mask, to simulate occlusion and
    varied object sizes found in real-world settings.

    This transformation is an evolution of CutOut and RandomErasing, offering more
    flexibility in the size, number of dropout regions, and fill values.

    Args:
        num_holes_range (tuple[int, int]): Range (min, max) for the number of rectangular
            regions to drop out. Default: (1, 1)
        hole_height_range (tuple[ScalarType, ScalarType]): Range (min, max) for the height
            of dropout regions. If int, specifies absolute pixel values. If float,
            interpreted as a fraction of the image height. Default: (8, 8)
        hole_width_range (tuple[ScalarType, ScalarType]): Range (min, max) for the width
            of dropout regions. If int, specifies absolute pixel values. If float,
            interpreted as a fraction of the image width. Default: (8, 8)
        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 (ColorType | None): Fill value for dropout regions in the mask.
            If None, mask regions corresponding to image dropouts are unchanged. Default: None
        p (float): Probability of applying the transform. Default: 0.5

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Note:
        - The actual number and size of dropout regions are randomly chosen within the specified ranges for each
            application.
        - When using float values for hole_height_range and hole_width_range, ensure they are between 0 and 1.
        - This implementation includes deprecation warnings for older parameter names (min_holes, max_holes, etc.).

    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.CoarseDropout(num_holes_range=(3, 6),
        ...                                hole_height_range=(10, 20),
        ...                                hole_width_range=(10, 20),
        ...                                fill_value=0,
        ...                                p=1.0)
        >>> transformed = augmentation(image=image, mask=mask)
        >>> transformed_image, transformed_mask = transformed["image"], transformed["mask"]

    References:
        - CutOut: https://arxiv.org/abs/1708.04552
        - Random Erasing: https://arxiv.org/abs/1708.04896
    c                   @  s   e Zd ZU eddZded< eddZded< ded< eddZded	< eddZded
< ded< eddZ	ded< eddZ
ded< ded< edddddddZed"dddddddZedddddd Zd!S )#zCoarseDropout.InitSchemar   )ge
int | None	min_holes	max_holeszVAnnotated[tuple[int, int], AfterValidator(check_1plus), AfterValidator(nondecreasing)]num_holes_rangeScalarType | None
min_height
max_heighttuple[ScalarType, ScalarType]hole_height_range	min_width	max_widthhole_width_rangezNumericType | Noneztuple[NumericType, NumericType])	min_value	max_valuedefault_rangereturnc                 C  s   |d k	r| p||fS |S )N )r    r!   r"   r$   r$   W/tmp/pip-unpacked-wheel-e8onvpoz/albumentations/augmentations/dropout/coarse_dropout.pyupdate_rangeX   s    z%CoarseDropout.InitSchema.update_rangestrfloatNone)range_value
range_nameminimumr#   c                 C  sp   || d   kr| d ks8n t d| d| d|  t| d trltdd | D slt d| d	|  d S )
Nr      zFirst value in z< should be less or equal than the second value and at least z. Got: c                 s  s&   | ]}d |  kodkn  V  qdS )r   r-   Nr$   ).0xr$   r$   r%   	<genexpr>i   s     z:CoarseDropout.InitSchema.validate_range.<locals>.<genexpr>zAll values in z! should be in [0, 1] range. Got: )
ValueError
isinstancer(   all)r*   r+   r,   r$   r$   r%   validate_rangeb   s     z'CoarseDropout.InitSchema.validate_rangeafter)moder
   r#   c                 C  s$  | j d k	rtdtdd | jd k	r0tdtdd | jd k	rHtdtdd | jd k	r`tdtdd | jd k	rxtdtdd | jd k	rtdtdd | jd k	r| | j | j| j	| _	| j
| j	d	d
d | jd k	r| | j| j| j| _| 
| jd | jd k	r| | j| j| j| _| 
| jd | S )Nz7`min_holes` is deprecated. Use num_holes_range instead.   )
stacklevelz7`max_holes` is deprecated. Use num_holes_range instead.z:`min_height` is deprecated. Use hole_height_range instead.z:`max_height` is deprecated. Use hole_height_range instead.z8`min_width` is deprecated. Use hole_width_range instead.z8`max_width` is deprecated. Use hole_width_range instead.r   r-   )r,   r   r   )r   r   DeprecationWarningr   r   r   r   r   r&   r   r4   r   r   selfr$   r$   r%   check_num_holes_and_dimensionsl   s,    







z7CoarseDropout.InitSchema.check_num_holes_and_dimensionsN)r   )__name__
__module____qualname__r   r   __annotations__r   r   r   r   r   staticmethodr&   r4   r   r=   r$   r$   r$   r%   
InitSchemaK   s   
		rC   Nr   r-   r-      rF         ?r   r   zColorType | Literal['random']zColorType | Noneztuple[int, int]r   zbool | Noner(   )r   r   r   r   r   r   
fill_valuemask_fill_valuer   r   r   always_applypc                   s*   t  j||||d |	| _|
| _|| _d S )N)rH   rI   rK   rJ   )super__init__r   r   r   )r<   r   r   r   r   r   r   rH   rI   r   r   r   rJ   rK   	__class__r$   r%   rM      s    zCoarseDropout.__init__intztuple[np.ndarray, np.ndarray])image_shapeheight_rangewidth_rangesizer#   c                 C  s   | dd \}}t |d tr|d }t|d |}|d }t|d |}	tjt|t|d |d}
tjt|t|	d |d}nD|tj|d |d |d t}
|tj|d |d |d t}|
|fS )z>Calculate random hole dimensions based on the provided ranges.Nr8   r   r-   rT   )r2   rP   minr   randintuniformZastype)rQ   rR   rS   rT   heightwidthr   r   r   r   hole_heightshole_widthsr$   r$   r%   calculate_hole_dimensions   s    ""z'CoarseDropout.calculate_hole_dimensionszdict[str, Any])paramsdatar#   c                 C  s   |d d d }t j| j }| j|| j| j|d\}}|d d \}}tjd|| d |d}	tjd|| d |d}
|	| }|
| }tj|
|	||gdd}d|iS )	Nshaper8   rU   r   r-   )Zaxisholes)	randomrW   r   r]   r   r   r   npstack)r<   r^   r_   rQ   Z	num_holesr[   r\   rY   rZ   Zy_minZx_minZy_maxZx_maxrb   r$   r$   r%   get_params_dependent_on_data   s    
z*CoarseDropout.get_params_dependent_on_dataztuple[str, ...]r7   c                   s   t   dS )Nr   r   r   )r   r   r   )rL   get_transform_init_args_namesr;   rN   r$   r%   rg      s    z+CoarseDropout.get_transform_init_args_names)NNNNNNr   NrD   rE   rE   NrG   )r>   r?   r@   __doc__r   rC   rM   rB   r]   rf   rg   __classcell__r$   r$   rN   r%   r      s(   7A             ,)
__future__r   rc   typingr   warningsr   Znumpyrd   Zpydanticr   r   r   Ztyping_extensionsr   r	   r
   Zalbumentationsr   Z/albumentations.augmentations.dropout.transformsr   Zalbumentations.core.pydanticr   r   Zalbumentations.core.typesr   r   r   __all__r   r$   r$   r$   r%   <module>   s   