Ë
      çi…(  ã                   óÎ   — d dl 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dlmZ d dl	m
Z
 d d	l	mZ d d
l	mZ d dlZ	 ddedee   defd„Z	 ddedee   de
fd„Z G d„ de«      Zy)é    )ÚDictÚOptionalé   )ÚProtocol)ÚProtocolFile)ÚSubset)ÚPreprocessor)ÚPreprocessors)Ú
Annotation)ÚTimeline)ÚSegmentNÚcurrent_fileÚexisting_preprocessorÚreturnc                 óØ   — |€| j                  dd«      }n || «      }|€y| j                  dd«      }|€|S t        d|«      }|r%|j                  «       |vr|j                  |d¬«      S |S )a  Preprocessor that crops 'annotated' according to 'duration'

    Returns 'annotated' unchanged if 'duration' is not available

    Parameters
    ----------
    current_file : ProtocolFile
        Protocol file.
    existing_preprocessor : Preprocessor, optional
        When provided, this preprocessor must be used to get the initial
        'annotated' instead of getting it from 'current_file["annotated"]'

    Returns
    -------
    cropped_annotated : Timeline
        "annotated" cropped by "duration".
    NÚ	annotatedÚdurationç        Úintersection©Úmode)Úgetr   ÚextentÚcrop)r   r   r   r   s       ú|/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pyannote/database/protocol/segmentation.pyÚcrop_annotatedr   *   s‡   € ð* Ð$Ø ×$Ñ$ [°$Ó7‰	á)¨,Ó7ˆ	àÐØà×Ñ 
¨DÓ1€HØÐØÐô �s˜HÓ%€Há˜×)Ñ)Ó+¨xÑ7Ø�~‰~˜h¨^ˆ~Ó<Ð<àÐó    c                 óÚ   — |€| j                  dd«      }n || «      }|€y| j                  dd«      }|€|S |r2|j                  |j                  «       «      s|j                  |d¬«      S |S )a  Preprocessor that crops 'annotation' by 'annotated'

    Returns 'annotation' unchanged if 'annotated' is not available

    Parameters
    ----------
    current_file : ProtocolFile
        Protocol file.
    existing_preprocessor : Preprocessor, optional
        When provided, this preprocessor must be used to get the initial
        'annotation' instead of getting it from 'current_file["annotation"]'

    Returns
    -------
    cropped_annotation : Annotation
        "annotation" cropped by "annotated".
    NÚ
annotationr   r   r   )r   ÚcoversÚget_timeliner   )r   r   r   r   s       r   Úcrop_annotationr"   T   s�   € ð* Ð$Ø!×%Ñ% l°DÓ9‰
á*¨<Ó8ˆ
àÐØà× Ñ  ¨dÓ3€IØÐØÐñ ˜×)Ñ)¨*×*AÑ*AÓ*CÔDØ�‰˜y¨~ˆÓ>Ð>àÐr   c                   óB   ‡ — e Zd ZdZddee   fˆ fd„Zddedefd„Z	ˆ xZ
S )	ÚSegmentationProtocolaí  A protocol for segmentation experiments

    A segmentation protocol can be defined programmatically by creating
    a class that inherits from SegmentationProtocol and implements at
    least one of `train_iter`, `development_iter` and `test_iter` methods:

        >>> class MySegmentationProtocol(SegmentationProtocol):
        ...     def train_iter(self) -> Iterator[Dict]:
        ...         yield {"uri": "filename1",
        ...                "annotation": Annotation(...),
        ...                "annotated": Timeline(...)}
        ...         yield {"uri": "filename2",
        ...                "annotation": Annotation(...),
        ...                "annotated": Timeline(...)}

    `{subset}_iter` should return an iterator of dictionnaries with
        - "uri" key (mandatory) that provides a unique file identifier (usually
          the filename),
        - "annotation" key (mandatory for train and development subsets) that
          provides reference segmentation as a `pyannote.core.Annotation`
          instance,
        - "annotated" key (recommended) that describes which part of the file
          has been annotated, as a `pyannote.core.Timeline` instance. Any part
          of "annotation" that lives outside of the provided "annotated" will
          be removed. This is also used by `pyannote.metrics` to remove
          un-annotated regions from its evaluation report, and by
          `pyannote.audio` to not consider empty un-annotated regions as
          non-speech.
        - any other key that the protocol may provide.

    It can then be used in Python like this:

        >>> protocol = MySegmentationProtocol()
        >>> for file in protocol.train():
        ...    print(file["uri"])
        filename1
        filename2

    A segmentation protocol can also be defined using `pyannote.database`
    configuration file, whose (configurable) path defaults to "~/database.yml".

    ~~~ Content of ~/database.yml ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Protocols:
      MyDatabase:
        Segmentation:
          MyProtocol:
            train:
                uri: /path/to/collection.lst
                annotation: /path/to/reference.rttm
                annotated: /path/to/reference.uem
                any_other_key: ... # see custom loader documentation
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    where "/path/to/collection.lst" contains the list of identifiers of the
    files in the collection:

    ~~~ Content of "/path/to/collection.lst ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    filename1
    filename2
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    "/path/to/reference.rttm" contains the reference segmentation using
    RTTM format:

    ~~~ Content of "/path/to/reference.rttm ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    SPEAKER filename1 1 3.168 0.800 <NA> <NA> music <NA> <NA>
    SPEAKER filename1 1 5.463 0.640 <NA> <NA> music <NA> <NA>
    SPEAKER filename1 1 5.496 0.574 <NA> <NA> speech <NA> <NA>
    SPEAKER filename1 1 10.454 0.499 <NA> <NA> speech <NA> <NA>
    SPEAKER filename2 1 2.977 0.391 <NA> <NA> noise <NA> <NA>
    SPEAKER filename2 1 18.705 0.964 <NA> <NA> noise <NA> <NA>
    SPEAKER filename2 1 22.269 0.457 <NA> <NA> music <NA> <NA>
    SPEAKER filename2 1 28.474 1.526 <NA> <NA> music <NA> <NA>
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    "/path/to/reference.uem" describes the annotated regions using UEM format:

    ~~~ Content of "/path/to/reference.uem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    filename1 NA 0.000 30.000
    filename2 NA 0.000 30.000
    filename2 NA 40.000 70.000
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    It can then be used in Python like this:

        >>> from pyannote.database import registry
        >>> protocol = registry.get_protocol('MyDatabase.SpeakerDiarization.MyProtocol')
        >>> for file in protocol.train():
        ...    print(file["uri"])
        filename1
        filename2
    Úpreprocessorsc                 óö   •— |€
t        «       }t        j                  t        |j	                  dd «      ¬«      |d<   t        j                  t
        |j	                  dd «      ¬«      |d<   t        ‰| �  |¬«       y )Nr   )r   r   )r%   )ÚdictÚ	functoolsÚpartialr   r   r"   ÚsuperÚ__init__)Úselfr%   Ú	__class__s     €r   r+   zSegmentationProtocol.__init__Ú   sv   ø€ àÐ Ü ›FˆMô &/×%6Ñ%6Ü°-×2CÑ2CÀKÐQUÓ2Vô&
ˆ�kÑ"ô '0×&7Ñ&7Ü°=×3DÑ3DÀ\ÐSWÓ3Xô'
ˆ�lÑ#ô 	‰Ñ }ÐÕ5r   Úsubsetr   c                 óD  — ddl m} d}d}d}i } t        | |«      «       D ]v  } ||«      }||j                  «       z  }|d   }	||	j	                  «       j                  «       z  }|	j                  «       D ]  \  }
}|
|vrd||
<   ||
xx   |z  cc<   Œ |dz  }Œx ||||dœ}|S )ab  Obtain global statistics on a given subset

        Parameters
        ----------
        subset : {'train', 'development', 'test'}

        Returns
        -------
        stats : dict
            Dictionary with the followings keys:
            * annotated: float
            total duration (in seconds) of the parts that were manually annotated
            * annotation: float
            total duration (in seconds) of actual annotations
            * n_files: int
            number of files in the subset
            * labels: dict
            maps classes with their total duration (in seconds)
        é   )Úget_annotatedr   r   r   r   )r   r   Ún_filesÚlabels)Úutilr1   Úgetattrr   r!   Úchart)r,   r.   r1   Úannotated_durationÚannotation_durationr2   r3   Úitemr   r   Úlabelr   Ústatss                r   r;   zSegmentationProtocol.statsí   s×   € õ* 	)à ÐØ!ÐØˆØˆà)”G˜D &Ó)Ö+ˆDá% dÓ+ˆIØ )×"4Ñ"4Ó"6Ñ6Ðð ˜lÑ+ˆJØ :×#:Ñ#:Ó#<×#EÑ#EÓ#GÑGÐà#-×#3Ñ#3Ö#5‘��xØ Ñ&Ø$'�F˜5‘MØ�u“ Ñ)”ð $6ð �q‰L‰Gð ,ð  ,Ø-ØØñ	
ˆð ˆr   ©N)Útrain)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   r+   r   r   r;   Ú__classcell__)r-   s   @r   r$   r$   |   s.   ø„ ñ[ñz6 h¨}Ñ&=õ 6ñ&2˜Fð 2°÷ 2r   r$   r<   )Útypingr   r   Úprotocolr   r   r   r	   r
   Úpyannote.corer   r   r   r(   r   r"   r$   © r   r   Ú<module>rG      s‡   ð÷< "Ý Ý "Ý Ý "Ý #Ý $Ý "Ý !Û ð QUñ'Øð'Ø7?ÀÑ7Mð'àó'ðV QUñ%Øð%Ø7?ÀÑ7Mð%àó%ôPc˜8õ cr   