o
    i                     @   sx   d dl mZ d dlmZ d dlZd dlZd dlZdd Zdd Zdd	 Z	d
d Z
dddZdd Zedkr:e  dS dS )    )print_function)
itemgetterNc                  C   s   t jdt jd} | jdtdddd | jdtd	d
dd | jdtdd
dd | jddd | jddd tjdtj	d  | 
 }t|}|S )Na  Compute the minimum detection cost function along with the threshold at which it occurs. Usage: sid/compute_min_dcf.py [options...] <scores-file> <trials-file> E.g., sid/compute_min_dcf.py --p-target 0.01 --c-miss 1 --c-fa 1 exp/scores/trials data/test/trials)descriptionformatter_classz
--p-targetp_target{Gz?z7The prior probability of the target speaker in a trial.)typedestdefaulthelpz--c-missc_miss   z9Cost of a missed detection.  This is usually not changed.z--c-fac_faz;Cost of a spurious detection.  This is usually not changed.scores_filenamezAInput scores file, with columns of the form <utt1> <utt2> <score>)r   trials_filenamezLInput trials file, with columns of the form <utt1> <utt2> <target/nontarget> 
)argparseArgumentParserArgumentDefaultsHelpFormatteradd_argumentfloatsysstderrwritejoinargv
parse_args	CheckArgs)parserargs r!   R/home/ubuntu/.local/lib/python3.10/site-packages/funasr/metrics/compute_min_dcf.pyGetArgs   sJ   	r#   c                 C   sD   | j dkr	td| jdkrtd| jdks| jdkr td| S )Nr   z--c-fa must be greater than 0z--c-miss must be greater than 0r   z1--p-target must be greater than 0 and less than 1)r   	Exceptionr   r   )r    r!   r!   r"   r   ?   s   

r   c           	         s   t tdd t| D tdd \}} fdd|D  g }g }tdt D ]3}|dkr>| |  |d |   q'|||d   |   |||d  d  |   q't t  fdd|D }fdd|D }|||fS )	Nc                 S   s   g | ]\}}||fqS r!   r!   ).0index	thresholdr!   r!   r"   
<listcomp>Q       z%ComputeErrorRates.<locals>.<listcomp>r   )keyc                    s   g | ]} | qS r!   r!   )r%   i)labelsr!   r"   r(   S   s    r   c                    s   g | ]}|t   qS r!   r   )r%   fn)	positivesr!   r"   r(   h   r)   c                    s   g | ]
}d |t    qS )r   r-   )r%   tn)	negativesr!   r"   r(   m   s    )zipsorted	enumerater   rangelenappendsum)	scoresr,   sorted_indexes
thresholdsfnstnsr+   fnrsfprsr!   )r,   r1   r/   r"   ComputeErrorRatesK   s"    
r@   c                 C   s   t d}|d }tdt| D ]}|| |  | |||  d|   }	|	|k r-|	}|| }qt|| |d|  }
||
 }||fS )Ninfr   r   )r   r5   r6   min)r>   r?   r;   r   r   r   	min_c_detmin_c_det_thresholdr+   c_detc_defmin_dcfr!   r!   r"   ComputeMinDcfs   s   $rH   r   r   c                 C   s  t | d }t |d }|}|}|}g }g }i }	|D ]}
|
  \}}}|d | }||	|< q|D ];}
|
  \}}}|d | }||	v rb|t| |	| dkr\|d q4|d q4td| d | d |  t||\}}}t||||||\}}||fS )Nrr   targetr   r   zMissing entry for z and )	open	readlinesrstripsplitr7   r   r$   r@   rH   )r   r   r   r   r   scores_filetrials_filer9   r,   trialslineutt1utt2rJ   trialscorer>   r?   r;   mindcfr'   r!   r!   r"   compute_min_dcf   s0   
rX   c               	   C   sH   t  } t| j| j| j| j| j\}}tj	d
||| j| j| j d S )NzLminDCF is {0:.4f} at threshold {1:.4f} (p-target={2}, c-miss={3}, c-fa={4})
)r#   rX   r   r   r   r   r   r   stdoutr   format)r    rW   r'   r!   r!   r"   main   s   r[   __main__)r   r   r   )
__future__r   operatorr   r   r   osr#   r   r@   rH   rX   r[   __name__r!   r!   r!   r"   <module>   s   -(
!
