o
     i                     @  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 g d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Zd	S )r   z+
    Base class of operation interface
    datar   kwargsreturndictc                       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i qS  )callable.0kvr   r   r   \/home/dell461/cl/sdc2/last_ska_mid/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r   r   r   r   r   r   )__name__
__module____qualname____doc__r!   r   r   r   r   r      s    r   c                      s,   e Zd ZdZdddZ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))
    r   Nonec              	   C  s4   t ttttttg ddd| _ddddd| _d S )	N)g      ?
   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   r   c                   s   t  j|fi |}| j D ] \}}|d }|d }t|tr/||v r/|||| | i q| D ]
\}}| ||< q4|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#   r$   r%   r&   r5   r!   __classcell__r   r   r>   r   r   ,   s    
r   c                   @  s$   e Zd ZdZdddZ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   r   c                   r   )z
        Applies the callables to the data, and convert the numerics to list or Python
        numeric types (int/float).

        Args:
            data: input data
        c                   s:   i | ]\}}t |r| v r|| | fi  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.evaluateNr@   r"   )r#   r$   r%   r&   r5   r!   r   r   r   r   r   j   s    
r   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    >