
      iV3                        d dl mZmZmZ d dlZd dlmZmZm	Z	 d dl
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dZdZdZdZdZdZdZdZdZ  G d de      Z! G d de!      Z" G d de!      Z# G d dee      Z$ G d de$      Z%y)     )TupleUnionOptionalN)SegmentTimeline
Annotation)pairwise   )
BaseMetric	f_measure)MetricComponentsDetails)UEMSupportMixinzsegmentation purityzsegmentation coveragezsegmentation F[purity|coverage]ztotal durationzintersection durationzpty total durationzpty intersection durationzcvg total durationzcvg intersection durationzsegmentation precisionzsegmentation recallznumber of boundariesznumber of matchesc                        e Zd ZdZddef fdZdededefdZded	e	eef   de
eef   fd
Zded	edefdZed        Zedefd       Zded	e	eef   fdZdedefdZ xZS )SegmentationCoveragezSegmentation coverage

    Parameters
    ----------
    tolerance : float, optional
        When provided, preprocess reference by filling intra-label gaps shorter
        than `tolerance` (in seconds).

    	tolerancec                 2    t        |   di | || _        y N super__init__r   selfr   kwargs	__class__s      r/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pyannote/metrics/segmentation.pyr   zSegmentationCoverage.__init__G   s    "6""    timelinecoveragereturnc                 B   t        g       }|D ]8  }|j                  |j                         |j                  |j                         : t	               }t        t        |            D ]  \  }}t        ||      }d||<    |j                  |d      j                         S )N_intersection)mode)
setaddstartendr   r	   sortedr   croprelabel_tracks)r   r   r    
boundariessegment	partitionr(   r)   s           r   
_partitionzSegmentationCoverage._partitionK   s    
 W
GNN7==)NN7;;'  
 L	"6*#56JE3eS)G!$Ig 7 ~~h^~<KKMMr   	reference
hypothesisc                    t        |t              st        d      t        |t              r|j                         }t	               }|j                         D ]y  }|j                  |      }|j                         D ]-  }|j                  | j                  k  s|j                  |       / |j                         D ]  }|j                  |        { |j                         }| j                  ||      }	| j                  ||      }
|	|
fS )Nz-reference must be an instance of `Annotation`)
isinstancer   	TypeErrorget_timeliner   labelslabel_timelinegapsdurationr   r'   supportr0   )r   r1   r2   filledlabelr8   gapr.   r    reference_partitionhypothesis_partitions              r   _preprocessz SegmentationCoverage._preprocess]   s     )Z0KLLj*-#-#:#:#<J %%'E&55e<N%**,<<$..0"&&s+ - *113

7# 4 ( >>#"oofh?#z8D"$888r   c                    | j                         }||z  }t        j                  |      j                         |t        <   t        j                  t        j
                  |d            j                         |t        <   |S )Nr
   axis)init_componentsnpsumitemPTY_CVG_TOTALmaxPTY_CVG_INTERr   r1   r2   detailKs        r   _processzSegmentationCoverage._processz   sa    %%' 
" "q	 0} "rvvaa'8 9 > > @}r   c                     t         S N)COVERAGE_NAMEclss    r   metric_namez SegmentationCoverage.metric_name   s    r   c                     t         t        gS rQ   )rI   rK   rS   s    r   metric_componentsz&SegmentationCoverage.metric_components   s    }--r   c                 P    | j                  ||      \  }}| j                  ||      S rQ   rA   rO   r   r1   r2   r   s       r   compute_componentsz'SegmentationCoverage.compute_components   s*     $ 0 0J G	:}}Y
33r   rM   c                 (    |t            |t           z  S rQ   )rK   rI   )r   rM   s     r   compute_metricz#SegmentationCoverage.compute_metric   s    m$vm'<<<r   )      ?)__name__
__module____qualname____doc__floatr   r   r   r0   r   r   rA   r   rO   classmethodrU   r   rW   r[   r]   __classcell__r   s   @r   r   r   <   s    #% #N%N%N*4N$9Z 9 %j(&: ;9Z+,9:	* 	* 	 	   ."2 . .4J 4',Z-A'B4
=W = =r   r   c                   D    e Zd ZdZedefd       Zdedeee	f   de
fdZy)SegmentationPurityzSegmentation purity

    Parameters
    ----------
    tolerance : float, optional
        When provided, preprocess reference by filling intra-label gaps shorter
        than `tolerance` (in seconds).

    r!   c                     t         S rQ   )PURITY_NAMErS   s    r   rU   zSegmentationPurity.metric_name       r   r1   r2   c                 P    | j                  ||      \  }}| j                  ||      S rQ   rY   rZ   s       r   r[   z%SegmentationPurity.compute_components   s,     !% 0 0J G	:}}Z33r   N)r_   r`   ra   rb   rd   strrU   r   r   r   r   r[   r   r   r   rh   rh      sH     C  4J 4',Z-A'B4(/4r   rh   c                        e Zd ZdZd fd	Zdedeeef   defdZ	dedeeef   defdZ
dedefd	Zddee   deeeef   fd
Zedefd       Zedefd       Z xZS )"SegmentationPurityCoverageFMeasureal  
    Compute segmentation purity and coverage, and return their F-score.


    Parameters
    ----------
    tolerance : float, optional
        When provided, preprocess reference by filling intra-label gaps shorter
        than `tolerance` (in seconds).

    beta : float, optional
            When beta > 1, greater importance is given to coverage.
            When beta < 1, greater importance is given to purity.
            Defaults to 1.

    See also
    --------
    pyannote.metrics.segmentation.SegmentationPurity
    pyannote.metrics.segmentation.SegmentationCoverage
    pyannote.metrics.base.f_measure
    c                 >    t        t        | 
  dd|i| || _        y )Nr   r   )r   ro   r   beta)r   r   rq   r   r   s       r   r   z+SegmentationPurityCoverageFMeasure.__init__   s#    0$@_9_X^_	r   r1   r2   r!   c                    | j                  ||      \  }}| j                         }||z  }t        j                  |      j	                         |t
        <   t        j                  t        j                  |d            j	                         |t        <   |t
           |t        <   t        j                  t        j                  |d            j	                         |t        <   |S )Nr
   rC   r   )
rA   rE   rF   rG   rH   	CVG_TOTALrJ   	CVG_INTER	PTY_TOTAL	PTY_INTERrL   s        r   rO   z+SegmentationPurityCoverageFMeasure._process   s     $ 0 0J G	:%%' 
"FF1INN,yFF266!!#45::<y #9-yFF266!!#45::<yr   c                 &    | j                  ||      S rQ   )rO   rZ   s       r   r[   z5SegmentationPurityCoverageFMeasure.compute_components   s     }}Y
33r   rM   c                 2    | j                  |      \  }}}|S )N)rM   )compute_metrics)r   rM   r#   values       r   r]   z1SegmentationPurityCoverageFMeasure.compute_metric   s     **&*91er   c                     || j                   n|}|t           dk(  rdn|t           |t           z  }|t           dk(  rdn|t           |t           z  }||t        ||| j                        fS )N              ?)rq   )accumulated_ru   rv   rs   rt   r   rq   )r   rM   purityr    s       r   ry   z2SegmentationPurityCoverageFMeasure.compute_metrics   s    &,n""& #r)BI&	):: 	
 #r)BI&	):: 	 x68$))!LLLr   c                     t         S rQ   )PURITY_COVERAGE_NAMErS   s    r   rU   z.SegmentationPurityCoverageFMeasure.metric_name   s    ##r   c                 .    t         t        t        t        gS rQ   )ru   rv   rs   rt   rS   s    r   rW   z4SegmentationPurityCoverageFMeasure.metric_components   s    9i;;r   )r^   r
   rQ   )r_   r`   ra   rb   r   r   r   r   r   rO   r[   rc   r]   r   r   ry   rd   rm   rU   r   rW   re   rf   s   @r   ro   ro      s    ,* ":x#78=D"4J 4',Z-A'B4(/4
W  Mhw&7 MUE5()M $C $ $ <"2 < <r   ro   c                   |     e Zd ZdZed        Zed        Zd fd	Zdee	e
f   dee	e
f   defdZd	edefd
Z xZS )SegmentationPrecisiona  Segmentation precision

    >>> from pyannote.core import Timeline, Segment
    >>> from pyannote.metrics.segmentation import SegmentationPrecision
    >>> precision = SegmentationPrecision()

    >>> reference = Timeline()
    >>> reference.add(Segment(0, 1))
    >>> reference.add(Segment(1, 2))
    >>> reference.add(Segment(2, 4))

    >>> hypothesis = Timeline()
    >>> hypothesis.add(Segment(0, 1))
    >>> hypothesis.add(Segment(1, 2))
    >>> hypothesis.add(Segment(2, 3))
    >>> hypothesis.add(Segment(3, 4))
    >>> precision(reference, hypothesis)
    0.6666666666666666

    >>> hypothesis = Timeline()
    >>> hypothesis.add(Segment(0, 4))
    >>> precision(reference, hypothesis)
    1.0

    c                     t         S rQ   )PRECISION_NAMErS   s    r   rU   z!SegmentationPrecision.metric_name  s    r   c                     t         t        gS rQ   )
PR_MATCHESPR_BOUNDARIESrS   s    r   rW   z'SegmentationPrecision.metric_components  s    M**r   c                 2    t        |   di | || _        y r   r   r   s      r   r   zSegmentationPrecision.__init__  s    "6""r   r1   r2   r!   c                    t        |t              r|j                         }t        |t              r|j                         }| j                         }d}t	        |      dz
  }t	        |      dz
  }||t
        <   |dk(  s|dk(  rd|t        <   |S |D cg c]  }|j                   c}d d }	|D cg c]  }|j                   c}d d }
t        j                  ||f      }t        |	      D ]+  \  }}t        |
      D ]  \  }}t        ||z
        |||f<    - t        j                  |t        j                  || j                  kD        <   t        j                  |      }|t        j                  k  r{|dz  }t        j                   |      }||z  }||z  }t        j                  ||d d f<   t        j                  |d d |f<   t        j                  |      }|t        j                  k  r{||t        <   |S c c}w c c}w )Nr|   r
   r   )r4   r   r6   rE   lenr   r   r)   rF   zeros	enumerateabsinfwherer   aminargmin)r   r1   r2   r   rM   	n_matchesNMr.   ref_boundarieshyp_boundariesdeltarrefBoundaryhhypBoundarykijs                      r   r[   z(SegmentationPrecision.compute_components!  s    i,!..0Ij*-#002J%%' 	 	NQ
Oa !"} 6Q!V!#F:M 6??Y''++Y?D5?@Z''++Z@"E !Q '7NA{"+N";;!+";<ad #< 8 35&&bhhut~~-./ GGEN "&&jNI 		% AQAAA &&E!Q$K&&E!Q$K A "&&j  'zG @@s   G8.G=rM   c                 \    |t            }|t           }|dk(  r|dk(  ryt        d      ||z  S )Nr|   r   r}    )r   r   
ValueError)r   rM   	numeratordenominators       r   r]   z$SegmentationPrecision.compute_metricc  s=    :&	]+"A~ n${**r   )r|   )r_   r`   ra   rb   rd   rU   rW   r   r   r   r   r   r[   rc   r]   re   rf   s   @r   r   r      s    4   + +#
@&+J,@&A@',Z-A'B@ )0@D+W + +r   r   c                   T     e Zd ZdZed        Zdeeef   deeef   de	f fdZ
 xZS )SegmentationRecalla  Segmentation recall

    >>> from pyannote.core import Timeline, Segment
    >>> from pyannote.metrics.segmentation import SegmentationRecall
    >>> recall = SegmentationRecall()

    >>> reference = Timeline()
    >>> reference.add(Segment(0, 1))
    >>> reference.add(Segment(1, 2))
    >>> reference.add(Segment(2, 4))

    >>> hypothesis = Timeline()
    >>> hypothesis.add(Segment(0, 1))
    >>> hypothesis.add(Segment(1, 2))
    >>> hypothesis.add(Segment(2, 3))
    >>> hypothesis.add(Segment(3, 4))
    >>> recall(reference, hypothesis)
    1.0

    >>> hypothesis = Timeline()
    >>> hypothesis.add(Segment(0, 4))
    >>> recall(reference, hypothesis)
    0.0

    c                     t         S rQ   )RECALL_NAMErS   s    r   rU   zSegmentationRecall.metric_name  rk   r   r1   r2   r!   c                 ,    t         t        |   ||      S rQ   )r   r   r[   )r   r1   r2   r   r   s       r   r[   z%SegmentationRecall.compute_components  s     'A	# 	#r   )r_   r`   ra   rb   rd   rU   r   r   r   r   r[   re   rf   s   @r   r   r   q  sQ    4  #E*h2F,G #',Z-A'B#(/# #r   r   )&typingr   r   r   numpyrF   pyannote.corer   r   r   pyannote.core.utils.generatorsr	   baser   r   typesr   r   utilsr   rj   rR   r   rI   rK   ru   rv   rs   rt   r   r   r   r   r   rh   ro   r   r   r   r   r   <module>r      s   > * )  7 7 3 ' , " $'8  ' 	'	 	'	)#& 
W=: W=t4- 4.I<)= I<Xu+OZ u+p##. ##r   