o
    i%                     @  s   d dl mZ d dlmZ d dlmZ d dlZd dlZd dl	m
Z
mZmZmZ d dlmZmZ ddlmZ G d	d
 d
eZ				ddddZdS )    )annotations)Sequence)AnyN)do_metric_reductionget_edge_surface_distanceignore_backgroundprepare_spacing)MetricReductionconvert_data_type   )CumulativeIterationMetricc                      sH   e Zd ZdZdddejdfd fddZdddZ	ddddZ  Z	S ) SurfaceDistanceMetrica  
    Compute Surface Distance between two tensors. It can support both multi-classes and multi-labels tasks.
    It supports both symmetric and asymmetric surface distance calculation.
    Input `y_pred` is compared with ground truth `y`.
    `y_preds` is expected to have binarized predictions and `y` should be in one-hot format.
    You can use suitable transforms in ``monai.transforms.post`` first to achieve binarized values.
    `y_preds` and `y` can be a list of channel-first Tensor (CHW[D]) or a batch-first Tensor (BCHW[D]).

    Example of the typical execution steps of this metric class follows :py:class:`monai.metrics.metric.Cumulative`.

    Args:
        include_background: whether to include distance computation on the first channel of
            the predicted output. Defaults to ``False``.
        symmetric: whether to calculate the symmetric average surface distance between
            `seg_pred` and `seg_gt`. Defaults to ``False``.
        distance_metric: : [``"euclidean"``, ``"chessboard"``, ``"taxicab"``]
            the metric used to compute surface distance. Defaults to ``"euclidean"``.
        reduction: define mode of reduction to the metrics, will only apply reduction on `not-nan` values,
            available reduction modes: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``,
            ``"mean_channel"``, ``"sum_channel"``}, default to ``"mean"``. if "none", will not do reduction.
        get_not_nans: whether to return the `not_nans` count, if True, aggregate() returns (metric, not_nans).
            Here `not_nans` count the number of not nans for the metric, thus its shape equals to the shape of the metric.

    F	euclideaninclude_backgroundbool	symmetricdistance_metricstr	reductionMetricReduction | strget_not_nansreturnNonec                   s,   t    || _|| _|| _|| _|| _d S N)super__init__r   r   r   r   r   )selfr   r   r   r   r   	__class__ `/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/metrics/surface_distance.pyr   4   s   

zSurfaceDistanceMetric.__init__y_predtorch.Tensorykwargsr   c              	   K  s4   |  dk r
tdt||| j| j| j|ddS )a  
        Args:
            y_pred: input data to compute, typical segmentation model output.
                It must be one-hot format and first dim is batch, example shape: [16, 3, 32, 32]. The values
                should be binarized.
            y: ground truth to compute the distance. It must be one-hot format and first dim is batch.
                The values should be binarized.
            kwargs: additional parameters, e.g. ``spacing`` should be passed to correctly compute the metric.
                ``spacing``: spacing of pixel (or voxel). This parameter is relevant only
                if ``distance_metric`` is set to ``"euclidean"``.
                If a single number, isotropic spacing with that value is used for all images in the batch. If a sequence of numbers,
                the length of the sequence must be equal to the image dimensions.
                This spacing will be used for all images in the batch.
                If a sequence of sequences, the length of the outer sequence must be equal to the batch size.
                If inner sequence has length 1, isotropic spacing with that value is used for all images in the batch,
                else the inner sequence length must be equal to the image dimensions. If ``None``, spacing of unity is used
                for all images in batch. Defaults to ``None``.

        Raises:
            ValueError: when `y_pred` has less than three dimensions.
           z-y_pred should have at least three dimensions.spacing)r!   r#   r   r   r   r&   )dim
ValueError compute_average_surface_distancer   r   r   get)r   r!   r#   r$   r   r   r    _compute_tensorC   s   z%SurfaceDistanceMetric._compute_tensorNMetricReduction | str | None0torch.Tensor | tuple[torch.Tensor, torch.Tensor]c                 C  sB   |   }t|tjstdt||p| j\}}| jr||fS |S )a  
        Execute reduction logic for the output of `compute_average_surface_distance`.

        Args:
            reduction: define mode of reduction to the metrics, will only apply reduction on `not-nan` values,
                available reduction modes: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``,
                ``"mean_channel"``, ``"sum_channel"``}, default to `self.reduction`. if "none", will not do reduction.

        z-the data to aggregate must be PyTorch Tensor.)
get_buffer
isinstancetorchTensorr(   r   r   r   )r   r   datafnot_nansr   r   r    	aggregatef   s
   zSurfaceDistanceMetric.aggregate)r   r   r   r   r   r   r   r   r   r   r   r   )r!   r"   r#   r"   r$   r   r   r"   r   )r   r,   r   r-   )
__name__
__module____qualname____doc__r	   MEANr   r+   r5   __classcell__r   r   r   r    r      s    
$r   Fr   r!   np.ndarray | torch.Tensorr#   r   r   r   r   r   r&   \int | float | np.ndarray | Sequence[int | float | np.ndarray | Sequence[int | float]] | Noner   r"   c              	   C  s6  |s
t | |d\} }t| tjtjdd } t|tjtjdd }|j| jkr3td| j d|j d| jdd \}}tj||ftj| j	d	}| j
d }	t|||	d
}
t||D ]3\}}t| ||f |||f ||
| ||d\}}}t|}|jdkrttjn| |||f< qZt|tj| j	tjdd S )a  
    This function is used to compute the Average Surface Distance from `y_pred` to `y`
    under the default setting.
    In addition, if sets ``symmetric = True``, the average symmetric surface distance between
    these two inputs will be returned.
    The implementation refers to `DeepMind's implementation <https://github.com/deepmind/surface-distance>`_.

    Args:
        y_pred: input data to compute, typical segmentation model output.
            It must be one-hot format and first dim is batch, example shape: [16, 3, 32, 32]. The values
            should be binarized.
        y: ground truth to compute mean the distance. It must be one-hot format and first dim is batch.
            The values should be binarized.
        include_background: whether to include distance computation on the first channel of
            the predicted output. Defaults to ``False``.
        symmetric: whether to calculate the symmetric average surface distance between
            `seg_pred` and `seg_gt`. Defaults to ``False``.
        distance_metric: : [``"euclidean"``, ``"chessboard"``, ``"taxicab"``]
            the metric used to compute surface distance. Defaults to ``"euclidean"``.
        spacing: spacing of pixel (or voxel). This parameter is relevant only if ``distance_metric`` is set to ``"euclidean"``.
            If a single number, isotropic spacing with that value is used for all images in the batch. If a sequence of numbers,
            the length of the sequence must be equal to the image dimensions. This spacing will be used for all images in the batch.
            If a sequence of sequences, the length of the outer sequence must be equal to the batch size.
            If inner sequence has length 1, isotropic spacing with that value is used for all images in the batch,
            else the inner sequence length must be equal to the image dimensions. If ``None``, spacing of unity is used
            for all images in batch. Defaults to ``None``.
    )r!   r#   )output_typedtyper   z*y_pred and y should have same shapes, got z and .N   )r?   device)r&   
batch_sizeimg_dim)r   r&   r   class_index)r   )r>   rB   r?   )r   r
   r0   r1   floatshaper(   emptyfloat32rB   ndimr   npndindexr   cattensornanmean)r!   r#   r   r   r   r&   rC   n_classZasdrD   spacing_listbc_	distancessurface_distancer   r   r    r)   {   s,   $



(r)   )FFr   N)r!   r<   r#   r<   r   r   r   r   r   r   r&   r=   r   r"   )
__future__r   collections.abcr   typingr   numpyrK   r0   monai.metrics.utilsr   r   r   r   monai.utilsr	   r
   metricr   r   r)   r   r   r   r    <module>   s   d