§
    ’©ºj¨2  ã                   óÞ   — d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z
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 esg d	¢Z G d
„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        ZdS )é    )ÚSequence)ÚAnyÚListÚOptionalÚUnion)ÚTensor)Úcompleteness_scoreÚhomogeneity_scoreÚv_measure_score)ÚMetric)Údim_zero_cat)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPE)zHomogeneityScore.plotzCompletenessScore.plotzVMeasureScore.plotc                   ó   ‡ — 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<   d	Ze
ed
<   ee         ed<   ee         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         df         dee         defd„Zˆ xZS )ÚHomogeneityScorea¥  Compute `Homogeneity Score`_.

    The homogeneity score is a metric to measure the homogeneity of a clustering. A clustering result satisfies
    homogeneity if all of its clusters contain only data points which are members of a single class. The metric is not
    symmetric, therefore swapping ``preds`` and ``target`` yields a different score.

    This clustering metric is an extrinsic measure, because it requires ground truth clustering labels, which may not
    be available in practice since clustering in generally is used for unsupervised learning.

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

    - ``preds`` (:class:`~torch.Tensor`): single integer tensor with shape ``(N,)`` with predicted cluster labels
    - ``target`` (:class:`~torch.Tensor`): single integer tensor with shape ``(N,)`` with ground truth cluster labels

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

    - ``rand_score`` (:class:`~torch.Tensor`): A tensor with the Rand Score

    Args:
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> import torch
        >>> from torchmetrics.clustering import HomogeneityScore
        >>> preds = torch.tensor([2, 1, 0, 1, 0])
        >>> target = torch.tensor([0, 2, 1, 1, 0])
        >>> metric = HomogeneityScore()
        >>> metric(preds, target)
        tensor(0.4744)

    TÚis_differentiableÚhigher_is_betterFÚfull_state_updateç        Úplot_lower_boundç      ð?Úplot_upper_boundÚpredsÚtargetÚkwargsÚreturnNc                 óš   •—  t          ¦   «         j        di |¤Ž |                      dg d¬¦  «         |                      dg d¬¦  «         d S ©Nr   Úcat©ÚdefaultÚdist_reduce_fxr   © ©ÚsuperÚ__init__Ú	add_state©Úselfr   Ú	__class__s     €úŠ/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/clustering/homogeneity_completeness_v_measure.pyr'   zHomogeneityScore.__init__J   óV   ø€ Ø�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"à�Š�w¨¸5ˆÑAÔAÐAØ�Š�x¨¸EˆÑBÔBÐBÐBÐBó    c                 ón   — | j                              |¦  «         | j                             |¦  «         dS ©z*Update state with predictions and targets.N©r   Úappendr   ©r*   r   r   s      r,   ÚupdatezHomogeneityScore.updateP   ó4   € àŒ
×Ò˜%Ñ Ô Ð ØŒ×Ò˜6Ñ"Ô"Ð"Ð"Ð"r.   c                 ój   — t          t          | j        ¦  «        t          | j        ¦  «        ¦  «        S ©úCompute rand score over state.)r
   r   r   r   ©r*   s    r,   ÚcomputezHomogeneityScore.computeU   s&   € å ¥¨d¬jÑ!9Ô!9½<ÈÌÑ;TÔ;TÑUÔUÐUr.   ÚvalÚaxc                 ó.   — |                       ||¦  «        S )aL  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

            >>> # Example plotting a single value
            >>> import torch
            >>> from torchmetrics.clustering import HomogeneityScore
            >>> metric = HomogeneityScore()
            >>> metric.update(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,)))
            >>> fig_, ax_ = metric.plot(metric.compute())

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.clustering import HomogeneityScore
            >>> metric = HomogeneityScore()
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,))))
            >>> fig_, ax_ = metric.plot(values)

        ©Ú_plot©r*   r;   r<   s      r,   ÚplotzHomogeneityScore.plotY   ó   € ðL �zŠz˜#˜rÑ"Ô"Ð"r.   ©NN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   Úfloatr   r   r   r   r'   r4   r:   r   r   r   r   r   rA   Ú__classcell__©r+   s   @r,   r   r   !   sf  ø€ € € € € € ðð ð@ #Ð�tÐ"Ð"Ñ"Ø!Ð�dÐ!Ð!Ñ!Ø#Ð�tÐ#Ð#Ñ#Ø!Ð�eÐ!Ð!Ñ!Ø!Ð�eÐ!Ð!Ñ!Ø�Œ<ÐÐÑØ�ŒLÐÐÑðC ð C¨ð Cð Cð Cð Cð Cð Cð#˜Fð #¨Fð #°tð #ð #ð #ð #ð
V˜ð Vð Vð Vð Vð&#ð &#˜˜f h¨vÔ&6¸Ð<Ô=ð &#È(ÐS[ÔJ\ð &#Ðhvð &#ð &#ð &#ð &#ð &#ð &#ð &#ð &#r.   r   c                   ó   ‡ — 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<   d	Ze
ed
<   ee         ed<   ee         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         df         dee         defd„Zˆ xZS )ÚCompletenessScorea]  Compute `Completeness Score`_.

    A clustering result satisfies completeness if all the data points that are members of a given class are elements of
    the same cluster. The metric is not symmetric, therefore swapping ``preds`` and ``target`` yields a different score.

    This clustering metric is an extrinsic measure, because it requires ground truth clustering labels, which may not
    be available in practice since clustering in generally is used for unsupervised learning.

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

    - ``preds`` (:class:`~torch.Tensor`): single integer tensor with shape ``(N,)`` with predicted cluster labels
    - ``target`` (:class:`~torch.Tensor`): single integer tensor with shape ``(N,)`` with ground truth cluster labels

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

    - ``rand_score`` (:class:`~torch.Tensor`): A tensor with the Rand Score

    Args:
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> import torch
        >>> from torchmetrics.clustering import CompletenessScore
        >>> preds = torch.tensor([2, 1, 0, 1, 0])
        >>> target = torch.tensor([0, 2, 1, 1, 0])
        >>> metric = CompletenessScore()
        >>> metric(preds, target)
        tensor(0.4744)

    Tr   r   Fr   r   r   r   r   r   r   r   r   Nc                 óš   •—  t          ¦   «         j        di |¤Ž |                      dg d¬¦  «         |                      dg d¬¦  «         d S r   r%   r)   s     €r,   r'   zCompletenessScore.__init__ª   r-   r.   c                 ón   — | j                              |¦  «         | j                             |¦  «         dS r0   r1   r3   s      r,   r4   zCompletenessScore.update°   r5   r.   c                 ój   — t          t          | j        ¦  «        t          | j        ¦  «        ¦  «        S r7   )r	   r   r   r   r9   s    r,   r:   zCompletenessScore.computeµ   s&   € å!¥,¨t¬zÑ":Ô":½LÈÌÑ<UÔ<UÑVÔVÐVr.   r;   r<   c                 ó.   — |                       ||¦  «        S )aP  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

            >>> # Example plotting a single value
            >>> import torch
            >>> from torchmetrics.clustering import CompletenessScore
            >>> metric = CompletenessScore()
            >>> metric.update(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,)))
            >>> fig_, ax_ = metric.plot(metric.compute())

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.clustering import CompletenessScore
            >>> metric = CompletenessScore()
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,))))
            >>> fig_, ax_ = metric.plot(values)

        r>   r@   s      r,   rA   zCompletenessScore.plot¹   rB   r.   rC   rD   rM   s   @r,   rO   rO   ‚   se  ø€ € € € € € ðð ð> #Ð�tÐ"Ð"Ñ"Ø!Ð�dÐ!Ð!Ñ!Ø#Ð�tÐ#Ð#Ñ#Ø!Ð�eÐ!Ð!Ñ!Ø!Ð�eÐ!Ð!Ñ!Ø�Œ<ÐÐÑØ�ŒLÐÐÑðC ð C¨ð Cð Cð Cð Cð Cð Cð#˜Fð #¨Fð #°tð #ð #ð #ð #ð
W˜ð Wð Wð Wð Wð&#ð &#˜˜f h¨vÔ&6¸Ð<Ô=ð &#È(ÐS[ÔJ\ð &#Ðhvð &#ð &#ð &#ð &#ð &#ð &#ð &#ð &#r.   rO   c                   ó  ‡ — 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<   d	Ze
ed
<   ee         ed<   ee         ed<   dde
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         df         dee         defd„Zˆ xZS )ÚVMeasureScorea†  Compute `V-Measure Score`_.

    The V-measure is the harmonic mean between homogeneity and completeness:

    ..math::
        v = \frac{(1 + \beta) * homogeneity * completeness}{\beta * homogeneity + completeness}

    where :math:`\beta` is a weight parameter that defines the weight of homogeneity in the harmonic mean, with the
    default value :math:`\beta=1`. The V-measure is symmetric, which means that swapping ``preds`` and ``target`` does
    not change the score.

    This clustering metric is an extrinsic measure, because it requires ground truth clustering labels, which may not
    be available in practice since clustering in generally is used for unsupervised learning.

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

    - ``preds`` (:class:`~torch.Tensor`): single integer tensor with shape ``(N,)`` with predicted cluster labels
    - ``target`` (:class:`~torch.Tensor`): single integer tensor with shape ``(N,)`` with ground truth cluster labels

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

    - ``rand_score`` (:class:`~torch.Tensor`): A tensor with the Rand Score

    Args:
        beta: Weight parameter that defines the weight of homogeneity in the harmonic mean
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example::
        >>> import torch
        >>> from torchmetrics.clustering import VMeasureScore
        >>> preds = torch.tensor([2, 1, 0, 1, 0])
        >>> target = torch.tensor([0, 2, 1, 1, 0])
        >>> metric = VMeasureScore(beta=2.0)
        >>> metric(preds, target)
        tensor(0.4744)

    Tr   r   Fr   r   r   r   r   r   r   Úbetar   r   Nc                 ó  •—  t          ¦   «         j        di |¤Ž t          |t          ¦  «        r|dk    st	          d|› d�¦  «        ‚|| _        |                      dg d¬¦  «         |                      dg d¬¦  «         d S )	Nr   z0Argument `beta` should be a positive float. Got ú.r   r    r!   r   r$   )r&   r'   Ú
isinstancerK   Ú
ValueErrorrV   r(   )r*   rV   r   r+   s      €r,   r'   zVMeasureScore.__init__  s“   ø€ Ø�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ý˜4¥Ñ'Ô'ð 	Y¨D°1ªH¨HÝÐWÐPTÐWÐWÐWÑXÔXÐXØˆŒ	à�Š�w¨¸5ˆÑAÔAÐAØ�Š�x¨¸EˆÑBÔBÐBÐBÐBr.   c                 ón   — | j                              |¦  «         | j                             |¦  «         dS r0   r1   r3   s      r,   r4   zVMeasureScore.update  r5   r.   c                 óx   — t          t          | j        ¦  «        t          | j        ¦  «        | j        ¬¦  «        S )r8   )rV   )r   r   r   r   rV   r9   s    r,   r:   zVMeasureScore.compute  s/   € å�|¨D¬JÑ7Ô7½ÀdÄkÑ9RÔ9RÐY]ÔYbÐcÑcÔcÐcr.   r;   r<   c                 ó.   — |                       ||¦  «        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

            >>> # Example plotting a single value
            >>> import torch
            >>> from torchmetrics.clustering import VMeasureScore
            >>> metric = VMeasureScore()
            >>> metric.update(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,)))
            >>> fig_, ax_ = metric.plot(metric.compute())

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.clustering import VMeasureScore
            >>> metric = VMeasureScore()
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,))))
            >>> fig_, ax_ = metric.plot(values)

        r>   r@   s      r,   rA   zVMeasureScore.plot#  rB   r.   )r   rC   rD   rM   s   @r,   rU   rU   â   st  ø€ € € € € € ð$ð $ðL #Ð�tÐ"Ð"Ñ"Ø!Ð�dÐ!Ð!Ñ!Ø#Ð�tÐ#Ð#Ñ#Ø!Ð�eÐ!Ð!Ñ!Ø!Ð�eÐ!Ð!Ñ!Ø�Œ<ÐÐÑØ�ŒLÐÐÑðCð C˜Uð C°Cð C¸Dð Cð Cð Cð Cð Cð Cð#˜Fð #¨Fð #°tð #ð #ð #ð #ð
d˜ð dð dð dð dð&#ð &#˜˜f h¨vÔ&6¸Ð<Ô=ð &#È(ÐS[ÔJ\ð &#Ðhvð &#ð &#ð &#ð &#ð &#ð &#ð &#ð &#r.   rU   N)Úcollections.abcr   Útypingr   r   r   r   Útorchr   ÚEtorchmetrics.functional.clustering.homogeneity_completeness_v_measurer	   r
   r   Útorchmetrics.metricr   Útorchmetrics.utilities.datar   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   rO   rU   r$   r.   r,   ú<module>rg      sŠ  ðð %Ð $Ð $Ð $Ð $Ð $Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -à Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð
 'Ð &Ð &Ð &Ð &Ð &Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø @Ð @Ð @Ð @Ð @Ð @Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @àð aØ`Ð`Ð`Ðð^#ð ^#ð ^#ð ^#ð ^#�vñ ^#ô ^#ð ^#ðB]#ð ]#ð ]#ð ]#ð ]#˜ñ ]#ô ]#ð ]#ð@g#ð g#ð g#ð g#ð g#�Fñ g#ô g#ð g#ð g#ð g#r.   