
      i#!                     H    d dl Z d dlmZmZmZ d dlmZmZmZ  G d d      Z	y)    N)OptionalTupleUnion)TimelineSegment
Annotationc                       e Zd ZdZ	 	 ddededededef
dZdededefd	Z	d
ededefdZ
	 	 	 	 	 ddededee   dededededeeeef   eeeef   eeeeef   f   fdZy)UEMSupportMixinu:   Provides 'uemify' method with optional (à la NIST) collaruem	referencecollarskip_overlapreturnc           	         |dk(  r|s|S g g }}|dkD  r{|j                         D ]h  }|j                  }|j                  t        |d|z  z
  |d|z  z                |j                  }|j                  t        |d|z  z
  |d|z  z                j |r>|j                  |      D ]*  \  \  }	}
\  }}|	|k(  r|
|k(  r|j                  |	|z         , ||z   }t        |      j                         j                  |      S )a  Extrude reference boundary collars from uem

        reference     |----|     |--------------|       |-------------|
        uem       |---------------------|    |-------------------------------|
        extruded  |--| |--| |---| |-----|    |-| |-----| |-----------| |-----|

        Parameters
        ----------
        uem : Timeline
            Evaluation map.
        reference : Annotation
            Reference annotation.
        collar : float, optional
            When provided, set the duration of collars centered around
            reference segment boundaries that are extruded from both reference
            and hypothesis. Defaults to 0. (i.e. no collar).
        skip_overlap : bool, optional
            Set to True to not evaluate overlap regions.
            Defaults to False (i.e. keep overlap regions).

        Returns
        -------
        extruded_uem : Timeline
                g      ?)segments)support)	itersegmentsstartappendr   endco_iterr   r   gaps)selfr   r   r   r   collarsoverlap_regionssegmenttsegment1track1segment2track2r   s                 k/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pyannote/metrics/utils.pyextrudezUEMSupportMixin.extrude&   s   < R<J#%r B;$113MMwq2;BKHI KKwq2;BKHI 4 :C:K:KI:V6"6$6Xvx'Ff,<&&x(':;	 ;W _,*22499#9FF    
hypothesisc                     |j                  d      }|j                  |j                  d             |j                         S )a  Return timeline common to both reference and hypothesis

        reference   |--------|    |------------|     |---------|         |----|
        hypothesis     |--------------| |------|   |----------------|
        timeline    |--|-----|----|---|-|------|   |-|---------|----|    |----|

        Parameters
        ----------
        reference : Annotation
        hypothesis : Annotation

        Returns
        -------
        timeline : Timeline
        TcopyF)get_timelineupdatesegmentation)r   r   r&   timelines       r#   common_timelinezUEMSupportMixin.common_timelineb   s@    " ))t)4
//U/;<$$&&r%   
annotationr-   c                     |j                         }|j                  d      }|j                  |      D ]:  \  }}|j                  |      D ]!  }|j	                  ||      }|||f   |||f<   # < |S )a  Project annotation onto timeline segments

        reference     |__A__|     |__B__|
                        |____C____|

        timeline    |---|---|---|   |---|

        projection  |_A_|_A_|_C_|   |_B_|
                        |_C_|

        Parameters
        ----------
        annotation : Annotation
        timeline : Timeline

        Returns
        -------
        projection : Annotation
        Fr(   )	candidate)emptyr*   r   
get_tracks	new_track)	r   r/   r-   
projection	timeline_segment_r   track_tracks	            r#   projectzUEMSupportMixin.projectw   s    (  %%'
+++7	!*!2!28!<Hg$//9",,W,G-7&8H-I
7E>* : "= r%   Nreturns_uemreturns_timelinec                    |r|j                         j                         }|j                         j                         }	||	z  }
t        |
r|
gng |j                        }t	        j
                  d       | j                  ||||      }|j                  |d      }|j                  |d      }|r6| j                  ||      }| j                  ||      }| j                  ||      }||f}|r||fz  }|r|fz  }|S )a   Crop 'reference' and 'hypothesis' to 'uem' support

        Parameters
        ----------
        reference, hypothesis : Annotation
            Reference and hypothesis annotations.
        uem : Timeline, optional
            Evaluation map.
        collar : float, optional
            When provided, set the duration of collars centered around
            reference segment boundaries that are extruded from both reference
            and hypothesis. Defaults to 0. (i.e. no collar).
        skip_overlap : bool, optional
            Set to True to not evaluate overlap regions.
            Defaults to False (i.e. keep overlap regions).
        returns_uem : bool, optional
            Set to True to return extruded uem as well.
            Defaults to False (i.e. only return reference and hypothesis)
        returns_timeline : bool, optional
            Set to True to oversegment reference and hypothesis so that they
            share the same internal timeline.

        Returns
        -------
        reference, hypothesis : Annotation
            Extruded reference and hypothesis annotations
        uem : Timeline
            Extruded uem (returned only when 'returns_uem' is True)
        timeline : Timeline:
            Common timeline (returned only when 'returns_timeline' is True)
        )r   urizL'uem' was approximated by the union of 'reference' and 'hypothesis' extents.)r   r   intersection)mode)
r*   extentr   r>   warningswarnr$   cropr.   r:   )r   r   r&   r   r   r   r;   r<   r_extenth_extentrA   r-   results                r#   uemifyzUEMSupportMixin.uemify   s   ^ ; --/668H!..0779H(FVHB(}}.CMM,-
 ll3	&(4  6 NN3^N<	__S~_>
 ++IzBHY9Ij(;JZ(sfFxk!Fr%   )r   F)Nr   FFF)__name__
__module____qualname____doc__r   r   floatboolr$   r.   r:   r   r   r   rH    r%   r#   r
   r
   #   s$   D
 !$%*	:G:G%:G :G #	:G 08	:Gx' ' ''**  Z > *.!$)#((-N$N%N X&N 	N
 "N !N "&N j*,-j*h67j*h@ACNr%   r
   )
rB   typingr   r   r   pyannote.corer   r   r   r
   rO   r%   r#   <module>rR      s"   :  ) ) 7 7~ ~r%   