o
    -wi                     @  s~   d dl mZ d dlZd dlmZmZ d dlZd dlmZ d dlm	Z	 d dl
mZmZ er2d dlmZ 						ddddZdS )    )annotationsN)TYPE_CHECKINGSequence)util)
plot_table)test_missing
test_types)CustomChart	ROC CurveFy_trueSequence[numbers.Number]y_probas Sequence[Sequence[float]] | Nonelabelslist[str] | Noneclasses_to_plotlist[numbers.Number] | Nonetitlestrsplit_tableboolreturnr	   c              	   C  s  t jddd}t jddd}t dd}t dd}	|| } ||}t| |d	s,d
S t| |d	s4d
S || }
|d
u r?|
}i }i }|||
|d }|D ]5}|d
urlt|
| t	set|
d |j
rl||
|  }n|
| }|j| |d|f |
| d\||< ||< }qP||dd | D |t| |t| dd}t|tjjkrtdtjj d |	j|dtjjd|d dg d}ttj|dddddd|ddd |d!S )"a	  Constructs Receiver Operating Characteristic (ROC) curve chart.

    Args:
        y_true: The true class labels (ground truth)
            for the target variable. Shape should be (num_samples,).
        y_probas: The predicted probabilities or
            decision scores for each class. Shape should be (num_samples, num_classes).
        labels: Human-readable labels corresponding to the class
            indices in `y_true`. For example, if `labels=['dog', 'cat']`,
            class 0 will be displayed as 'dog' and class 1 as 'cat' in the plot.
            If None, the raw class indices from `y_true` will be used.
            Default is None.
        classes_to_plot: A subset of unique class labels
            to include in the ROC curve. If None, all classes in `y_true` will
            be plotted. Default is None.
        title: Title of the ROC curve plot. Default is "ROC Curve".
        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:
        wandb.Error: If numpy, pandas, or scikit-learn are not found.

    Example:
    ```python
    import numpy as np
    import wandb

    # Simulate a medical diagnosis classification problem with three diseases
    n_samples = 200
    n_classes = 3

    # True labels: assign "Diabetes", "Hypertension", or "Heart Disease" to
    # each sample
    disease_labels = ["Diabetes", "Hypertension", "Heart Disease"]
    # 0: Diabetes, 1: Hypertension, 2: Heart Disease
    y_true = np.random.choice([0, 1, 2], size=n_samples)

    # Predicted probabilities: simulate predictions, ensuring they sum to 1
    # for each sample
    y_probas = np.random.dirichlet(np.ones(n_classes), size=n_samples)

    # Specify classes to plot (plotting all three diseases)
    classes_to_plot = [0, 1, 2]

    # Initialize a W&B run and log a ROC curve plot for disease classification
    with wandb.init(project="medical_diagnosis") as run:
        roc_plot = wandb.plot.roc_curve(
            y_true=y_true,
            y_probas=y_probas,
            labels=disease_labels,
            classes_to_plot=classes_to_plot,
            title="ROC Curve for Disease Classification",
        )
        run.log({"roc-curve": roc_plot})
    ```
    numpyz@roc requires the numpy library, install with `pip install numpy`)requiredpandaszBroc requires the pandas library, install with `pip install pandas`zsklearn.metricszHroc requires the scikit library, install with `pip install scikit-learn`zsklearn.utils)r   r   Nr   .)	pos_labelc                 S  s   g | ]\}}|gt | qS  )len).0kvr   r   Q/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/plot/roc_curve.py
<listcomp>   s    zroc_curve.<locals>.<listcomp>)classfprtpr   zwandb uses only z! data points to create the plots.F*   r#   )replace	n_samplesrandom_statestratify)r$   r%   r#   )	dataframezwandb/area-under-curve/v0r$   r%   )xyr#   zFalse positive ratezTrue positive rate)r   zx-axis-titlezy-axis-title)
data_tablevega_spec_namefieldsstring_fieldsr   )r   
get_modulearrayr   r   uniquewhereisin
isinstanceintinteger	roc_curve	DataFramehstackitemslistvaluesroundr   wandbTableMAX_ROWStermwarnresamplesort_valuesr   )r   r   r   r   r   r   nppdsklearn_metricssklearn_utilsclassesr$   r%   indices_to_ploticlass_label_dfr   r   r!   r;      s   E




r;   )NNNr
   F)r   r   r   r   r   r   r   r   r   r   r   r   r   r	   )
__future__r   numberstypingr   r   rB   r   wandb.plot.custom_chartr   wandb.plot.utilsr   r   r	   r;   r   r   r   r!   <module>   s    