o
    i                     @  sl   d dl mZ d dlZd dlmZmZ d dlZd dlZd dl	m
Z
 d dlmZ d dlmZ G dd deZdS )	    )annotationsN)HashableMapping)KeysCollection)MapTransform)ImageMetaKeyc                      s8   e Zd ZdZ					dd fddZdddZ  ZS )EnsureSameShapeda  
    Checks if segmentation label images (in keys) have the same spatial shape as the main image (in source_key),
    and raise an error if the shapes are significantly different.
    If the shapes are only slightly different (within an allowed_shape_difference in each dim), then resize the label using
    nearest interpolation. This transform is designed to correct datasets with slight label shape mismatches.
    Generally image and segmentation label must have the same spatial shape, however some public datasets are having slight
    shape mismatches, which will cause potential crashes when calculating loss or metric functions.
    labelFimage   Tkeysr   allow_missing_keysbool
source_keystrallowed_shape_differenceintwarnreturnNonec                   s&   t  j||d || _|| _|| _dS )a  
        Args:
            keys: keys of the corresponding items to be compared to the source_key item shape.
            allow_missing_keys: do not raise exception if key is missing.
            source_key: key of the item with the reference shape.
            allowed_shape_difference: raises error if shapes are different more than this value in any dimension,
                otherwise corrects for the shape mismatch using nearest interpolation.
            warn: if `True` prints a warning if the label image is resized


        )r   r   N)super__init__r   r   r   )selfr   r   r   r   r   	__class__ a/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/apps/auto3dseg/transforms.pyr   #   s   
zEnsureSameShaped.__init__dataMapping[Hashable, torch.Tensor]dict[Hashable, torch.Tensor]c                 C  s  t |}|| j jdd  }| |D ]q}|| jdd  }||krd}t|| dr<t|| jtr<|| jt	j
}tjt|t|| jdrs| jr^td| d| d| d| d		 tjjj|| d
|ddd
||< qtd| d| d| d| d		q|S )N    meta)atolzThe z with shape z' was resized to match the source shape z, the metadata was not updated .r   znearest-exact)inputsizemodez shape z$ is different from the source shape  )dictr   shapekey_iteratorhasattr
isinstancer"   r   getr   FILENAME_OR_OBJnpallcloselistr   r   warningstorchnn
functionalinterpolate	unsqueezesqueeze
ValueError)r   r   dimage_shapekeyZlabel_shapefilenamer   r   r   __call__;   s2   zEnsureSameShaped.__call__)r	   Fr
   r   T)r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   )__name__
__module____qualname____doc__r   r?   __classcell__r   r   r   r   r      s    r   )
__future__r   r3   collections.abcr   r   numpyr0   r4   monai.configr   monai.transformsr   monai.utils.miscr   r   r   r   r   r   <module>   s   