
      ip1                         d dl mZmZ d dlZd dlmZmZ d dlm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZ erd d	lmZ d
ZdZdZdZdZdZ G d de      Zy)    )OptionalTYPE_CHECKINGN)
AnnotationTimeline)linear_sum_assignment   )UEMSupportMixin)LabelMatcher)MATCH_CORRECTMATCH_CONFUSIONMATCH_MISSED_DETECTIONMATCH_FALSE_ALARM	DataArray	reference
hypothesis
regressionimprovementboth_correctboth_incorrectc                        e Zd ZdZddedef fdZ	 	 ddededee	   defd	Z
d
 Z	 	 ddedededee	   def
dZ	 ddededee	   ddfdZ xZS )IdentificationErrorAnalysisaA  

    Parameters
    ----------
    collar : float, optional
        Duration (in seconds) of collars removed from evaluation around
        boundaries of reference segments.
    skip_overlap : bool, optional
        Set to True to not evaluate overlap regions.
        Defaults to False (i.e. keep overlap regions).
    collarskip_overlapc                 \    t         |           t               | _        || _        || _        y N)super__init__r
   matcherr   r   )selfr   r   	__class__s      {/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pyannote/metrics/errors/identification.pyr   z$IdentificationErrorAnalysis.__init__A   s&    #~(    r   r   uemuemifiedc                    | j                  |||| j                  | j                  d      \  }}}t        |j                  |j
                        }|D ]  }	|j                  |	d      }
|j                  |	d      }| j                  |
|      \  }}|t           D ]*  \  }}|j                  |	t              }t        ||f||	|f<   , |t           D ]*  \  }}|j                  |	t              }t        ||f||	|f<   , |t           D ]'  }|j                  |	t              }t        |df||	|f<   ) |t           D ]'  }|j                  |	t              }t        d|f||	|f<   )  |r|||fS |S )a=  Get error analysis as `Annotation`

        Labels are (status, reference_label, hypothesis_label) tuples.
        `status` is either 'correct', 'confusion', 'missed detection' or
        'false alarm'.
        `reference_label` is None in case of 'false alarm'.
        `hypothesis_label` is None in case of 'missed detection'.

        Parameters
        ----------
        uemified : bool, optional
            Returns "uemified" version of reference and hypothesis.
            Defaults to False.

        Returns
        -------
        errors : `Annotation`

        T)r$   r   r   returns_timelineurimodalityFunique)prefixN)uemifyr   r   r   r)   r*   
get_labelsr   r   	new_trackr   r   r   )r    r   r   r$   r%   RHcommon_timelineerrorssegmentrlabelshlabels_detailsrhtracks                    r"   
differencez&IdentificationErrorAnalysis.differenceH   s   2 !%zs;;T->->! !, !#1o
 		8J8JK 'G ll75l9G ll75l9Ggw7JAw.1(((G*7A)>w~& /  01(((I*91a)@w~& 1 34((0F ) H*@!T)Jw~& 5
 ./((9J(K*;T1)Ew~& 0/ '6 j&00Mr#   c                 B    |\  }}}|\  }}}||k(  d||k(  z   ||k(  z   z  S )N    )	r    beforeafterb_typeb_refb_hypa_typea_refa_hyps	            r"   _match_errorsz)IdentificationErrorAnalysis._match_errors   s<    %u$u1&(8#9Ue^#LMMr#   rA   rB   c                    | j                  |||d      \  }}}| j                  |||d      \  }}}t        |j                  |j                        }	|j	                         j                  |j	                               }
|
j                         }
| j                  ||
      }| j                  ||
      }|
D ]  }|j                  |d      }|j                  |d      }t        |      }t        |      }t        ||      }t        j                  ||ft              }t        |      D ]/  \  }}t        |      D ]  \  }}| j                  ||      |||f<    1 t!        t#        |        D ]]  \  }}||k\  r.|	j%                  |t&        t&              }t&        d ||   f|	||f<   :||k\  r.|	j%                  |t(        t(              }t(        ||   d f|	||f<   m||   d   t*        k(  rq||   d   t*        k(  r1|	j%                  |t,        t,              }t,        ||   ||   f|	||f<   |	j%                  |t&        t&              }t&        ||   ||   f|	||f<   ||   d   t*        k(  r2|	j%                  |t(        t(              }t(        ||   ||   f|	||f<   .|	j%                  |t.        t.              }t.        ||   ||   f|	||f<   `  |	j1                         }	|r||||	fS |	S )	NTr$   r%   r(   Fr+   )dtype)	candidater-   r   )r=   r   r)   r*   get_timelineunionsegmentation_taggerr/   lenmaxnpzerosint	enumeraterI   zipr   r0   
REGRESSIONIMPROVEMENTr   BOTH_CORRECTBOTH_INCORRECTsupport)r    r   rA   rB   r$   r%   r8   errors_beforeerrors_after	behaviorsr3   BAr5   
old_errors
new_errorsn1n2nmatchi1e1i2e2r<   s                            r"   r   z&IdentificationErrorAnalysis.regression   sd    $(??v3 $3 $7 6= *.u# *9 *6&	5, 9==9;M;MN	 '335;;&&(*)668 LL8 LL7&Gge<Jge<JZBZBBAHHaV3/E#J/B'
3FB$($6$6r2$>E"b&M 4 0 4eV<=B8%//:D7A 0 CE #D*R.1:Igun- 2X%//:E7B 0 DE $Z^T1;Igun-  ^A&-7!"~a(M9 ) 3 3G>J;G !4 !I )*R.*R.5J	'5.1 !* 3 3G>H;E !4 !G '
2
25H	'5.1
 ""~a(M9 ) 3 3G>I;F !4 !H (BB5I	'5.1 !* 3 3G>L;I !4 !K +JrNJrN5L	'5.1[ > '| %%'	feY66r#   returnr   c                    | j                  |||d      \  }}}|j                         }t        |j                               t        |j                               z
  }t        |j                               t        |j                               z  }t        |j                               }|j                  }	t        ||	      }t        ||	      }t        ||	      }||D 
cg c]
  }
t        |
f c}
z   }t        t        t        t        t        t        g|z   }	 ddlm}  |t        j                   t#        |      t#        |      f      d|fd|fg	      }|D ]{  \  \  }}}
}|t        k(  r6|j$                  ||
fxx   |z  cc<   |j$                  |t        fxx   |z  cc<   |t        k(  r|j$                  ||
fxx   |z  cc<   |j$                  |t        fxx   |z  cc<   |
|v rC|j$                  t        |
f|fxx   |z  cc<   |j$                  t        |
ft        fxx   |z  cc<   n6|j$                  |
|fxx   |z  cc<   |j$                  |
t        fxx   |z  cc<   |t        k(  rE|
|v r$|j$                  t        |
ft        fxx   |z  cc<   n|j$                  |
t        fxx   |z  cc<   |t        k(  s_|j$                  |t        fxx   |z  cc<   ~ |D ]  }t'        |t(              r#|d   t        k(  rd
}|j+                  |d         }n"|j+                  |      }|j+                  |      }||j$                  |t        f<   ||j$                  |t        f<    |S c c}
w # t        $ r d}t        |      w xY w)NTrK   )cmpr   r   zLPlease install xarray dependency to use class 'IdentificationErrorAnalysis'.r   r   )coords        r?   )r=   chartsetlabels_cmp_labelssortedr   REFERENCE_TOTALHYPOTHESIS_TOTALr   r   r   xarrayr   ImportErrorrT   rU   rR   loc
isinstancetuplelabel_duration)r    r   r   r$   r4   rr   falseAlarmLabelshLabelsrLabelscmp_funchLabelr   msgmatrixstatusrLabeldurationr:   r;   s                      r"   r   z"IdentificationErrorAnalysis.matrix   s   
 )-zsT )8 );%	:v  z0023c):J:J:L6MMi&&()C
0A0A0C,DDi&&() ((!"2Ah/h/ +;=+; 08+;= =
 &'7#_')? 		#( HHc'lCL12 '*\7,CDF
 38.$VVVh &

66>*h6*

6=01X=1 (

66>*h6*

6?23x?3--JJ 16:FBCxOCJJ 16:OKLPXXLJJvv~.(:.JJv678C7**--JJ 16:<MMNRZZNJJv'889XE9//

6#99:hF:5 38: F&%(VAY:K-K--fQi8,,V4--f523FJJv./34FJJv//0  G=  	#1  c""	#s   !MM$ $M;)rq   F)NFr   )__name__
__module____qualname____doc__floatboolr   r   r   r   r=   rI   r   r   __classcell__)r!   s   @r"   r   r   4   s    
)u ) ) .2$)	?(?)? !*? "	?BN .2$)](]%] %] !*	]
 "]D *.^$^%^ X&^ 3>^r#   r   )typingr   r   numpyrT   pyannote.corer   r   scipy.optimizer   identificationr	   r   r
   r   r   r   r   ry   r   rw   rx   rY   rZ   r[   r\   r   r@   r#   r"   <module>r      sX   : +  . 0 , ". .   
!X/ Xr#   