o
    iI                     @  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	 d dl
mZmZmZ edejed\ZZer;d d	lmZ n
edejed
\ZZG dd dZdS )    )annotations)TYPE_CHECKING)KeysCollection)IterationEvents)
Decollated)
IgniteInfomin_versionoptional_importzignite.engineEvents)Enginer   c                   @  s>   e Zd ZdZ							ddddZdddZ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eventstrdetachbooldecollate_batch
batch_keysKeysCollection | Nonedecollate_outputoutput_keysallow_missing_keysc                 C  sX   |  }|dvrtd|| _|rt|||dnd | _|r't|||d| _d S 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   `/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/handlers/decollate_batch.py__init__3   s   
zDecollateBatch.__init__enginer   returnNonec                 C  s.   | j dkr|tj|  dS |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  sh   | j durt|jjttfr|  |jj|j_| jdur0t|jjttfr2| |jj|j_dS dS dS )r%   N)r   
isinstancestatebatchlistdictr   outputr'   r   r   r    __call__X   s
   zDecollateBatch.__call__)r   TTNTNF)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r"   r   r#   r$   )__name__
__module____qualname____doc__r!   r(   r/   r   r   r   r    r      s    

r   N)
__future__r   typingr   monai.configr   monai.engines.utilsr   monai.transformsr   monai.utilsr   r   r	   OPT_IMPORT_VERSIONr
   _ignite.enginer   r   r   r   r   r    <module>   s   