o
    i                     @   sL   d dl Zd dlmZ d dlZdddZdd Zdd	 Zed
kr$e  dS dS )    N)	roc_curve   c                 C   sx   t | ||d\}}}d| }|tt||  }|tt||  }|tt||  }	||	 d }
|
|fS )a  
    Python compute equal error rate (eer)
    ONLY tested on binary classification

    :param label: ground-truth label, should be a 1-d list or np.array, each element represents the ground-truth label of one sample
    :param pred: model prediction, should be a 1-d list or np.array, each element represents the model prediction of one sample
    :param positive_label: the class that is viewed as positive class when computing EER
    :return: equal error rate (EER)
    )	pos_labelr      )r   np	nanargminabsolute)labelpredpositive_labelfprtpr	thresholdfnreer_thresholdeer_1eer_2eer r   N/home/ubuntu/.local/lib/python3.10/site-packages/funasr/metrics/compute_eer.py_compute_eer   s   r   c                 C   s   g }t | dD ]}|| ddd dk qtj|td}g }t |dD ]}|t| ddd  q'tj|td}t||\}}||fS )Nr r   target)dtype)	openappendstriprsplitr   arrayintfloatr   )trials_pathscores_pathlabelsone_linescoresr   r   r   r   r   compute_eer!   s     r(   c                  C   sV   t  } | jddd | jddd |  }t|j|j\}}td|d | d S )Ntrialsz
trial list)helpr'   z score file, normalized to [0, 1]z!EER is {:.4f} at threshold {:.4f}g      Y@)	argparseArgumentParseradd_argument
parse_argsr(   r)   r'   printformat)parserargsr   r   r   r   r   main0   s   r3   __main__)r   )	numpyr   sklearn.metricsr   r+   r   r(   r3   __name__r   r   r   r   <module>   s    


