
    h                        S r SSKJr  SSKJrJr  SSKrSSKJ	r	  SSK
Jr  SSKJr  SSKJrJr  S	S
KJr  S/rSr " S S\5      rg)a  Implementation of the Channel Dropout transform for multi-channel images.

This module provides the ChannelDropout transform, which randomly drops (sets to a fill value)
one or more channels in multi-channel images. This augmentation can help models become more
robust to missing or corrupted channel information and encourage learning from all available
channels rather than relying on a subset.
    )annotations)	AnnotatedAnyN)get_num_channels)AfterValidator)check_range_bounds)BaseTransformInitSchemaImageOnlyTransform   )channel_dropoutChannelDropout   c                  n   ^  \ rS rSrSr " S S\5      r   S	     S
U 4S jjjrSS jrSS jr	Sr
U =r$ )r      a  Randomly drop channels in the input image.

This transform randomly selects a number of channels to drop from the input image
and replaces them with a specified fill value. This can improve model robustness
to missing or corrupted channels.

The technique is conceptually similar to:
- Dropout layers in neural networks, which randomly set input units to 0 during training.
- CoarseDropout augmentation, which drops out regions in the spatial dimensions of the image.

However, ChannelDropout operates on the channel dimension, effectively "dropping out"
entire color channels or feature maps.

Args:
    channel_drop_range (tuple[int, int]): Range from which to choose the number
        of channels to drop. The actual number will be randomly selected from
        the inclusive range [min, max]. Default: (1, 1).
    fill (float): Pixel value used to fill the dropped channels.
        Default: 0.
    p (float): Probability of applying the transform. Must be in the range
        [0, 1]. Default: 0.5.

Raises:
    NotImplementedError: If the input image has only one channel.
    ValueError: If the upper bound of channel_drop_range is greater than or
        equal to the number of channels in the input image.

Targets:
    image, volume

Image types:
    uint8, float32

Examples:
    >>> import numpy as np
    >>> import albumentations as A
    >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
    >>> transform = A.ChannelDropout(channel_drop_range=(1, 2), fill=128, p=1.0)
    >>> result = transform(image=image)
    >>> dropped_image = result['image']
    >>> assert dropped_image.shape == image.shape
    >>> assert np.any(dropped_image != image)  # Some channels should be different

Note:
    - The number of channels to drop is randomly chosen within the specified range.
    - Channels are randomly selected for dropping.
    - This transform is not applicable to single-channel (grayscale) images.
    - The transform will raise an error if it's not possible to drop the specified
      number of channels (e.g., trying to drop 3 channels from an RGB image).
    - This augmentation can be particularly useful for training models to be robust
      against missing or corrupted channel data in multi-spectral or hyperspectral imagery.

c                  *    \ rS rSr% S\S'   S\S'   Srg)ChannelDropout.InitSchemaR   zGAnnotated[tuple[int, int], AfterValidator(check_range_bounds(1, None))]channel_drop_rangefloatfill N)__name__
__module____qualname____firstlineno____annotations____static_attributes__r       n/var/www/fran/franai/venv/lib/python3.13/site-packages/albumentations/augmentations/dropout/channel_dropout.py
InitSchemar   R   s    ccr   r    c                8   > [         TU ]  US9  Xl        X l        g )N)p)super__init__r   r   )selfr   r   r"   	__class__s       r   r$   ChannelDropout.__init__V   s      	1"4	r   c                .    [        XU R                  5      $ )a
  Apply channel dropout to the image.

Args:
    img (np.ndarray): Image to apply channel dropout to.
    channels_to_drop (list[int]): List of channel indices to drop.
    **params (Any): Additional parameters.

Returns:
    np.ndarray: Image with dropped channels.

)r   r   )r%   imgchannels_to_dropparamss       r   applyChannelDropout.applya   s     sdii@@r   c                8   SU;   a  US   OUS   S   n[        U5      nUS:X  a  Sn[        U5      eU R                  S   U:  a  Sn[        U5      eU R                  R
                  " U R                  6 nU R                  R                  [        U5      US9nSU0$ )	zGet parameters that depend on input data.

Args:
    params (dict[str, Any]): Parameters.
    data (dict[str, Any]): Input data.

Returns:
    dict[str, list[int]]: Dictionary with channels to drop.

imageimagesr   r   z6Images has one channel. ChannelDropout is not defined.z,Can not drop all channels in ChannelDropout.)kr*   )r   NotImplementedErrorr   
ValueError	py_randomrandintsamplerange)r%   r+   datar/   num_channelsmsgnum_drop_channelsr*   s           r   get_params_dependent_on_data+ChannelDropout.get_params_dependent_on_datao   s     ")DWd8nQ6G'.1JC%c**""1%5@CS/! NN22D4K4KL>>00|1DHY0Z"$455r   )r   r   ))r   r   r   g      ?)r   ztuple[int, int]r   r   r"   r   )r)   
np.ndarrayr*   z	list[int]r+   r   returnr>   )r+   dict[str, Any]r8   r@   r?   zdict[str, list[int]])r   r   r   r   __doc__r	   r    r$   r,   r<   r   __classcell__)r&   s   @r   r   r      sW    4l,  /5		+	 	 		 	A6 6r   )rA   
__future__r   typingr   r   numpynpalbucorer   pydanticr   albumentations.core.pydanticr   (albumentations.core.transforms_interfacer	   r
   
functionalr   __all__MIN_DROPOUT_CHANNEL_LIST_LENGTHr   r   r   r   <module>rN      sA    # !  % # ; ` '
"# k6' k6r   