o
    }oi%                     @   sr  d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	m
Z d dlZd dlmZmZmZmZmZmZmZ deee ejf deee ejf defdd	Zdeee ejf deee ejf defd
dZdeee ejf deee ejf defddZdeee ejf deee ejf defddZ		d.deee ejf deee ejf dededeeeeeee ee f f f f
ddZ			d/deee ejf deee ejf dedededeeeeeee ee f f eeeef eeeeeee ee f f f fddZd0deee ejf dee ef de fddZ!	d1deee ejf deee ejf dee ef de fdd Z"	!d2deee ejf deee ejf dee ef de fd"d#Z#	$d3deee ejf deee ejf dee ef de fd%d&Z$	'		d4d(eee ejf d)eee ejf dee ef de d*ee  d+ee  fd,d-Z%dS )5    N)Path)ListOptionalTupleUnion)PrecisionRecallDisplayRocCurveDisplayaverage_precision_scorelog_lossprecision_recall_curveroc_auc_score	roc_curvey_truey_scorereturnc                 C   sr   t | } t |}t| t|ksJ t | dkr"t | dks$J t | dks2t | dkr4dS t| |S )zCompute Area Under the Receiver Operating Characteristic Curve (ROC AUC) from prediction scores.

    Note: If only one class is present in y_true, 0.5 is returned.
    r      g      ?)nparraylenallr   r   r    r   g/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/asr/parts/utils/confidence_metrics.pyauc_roc!   s   

 
r   c                 C   sd   t | } t |}t| t|ksJ t | dkr"t | dks$J t | dkr-dS t| |S )zCompute Area Under the Precision-Recall Curve (PR AUC) from prediction scores.

    Note: If only regatives are present in y_true, 0.0 is returned.
    r   r           r   r   r   r   r	   r   r   r   r   auc_pr/   s   

 
r   c                 C   sl   t | } t |}t| t|ksJ t | dkr"t | dks$J t | dkr-dS td|  d| S )a  Compute Area Under the Negative Predictive Value vs. True Negative Rate Curve (NT AUC) from prediction scores.

    This metric can be thought of as a PR AUC in which errors are treated as positives.

    Note: If only positives are present in y_true, 0.0 is returned.
    r   r   r   r   r   r   r   r   auc_nt=   s   

 r   c                 C   s   t | } t |}t| t|ksJ t | dkr"t | dks$J t | dks2t | dkr6tj S |  }d}t|| | td| | d|    }|t| | | S )a  Compute Normalized Cross Entropy (NCE) from prediction scores. Also known as the Normalized Mutual Information.

    NCE measures how close the correct prediction scores are to one and the incorrect prediction scores are to zero.
    Negative NCE values indicate that the classifier performs worse than the setting all prediction scores
    as the proportion of correct predictions.

    Note: If only one class is present in y_true, 0.5 is returned.
    r   r   gV瞯<)	r   r   r   r   mathinfmeanlogr
   )r   r   pepsHpr   r   r   nceM   s   
	
 .r%   d   Fn_binsreturn_curvec              	   C   s  t | } t |}t| t|ksJ t | dkr"t | dks$J t d| |gj}t |t |}}t |}g }g }	t|D ]s}
|
| |
d | }}|| d }|	| |jd |kt	}|t 
t||f }||k||k@  d }t|||
< ||
 dkr|| | | k ||
  ||
< ||  ||
  ||
< |||
 t ||
 ||
    qFt|t| }|r||	|ffS |S )a  Compute Expected Calibration Error (ECE) from prediction scores.

    ECE measures how close the correct prediction scores are to one and the incorrect prediction scores are to zero.
    ECE ranges from zero to one with the best value zero (the lower the value, the better).
    r   r      )r   r   r   r   Tzerosrangeappendastypeintarangenonzerosumabs)r   r   r'   r(   pyaccconfBm	ece_curve
thresholdsmab	thresholdpy_indexpy_value	bin_rangeecer   r   r   rA   b   s2   

 

 &rA   return_std_maximumc                 C   s  t | } t |}t dd|d }t| t|ksJ t | dkr+t | dks-J t | dks;t | dkr_|rL|rLddd|t t|ffS |rPdS |r]d|t t|ffS dS | dk}tt| d d}tt| | d}|| }	||  }
g }|D ]#}t|
|k  d | }t|	|k  d | }|t	||  qt |}|r|r|
 | | ||ffS |r|
 | | fS |r|
 ||ffS |
 S )aY  Compute Area Under the Youden's Curve (YC AUC) from prediction scores.

    YC AUC represents the rate of the effective threshold range.

    If return_std_maximum is set to True, std and maximum values of the Youden's Curve are returned with the AUC.

    Note: If only one class is present in y_true, zeroes are returned for every entity.
    r   r   r   )r   r   r   )r   r   linspacer   r   r+   maxr1   r-   r3   r    std)r   r   r'   rB   r(   r9   mask_correctcount_correctcount_incorrecty_score_correcty_score_incorrectycr=   tnrfnrr   r   r   auc_yc   s>   

 

rN   histplot_dirnamec                 C   sl   t j|dd tjt| ddd t| td td tj	t
|t
|d  d	d
 t  d S )NTexist_ok2   )r   r   )r,   zConfidence scoreCount.png,  dpi)osmakedirspltrO   r   r   titlexlabelylabelsavefigr   clf)r   rP   rQ   r   r   r   save_confidence_hist   s   


rb   rocc                 C      t | t |ks
J tj|dd tdt|  dt| \}}}t||d  t	| tj
t|t|d  dd t  d S )NTrR   r   )fprtprrV   rW   rX   )r   rZ   r[   r   r   r   r   plotr\   r]   r`   r   ra   )r   r   rP   rQ   re   rf   _r   r   r   save_roc_curve      $
ri   prc                 C   s   t | t |ks
J tj|dd tt| t|\}}}t||d  t	| tj
t|t|d  dd t  d S )NTrR   	precisionrecallrV   rW   rX   r   rZ   r[   r   r   r   r   rg   r\   r]   r`   r   ra   r   r   rP   rQ   rm   rn   rh   r   r   r   save_pr_curve   s   
rq   ntc                 C   rd   )NTrR   r   rl   rV   rW   rX   ro   rp   r   r   r   save_nt_curve   rj   rs   my_awesome_curver9   valuesr^   r_   c                 C   s   t | t |ks
J tj|dd t| | tddg tddg t| |d ur3t| |d ur<t	| tj
t|t|d  dd t  d S )NTrR   r   r   rV   rW   rX   )r   rZ   r[   r\   rg   xlimylimr]   r^   r_   r`   r   ra   )r9   ru   rP   rQ   r^   r_   r   r   r   save_custom_confidence_curve   s   


rx   )r&   F)r&   FF)rO   )rc   )rk   )rr   )rt   NN)&r   rZ   pathlibr   typingr   r   r   r   matplotlib.pyplotpyplotr\   numpyr   sklearn.metricsr   r   r	   r
   r   r   r   r/   ndarrayfloatr   r   r   r%   boolrA   rN   strrb   ri   rq   rs   rx   r   r   r   r   <module>   s   $2222"
*
.7






