o
    -wi                     @  st   d dl mZ d dlmZmZmZ d dlZd dlmZ d dlm	Z	 er(d dlm
Z
 edZ							ddddZdS )    )annotations)TYPE_CHECKINGSequenceTypeVarN)util)
plot_table)CustomChartTConfusion Matrix CurveFprobs Sequence[Sequence[float]] | Noney_trueSequence[T] | Nonepredsclass_namesSequence[str] | Nonetitlestrsplit_tableboolreturnr   c                   s~  t jddd}| dur|durtd| dur |j| dd }t|t|kr,td durWt tt}tt|t krHtd	tt|t krVtd
nt|	t|}t|dd tD  dd t
tt|D }|ftt|D ]}	|||	  |||	  f  d7  < q fddtD }
ttjg d|
ddddddd|i|dS )a  Constructs a confusion matrix from a sequence of probabilities or predictions.

    Args:
        probs: A sequence of predicted probabilities for each
            class. The sequence shape should be (N, K) where N is the number of samples
            and K is the number of classes. If provided, `preds` should not be provided.
        y_true: A sequence of true labels.
        preds: A sequence of predicted class labels. If provided,
            `probs` should not be provided.
        class_names: Sequence of class names. If not
            provided, class names will be defined as "Class_1", "Class_2", etc.
        title: Title of the confusion matrix chart.
        split_table: Whether the table should be split into a separate section
            in the W&B UI. If `True`, the table will be displayed in a section named
            "Custom Chart Tables". Default is `False`.

    Returns:
        CustomChart: A custom chart object that can be logged to W&B. To log the
            chart, pass it to `wandb.log()`.

    Raises:
        ValueError: If both `probs` and `preds` are provided or if the number of
            predictions and true labels are not equal. If the number of unique
            predicted classes exceeds the number of class names or if the number of
            unique true labels exceeds the number of class names.
        wandb.Error: If numpy is not installed.

    Examples:
    Logging a confusion matrix with random probabilities for wildlife
    classification:

    ```python
    import numpy as np
    import wandb

    # Define class names for wildlife
    wildlife_class_names = ["Lion", "Tiger", "Elephant", "Zebra"]

    # Generate random true labels (0 to 3 for 10 samples)
    wildlife_y_true = np.random.randint(0, 4, size=10)

    # Generate random probabilities for each class (10 samples x 4 classes)
    wildlife_probs = np.random.rand(10, 4)
    wildlife_probs = np.exp(wildlife_probs) / np.sum(
        np.exp(wildlife_probs),
        axis=1,
        keepdims=True,
    )

    # Initialize W&B run and log confusion matrix
    with wandb.init(project="wildlife_classification") as run:
        confusion_matrix = wandb.plot.confusion_matrix(
            probs=wildlife_probs,
            y_true=wildlife_y_true,
            class_names=wildlife_class_names,
            title="Wildlife Classification Confusion Matrix",
        )
        run.log({"wildlife_confusion_matrix": confusion_matrix})
    ```

    In this example, random probabilities are used to generate a confusion
    matrix.

    Logging a confusion matrix with simulated model predictions and 85%
    accuracy:

    ```python
    import numpy as np
    import wandb

    # Define class names for wildlife
    wildlife_class_names = ["Lion", "Tiger", "Elephant", "Zebra"]

    # Simulate true labels for 200 animal images (imbalanced distribution)
    wildlife_y_true = np.random.choice(
        [0, 1, 2, 3],
        size=200,
        p=[0.2, 0.3, 0.25, 0.25],
    )

    # Simulate model predictions with 85% accuracy
    wildlife_preds = [
        y_t
        if np.random.rand() < 0.85
        else np.random.choice([x for x in range(4) if x != y_t])
        for y_t in wildlife_y_true
    ]

    # Initialize W&B run and log confusion matrix
    with wandb.init(project="wildlife_classification") as run:
        confusion_matrix = wandb.plot.confusion_matrix(
            preds=wildlife_preds,
            y_true=wildlife_y_true,
            class_names=wildlife_class_names,
            title="Simulated Wildlife Classification Confusion Matrix",
        )
        run.log({"wildlife_confusion_matrix": confusion_matrix})
    ```

    In this example, predictions are simulated with 85% accuracy to generate a
    confusion matrix.
    numpy)zVnumpy is required to use wandb.plot.confusion_matrix, install with `pip install numpy`)requiredNz<Only one of `probs` or `preds` should be provided, not both.   )axisz8The number of predictions and true labels must be equal.zIThe number of unique predicted classes exceeds the number of class names.zCThe number of unique true labels exceeds the number of class names.c                 S  s   g | ]	}d |d  qS )Class_r    ).0ir   r   X/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/plot/confusion_matrix.py
<listcomp>   s    z$confusion_matrix.<locals>.<listcomp>c                 S  s   i | ]\}}||qS r   r   )r   r   valr   r   r   
<dictcomp>   s    z$confusion_matrix.<locals>.<dictcomp>c                   s4   g | ]}t D ]} |  | ||f gqqS r   )range)r   r   jr   counts	n_classesr   r   r       s    )Actual	PredictednPredictions)columnsdatazwandb/confusion_matrix/v1r(   r)   r*   r   )
data_tablevega_spec_namefieldsstring_fieldsr   )r   
get_module
ValueErrorargmaxtolistlenlistr#   setunion	enumeratesortedzerosr   wandbTable)r   r   r   r   r   r   np	class_idxclass_mappingr   r,   r   r%   r   confusion_matrix   sZ   n&rA   )NNNNr
   F)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )
__future__r   typingr   r   r   r<   r   wandb.plot.custom_chartr   r   r	   rA   r   r   r   r   <module>   s    