o
    piT                     @   s  d dl mZmZ d dlmZmZ ddlmZmZ ddl	m
Z
mZ ddlmZ dZdZd	Zd
ZG dd deeZdZdZdZdZdZG dd deZdZdZdZG dd deZdZdZdZG dd deZ dZ!dZ"dZ#dZ$G dd deeZ%d Z&d!Z'd"Z(d	Z)d
Z*G d#d$ d$eeZ+d%S )&    )OptionalTuple)
AnnotationTimeline   )
BaseMetric	f_measure)DetailsMetricComponents)UEMSupportMixinzdetection error ratetotalzfalse alarmmissc                	       s   e Zd ZdZedefddZedefddZdd	e	d
e
f fddZ	ddededee defddZdede	fddZ  ZS )DetectionErrorRatea  Detection error rate

    This metric can be used to evaluate binary classification tasks such as
    speech activity detection, for instance. Inputs are expected to only
    contain segments corresponding to the positive class (e.g. speech regions).
    Gaps in the inputs considered as the negative class (e.g. non-speech
    regions).

    It is computed as (fa + miss) / total, where fa is the duration of false
    alarm (e.g. non-speech classified as speech), miss is the duration of
    missed detection (e.g. speech classified as non-speech), and total is the
    total duration of the positive class in the reference.

    Parameters
    ----------
    collar : float, optional
        Duration (in seconds) of collars removed from evaluation around
        boundaries of reference segments (one half before, one half after).
    skip_overlap : bool, optional
        Set to True to not evaluate overlap regions.
        Defaults to False (i.e. keep overlap regions).
    returnc                 C      t S N)DER_NAMEcls r   N/home/ubuntu/.local/lib/python3.10/site-packages/pyannote/metrics/detection.pymetric_nameC      zDetectionErrorRate.metric_namec                 C   
   t ttgS r   )	DER_TOTALDER_FALSE_ALARMDER_MISSr   r   r   r   metric_componentsG      
z$DetectionErrorRate.metric_components        Fcollarskip_overlapc                    s&   t t| jdi | || _|| _d S Nr   )superr   __init__r    r!   )selfr    r!   kwargs	__class__r   r   r$   K   s   
zDetectionErrorRate.__init__N	reference
hypothesisuemc                 K   s   | j |||| j| jdd\}}}|jdd }|jdd }|j|d}|j|d}d}||D ]\}}	|||	@ j7 }q3d}
||D ]\}}|
||@ j7 }
qFi }|
|t< ||t	< | |t
< |S NT)r+   r    r!   returns_uemF)copy)supportr   )uemifyr    r!   get_timeliner/   gapsco_iterdurationr   r   r   )r%   r)   r*   r+   r&   
reference_hypothesis_false_positiver_hfalse_negativerh_detailr   r   r   compute_componentsP   s(   z%DetectionErrorRate.compute_componentsr=   c                 C   s@   d|t  |t   }d|t  }|dkr|dkrdS dS || S N      ?r   r   )r   r   r   )r%   r=   errorr   r   r   r   compute_metricp   s   z!DetectionErrorRate.compute_metric)r   Fr   )__name__
__module____qualname____doc__classmethodstrr   r
   r   floatboolr$   r   r   r   r	   r>   rB   __classcell__r   r   r'   r   r   +   s$    
 r   zdetection accuracyztrue positiveztrue negativezfalse positivezfalse negativec                	   @   b   e Zd ZdZedd ZedefddZ	dded	ed
e	e
 defddZdedefddZdS )DetectionAccuracya  Detection accuracy

    This metric can be used to evaluate binary classification tasks such as
    speech activity detection, for instance. Inputs are expected to only
    contain segments corresponding to the positive class (e.g. speech regions).
    Gaps in the inputs considered as the negative class (e.g. non-speech
    regions).

    It is computed as (tp + tn) / total, where tp is the duration of true
    positive (e.g. speech classified as speech), tn is the duration of true
    negative (e.g. non-speech classified as non-speech), and total is the total
    duration of the input signal.

    Parameters
    ----------
    collar : float, optional
        Duration (in seconds) of collars removed from evaluation around
        boundaries of reference segments (one half before, one half after).
    skip_overlap : bool, optional
        Set to True to not evaluate overlap regions.
        Defaults to False (i.e. keep overlap regions).
    c                 C   r   r   )ACCURACY_NAMEr   r   r   r   r      r   zDetectionAccuracy.metric_namer   c                 C      t tttgS r   )ACCURACY_TRUE_POSITIVEACCURACY_TRUE_NEGATIVEACCURACY_FALSE_POSITIVEACCURACY_FALSE_NEGATIVEr   r   r   r   r      s   z#DetectionAccuracy.metric_componentsNr)   r*   r+   c                 K   s  | j |||| j| jdd\}}}|jdd }|jdd }|j|d}|j|d}d}||D ]\}}	|||	@ j7 }q3d}
||D ]\}}|
||@ j7 }
qFd}||D ]\}}	|||	@ j7 }qYd}||D ]\}}|||@ j7 }qli }|
|t< ||t	< ||t
< ||t< |S r,   )r0   r    r!   r1   r/   r2   r3   r4   rQ   rP   rS   rR   )r%   r)   r*   r+   r&   r5   r6   true_positiver;   r9   true_negativer8   r<   r7   r:   r=   r   r   r   r>      s6   z$DetectionAccuracy.compute_componentsr=   c                 C   sL   d|t  |t   }d|t  |t  |t  |t   }|dkr"dS || S Nr@   r   )rQ   rP   rS   rR   )r%   r=   	numeratordenominatorr   r   r   rB      s   z DetectionAccuracy.compute_metricr   rC   rD   rE   rF   rG   r   r
   r   r   r   r   r	   r>   rI   rB   r   r   r   r   rM      s"    

)rM   zdetection precision	retrievedzrelevant retrievedc                	   @   rL   )DetectionPrecisiona  Detection precision

    This metric can be used to evaluate binary classification tasks such as
    speech activity detection, for instance. Inputs are expected to only
    contain segments corresponding to the positive class (e.g. speech regions).
    Gaps in the inputs considered as the negative class (e.g. non-speech
    regions).

    It is computed as tp / (tp + fp), where tp is the duration of true positive
    (e.g. speech classified as speech), and fp is the duration of false
    positive (e.g. non-speech classified as speech).

    Parameters
    ----------
    collar : float, optional
        Duration (in seconds) of collars removed from evaluation around
        boundaries of reference segments (one half before, one half after).
    skip_overlap : bool, optional
        Set to True to not evaluate overlap regions.
        Defaults to False (i.e. keep overlap regions).
    c                 C   r   r   )PRECISION_NAMEr   r   r   r   r      r   zDetectionPrecision.metric_namer   c                 C      t tgS r   )PRECISION_RETRIEVEDPRECISION_RELEVANT_RETRIEVEDr   r   r   r   r         z$DetectionPrecision.metric_componentsNr)   r*   r+   c                 K   s   | j |||| j| jdd\}}}|jdd }|jdd }|j|d}d}||D ]\}}|||@ j7 }q-d}	||D ]\}
}|	|
|@ j7 }	q@i }||	 |t< ||t	< |S r,   )
r0   r    r!   r1   r/   r2   r3   r4   r^   r_   )r%   r)   r*   r+   r&   r5   rT   r;   r9   r7   r8   r=   r   r   r   r>      $   z%DetectionPrecision.compute_componentsr=   c                 C   s,   d|t   }d|t  }|dkrdS || S rV   )r_   r^   )r%   r=   relevant_retrievedrZ   r   r   r   rB     s
   z!DetectionPrecision.compute_metricr   rY   r   r   r   r   r[      "    

r[   zdetection recallrelevantc                	   @   rL   )DetectionRecalla~  Detection recall

    This metric can be used to evaluate binary classification tasks such as
    speech activity detection, for instance. Inputs are expected to only
    contain segments corresponding to the positive class (e.g. speech regions).
    Gaps in the inputs considered as the negative class (e.g. non-speech
    regions).

    It is computed as tp / (tp + fn), where tp is the duration of true positive
    (e.g. speech classified as speech), and fn is the duration of false
    negative (e.g. speech classified as non-speech).

    Parameters
    ----------
    collar : float, optional
        Duration (in seconds) of collars removed from evaluation around
        boundaries of reference segments (one half before, one half after).
    skip_overlap : bool, optional
        Set to True to not evaluate overlap regions.
        Defaults to False (i.e. keep overlap regions).
    c                 C   r   r   )RECALL_NAMEr   r   r   r   r   B  r   zDetectionRecall.metric_namer   c                 C   r]   r   )RECALL_RELEVANTRECALL_RELEVANT_RETRIEVEDr   r   r   r   r   F  r`   z!DetectionRecall.metric_componentsNr)   r*   r+   c                 K   s   | j |||| j| jdd\}}}|jdd }|jdd }|j|d}d}||D ]\}}|||@ j7 }q-d}	||D ]\}}
|	||
@ j7 }	q@i }||	 |t< ||t	< |S r,   )
r0   r    r!   r1   r/   r2   r3   r4   rg   rh   )r%   r)   r*   r+   r&   r6   rT   r;   r9   r:   r<   r=   r   r   r   r>   J  ra   z"DetectionRecall.compute_componentsr=   c                 C   s8   d|t   }d|t  }|dkr|dkrdS dS || S r?   )rh   rg   )r%   r=   rb   rd   r   r   r   rB   h  s   zDetectionRecall.compute_metricr   rY   r   r   r   r   re   +  rc   re   zF[precision|recall]c                	       s   e Zd ZdZedd Zedd Z		dd	ed
edef fddZ		dde
de
dee defddZdedefddZddee deeeef fddZ  ZS ) DetectionPrecisionRecallFMeasurea  Compute detection precision and recall, and return their F-score

    Parameters
    ----------
    collar : float, optional
        Duration (in seconds) of collars removed from evaluation around
        boundaries of reference segments (one half before, one half after).
    skip_overlap : bool, optional
        Set to True to not evaluate overlap regions.
        Defaults to False (i.e. keep overlap regions).
    beta : float, optional
        When beta > 1, greater importance is given to recall.
        When beta < 1, greater importance is given to precision.
        Defaults to 1.

    See also
    --------
    pyannote.metrics.detection.DetectionPrecision
    pyannote.metrics.detection.DetectionRecall
    pyannote.metrics.base.f_measure

    c                 C   r   r   )DFS_NAMEr   r   r   r   r     r   z,DetectionPrecisionRecallFMeasure.metric_namec                 C   r   r   )DFS_PRECISION_RETRIEVEDDFS_RECALL_RELEVANTDFS_RELEVANT_RETRIEVEDr   r   r   r   r     r   z2DetectionPrecisionRecallFMeasure.metric_componentsr   Fr@   r    r!   betac                    s,   t t| jdi | || _|| _|| _d S r"   )r#   ri   r$   r    r!   rn   )r%   r    r!   rn   r&   r'   r   r   r$     s   
z)DetectionPrecisionRecallFMeasure.__init__Nr)   r*   r+   r   c                 K   s   | j |||| j| jdd\}}}|jdd }|jdd }|j|d}|j|d}d}||D ]\}}	|||	@ j7 }q3d}
||D ]\}}	|
||	@ j7 }
qFd}||D ]\}}|||@ j7 }qYt||
 t	|| t
|i}|S r,   )r0   r    r!   r1   r/   r2   r3   r4   rk   rl   rm   )r%   r)   r*   r+   r&   r5   r6   rT   r;   r9   r7   r8   r:   r<   r=   r   r   r   r>     s.   z3DetectionPrecisionRecallFMeasure.compute_componentsr=   c                 C   s   | j |d\}}}|S )N)r=   )compute_metrics)r%   r=   _valuer   r   r   rB     s   z/DetectionPrecisionRecallFMeasure.compute_metricc                 C   sz   |d u r| j n|}|t }|t }|t }|dkrd}n|| }|dkr.|dkr+d}nd}n|| }||t||| jdfS )Nr   r   r   r@   )rn   )accumulated_rk   rl   rm   r   rn   )r%   r=   precision_retrievedrecall_relevantrb   	precisionrecallr   r   r   ro     s   z0DetectionPrecisionRecallFMeasure.compute_metrics)r   Fr@   r   )rC   rD   rE   rF   rG   r   r   rI   rJ   r$   r   r   r   r	   r>   rB   r   ro   rK   r   r   r'   r   ri   z  s2    



&ri   zdetection cost functionzpositive class totalznegative class totalc                	       sx   e Zd ZdZ		d fdd	Zedd	 Zed
efddZ	dde	de	de
e d
efddZded
efddZ  ZS )DetectionCostFunctiona  Detection cost function.

    This metric can be used to evaluate binary classification tasks such as
    speech activity detection. Inputs are expected to only contain segments
    corresponding to the positive class (e.g. speech regions). Gaps in the
    inputs considered as the negative class (e.g. non-speech regions).

    Detection cost function (DCF), as defined by NIST for OpenSAT 2019, is
    0.25*far + 0.75*missr, where far is the false alarm rate
    (i.e., the proportion of non-speech incorrectly classified as speech)
    and missr is the miss rate (i.e., the proportion of speech incorrectly
    classified as non-speech.

    Parameters
    ----------
    collar : float, optional
        Duration (in seconds) of collars removed from evaluation around
        boundaries of reference segments (one half before, one half after).
        Defaults to 0.0.

    skip_overlap : bool, optional
        Set to True to not evaluate overlap regions.
        Defaults to False (i.e. keep overlap regions).

    fa_weight : float, optional
        Weight for false alarm rate.
        Defaults to 0.25.

    miss_weight : float, optional
        Weight for miss rate.
        Defaults to 0.75.

    kwargs
        Keyword arguments passed to :class:`pyannote.metrics.base.BaseMetric`.

    References
    ----------
    "OpenSAT19 Evaluation Plan v2." https://www.nist.gov/system/files/documents/2018/11/05/opensat19_evaluation_plan_v2_11-5-18.pdf
    r   F      ?      ?c                    s2   t t| jdi | || _|| _|| _|| _d S r"   )r#   rw   r$   r    r!   	fa_weightmiss_weight)r%   r    r!   rz   r{   r&   r'   r   r   r$     s
   
zDetectionCostFunction.__init__c                 C   r   r   )DCF_NAMEr   r   r   r   r     r   z!DetectionCostFunction.metric_namer   c                 C   rO   r   )DCF_POS_TOTALDCF_NEG_TOTALDCF_FALSE_ALARMDCF_MISSr   r   r   r   r   !  s   z'DetectionCostFunction.metric_componentsNr)   r*   r+   c                 K   s   | j |||| j| jdd\}}}|jdd }|jdd }|j|d}|j|d}| }| }d}	||D ]\}
}|	|
|@ j7 }	q;d}||D ]\}}|||@ j7 }qNt|t	|t
|	t|i}|S r,   )r0   r    r!   r1   r/   r2   r4   r3   r}   r~   r   r   )r%   r)   r*   r+   r&   r5   r6   pos_durneg_durmiss_durr;   r<   fa_durr8   r9   
componentsr   r   r   r>   %  s.   z(DetectionCostFunction.compute_componentsr   c           	      C   sP   dd }|t  }|t }|||}|t }|t }|||}| j| | j|  S )Nc                 S   s    |dkr| dkr
dS dS | | S )Nr   r@   r   )numdenomr   r   r   _compute_rateO  s
   z;DetectionCostFunction.compute_metric.<locals>._compute_rate)r~   r   r}   r   rz   r{   )	r%   r   r   r   r   fa_rater   r   	miss_rater   r   r   rB   N  s   

z$DetectionCostFunction.compute_metric)r   Frx   ry   r   )rC   rD   rE   rF   r$   rG   r   r
   r   r   r   r   r	   r>   rI   rB   rK   r   r   r'   r   rw     s(    (

)rw   N),typingr   r   pyannote.corer   r   baser   r   typesr	   r
   utilsr   r   r   r   r   r   rN   rP   rQ   rR   rS   rM   r\   r^   r_   r[   rf   rg   rh   re   rj   rk   rl   rm   ri   r|   r}   r~   r   r   rw   r   r   r   r   <module>   sF   QXFIk