Ë
    ÿÍ:jA2  ã                   ó®   — d dl Z d dlmZmZmZmZmZm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mZ ddlmZmZ d	Zd
ZdZdZdZdZ G d„ de«      Zy)é    N)ÚUnionÚIterableÚOptionalÚTupleÚListÚDict)Ú	ArrayLike)ÚSegmentÚ
AnnotationÚSlidingWindowFeatureÚTimelineé   )Ú
BaseMetric)Ú	det_curve)ÚMetricComponentsÚDetailsÚtargetÚspeaker_latencyÚ	spk_scoreÚabsolute_latencyÚ	abs_scoreÚscorec            	       óB  ‡ — e Zd ZdZedefd„«       Zdeeef   fd„Z		 	 dde
e   de
e   fˆ fd„Zdefd	„Zd
edee   dee   defd„Zd
eeef   dee   dee   defd„Zd
eeef   deeeeeef      f   defd„Zed„ «       Zed„ «       Z	 	 	 	 ddedededefd„Zˆ xZS )ÚLowLatencySpeakerSpottinga  Evaluation of low-latency speaker spotting (LLSS) systems

    LLSS systems can be evaluated in two ways: with fixed or variable latency.

    * When latency is fixed a priori (default), only scores reported by the
    system within the requested latency range are considered. Varying the
    detection threshold has no impact on the actual latency of the system. It
    only impacts the detection performance.

    * In variable latency mode, the whole stream of scores is considered.
    Varying the detection threshold will impact both the detection performance
    and the detection latency. Each trial will result in the alarm being
    triggered with a different latency. In case the alarm is not triggered at
    all (missed detection), the latency is arbitrarily set to the value one
    would obtain if it were triggered at the end of the last target speech
    turn. The reported latency is the average latency over all target trials.

    Parameters
    ----------
    latencies : float iterable, optional
        Switch to fixed latency mode, using provided `latencies`.
        Defaults to [1, 5, 10, 30, 60] (in seconds).
    thresholds : float iterable, optional
        Switch to variable latency mode, using provided detection `thresholds`.
        Defaults to fixed latency mode.
    Úreturnc                  ó   — y)NzLow-latency speaker spotting© )Úclss    ún/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/pyannote/metrics/spotting.pyÚmetric_namez%LowLatencySpeakerSpotting.metric_nameL   s   € à-ó    c                 ó
   — ddiS )Nr   g        r   )Úselfs    r   Úmetric_componentsz+LowLatencySpeakerSpotting.metric_componentsP   s   € Ø˜"ˆ~Ðr!   Ú
thresholdsÚ	latenciesc                 óÄ   •— t         ‰| �  «        |€|€g d¢}|�|�t        d«      ‚|�t        j                  |«      | _        |�t        j                  |«      }|| _        y )N)r   é   é
   é   é<   z3One must choose between fixed and variable latency.)ÚsuperÚ__init__Ú
ValueErrorÚnpÚsortr%   r&   )r#   r%   r&   Ú	__class__s      €r   r-   z"LowLatencySpeakerSpotting.__init__S   sp   ø€ ô 	‰ÑÔàÐ )Ð"3Ú*ˆIàÐ! iÐ&;ÜØEóGð Gð Ð!Ü Ÿg™g jÓ1ˆDŒOàÐ ÜŸ™ 	Ó*ˆIà"ˆ�r!   Údetailc                  ó   — y )Nr   )r#   r2   s     r   Úcompute_metricz(LowLatencySpeakerSpotting.compute_metricg   s   € Ør!   Ú	referenceÚ
timestampsÚscoresc           
      ó’  — |sFd}t        j                  |«      t        j                  t        | j                  «      df«      z  }|}�n¢d}t        j
                  |D �cg c]  }|j                  ‘Œ c}«      }g }g }t        t        j                  || j                  «      | j                  «      D ]ð  \  }	}
	 ||	   j                  ||	   |
z
  z
  }t        j                  ||«      }|dk  rt        j                  j                   }nt        j                  |d | «      }|j                  |«       |d   j                  |
z   }t        j                  ||«      }|dk  rt        j                  j                   }nt        j                  |d | «      }|j                  |«       Œò t        j                  |«      j!                  d«      }t        j                  |«      j!                  d«      }t"        |t$        | j                  t&        |t(        | j                  t*        |iS c c}w # t        $ r t        j                  |«      }Y �Œ'w xY w)NFr   Tr   ©éÿÿÿÿr   )r/   ÚmaxÚonesÚlenr&   ÚcumsumÚdurationÚzipÚsearchsortedÚendÚsysÚ
float_infoÚ
IndexErrorÚappendÚstartÚarrayÚreshapeÚSPOTTING_TARGETÚSPOTTING_SPK_LATENCYÚSPOTTING_SCOREÚSPOTTING_ABS_LATENCYÚSPOTTING_ABS_SCORE)r#   r5   r6   r7   Útarget_trialr   r   ÚsegmentÚtotalÚiÚlatencyÚtÚup_toÚss                 r   Ú_fixed_latencyz(LowLatencySpeakerSpotting._fixed_latencyj   sö  € ñ Ø ˆLÜŸ™˜v›¬¯©´#°d·n±nÓ2EÀqÐ1IÓ)JÑJˆIØ!ŠIð  ˆLô —I‘I¸yÖI°G˜w×/Ó/ÒIÓJˆEð ˆIØˆIô "¤"§/¡/°%¸¿¹Ó"HØ"&§.¡.ó2ò $‘
��7ð
'Ø! !™×(Ñ(¨E°!©H°wÑ,>Ñ?�AÜŸO™O¨J¸Ó:�EØ˜q’yÜ Ÿ^™^×/Ñ/Ð/™äŸF™F 6¨&¨5 >Ó2˜ð × Ñ  Ô#ð ˜a‘L×&Ñ&¨Ñ0�äŸ™¨
°AÓ6�Ø˜1’9ÜŸ™×+Ñ+Ð+‘AäŸ™˜v f u˜~Ó.�AØ× Ñ  Õ#ð3$ô6 Ÿ™ Ó+×3Ñ3°GÓ<ˆIÜŸ™ Ó+×3Ñ3°GÓ<ˆIô ˜\Ü  $§.¡.Ü˜IÜ  $§.¡.Ü 	ð
ð 	
ùòM Jøô( "ò 'ÜŸ™˜v›“Að'ús   ÁHÂ7A'H$È$IÉIc                 ó�  — t         j                  t        j                  t        |«      df«      z  }t         j                  t        j                  t        |«      df«      z  }t	        |t
        «      r|j                  d¬«      }|ri|d   j                  }t        |«      D ]L  \  }}	t        ||	«      }
|j                  |
«      j                  «       ||<   t        d|
j                  «      ||<   ŒN t         j                  j                  |«      j                  d«      }|| j                   kD  }t        j"                  t        | j                   «      D ��cg c]"  \  }}t        j$                  |d d …|f   d«      ‘Œ$ c}}«      }|rnd}t        j&                  ||d¬«      }t        j&                  ||d¬«      }|d	d d …f   }|j)                  «       j                  || <   |j                  «       || <   n"d}t         j                  }t         j                  }t*        |t,        |t.        |t0        t        j                  |«      iS c c}}w )
Nr   F)Úcopyr   r9   TÚclip©Úmoder:   )r/   Únanr<   r=   Ú
isinstancer   Úget_timelinerG   Ú	enumerater
   Úcropr?   r;   ÚmaximumÚ
accumulaterI   r%   rH   rA   ÚtakeÚextentrJ   rM   rK   rL   )r#   r5   r6   r7   Úkwargsr   r   Ú
first_timerR   rT   Úso_farÚmaxcumÚ	triggeredÚ_ÚindicesrO   Úpositives                    r   Ú_variable_latencyz+LowLatencySpeakerSpotting._variable_latency¤   s   € ô
 Ÿ&™&¤2§7¡7¬C°
«O¸QÐ+?Ó#@Ñ@ˆÜŸ6™6¤B§G¡G¬S°«_¸aÐ,@Ó$AÑAÐÜ�i¤Ô,Ø!×.Ñ.°EÐ.Ó:ˆIÙØ" 1™×+Ñ+ˆJÜ! *Ó-ò >‘��1Ü  ¨QÓ/�Ø%.§^¡^°FÓ%;×%DÑ%DÓ%F� Ñ"Ü&)¨!¨V¯_©_Ó&=Ð  Ò#ð>ô —*‘*×'Ñ'¨Ó/×8Ñ8¸ÓAˆØ˜TŸ_™_Ñ,ˆ	Ü—(‘(Ü(1°$·/±/Ó(B÷DÙ $  1ô ŸO™O¨I²a¸°d©O¸TÕBó Dó Eˆñ àˆLä!Ÿw™wÐ'7¸ÀvÔNÐÜ Ÿg™g o°wÀVÔLˆOð ! ¢Q Ñ'ˆHð +4×*:Ñ*:Ó*<×*EÑ*EÐ˜h˜YÑ'ð *3×);Ñ);Ó)=ˆO˜X˜IÒ&ð !ˆLô  "Ÿv™vÐÜ Ÿf™fˆOô ˜\Ü Ð"2Ü  /ÜœBŸF™F 6›Nð	
ð 	
ùó9Ds   Å'I
Ú
hypothesisc                 óè   — t        |t        «      r |D ��cg c]  \  }}|j                  |f‘Œ }}}t        |Ž \  }}| j                  €| j                  |||«      S | j                  |||«      S c c}}w )zŸ

        Parameters
        ----------
        reference : Timeline or Annotation
        hypothesis : SlidingWindowFeature or (time, score) iterable
        )r^   r   rB   r@   r&   rn   rW   )r#   r5   ro   rf   ÚwindowÚvaluer6   r7   s           r   Úcompute_componentsz,LowLatencySpeakerSpotting.compute_componentsÛ   sz   € ô �jÔ"6Ô7ØCM×N±-°&¸%˜6Ÿ:™: uÒ-ÐNˆJÑNÜ  *Ð-Ñˆ
�Fà�>‰>Ð!Ø×)Ñ)¨)°ZÀÓHÐHð ×&Ñ& y°*¸fÓEÐEùó Os   –A.c                 ó„   — | D ��cg c]  \  }}|t            r	|t           ‘Œ }}}t        j                  |d¬«      S c c}}w ©Nr   )Úaxis)rJ   rM   r/   Únanmean©r#   rk   Útrialr&   s       r   r   z*LowLatencySpeakerSpotting.absolute_latencyñ   óF   € àAE÷ 0±X°Q¸ØœoÒ.ð Ô/Ó0ð 0ˆ	ñ 0ä�z‰z˜)¨!Ô,Ð,ùó0ó   †<c                 ó„   — | D ��cg c]  \  }}|t            r	|t           ‘Œ }}}t        j                  |d¬«      S c c}}w ru   )rJ   rK   r/   rw   rx   s       r   r   z)LowLatencySpeakerSpotting.speaker_latency÷   rz   r{   Ú	cost_missÚcost_faÚprior_targetÚreturn_latencyc                 ó$  — | j                   �€Et        j                  | D ��cg c]  \  }}|t           ‘Œ c}}«      }t        j                  | D ��cg c]  \  }}|t           ‘Œ c}}«      }t        ||d¬«      \  }	}
}}|	ddd…   |
ddd…   |ddd…   }}
}	||
z  |z  ||	z  d|z
  z  z   }|rŸt        j                  || j                  d¬«      }t        j                  ||d¬	«      }t        j                  |	|d¬	«      }	t        j                  |
|d¬	«      }
t        j                  ||d¬	«      }||	|
||| j                  | j                  fS ||	|
||fS t        j                  | D ��cg c]  \  }}|t           ‘Œ c}}«      }t        j                  | D ��cg c]  \  }}|t           ‘Œ c}}«      }t        j                  | D ��cg c]  \  }}|t           ‘Œ c}}«      }i }||d
œj                  «       D ]z  \  }}i ||<   t        | j                   «      D ]X  \  }}t        ||dd…|f   d¬«      \  }	}
}}|	ddd…   |
ddd…   |ddd…   }}
}	||
z  |z  ||	z  d|z
  z  z   }||	|
||f||   |<   ŒZ Œ| |S c c}}w c c}}w c c}}w c c}}w c c}}w )a¸  DET curve

        Parameters
        ----------
        cost_miss : float, optional
            Cost of missed detections. Defaults to 100.
        cost_fa : float, optional
            Cost of false alarms. Defaults to 1.
        prior_target : float, optional
            Target trial prior. Defaults to 0.5.
        return_latency : bool, optional
            Set to True to return latency.
            Has no effect when latencies are given at initialization time.

        Returns
        -------
        thresholds : numpy array
            Detection thresholds
        fpr : numpy array
            False alarm rate
        fnr : numpy array
            False rejection rate
        eer : float
            Equal error rate
        cdet : numpy array
            Cdet cost function
        speaker_latency : numpy array
        absolute_latency : numpy array
            Speaker and absolute latency when return_latency is set to True.
        NF)Ú	distancesr:   g      ð?Úleft)ÚsiderZ   r[   )ÚspeakerÚabsolute)r&   r/   rH   rJ   rL   r   rA   r%   rd   r   r   ÚSPOTTING_SPK_SCORErN   Úitemsr`   )r#   r}   r~   r   r€   rk   ry   Úy_truer7   ÚfprÚfnrr%   ÚeerÚcdetrl   Ú
spk_scoresÚ
abs_scoresÚresultÚkeyrR   rS   Úthetas                         r   r   z#LowLatencySpeakerSpotting.det_curveþ   s²  € ðH �>‰>Ñ!ä—X‘XÀd×K¹(¸!¸U˜u¤_Ó5ÓKÓLˆFÜ—X‘XÀT×J¹¸¸E˜u¤^Ó4ÓJÓKˆFÜ(1°&¸&ÈEÔ(RÑ%ˆC��j #Ø#&¡t¨ t¡9¨c±$°B°$©i¸ÁDÀbÀDÑ9I�j�ˆCØ˜s‘? \Ñ1Ø˜S‘= B¨Ñ$5Ñ6ñ7ˆDñ ô Ÿ/™/¨*°d·o±oÈFÔS�äŸW™W Z°¸vÔF�
Ü—g‘g˜c 7°Ô8�Ü—g‘g˜c 7°Ô8�Ü—w‘w˜t W°6Ô:�Ø! 3¨¨S°$Ø×+Ñ+¨T×-BÑ-BðCð Cð " 3¨¨S°$Ð6Ð6ô —X‘XÀd×K¹(¸!¸U˜u¤_Ó5ÓKÓLˆFÜŸ™ÈT×"RÁÀÀE 5Ô);Ó#<Ó"RÓSˆJÜŸ™ÈT×"RÁÀÀE 5Ô);Ó#<Ó"RÓSˆJàˆFØ+5Ø,6ñ 8ß8=¹»òF‘��Vð !��s‘ä"+¨D¯N©NÓ";ò F‘J�A�wÜ+4°V¸VÂAÀqÀD¹\Ø?Dô,FÑ(�C˜˜e Sà&)©$¨B¨$¡i°±T°r°T±¸EÁ$ÀBÀ$¹K˜e˜�CØ$ s™?¨\Ñ9Ø" S™=¨B°Ñ,=Ñ>ñ?�Dà+0°#°s¸CÀÐ+E�F˜3‘K Ò(ñFðFð ˆMùóU LùÛJùó. LùÛ"RùÛ"Rs   ¢I4
ÁI:
Å'J 
ÆJ
ÇJ
)NN)éd   r   g{®Gáz„?F) Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚclassmethodÚstrr    r   Úfloatr$   r   r	   r-   r   r4   r   r   r   rW   r   r   rn   r   r   r   rs   Úpropertyr   r   Úboolr   Ú__classcell__)r1   s   @r   r   r   0   s€  ø„ ñð6 ð.˜Cò .ó ð.ð 4¨¨U¨
Ñ#3ó ð 48Ø26ñ#Ø% iÑ0ð#à$ YÑ/õ#ð(Ð%5ó ð8
¨ð 8
Ø#'¨¡;ð8
Ø8<¸U¹ð8
ØHOó8
ðt5
¨5°¸:Ð1EÑ+Fð 5
Ø&*¨5¡kð5
Ø;?À¹;ð5
à'.ó5
ðnF¨E°(¸JÐ2FÑ,Gð FØ',Ð-AØ-5°e¸EÀ5¸LÑ6IÑ-Jð.Kñ (LðFð )0óFð, ñ-ó ð-ð
 ñ-ó ð-ð &)Ø#$Ø(,Ø).ñ	PØ"ðPà ðPð !&ðPð #'÷	Pr!   r   )rC   Útypingr   r   r   r   r   r   Únumpyr/   Únumpy.typingr	   Úpyannote.corer
   r   r   r   Úbaser   Úbinary_classificationr   Útypesr   r   rJ   rK   r‡   rM   rN   rL   r   r   r!   r   ú<module>r¥      sU   ðó: ß ?× ?ã Ý "ß MÓ Må Ý ,ß ,à€Ø(Ð Ø Ð Ø)Ð Ø Ð Ø€ô^ 
õ ^r!   