U
    Ph                      @  s   d Z ddlmZ ddlmZ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 dd	lmZmZ dd
lmZ ddddddgZG dd deeZG dd deeZe ZZe ZZdS )z
A collection of dictionary-based wrappers for moving between MetaTensor types and dictionaries of data.
These can be used to make backwards compatible code.

Class names are ended with 'd' to denote dictionary-based transforms.
    )annotations)HashableMappingSequenceN)KeysCollectionNdarrayOrTensor)
MetaTensor)InvertibleTransform)MapTransform)PostFixTransformBackends)ensure_tuple_repFromMetaTensordFromMetaTensorDFromMetaTensorDictToMetaTensordToMetaTensorDToMetaTensorDictc                      s\   e Zd ZdZejejejgZddddd fdd	Z	d
ddddZ
d
ddddZ  ZS )r   z
    Dictionary-based transform to convert MetaTensor to a dictionary.

    If input is `{"a": MetaTensor, "b": MetaTensor}`, then output will
    have the form `{"a": torch.Tensor, "a_meta_dict": dict, "a_transforms": list, "b": ...}`.
    tensorFr   zSequence[str] | strbool)keys	data_typeallow_missing_keysc                   s2   t  || tdd t|t| jD | _dS )aD  
        Args:
            keys: keys of the corresponding items to be transformed.
                See also: :py:class:`monai.transforms.compose.MapTransform`
            data_type: target data type to convert, should be "tensor" or "numpy".
            allow_missing_keys: don't raise exception if key is missing.
        c                 s  s   | ]}|d kV  qdS )r   N ).0dr   r   ]/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/transforms/meta_utility/dictionary.py	<genexpr>?   s     z+FromMetaTensord.__init__.<locals>.<genexpr>N)super__init__tupler   lenr   as_tensor_output)selfr   r   r   	__class__r   r   r   4   s    
zFromMetaTensord.__init__"Mapping[Hashable, NdarrayOrTensor]dict[Hashable, NdarrayOrTensor]datareturnc                 C  sX   t |}| || jD ]<\}}|| }||j||r:tjntjd | 	|| q|S )N)output_type)
dictkey_iteratorr"   updateas_dicttorchTensornpndarraypush_transform)r#   r)   r   keytimr   r   r   __call__A   s     zFromMetaTensord.__call__c                 C  sv   t |}| |D ]^}| ||}|| }|t|d }|t|d }t|||d}|||< | || q|S N)metaapplied_operations)	r,   r-   get_most_recent_transformpopr   r:   
transformsr   pop_transform)r#   r)   r   r5   _r7   r:   r>   r   r   r   inverseI   s    zFromMetaTensord.inverse)r   F)__name__
__module____qualname____doc__r   TORCHNUMPYCUPYbackendr   r8   rA   __classcell__r   r   r$   r   r   *   s      c                   @  s@   e Zd ZdZejejejgZdddddZ	dddddZ
d	S )
r   z
    Dictionary-based transform to convert a dictionary to MetaTensor.

    If input is `{"a": torch.Tensor, "a_meta_dict": dict, "b": ...}`, then output will
    have the form `{"a": MetaTensor, "b": MetaTensor}`.
    r&   r'   r(   c                 C  sj   t |}| |D ]R}| || || }|t|d }|t|d }t|||d}|||< q|S r9   )r,   r-   r4   r=   r   r:   r>   r   )r#   r)   r   r5   r7   r:   r>   r   r   r   r8   c   s    
zToMetaTensord.__call__c                 C  sL   t |}| |D ]4}| ||}|| }||| | || q|S )N)r,   r-   r<   r.   r/   r?   )r#   r)   r   r5   r@   r7   r   r   r   rA   n   s    zToMetaTensord.inverseN)rB   rC   rD   rE   r   rF   rG   rH   rI   r8   rA   r   r   r   r   r   Y   s   )rE   
__future__r   collections.abcr   r   r   numpyr2   r0   monai.config.type_definitionsr   r   monai.data.meta_tensorr   monai.transforms.inverser	   monai.transforms.transformr
   monai.utils.enumsr   r   monai.utils.miscr   __all__r   r   r   r   r   r   r   r   r   r   <module>   s*   
/"