U
    PhT                  
   @  sz  d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	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 ddlmZ dd	lmZ dd
lmZmZmZ ddlmZm Z m!Z! ddl"m#Z# ddl$m%Z% dddddddgZ&edZ'd)ddddddddddZ(d*dddddddd!d"d#dZ)G d$d deeZ*G d%d deZ+G d&d de+eZ,G d'd de*e+Z-G d(d de+Z.dS )+z:
A collection of generic interfaces for MONAI transforms.
    )annotationsN)ABCabstractmethod)Callable	GeneratorHashableIterableMapping)AnyTypeVar)config
transforms)KeysCollection)
MetaTensor)	LazyTraitRandomizableTraitThreadUnsafe)MAX_SEEDensure_tuplefirst)TransformBackends)MONAIEnvVarsr   apply_transformRandomizableLazyTransformRandomizableTransform	TransformMapTransform
ReturnTypeFzCallable[..., ReturnType]r
   boolbool | Nonezdict | Nonez
bool | str)	transformdataunpack_parameterslazy	overrideslogger_namereturnc                 C  sh   ddl m} || ||||}t|trJ|rJt| trB| |d|iS | | S t| tr`| ||dS | |S )a%
  
    Perform a transform 'transform' on 'data', according to the other parameters specified.

    If `data` is a tuple and `unpack_parameters` is True, each parameter of `data` is unpacked
    as arguments to `transform`. Otherwise `data` is considered as single argument to `transform`.

    If 'lazy' is True, this method first checks whether it can execute this method lazily. If it
    can't, it will ensure that all pending lazy transforms on 'data' are applied before applying
    this 'transform' to it. If 'lazy' is True, and 'overrides' are provided, those overrides will
    be applied to the pending operations on 'data'. See ``Compose`` for more details on lazy
    resampling, which is an experimental feature for 1.2.

    Please note, this class is function is designed to be called by ``apply_transform``.
    In general, you should not need to make specific use of it unless you are implementing
    pipeline execution mechanisms.

    Args:
        transform: a callable to be used to transform `data`.
        data: the tensorlike or dictionary of tensorlikes to be executed on
        unpack_parameters: whether to unpack parameters for `transform`. Defaults to False.
        lazy: whether to enable lazy evaluation for lazy transforms. If False, transforms will be
            carried out on a transform by transform basis. If True, all lazy transforms will
            be executed by accumulating changes and resampling as few times as possible.
            See the :ref:`Lazy Resampling topic<lazy_resampling> for more information about lazy resampling.
        overrides: this optional parameter allows you to specify a dictionary of parameters that should be overridden
            when executing a pipeline. These each parameter that is compatible with a given transform is then applied
            to that transform before it is executed. Note that overrides are currently only applied when
            :ref:`Lazy Resampling<lazy_resampling>` is enabled for the pipeline or a given transform. If lazy is False
            they are ignored. Currently supported args are:
            {``"mode"``, ``"padding_mode"``, ``"dtype"``, ``"align_corners"``, ``"resample_mode"``, ``device``}.
        logger_name: this optional parameter allows you to specify a logger by name for logging of pipeline execution.
            Setting this to False disables logging. Setting it to True enables logging to the default loggers.
            Setting a string overrides the logger name to which logging is performed.

    Returns:
        ReturnType: The return type of `transform`.
    r   )!apply_pending_transforms_in_orderr$   r$   ) monai.transforms.lazy.functionalr(   
isinstancetupler   )r!   r"   r#   r$   r%   r&   r(    r-   O/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/transforms/transform.py_apply_transform.   s
    - r/   Tzlist[ReturnType] | ReturnType)r!   r"   	map_itemsunpack_items	log_statsr$   r%   r'   c              
     sX  zBt |ttfr0|r0fdd|D W S t|W S  tk
rR } zt r` dk	r2t tjj	s2t t
rtjjjddd ntjjjddd t j}|dtj d t |ttfr|d }dd
d fdd}	t |tr(| D ]\}
}|	||
d qn
|	|d td |W 5 d}~X Y nX dS )a  
    Transform `data` with `transform`.

    If `data` is a list or tuple and `map_data` is True, each item of `data` will be transformed
    and this method returns a list of outcomes.
    otherwise transform will be applied once with `data` as the argument.

    Args:
        transform: a callable to be used to transform `data`.
        data: an object to be transformed.
        map_items: whether to apply transform to each item in `data`,
            if `data` is a list or tuple. Defaults to True.
        unpack_items: whether to unpack parameters using `*`. Defaults to False.
        log_stats: log errors when they occur in the processing pipeline. By default, this is set to False, which
            disables the logger for processing pipeline errors. Setting it to None or True will enable logging to the
            default logger name. Setting it to a string specifies the logger to which errors should be logged.
        lazy: whether to execute in lazy mode or not. See the :ref:`Lazy Resampling topic<lazy_resampling> for more
            information about lazy resampling. Defaults to None.
        overrides: optional overrides to apply to transform parameters. This parameter is ignored unless transforms
            are being executed lazily. See the :ref:`Lazy Resampling topic<lazy_resampling> for more details and
            examples of its usage.

    Raises:
        Exception: When ``transform`` raises an exception.

    Returns:
        Union[List[ReturnType], ReturnType]: The return type of `transform` or a list thereof.
    c              	     s   g | ]}t | qS r-   )r/   ).0item)r$   r2   r%   r!   r1   r-   r.   
<listcomp>   s     z#apply_transform.<locals>.<listcomp>F)
data_shapevalue_rangename)r6   r7   z
=== Transform input info -- z ===r   Dataz
str | None)prefixc                   s6   t | tjtjfr$ | dd|d n | d|d d S )NT)imgr6   r7   r:   )r;   
data_valuer:   )r+   npndarraytorchTensorr"   r:   )	datastatsr-   r.   
_log_stats   s    z#apply_transform.<locals>._log_statsrA   r"   zapplying transform N)r9   )r+   listr,   r/   	Exceptionr   debugr   composeComposestrZutilityarray	DataStatslogging	getLoggerZ_logger_nameerrortype__name__dictitemsRuntimeError)r!   r"   r0   r1   r2   r$   r%   eloggerrC   kvr-   )rB   r$   r2   r%   r!   r1   r.   r   e   s*    %

c                   @  sH   e Zd ZU dZej Zded< dddd ddd	Z	d
ddddZ
dS )r   a7  
    An interface for handling random state locally, currently based on a class
    variable `R`, which is an instance of `np.random.RandomState`.  This
    provides the flexibility of component-specific determinism without
    affecting the global states.  It is recommended to use this API with
    :py:class:`monai.data.DataLoader` for deterministic behaviour of the
    preprocessing pipelines. This API is not thread-safe. Additionally,
    deepcopying instance of this class often causes insufficient randomness as
    the random states will be duplicated.
    znp.random.RandomStateRNz
int | Noneznp.random.RandomState | None)seedstater'   c                 C  s   |dk	r>t |ttjfs t|n|}|t }tj|| _| S |dk	rtt |tjjsjt	dt
|j d|| _| S tj | _| S )a  
        Set the random state locally, to control the randomness, the derived
        classes should use :py:attr:`self.R` instead of `np.random` to introduce random
        factors.

        Args:
            seed: set the random state with an integer seed.
            state: set the random state with a `np.random.RandomState` object.

        Raises:
            TypeError: When ``state`` is not an ``Optional[np.random.RandomState]``.

        Returns:
            a Randomizable instance.

        Nz5state must be None or a np.random.RandomState but is .)r+   intr=   integeridr   randomRandomStaterY   	TypeErrorrP   rQ   )selfrZ   r[   _seedr-   r-   r.   set_random_state   s    zRandomizable.set_random_stater
   Noner"   r'   c                 C  s   t d| jj ddS )a  
        Within this method, :py:attr:`self.R` should be used, instead of `np.random`, to introduce random factors.

        all :py:attr:`self.R` calls happen here so that we have a better chance to
        identify errors of sync the random state.

        This method can generate the random factors based on properties of the input data.

        Raises:
            NotImplementedError: When the subclass does not override this method.
        	Subclass  must implement this method.NNotImplementedError	__class__rQ   rc   r"   r-   r-   r.   	randomize   s    zRandomizable.randomize)NN)rQ   
__module____qualname____doc__r=   r`   ra   rY   __annotations__re   rn   r-   r-   r-   r.   r      s   
 c                   @  s0   e Zd ZU dZg Zded< eddddZdS )	r   a:  
    An abstract class of a ``Transform``.
    A transform is callable that processes ``data``.

    It could be stateful and may modify ``data`` in place,
    the implementation should be aware of:

        #. thread safety when mutating its own states.
           When used from a multi-process context, transform's instance variables are read-only.
           thread-unsafe transforms should inherit :py:class:`monai.transforms.ThreadUnsafe`.
        #. ``data`` content unused by this transform may still be used in the
           subsequent transforms in a composed transform.
        #. storing too much information in ``data`` may cause some memory issue or IPC sync issue,
           especially in the multi-processing environment of PyTorch DataLoader.

    See Also

        :py:class:`monai.transforms.Compose`
    zlist[TransformBackends]backendr
   rD   c                 C  s   t d| jj ddS )a  
        ``data`` is an element which often comes from an iteration over an
        iterable, such as :py:class:`torch.utils.data.Dataset`. This method should
        return an updated version of ``data``.
        To simplify the input validations, most of the transforms assume that

        - ``data`` is a Numpy ndarray, PyTorch Tensor or string,
        - the data shape can be:

          #. string data without shape, `LoadImage` transform expects file paths,
          #. most of the pre-/post-processing transforms expect: ``(num_channels, spatial_dim_1[, spatial_dim_2, ...])``,
             except for example: `AddChannel` expects (spatial_dim_1[, spatial_dim_2, ...])

        - the channel dimension is often not omitted even if number of channels is one.

        This method can optionally take additional arguments to help execute transformation operation.

        Raises:
            NotImplementedError: When the subclass does not override this method.

        rh   ri   Nrj   rm   r-   r-   r.   __call__	  s    zTransform.__call__N)rQ   ro   rp   rq   rs   rr   r   rt   r-   r-   r-   r.   r      s   
c                   @  sL   e Zd ZdZdddddZedd Zejddd	dZed
d ZdS )r   z
    An implementation of functionality for lazy transforms that can be subclassed by array and
    dictionary transforms to simplify implementation of new lazy transforms.
    Fr    r)   c                 C  s.   |d k	r$t |ts$tdt| || _d S Nz#lazy must be a bool but is of type r+   r   rb   rP   _lazyrc   r$   r-   r-   r.   __init__)  s    
zLazyTransform.__init__c                 C  s   | j S )N)rw   rc   r-   r-   r.   r$   /  s    zLazyTransform.lazyc                 C  s.   |d k	r$t |ts$tdt| || _d S ru   rv   rx   r-   r-   r.   r$   3  s    
c                 C  s   dS )NFr-   rz   r-   r-   r.   requires_current_data:  s    z#LazyTransform.requires_current_dataN)F)	rQ   ro   rp   rq   ry   propertyr$   setterr{   r-   r-   r-   r.   r   #  s   
c                   @  s2   e Zd ZdZddddddZd	d
dddZdS )r   ag  
    An interface for handling random state locally, currently based on a class variable `R`,
    which is an instance of `np.random.RandomState`.
    This class introduces a randomized flag `_do_transform`, is mainly for randomized data augmentation transforms.
    For example:

    .. code-block:: python

        from monai.transforms import RandomizableTransform

        class RandShiftIntensity100(RandomizableTransform):
            def randomize(self):
                super().randomize(None)
                self._offset = self.R.uniform(low=0, high=100)

            def __call__(self, img):
                self.randomize()
                if not self._do_transform:
                    return img
                return img + self._offset

        transform = RandShiftIntensity()
        transform.set_random_state(seed=0)
        print(transform(10))

          ?Tfloatr   )probdo_transformc                 C  s   || _ tt|dd| _d S )Ng        r~   )_do_transformminmaxr   )rc   r   r   r-   r-   r.   ry   [  s    zRandomizableTransform.__init__r
   rf   rg   c                 C  s   | j  | jk | _dS )a^  
        Within this method, :py:attr:`self.R` should be used, instead of `np.random`, to introduce random factors.

        all :py:attr:`self.R` calls happen here so that we have a better chance to
        identify errors of sync the random state.

        This method can generate the random factors based on properties of the input data.
        N)rY   randr   r   rm   r-   r-   r.   rn   _  s    	zRandomizableTransform.randomizeN)r~   T)rQ   ro   rp   rq   ry   rn   r-   r-   r-   r.   r   ?  s   c                      sh   e Zd ZdZdd Zddddd fd	d
Zdd Zedd ZddddddZ	ddddZ
  ZS )r   aV  
    A subclass of :py:class:`monai.transforms.Transform` with an assumption
    that the ``data`` input of ``self.__call__`` is a MutableMapping such as ``dict``.

    The ``keys`` parameter will be used to get and set the actual data
    item to transform.  That is, the callable of this transform should
    follow the pattern:

        .. code-block:: python

            def __call__(self, data):
                for key in self.keys:
                    if key in data:
                        # update output data with some_transform_function(data[key]).
                    else:
                        # raise exception unless allow_missing_keys==True.
                return data

    Raises:
        ValueError: When ``keys`` is an empty iterable.
        TypeError: When ``keys`` type is not in ``Union[Hashable, Iterable[Hashable]]``.

    c                 O  sB   t jr8t| jtjd| _t| dr8t| jtj	j
| _t| S )Npostinverse)r   USE_META_DICTr   attach_hookrt   r   call_updatehasattrr   InvertibleTransforminverse_updater   __new__)clsargskwargsr-   r-   r.   r     s
    
zMapTransform.__new__Fr   r   rf   )keysallow_missing_keysr'   c                   sX   t    t|| _|| _| js(td| jD ]$}t|ts.tdt	|j
 dq.d S )Nzkeys must be non empty.z:keys must be one of (Hashable, Iterable[Hashable]) but is r\   )superry   r   r   r   
ValueErrorr+   r   rb   rP   rQ   )rc   r   r   keyrl   r-   r.   ry     s    



zMapTransform.__init__c                 C  s   t |tttfs|S d}t |tr.|gd }}|r@t |d tsP|rL|d S |S dd |D }t|D ]@\}}|D ]2}t || tsqrtj||t | tj d||< qrqf|r|d S |S )z
        This function is to be called after every `self.__call__(data)`,
        update `data[key_transforms]` and `data[key_meta_dict]` using the content from MetaTensor `data[key]`,
        for MetaTensor backward compatibility 0.9.0.
        FTr   c                 S  s   g | ]}t |qS r-   )rR   )r3   xr-   r-   r.   r5     s     z,MapTransform.call_update.<locals>.<listcomp>)t)	r+   rE   r,   r	   	enumerater   r   sync_meta_infoInvertD)rc   r"   is_dictZlist_didxZdict_irW   r-   r-   r.   r     s    
"zMapTransform.call_updatec                 C  s   t d| jj ddS )a  
        ``data`` often comes from an iteration over an iterable,
        such as :py:class:`torch.utils.data.Dataset`.

        To simplify the input validations, this method assumes:

        - ``data`` is a Python dictionary,
        - ``data[key]`` is a Numpy ndarray, PyTorch Tensor or string, where ``key`` is an element
          of ``self.keys``, the data shape can be:

          #. string data without shape, `LoadImaged` transform expects file paths,
          #. most of the pre-/post-processing transforms expect: ``(num_channels, spatial_dim_1[, spatial_dim_2, ...])``,
             except for example: `AddChanneld` expects (spatial_dim_1[, spatial_dim_2, ...])

        - the channel dimension is often not omitted even if number of channels is one.

        Raises:
            NotImplementedError: When the subclass does not override this method.

        returns:
            An updated dictionary version of ``data`` by applying the transform.

        rh   ri   Nrj   rm   r-   r-   r.   rt     s    zMapTransform.__call__zMapping[Hashable, Any]zIterable | Noner   )r"   extra_iterablesr'   c                 g  sv   |pdgt | j g}t| jf| D ]J^}}||krP|rH|ft| n|V  q&| js&td| d| jj dq&dS )a[  
        Iterate across keys and optionally extra iterables. If key is missing, exception is raised if
        `allow_missing_keys==False` (default). If `allow_missing_keys==True`, key is skipped.

        Args:
            data: data that the transform will be applied to
            extra_iterables: anything else to be iterated through
        NzKey `z` of transform `z8` was missing in the data and allow_missing_keys==False.)lenr   zipr,   r   KeyErrorrl   rQ   )rc   r"   r   Zex_itersr   Z	_ex_itersr-   r-   r.   key_iterator  s    
zMapTransform.key_iteratorzdict[Hashable, Any]rD   c                 C  s   t | |dS )z
        Get the first available key of `self.keys` in the input `data` dictionary.
        If no available key, return an empty tuple `()`.

        Args:
            data: data that the transform will be applied to.

        r-   )r   r   rm   r-   r-   r.   	first_key  s    	zMapTransform.first_key)F)rQ   ro   rp   rq   r   ry   r   r   rt   r   r   __classcell__r-   r-   r   r.   r   k  s   


)FFNF)TFFNN)/rq   
__future__r   rM   abcr   r   collections.abcr   r   r   r   r	   typingr
   r   numpyr=   r?   monair   r   monai.configr   monai.data.meta_tensorr   monai.transforms.traitsr   r   r   monai.utilsr   r   r   monai.utils.enumsr   monai.utils.miscr   __all__r   r/   r   r   r   r   r   r   r-   r-   r-   r.   <module>   sN   
    :     I=8,