Ë
    ÿÍ:jo4  ã                   óÎ   — d dl mZmZmZmZmZ d dlZd dlZd dl	Z
d dlZd dlmZmZ d dlmZmZ  G d„ d«      ZdZdZd	Z G d
„ de«      ZdZdZd	Z G d„ de«      Zddededefd„Zy)é    )ÚListÚUnionÚOptionalÚSetÚTupleN)Ú
AnnotationÚTimeline)ÚDetailsÚMetricComponentsc            
       óL  ‡ — e Zd ZdZedefd„«       Zedefd„«       Zˆ fd„Z	d„ Z
d„ Zed„ «       Z	 dd	eeef   d
eeef   dedee   fd„Zddedej*                  fd„Zd„ Zd„ Zdedeeef   fd„Zd„ Zd	eeef   d
eeef   defd„Zdefd„Zddedeeeeef   f   fd„Z ˆ xZ!S )Ú
BaseMetriczÌ
    :class:`BaseMetric` is the base class for most pyannote evaluation metrics.

    Attributes
    ----------
    name : str
        Human-readable name of the metric (eg. 'diarization error rate')
    Úreturnc                 ó2   — t        | j                  dz   «      ‚)Nz\ is missing a 'metric_name' class method. It should return the name of the metric as string.©ÚNotImplementedErrorÚ__name__©Úclss    új/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/pyannote/metrics/base.pyÚmetric_namezBaseMetric.metric_name1   s"   € ä!Ø�L‰Lð Pñ Pó
ð 	
ó    c                 ó2   — t        | j                  dz   «      ‚)Nzh is missing a 'metric_components' class method. It should return the list of names of metric components.r   r   s    r   Úmetric_componentszBaseMetric.metric_components8   s"   € ä!Ø�L‰Lð Vñ Vó
ð 	
r   c                 óØ   •— t         t        | �  «        | j                  j	                  «       | _        t        | j                  j                  «       «      | _        | j                  «        y ©N)
Úsuperr   Ú__init__Ú	__class__r   Úmetric_name_Úsetr   Úcomponents_Úreset)ÚselfÚkwargsr   s     €r   r   zBaseMetric.__init__?   sH   ø€ ÜŒj˜$Ñ(Ô*Ø ŸN™N×6Ñ6Ó8ˆÔÜ%(¨¯©×)IÑ)IÓ)KÓ%LˆÔØ�
‰
�r   c                 óB   — | j                   D �ci c]  }|d“Œ c}S c c}w )Nç        )r!   ©r#   Úvalues     r   Úinit_componentszBaseMetric.init_componentsE   s    € Ø(,×(8Ñ(8Ö9˜u��s‘
Ò9Ð9ùÒ9s   �
c                 ó€   — t        «       | _        t        «       | _        | j                  D ]  }d| j                  |<   Œ y)z.Reset accumulated components and metric valuesr&   N)ÚdictÚaccumulated_ÚlistÚresults_r!   r'   s     r   r"   zBaseMetric.resetH   s:   € ä%)£VˆÔÜ"›fˆŒØ×%Ñ%ò 	+ˆEØ'*ˆD×Ñ˜eÒ$ñ	+r   c                 ó"   — | j                  «       S )zMetric name.)r   ©r#   s    r   ÚnamezBaseMetric.nameO   s   € ð ×ÑÓ!Ð!r   Ú	referenceÚ
hypothesisÚdetailedÚuric                 ó>  —  | j                   ||fi |¤Ž}| j                  |«      || j                  <   |xs t        |dd«      }| j                  j                  ||f«       | j                  D ]  }| j                  |xx   ||   z  cc<   Œ |r|S || j                     S )aÞ  Compute metric value and accumulate components

        Parameters
        ----------
        reference : type depends on the metric
            Manual `reference`
        hypothesis : type depends on the metric
            Evaluated `hypothesis`
        uri : optional
            Override uri.
        detailed : bool, optional
            By default (False), return metric value only.
            Set `detailed` to True to return dictionary where keys are
            components names and values are component values

        Returns
        -------
        value : float (if `detailed` is False)
            Metric value
        components : dict (if `detailed` is True)
            `components` updated with metric value
        r5   ÚNA)Úcompute_componentsÚcompute_metricr   Úgetattrr.   Úappendr!   r,   )r#   r2   r3   r4   r5   r$   Ú
componentsr1   s           r   Ú__call__zBaseMetric.__call__W   s´   € ð6 -�T×,Ñ,¨Y¸
ÑMÀfÑMˆ
ð )-×(;Ñ(;¸JÓ(Gˆ
�4×$Ñ$Ñ%ð Ò4”W˜Y¨¨tÓ4ˆØ�‰×Ñ˜c :Ð.Ô/ð ×$Ñ$ò 	8ˆDØ×Ñ˜dÓ# z°$Ñ'7Ñ7Ô#ð	8ñ ØÐà˜$×+Ñ+Ñ,Ð,r   Údisplayc                 óR  — g }g }d| j                  «       v }| j                  D ]¡  \  }}i }|r|d   }|j                  «       D ]^  \  }	}
|	| j                  k(  rd|
z  ||	df<   Œ |	dk(  r|
||	df<   Œ-|
||	df<   |sŒ7dkD  rd|
z  |z  ||	df<   ŒJt        j
                  ||	df<   Œ` |j                  |«       |j                  |«       Œ£ i }| j                  }|r|d   }|j                  «       D ]^  \  }	}
|	| j                  k(  rd|
z  ||	df<   Œ |	dk(  r|
||	df<   Œ-|
||	df<   |sŒ7dkD  rd|
z  |z  ||	df<   ŒJt        j
                  ||	df<   Œ` dt        | «      z  || j                  df<   |j                  |«       |j                  d«       t        j                  |«      }||d<   |j                  d«      }t        j                  j                  |j                  «      |_        || j                  g| j                  «       z      }|rt        |j!                  dd	d
d„ ¬«      «       |S )ak  Evaluation report

        Parameters
        ----------
        display : bool, optional
            Set to True to print the report to stdout.

        Returns
        -------
        report : pandas.DataFrame
            Dataframe with one column per metric component, one row per
            evaluated item, and one final row for accumulated results.
        Útotaléd   ú%Ú r   ÚTOTALÚitemTFÚrightc                 ó$   — dj                  | «      S ©Nz{0:.2f}©Úformat©Úfs    r   ú<lambda>z#BaseMetric.report.<locals>.<lambda>Ò   s   € ¨9×+;Ñ+;¸AÓ+>€ r   )ÚindexÚsparsifyÚjustifyÚfloat_format)r   r.   Úitemsr1   ÚnpÚnanr;   r,   ÚabsÚpdÚ	DataFrameÚ	set_indexÚ
MultiIndexÚfrom_tuplesÚcolumnsÚprintÚ	to_string)r#   r>   ÚreportÚurisÚpercentr5   r<   Úrowr@   Úkeyr(   Údfs               r   r^   zBaseMetric.report„   sY  € ð ˆØˆà˜T×3Ñ3Ó5Ð5ˆà#Ÿ}™}ò 	‰OˆC�ØˆCÙØ" 7Ñ+�Ø(×.Ñ.Ó0ò 3‘
��UØ˜$Ÿ)™)Ò#Ø$'¨%¡K�C˜˜S˜’MØ˜G’^Ø#(�C˜˜R˜’Là#(�C˜˜R˜‘LÚØ  1š9Ø,/°%©K¸%Ñ,?˜C  S šMä,.¯F©F˜C  S šMð3ð �M‰M˜#ÔØ�K‰K˜Õð%	ð( ˆØ×&Ñ&ˆ
áØ˜wÑ'ˆEà$×*Ñ*Ó,ò 	/‰JˆC�Ø�d—i‘iÒØ # e¡��C˜�H’Ø˜’Ø$��C˜�G’à$��C˜�G‘ÚØ˜q’yØ(+¨e©°eÑ(;˜˜C ˜Hšä(*¯©˜˜C ˜Hšð	/ð "¤C¨£I™oˆˆD�I‰I�sˆNÑØ�‰�cÔØ�‰�GÔä�\‰\˜&Ó!ˆàˆˆ6‰
Ø�\‰\˜&Ó!ˆä—]‘]×.Ñ.¨r¯z©zÓ:ˆŒ
à�—‘�˜d×4Ñ4Ó6Ñ6Ñ7ˆáÜØ—‘ØØ"Ø#Ù!>ð	 ó ôð ˆ	r   c                 óN   — | j                  d¬«      }|j                  dd„ ¬«      S )NF)r>   c                 ó$   — dj                  | «      S rH   rI   rK   s    r   rM   z$BaseMetric.__str__.<locals>.<lambda>Û   s   € °9×3CÑ3CÀAÓ3F€ r   )rO   rQ   )r^   r]   )r#   r^   s     r   Ú__str__zBaseMetric.__str__Ø   s0   € Ø—‘ U�Ó+ˆØ×ÑØÑ)Fð  ó 
ð 	
r   c                 ó8   — | j                  | j                  «      S )z0Compute metric value from accumulated components)r9   r,   r0   s    r   Ú__abs__zBaseMetric.__abs__Þ   s   € à×"Ñ" 4×#4Ñ#4Ó5Ð5r   Ú	componentc                 ój   — |t        ddd«      k(  rt        | j                  «      S | j                  |   S )a  Get value of accumulated `component`.

        Parameters
        ----------
        component : str
            Name of `component`

        Returns
        -------
        value : type depends on the metric
            Value of accumulated `component`

        N)Úslicer+   r,   )r#   ri   s     r   Ú__getitem__zBaseMetric.__getitem__â   s7   € ð œ˜d D¨$Ó/Ò/Ü˜×)Ñ)Ó*Ð*à×$Ñ$ YÑ/Ð/r   c              #   ó@   K  — | j                   D ]  \  }}||f–— Œ y­w)z*Iterator over the accumulated (uri, value)N)r.   )r#   r5   ri   s      r   Ú__iter__zBaseMetric.__iter__õ   s'   è ø€ à"Ÿm™mò 	!‰NˆC�Ø�y�.Ó ñ	!ùs   ‚c                 óF   — t        | j                  j                  dz   «      ‚)a�  Compute metric components

        Parameters
        ----------
        reference : type depends on the metric
            Manual `reference`
        hypothesis : same as `reference`
            Evaluated `hypothesis`

        Returns
        -------
        components : dict
            Dictionary where keys are component names and values are component
            values

        z‡ is missing a 'compute_components' method.It should return a dictionary where keys are component names and values are component values.©r   r   r   )r#   r2   r3   r$   s       r   r8   zBaseMetric.compute_componentsú   s*   € ô( "Ø�N‰N×#Ñ#ð 'Iñ Ió
ð 	
r   r<   c                 óF   — t        | j                  j                  dz   «      ‚)aA  Compute metric value from computed `components`

        Parameters
        ----------
        components : dict
            Dictionary where keys are components names and values are component
            values

        Returns
        -------
        value : type depends on the metric
            Metric value
        z” is missing a 'compute_metric' method. It should return the actual value of the metric based on the precomputed component dictionary given as input.rp   )r#   r<   s     r   r9   zBaseMetric.compute_metric  s*   € ô "Ø�N‰N×#Ñ#ð '`ñ `ó
ð 	
r   Úalphac                 óB  — | j                   D ��cg c]  \  }}|| j                     ‘Œ }}}t        |«      dk(  rt        d«      ‚t        |«      dk(  r$t	        j
                  d«       |d   x}x}}|||ffS t        j                  j                  ||¬«      d   S c c}}w )aP  Compute confidence interval on accumulated metric values

        Parameters
        ----------
        alpha : float, optional
            Probability that the returned confidence interval contains
            the true metric value.

        Returns
        -------
        (center, (lower, upper))
            with center the mean of the conditional pdf of the metric value
            and (lower, upper) is a confidence interval centered on the median,
            containing the estimate to a probability alpha.

        See Also:
        ---------
        scipy.stats.bayes_mvs

        r   zFPlease evaluate a bunch of files before computing confidence interval.é   zCCannot compute a reliable confidence interval out of just one file.)rr   )	r.   r   ÚlenÚ
ValueErrorÚwarningsÚwarnÚscipyÚstatsÚ	bayes_mvs)r#   rr   Ú_ÚrÚvaluesÚcenterÚlowerÚuppers           r   Úconfidence_intervalzBaseMetric.confidence_interval(  s¤   € ð. 48·=±=×A©4¨1¨a�!�D×%Ñ%Ó&ÐAˆÑAäˆv‹;˜!ÒÜÐeÓfÐfä�‹[˜AÒÜ�M‰MÐ_Ô`Ø%+¨A¡YÐ.ˆFÐ.�U˜UØ˜E 5˜>Ð)Ð)ô —;‘;×(Ñ(¨°uÐ(Ó=¸aÑ@Ð@ùó Bs   �B)FN)F)gÍÌÌÌÌÌì?)"r   Ú
__module__Ú__qualname__Ú__doc__ÚclassmethodÚstrr   r   r   r   r)   r"   Úpropertyr1   r   r	   r   Úboolr   r=   rV   rW   r^   rf   rh   Úfloatr
   rl   rn   r8   r9   r   r‚   Ú__classcell__)r   s   @r   r   r   '   sT  ø„ ñð ð
˜Cò 
ó ð
ð ð
Ð"2ò 
ó ð
ôò:ò+ð ñ"ó ð"ð ?Cñ+- %¨°*Ð(<Ñ"=ð +-Ø" 8¨ZÐ#7Ñ8ð+-àð+-à.6°s©mó+-ñZR˜dð R¨r¯|©|ó Ròh
ò6ð0 Sð 0¨U°5¸'°>Ñ-Bó 0ò&!ð

Ø&+¨H°jÐ,@Ñ&Að
à',¨X°zÐ-AÑ'Bð
ð )0ó
ð4
¨ó 
ñ("A¨ð "AØ�U˜E %¨ ,Ñ/Ð/Ñ0÷"Ar   r   Ú	precisionz# retrievedz# relevant retrievedc                   óF   — e Zd ZdZed„ «       Zedefd„«       Zdede	fd„Z
y)Ú	PrecisionaU  
    :class:`Precision` is a base class for precision-like evaluation metrics.

    It defines two components '# retrieved' and '# relevant retrieved' and the
    compute_metric() method to compute the actual precision:

        Precision = # retrieved / # relevant retrieved

    Inheriting classes must implement compute_components().
    c                 ó   — t         S r   )ÚPRECISION_NAMEr   s    r   r   zPrecision.metric_name^  s   € äÐr   r   c                 ó   — t         t        gS r   )ÚPRECISION_RETRIEVEDÚPRECISION_RELEVANT_RETRIEVEDr   s    r   r   zPrecision.metric_componentsb  s   € ä#Ô%AÐBÐBr   r<   c                 ó\   — |t            }|t           }|dk(  r|dk(  ryt        d«      ‚||z  S )z#Compute precision from `components`r&   r   ç      ð?rC   )r“   r’   rv   ©r#   r<   Ú	numeratorÚdenominators       r   r9   zPrecision.compute_metricf  s?   € àÔ;Ñ<ˆ	Ø Ô!4Ñ5ˆØ˜#ÒØ˜AŠ~Øä  “nÐ$à˜{Ñ*Ð*r   N©r   rƒ   r„   r…   r†   r   r   r   r
   rŠ   r9   © r   r   rŽ   rŽ   R  sM   „ ñ	ð ñó ðð ðCÐ"2ò Có ðCð
+¨ð 
+°Uô 
+r   rŽ   Úrecallz
# relevantc                   óF   — e Zd ZdZed„ «       Zedefd„«       Zdede	fd„Z
y)ÚRecallaG  
    :class:`Recall` is a base class for recall-like evaluation metrics.

    It defines two components '# relevant' and '# relevant retrieved' and the
    compute_metric() method to compute the actual recall:

        Recall = # relevant retrieved / # relevant

    Inheriting classes must implement compute_components().
    c                 ó   — t         S r   )ÚRECALL_NAMEr   s    r   r   zRecall.metric_name„  s   € äÐr   r   c                 ó   — t         t        gS r   )ÚRECALL_RELEVANTÚRECALL_RELEVANT_RETRIEVEDr   s    r   r   zRecall.metric_componentsˆ  s   € äÔ!:Ð;Ð;r   r<   c                 ó\   — |t            }|t           }|dk(  r|dk(  ryt        d«      ‚||z  S )z Compute recall from `components`r&   r   r•   rC   )r¢   r¡   rv   r–   s       r   r9   zRecall.compute_metricŒ  s>   € àÔ8Ñ9ˆ	Ø ¤Ñ1ˆØ˜#ÒØ˜AŠ~Øä  “nÐ$à˜{Ñ*Ð*r   Nr™   rš   r   r   r�   r�   x  sJ   „ ñ	ð ñó ðð ð<Ð"2ò <ó ð<ð
+¨ð 
+°Uô 
+r   r�   r   c                 óH   — | |z   dk(  ryd||z  z   | z  |z  ||z  | z  |z   z  S )u¦   Compute f-measure

    f-measure is defined as follows:
        F(P, R, b) = (1+bÂ²).P.R / (bÂ².P + R)

    where P is `precision`, R is `recall` and b is `beta`
    r&   r   rt   rš   )rŒ   r›   Úbetas      r   Ú	f_measurer¦   ™  s?   € ð �6Ñ˜SÒ ØØ��t‘‰O˜yÑ(¨6Ñ1°T¸D±[À9Ñ5LÈvÑ5UÑVÐVr   )r•   )Útypingr   r   r   r   r   rw   ÚnumpyrS   ÚpandasrV   Úscipy.statsry   Úpyannote.corer   r	   Úpyannote.metrics.typesr
   r   r   r�   r’   r“   rŽ   rŸ   r¡   r¢   r�   rŠ   r¦   rš   r   r   ú<module>r­      s‹   ð÷8 5Õ 4ã Û Û Û ß .ç <÷cAñ cAðL	 €Ø#Ð Ø5Ð ô+�
ô +ðB €Ø€Ø2Ð ô+ˆZô +ñB
W˜ð 
W¨ð 
W¸Eô 
Wr   