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)fowlkes_mallows_index)Metric)dim_zero_cat)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPEFowlkesMallowsIndex.plotc                       s   e Zd ZU dZdZeed< dZe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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 )FowlkesMallowsIndexa  Compute `Fowlkes-Mallows Index`_.

    .. math::
        FMI(U,V) = \frac{TP}{\sqrt{(TP + FP) * (TP + FN)}}

    Where :math:`TP` is the number of true positives, :math:`FP` is the number of false positives, and :math:`FN` is
    the number of false negatives.

    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:

    - ``fmi`` (:class:`~torch.Tensor`): A tensor with the Fowlkes-Mallows index.

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

    Example::
        >>> import torch
        >>> from torchmetrics.clustering import FowlkesMallowsIndex
        >>> preds = torch.tensor([2, 2, 0, 1, 0])
        >>> target = torch.tensor([2, 2, 1, 1, 0])
        >>> fmi = FowlkesMallowsIndex()
        >>> fmi(preds, target)
        tensor(0.5000)

    Tis_differentiablehigher_is_betterFfull_state_updateg        plot_lower_boundg      ?plot_upper_boundpredstargetkwargsreturnNc                    s6   t  jdi | | jdg dd | jdg dd d S )Nr   cat)defaultdist_reduce_fxr    )super__init__	add_state)selfr   	__class__r   j/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/clustering/fowlkes_mallows_index.pyr   E   s   zFowlkesMallowsIndex.__init__c                 C   s   | j | | j| dS )z*Update state with predictions and targets.N)r   appendr   )r    r   r   r   r   r#   updateK   s   zFowlkesMallowsIndex.updatec                 C   s   t t| jt| jS )z)Compute Fowlkes-Mallows index over state.)r   r
   r   r   )r    r   r   r#   computeP   s   zFowlkesMallowsIndex.computevalaxc                 C   s   |  ||S )aX  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 FowlkesMallowsIndex
            >>> metric = FowlkesMallowsIndex()
            >>> 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 FowlkesMallowsIndex
            >>> metric = FowlkesMallowsIndex()
            >>> 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(   r   r   r#   plotT   s   &r   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   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.clusteringr   torchmetrics.metricr	   torchmetrics.utilities.datar
   torchmetrics.utilities.importsr   torchmetrics.utilities.plotr   r   __doctest_skip__r   r   r   r   r#   <module>   s   