U
    Ph                     @  s   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 dddgZG d	d deZG d
d deZG dd deZdS )    )annotations)UserDict)partial)Any)maxmeanmedianmin
percentilestd
OperationsSampleOperationsSummaryOperationsc                   @  s"   e Zd ZdZddddddZdS )r   z+
    Base class of operation interface
    r   dictdatakwargsreturnc                   s    fdd| j  D S )a  
        For key-value pairs in the self.data, if the value is a callable,
        then this function will apply the callable to the input data.
        The result will be written under the same key under the output dict.

        Args:
            data: input data.

        Returns:
            a dictionary which has same keys as the self.data if the value
                is callable.
        c                   s&   i | ]\}}t |r|| fqS  )callable.0kvr   r   r   O/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/auto3dseg/operations.py
<dictcomp>)   s       z'Operations.evaluate.<locals>.<dictcomp>r   itemsselfr   r   r   r   r   evaluate   s    zOperations.evaluateN)__name__
__module____qualname____doc__r!   r   r   r   r   r      s   c                      s8   e Zd ZdZddddZdddd fd	d
Z  ZS )r   aY  
    Apply statistical operation to a sample (image/ndarray/tensor).

    Notes:
        Percentile operation uses a partial function that embeds different kwargs (q).
        In order to print the result nicely, data_addon is added to map the numbers
        generated by percentile to different keys ("percentile_00_5" for example).
        Annotation of the postfix means the percentage for percentile computation.
        For example, _00_5 means 0.5% and _99_5 means 99.5%.

    Example:

        .. code-block:: python

            # use the existing operations
            import numpy as np
            op = SampleOperations()
            data_np = np.random.rand(10, 10).astype(np.float64)
            print(op.evaluate(data_np))

            # add a new operation
            op.update({"sum": np.sum})
            print(op.evaluate(data_np))
    Noner   c                 C  s8   t ttttttddddgdd| _ddd	d
d| _d S )Ng      ?
   Z   g     X@)q)r   r   r   r	   stdevr
   )r
   r   )r
      )r
      )r
      )percentile_00_5percentile_10_0percentile_90_0percentile_99_5)	r   r   r   r	   r   r   r
   r   
data_addonr    r   r   r   __init__F   s    	zSampleOperations.__init__r   r   r   c                   s~   t  j|f|}| j D ]@\}}|d }|d }t|tr||kr|||| | i q| D ]\}}| ||< qd|S )z
        Applies the callables to the data, and convert the
        numerics to list or Python numeric types (int/float).

        Args:
            data: input data
        r   r,   )superr!   r3   r   
isinstancetupleupdatetolist)r    r   r   retr   r   cacheidx	__class__r   r   r!   V   s    zSampleOperations.evaluate)r"   r#   r$   r%   r5   r!   __classcell__r   r   r>   r   r   ,   s   c                   @  s0   e Zd ZdZddddZddddd	d
ZdS )r   aW  
    Apply statistical operation to summarize a dict. The key-value looks like: {"max", "min"
    ,"mean", ....}. The value may contain multiple values in a list format. Then this operation
    will apply the operation to the list. Typically, the dict is generated by multiple
    `SampleOperation` and `concat_multikeys_to_dict` functions.

    Examples:

        .. code-block:: python

            import numpy as np
            data = {
                "min": np.random.rand(4),
                "max": np.random.rand(4),
                "mean": np.random.rand(4),
                "sum": np.random.rand(4),
            }
            op = SummaryOperations()
            print(op.evaluate(data)) # "sum" is not registered yet, so it won't contain "sum"

            op.update({"sum", np.sum})
            print(op.evaluate(data)) # output has "sum"
    r&   r'   c              
   C  s   t ttttttttd	| _d S )N)	r   r   r   r	   r+   r/   r0   r1   r2   )r   r   r	   r   r4   r   r   r   r5      s    zSummaryOperations.__init__r   r   r   c                   s    fdd| j  D S )z
        Applies the callables to the data, and convert the numerics to list or Python
        numeric types (int/float).

        Args:
            data: input data
        c                   s6   i | ].\}}t |r| kr|| | f qS r   )r   r:   r   r   r   r   r      s
        z.SummaryOperations.evaluate.<locals>.<dictcomp>r   r   r   r   r   r!      s    zSummaryOperations.evaluateN)r"   r#   r$   r%   r5   r!   r   r   r   r   r   j   s   N)
__future__r   collectionsr   	functoolsr   typingr   0monai.transforms.utils_pytorch_numpy_unificationr   r   r   r	   r
   r   __all__r   r   r   r   r   r   r   <module>   s    
>