§
    ’©ºjM  ã                   óœ   — d dl mZ d dlmZmZmZmZ d dlZd dlmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZmZ esd	gZ G d
„ de¦  «        ZdS )é    )ÚSequence)ÚAnyÚLiteralÚOptionalÚUnionN)ÚTensor)Ú!_hausdorff_distance_validate_argsÚhausdorff_distance)ÚMetric)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEzHausdorffDistance.plotc                   óJ  ‡ — e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	e
ed<   eed	<   eed
<   	 	 	 	 	 d dededed         deeeee
         f                  deded         deddfˆ fd„Zdededdfd„Zdefd„Z	 d!deeeee         f                  dee         defd„Zˆ xZS )"ÚHausdorffDistancea  Compute the `Hausdorff Distance`_ between two subsets of a metric space for semantic segmentation.

    .. math::
        d_{\Pi}(X,Y) = \max{/sup_{x\in X} {d(x,Y)}, /sup_{y\in Y} {d(X,y)}}

    where :math:`\X, \Y` are two subsets of a metric space with distance metric :math:`d`. The Hausdorff distance is
    the maximum distance from a point in one set to the closest point in the other set. The Hausdorff distance is a
    measure of the degree of mismatch between two sets.

    As input to ``forward`` and ``update`` the metric accepts the following input:

    - ``preds`` (:class:`~torch.Tensor`): An one-hot boolean tensor of shape ``(N, C, ...)`` with ``N`` being
        the number of samples and ``C`` the number of classes. Alternatively, an integer tensor of shape ``(N, ...)``
        can be provided, where the integer values correspond to the class index. The input type can be controlled
        with the ``input_format`` argument.
    - ``target`` (:class:`~torch.Tensor`): An one-hot boolean tensor of shape ``(N, C, ...)`` with ``N`` being
        the number of samples and ``C`` the number of classes. Alternatively, an integer tensor of shape ``(N, ...)``
        can be provided, where the integer values correspond to the class index. The input type can be controlled
        with the ``input_format`` argument.

    As output of ``forward`` and ``compute`` the metric returns the following output:

    - ``hausdorff_distance`` (:class:`~torch.Tensor`): A scalar float tensor with the Hausdorff distance averaged over
        classes and samples

    Args:
        num_classes: number of classes
        include_background: whether to include background class in calculation
        distance_metric: distance metric to calculate surface distance. Choose one of `"euclidean"`,
          `"chessboard"` or `"taxicab"`
        spacing: spacing between pixels along each spatial dimension. If not provided the spacing is assumed to be 1
        directed: whether to calculate directed or undirected Hausdorff distance
        input_format: What kind of input the function receives.
            Choose between ``"one-hot"`` for one-hot encoded tensors, ``"index"`` for index tensors
            or ``"mixed"`` for one one-hot encoded and one index tensor
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> from torch import randint
        >>> from torchmetrics.segmentation import HausdorffDistance
        >>> preds = randint(0, 2, (4, 5, 16, 16))  # 4 samples, 5 classes, 16x16 prediction
        >>> target = randint(0, 2, (4, 5, 16, 16))  # 4 samples, 5 classes, 16x16 target
        >>> hausdorff_distance = HausdorffDistance(distance_metric="euclidean", num_classes=5)
        >>> hausdorff_distance(preds, target)
        tensor(1.9567)

    TÚis_differentiableFÚhigher_is_betterÚfull_state_updateç        Úplot_lower_boundÚscoreÚtotalÚ	euclideanNúone-hotÚnum_classesÚinclude_backgroundÚdistance_metric)r   Ú
chessboardÚtaxicabÚspacingÚdirectedÚinput_format)r   ÚindexÚmixedÚkwargsÚreturnc                 ó^  •—  t          ¦   «         j        di |¤Ž t          ||||||¦  «         || _        || _        || _        || _        || _        || _        |  	                    dt          j        d¦  «        d¬¦  «         |  	                    dt          j        d¦  «        d¬¦  «         d S )Nr   r   Úsum)ÚdefaultÚdist_reduce_fxr   r   © )ÚsuperÚ__init__r	   r   r   r   r   r    r!   Ú	add_stateÚtorchÚtensor)	Úselfr   r   r   r   r    r!   r$   Ú	__class__s	           €ú|/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/segmentation/hausdorff_distance.pyr,   zHausdorffDistance.__init__X   s»   ø€ ð 	�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ý)ØÐ+¨_¸gÀxÐQ]ñ	
ô 	
ð 	
ð 'ˆÔØ"4ˆÔØ.ˆÔØˆŒØ ˆŒØ(ˆÔØ�Š�w­¬°SÑ(9Ô(9È%ˆÑPÔPÐPØ�Š�w­¬°Q©¬ÈˆÑNÔNÐNÐNÐNó    ÚpredsÚtargetc           
      óø   — t          ||| j        | j        | j        | j        | j        | j        ¬¦  «        }| xj        |                     ¦   «         z  c_        | xj	        | 
                    ¦   «         z  c_	        dS )z*Update state with predictions and targets.)r   r   r   r    r!   N)r
   r   r   r   r   r    r!   r   r'   r   Únumel)r0   r4   r5   r   s       r2   ÚupdatezHausdorffDistance.updateo   st   € å"ØØØÔØ#Ô6Ø Ô0Ø”LØ”]ØÔ*ð	
ñ 	
ô 	
ˆð 	ˆ
Œ
�e—i’i‘k”kÑ!ˆ
Œ
Øˆ
Œ
�e—k’k‘m”mÑ#ˆ
Œ
ˆ
ˆ
r3   c                 ó    — | j         | j        z  S )z-Compute final Hausdorff distance over states.)r   r   )r0   s    r2   ÚcomputezHausdorffDistance.compute~   s   € àŒz˜DœJÑ&Ð&r3   ÚvalÚaxc                 ó.   — |                       ||¦  «        S )aý  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> from torch import randint
            >>> from torchmetrics.segmentation import HausdorffDistance
            >>> preds = randint(0, 2, (4, 5, 16, 16))  # 4 samples, 5 classes, 16x16 prediction
            >>> target = randint(0, 2, (4, 5, 16, 16))  # 4 samples, 5 classes, 16x16 target
            >>> metric = HausdorffDistance(num_classes=5)
            >>> metric.update(preds, target)
            >>> fig_, ax_ = metric.plot()

        )Ú_plot)r0   r;   r<   s      r2   ÚplotzHausdorffDistance.plot‚   s   € ð: �zŠz˜#˜rÑ"Ô"Ð"r3   )Fr   NFr   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   Úfloatr   Úintr   r   r   Úlistr   r,   r8   r:   r   r   r   r?   Ú__classcell__)r1   s   @r2   r   r      sÊ  ø€ € € € € € ð.ð .ð` #Ð�tÐ"Ð"Ñ"Ø"Ð�dÐ"Ð"Ñ"Ø#Ð�tÐ#Ð#Ñ#Ø!Ð�eÐ!Ð!Ñ!à€M€M�MØ€M€M�Mð
 $)ØITØ8<ØØ=FðOð OàðOð !ðOð !Ð!EÔFð	Oð
 ˜% ¨¨U¬Ð 3Ô4Ô5ðOð ðOð Ð9Ô:ðOð ðOð 
ðOð Oð Oð Oð Oð Oð.$˜Fð $¨Fð $°tð $ð $ð $ð $ð'˜ð 'ð 'ð 'ð 'ð
 _cð#ð #Ø˜E &¨(°6Ô*:Ð":Ô;Ô<ð#ØIQÐRZÔI[ð#à	ð#ð #ð #ð #ð #ð #ð #ð #r3   r   )Úcollections.abcr   Útypingr   r   r   r   r.   r   Ú7torchmetrics.functional.segmentation.hausdorff_distancer	   r
   Útorchmetrics.metricr   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r*   r3   r2   ú<module>rQ      s  ðð %Ð $Ð $Ð $Ð $Ð $Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0à €€€Ø Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð 'Ð &Ð &Ð &Ð &Ð &Ø @Ð @Ð @Ð @Ð @Ð @Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @àð 2Ø0Ð1Ðð@#ð @#ð @#ð @#ð @#˜ñ @#ô @#ð @#ð @#ð @#r3   