o
    i                    @  s|  d 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
 ddlZddlZddlmZmZmZ ddlm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mZ ddlm Z  ddl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z; ddl<m=Z=m>Z>m?Z? ddl@mAZA ddlBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJ ddlKmLZL ddlMmNZN ddlOmPZP ePd\ZQZRg dZSG dd de>e e=ZTG dd de>e e=ZUG dd de>e e=ZVG dd de>e e=ZWG d d! d!e>e e=ZXG d"d# d#e?e>e e=ZYG d$d% d%e>e e=ZZG d&d' d'e>e e=Z[G d(d) d)e?e>e e=Z\G d*d+ d+e?e>Z]G d,d- d-e?e>Z^G d.d/ d/e>e e=Z_G d0d1 d1e?e>e e=Z`G d2d3 d3e?e>e e=ZaG d4d5 d5e>e e=ZbG d6d7 d7e?e>e e=ZcG d8d9 d9e>e e=ZdG d:d; d;e?e>e e=ZeG d<d= d=e>ZfG d>d? d?e?e>ZgG d@dA dAe>e;ZhG dBdC dCe>e;ZiG dDdE dEe?e>e;ZjG dFdG dGe?e>ZkG dHdI dIe>ZlG dJdK dKe>ZmeT ZnZoeU ZpZqeV ZrZseW ZtZueX ZvZweY ZxZyeZ ZzZ{e[ Z|Z}e\ Z~Ze] ZZe^ ZZe_ ZZe` ZZef ZZeg ZZea ZZeb ZZec ZZed ZZee ZZeh ZZei ZZej ZZek ZZel ZZem ZZdS )Lz
A collection of dictionary-based wrappers around the "vanilla" transforms for spatial operations
defined in :py:class:`monai.transforms.spatial.array`.

Class names are ended with 'd' to denote dictionary-based transforms.
    )annotationsN)HashableMappingSequence)Anycast)	DtypeLikeKeysCollectionSequenceStr)NdarrayOrTensor)BoxModeStandardMode)get_track_meta)
MetaTensor)GaussianFilter)CenterSpatialCrop)InvertibleTransform)AffineConvertBoxToPointsConvertPointsToBoxesFlipGridDistortion	GridPatch	GridSplitOrientationRand2DElasticRand3DElastic
RandAffineRandAxisFlipRandGridDistortionRandGridPatch
RandRotateRandSimulateLowResolutionRandZoomResampleToMatchResizeRotateRotate90SpacingSpatialResampleZoom)MultiSampleTrait)LazyTransformMapTransformRandomizableTransform)create_grid)GridSampleModeGridSamplePadModeInterpolateModeNumpyPadModeconvert_to_tensorensure_tupleensure_tuple_repfall_back_tuple)deprecated_arg_default)	TraceKeys)optional_importnibabel)FSpatialResampledResampleToMatchdSpacingdOrientationd	Rotate90dRandRotate90dResizedAffinedRandAffinedRand2DElasticdRand3DElasticdFlipd	RandFlipdGridDistortiondRandGridDistortiondRandAxisFlipdRotatedRandRotatedZoomd	RandZoomdSpatialResampleDSpatialResampleDictSpacingDSpacingDictOrientationDOrientationDict	Rotate90DRotate90DictRandRotate90DRandRotate90DictResizeD
ResizeDictAffineD
AffineDictRandAffineDRandAffineDictRand2DElasticDRand2DElasticDictRand3DElasticDRand3DElasticDictFlipDFlipDict	RandFlipDRandFlipDictGridDistortionDGridDistortionDictRandGridDistortionDRandGridDistortionDictRandAxisFlipDRandAxisFlipDictRotateD
RotateDictRandRotateDRandRotateDictZoomDZoomDict	RandZoomDRandZoomDict
GridSplitd
GridSplitDGridSplitDict
GridPatchd
GridPatchDGridPatchDictRandGridPatchdRandGridPatchDRandGridPatchDictRandSimulateLowResolutiondRandSimulateLowResolutionDRandSimulateLowResolutionDictc                   @  s^   e Zd ZdZejZejej	de
jdddfd"ddZejjd#ddZd$d%ddZd&d d!ZdS )'r<   a  
    Dictionary-based wrapper of :py:class:`monai.transforms.SpatialResample`.

    This transform assumes the ``data`` dictionary has a key for the input
    data's metadata and contains ``src_affine`` and ``dst_affine`` required by
    `SpatialResample`. The key is formed by ``key_{meta_key_postfix}``.  The
    transform will swap ``src_affine`` and ``dst_affine`` affine (with potential data type
    changes) in the dictionary so that ``src_affine`` always refers to the current
    status of affine.

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

    See also:
        :py:class:`monai.transforms.SpatialResample`
    F
dst_affinekeysr	   moder
   padding_modealign_cornersSequence[bool] | booldtypeSequence[DtypeLike] | DtypeLikedst_keysKeysCollection | Noneallow_missing_keysboollazyreturnNonec	           	      C  s   t | || tj| |d t|d| _t|t| j| _t|t| j| _	t|t| j| _
t|t| j| _t|t| j| _dS )aN	  
        Args:
            keys: keys of the corresponding items to be transformed.
            mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers).
                Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used
                and the value represents the order of the spline interpolation.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
                Padding mode for outside grid values. Defaults to ``"border"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When `mode` is an integer, using numpy/cupy backends, this argument accepts
                {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            align_corners: Geometrically, we consider the pixels of the input as squares rather than points.
                See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
                It also can be a sequence of bool, each element corresponds to a key in ``keys``.
            dtype: data type for resampling computation. Defaults to ``float64`` for best precision.
                If None, use the data type of input data. To be compatible with other modules,
                the output data type is always ``float32``.
                It also can be a sequence of dtypes, each element corresponds to a key in ``keys``.
            dst_keys: the key of the corresponding ``dst_affine`` in the metadata dictionary.
            allow_missing_keys: don't raise exception if key is missing.
            lazy: a flag to indicate whether this transform should execute lazily or not.
                Defaults to False.
        r   N)r-   __init__r,   r)   sp_transformr6   lenr   r   r   r   r   r   )	selfr   r   r   r   r   r   r   r    r   e/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/transforms/spatial/dictionary.pyr      s   (zSpatialResampled.__init__valc                 C     || _ || j_d S N)_lazyr   r   r   r   r   r   r   r         zSpatialResampled.lazyNdataMapping[Hashable, torch.Tensor]bool | Nonedict[Hashable, torch.Tensor]c                 C  st   |du r| j n|}t|}| || j| j| j| j| jD ]\}}}}}	}
| j|| ||
 d||||	|d||< q|S )  
        Args:
            data: a dictionary containing the tensor-like data to be processed. The ``keys`` specified
                in this dictionary must be tensor like arrays that are channel first and have at most
                three spatial dimensions
            lazy: a flag to indicate whether this transform should execute lazily or not
                during this call. Setting this to False or True overrides the ``lazy`` flag set
                during initialization for this call. Defaults to None.

        Returns:
            a dictionary containing the transformed data, as well as any other data present in the dictionary
        N)imgr   spatial_sizer   r   r   r   r   )	r   dictkey_iteratorr   r   r   r   r   r   )r   r   r   lazy_dkeyr   r   r   r   dst_keyr   r   r   __call__   s    
zSpatialResampled.__call__c                 C  0   t |}| |D ]}| j|| ||< q	|S r   )r   r   r   inverser   r   r   r   r   r   r   r        zSpatialResampled.inverse)r   r	   r   r
   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__r)   backendr0   BILINEARr1   BORDERnpfloat64r   r,   r   setterr   r   r   r   r   r   r<      s    1r<   c                   @  s\   e Zd ZdZejZejej	de
jddfd!ddZejjd"ddZd#d$ddZd%dd ZdS )&r=   z
    Dictionary-based wrapper of :py:class:`monai.transforms.ResampleToMatch`.

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

    Fr   r	   key_dststrr   r
   r   r   r   r   r   r   r   r   c	           	      C  sz   t | || tj| |d || _t|t| j| _t|t| j| _t|t| j| _	t|t| j| _
t|d| _dS )a'	  
        Args:
            keys: keys of the corresponding items to be transformed.
            key_dst: key of image to resample to match.
            mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers).
                Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used
                and the value represents the order of the spline interpolation.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
                Padding mode for outside grid values. Defaults to ``"border"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When `mode` is an integer, using numpy/cupy backends, this argument accepts
                {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            align_corners: Geometrically, we consider the pixels of the input as squares rather than points.
                See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
                It also can be a sequence of bool, each element corresponds to a key in ``keys``.
            dtype: data type for resampling computation. Defaults to ``float64`` for best precision.
                If None, use the data type of input data. To be compatible with other modules,
                the output data type is always ``float32``.
                It also can be a sequence of dtypes, each element corresponds to a key in ``keys``.
            allow_missing_keys: don't raise exception if key is missing.
            lazy: a flag to indicate whether this transform should execute lazily or not.
                Defaults to False
        r   N)r-   r   r,   r   r6   r   r   r   r   r   r   r$   	resampler)	r   r   r   r   r   r   r   r   r   r   r   r   r     s   (zResampleToMatchd.__init__r   r   r   c                 C  r   r   )r   r   r   r   r   r   r   r   E  r   zResampleToMatchd.lazyNr   r   r   r   c           
   
   C  sn   |du r| j n|}t|}| || j| j| j| jD ]\}}}}}	| j|| || j ||||	|d||< q|S )r   N)r   img_dstr   r   r   r   r   )	r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   r   r   r   r   J  s   	zResampleToMatchd.__call__c                 C  r   r   )r   r   r   r   r   r   r   r   r   g  r   zResampleToMatchd.inverse)r   r	   r   r   r   r
   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r$   r   r0   r   r1   r   r   r   r   r,   r   r   r   r   r   r   r   r   r=   	  s    1r=   c                   @  sh   e Zd ZdZejZdejej	de
jdddddddfd+ddZejjd,dd Zd-d.d%d&Zd/d)d*ZdS )0r>   a]  
    Dictionary-based wrapper of :py:class:`monai.transforms.Spacing`.

    This transform assumes the ``data`` dictionary has a key for the input
    data's metadata and contains `affine` field.  The key is formed by ``key_{meta_key_postfix}``.

    After resampling the input array, this transform will write the new affine
    to the `affine` field of metadata which is formed by ``key_{meta_key_postfix}``.

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

    see also:
        :py:class:`monai.transforms.Spacing`
    FNTr   r	   pixdimSequence[float] | floatdiagonalr   r   r
   r   r   r   r   r   scale_extentrecompute_affine
min_pixdimSequence[float] | float | None
max_pixdimensure_same_shaper   r   r   r   c                 C  s   t | || tj| |d t|||	|
||d| _t|t| j| _t|t| j| _	t|t| j| _
t|t| j| _t|t| j| _|| _dS )a  
        Args:
            pixdim: output voxel spacing. if providing a single number, will use it for the first dimension.
                items of the pixdim sequence map to the spatial dimensions of input image, if length
                of pixdim sequence is longer than image spatial dimensions, will ignore the longer part,
                if shorter, will pad with `1.0`.
                if the components of the `pixdim` are non-positive values, the transform will use the
                corresponding components of the original pixdim, which is computed from the `affine`
                matrix of input image.
            diagonal: whether to resample the input to have a diagonal affine matrix.
                If True, the input data is resampled to the following affine::

                    np.diag((pixdim_0, pixdim_1, pixdim_2, 1))

                This effectively resets the volume to the world coordinate system (RAS+ in nibabel).
                The original orientation, rotation, shearing are not preserved.

                If False, the axes orientation, orthogonal rotation and
                translations components from the original affine will be
                preserved in the target affine. This option will not flip/swap
                axes against the original ones.
            mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers).
                Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used
                and the value represents the order of the spline interpolation.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
                Padding mode for outside grid values. Defaults to ``"border"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When `mode` is an integer, using numpy/cupy backends, this argument accepts
                {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            align_corners: Geometrically, we consider the pixels of the input as squares rather than points.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                It also can be a sequence of bool, each element corresponds to a key in ``keys``.
            dtype: data type for resampling computation. Defaults to ``float64`` for best precision.
                If None, use the data type of input data. To be compatible with other modules,
                the output data type is always ``float32``.
                It also can be a sequence of dtypes, each element corresponds to a key in ``keys``.
            scale_extent: whether the scale is computed based on the spacing or the full extent of voxels,
                default False. The option is ignored if output spatial size is specified when calling this transform.
                See also: :py:func:`monai.data.utils.compute_shape_offset`. When this is True, `align_corners`
                should be `True` because `compute_shape_offset` already provides the corner alignment shift/scaling.
            recompute_affine: whether to recompute affine based on the output shape. The affine computed
                analytically does not reflect the potential quantization errors in terms of the output shape.
                Set this flag to True to recompute the output affine based on the actual pixdim. Default to ``False``.
            min_pixdim: minimal input spacing to be resampled. If provided, input image with a larger spacing than this
                value will be kept in its original spacing (not be resampled to `pixdim`). Set it to `None` to use the
                value of `pixdim`. Default to `None`.
            max_pixdim: maximal input spacing to be resampled. If provided, input image with a smaller spacing than this
                value will be kept in its original spacing (not be resampled to `pixdim`). Set it to `None` to use the
                value of `pixdim`. Default to `None`.
            ensure_same_shape: when the inputs have the same spatial shape, and almost the same pixdim,
                whether to ensure exactly the same output spatial shape.  Default to True.
            allow_missing_keys: don't raise exception if key is missing.
            lazy: a flag to indicate whether this transform should execute lazily or not.
                Defaults to False
        r   )r   r   r   r   r   N)r-   r   r,   r(   spacing_transformr6   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    N
zSpacingd.__init__r   c                 C  r   r   )r   r   r   r   r   r   r   r     r   zSpacingd.lazyr   r   r   r   c                 C  s"  t |}d\}}}d}|du r| jn|}| || j| j| j| j| jD ]k\}	}
}}}}| jr_t	||	 t
r_|du rJ|du rJ||	  ||	 j}}nt|||	  o^tj|||	 jdd}| j||	 |
|||||rm|nd|d||	< |du rt	||	 t
r||	  n||	 jdd }q#|S )r   )NNFNgMbP?)atol)
data_arrayr   r   r   r   r   output_spatial_shaper      )r   r   r   r   r   r   r   r   r   
isinstancer   peek_pending_shaper   r   allcloser   shape)r   r   r   r   _init_shape_pixdimZshould_matchZoutput_shape_kr   r   r   r   r   r   r   r   r   r   r     s6   



,zSpacingd.__call__"Mapping[Hashable, NdarrayOrTensor]dict[Hashable, NdarrayOrTensor]c                 C  s8   t |}| |D ]}| jttj|| ||< q	|S r   )r   r   r   r   r   torchTensorr   r   r   r   r     s   zSpacingd.inverse)r   r	   r   r   r   r   r   r
   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r(   r   r0   r   r1   r   r   r   r   r,   r   r   r   r   r   r   r   r   r>   n  s(    _+r>   c                   @  sd   e Zd ZdZejZeddddd					d d!ddZej	j
d"ddZ	d#d$ddZd%ddZdS )&r?   a  
    Dictionary-based wrapper of :py:class:`monai.transforms.Orientation`.

    This transform assumes the channel-first input format.
    In the case of using this transform for normalizing the orientations of images,
    it should be used before any anisotropic spatial transforms.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.
    labels))LR)PA)ISNzDefault value changed to None meaning that the transform now uses the 'space' of a meta-tensor, if applicable, to determine appropriate axis labels.)nameold_defaultnew_default
msg_suffixFr   r	   axcodes
str | Noneas_closest_canonicalr    Sequence[tuple[str, str]] | Noner   r   r   r   c                 C  s2   t | || tj| |d t||||d| _dS )a  
        Args:
            axcodes: N elements sequence for spatial ND input's orientation.
                e.g. axcodes='RAS' represents 3D orientation:
                (Left, Right), (Posterior, Anterior), (Inferior, Superior).
                default orientation labels options are: 'L' and 'R' for the first dimension,
                'P' and 'A' for the second, 'I' and 'S' for the third.
            as_closest_canonical: if True, load the image as closest to canonical axis format.
            labels: optional, None or sequence of (2,) sequences
                (2,) sequences are labels for (beginning, end) of output axis.
                If ``None``, an appropriate value is chosen depending on the
                value of the ``"space"`` metadata item of a metatensor: if
                ``"space"`` is ``"LPS"``, the value used is ``(('R', 'L'),
                ('A', 'P'), ('I', 'S'))``, if ``"space"`` is ``"RPS"`` or the
                input is not a meta-tensor or has no ``"space"`` item, the
                value ``(('L', 'R'), ('P', 'A'), ('I', 'S'))`` is used. If not
                ``None``, the provided value is always used and the ``"space"``
                metadata item (if any) of the input is ignored.
            allow_missing_keys: don't raise exception if key is missing.
            lazy: a flag to indicate whether this transform should execute lazily or not.
                Defaults to False

        See Also:
            `nibabel.orientations.ornt2axcodes`.

        r   )r   r   r   r   N)r-   r   r,   r   ornt_transform)r   r   r   r   r   r   r   r   r   r   r   %  s
   ,zOrientationd.__init__r   c                 C  r   r   )r   r   r   r   r   r   r   r   W  r   zOrientationd.lazyr   r   r   r   c                 C  D   t |}|du r| jn|}| |D ]}| j|| |d||< q|S r   Nr   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   \  
   zOrientationd.__call__c                 C  r   r   )r   r   r   r   r   r   r   r   r   o  r   zOrientationd.inverse)NFNFF)r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r8   r   r,   r   r   r   r   r   r   r   r   r?     s&    )r?   c                   @  sR   e Zd ZdZejZ				ddddZejj	d ddZd!d"ddZ
d#ddZdS )$r@   z
    Dictionary-based wrapper of :py:class:`monai.transforms.Rotate90`.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.
    r   r   r   Fr   r	   kintspatial_axestuple[int, int]r   r   r   r   r   c                 C  s0   t | || tj| |d t|||d| _dS )a  
        Args:
            k: number of times to rotate by 90 degrees.
            spatial_axes: 2 int numbers, defines the plane to rotate with 2 spatial axes.
                Default: (0, 1), this is the first two axis in spatial dimensions.
            allow_missing_keys: don't raise exception if key is missing.
            lazy: a flag to indicate whether this transform should execute lazily or not.
                Defaults to False
        r   N)r-   r   r,   r'   rotator)r   r   r   r   r   r   r   r   r   r     s   zRotate90d.__init__r   c                 C  r   r   )r   r   r   r   r   r   r   r     r   zRotate90d.lazyNr   r   r   r   c                 C  r   r   )r   r   r   r   r   r   r   r   r     r   zRotate90d.__call__c                 C  r   r   r   r   r   r   r   r   r   r   r     r   zRotate90d.inverse)r   r   FF)r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r'   r   r   r,   r   r   r   r   r   r   r   r   r@   v  s    r@   c                      sX   e Zd ZdZejZ					d!d"ddZd#d$ fddZ	d#d%ddZd&dd Z	  Z
S )'rA   aA  
    Dictionary-based version :py:class:`monai.transforms.RandRotate90`.
    With probability `prob`, input arrays are rotated by 90 degrees
    in the plane specified by `spatial_axes`.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.
    皙?   r   Fr   r	   probfloatmax_kr   r   r   r   r   r   r   r   c                 C  s>   t | || t| | tj| |d || _|| _d| _dS )a  
        Args:
            keys: keys of the corresponding items to be transformed.
                See also: :py:class:`monai.transforms.compose.MapTransform`
            prob: probability of rotating.
                (Default 0.1, with 10% probability it returns a rotated array.)
            max_k: number of rotations will be sampled from `np.random.randint(max_k) + 1`.
                (Default 3)
            spatial_axes: 2 int numbers, defines the plane to rotate with 2 spatial axes.
                Default: (0, 1), this is the first two axis in spatial dimensions.
            allow_missing_keys: don't raise exception if key is missing.
            lazy: a flag to indicate whether this transform should execute lazily or not.
                Defaults to False
        r   r   N)r-   r   r.   r,   r   r   _rand_k)r   r   r   r   r   r   r   r   r   r   r     s   
zRandRotate90d.__init__Nr   
Any | Nonec                   s$   | j | jd | _t d  d S )Nr   )r   randintr   r   super	randomize)r   r   	__class__r   r   r     s   zRandRotate90d.randomizer   r   c                 C  s   |    t|}|du r| jn|}t| j| j|d}| |D ] }| jr*||| nt|| t	 d||< | j
|| d|d q|S r   Nr   
track_metaTreplacer   )r   r   r   r'   r   r   r   _do_transformr4   r   push_transform)r   r   r   r   r   r   r   r   r   r   r     s   (zRandRotate90d.__call__r   c                 C  s`   t |}| |D ]$}t|| tsq	| || }|tj r-t || |tj	 ||< q	|S r   )
r   r   r   r   pop_transformr9   DO_TRANSFORMr'   inverse_transform
EXTRA_INFOr   r   r   r   xformr   r   r   r     s   
zRandRotate90d.inverse)r   r   r   FF)r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   )r   r   r   r   r   r   r   )r   r   r   r   r'   r   r   r   r   r   __classcell__r   r   r   r   rA     s    	 rA   c                   @  s^   e Zd ZdZejZdejdddej	ddfd'ddZ
ejjd(ddZd)d*d#d$Zd+d%d&ZdS ),rB   a7  
    Dictionary-based wrapper of :py:class:`monai.transforms.Resize`.

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

    Args:
        keys: keys of the corresponding items to be transformed.
            See also: :py:class:`monai.transforms.compose.MapTransform`
        spatial_size: expected shape of spatial dimensions after resize operation.
            if some components of the `spatial_size` are non-positive values, the transform will use the
            corresponding components of img size. For example, `spatial_size=(32, -1)` will be adapted
            to `(32, 64)` if the second spatial dimension size of img is `64`.
        size_mode: should be "all" or "longest", if "all", will use `spatial_size` for all the spatial dims,
            if "longest", rescale the image so that only the longest side is equal to specified `spatial_size`,
            which must be an int number in this case, keeping the aspect ratio of the initial image, refer to:
            https://albumentations.ai/docs/api_reference/augmentations/geometric/resize/
            #albumentations.augmentations.geometric.resize.LongestMaxSize.
        mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``}
            The interpolation mode. Defaults to ``"area"``.
            See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html
            It also can be a sequence of string, each element corresponds to a key in ``keys``.
        align_corners: This only has an effect when mode is
            'linear', 'bilinear', 'bicubic' or 'trilinear'. Default: None.
            See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html
            It also can be a sequence of bool or None, each element corresponds to a key in ``keys``.
        anti_aliasing: bool
            Whether to apply a Gaussian filter to smooth the image prior
            to downsampling. It is crucial to filter when downsampling
            the image to avoid aliasing artifacts. See also ``skimage.transform.resize``
        anti_aliasing_sigma: {float, tuple of floats}, optional
            Standard deviation for Gaussian filtering used when anti-aliasing.
            By default, this value is chosen as (s - 1) / 2 where s is the
            downsampling factor, where s > 1. For the up-size case, s < 1, no
            anti-aliasing is performed prior to rescaling.
        dtype: data type for resampling computation. Defaults to ``float32``.
            If None, use the data type of input data.
        allow_missing_keys: don't raise exception if key is missing.
        lazy: a flag to indicate whether this transform should execute lazily or not.
            Defaults to False
    allNFr   r	   r   Sequence[int] | int	size_moder   r   r
   r   #Sequence[bool | None] | bool | Noneanti_aliasingr   anti_aliasing_sigmaISequence[Sequence[float] | float | None] | Sequence[float] | float | Noner   ;Sequence[DtypeLike | torch.dtype] | DtypeLike | torch.dtyper   r   r   r   r   c                 C  s   t | ||	 tj| |
d t|t| j| _t|t| j| _t|t| j| _t|t| j| _	t|t| j| _
t|||
d| _d S )Nr   )r   r  r   )r-   r   r,   r6   r   r   r   r   r   r  r  r%   resizer)r   r   r   r  r   r   r  r  r   r   r   r   r   r   r   7  s   zResized.__init__r   c                 C  r   r   )r   r  r   r   r   r   r   r   M  r   zResized.lazyr   r   r   r   c              
   C  sn   t |}|du r| jn|}| || j| j| j| j| jD ]\}}}}}	}
| j|| ||||	|
|d||< q|S )r   N)r   r   r  r  r   r   )	r   r   r   r   r   r  r  r   r  )r   r   r   r   r   r   r   r   r  r  r   r   r   r   r   R  s   	zResized.__call__c                 C  r   r   )r   r   r  r   r   r   r   r   r   o  r   zResized.inverse)r   r	   r   r  r  r   r   r
   r   r  r  r   r  r  r   r  r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r%   r   r2   AREAr   float32r   r,   r   r   r   r   r   r   r   r   rB   
  s     *rB   c                   @  sj   e Zd ZdZejZddddddejej	de
jdddfd)ddZejjd*dd Zd+d,d%d&Zd-d'd(ZdS ).rC   z
    Dictionary-based wrapper of :py:class:`monai.transforms.Affine`.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.
    NFr   r	   rotate_paramsr   shear_paramstranslate_paramsscale_paramsaffineNdarrayOrTensor | Noner   Sequence[int] | int | Noner   r
   r   devicetorch.device | Noner   DtypeLike | torch.dtyper   r   r   r   r   r   c                 C  sb   t | || tj| |d t|||||||
|||d
| _t|t| j| _t|	t| j| _	dS )aS  
        Args:
            keys: keys of the corresponding items to be transformed.
            rotate_params: a rotation angle in radians, a scalar for 2D image, a tuple of 3 floats for 3D.
                Defaults to no rotation.
            shear_params: shearing factors for affine matrix, take a 3D affine as example::

                [
                    [1.0, params[0], params[1], 0.0],
                    [params[2], 1.0, params[3], 0.0],
                    [params[4], params[5], 1.0, 0.0],
                    [0.0, 0.0, 0.0, 1.0],
                ]

                a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing.
            translate_params: a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Translation is in
                pixel/voxel relative to the center of the input image. Defaults to no translation.
            scale_params: scale factor for every spatial dims. a tuple of 2 floats for 2D,
                a tuple of 3 floats for 3D. Defaults to `1.0`.
            affine: if applied, ignore the params (`rotate_params`, etc.) and use the
                supplied matrix. Should be square with each side = num of image spatial
                dimensions + 1.
            spatial_size: output image spatial size.
                if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1,
                the transform will use the spatial size of `img`.
                if some components of the `spatial_size` are non-positive values, the transform will use the
                corresponding components of img size. For example, `spatial_size=(32, -1)` will be adapted
                to `(32, 64)` if the second spatial dimension size of img is `64`.
            mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers).
                Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used
                and the value represents the order of the spline interpolation.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
                Padding mode for outside grid values. Defaults to ``"reflection"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When `mode` is an integer, using numpy/cupy backends, this argument accepts
                {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            device: device on which the tensor will be allocated.
            dtype: data type for resampling computation. Defaults to ``float32``.
                If ``None``, use the data type of input data. To be compatible with other modules,
                the output data type is always `float32`.
            align_corners: Defaults to False.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
            allow_missing_keys: don't raise exception if key is missing.
            lazy: a flag to indicate whether this transform should execute lazily or not.
                Defaults to False

        See also:
            - :py:class:`monai.transforms.compose.MapTransform`
            - :py:class:`RandAffineGrid` for the random affine parameters configurations.

        r   )
r  r  r  r  r  r   r!  r   r   r   N)
r-   r   r,   r   r  r6   r   r   r   r   )r   r   r  r  r  r  r  r   r   r   r!  r   r   r   r   r   r   r   r     s    JzAffined.__init__r   c                 C  r   r   )r   r  r   r   r   r   r   r     r   zAffined.lazyr   r   r   r   c           	      C  sZ   |du r| j n|}t|}| || j| jD ]\}}}| j|| |||d\||< }q|S )r   N)r   r   r   )r   r   r   r   r   r  )	r   r   r   r   r   r   r   r   _r   r   r   r     s
    zAffined.__call__c                 C  r   r   )r   r   r  r   r   r   r   r   r     r   zAffined.inverse)r   r	   r  r   r  r   r  r   r  r   r  r  r   r   r   r
   r   r
   r!  r"  r   r#  r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r0   r   r1   
REFLECTIONr   r  r   r,   r   r   r   r   r   r   r   r   rC   v  s*    [rC   c                      s|   e Zd ZdZejZddddddejej	ddddfd.ddZ
ejjd/ddZd0d1 fd$d%Z	d2d3d*d+Zd4d,d-Z  ZS )5rD   z
    Dictionary-based wrapper of :py:class:`monai.transforms.RandAffine`.

    This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
    for more information.
    Nr   Fr   r	   r   r   r   r   rotate_range4Sequence[tuple[float, float] | float] | float | Noneshear_rangetranslate_rangescale_ranger   r
   r   
cache_gridr   r!  r"  r   r   r   r   c                 C  sl   t | || t| | tj| |d td||||||
||d	| _t|t| j| _	t|	t| j| _
dS )a:  
        Args:
            keys: keys of the corresponding items to be transformed.
            spatial_size: output image spatial size.
                if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1,
                the transform will use the spatial size of `img`.
                if some components of the `spatial_size` are non-positive values, the transform will use the
                corresponding components of img size. For example, `spatial_size=(32, -1)` will be adapted
                to `(32, 64)` if the second spatial dimension size of img is `64`.
            prob: probability of returning a randomized affine grid.
                defaults to 0.1, with 10% chance returns a randomized grid.
            rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then
                `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter
                for the `i`th spatial dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used.
                This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be
                in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]`
                for dim0 and nothing for the remaining dimensions.
            shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select
                shearing factors(a tuple of 2 floats for 2D, a tuple of 6 floats for 3D) for affine matrix,
                take a 3D affine as example::

                    [
                        [1.0, params[0], params[1], 0.0],
                        [params[2], 1.0, params[3], 0.0],
                        [params[4], params[5], 1.0, 0.0],
                        [0.0, 0.0, 0.0, 1.0],
                    ]

            translate_range: translate range with format matching `rotate_range`, it defines the range to randomly
                select pixel/voxel to translate for every spatial dims.
            scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select
                the scale factor to translate for every spatial dims. A value of 1.0 is added to the result.
                This allows 0 to correspond to no change (i.e., a scaling of 1.0).
            mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers).
                Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used
                and the value represents the order of the spline interpolation.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
                Padding mode for outside grid values. Defaults to ``"reflection"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When `mode` is an integer, using numpy/cupy backends, this argument accepts
                {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            cache_grid: whether to cache the identity sampling grid.
                If the spatial size is not dynamically defined by input image, enabling this option could
                accelerate the transform.
            device: device on which the tensor will be allocated.
            allow_missing_keys: don't raise exception if key is missing.
            lazy: a flag to indicate whether this transform should execute lazily or not.
                Defaults to False

        See also:
            - :py:class:`monai.transforms.compose.MapTransform`
            - :py:class:`RandAffineGrid` for the random affine parameters configurations.

        r         ?)	r   r&  r(  r)  r*  r   r+  r!  r   N)r-   r   r.   r,   r   rand_affiner6   r   r   r   r   )r   r   r   r   r&  r(  r)  r*  r   r   r+  r!  r   r   r   r   r   r     s    LzRandAffined.__init__r   c                 C  r   r   )r   r-  r   r   r   r   r   r   a  r   zRandAffined.lazyseed
int | Nonestatenp.random.RandomState | Nonec                       | j || t || | S r   )r-  set_random_stater   r   r.  r0  r   r   r   r3  f     zRandAffined.set_random_stater   r   r   r   c              
   C  sV  t |}| |}|dkrt|t d}|S | d | j  || }t|tr-| n|j	dd }|du r;| j
n|}t| jj|}	| jpL|	t|k}
d}|
re| jj|	|d}| jre| jj|	||d}|du rkdn|}| || j| jD ]2\}}}|
r| j|| d||d||d||< nt|| t tjd	||< |
| _| j|| d|d
 qv|S )r   r   r  Nr   r   )gridr   r   Tr  r   r  )r   	first_keyr4   r   r   r-  r   r   r   r   r   r7   r   r  r5   get_identity_gridrand_affine_gridr   r   r   r   r  r  )r   r   r   r   r8  outitemr   r   sp_sizedo_resamplingr6  r   r   r   r   r   r   r   k  s2   


 "zRandAffined.__call__c                 C  s~   t |}| |D ]3}| || }tj|tj vrq	|tj tj d }|r<|| j|tj  | j|| ||< q	|S )Nr>  )	r   r   r  r9   r  applied_operationsappendr-  r   )r   r   r   r   trr>  r   r   r   r     s   zRandAffined.inverse)r   r	   r   r   r   r   r&  r'  r(  r'  r)  r'  r*  r'  r   r
   r   r
   r+  r   r!  r"  r   r   r   r   r   r   r   NN)r.  r/  r0  r1  r   rD   r   )r   r   r   r   r   r   r   )r   r   r   r   r   r   r0   r   r1   r%  r   r,   r   r   r3  r   r   r  r   r   r   r   rD     s,    ]2rD   c                
      sX   e Zd ZdZejZddddddejej	ddf
d*ddZ
d+d, fd#d$Zd-d(d)Z  ZS ).rE   zQ
    Dictionary-based wrapper of :py:class:`monai.transforms.Rand2DElastic`.
    Nr   Fr   r	   spacingtuple[float, float] | floatmagnitude_rangetuple[float, float]r   tuple[int, int] | int | Noner   r   r&  r'  r(  r)  r*  r   r
   r   r!  r"  r   r   r   r   c                 C  ^   t | || t| | t||d||||	||d	| _t|
t| j| _t|t| j| _	dS )a  
        Args:
            keys: keys of the corresponding items to be transformed.
            spacing: distance in between the control points.
            magnitude_range: 2 int numbers, the random offsets will be generated from
                ``uniform[magnitude[0], magnitude[1])``.
            spatial_size: specifying output image spatial size [h, w].
                if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1,
                the transform will use the spatial size of `img`.
                if some components of the `spatial_size` are non-positive values, the transform will use the
                corresponding components of img size. For example, `spatial_size=(32, -1)` will be adapted
                to `(32, 64)` if the second spatial dimension size of img is `64`.
            prob: probability of returning a randomized affine grid.
                defaults to 0.1, with 10% chance returns a randomized grid,
                otherwise returns a ``spatial_size`` centered area extracted from the input image.
            rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then
                `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter
                for the `i`th spatial dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used.
                This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be
                in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]`
                for dim0 and nothing for the remaining dimensions.
            shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select
                shearing factors(a tuple of 2 floats for 2D) for affine matrix, take a 2D affine as example::

                    [
                        [1.0, params[0], 0.0],
                        [params[1], 1.0, 0.0],
                        [0.0, 0.0, 1.0],
                    ]

            translate_range: translate range with format matching `rotate_range`, it defines the range to randomly
                select pixel to translate for every spatial dims.
            scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select
                the scale factor to translate for every spatial dims. A value of 1.0 is added to the result.
                This allows 0 to correspond to no change (i.e., a scaling of 1.0).
            mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers).
                Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used
                and the value represents the order of the spline interpolation.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
                Padding mode for outside grid values. Defaults to ``"reflection"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When `mode` is an integer, using numpy/cupy backends, this argument accepts
                {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            device: device on which the tensor will be allocated.
            allow_missing_keys: don't raise exception if key is missing.

        See also:
            - :py:class:`RandAffineGrid` for the random affine parameters configurations.
            - :py:class:`Affine` for the affine transformation parameters configurations.

        r,  )	rC  rE  r   r&  r(  r)  r*  r   r!  N)
r-   r   r.   r   rand_2d_elasticr6   r   r   r   r   )r   r   rC  rE  r   r   r&  r(  r)  r*  r   r   r!  r   r   r   r   r     s   IzRand2DElasticd.__init__r.  r/  r0  r1  c                   r2  r   )rI  r3  r   r4  r   r   r   r3    r5  zRand2DElasticd.set_random_stater   r   r   c                 C  sv  t |}| |}|dkrt|t d}|S | d | jj}|du r6t|| tj	r6|| j}| j
| t|| trK|| jrKtd| d t| jj|| jdd }| j| | jr| jj|d}| jj|d}tjjjd	|d
t| jjjdtjjdd}t|d|d
 }nttj	t ||dd}| !|| j"| j#D ]\}}	}
| jj$|| ||	|
d||< q|S )  
        Args:
            data: a dictionary containing the tensor-like data to be processed. The ``keys`` specified
                in this dictionary must be tensor like arrays that are channel first and have at most
                three spatial dimensions

        Returns:
            a dictionary containing the transformed data, as well as any other data present in the dictionary
        r   r  Ndata['B'] has pending operations, transform may return incorrect results.r   )r   r6  Tr      F)recompute_scale_factorinputscale_factorr   r   )roi_sizer   r   r!  r   r   r   )%r   r8  r4   r   r   rI  r!  r   r   r   
set_devicer   pending_operationswarningswarnr7   r   r   r  deform_gridr:  nn
functionalinterpolate	unsqueezer6   rC  r2   BICUBICvaluer   r   r/   r   r   r   r   )r   r   r   r8  r;  r!  r=  r6  r   r   r   r   r   r   r     s:   



zRand2DElasticd.__call__)r   r	   rC  rD  rE  rF  r   rG  r   r   r&  r'  r(  r'  r)  r'  r*  r'  r   r
   r   r
   r!  r"  r   r   r   r   rB  )r.  r/  r0  r1  r   rE   r   )r   r   r   r   r   r   r0   r   r1   r%  r   r3  r   r  r   r   r   r   rE     s     YrE   c                
      sX   e Zd ZdZejZddddddejej	ddf
d)ddZ
d*d+ fd"d#Zd,d'd(Z  ZS )-rF   zQ
    Dictionary-based wrapper of :py:class:`monai.transforms.Rand3DElastic`.
    Nr   Fr   r	   sigma_rangerF  rE  r   !tuple[int, int, int] | int | Noner   r   r&  r'  r(  r)  r*  r   r
   r   r!  r"  r   r   r   r   c                 C  rH  )a  
        Args:
            keys: keys of the corresponding items to be transformed.
            sigma_range: a Gaussian kernel with standard deviation sampled from
                ``uniform[sigma_range[0], sigma_range[1])`` will be used to smooth the random offset grid.
            magnitude_range: the random offsets on the grid will be generated from
                ``uniform[magnitude[0], magnitude[1])``.
            spatial_size: specifying output image spatial size [h, w, d].
                if `spatial_size` and `self.spatial_size` are not defined, or smaller than 1,
                the transform will use the spatial size of `img`.
                if some components of the `spatial_size` are non-positive values, the transform will use the
                corresponding components of img size. For example, `spatial_size=(32, 32, -1)` will be adapted
                to `(32, 32, 64)` if the third spatial dimension size of img is `64`.
            prob: probability of returning a randomized affine grid.
                defaults to 0.1, with 10% chance returns a randomized grid,
                otherwise returns a ``spatial_size`` centered area extracted from the input image.
            rotate_range: angle range in radians. If element `i` is a pair of (min, max) values, then
                `uniform[-rotate_range[i][0], rotate_range[i][1])` will be used to generate the rotation parameter
                for the `i`th spatial dimension. If not, `uniform[-rotate_range[i], rotate_range[i])` will be used.
                This can be altered on a per-dimension basis. E.g., `((0,3), 1, ...)`: for dim0, rotation will be
                in range `[0, 3]`, and for dim1 `[-1, 1]` will be used. Setting a single value will use `[-x, x]`
                for dim0 and nothing for the remaining dimensions.
            shear_range: shear range with format matching `rotate_range`, it defines the range to randomly select
                shearing factors(a tuple of 6 floats for 3D) for affine matrix, take a 3D affine as example::

                    [
                        [1.0, params[0], params[1], 0.0],
                        [params[2], 1.0, params[3], 0.0],
                        [params[4], params[5], 1.0, 0.0],
                        [0.0, 0.0, 0.0, 1.0],
                    ]

            translate_range: translate range with format matching `rotate_range`, it defines the range to randomly
                select voxel to translate for every spatial dims.
            scale_range: scaling range with format matching `rotate_range`. it defines the range to randomly select
                the scale factor to translate for every spatial dims. A value of 1.0 is added to the result.
                This allows 0 to correspond to no change (i.e., a scaling of 1.0).
            mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers).
                Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used
                and the value represents the order of the spline interpolation.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
                Padding mode for outside grid values. Defaults to ``"reflection"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When `mode` is an integer, using numpy/cupy backends, this argument accepts
                {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            device: device on which the tensor will be allocated.
            allow_missing_keys: don't raise exception if key is missing.

        See also:
            - :py:class:`RandAffineGrid` for the random affine parameters configurations.
            - :py:class:`Affine` for the affine transformation parameters configurations.

        r,  )	r`  rE  r   r&  r(  r)  r*  r   r!  N)
r-   r   r.   r   rand_3d_elasticr6   r   r   r   r   )r   r   r`  rE  r   r   r&  r(  r)  r*  r   r   r!  r   r   r   r   r   G  s   KzRand3DElasticd.__init__r.  r/  r0  r1  c                   r2  r   )rb  r3  r   r4  r   r   r   r3    r5  zRand3DElasticd.set_random_stater   r   r   c                 C  sr  t |}| |}|dkrt|t d}|S | d t|| tr0|| jr0t	d| d t
| jj|| jdd }| j| | jj}|du r_t|| tjr_|| j}| j| t||dd}| jrtd	| jjd
d|}tj| jj|dd}	|dd	  ||	d | jj 7  < | jj|d}| || j| jD ]\}
}}| jj||
 |||d||
< q|S )rJ  r   r  NrK  rL  r   r   rS  r   g      @)spatial_dimssigma	truncated)r!  r   rM  rT  ) r   r8  r4   r   r   r   r   rV  rW  rX  r7   rb  r   r   r!  r   r   rU  r/   r  r   rd  to	as_tensorrand_offsetr]  	magnituder:  r   r   r   r   )r   r   r   r8  r;  r=  r!  r6  gaussianoffsetr   r   r   r   r   r   r     s.   



$zRand3DElasticd.__call__)r   r	   r`  rF  rE  rF  r   ra  r   r   r&  r'  r(  r'  r)  r'  r*  r'  r   r
   r   r
   r!  r"  r   r   r   r   rB  )r.  r/  r0  r1  r   rF   r   )r   r   r   r   r   r   r0   r   r1   r%  r   r3  r   r  r   r   r   r   rF   @  s     [rF   c                   @  sP   e Zd ZdZejZ			ddddZejj	dddZddddZ
dddZdS ) rG   a  
    Dictionary-based wrapper of :py:class:`monai.transforms.Flip`.

    See `numpy.flip` for additional details.
    https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html

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

    Args:
        keys: Keys to pick data for transformation.
        spatial_axis: Spatial axes along which to flip over. Default is None.
        allow_missing_keys: don't raise exception if key is missing.
        lazy: a flag to indicate whether this transform should execute lazily or not.
            Defaults to False
    NFr   r	   spatial_axisr   r   r   r   r   r   c                 C  s,   t | || tj| |d t|d| _d S )Nr   )rl  )r-   r   r,   r   flipper)r   r   rl  r   r   r   r   r   r     s   zFlipd.__init__r   c                 C     || j _|| _d S r   rm  r   r   r   r   r   r   r        
z
Flipd.lazyr   r   r   r   c                 C  r   r   )r   r   r   rm  r   r   r   r   r     r   zFlipd.__call__c                 C  r   r   )r   r   rm  r   r   r   r   r   r     r   zFlipd.inverse)NFF)
r   r	   rl  r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r,   r   r   r   r   r   r   r   r   rG     s    rG   c                      sf   e Zd ZdZejZ				d#d$ddZejj	d%ddZd&d' fddZ
d(d)dd Zd*d!d"Z  ZS )+rH   a  
    Dictionary-based version :py:class:`monai.transforms.RandFlip`.

    See `numpy.flip` for additional details.
    https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html

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

    Args:
        keys: Keys to pick data for transformation.
        prob: Probability of flipping.
        spatial_axis: Spatial axes along which to flip over. Default is None.
        allow_missing_keys: don't raise exception if key is missing.
        lazy: a flag to indicate whether this transform should execute lazily or not.
            Defaults to False
    r   NFr   r	   r   r   rl  r   r   r   r   r   r   c                 C  s:   t | || t| | tj| |d t||d| _d S )Nr   )rl  r   )r-   r   r.   r,   r   rm  )r   r   r   rl  r   r   r   r   r   r   #  s   zRandFlipd.__init__r   c                 C  rn  r   ro  r   r   r   r   r   0  rp  zRandFlipd.lazyr.  r/  r0  r1  c                   s   t  || | S r   )r   r3  r4  r   r   r   r3  5  s   zRandFlipd.set_random_stater   r   r   r   c                 C  s   t |}| d |du r| jn|}| |D ]&}| jr(| j|| |d||< nt|| t d||< | j|| d|d q|S r  )	r   r   r   r   r  rm  r4   r   r  r   r   r   r   r   9  s   
zRandFlipd.__call__c              	   C  st   t |}| |D ].}| || }|tj sq	| jd | || ||< W d    n1 s2w   Y  q	|S )NF)r   r   r  r9   r	  rm  trace_transformr  r   r   r   r   R  s   
zRandFlipd.inverse)r   NFF)r   r	   r   r   rl  r   r   r   r   r   r   r   rq  rB  )r.  r/  r0  r1  r   rH   r   r   r   )r   r   r   r   r   r   r   r,   r   r   r3  r   r   r  r   r   r   r   rH     s    rH   c                      s`   e Zd ZdZejZ	d!d"ddZejj	d#ddZd$d% fddZ
d&d'ddZd(dd Z  ZS ))rK   ae  
    Dictionary-based version :py:class:`monai.transforms.RandAxisFlip`.

    See `numpy.flip` for additional details.
    https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html

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

    Args:
        keys: Keys to pick data for transformation.
        prob: Probability of flipping.
        allow_missing_keys: don't raise exception if key is missing.
        lazy: a flag to indicate whether this transform should execute lazily or not.
            Defaults to False
    r   Fr   r	   r   r   r   r   r   r   r   c                 C  s:   t | || t| | tj| |d td|d| _d S )Nr   r,  )r   r   )r-   r   r.   r,   r   rm  )r   r   r   r   r   r   r   r   r   q  s   zRandAxisFlipd.__init__r   c                 C  rn  r   ro  r   r   r   r   r   y  rp  zRandAxisFlipd.lazyNr.  r/  r0  r1  c                       t  || | j|| | S r   )r   r3  rm  r4  r   r   r   r3  ~  r5  zRandAxisFlipd.set_random_stater   r   r   r   c                 C  s   t |}| |}|dkr|S | d | j||  |du r#| jn|}| |D ]'}| jr<| j|| d|d||< nt|| t d||< | j	|| d|d q*|S )r   r   NF)r   r   r  Tr  )
r   r8  r   rm  r   r   r  r4   r   r  )r   r   r   r   r8  r   r   r   r   r   r     s   

zRandAxisFlipd.__call__c                 C  ^   t |}| |D ]#}| || }|tj r,|| j|tj  | j	|| ||< q	|S r   )
r   r   r  r9   r	  r?  r@  r  rm  r   r  r   r   r   r        
zRandAxisFlipd.inverse)r   FF)
r   r	   r   r   r   r   r   r   r   r   rq  rB  )r.  r/  r0  r1  r   rK   r   r   r   )r   r   r   r   r   r   r   r,   r   r   r3  r   r   r  r   r   r   r   rK   ]  s     rK   c                   @  s^   e Zd ZdZejZdejej	de
jddfd#ddZejjd$ddZd%d&dd Zd'd!d"ZdS )(rL   a  
    Dictionary-based wrapper of :py:class:`monai.transforms.Rotate`.

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

    Args:
        keys: Keys to pick data for transformation.
        angle: Rotation angle(s) in radians.
        keep_size: If it is False, the output shape is adapted so that the
            input array is contained completely in the output.
            If it is True, the output shape is the same as the input. Default is True.
        mode: {``"bilinear"``, ``"nearest"``}
            Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
            See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
            It also can be a sequence of string, each element corresponds to a key in ``keys``.
        padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
            Padding mode for outside grid values. Defaults to ``"border"``.
            See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
            It also can be a sequence of string, each element corresponds to a key in ``keys``.
        align_corners: Defaults to False.
            See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
            It also can be a sequence of bool, each element corresponds to a key in ``keys``.
        dtype: data type for resampling computation. Defaults to ``float32``.
            If None, use the data type of input data. To be compatible with other modules,
            the output data type is always ``float32``.
            It also can be a sequence of dtype or None, each element corresponds to a key in ``keys``.
        allow_missing_keys: don't raise exception if key is missing.
        lazy: a flag to indicate whether this transform should execute lazily or not.
            Defaults to False
    TFr   r	   angler   	keep_sizer   r   r
   r   r   r   r   r  r   r   r   r   c
           
      C  sx   t | || tj| |	d t|||	d| _t|t| j| _t|t| j| _	t|t| j| _
t|t| j| _d S )Nr   )rv  rw  r   )r-   r   r,   r&   r   r6   r   r   r   r   r   r   )
r   r   rv  rw  r   r   r   r   r   r   r   r   r   r     s   zRotated.__init__r   c                 C  rn  r   )r   r   r   r   r   r   r   r     rp  zRotated.lazyNr   r   r   r   c           
   	   C  f   t |}|du r| jn|}| || j| j| j| jD ]\}}}}}	| j|| ||||	|d||< q|S r   N)r   r   r   r   r   )r   r   r   r   r   r   r   r   
r   r   r   r   r   r   r   r   r   r   r   r   r   r        zRotated.__call__c                 C  r   r   r   r   r   r   r   r     r   zRotated.inverse)r   r	   rv  r   rw  r   r   r
   r   r
   r   r   r   r  r   r   r   r   r   r   rq  r   r   r   )r   r   r   r   r&   r   r0   r   r1   r   r   r  r   r,   r   r   r   r   r   r   r   r   rL     s     rL   c                      sz   e Zd ZdZejZdddddejej	de
jddfd/ddZejjd0ddZd1d2 fd%d&Zd3d4d+d,Zd5d-d.Z  ZS )6rM   a5
  
    Dictionary-based version :py:class:`monai.transforms.RandRotate`
    Randomly rotates the input arrays.

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

    Args:
        keys: Keys to pick data for transformation.
        range_x: Range of rotation angle in radians in the plane defined by the first and second axes.
            If single number, angle is uniformly sampled from (-range_x, range_x).
        range_y: Range of rotation angle in radians in the plane defined by the first and third axes.
            If single number, angle is uniformly sampled from (-range_y, range_y). only work for 3D data.
        range_z: Range of rotation angle in radians in the plane defined by the second and third axes.
            If single number, angle is uniformly sampled from (-range_z, range_z). only work for 3D data.
        prob: Probability of rotation.
        keep_size: If it is False, the output shape is adapted so that the
            input array is contained completely in the output.
            If it is True, the output shape is the same as the input. Default is True.
        mode: {``"bilinear"``, ``"nearest"``}
            Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
            See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
            It also can be a sequence of string, each element corresponds to a key in ``keys``.
        padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
            Padding mode for outside grid values. Defaults to ``"border"``.
            See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
            It also can be a sequence of string, each element corresponds to a key in ``keys``.
        align_corners: Defaults to False.
            See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html
            It also can be a sequence of bool, each element corresponds to a key in ``keys``.
        dtype: data type for resampling computation. Defaults to ``float64`` for best precision.
            If None, use the data type of input data. To be compatible with other modules,
            the output data type is always ``float32``.
            It also can be a sequence of dtype or None, each element corresponds to a key in ``keys``.
        allow_missing_keys: don't raise exception if key is missing.
        lazy: a flag to indicate whether this transform should execute lazily or not.
            Defaults to False
            r   TFr   r	   range_xrD  range_yrange_zr   r   rw  r   r   r
   r   r   r   r   r  r   r   r   r   c                 C  s   t | || t| | tj| |d t|||d||d| _t|t| j| _	t|t| j| _
t|	t| j| _t|
t| j| _d S )Nr   r,  )r}  r~  r  r   rw  r   )r-   r   r.   r,   r!   rand_rotater6   r   r   r   r   r   r   )r   r   r}  r~  r  r   rw  r   r   r   r   r   r   r   r   r   r   2  s   zRandRotated.__init__r   c                 C  rn  r   )r  r   r   r   r   r   r   r   L  rp  zRandRotated.lazyNr.  r/  r0  r1  c                   rs  r   )r   r3  r  r4  r   r   r   r3  Q  r5  zRandRotated.set_random_stater   r   r   r   c           
   
   C  s   t |}| d | j  |du r| jn|}| || j| j| j| jD ]2\}}}}}	| j	r?| j|| ||||	d|d||< nt
|| t tjd||< | j|| d|d q$|S )r   NFr   r   r   r   r   r   r7  Tr  )r   r   r  r   r   r   r   r   r   r  r4   r   r   r  r  rz  r   r   r   r   V  s(   


zRandRotated.__call__c                 C  rt  r   )
r   r   r  r9   r	  r?  r@  r  r  r   r  r   r   r   r   |  ru  zRandRotated.inverse)r   r	   r}  rD  r~  rD  r  rD  r   r   rw  r   r   r
   r   r
   r   r   r   r  r   r   r   r   r   r   rq  rB  )r.  r/  r0  r1  r   rM   r   r   r   )r   r   r   r   r!   r   r0   r   r1   r   r   r  r   r,   r   r   r3  r   r   r  r   r   r   r   rM     s(    '&rM   c                   @  s^   e Zd ZdZejZejej	de
jdddfd#ddZejjd$ddZd%d&dd Zd'd!d"ZdS )(rN   aS	  
    Dictionary-based wrapper of :py:class:`monai.transforms.Zoom`.

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

    Args:
        keys: Keys to pick data for transformation.
        zoom: The zoom factor along the spatial axes.
            If a float, zoom is the same for each spatial axis.
            If a sequence, zoom should contain one value for each spatial axis.
        mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``}
            The interpolation mode. Defaults to ``"area"``.
            See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html
            It also can be a sequence of string, each element corresponds to a key in ``keys``.
        padding_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 ``"edge"``.
            The mode to pad data after zooming.
            See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
            https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
        align_corners: This only has an effect when mode is
            'linear', 'bilinear', 'bicubic' or 'trilinear'. Default: None.
            See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html
            It also can be a sequence of bool or None, each element corresponds to a key in ``keys``.
        dtype: data type for resampling computation. Defaults to ``float32``.
            If None, use the data type of input data.
        keep_size: Should keep original size (pad if needed), default is True.
        allow_missing_keys: don't raise exception if key is missing.
        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.

    NTFr   r	   zoomr   r   r
   r   r   r  r   r  rw  r   r   r   r   r   c
                 K  s   t | || tj| |	d t|t| j| _t|t| j| _t|t| j| _t|t| j| _	t
d|||	d|
| _d S )Nr   )r  rw  r   r   )r-   r   r,   r6   r   r   r   r   r   r   r*   zoomer)r   r   r  r   r   r   r   rw  r   r   kwargsr   r   r   r     s   zZoomd.__init__r   c                 C  rn  r   )r  r   r   r   r   r   r   r     rp  z
Zoomd.lazyr   r   r   r   c           
   	   C  rx  ry  )r   r   r   r   r   r   r   r  rz  r   r   r   r     r{  zZoomd.__call__c                 C  r   r   )r   r   r  r   r   r   r   r   r     r   zZoomd.inverse)r   r	   r  r   r   r
   r   r
   r   r  r   r  rw  r   r   r   r   r   r   r   rq  r   r   r   )r   r   r   r   r*   r   r2   r  r3   EDGEr   r  r   r,   r   r   r   r   r   r   r   r   rN     s    %rN   c                
      sx   e Zd ZdZejZdddejej	de
jdddf
d/ddZejjd0dd Zd1d2 fd%d&Zd3d4d+d,Zd5d-d.Z  ZS )6rO   aJ  
    Dict-based version :py:class:`monai.transforms.RandZoom`.

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

    Args:
        keys: Keys to pick data for transformation.
        prob: Probability of zooming.
        min_zoom: Min zoom factor. Can be float or sequence same size as image.
            If a float, select a random factor from `[min_zoom, max_zoom]` then apply to all spatial dims
            to keep the original spatial shape ratio.
            If a sequence, min_zoom should contain one value for each spatial axis.
            If 2 values provided for 3D data, use the first value for both H & W dims to keep the same zoom ratio.
        max_zoom: Max zoom factor. Can be float or sequence same size as image.
            If a float, select a random factor from `[min_zoom, max_zoom]` then apply to all spatial dims
            to keep the original spatial shape ratio.
            If a sequence, max_zoom should contain one value for each spatial axis.
            If 2 values provided for 3D data, use the first value for both H & W dims to keep the same zoom ratio.
        mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``}
            The interpolation mode. Defaults to ``"area"``.
            See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html
            It also can be a sequence of string, each element corresponds to a key in ``keys``.
        padding_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 ``"edge"``.
            The mode to pad data after zooming.
            See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
            https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
        align_corners: This only has an effect when mode is
            'linear', 'bilinear', 'bicubic' or 'trilinear'. Default: None.
            See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html
            It also can be a sequence of bool or None, each element corresponds to a key in ``keys``.
        dtype: data type for resampling computation. Defaults to ``float32``.
            If None, use the data type of input data.
        keep_size: Should keep original size (pad if needed), default is True.
        allow_missing_keys: don't raise exception if key is missing.
        lazy: a flag to indicate whether this transform should execute lazily or not.
            Defaults to False
        kwargs: other args for `np.pad` API, note that `np.pad` treats channel dimension as the first dimension.
            more details: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
    r   g?g?NTFr   r	   r   r   min_zoomr   max_zoomr   r
   r   r   r  r   r  rw  r   r   r   r   r   c                 K  s   t | ||
 t| | tj| |d tdd|||	|d|| _t|t| j| _	t|t| j| _
t|t| j| _t|t| j| _d S )Nr   r,  )r   r  r  rw  r   r   )r-   r   r.   r,   r#   	rand_zoomr6   r   r   r   r   r   r   )r   r   r   r  r  r   r   r   r   rw  r   r   r  r   r   r   r     s   
zRandZoomd.__init__r   c                 C  rn  r   )r  r   r   r   r   r   r   r   0  rp  zRandZoomd.lazyr.  r/  r0  r1  c                   rs  r   )r   r3  r  r4  r   r   r   r3  5  r5  zRandZoomd.set_random_stater   r   r   r   c              
   C  s   t |}| |}|dkrt|t d}|S | d | j||  |du r*| jn|}| || j| j	| j
| jD ]2\}}}	}
}| jrT| j|| ||	|
|d|d||< nt|| t tjd||< | j|| d|d q9|S )	r   r   r  NFr  r7  Tr  )r   r8  r4   r   r   r  r   r   r   r   r   r   r  r   r  r  )r   r   r   r   r8  r;  r   r   r   r   r   r   r   r   r   r   :  s0   


zRandZoomd.__call__c                 C  rt  r   )
r   r   r  r9   r	  r?  r@  r  r  r   r  r   r   r   r   e  ru  zRandZoomd.inverse)r   r	   r   r   r  r   r  r   r   r
   r   r
   r   r  r   r  rw  r   r   r   r   r   r   r   rq  rB  )r.  r/  r0  r1  r   rO   r   r   r   )r   r   r   r   r#   r   r2   r  r3   r  r   r  r   r,   r   r   r3  r   r   r  r   r   r   r   rO     s&    ,+rO   c                      s@   e Zd ZdZejZejej	ddfd fddZ
dddZ  ZS )rI   zR
    Dictionary-based wrapper of :py:class:`monai.transforms.GridDistortion`.
    NFr   r	   	num_cellstuple[int] | intdistort_stepslist[tuple]r   r   r   r!  r"  r   r   r   r   c                   sF   t  || t|||d| _t|t| j| _t|t| j| _dS )a  
        Args:
            keys: keys of the corresponding items to be transformed.
            num_cells: number of grid cells on each dimension.
            distort_steps: This argument is a list of tuples, where each tuple contains the distort steps of the
                corresponding dimensions (in the order of H, W[, D]). The length of each tuple equals to `num_cells + 1`.
                Each value in the tuple represents the distort step of the related cell.
            mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers).
                Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used
                and the value represents the order of the spline interpolation.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
                Padding mode for outside grid values. Defaults to ``"border"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When `mode` is an integer, using numpy/cupy backends, this argument accepts
                {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            device: device on which the tensor will be allocated.
            allow_missing_keys: don't raise exception if key is missing.
        )r  r  r!  N)	r   r   r   grid_distortionr6   r   r   r   r   )r   r   r  r  r   r   r!  r   r   r   r   r   v  s   "zGridDistortiond.__init__r   r   r   c                 C  sB   t |}| || j| jD ]\}}}| j|| ||d||< q|S )rJ  rT  )r   r   r   r   r  )r   r   r   r   r   r   r   r   r   r     s   
zGridDistortiond.__call__)r   r	   r  r  r  r  r   r   r   r   r!  r"  r   r   r   r   r   )r   r   r   r   r   r   r0   r   r1   r   r   r   r  r   r   r   r   rI   o  s    'rI   c                      sT   e Zd ZdZejZdddejej	ddfd%ddZ
	d&d' fddZd(d#d$Z  ZS ))rJ   zV
    Dictionary-based wrapper of :py:class:`monai.transforms.RandGridDistortion`.
       r   )gQgQ?NFr   r	   r  r  r   r   distort_limitrD  r   r   r   r!  r"  r   r   r   r   c	           	      C  sT   t | || t| | t|d||d| _t|t| j| _t|t| j| _	dS )a  
        Args:
            keys: keys of the corresponding items to be transformed.
            num_cells: number of grid cells on each dimension.
            prob: probability of returning a randomized grid distortion transform. Defaults to 0.1.
            distort_limit: range to randomly distort.
                If single number, distort_limit is picked from (-distort_limit, distort_limit).
                Defaults to (-0.03, 0.03).
            mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers).
                Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used
                and the value represents the order of the spline interpolation.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``}
                Padding mode for outside grid values. Defaults to ``"border"``.
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html
                When `mode` is an integer, using numpy/cupy backends, this argument accepts
                {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}.
                See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html
                It also can be a sequence, each element corresponds to a key in ``keys``.
            device: device on which the tensor will be allocated.
            allow_missing_keys: don't raise exception if key is missing.

        r,  )r  r   r  r!  N)
r-   r   r.   r   rand_grid_distortionr6   r   r   r   r   )	r   r   r  r   r  r   r   r!  r   r   r   r   r     s   %zRandGridDistortiond.__init__r.  r/  r0  r1  c                   rs  r   )r   r3  r  r4  r   r   r   r3       z$RandGridDistortiond.set_random_stater   r   r   c                 C  s   t |}| d | jst|t d}|S | |}|dkr't|t d}|S t|| tr<|| jr<t	
d| d | j|| jdd  | || j| jD ]\}}}| j|| ||dd||< qR|S )	rJ  Nr  r   rK  rL  r   F)r   r   r   )r   r   r  r4   r   r8  r   r   rV  rW  rX  r  r   r   r   r   )r   r   r   r;  r8  r   r   r   r   r   r   r     s   


zRandGridDistortiond.__call__)r   r	   r  r  r   r   r  rD  r   r   r   r   r!  r"  r   r   r   r   rB  )r.  r/  r0  r1  r   rJ   r   )r   r   r   r   r   r   r0   r   r1   r   r   r3  r   r  r   r   r   r   rJ     s    .rJ   c                      s:   e Zd ZdZejZ			dd fddZdddZ  ZS )rv   a  
    Split the image into patches based on the provided grid in 2D.

    Args:
        keys: keys of the corresponding items to be transformed.
        grid: a tuple define the shape of the grid upon which the image is split. Defaults to (2, 2)
        size: a tuple or an integer that defines the output patch sizes,
            or a dictionary that define it separately for each key, like {"image": 3, "mask", (2, 2)}.
            If it's an integer, the value will be repeated for each dimension.
            The default is None, where the patch size will be inferred from the grid shape.
        allow_missing_keys: don't raise exception if key is missing.

    Note: This transform currently support only image with two spatial dimensions.
    rN  rN  NFr   r	   r6  r   sizeKint | tuple[int, int] | dict[Hashable, int | tuple[int, int] | None] | Noner   r   c                   sH   t  || || _t tr n	 fdd| jD | _t|d| _d S )Nc                   s   i | ]}| qS r   r   ).0r   r  r   r   
<dictcomp> 	  s    z'GridSplitd.__init__.<locals>.<dictcomp>rM  )	r   r   r6  r   r   r   r  r   splitter)r   r   r6  r  r   r   r  r   r   	  s   $zGridSplitd.__init__r   r   r   %list[dict[Hashable, NdarrayOrTensor]]c                   sr   t | t| j} fddt|D }|  D ]}|  | | j| }t|D ]
}|| || |< q+q|S )rJ  c                   s   g | ]}t  qS r   )r   )r  r$  r   r   r   
<listcomp>/	  s    z'GridSplitd.__call__.<locals>.<listcomp>)r   r   prodr6  ranger   r  r  )r   r   	n_outputsoutputr   resultir   r  r   r   #	  s   
zGridSplitd.__call__)r  NF)r   r	   r6  r   r  r  r   r   )r   r   r   r  )	r   r   r   r   r   r   r   r   r  r   r   r   r   rv   	  s    rv   c                      sB   e Zd ZdZejZ							dd fddZd ddZ  ZS )!ry   a  
    Extract all the patches sweeping the entire image in a row-major sliding-window manner with possible overlaps.
    It can sort the patches and return all or a subset of them.

    Args:
        keys: keys of the corresponding items to be transformed.
        patch_size: size of patches to generate slices for, 0 or None selects whole dimension
        offset: starting position in the array, default is 0 for each dimension.
            np.random.randint(0, patch_size, 2) creates random start between 0 and `patch_size` for a 2D image.
        num_patches: number of patches (or maximum number of patches) to return.
            If the requested number of patches is greater than the number of available patches,
            padding will be applied to provide exactly `num_patches` patches unless `threshold` is set.
            When `threshold` is set, this value is treated as the maximum number of patches.
            Defaults to None, which does not limit number of the patches.
        overlap: amount of overlap between patches in each dimension. Default to 0.0.
        sort_fn: when `num_patches` is provided, it determines if keep patches with highest values (`"max"`),
            lowest values (`"min"`), or in their default order (`None`). Default to None.
        threshold: a value to keep only the patches whose sum of intensities are less than the threshold.
            Defaults to no filtering.
        pad_mode: the  mode for padding the input image by `patch_size` to include patches that cross boundaries.
            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 `None`, which means no padding will be applied.
            See also: https://numpy.org/doc/stable/reference/generated/numpy.pad.html
            https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
            requires pytorch >= 1.10 for best compatibility.
        allow_missing_keys: don't raise exception if key is missing.
        pad_kwargs: other arguments for the `np.pad` or `torch.pad` function.
            note that `np.pad` treats channel dimension as the first dimension.

    Returns:
        dictionary, contains the all the original key/value with the values for `keys`
            replaced by the patches, a MetaTensor with following metadata:

            - `PatchKeys.LOCATION`: the starting location of the patch in the image,
            - `PatchKeys.COUNT`: total number of patches in the image,
            - "spatial_shape": spatial size of the extracted patch, and
            - "offset": the amount of offset for the patches in the image (starting position of the first patch)
    Nr|  Fr   r	   
patch_sizeSequence[int]rk  Sequence[int] | Nonenum_patchesr/  overlapr   sort_fnr   	thresholdfloat | Nonepad_moder   r   c
              
     s2   t  ||	 td|||||||d|
| _d S )N)r  rk  r  r  r  r  r  r   )r   r   r   patcher)r   r   r  rk  r  r  r  r  r  r   
pad_kwargsr   r   r   r   d	  s   zGridPatchd.__init__r   r   r   r   c                 C  s.   t |}| |D ]}| || ||< q	|S )rJ  )r   r   r  r   r   r   r   r   }	  s   
zGridPatchd.__call__)NNr|  NNNF)r   r	   r  r  rk  r  r  r/  r  r   r  r   r  r  r  r   r   r   r   )	r   r   r   r   r   r   r   r   r  r   r   r   r   ry   7	  s    *ry   c                      sP   e Zd ZdZejZ								d$d%ddZd&d' fddZd(d"d#Z  Z	S ))r|   a  
    Extract all the patches sweeping the entire image in a row-major sliding-window manner with possible overlaps,
    and with random offset for the minimal corner of the image, (0,0) for 2D and (0,0,0) for 3D.
    It can sort the patches and return all or a subset of them.

    Args:
        keys: keys of the corresponding items to be transformed.
        patch_size: size of patches to generate slices for, 0 or None selects whole dimension
        min_offset: the minimum range of starting position to be selected randomly. Defaults to 0.
        max_offset: the maximum range of starting position to be selected randomly.
            Defaults to image size modulo patch size.
        num_patches: number of patches (or maximum number of patches) to return.
            If the requested number of patches is greater than the number of available patches,
            padding will be applied to provide exactly `num_patches` patches unless `threshold` is set.
            When `threshold` is set, this value is treated as the maximum number of patches.
            Defaults to None, which does not limit number of the patches.
        overlap: the amount of overlap of neighboring patches in each dimension (a value between 0.0 and 1.0).
            If only one float number is given, it will be applied to all dimensions. Defaults to 0.0.
        sort_fn: when `num_patches` is provided, it determines if keep patches with highest values (`"max"`),
            lowest values (`"min"`), in random ("random"), or in their default order (`None`). Default to None.
        threshold: a value to keep only the patches whose sum of intensities are less than the threshold.
            Defaults to no filtering.
        pad_mode: the  mode for padding the input image by `patch_size` to include patches that cross boundaries.
            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 `None`, which means no padding will be applied.
            See also: https://numpy.org/doc/stable/reference/generated/numpy.pad.html
            https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
            requires pytorch >= 1.10 for best compatibility.
        allow_missing_keys: don't raise exception if key is missing.
        pad_kwargs: other arguments for the `np.pad` or `torch.pad` function.
            note that `np.pad` treats channel dimension as the first dimension.

    Returns:
        dictionary, contains the all the original key/value with the values for `keys`
            replaced by the patches, a MetaTensor with following metadata:

            - `PatchKeys.LOCATION`: the starting location of the patch in the image,
            - `PatchKeys.COUNT`: total number of patches in the image,
            - "spatial_shape": spatial size of the extracted patch, and
            - "offset": the amount of offset for the patches in the image (starting position of the first patch)

    Nr|  Fr   r	   r  r  
min_offsetr   
max_offsetr  r/  r  r   r  r   r  r  r  r   r   c                 K  s4   t | ||
 td||||||||	d|| _d S )N)r  r  r  r  r  r  r  r  r   )r-   r   r    r  )r   r   r  r  r  r  r  r  r  r  r   r  r   r   r   r   	  s   	zRandGridPatchd.__init__r.  r0  r1  r   c                   rs  r   )r   r3  r  r4  r   r   r   r3  	  r5  zRandGridPatchd.set_random_stater   r   r   c                 C  sR   t |}| |D ]
}| j||   | |D ]}| j|| dd||< q|S )rJ  F)r   )r   r   r  r   r   r   r   r   r   	  s   
zRandGridPatchd.__call__)NNNr|  NNNF)r   r	   r  r  r  r   r  r   r  r/  r  r   r  r   r  r  r  r   r   r   rB  )r.  r/  r0  r1  r   r|   r   )
r   r   r   r   r    r   r   r3  r   r  r   r   r   r   r|   	  s    .r|   c                      sT   e Zd ZdZejZdejejddddfd ddZ		d!d" fddZ
d#ddZ  ZS )$r   a)  
    Dictionary-based wrapper of :py:class:`monai.transforms.RandSimulateLowResolution`.
    Random simulation of low resolution corresponding to nnU-Net's SimulateLowResolutionTransform
    (https://github.com/MIC-DKFZ/batchgenerators/blob/7651ece69faf55263dd582a9f5cbd149ed9c3ad0/batchgenerators/transforms/resample_transforms.py#L23)
    First, the array/tensor is resampled at lower resolution as determined by the zoom_factor which is uniformly sampled
    from the `zoom_range`. Then, the array/tensor is resampled at the original resolution.
    r   )g      ?r,  FNr   r	   r   r   downsample_modeInterpolateMode | strupsample_moder   r   r!  r"  r   r   c	           	      C  s\   t | || t| | || _|| _|| _|| _|| _td| j| j| j| j| jd| _	dS )a  
        Args:
            keys: keys of the corresponding items to be transformed.
            prob: probability of performing this augmentation
            downsample_mode: interpolation mode for downsampling operation
            upsample_mode: interpolation mode for upsampling operation
            zoom_range: range from which the random zoom factor for the downsampling and upsampling operation is
            sampled. It determines the shape of the downsampled tensor.
            align_corners: This only has an effect when downsample_mode or upsample_mode  is 'linear', 'bilinear',
                'bicubic' or 'trilinear'. Default: False
                See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html
            allow_missing_keys: don't raise exception if key is missing.
            device: device on which the tensor will be allocated.

        See also:
            - :py:class:`monai.transforms.compose.MapTransform`

        r,  )r   r  r  
zoom_ranger   r!  N)
r-   r   r.   r  r  r  r   r!  r"   sim_lowres_tfm)	r   r   r   r  r  r  r   r   r!  r   r   r   r   	  s   z#RandSimulateLowResolutiond.__init__r.  r/  r0  r1  c                   rs  r   )r   r3  r  r4  r   r   r   r3  ,
  r  z+RandSimulateLowResolutiond.set_random_stater   r   r   c                 C  s~   t |}| |}|dkrt|t d}|S | d | |D ]}| jr/| || ||< q t|| t tj	d||< q |S )a  
        Args:
            data: a dictionary containing the tensor-like data to be transformed. The ``keys`` specified
                in this dictionary must be tensor like arrays that are channel first and have at most
                three spatial dimensions
        r   r  Nr7  )
r   r8  r4   r   r   r   r  r  r   r  )r   r   r   r8  r;  r   r   r   r   r   3
  s   

z#RandSimulateLowResolutiond.__call__)r   r	   r   r   r  r  r  r  r   r   r!  r"  r   r   rB  )r.  r/  r0  r1  r   r   r   )r   r   r   r   r   r   r2   NEAREST	TRILINEARr   r3  r   r  r   r   r   r   r   	  s    0r   c                      s8   e Zd ZdZejZdedfd fd
dZdd Z  Z	S )ConvertBoxToPointsdzV
    Dictionary-based wrapper of :py:class:`monai.transforms.ConvertBoxToPoints`.
    pointsFr   r	   r   $str | BoxMode | type[BoxMode] | Noner   r   c                   s$   t  || || _t|d| _dS )a"  
        Args:
            keys: keys of the corresponding items to be transformed.
            point_key: key to store the point data.
            mode: the mode of the input boxes. Defaults to StandardMode.
            allow_missing_keys: don't raise exception if key is missing.
        )r   N)r   r   	point_keyr   	converter)r   r   r  r   r   r   r   r   r   R
  s   zConvertBoxToPointsd.__init__c                 C  0   t |}| |D ]}| || || j< q	|S r   )r   r   r  r  r   r   r   r   r   d
  r   zConvertBoxToPointsd.__call__)r   r	   r   r  r   r   )
r   r   r   r   r   r   r   r   r   r  r   r   r   r   r  K
  s    r  c                      s,   e Zd ZdZdd fdd	Zd
d Z  ZS )ConvertPointsToBoxesdzX
    Dictionary-based wrapper of :py:class:`monai.transforms.ConvertPointsToBoxes`.
    boxFr   r	   r   r   c                   s    t  || || _t | _dS )z
        Args:
            keys: keys of the corresponding items to be transformed.
            box_key: key to store the box data.
            allow_missing_keys: don't raise exception if key is missing.
        N)r   r   box_keyr   r  )r   r   r  r   r   r   r   r   p
  s   zConvertPointsToBoxesd.__init__c                 C  r  r   )r   r   r  r  r   r   r   r   r   {
  r   zConvertPointsToBoxesd.__call__)r  F)r   r	   r   r   )r   r   r   r   r   r   r  r   r   r   r   r  k
  s    r  )r   
__future__r   rW  collections.abcr   r   r   typingr   r   numpyr   r   monai.configr   r	   r
   monai.config.type_definitionsr   monai.data.box_utilsr   r   monai.data.meta_objr   monai.data.meta_tensorr   Z"monai.networks.layers.simplelayersr   monai.transforms.croppad.arrayr   monai.transforms.inverser   monai.transforms.spatial.arrayr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   monai.transforms.traitsr+   monai.transforms.transformr,   r-   r.   monai.transforms.utilsr/   monai.utilsr0   r1   r2   r3   r4   r5   r6   r7   monai.utils.deprecate_utilsr8   monai.utils.enumsr9   monai.utils.moduler:   nibr$  __all__r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rK   rL   rM   rN   rO   rI   rJ   rv   ry   r|   r   r  r  rP   rQ   ResampleToMatchDResampleToMatchDictrR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   ru   rw   rx   rz   r{   r}   r~   r   r   ConvertBoxToPointsDConvertBoxToPointsDictConvertPointsToBoxesDConvertPointsToBoxesDictr   r   r   r   <module>   s   
h(
Joe *_>Vl  2  >OP[~a 	>X2VeY 