o
    pi#!                     @   sB   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 dZ	dS )    N)OptionalTupleUnion)TimelineSegment
Annotationc                   @   s   e Zd ZdZ		ddededededef
d	d
ZdededefddZ	dededefd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dZdS )UEMSupportMixinu:   Provides 'uemify' method with optional (à la NIST) collar        Fuem	referencecollarskip_overlapreturnc                 C   s   |dkr|s|S g g }}|dkr>|  D ](}|j}|t|d|  |d|   |j}|t|d|  |d|   q|r^||D ]\\}	}
\}}|	|krV|
|krVqE||	|@  qE|| }t|d j|d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
        r	   g      ?)segments)support)	itersegmentsstartappendr   endco_iterr   r   gaps)selfr
   r   r   r   collarsoverlap_regionssegmenttsegment1track1segment2track2r    r    J/home/ubuntu/.local/lib/python3.10/site-packages/pyannote/metrics/utils.pyextrude&   s    
 "zUEMSupportMixin.extrude
hypothesisc                 C   s&   |j dd}||j dd | 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#   timeliner    r    r!   common_timelineb   s   zUEMSupportMixin.common_timeline
annotationr)   c           	      C   s^   |  }|jdd}||D ]\}}||D ]}|j||d}|||f |||f< qq|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_trackr    r    r!   projectw   s   zUEMSupportMixin.projectNreturns_uemreturns_timelinec                 C   s   |du r%|   }|   }	||	B }
t|
r|
gng |jd}td | j||||d}|j|dd}|j|dd}|rP| ||}| 	||}| 	||}||f}|r[||f7 }|rb||f7 }|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)
        N)r   urizL'uem' was approximated by the union of 'reference' and 'hypothesis' extents.)r   r   intersection)mode)
r&   extentr   r8   warningswarnr"   cropr*   r5   )r   r   r#   r
   r   r   r6   r7   r_extenth_extentr;   r)   resultr    r    r!   uemify   s2   /


zUEMSupportMixin.uemify)r	   F)Nr	   FFF)__name__
__module____qualname____doc__r   r   floatboolr"   r*   r5   r   r   r   rB   r    r    r    r!   r   #   s\    

<

r   )
r<   typingr   r   r   pyannote.corer   r   r   r   r    r    r    r!   <module>   s   