U
    {PhI                     @  s   d dl 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mZ edejed\ZZervd d	lmZ nedejed
\ZZG dd dZdS )    )annotations)TYPE_CHECKING)
IgniteInfoKeysCollection)IterationEvents)
Decollated)min_versionoptional_importzignite.engineEvents)Enginer   c                	   @  sL   e Zd ZdZddddddddd	d
dZdddddZdddddZdS )DecollateBatcha  
    Ignite handler to execute the `decollate batch` logic for `engine.state.batch` and `engine.state.output`.
    Typical usage is to set `decollate=False` in the engine and execute some postprocessing logic first
    then decollate the batch, otherwise, engine will decollate batch before the postprocessing.

    Args:
        event: expected EVENT to attach the handler, should be "MODEL_COMPLETED" or "ITERATION_COMPLETED".
            default to "MODEL_COMPLETED".
        detach: whether to detach the tensors. scalars tensors will be detached into number types
            instead of torch tensors.
        decollate_batch: whether to decollate `engine.state.batch` of ignite engine.
        batch_keys: if `decollate_batch=True`, specify the keys of the corresponding items to decollate
            in `engine.state.batch`, note that it will delete other keys not specified. if None,
            will decollate all the keys. it replicates the scalar values to every item of the decollated list.
        decollate_output: whether to decollate `engine.state.output` of ignite engine.
        output_keys: if `decollate_output=True`, specify the keys of the corresponding items to decollate
            in `engine.state.output`, note that it will delete other keys not specified. if None,
            will decollate all the keys. it replicates the scalar values to every item of the decollated list.
        allow_missing_keys: don't raise exception if key is missing.

    MODEL_COMPLETEDTNFstrboolzKeysCollection | None)eventdetachdecollate_batch
batch_keysdecollate_outputoutput_keysallow_missing_keysc                 C  sR   |  }|dkrtd|| _|r0t|||dnd | _|rHt|||dnd | _d S )N)r   ITERATION_COMPLETEDz;event should be `MODEL_COMPLETED` or `ITERATION_COMPLETED`.)keysr   r   )upper
ValueErrorr   r   batch_transformoutput_transform)selfr   r   r   r   r   r   r    r   S/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/handlers/decollate_batch.py__init__3   s    
zDecollateBatch.__init__r   None)enginereturnc                 C  s,   | j dkr|tj|  n|tj|  dS )g
        Args:
            engine: Ignite Engine, it can be a trainer, validator or evaluator.
        r   N)r   add_event_handlerr   r   r
   r   r   r"   r   r   r   attachN   s    
zDecollateBatch.attachc                 C  s`   | j dk	r.t|jjttfr.|  |jj|j_| jdk	r\t|jjttfr\| |jj|j_dS )r$   N)r   
isinstancestatebatchlistdictr   outputr&   r   r   r   __call__X   s    zDecollateBatch.__call__)r   TTNTNF)__name__
__module____qualname____doc__r    r'   r.   r   r   r   r   r      s          
r   N)
__future__r   typingr   monai.configr   r   monai.engines.utilsr   monai.transformsr   monai.utilsr   r	   OPT_IMPORT_VERSIONr
   _ignite.enginer   r   r   r   r   r   <module>   s   