U
    tPÓh‹  ã                   @  sf   d dl mZ d dlZd dlZd dlmZ d dlmZmZ d dl	Z	d dl
mZ dgZG dd„ deƒZdS )é    )ÚannotationsN)ÚCallable)ÚAnyÚOptional)Ú_LossÚ
MaskedLossc                      sB   e Zd ZdZdddddœ‡ fdd„Zdd	d	d
d	dœdd„Z‡  ZS )r   zÐ
    This is a wrapper class for the loss functions.  It allows for additional
    weighting masks to be applied to both input and target.

    See Also:
        - :py:class:`monai.losses.MaskedDiceLoss`
    z<Callable[[torch.Tensor, torch.Tensor], torch.Tensor] | _Lossr   ÚNone)ÚlossÚ	loss_argsÚloss_kwargsÚreturnc                   s:   t ƒ  ¡  t |¡r|||Žn|| _t| jƒs6tdƒ‚dS )a?  
        Args:
            loss: loss function to be wrapped, this could be a loss class or an instance of a loss class.
            loss_args: arguments to the loss function's constructor if `loss` is a class.
            loss_kwargs: keyword arguments to the loss function's constructor if `loss` is a class.
        z"The loss function is not callable.N)ÚsuperÚ__init__ÚinspectÚisclassr	   ÚcallableÚ
ValueError)Úselfr	   r
   r   ©Ú	__class__© úN/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/losses/spatial_mask.pyr   "   s
    	
ÿ
zMaskedLoss.__init__Nztorch.TensorzOptional[torch.Tensor])ÚinputÚtargetÚmaskr   c                 C  sþ   |dkrt  d¡ |  ||¡S | ¡ | ¡ krJt  d|j› d|j› d¡ |jd |jd kr†|jd dkr†td|j› d	|j› dƒ‚| ¡ dkrê|jd dkr²td
|j› dƒ‚|jdd… |jdd… krêt  d|j› d|j› d¡ |  || || ¡S )z²
        Args:
            input: the shape should be BNH[WD].
            target: the shape should be BNH[WD].
            mask: the shape should be B1H[WD] or 11H[WD].
        Nz+No mask value specified for the MaskedLoss.zDim of input (z) is different from mask (z).r   é   zBatch size of mask (z!) must be one or equal to input (zMask (z) must have only one channel.é   zSpatial size of input ()ÚwarningsÚwarnr	   ÚdimÚshaper   )r   r   r   r   r   r   r   Úforward2   s    
"zMaskedLoss.forward)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   Ú__classcell__r   r   r   r   r      s   )Ú
__future__r   r   r   Úcollections.abcr   Útypingr   r   ÚtorchÚtorch.nn.modules.lossr   Ú__all__r   r   r   r   r   Ú<module>   s   