o
    iN#                    @  s  d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlZddlZddlmZ dd	l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 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)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z2 ddl0m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= g dZ>G dd dee$Z?G dd de?Z@G dd de?ZAG dd de?ZBG dd dee$ZCG dd  d eCZDG d!d" d"eCZEG d#d$ d$eCZFG d%d& d&e%eCZGG d'd( d(eGZHG d)d* d*e%e e$e"ZIG d+d, d,eCZJG d-d. d.e%e e$e"ZKG d/d0 d0e%e e$e"ZLG d1d2 d2e%e e$e"ZMG d3d4 d4ee$ZNG d5d6 d6e&ZOdS )7zC
A collection of "vanilla" transforms for crop and pad operations.
    )annotationsN)CallableSequence)chainceil)Any)IndexSelection)NdarrayOrTensor)get_track_meta)
MetaTensor)get_random_patchget_valid_patch_size)	crop_funcpad_func)InvertibleTransformTraceableTransform)MultiSampleTrait)LazyTransformRandomizable	Transform)compute_divisible_spatial_size#generate_label_classes_crop_centers#generate_pos_neg_label_crop_centersgenerate_spatial_bounding_boxis_positivemap_binary_to_indicesmap_classes_to_indicesweighted_patch_samples)ImageMetaKey)LazyAttrMethodPytorchPadMode	TraceKeysTransformBackendsconvert_data_typeconvert_to_tensorensure_tupleensure_tuple_repfall_back_tuplelook_up_option)Pad
SpatialPad	BorderPadDivisiblePadCropSpatialCropCenterSpatialCropCenterScaleCropRandSpatialCropRandScaleCropRandSpatialCropSamplesCropForegroundRandWeightedCropRandCropByPosNegLabelRandCropByLabelClassesResizeWithPadOrCropBoundingRectc                   @  sV   e Zd ZdZejejgZdej	dfdddZ
dddZ			dd ddZd!ddZdS )"r+   a  
    Perform padding for a given an amount of padding in each dimension.

    `torch.nn.functional.pad` is used unless the mode or kwargs are not available in torch,
    in which case `np.pad` will be used.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        to_pad: the amount to pad in each dimension (including the channel) [(low_H, high_H), (low_W, high_W), ...].
            if None, must provide in the `__call__` at runtime.
        mode: available modes: (Numpy) {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
            ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
            (PyTorch) {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
            One of the listed string values or a user supplied function. Defaults to ``"constant"``.
            See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
            https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
            requires pytorch >= 1.10 for best compatibility.
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
        kwargs: other arguments for the `np.pad` or `torch.pad` function.
            note that `np.pad` treats channel dimension as the first dimension.

    NFto_padtuple[tuple[int, int]] | NonemodestrlazyboolreturnNonec                 K  s"   t | | || _|| _|| _d S N)r   __init__r<   r>   kwargs)selfr<   r>   r@   rF    rH   `/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/transforms/croppad/array.pyrE   m   s   
zPad.__init__spatial_shapeSequence[int]tuple[tuple[int, int]]c                 C  s   t d| jj d)z
        dynamically compute the pad width according to the spatial shape.
        the output is the amount of padding for all dimensions including the channel.

        Args:
            spatial_shape: spatial shape of the original image.

        z	subclass z must implement this method.)NotImplementedError	__class____name__)rG   rJ   rH   rH   rI   compute_pad_widthy   s   	zPad.compute_pad_widthimgtorch.Tensor
str | Nonebool | Nonec                 K  s   |du r| j n|}|du r"t|tr| n|jdd }| |}|du r)| jn|}t| j}	|		| t
|t d}
|du rC| jn|}t|
||  ||fi |	S )as  
        Args:
            img: data to be transformed, assuming `img` is channel-first and padding doesn't apply to the channel dim.
            to_pad: the amount to be padded in each dimension [(low_H, high_H), (low_W, high_W), ...].
                default to `self.to_pad`.
            mode: available modes: (Numpy) {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
                ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
                (PyTorch) {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
                One of the listed string values or a user supplied function. Defaults to ``"constant"``.
                See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
                https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
            lazy: a flag to override the lazy behaviour for this call, if set. Defaults to None.
            kwargs: other arguments for the `np.pad` or `torch.pad` function.
                note that `np.pad` treats channel dimension as the first dimension.

        N   data
track_meta)r<   
isinstancer   peek_pending_shapeshaperP   r>   dictrF   updater&   r   r@   r   get_transform_info)rG   rQ   r<   r>   r@   rF   Zto_pad_rJ   Zmode_kwargs_img_tlazy_rH   rH   rI   __call__   s    


zPad.__call__rW   r   c           	      C  s   |  |}|tj d }|d d dks|d d dkr<|d d }tt|d d |d t|}||t||  }dd |dd  D }dd t|jdd  |dd  D }t||d}|	d ||W  d    S 1 stw   Y  d S )	Npaddedr   rU   c                 S  s   g | ]}|d  qS r   rH   .0irH   rH   rI   
<listcomp>       zPad.inverse.<locals>.<listcomp>c                 S  s   g | ]
\}}||d   qS )rU   rH   rf   rg   jrH   rH   rI   rh          	roi_startroi_endF)
pop_transformr#   
EXTRA_INFOminmaxlenzipr[   r0   trace_transform)	rG   rW   	transformrc   sern   ro   cropperrH   rH   rI   inverse   s   
  &$zPad.inverse)r<   r=   r>   r?   r@   rA   rB   rC   rJ   rK   rB   rL   NNN)
rQ   rR   r<   r=   r>   rS   r@   rT   rB   rR   )rW   r   rB   r   )rO   
__module____qualname____doc__r$   TORCHNUMPYbackendr"   CONSTANTrE   rP   rb   r{   rH   rH   rH   rI   r+   Q   s    
$r+   c                      s8   e Zd ZdZejejdfd fddZdddZ	  Z
S )r,   a  
    Performs padding to the data, symmetric for all sides or all on one side for each dimension.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        spatial_size: the spatial size of output data after padding, if a dimension of the input
            data size is larger than the pad size, will not pad that dimension.
            If its components have non-positive values, the corresponding size of input image will be used
            (no padding). for example: if the spatial size of input data is [30, 30, 30] and
            `spatial_size=[32, 25, -1]`, the spatial size of output data will be [32, 30, 30].
        method: {``"symmetric"``, ``"end"``}
            Pad image symmetrically on every side or only pad at the end sides. Defaults to ``"symmetric"``.
        mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
            ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
            available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
            One of the listed string values or a user supplied function. Defaults to ``"constant"``.
            See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
            https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
        kwargs: other arguments for the `np.pad` or `torch.pad` function.
            note that `np.pad` treats channel dimension as the first dimension.

    Fspatial_size7Sequence[int] | int | tuple[tuple[int, ...] | int, ...]methodr?   r>   r@   rA   rB   rC   c                   s.   || _ t|t| _t jd||d| d S Nr>   r@   rH   )r   r*   r!   r   superrE   )rG   r   r   r>   r@   rF   rN   rH   rI   rE      s   zSpatialPad.__init__rJ   rK   rL   c                   s   t | j }| jtjkr2g }t|D ]\}}t| |  d}|t|d t||d  f qn fddt|D }t	dg| S )z
        dynamically compute the pad width according to the spatial shape.

        Args:
            spatial_shape: spatial shape of the original image.

        r      c                   s*   g | ]\}}d t t| |  d fqS rd   )intrs   )rf   rg   sp_irJ   rH   rI   rh      s   * z0SpatialPad.compute_pad_width.<locals>.<listcomp>r   r   )
r)   r   r   r!   	SYMMETRIC	enumeraters   appendr   tuple)rG   rJ   r   	pad_widthrg   r   widthrH   r   rI   rP      s   $zSpatialPad.compute_pad_width)
r   r   r   r?   r>   r?   r@   rA   rB   rC   r|   )rO   r~   r   r   r!   r   r"   r   rE   rP   __classcell__rH   rH   r   rI   r,      s    r,   c                      s4   e Zd ZdZejdfd fddZdddZ  ZS )r-   a  
    Pad the input data by adding specified borders to every dimension.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        spatial_border: specified size for every spatial border. Any -ve values will be set to 0. It can be 3 shapes:

            - single int number, pad all the borders with the same size.
            - length equals the length of image shape, pad every spatial dimension separately.
              for example, image shape(CHW) is [1, 4, 4], spatial_border is [2, 1],
              pad every border of H dim with 2, pad every border of W dim with 1, result shape is [1, 8, 6].
            - length equals 2 x (length of image shape), pad every border of every dimension separately.
              for example, image shape(CHW) is [1, 4, 4], spatial_border is [1, 2, 3, 4], pad top of H dim with 1,
              pad bottom of H dim with 2, pad left of W dim with 3, pad right of W dim with 4.
              the result shape is [1, 7, 11].
        mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
            ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
            available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
            One of the listed string values or a user supplied function. Defaults to ``"constant"``.
            See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
            https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
        kwargs: other arguments for the `np.pad` or `torch.pad` function.
            note that `np.pad` treats channel dimension as the first dimension.

    Fspatial_borderSequence[int] | intr>   r?   r@   rA   rB   rC   c                   s"   || _ t jd||d| d S r   )r   r   rE   )rG   r   r>   r@   rF   r   rH   rI   rE     s   zBorderPad.__init__rJ   rK   rL   c              	     s   t | j tdd  D std  dtdd  D  t dkr/ fdd|D }nDt t|krEd	d  d t| D }n.t t|d
 kr] fddtt|D }ntdt  dt| dd
t|  dtdg| S )Nc                 s  s    | ]}t |tV  qd S rD   )rY   r   rf   brH   rH   rI   	<genexpr>      z.BorderPad.compute_pad_width.<locals>.<genexpr>z0self.spatial_border must contain only ints, got .c                 s  s    | ]}t d |V  qdS )r   N)rs   r   rH   rH   rI   r     r   rU   c                   s$   g | ]}t  d  t  d  fqS rd   r   )rf   _r   rH   rI   rh     s   $ z/BorderPad.compute_pad_width.<locals>.<listcomp>c                 S  s   g | ]
}t |t |fqS rH   r   )rf   sprH   rH   rI   rh     rl   r   c                   s0   g | ]}t  d |  t  d | d  fqS )r   rU   r   re   r   rH   rI   rh     s    $z#Unsupported spatial_border length: z/, available options are [1, len(spatial_shape)=z, 2*len(spatial_shape)=z].r   )r'   r   all
ValueErrorr   rt   range)rG   rJ   Zdata_pad_widthrH   r   rI   rP     s(   



zBorderPad.compute_pad_width)r   r   r>   r?   r@   rA   rB   rC   r|   )	rO   r~   r   r   r"   r   rE   rP   r   rH   rH   r   rI   r-      s
    r-   c                      s>   e Zd ZdZejZejej	dfd fddZ
dddZ  ZS )r.   z
    Pad the input data, so that the spatial sizes are divisible by `k`.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.
    Fkr   r>   r?   r   r@   rA   rB   rC   c                   s,   || _ t|| _t jd||d| dS )aS  
        Args:
            k: the target k for each spatial dimension.
                if `k` is negative or 0, the original size is preserved.
                if `k` is an int, the same `k` be applied to all the input spatial dimensions.
            mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
                ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
                available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
                One of the listed string values or a user supplied function. Defaults to ``"constant"``.
                See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
                https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
            method: {``"symmetric"``, ``"end"``}
                Pad image symmetrically on every side or only pad at the end sides. Defaults to ``"symmetric"``.
            lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
            kwargs: other arguments for the `np.pad` or `torch.pad` function.
                note that `np.pad` treats channel dimension as the first dimension.

        See also :py:class:`monai.transforms.SpatialPad`
        r   NrH   )r   r!   r   r   rE   )rG   r   r>   r   r@   rF   r   rH   rI   rE   4  s   
zDivisiblePad.__init__rJ   rK   rL   c                 C  s&   t || jd}t|| jd}||S )N)rJ   r   )r   r   )r   r   r,   r   rP   )rG   rJ   new_sizeZspatial_padrH   rH   rI   rP   S  s   
zDivisiblePad.compute_pad_width)
r   r   r>   r?   r   r?   r@   rA   rB   rC   r|   )rO   r~   r   r   r,   r   r"   r   r!   r   rE   rP   r   rH   rH   r   rI   r.   *  s    r.   c                   @  sV   e Zd ZdZejgZddddZe					dd ddZ		d!d"ddZ
d#ddZdS )$r/   a+  
    Perform crop operations on the input image.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
    Fr@   rA   c                 C  s   t | | d S rD   )r   rE   )rG   r@   rH   rH   rI   rE   f  s   zCrop.__init__N
roi_center,Sequence[int] | int | NdarrayOrTensor | Noneroi_sizern   ro   
roi_slicesSequence[slice] | NonerB   tuple[slice]c                 C  s@  |rt dd |D std| dt|S | durO|durOt| tjddd}t|tjddd}t|}tj|d	d
d}t|| |}	t|	| |	}
n+|du sW|du r[tdt|tjdd}	t|	t|	}	t|tjdd}
t|
|	}
|		 dkrtt
t|	 t|
 gS tdd t|	 |
 D S )a~  
        Compute the crop slices based on specified `center & size` or `start & end` or `slices`.

        Args:
            roi_center: voxel coordinates for center of the crop ROI.
            roi_size: size of the crop ROI, if a dimension of ROI size is larger than image size,
                will not crop that dimension of the image.
            roi_start: voxel coordinates for start of the crop ROI.
            roi_end: voxel coordinates for end of the crop ROI, if a coordinate is out of image,
                use the end coordinate of image.
            roi_slices: list of slices for each of the spatial dimensions.

        c                 s  s$    | ]}|j d u p|j dkV  qd S )NrU   )step)rf   rx   rH   rH   rI   r     s   " z&Crop.compute_slices.<locals>.<genexpr>z8only slice steps of 1/None are currently supported, got r   NTcpu)rW   dtypewrap_sequencedevicer   floor)rounding_modezAplease specify either roi_center, roi_size or roi_start, roi_end.)rW   r   r   rU   c                 S  s"   g | ]\}}t t|t|qS rH   )slicer   )rf   rx   ry   rH   rH   rI   rh     s   " z'Crop.compute_slices.<locals>.<listcomp>)r   r   r'   r&   torchint16
zeros_likedividemaximumnumelr   r   itemru   tolist)r   r   rn   ro   r   Zroi_center_tZ
roi_size_tZ_zeroshalfroi_start_t	roi_end_trH   rH   rI   compute_slicesi  s(   
  zCrop.compute_slicesrQ   rR   slicestuple[slice, ...]rT   c                 C  s   t |}tt|tr| n|jdd }t||k r)|tdg|t|  7 }t tdg|d|  }t|t d}|du rD| j	n|}t
|t|||  S )
        Apply the transform to `img`, assuming `img` is channel-first and
        slicing doesn't apply to the channel dim.

        rU   NrV   )listrt   rY   r   rZ   r[   r   r&   r   r@   r   r   r^   )rG   rQ   r   r@   Zslices_sdr`   ra   rH   rH   rI   rb     s   $zCrop.__call__r   c                 C  sV   |  |}|tj d }t|}|d ||W  d    S 1 s$w   Y  d S )NcroppedF)rp   r#   rq   r-   rv   )rG   rQ   rw   r   inverse_transformrH   rH   rI   r{     s   
$zCrop.inverseF)r@   rA   )NNNNN)r   r   r   r   rn   r   ro   r   r   r   rB   r   rD   )rQ   rR   r   r   r@   rT   rB   rR   rQ   r   rB   r   )rO   r~   r   r   r$   r   r   rE   staticmethodr   rb   r{   rH   rH   rH   rI   r/   Y  s    
/r/   c                      s@   e Zd ZdZ						dd fddZdd fddZ  ZS )r0   a  
    General purpose cropper to produce sub-volume region of interest (ROI).
    If a dimension of the expected ROI size is larger than the input image size, will not crop that dimension.
    So the cropped result may be smaller than the expected ROI, and the cropped results of several images may
    not have exactly the same shape.
    It can support to crop ND spatial (channel-first) data.

    The cropped region can be parameterised in various ways:
        - a list of slices for each spatial dimension (allows for use of negative indexing and `None`)
        - a spatial center and size
        - the start and end coordinates of the ROI

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.
    NFr   r   r   rn   ro   r   r   r@   rA   rB   rC   c                   s&   t  | | j|||||d| _dS )a  
        Args:
            roi_center: voxel coordinates for center of the crop ROI.
            roi_size: size of the crop ROI, if a dimension of ROI size is larger than image size,
                will not crop that dimension of the image.
            roi_start: voxel coordinates for start of the crop ROI.
            roi_end: voxel coordinates for end of the crop ROI, if a coordinate is out of image,
                use the end coordinate of image.
            roi_slices: list of slices for each of the spatial dimensions.
            lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.

        )r   r   rn   ro   r   N)r   rE   r   r   )rG   r   r   rn   ro   r   r@   r   rH   rI   rE     s   
zSpatialCrop.__init__rQ   rR   rT   c                   s*   |du r| j n|}t j|t| j|dS )r   NrQ   r   r@   )r@   r   rb   r'   r   rG   rQ   r@   ra   r   rH   rI   rb     s   zSpatialCrop.__call__)NNNNNF)r   r   r   r   rn   r   ro   r   r   r   r@   rA   rB   rC   rD   rQ   rR   r@   rT   rB   rR   rO   r~   r   r   rE   rb   r   rH   rH   r   rI   r0     s    r0   c                      sB   e Zd ZdZdd fd	d
Zd fddZdd fddZ  ZS )r1   a  
    Crop at the center of image with specified ROI size.
    If a dimension of the expected ROI size is larger than the input image size, will not crop that dimension.
    So the cropped result may be smaller than the expected ROI, and the cropped results of several images may
    not have exactly the same shape.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        roi_size: the spatial size of the crop region e.g. [224,224,128]
            if a dimension of ROI size is larger than image size, will not crop that dimension of the image.
            If its components have non-positive values, the corresponding size of input image will be used.
            for example: if the spatial size of input data is [40, 40, 40] and `roi_size=[32, 64, -1]`,
            the spatial size of output data will be [32, 40, 40].
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
    Fr   r   r@   rA   rB   rC   c                      t  j|d || _d S Nr@   )r   rE   r   )rG   r   r@   r   rH   rI   rE        
zCenterSpatialCrop.__init__r   rK   r   c                   s*   t | j|}dd |D }t j||dS )Nc                 S  s   g | ]}|d  qS )r   rH   re   rH   rH   rI   rh     ri   z4CenterSpatialCrop.compute_slices.<locals>.<listcomp>)r   r   )r)   r   r   r   )rG   r   r   r   r   rH   rI   r     s   z CenterSpatialCrop.compute_slicesNrQ   rR   rT   c                   sF   |du r| j n|}t j|| t|tr| n|jdd |dS )r   NrU   r   )r@   r   rb   r   rY   r   rZ   r[   r   r   rH   rI   rb     s   $zCenterSpatialCrop.__call__r   )r   r   r@   rA   rB   rC   )r   rK   rB   r   rD   r   )rO   r~   r   r   rE   r   rb   r   rH   rH   r   rI   r1     s
    r1   c                      s4   e Zd ZdZdd fddZdd fddZ  ZS )r2   a%  
    Crop at the center of image with specified scale of ROI size.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        roi_scale: specifies the expected scale of image size to crop. e.g. [0.3, 0.4, 0.5] or a number for all dims.
            If its components have non-positive values, will use `1.0` instead, which means the input image size.
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
    F	roi_scaleSequence[float] | floatr@   rA   c                   r   r   )r   rE   r   )rG   r   r@   r   rH   rI   rE   !  r   zCenterScaleCrop.__init__NrQ   rR   rT   rB   c                   sz   t |tr	| n|jdd  }t|}dd tt| j||D }|d u r)| jn|}t	||d}t
 j||||dS )NrU   c                 S     g | ]
\}}t || qS rH   r   rf   rrx   rH   rH   rI   rh   (  rl   z,CenterScaleCrop.__call__.<locals>.<listcomp>r   r@   r   )rY   r   rZ   r[   rt   ru   r(   r   r@   r1   r   rb   r   )rG   rQ   r@   img_sizendimr   ra   rz   r   rH   rI   rb   %  s    zCenterScaleCrop.__call__r   )r   r   r@   rA   rD   r   r   rH   rH   r   rI   r2     s    r2   c                      sF   e Zd ZdZ				dd fddZdddZdd fddZ  ZS ) r3   aQ  
    Crop image with random size or specific size ROI. It can crop at a random position as center
    or at the image center. And allows to set the minimum and maximum size to limit the randomly generated ROI.

    Note: even `random_size=False`, if a dimension of the expected ROI size is larger than the input image size,
    will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped results
    of several images may not have exactly the same shape.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        roi_size: if `random_size` is True, it specifies the minimum crop region.
            if `random_size` is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128]
            if a dimension of ROI size is larger than image size, will not crop that dimension of the image.
            If its components have non-positive values, the corresponding size of input image will be used.
            for example: if the spatial size of input data is [40, 40, 40] and `roi_size=[32, 64, -1]`,
            the spatial size of output data will be [32, 40, 40].
        max_roi_size: if `random_size` is True and `roi_size` specifies the min crop region size, `max_roi_size`
            can specify the max crop region size. if None, defaults to the input image size.
            if its components have non-positive values, the corresponding size of input image will be used.
        random_center: crop at random position as center or the image center.
        random_size: crop with random size or specific size ROI.
            if True, the actual size is sampled from `randint(roi_size, max_roi_size + 1)`.
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
    NTFr   r   max_roi_sizeSequence[int] | int | Nonerandom_centerrA   random_sizer@   rB   rC   c                   s2   t  | || _|| _|| _|| _d | _|  d S rD   )r   rE   r   r   r   r   _size)rG   r   r   r   r   r@   r   rH   rI   rE   J  s   zRandSpatialCrop.__init__r   rK   c                   s   t j|_jrAjd u r|nt j| tdd tj D r0tdj d  dt fddt	t
|D _jrTt|j}t||j_d S d S )Nc                 s  s    | ]	\}}||kV  qd S rD   rH   rj   rH   rH   rI   r   ^  s    z,RandSpatialCrop.randomize.<locals>.<genexpr>zmin ROI size: z is larger than max ROI size: r   c                 3  s.    | ]}j jj|  | d  dV  qdS )rU   )lowhighN)Rrandintr   re   max_sizerG   rH   rI   r   `  s   , )r)   r   r   r   r   anyru   r   r   r   rt   r   r   r   r   _slices)rG   r   Z
valid_sizerH   r   rI   	randomizeZ  s   "zRandSpatialCrop.randomizerQ   rR   r   rT   c                   s   t |tr	| n|jdd }|r| | | jdu r td|du r'| jn|}| jr6t	 j
|| j|dS t| j|d}t	 j
||||dS )r   rU   Nzself._size not specified.r   r   )rY   r   rZ   r[   r   r   RuntimeErrorr@   r   r   rb   r   r1   r   )rG   rQ   r   r@   r   ra   rz   r   rH   rI   rb   e  s    

zRandSpatialCrop.__call__NTFF)r   r   r   r   r   rA   r   rA   r@   rA   rB   rC   r   rK   rB   rC   TNrQ   rR   r   rA   r@   rT   rB   rR   )rO   r~   r   r   rE   r   rb   r   rH   rH   r   rI   r3   .  s    
r3   c                      sR   e Zd ZdZ				dd fddZdd Zd fddZd d! fddZ  ZS )"r4   a5  
    Subclass of :py:class:`monai.transforms.RandSpatialCrop`. Crop image with
    random size or specific size ROI.  It can crop at a random position as
    center or at the image center.  And allows to set the minimum and maximum
    scale of image size to limit the randomly generated ROI.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        roi_scale: if `random_size` is True, it specifies the minimum crop size: `roi_scale * image spatial size`.
            if `random_size` is False, it specifies the expected scale of image size to crop. e.g. [0.3, 0.4, 0.5].
            If its components have non-positive values, will use `1.0` instead, which means the input image size.
        max_roi_scale: if `random_size` is True and `roi_scale` specifies the min crop region size, `max_roi_scale`
            can specify the max crop region size: `max_roi_scale * image spatial size`.
            if None, defaults to the input image size. if its components have non-positive values,
            will use `1.0` instead, which means the input image size.
        random_center: crop at random position as center or the image center.
        random_size: crop with random size or specified size ROI by `roi_scale * image spatial size`.
            if True, the actual size is sampled from
            `randint(roi_scale * image spatial size, max_roi_scale * image spatial size + 1)`.
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
    NTFr   r   max_roi_scaleSequence[float] | float | Noner   rA   r   r@   rB   rC   c                   s&   t  jdd |||d || _|| _d S )N)r   r   r   r   r@   )r   rE   r   r   )rG   r   r   r   r   r@   r   rH   rI   rE     s
   

zRandScaleCrop.__init__c                 C  s\   t |}dd tt| j||D | _| jd ur)dd tt| j||D | _d S d | _d S )Nc                 S  r   rH   r   r   rH   rH   rI   rh     rl   z2RandScaleCrop.get_max_roi_size.<locals>.<listcomp>c                 S  r   rH   r   r   rH   rH   rI   rh     rl   )rt   ru   r(   r   r   r   r   )rG   r   r   rH   rH   rI   get_max_roi_size  s
   
"
zRandScaleCrop.get_max_roi_sizer   rK   c                   s   |  | t | d S rD   )r   r   r   )rG   r   r   rH   rI   r     s   
zRandScaleCrop.randomizerQ   rR   r   rT   c                   sJ   |  t|tr| n|jdd  |du r| jn|}t j|||dS )r   rU   N)rQ   r   r@   )r   rY   r   rZ   r[   r@   r   rb   )rG   rQ   r   r@   ra   r   rH   rI   rb     s   &zRandScaleCrop.__call__r   )r   r   r   r   r   rA   r   rA   r@   rA   rB   rC   r   r   r   )	rO   r~   r   r   rE   r   r   rb   r   rH   rH   r   rI   r4   w  s    r4   c                      sj   e Zd ZdZejZ				d&d'ddZ	d(d) fddZej	j
d*ddZ	d+d,ddZd+d-d$d%Z  ZS ).r5   a   
    Crop image with random size or specific size ROI to generate a list of N samples.
    It can crop at a random position as center or at the image center. And allows to set
    the minimum size to limit the randomly generated ROI.
    It will return a list of cropped images.

    Note: even `random_size=False`, if a dimension of the expected ROI size is larger than the input image size,
    will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped
    results of several images may not have exactly the same shape.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        roi_size: if `random_size` is True, it specifies the minimum crop region.
            if `random_size` is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128]
            if a dimension of ROI size is larger than image size, will not crop that dimension of the image.
            If its components have non-positive values, the corresponding size of input image will be used.
            for example: if the spatial size of input data is [40, 40, 40] and `roi_size=[32, 64, -1]`,
            the spatial size of output data will be [32, 40, 40].
        num_samples: number of samples (crop regions) to take in the returned list.
        max_roi_size: if `random_size` is True and `roi_size` specifies the min crop region size, `max_roi_size`
            can specify the max crop region size. if None, defaults to the input image size.
            if its components have non-positive values, the corresponding size of input image will be used.
        random_center: crop at random position as center or the image center.
        random_size: crop with random size or specific size ROI.
            The actual size is sampled from `randint(roi_size, img_size)`.
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.

    Raises:
        ValueError: When ``num_samples`` is nonpositive.

    NTFr   r   num_samplesr   r   r   r   rA   r   r@   rB   rC   c                 C  s@   t | | |dk rtd| d|| _t|||||| _d S )NrU   z"num_samples must be positive, got r   )r   rE   r   r   r3   rz   )rG   r   r   r   r   r   r@   rH   rH   rI   rE     s
   	zRandSpatialCropSamples.__init__seed
int | Nonestatenp.random.RandomState | Nonec                   s    t  || | j|| | S rD   )r   set_random_staterz   )rG   r   r   r   rH   rI   r     s   z'RandSpatialCropSamples.set_random_statevaluec                 C     || _ || j_d S rD   )_lazyrz   r@   )rG   r   rH   rH   rI   r@        zRandSpatialCropSamples.lazyrW   
Any | Nonec                 C  s   d S rD   rH   )rG   rW   rH   rH   rI   r     s   z RandSpatialCropSamples.randomizerQ   rR   rT   list[torch.Tensor]c                 C  sd   g }|du r	| j n|}t| jD ]}| j||d}t r*||jtj< | j|d|d |	| q|S )z
        Apply the transform to `img`, assuming `img` is channel-first and
        cropping doesn't change the channel dim.
        Nr   Treplacer@   )
r@   r   r   rz   r   metaKeyPATCH_INDEXpush_transformr   )rG   rQ   r@   retra   rg   r   rH   rH   rI   rb     s   zRandSpatialCropSamples.__call__r   )r   r   r   r   r   r   r   rA   r   rA   r@   rA   rB   rC   NN)r   r   r   r   rB   r5   )r   rA   rB   rC   rD   )rW   r   rB   rC   )rQ   rR   r@   rT   rB   r   )rO   r~   r   r   r3   r   rE   r   r   r@   setterr   rb   r   rH   rH   r   rI   r5     s    "r5   c                      s   e Zd ZdZedddddejdfd-ddZej	j
d.ddZ	edd Zd/dd Z		d0d1 fd%d&Z	d2d3d(d)Zd4 fd+d,Z  ZS )5r6   a  
    Crop an image using a bounding box. The bounding box is generated by selecting foreground using select_fn
    at channels channel_indices. margin is added in each spatial dimension of the bounding box.
    The typical usage is to help training and evaluation if the valid part is small in the whole medical image.
    Users can define arbitrary function to select expected foreground from the whole image or specified channels.
    And it can also add margin to every dim of the bounding box of foreground object.
    For example:

    .. code-block:: python

        image = np.array(
            [[[0, 0, 0, 0, 0],
              [0, 1, 2, 1, 0],
              [0, 1, 3, 2, 0],
              [0, 1, 2, 1, 0],
              [0, 0, 0, 0, 0]]])  # 1x5x5, single channel 5x5 image


        def threshold_at_one(x):
            # threshold at 1
            return x > 1


        cropper = CropForeground(select_fn=threshold_at_one, margin=0)
        print(cropper(image))
        [[[2, 1],
          [3, 2],
          [2, 1]]]

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Nr   FrU   	select_fnr   channel_indicesIndexSelection | Nonemarginr   allow_smallerrA   return_coordsk_divisibler>   r?   r@   rB   rC   c	           
      K  sZ   t | | || _|durt|nd| _|| _|| _|| _|| _t	d||d|	| _
dS )a  
        Args:
            select_fn: function to select expected foreground, default is to select values > 0.
            channel_indices: if defined, select foreground only on the specified channels
                of image. if None, select foreground on the whole image.
            margin: add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims.
            allow_smaller: when computing box size with `margin`, whether to allow the image edges to be smaller than the
                final box edges. If `False`, part of a padded output box might be outside of the original image, if `True`,
                the image edges will be used as the box edges. Default to `False`.
                The default value is changed from `True` to `False` in v1.5.0.
            return_coords: whether return the coordinates of spatial bounding box for foreground.
            k_divisible: make each spatial dimension to be divisible by k, default to 1.
                if `k_divisible` is an int, the same `k` be applied to all the input spatial dimensions.
            mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
                ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
                available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
                One of the listed string values or a user supplied function. Defaults to ``"constant"``.
                See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
                https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
            lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
            pad_kwargs: other arguments for the `np.pad` or `torch.pad` function.
                note that `np.pad` treats channel dimension as the first dimension.

        Nr   rH   )r   rE   r  r'   r	  r  r  r  r  r+   padder)
rG   r  r	  r  r  r  r  r>   r@   
pad_kwargsrH   rH   rI   rE   +  s   $zCropForeground.__init___valc                 C  r   rD   )r   r  r@   rG   r  rH   rH   rI   r@   X  r   zCropForeground.lazyc                 C     dS NFrH   rG   rH   rH   rI   requires_current_data]     z$CropForeground.requires_current_datarQ   rR   tuple[np.ndarray, np.ndarray]c           	      C  s   t || j| j| j| j\}}t|tjtjdd^}}t|tjtjdd^}}|| }t	t
| | jd}|tt	|| d }|| }||fS )z
        Compute the start points and end points of bounding box to crop.
        And adjust bounding box coords to be divisible by `k`.

        T)output_typer   r   )r   r   )r   r  r	  r  r  r%   npndarrayr   asarrayr   r   r  floor_divide)	rG   rQ   	box_startbox_endZ
box_start_r   Zbox_end_Zorig_spatial_sizer   rH   rH   rI   compute_bounding_boxa  s   z#CropForeground.compute_bounding_boxr  
np.ndarrayr  rS   c                   sR  | j ||d}t j|||d}t| d}	t|tt|tr%| n|j	dd  d}
t
tt|	 |
  }t|dt|trK| n|j	dd }| jjd||||d|}t rt|tr|sz|j |jd tj d	< |S |j }|j }|tj }||d	< | j||tj|tj |tj |tj  ||d
 |S )z:
        Crop and pad based on the bounding box.

        rm   r   r   rU   Nr   )rQ   r<   r>   r@   r   pad_info)	orig_sizesp_sizeaffiner@   
extra_inforH   )r   r   rb   r  r   r  rY   r   rZ   r[   r   r   ru   r   r-   rP   r  r   applied_operationspopr#   rq   pending_operationsr  get	ORIG_SIZEr    SHAPEAFFINE)rG   rQ   r  r  r>   r@   r  r   r   Zpad_to_startZ
pad_to_endpadr   r  r"  	crop_infoextrar   rH   rI   crop_padt  s8   *




zCropForeground.crop_padrT   c           	      K  sP   |  |\}}|du r| jn|}| j||||fd|i|}| jr&|||fS |S )z
        Apply the transform to `img`, assuming `img` is channel-first and
        slicing doesn't change the channel dim.
        Nr@   )r   r@   r1  r  )	rG   rQ   r>   r@   r  r  r  ra   r   rH   rH   rI   rb     s   
zCropForeground.__call__r   c                   s>   |  |}|tj d}|j| | j|}t |S )Nr"  )	get_most_recent_transformr#   rq   r(  r'  r   r  r{   r   )rG   rQ   rw   r"  invr   rH   rI   r{     s
   
zCropForeground.inverse)r  r   r	  r
  r  r   r  rA   r  rA   r  r   r>   r?   r@   rA   rB   rC   r  rA   )rQ   rR   rB   r  r  )rQ   rR   r  r!  r  r!  r>   rS   r@   rA   rB   rR   r  rQ   rR   r>   rS   r@   rT   rB   rR   r   )rO   r~   r   r   r   r"   r   rE   r/   r@   r  propertyr  r   r1  rb   r{   r   rH   rH   r   rI   r6     s,    $-

-r6   c                   @  sV   e Zd ZdZejZ			dd ddZd!ddZej	j
d"ddZ				d#d$ddZdS )%r7   a  
    Samples a list of `num_samples` image patches according to the provided `weight_map`.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        spatial_size: the spatial size of the image patch e.g. [224, 224, 128].
            If its components have non-positive values, the corresponding size of `img` will be used.
        num_samples: number of samples (image patches) to take in the returned list.
        weight_map: weight map used to generate patch samples. The weights must be non-negative.
            Each element denotes a sampling weight of the spatial location. 0 indicates no sampling.
            It should be a single-channel array in shape, for example, `(1, spatial_dim_0, spatial_dim_1, ...)`.
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
    rU   NFr   r   r   r   
weight_mapNdarrayOrTensor | Noner@   rA   c                 C  s0   t | | t|| _t|| _|| _g | _d S rD   )r   rE   r'   r   r   r   r7  centers)rG   r   r   r7  r@   rH   rH   rI   rE     s
   


zRandWeightedCrop.__init__r
   rB   rC   c                 C  s    t | j|d | j| jd| _d S )Nr   )r   w	n_samplesZr_state)r   r   r   r   r9  )rG   r7  rH   rH   rI   r     s   zRandWeightedCrop.randomizer  c                 C  
   || _ d S rD   r   r  rH   rH   rI   r@        
zRandWeightedCrop.lazyTrQ   rR   r   rT   r   c                 C  s  t |tr	| n|jdd }|rF|du r| j}|du r!tdt |tr*| n|jdd }||krAtd| d| d | | t	| j
|}g }|du rU| jn|}	t| jD ],\}
}t|||	d}||}t r|}|
|jtj< ||jd< | j|d	|	d
 || q\|S )a  
        Args:
            img: input image to sample patches from. assuming `img` is a channel-first array.
            weight_map: weight map used to generate patch samples. The weights must be non-negative.
                Each element denotes a sampling weight of the spatial location. 0 indicates no sampling.
                It should be a single-channel array in shape, for example, `(1, spatial_dim_0, spatial_dim_1, ...)`
            randomize: whether to execute random operations, default to `True`.
            lazy: a flag to override the lazy behaviour for this call, if set. Defaults to None.

        Returns:
            A list of image patches
        rU   Nz8weight map must be provided for weighted patch sampling.z-image and weight map spatial shape mismatch: z vs r   r   r   r@   crop_centerTr   )rY   r   rZ   r[   r7  r   warningswarnr   r)   r   r@   r   r9  r0   r   r  r  r  r  r   )rG   rQ   r7  r   r@   	img_shapeZw_shapeZ_spatial_sizeresultsra   rg   centerrz   r   ret_rH   rH   rI   rb     s.     

zRandWeightedCrop.__call__)rU   NF)r   r   r   r   r7  r8  r@   rA   )r7  r
   rB   rC   r4  )NTN)
rQ   rR   r7  r8  r   rA   r@   rT   rB   r   )rO   r~   r   r   r0   r   rE   r   r   r@   r  rb   rH   rH   rH   rI   r7     s    
r7   c                   @  s   e Zd ZdZejZ										d+d,ddZ				d-d.ddZej	j
d/dd Z	ed!d" Z					#	d0d1d)d*ZdS )2r8   aL  
    Crop random fixed sized regions with the center being a foreground or background voxel
    based on the Pos Neg Ratio.
    And will return a list of arrays for all the cropped images.
    For example, crop two (3 x 3) arrays from (5 x 5) array with pos/neg=1::

        [[[0, 0, 0, 0, 0],
          [0, 1, 2, 1, 0],            [[0, 1, 2],     [[2, 1, 0],
          [0, 1, 3, 0, 0],     -->     [0, 1, 3],      [3, 0, 0],
          [0, 0, 0, 0, 0],             [0, 0, 0]]      [0, 0, 0]]
          [0, 0, 0, 0, 0]]]

    If a dimension of the expected spatial size is larger than the input image size,
    will not crop that dimension. So the cropped result may be smaller than expected size, and the cropped
    results of several images may not have exactly same shape.
    And if the crop ROI is partly out of the image, will automatically adjust the crop center to ensure the
    valid crop ROI.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        spatial_size: the spatial size of the crop region e.g. [224, 224, 128].
            if a dimension of ROI size is larger than image size, will not crop that dimension of the image.
            if its components have non-positive values, the corresponding size of `label` will be used.
            for example: if the spatial size of input data is [40, 40, 40] and `spatial_size=[32, 64, -1]`,
            the spatial size of output data will be [32, 40, 40].
        label: the label image that is used for finding foreground/background, if None, must set at
            `self.__call__`.  Non-zero indicates foreground, zero indicates background.
        pos: used with `neg` together to calculate the ratio ``pos / (pos + neg)`` for the probability
            to pick a foreground voxel as a center rather than a background voxel.
        neg: used with `pos` together to calculate the ratio ``pos / (pos + neg)`` for the probability
            to pick a foreground voxel as a center rather than a background voxel.
        num_samples: number of samples (crop regions) to take in each list.
        image: optional image data to help select valid area, can be same as `img` or another image array.
            if not None, use ``label == 0 & image > image_threshold`` to select the negative
            sample (background) center. So the crop center will only come from the valid image areas.
        image_threshold: if enabled `image`, use ``image > image_threshold`` to determine
            the valid image content areas.
        fg_indices: if provided pre-computed foreground indices of `label`, will ignore above `image` and
            `image_threshold`, and randomly select crop centers based on them, need to provide `fg_indices`
            and `bg_indices` together, expect to be 1 dim array of spatial indices after flattening.
            a typical usage is to call `FgBgToIndices` transform first and cache the results.
        bg_indices: if provided pre-computed background indices of `label`, will ignore above `image` and
            `image_threshold`, and randomly select crop centers based on them, need to provide `fg_indices`
            and `bg_indices` together, expect to be 1 dim array of spatial indices after flattening.
            a typical usage is to call `FgBgToIndices` transform first and cache the results.
        allow_smaller: if `False`, an exception will be raised if the image is smaller than
            the requested ROI in any dimension. If `True`, any smaller dimensions will be set to
            match the cropped size (i.e., no cropping in that dimension).
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.

    Raises:
        ValueError: When ``pos`` or ``neg`` are negative.
        ValueError: When ``pos=0`` and ``neg=0``. Incompatible values.

    N      ?rU           Fr   r   labeltorch.Tensor | Noneposfloatnegr   r   imageimage_threshold
fg_indicesr8  
bg_indicesr  rA   r@   rB   rC   c                 C  s   t | | || _|| _|dk s|dk rtd| d| d|| dkr)td|||  | _|| _|| _|| _d | _	|| _
|	| _|
| _d S )Nr   z)pos and neg must be nonnegative, got pos=z neg=r   z%Incompatible values: pos=0 and neg=0.)r   rE   r   rI  r   	pos_ratior   rN  rO  r9  rP  rQ  r  )rG   r   rI  rK  rM  r   rN  rO  rP  rQ  r  r@   rH   rH   rI   rE   N  s   
zRandCropByPosNegLabel.__init__c              	   C  s   |d u r| j n|}|d u r| jn|}|d u s|d u r+|d u r"tdt||| j\}}d }|d urBt|tr:| n|jdd  }n|d urVt|trO| n|jdd  }|d u r^tdt	| j
| j| j|||| j| j| _d S )Nzlabel must be provided.rU   z9label or image must be provided to get the spatial shape.)rP  rQ  r   r   rO  rY   r   rZ   r[   r   r   r   rR  r   r  r9  )rG   rI  rP  rQ  rN  Zfg_indices_Zbg_indices__shaperH   rH   rI   r   l  s.   " 
zRandCropByPosNegLabel.randomizer  c                 C  r<  rD   r=  r  rH   rH   rI   r@     r>  zRandCropByPosNegLabel.lazyc                 C  r  r  rH   r  rH   rH   rI   r    r  z+RandCropByPosNegLabel.requires_current_dataTrQ   rR   r   rT   r   c                 C  s   |du r| j }|r|du r| j}| |||| g }| jdurqt|tr(| n|jdd }	t| j	|	d}
|du r=| j
n|}t| jD ],\}}t||
|d}||}t rk|}||jtj< ||jd< | j|d|d || qD|S )a@  
        Args:
            img: input data to crop samples from based on the pos/neg ratio of `label` and `image`.
                Assumes `img` is a channel-first array.
            label: the label image that is used for finding foreground/background, if None, use `self.label`.
            image: optional image data to help select valid area, can be same as `img` or another image array.
                use ``label == 0 & image > image_threshold`` to select the negative sample(background) center.
                so the crop center will only exist on valid image area. if None, use `self.image`.
            fg_indices: foreground indices to randomly select crop centers,
                need to provide `fg_indices` and `bg_indices` together.
            bg_indices: background indices to randomly select crop centers,
                need to provide `fg_indices` and `bg_indices` together.
            randomize: whether to execute the random operations, default to `True`.
            lazy: a flag to override the lazy behaviour for this call, if set. Defaults to None.

        NrU   defaultr?  r@  Tr   )rN  rI  r   r9  rY   r   rZ   r[   r)   r   r@   r   r0   r   r  r  r  r  r   )rG   rQ   rI  rN  rP  rQ  r   r@   rD  rC  r   ra   rg   rE  rz   r   rF  rH   rH   rI   rb     s*   
 
zRandCropByPosNegLabel.__call__)
NrG  rG  rU   NrH  NNFF)r   r   rI  rJ  rK  rL  rM  rL  r   r   rN  rJ  rO  rL  rP  r8  rQ  r8  r  rA   r@   rA   rB   rC   )NNNN)
rI  rJ  rP  r8  rQ  r8  rN  rJ  rB   rC   r4  )NNNNTN)rQ   rR   rI  rJ  rN  rJ  rP  r8  rQ  r8  r   rA   r@   rT   rB   r   rO   r~   r   r   r0   r   rE   r   r   r@   r  r6  r  rb   rH   rH   rH   rI   r8     s<    : 
r8   c                   @  s~   e Zd ZdZejZ											d-d.ddZ			d/d0dd Zej	j
d1d"d#Z	ed$d% Z					d2d3d+d,ZdS )4r9   a)  
    Crop random fixed sized regions with the center being a class based on the specified ratios of every class.
    The label data can be One-Hot format array or Argmax data. And will return a list of arrays for all the
    cropped images. For example, crop two (3 x 3) arrays from (5 x 5) array with `ratios=[1, 2, 3, 1]`::

        image = np.array([
            [[0.0, 0.3, 0.4, 0.2, 0.0],
            [0.0, 0.1, 0.2, 0.1, 0.4],
            [0.0, 0.3, 0.5, 0.2, 0.0],
            [0.1, 0.2, 0.1, 0.1, 0.0],
            [0.0, 0.1, 0.2, 0.1, 0.0]]
        ])
        label = np.array([
            [[0, 0, 0, 0, 0],
            [0, 1, 2, 1, 0],
            [0, 1, 3, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0]]
        ])
        cropper = RandCropByLabelClasses(
            spatial_size=[3, 3],
            ratios=[1, 2, 3, 1],
            num_classes=4,
            num_samples=2,
        )
        label_samples = cropper(img=label, label=label, image=image)

        The 2 randomly cropped samples of `label` can be:
        [[0, 1, 2],     [[0, 0, 0],
         [0, 1, 3],      [1, 2, 1],
         [0, 0, 0]]      [1, 3, 0]]

    If a dimension of the expected spatial size is larger than the input image size,
    will not crop that dimension. So the cropped result may be smaller than expected size, and the cropped
    results of several images may not have exactly same shape.
    And if the crop ROI is partly out of the image, will automatically adjust the crop center to ensure the
    valid crop ROI.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        spatial_size: the spatial size of the crop region e.g. [224, 224, 128].
            if a dimension of ROI size is larger than image size, will not crop that dimension of the image.
            if its components have non-positive values, the corresponding size of `label` will be used.
            for example: if the spatial size of input data is [40, 40, 40] and `spatial_size=[32, 64, -1]`,
            the spatial size of output data will be [32, 40, 40].
        ratios: specified ratios of every class in the label to generate crop centers, including background class.
            if None, every class will have the same ratio to generate crop centers.
        label: the label image that is used for finding every class, if None, must set at `self.__call__`.
        num_classes: number of classes for argmax label, not necessary for One-Hot label.
        num_samples: number of samples (crop regions) to take in each list.
        image: if image is not None, only return the indices of every class that are within the valid
            region of the image (``image > image_threshold``).
        image_threshold: if enabled `image`, use ``image > image_threshold`` to
            determine the valid image content area and select class indices only in this area.
        indices: if provided pre-computed indices of every class, will ignore above `image` and
            `image_threshold`, and randomly select crop centers based on them, expect to be 1 dim array
            of spatial indices after flattening. a typical usage is to call `ClassesToIndices` transform first
            and cache the results for better performance.
        allow_smaller: if `False`, an exception will be raised if the image is smaller than
            the requested ROI in any dimension. If `True`, any smaller dimensions will remain
            unchanged.
        warn: if `True` prints a warning if a class is not present in the label.
        max_samples_per_class: maximum length of indices to sample in each class to reduce memory consumption.
            Default is None, no subsampling.
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
    NrU   rH  FTr   r   ratioslist[float | int] | NonerI  rJ  num_classesr   r   r   rN  rO  rL  indiceslist[NdarrayOrTensor] | Noner  rA   rB  max_samples_per_classr@   rB   rC   c                 C  sX   t | | || _|| _|| _|| _|| _|| _|| _d | _	|| _
|	| _|
| _|| _d S rD   )r   rE   r   rW  rI  rY  r   rN  rO  r9  rZ  r  rB  r\  )rG   r   rW  rI  rY  r   rN  rO  rZ  r  rB  r\  r@   rH   rH   rI   rE     s   
zRandCropByLabelClasses.__init__c              	   C  s   |d u r| j n|}|d u r |d u rtdt|| j|| j| j}d }|d ur7t|tr/| n|j	dd  }n|d urKt|trD| n|j	dd  }|d u rStdt
| j| j||| j| j| j| j| _d S )Nzlabel must not be None.rU   zBlabel or image must be provided to infer the output spatial shape.)rZ  r   r   rY  rO  r\  rY   r   rZ   r[   r   r   r   rW  r   r  rB  r9  )rG   rI  rZ  rN  Zindices_rS  rH   rH   rI   r   )  s"   " 
z RandCropByLabelClasses.randomizer  c                 C  r<  rD   r=  r  rH   rH   rI   r@   A  r>  zRandCropByLabelClasses.lazyc                 C  r  r  rH   r  rH   rH   rI   r  E  r  z,RandCropByLabelClasses.requires_current_datarQ   rR   r   rT   r   c                 C  s   |du r| j }|r|du r| j}| ||| g }| jdurrt|tr'| n|jdd }t| j	|d}	|du r<| j
n|}
t| jD ].\}}tt||	|
d}||}t rl|}||jtj< ||jd< | j|d|
d || qC|S )a  
        Args:
            img: input data to crop samples from based on the ratios of every class, assumes `img` is a
                channel-first array.
            label: the label image that is used for finding indices of every class, if None, use `self.label`.
            image: optional image data to help select valid area, can be same as `img` or another image array.
                use ``image > image_threshold`` to select the centers only in valid region. if None, use `self.image`.
            indices: list of indices for every class in the image, used to randomly select crop centers.
            randomize: whether to execute the random operations, default to `True`.
            lazy: a flag to override the lazy behaviour for this call, if set. Defaults to None.
        NrU   rT  r?  r@  Tr   )rN  rI  r   r9  rY   r   rZ   r[   r)   r   r@   r   r0   r   r   r  r  r  r  r   )rG   rQ   rI  rN  rZ  r   r@   rD  rC  r   ra   rg   rE  rz   r   rF  rH   rH   rI   rb   I  s*   
 
zRandCropByLabelClasses.__call__)NNNrU   NrH  NFTNF)r   r   rW  rX  rI  rJ  rY  r   r   r   rN  rJ  rO  rL  rZ  r[  r  rA   rB  rA   r\  r   r@   rA   rB   rC   r}   )rI  rJ  rZ  r[  rN  rJ  rB   rC   r4  )NNNTN)rQ   rR   rI  rJ  rN  rJ  rZ  r[  r   rA   r@   rT   rB   r   rV  rH   rH   rH   rI   r9     s:    E
r9   c                   @  sr   e Zd ZdZeeejeej@ Ze	j
ejdfdd
dZejjdddZ	ddddZd ddZd ddZdS )!r:   a  
    Resize an image to a target spatial size by either centrally cropping the image or
    padding it evenly with a user-specified mode.
    When the dimension is smaller than the target size, do symmetric padding along that dim.
    When the dimension is larger than the target size, do central cropping along that dim.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.

    Args:
        spatial_size: the spatial size of output data after padding or crop.
            If has non-positive values, the corresponding size of input image will be used (no padding).
        method: {``"symmetric"``, ``"end"``}
            Pad image symmetrically on every side or only pad at the end sides. Defaults to ``"symmetric"``.
        mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
            ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
            available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
            One of the listed string values or a user supplied function. Defaults to ``"constant"``.
            See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
            https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
        pad_kwargs: other arguments for the `np.pad` or `torch.pad` function.
            note that `np.pad` treats channel dimension as the first dimension.
        lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.

    Fr   r   r   r?   r>   r@   rA   c                 K  s8   t | | td||||d|| _t||d| _d S )N)r   r   r>   r@   r   rH   )r   rE   r,   r  r1   rz   )rG   r   r   r>   r@   r  rH   rH   rI   rE     s   zResizeWithPadOrCrop.__init__valc                 C  s   || j _|| j_|| _d S rD   )r  r@   rz   r   )rG   r]  rH   rH   rI   r@     s   
zResizeWithPadOrCrop.lazyNrQ   rR   rS   rT   rB   c                 K  s   |du r| j n|}| j| ||f||d|}t rg|}|s>|j }|j }	|	tj}
| j	||
||	d|d |S |j
 }|j
 }	|	tj}
| j	||
|tj |	tj |tj  ||	d|d |S )a   
        Args:
            img: data to pad or crop, assuming `img` is channel-first and
                padding or cropping doesn't apply to the channel dim.
            mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
                ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
                available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
                One of the listed string values or a user supplied function. Defaults to ``"constant"``.
                See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
                https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
            lazy: a flag to override the lazy behaviour for this call, if set. Defaults to None.
            pad_kwargs: other arguments for the `np.pad` or `torch.pad` function.
                note that `np.pad` treats channel dimension as the first dimension.

        Nr   )r"  r/  )r#  r&  r@   )r#  r$  r%  r&  r@   )r@   r  rz   r   r'  r(  r*  r#   r+  r  r)  r    r,  r-  )rG   rQ   r>   r@   r  ra   r  rF  r"  r/  r#  rH   rH   rI   rb     s0    



	zResizeWithPadOrCrop.__call__r   c                 C  s   |  |}| ||S rD   )rp   r   )rG   rQ   rw   rH   rH   rI   r{     s   
zResizeWithPadOrCrop.inversec                 C  sP   |t j d}|t j d}|j| |j| | j|}| j|S )Nr/  r"  )r#   rq   r(  r'  r   r  r{   rz   )rG   rQ   rw   r/  r"  r3  rH   rH   rI   r     s   z%ResizeWithPadOrCrop.inverse_transform)r   r   r   r?   r>   r?   r@   rA   )r]  rA   r  r5  r   )rO   r~   r   r   r   setr,   r   r1   r!   r   r"   r   rE   r   r@   r  rb   r{   r   rH   rH   rH   rI   r:   u  s    
-r:   c                   @  s4   e Zd ZdZejejgZefdddZ	dddZ
dS )r;   a  
    Compute coordinates of axis-aligned bounding rectangles from input image `img`.
    The output format of the coordinates is (shape is [channel, 2 * spatial dims]):

        [[1st_spatial_dim_start, 1st_spatial_dim_end,
         2nd_spatial_dim_start, 2nd_spatial_dim_end,
         ...,
         Nth_spatial_dim_start, Nth_spatial_dim_end],

         ...

         [1st_spatial_dim_start, 1st_spatial_dim_end,
         2nd_spatial_dim_start, 2nd_spatial_dim_end,
         ...,
         Nth_spatial_dim_start, Nth_spatial_dim_end]]

    The bounding boxes edges are aligned with the input image edges.
    This function returns [0, 0, ...] if there's no positive intensity.

    Args:
        select_fn: function to select expected foreground, default is to select values > 0.
    r  r   rB   rC   c                 C  r<  rD   )r  )rG   r  rH   rH   rI   rE     s   
zBoundingRect.__init__rQ   r
   r!  c                 C  sT   g }t |jd D ]}t|| j|d\}}|dd t||D  q	tj|ddS )z]
        See also: :py:class:`monai.transforms.utils.generate_spatial_bounding_box`.
        r   )r  r	  c                 S  s   g | ]	}|D ]}|qqS rH   rH   )rf   r   rg   rH   rH   rI   rh     s    z)BoundingRect.__call__.<locals>.<listcomp>)axis)r   r[   r   r  r   ru   r  stack)rG   rQ   bboxchannelstart_end_rH   rH   rI   rb     s
   zBoundingRect.__call__N)r  r   rB   rC   )rQ   r
   rB   r!  )rO   r~   r   r   r$   r   r   r   r   rE   rb   rH   rH   rH   rI   r;     s
    r;   )Pr   
__future__r   rA  collections.abcr   r   	itertoolsr   mathr   typingr   numpyr  r   Zmonai.configr	   monai.config.type_definitionsr
   monai.data.meta_objr   monai.data.meta_tensorr   monai.data.utilsr   r   #monai.transforms.croppad.functionalr   r   monai.transforms.inverser   r   monai.transforms.traitsr   monai.transforms.transformr   r   r   monai.transforms.utilsr   r   r   r   r   r   r   r   monai.utilsr   r  r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   __all__r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   rH   rH   rH   rI   <module>   sT   
(
4e::/\5*I>S 3W 4 2l