o
    .wi	                     @   sr   d dl Z d dl mZ d dlmZ d dlmZ d dlmZ es!dgZdedefd	d
Z	dedede
defddZdS )    N)Tensor)multiclass_confusion_matrix)check_cluster_labels)"_TORCH_LINEAR_ASSIGNMENT_AVAILABLEcluster_accuracyconfmatreturnc                 C   sX   ddl m} | d } ||  |  }| d } | t| jd | f }| |   S )z9Computes the clustering accuracy from a confusion matrix.r   )batch_linear_assignmentN)torch_linear_assignmentr	   maxtorcharangeshapeflattensum)r   r	   
assignmenttps r   p/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/clustering/cluster_accuracy.py_cluster_accuracy_compute   s   r   predstargetnum_classesc                 C   s,   t stdt| | t| ||d}t|S )a  Computes the clustering accuracy between the predicted and target clusters.

    Args:
        preds: predicted cluster labels
        target: ground truth cluster labels
        num_classes: number of classes

    Returns:
        Scalar tensor with clustering accuracy between 0.0 and 1.0

    Raises:
        RuntimeError:
            If `torch_linear_assignment` is not installed

    Example:
        >>> from torchmetrics.functional.clustering import cluster_accuracy
        >>> preds = torch.tensor([0, 0, 1, 1])
        >>> target = torch.tensor([1, 1, 0, 0])
        >>> cluster_accuracy(preds, target, 2)
        tensor(1.000)

    zaMissing `torch_linear_assignment`. Please install it with `pip install torchmetrics[clustering]`.)r   )r   RuntimeErrorr   r   r   )r   r   r   r   r   r   r   r   &   s   
)r   r   &torchmetrics.functional.classificationr   (torchmetrics.functional.clustering.utilsr   torchmetrics.utilities.importsr   __doctest_skip__r   intr   r   r   r   r   <module>   s   