Ë
    ÿÍ:j:0  ã                   óò   — d dl mZ d dlmZmZmZ d dlZd dlm	Z	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d	ej,                  d
ej,                  fd„Z G d„ de«      Z G d„ de«      Z G d„ de«      Zy)é    )Úsingledispatchmethod)ÚDictÚListÚOptionalN)Ú
AnnotationÚSegmentÚSlidingWindowÚSlidingWindowFeatureÚTimeline)Ú
BaseMetric)Ú DetectionPrecisionRecallFMeasure)ÚDiarizationErrorRate)Ú	permutateÚ	referenceÚ
hypothesisc                 ó�  — | j                  t        j                  «      } |j                  t        j                  «      }t        | t        j                     |«      \  \  }}dt        j
                  | «      z  }t        j
                  |d¬«      t        j
                  | d¬«      z
  }t        j                  d|«      }t        j                  d| «      }t        j
                  || k7  |z  d¬«      |z
  }t        j
                  |«      }t        j
                  |«      }t        j
                  |«      }||z   |z   |z  }|||||dœfS )a­  Discrete diarization error rate

    Parameters
    ----------
    reference : (num_frames, num_speakers) np.ndarray
        Discretized reference diarization.
        reference[f, s] = 1 if sth speaker is active at frame f, 0 otherwise
    hypothesis : (num_frames, num_speakers) np.ndarray
        Discretized hypothesized diarization.
       hypothesis[f, s] = 1 if sth speaker is active at frame f, 0 otherwise

    Returns
    -------
    der : float
        (false_alarm + missed_detection + confusion) / total
    components : dict
        Diarization error rate components, in number of frames.
        Keys are "false alarm", "missed detection", "confusion", and "total".
    ç      ð?é   )Úaxisr   )úfalse alarmúmissed detectionÚ	confusionÚtotal)ÚastypeÚnpÚhalfr   ÚnewaxisÚsumÚmaximum)	r   r   Ú_r   Údetection_errorÚfalse_alarmÚmissed_detectionr   Úders	            úp/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/pyannote/audio/utils/metric.pyÚdiscrete_diarization_error_rater&   )   s'  € ð* × Ñ ¤§¡Ó)€IØ×"Ñ"¤2§7¡7Ó+€Jô ! ¬2¯:©:Ñ!6¸
ÓCÑ�M€Z�1ð ”"—&‘&˜Ó#Ñ#€Eô —f‘f˜Z¨aÔ0´2·6±6¸)È!Ô3LÑL€OÜ—*‘*˜Q Ó0€KÜ—z‘z ! oÐ%5Ó6Ðô —‘˜
 iÑ/°:Ñ=ÀAÔFÈÑT€Iä—&‘&˜Ó%€KÜ—v‘vÐ.Ó/ÐÜ—‘�yÓ!€IàÐ)Ñ)¨IÑ5¸Ñ
>€Cð 	à&Ø 0Ø"Øñ		
ðð ó    c            	       ó  — e Zd ZdZed„ «       Zed„ «       Z	 ddee   fd„Z	e
	 ddee   fd„«       Zej                  	 ddej                  d	ej                  dee   fd
„«       Zej                  	 dded	edee   fd„«       Zd„ Zy)ÚDiscreteDiarizationErrorRatez9Compute diarization error rate on discretized annotationsc                  ó   — y)Nzdiscrete diarization error rate© ©Úclss    r%   Úmetric_namez(DiscreteDiarizationErrorRate.metric_namec   s   € à0r'   c                 ó
   — g d¢S ©N)r   r   r   r   r+   r,   s    r%   Úmetric_componentsz.DiscreteDiarizationErrorRate.metric_componentsg   ó   € âHÐHr'   NÚuemc                 ó*   — | j                  |||¬«      S )N©r3   )Úcompute_components_helper)Úselfr   r   r3   s       r%   Úcompute_componentsz/DiscreteDiarizationErrorRate.compute_componentsk   s   € ð ×-Ñ-¨j¸)ÈÐ-ÓMÐMr'   c                 óL   — |j                   j                  }t        d|› d�«      ‚)NzProviding hypothesis as z instances is not supported.)Ú	__class__Ú__name__ÚNotImplementedError)r7   r   r   r3   Úklasss        r%   r6   z6DiscreteDiarizationErrorRate.compute_components_helpers   s/   € ð ×$Ñ$×-Ñ-ˆÜ!Ø& u gÐ-IÐJó
ð 	
r'   r   r   c                 óˆ  — |j                   dk7  rt        d«      ‚|�t        d«      ‚|j                  \  }}|j                   dk7  rt        d«      ‚|j                  \  }}||k7  rt        d«      ‚||kD  rt	        j
                  |dd||z
  ff«      }n"||kD  rt	        j
                  |dd||z
  ff«      }t        ||«      d   S )	Né   z>Only (num_frames, num_speakers)-shaped reference is supported.z)`uem` is not supported with numpy arrays.z?Only (num_frames, num_speakers)-shaped hypothesis is supported.z=reference and hypothesis must have the same number of frames.)r   r   r   r   )Úndimr<   Ú
ValueErrorÚshaper   Úpadr&   )r7   r   r   r3   Úref_num_framesÚref_num_speakersÚhyp_num_framesÚhyp_num_speakerss           r%   Úder_from_ndarrayz-DiscreteDiarizationErrorRate.der_from_ndarray|   sû   € ð �>‰>˜QÒÜ%ØPóð ð ˆ?ÜÐHÓIÐIà+4¯?©?Ñ(ˆÐ(à�?‰?˜aÒÜ%ØQóð ð ,6×+;Ñ+;Ñ(ˆÐ(à˜^Ò+ÜØOóð ð Ð.Ò.ÜŸ™Ø˜F QÐ(8Ð;KÑ(KÐ$LÐMó‰Ið Ð 0Ò0ÜŸ™Ø˜V aÐ)9Ð<LÑ)LÐ%MÐNóˆJô /¨y¸*ÓEÀaÑHÐHr'   c                 óL  — |j                   j                  }|dk  s|dkD  rt        d«      ‚|dk(  r|j                  }|j                  }nc|dk(  r^|j                  }|j                   j
                  \  }}	}
t        |d   j                  ||dz
     j                  «      }|j                  |	z  }|j                  ¬«      }|dk(  r¼|€&| j                  |j                   |j                   «      S t        |g«      j                  |«      st        d«      ‚| j                  «       }|D ]W  }|j!                  |«      }|j!                  |«      }| j                  ||«      }| j"                  D ]  }||xx   ||   z  cc<   Œ ŒY |S |dk(  rŸ| j                  «       }|D ]ˆ  \  }}|�|j                  t        |g«      «      sŒ$|j!                  |d¬	«      }t%        	|j
                  d   «      }| j                  |d | |d | «      }| j"                  D ]  }||xx   ||   z  cc<   Œ ŒŠ |S y )
Nr?   é   ziOnly (num_frames, num_speakers) or (num_chunks, num_frames, num_speakers)-shaped hypothesis is supported.r   r   )Ú
resolutionz)`uem` must fully cover hypothesis extent.Úcenter)Úmode)Údatar@   r<   ÚextentÚsliding_windowrB   r   ÚstartÚendÚdurationÚ
discretizer6   r   ÚcoversrA   Úinit_componentsÚcropÚcomponents_Úmin)r7   r   r   r3   r@   ÚsupportrK   ÚchunksÚ
num_chunksÚ
num_framesr    Ú
componentsÚsegmentÚhÚrÚsegment_componentÚnameÚwindowÚhypothesis_windowÚreference_windowÚcommon_num_framesÚwindow_componentss                         r%   Úder_from_swfz)DiscreteDiarizationErrorRate.der_from_swf¥   sk  € ð �‰×#Ñ#ˆØ�!Š8�t˜a’xÜ%ð+óð ð �1Š9Ø ×'Ñ'ˆGØ#×2Ñ2‰Jð �QŠYØ×.Ñ.ˆFØ(2¯©×(=Ñ(=Ñ%ˆJ˜
 AÜ˜f Q™iŸo™o¨v°jÀ1±nÑ/E×/IÑ/IÓJˆGØŸ™¨:Ñ5ˆJð ×(Ñ(¨¸ZÐ(ÓHˆ	ð �1Š9àˆ{Ø×5Ñ5°j·o±oÀyÇ~Á~ÓVÐVä˜W˜IÓ&×-Ñ-¨cÔ2Ü Ð!LÓMÐMà×-Ñ-Ó/ˆJØò @�Ø—O‘O GÓ,�Ø—N‘N 7Ó+�Ø$(×$BÑ$BÀ1ÀaÓ$HÐ!Ø ×,Ñ,ò @�DØ˜tÓ$Ð(9¸$Ñ(?Ñ?Ô$ñ@ð	@ð Ðð �QŠYà×-Ñ-Ó/ˆJØ-7ò @Ñ)�Ð)ð �?¨3¯:©:´hÀ¸xÓ6HÔ+IØà#,§>¡>°&¸x >Ó#HÐ ä$'¨
Ð4D×4JÑ4JÈ1Ñ4MÓ$NÐ!à$(×$BÑ$BØ%Ð&8Ð'8Ð9Ø$Ð%7Ð&7Ð8ó%Ð!ð
 !×,Ñ,ò @�DØ˜tÓ$Ð(9¸$Ñ(?Ñ?Ô$ñ@ð@ð$ Ðð+ r'   c                 ó0   — |d   |d   z   |d   z   |d   z  S ©Nr   r   r   r   r+   ©r7   r^   s     r%   Úcompute_metricz+DiscreteDiarizationErrorRate.compute_metricí   ó9   € à�}Ñ%ØÐ+Ñ,ñ-à˜Ñ%ñ&ð �wÑñ	 ð 	 r'   ©N)r;   Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr.   r1   r   r   r8   r   r6   Úregisterr   ÚndarrayrH   r
   r   ri   rm   r+   r'   r%   r)   r)   `   s  „ ÙCàñ1ó ð1ð ñIó ðIð #'ñ	Nð �hÑó	Nð à?Cñ
Ø*2°8Ñ*<ò
ó ð
ð ×'Ñ'ð
 #'ñ	&Ià—J‘Jð&Ið —:‘:ð&Ið �hÑò	&Ió (ð&IðP ×'Ñ'ð
 #'ñ	Eà(ðEð ðEð �hÑò	Eó (ðEóN r'   r)   c                   ób   ‡ — e Zd Zddefˆ fd„Zed„ «       Zed„ «       Z	 d	dee	   fd„Z
d„ Zˆ xZS )
ÚSlidingDiarizationErrorRaterd   c                 ó0   •— t         ‰| �  «        || _        y ro   )ÚsuperÚ__init__rd   )r7   rd   r:   s     €r%   rz   z$SlidingDiarizationErrorRate.__init__ö   s   ø€ Ü‰ÑÔØˆ�r'   c                  ó   — y)Nzwindow diarization error rater+   r,   s    r%   r.   z'SlidingDiarizationErrorRate.metric_nameú   s   € à.r'   c                 ó
   — g d¢S r0   r+   r,   s    r%   r1   z-SlidingDiarizationErrorRate.metric_componentsþ   r2   r'   r3   c                 ó  — |€t        d«      ‚t        «       }t        | j                  d| j                  z  ¬«      } ||«      D ]5  } ||j	                  |«      |j	                  |«      t        |g«      ¬«      }Œ7 |d d  S )Nz9SlidingDiarizationErrorRate expects `uem` to be provided.g      à?)rS   Ústepr5   )rA   r   r	   rd   rW   r   )r7   r   r   r3   r$   rd   Úchunkr    s           r%   r8   z.SlidingDiarizationErrorRate.compute_components  s„   € ð ˆ;ÜØKóð ô #Ó$ˆä¨¯©¸#ÀÇÁÑ:KÔLˆá˜C“[ò 	ˆEÙØ—‘˜uÓ% z§¡°uÓ'=Ä8ÈUÈGÓCTô‰Að	ð
 ‘1ˆvˆr'   c                 ó0   — |d   |d   z   |d   z   |d   z  S rk   r+   rl   s     r%   rm   z*SlidingDiarizationErrorRate.compute_metric  rn   r'   )g      $@ro   )r;   rp   rq   Úfloatrz   rs   r.   r1   r   r   r8   rm   Ú__classcell__©r:   s   @r%   rw   rw   õ   sW   ø„ ñ˜uõ ð ñ/ó ð/ð ñIó ðIð #'ñ	ð �hÑó	ö. r'   rw   c                   ó”   ‡ — e Zd ZdZd„ Zed„ «       Z	 	 ddee   de	de	fd„Z
ˆ fd„Z	 dd	ed
efd„Zdeee	f   fd„Zdˆ fd„	Zd„ Zˆ xZS )ÚMacroAverageFMeasurea   Compute macro-average F-measure

    Parameters
    ----------
    collar : float, optional
        Duration (in seconds) of collars removed from evaluation around
        boundaries of reference segments (one half before, one half after).
    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.DetectionPrecisionRecallFMeasure
    c                 ó   — | j                   S ro   )Úclasses)r7   s    r%   r1   z&MacroAverageFMeasure.metric_components3  s   € Ø�|‰|Ðr'   c                  ó   — y)NzMacro F-measurer+   r,   s    r%   r.   z MacroAverageFMeasure.metric_name6  s   € à r'   r‡   ÚcollarÚbetac           
      ó  — | j                  «       | _        || _        t        | j	                  «       «      | _        || _        || _        | j                  D �ci c]  }|t        d||dœ|¤Ž“Œ c}| _	        | j                  «        y c c}w )N)r‰   rŠ   r+   )r.   Úmetric_name_r‡   Úsetr1   rX   r‰   rŠ   r   Ú_sub_metricsÚreset)r7   r‡   r‰   rŠ   ÚkwargsÚlabels         r%   rz   zMacroAverageFMeasure.__init__:  sŠ   € ð !×,Ñ,Ó.ˆÔàˆŒÜ˜t×5Ñ5Ó7Ó8ˆÔàˆŒØˆŒ	ð Ÿ™öJ
àð Ô3ÐW¸6ÈÑWÐPVÑWÑWòJ
ˆÔð
 	�
‰
�ùòJ
s   ÁBc                 ó€   •— t         ‰| �  «        | j                  j                  «       D ]  }|j                  «        Œ y ro   )ry   r�   rŽ   Úvalues)r7   Ú
sub_metricr:   s     €r%   r�   zMacroAverageFMeasure.resetP  s6   ø€ Ü‰‰ŒØ×+Ñ+×2Ñ2Ó4ò 	ˆJØ×ÑÕñ	r'   r   r   c                 óÈ   — | j                  «       }| j                  j                  «       D ]4  \  }} |d|j                  |g«      |j                  |g«      |dœ|¤Ž||<   Œ6 |S )N)r   r   r3   r+   )rV   rŽ   ÚitemsÚsubset)r7   r   r   r3   r�   Údetailsr‘   r”   s           r%   r8   z'MacroAverageFMeasure.compute_componentsU  s|   € ð ×&Ñ&Ó(ˆØ!%×!2Ñ!2×!8Ñ!8Ó!:ò 	ÑˆE�:Ù'ð Ø#×*Ñ*¨E¨7Ó3Ø%×,Ñ,¨e¨WÓ5Øñð ñ	ˆG�EŠNð	ð ˆr'   Údetailc                 óZ   — t        j                  t        |j                  «       «      «      S ro   )r   ÚmeanÚlistr“   )r7   r™   s     r%   rm   z#MacroAverageFMeasure.compute_metricc  s   € Ü�w‰w”t˜FŸM™M›OÓ,Ó-Ð-r'   c                 óä   •— t         ‰| �  d¬«      }| j                  j                  «       D ]   \  }}t	        |«      |j
                  d   |<   Œ" |rt        |j                  dddd„ ¬«      «       |S )NF)ÚdisplayÚTOTALTÚrightc                 ó$   — dj                  | «      S )Nz{0:.2f})Úformat)Úfs    r%   ú<lambda>z-MacroAverageFMeasure.report.<locals>.<lambda>r  s   € ¨9×+;Ñ+;¸AÓ+>€ r'   )ÚindexÚsparsifyÚjustifyÚfloat_format)ry   ÚreportrŽ   r–   ÚabsÚlocÚprintÚ	to_string)r7   rž   Údfr‘   r”   r:   s        €r%   r©   zMacroAverageFMeasure.reportf  s}   ø€ Ü‰W‰^ Eˆ^Ó*ˆà!%×!2Ñ!2×!8Ñ!8Ó!:ò 	5ÑˆE�:Ü%(¨£_ˆB�F‰F�7‰O˜EÒ"ð	5ñ ÜØ—‘ØØ"Ø#Ù!>ð	 ó ôð ˆ	r'   c                 ó”   — t        j                  | j                  j                  «       D �cg c]  }t	        |«      ‘Œ c}«      S c c}w ro   )r   r›   rŽ   r“   rª   )r7   r”   s     r%   Ú__abs__zMacroAverageFMeasure.__abs__x  s2   € Ü�w‰w¸$×:KÑ:K×:RÑ:RÓ:TÖU¨Jœ˜J�ÒUÓVÐVùÒUs   ¬A)g        r   ro   )F)r;   rp   rq   rr   r1   rs   r.   r   Ústrr�   rz   r�   r   r8   r   rm   r©   r°   r‚   rƒ   s   @r%   r…   r…   !  s�   ø„ ñò"ð ñ!ó ð!ð Øñ	à�c‘ðð ðð ó	ô,ð BFñØ#ðØ1;óð. T¨#¨u¨*Ñ%5ó .õö$Wr'   r…   )Ú	functoolsr   Útypingr   r   r   Únumpyr   Úpyannote.corer   r   r	   r
   r   Úpyannote.metrics.baser   Úpyannote.metrics.detectionr   Úpyannote.metrics.diarizationr   Ú pyannote.audio.utils.permutationr   ru   r&   r)   rw   r…   r+   r'   r%   ú<module>rº      ss   ðõ. +ß 'Ñ 'ã ÷õ õ -Ý GÝ =å 6ð4¨r¯z©zð 4ÀrÇzÁzó 4ônR  :ô R ôj)  *ô ) ôXXW˜:õ XWr'   