o
    .wi                     @   s   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
 d dl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 deZdS )    )Sequence)AnyListOptionalUnion)Tensor)
dunn_index)Metric)dim_zero_cat)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPEDunnIndex.plotc                       s   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 ed	< ee ed
< dde
deddf fddZd	ed
eddfddZdefddZddeeee df dee defddZ  ZS )	DunnIndexa  Compute `Dunn Index`_.

    .. math::
        DI_m = \frac{\min_{1\leq i<j\leq m} \delta(C_i,C_j)}{\max_{1\leq k\leq m} \Delta_k}

    Where :math:`C_i` is a cluster of tensors, :math:`C_j` is a cluster of tensors,
    and :math:`\delta(C_i,C_j)` is the intercluster distance metric for :math:`m` clusters.

    This clustering metric is an intrinsic measure, because it does not rely on ground truth labels for the evaluation.
    Instead it examines how well the clusters are separated from each other. The score is higher when clusters are dense
    and well separated, which relates to a standard concept of a cluster.

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

    - ``data`` (:class:`~torch.Tensor`): float tensor with shape ``(N,d)`` with the embedded data. ``d`` is the
      dimensionality of the embedding space.
    - ``labels`` (:class:`~torch.Tensor`): single integer tensor with shape ``(N,)`` with cluster labels

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

    - ``dunn_index`` (:class:`~torch.Tensor`): A tensor with the Dunn Index

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

    Example::
        >>> import torch
        >>> from torchmetrics.clustering import DunnIndex
        >>> data = torch.tensor([[0, 0], [0.5, 0], [1, 0], [0.5, 1]])
        >>> labels = torch.tensor([0, 0, 0, 1])
        >>> dunn_index = DunnIndex(p=2)
        >>> dunn_index(data, labels)
        tensor(2.)

    Tis_differentiablehigher_is_betterFfull_state_updateg        plot_lower_bounddatalabels   pkwargsreturnNc                    s<   t  jdi | || _| jdg dd | jdg dd d S )Nr   cat)defaultdist_reduce_fxr    )super__init__r   	add_state)selfr   r   	__class__r   _/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/clustering/dunn_index.pyr   I   s   zDunnIndex.__init__c                 C   s   | j | | j| dS )z*Update state with predictions and targets.N)r   appendr   )r!   r   r   r   r   r$   updateP   s   zDunnIndex.updatec                 C   s   t t| jt| j| jS )z&Compute mutual information over state.)r   r
   r   r   r   )r!   r   r   r$   computeU   s   zDunnIndex.computevalaxc                 C   s   |  ||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 DunnIndex
            >>> data = torch.tensor([[0, 0], [0.5, 0], [1, 0], [0.5, 1]])
            >>> labels = torch.tensor([0, 0, 0, 1])
            >>> metric = DunnIndex(p=2)
            >>> metric.update(data, labels)
            >>> fig_, ax_ = metric.plot(metric.compute())

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.clustering import DunnIndex
            >>> metric = DunnIndex(p=2)
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(torch.randn(50, 3), torch.randint(0, 2, (50,))))
            >>> fig_, ax_ = metric.plot(values)

        )_plot)r!   r(   r)   r   r   r$   plotY   s   (r   )r   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   floatr   r   r   r   r&   r'   r   r   r   r   r   r+   __classcell__r   r   r"   r$   r      s   
 $2r   N)collections.abcr   typingr   r   r   r   torchr   -torchmetrics.functional.clustering.dunn_indexr   torchmetrics.metricr	   torchmetrics.utilities.datar
   torchmetrics.utilities.importsr   torchmetrics.utilities.plotr   r   __doctest_skip__r   r   r   r   r$   <module>   s   