Ë
      çiÜ9  ã                   ó  — d Z ddlZddlZddlZddlZddlmZmZmZm	Z	m
Z
mZmZ 	 ddlmZ ed   ZddddœZed	   Ze	d
ge
f   Zeeef   Z G d„ d
ej*                  j,                  «      Z G d„ d«      Zy# e$ r	 ddlmZ Y ŒVw xY w)z 
#########
Protocols
#########

é    N)ÚUnionÚDictÚIteratorÚCallableÚAnyÚTextÚOptional)ÚLiteral)ÚtrainÚdevelopmentÚtestÚtrnÚdevÚtst)ÚfileÚdatabaseÚglobalÚProtocolFilec                   ón   — e Zd ZdZddeed f   defd„Zd„ Zd„ Zd„ Z	d	„ Z
d
„ Zd„ Zd„ Zd„ Zded    fd„Zy)r   a  Protocol file with lazy preprocessors

    This is a dict-like data structure where some values may depend on other
    values, and are only computed if/when requested. Once computed, they are
    cached and never recomputed again.

    Parameters
    ----------
    precomputed : dict
        Regular dictionary with precomputed values
    lazy : dict, optional
        Dictionary describing how lazy value needs to be computed.
        Values are callable expecting a dictionary as input and returning the
        computed value.

    NÚprecomputedÚlazyc                 óÌ  — |€
t        «       }t        |t        «      rvt        |«      | _        t        |j                  «      t        |«      z  D ]  }||   | j                  |<   Œ t        |j                  «      }|j                  |«       || _        n t        |«      | _        t        |«      | _        t        j                  «       | _
        t        j                  «       | _        y ©N)ÚdictÚ
isinstancer   ÚabsÚ_storeÚsetr   ÚupdateÚ	threadingÚRLockÚlock_ÚcollectionsÚCounterÚevaluating_)Úselfr   r   ÚkeyÚcompound_lazys        úx/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pyannote/database/protocol/protocol.pyÚ__init__zProtocolFile.__init__I   s¼   € àˆ<Ü“6ˆDä�k¤<Ô0ô !$ KÓ 0ˆDŒKô
 ˜;×+Ñ+Ó,¬s°4«yÔ8�Ø#.¨sÑ#3�—‘˜CÒ ð 9ô ! ×!1Ñ!1Ó2ˆMØ× Ñ  Ô&Ø+ˆD�Iô
 ˜{Ó+ˆDŒKÜ˜T›
ˆDŒIô —_‘_Ó&ˆŒ
ô '×.Ñ.Ó0ˆÕó    c                 ó6   — t        | j                  «      }|d= |S )Nr"   )r   Ú__dict__©r&   Úds     r)   Ú__getstate__zProtocolFile.__getstate__o   s   € Ü�—‘ÓˆØˆgˆJØˆr+   c                 ól   — | j                   j                  |«       t        j                  «       | _        y r   )r-   r   r    r!   r"   r.   s     r)   Ú__setstate__zProtocolFile.__setstate__u   s"   € Ø�‰×Ñ˜QÔÜ—_‘_Ó&ˆ�
r+   c                 óp   — | j                   5  t        | j                  «      cd d d «       S # 1 sw Y   y xY wr   )r"   r   r   ©r&   s    r)   Ú__abs__zProtocolFile.__abs__y   s   € Ø�Z‹ZÜ˜Ÿ™Ó$÷ �ZŠZús   �,¬5c                 ó  — | j                   5  || j                  v rÂ| j                  |   dk(  r°| j                  j                  |g«        | j                  |   | «      }| j                  |= || j                  v r9|| j                  |   k7  r'd}t        j                  |j                  |¬«      «       || j                  |<   | j                  j                  |g«       | j                  |   cd d d «       S # 1 sw Y   y xY w)Nr   zEExisting precomputed key "{key}" has been modified by a preprocessor.)r'   )	r"   r   r%   r   r   ÚwarningsÚwarnÚformatÚsubtract)r&   r'   ÚvalueÚmsgs       r)   Ú__getitem__zProtocolFile.__getitem__}   sØ   € Ø�Z‹Zà�d—i‘iÑ D×$4Ñ$4°SÑ$9¸QÒ$>ð × Ñ ×'Ñ'¨¨Ô.ð '˜Ÿ	™	 #™ tÓ,�Ø—I‘I˜c�Nð ˜$Ÿ+™+Ñ%¨%°4·;±;¸sÑ3CÒ*CØa�CÜ—M‘M #§*¡*° *Ó"5Ô6ð $)�—‘˜CÑ ð × Ñ ×)Ñ)¨3¨%Ô0à—;‘;˜sÑ#÷/ �ZŠZús   �CC6Ã6C?c                 óš   — | j                   5  || j                  v r| j                  |= || j                  |<   d d d «       y # 1 sw Y   y xY wr   ©r"   r   r   )r&   r'   r;   s      r)   Ú__setitem__zProtocolFile.__setitem__—   s8   € Ø�Z‹Zà�d—i‘iÑØ—I‘I˜c�Nà$ˆD�K‰K˜Ñ÷ �Z‰Zús   �+AÁA
c                 ó–   — | j                   5  || j                  v r| j                  |= | j                  |= d d d «       y # 1 sw Y   y xY wr   r?   )r&   r'   s     r)   Ú__delitem__zProtocolFile.__delitem__Ÿ   s6   € Ø�Z‹Zà�d—i‘iÑØ—I‘I˜c�Nà—‘˜CÐ ÷ �Z‰Zús	   �)?¿Ac              #   óî   K  — | j                   5  t        | j                  «      }|D ]  }|–— Œ t        | j                  «      }|D ]  }|| j                  v rŒ|–— Œ 	 d d d «       y # 1 sw Y   y xY w­wr   )r"   Úlistr   r   )r&   Ú
store_keysr'   Ú	lazy_keyss       r)   Ú__iter__zProtocolFile.__iter__§   s`   è ø€ Ø�Z‹Zä˜dŸk™kÓ*ˆJÛ!�Ø“	ð "ô ˜TŸY™Y›ˆIÛ �Ø˜$Ÿ+™+Ñ%ØØ“	ñ !÷ �Z‰Züs   ‚A5�AA)Á 	A5Á)A2Á.A5c                 ó®   — | j                   5  t        t        | j                  «      t        | j                  «      z  «      cd d d «       S # 1 sw Y   y xY wr   )r"   Úlenr   r   r   r4   s    r)   Ú__len__zProtocolFile.__len__´   s1   € Ø�Z‹ZÜ”s˜4Ÿ;™;Ó'¬#¨d¯i©i«.Ñ8Ó9÷ �ZŠZús   �4AÁAÚreturnc           	   #   ó&  K  — | d   }t        |t        «      s| –— yt        |«      }d|i}t        | «      j	                  «       D ]g  \  }}|dk(  rŒt        |t        «      st        j                  |«      ||<   Œ5t        |«      |k7  r d|› d|› dt        |«      › d�}t        |«      ‚|||<   Œi t        |j                  «       «      }t        |j                  «       Ž D ]/  }t        t        ||«      «      }	t        |	| j                  «      –— Œ1 y­w)a>  Iterate over all files

        When `current_file` refers to only one file,
            yield it and return.
        When `current_file` refers to a list of file (i.e. 'uri' is a list),
            yield each file separately.

        Examples
        --------
        >>> current_file = ProtocolFile({
        ...     'uri': 'my_uri',
        ...     'database': 'my_database'})
        >>> for file in current_file.files():
        ...     print(file['uri'], file['database'])
        my_uri my_database

        >>> current_file = {
        ...     'uri': ['my_uri1', 'my_uri2', 'my_uri3'],
        ...     'database': 'my_database'}
        >>> for file in current_file.files():
        ...     print(file['uri'], file['database'])
        my_uri1 my_database
        my_uri2 my_database
        my_uri3 my_database

        ÚuriNz#Mismatch between number of "uris" (z) and number of "z" (z).)r   rD   rI   r   ÚitemsÚ	itertoolsÚrepeatÚ
ValueErrorÚkeysÚzipÚvaluesr   r   r   )
r&   ÚurisÚn_urisr   r'   r;   r<   rR   rT   Úprecomputed_ones
             r)   ÚfileszProtocolFile.files¸   s  è ø€ ð8 �E‰{ˆÜ˜$¤Ô%ØŠJØä�T“ˆð ˜d�mˆÜ˜d›)Ÿ/™/Ö+‰JˆC�à�eŠ|Øä˜e¤TÔ*Ü#,×#3Ñ#3°EÓ#:�˜CÒ ô �u“: Ò'à=¸f¸Xð F*Ø*-¨¨c´#°e³*°¸RðAð ô % S›/Ð)Ø#(�˜CÒ ð ,ô" �K×$Ñ$Ó&Ó'ˆÜ˜;×-Ñ-Ó/Ó0ˆFÜ"¤3 t¨VÓ#4Ó5ˆOÜ˜°·	±	Ó:Ó:ñ 1ùs   ‚DDr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r*   r0   r2   r5   r=   r@   rB   rG   rJ   r   rX   © r+   r)   r   r   7   s^   „ ññ"#1 E¨$°Ð*>Ñ$?ð #1Àtó #1òLò'ò%ò$ò4%ò!òò:ð:;�x Ñ/ô :;r+   c                   ó  ‡ — e Zd ZdZddee   fˆ fd„Zdeee	f   de	fd„Z
d„ Zdeeee	f      fd„Zdeeee	f      fd	„Zdeeee	f      fd
„Zdedee	   fd„Zdee	   fd„Zdee	   fd„Zdee	   fd„Zdee	   fd„Zˆ xZS )ÚProtocolaæ	  Experimental protocol

    An experimental protocol usually defines three subsets: a training subset,
    a development subset, and a test subset.

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

        >>> class MyProtocol(Protocol):
        ...     def train_iter(self) -> Iterator[Dict]:
        ...         yield {"uri": "filename1", "any_other_key": "..."}
        ...         yield {"uri": "filename2", "any_other_key": "..."}

    `{subset}_iter` should return an iterator of dictionnaries with
        - "uri" key (mandatory) that provides a unique file identifier (usually
          the filename),
        - any other key that the protocol may provide.

    It can then be used in Python like this:

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

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

    ~~~ Content of ~/database.yml ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Protocols:
      MyDatabase:
        Protocol:
          MyProtocol:
            train:
                uri: /path/to/collection.lst
                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
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    It can then be used in Python like this:

        >>> from pyannote.database import registry
        >>> protocol = registry.get_protocol('MyDatabase.Protocol.MyProtocol')
        >>> for file in protocol.train():
        ...    print(file["uri"])
        filename1
        filename2

    This class is usually inherited from, but can be used directly.

    Parameters
    ----------
    preprocessors : dict
        Preprocess protocol files so that `file[key] = preprocessors[key](file)`
        for each key in `preprocessors`. In case `preprocessors[key]` is not
        callable, it should be a string containing placeholders for `file` keys
        (e.g. {'audio': '/path/to/{uri}.wav'})
    Úpreprocessorsc                 ó@  •‡— t         ‰| �  «        |€
t        «       }t        «       | _        |j	                  «       D ]`  \  }}t        |«      r|| j                  |<   Œ!t        |t        «      r t        |«      Šˆfd„}|| j                  |<   ŒQd|› d�}t        |«      ‚ y )Nc                 ó(   •—  ‰j                   di | ¤ŽS )Nr]   )r9   )Úcurrent_fileÚpreprocessor_copys    €r)   ÚfunczProtocol.__init__.<locals>.funcK  s   ø€ Ø3Ð,×3Ñ3ÑC°lÑCÐCr+   Ú"z2" preprocessor is neither a callable nor a string.)	Úsuperr*   r   r`   rN   Úcallabler   ÚstrrQ   )r&   r`   r'   Úpreprocessorre   r<   rd   Ú	__class__s         @€r)   r*   zProtocol.__init__:  sŸ   ù€ Ü‰ÑÔàÐ Ü ›FˆMä!›VˆÔØ!.×!4Ñ!4Ö!6ÑˆC�ä˜Ô%Ø*6�×"Ñ" 3Ò'ô ˜L¬#Ô.Ü$'¨Ó$5Ð!ôDð +/�×"Ñ" 3Ò'ð ˜#˜ÐPÐQ�Ü  “oÐ%ñ# "7r+   rc   rK   c                 ó0   — t        || j                  ¬«      S )N)r   )r   r`   )r&   rc   s     r)   Ú
preprocesszProtocol.preprocessT  s   € Ü˜L¨t×/AÑ/AÔBÐBr+   c                 ó   — | j                   S r   )r\   r4   s    r)   Ú__str__zProtocol.__str__W  s   € Ø�|‰|Ðr+   c                 ó   — t        «       ‚)z)Iterate over files in the training subset©ÚNotImplementedErrorr4   s    r)   Ú
train_iterzProtocol.train_iterZ  ó   € ä!Ó#Ð#r+   c                 ó   — t        «       ‚)z,Iterate over files in the development subsetrq   r4   s    r)   Údevelopment_iterzProtocol.development_iter^  rt   r+   c                 ó   — t        «       ‚)z%Iterate over files in the test subsetrq   r4   s    r)   Ú	test_iterzProtocol.test_iterb  rt   r+   Úsubsetc              #   ó  K  — 	  t        | |› d�«      «       }|D ]  }| j	                  |«      –— Œ y # t        t        f$ r@ t        |   }	  t        | |› d�«      «       }n# t        $ r d|› d�}t        |«      ‚w xY wY Œiw xY w­w)NÚ_iterzProtocol does not implement a z subset.)ÚgetattrÚAttributeErrorrr   ÚLEGACY_SUBSET_MAPPINGrm   )r&   ry   rX   Úsubset_legacyr<   r   s         r)   Úsubset_helperzProtocol.subset_helperf  sª   è ø€ ð	/Ø3”G˜D V H¨EÐ"2Ó3Ó5ˆEó ˆDØ—/‘/ $Ó'Ó'ñ øô Ô 3Ð4ò 
	/ô
 2°&Ñ9ˆMð/Ø>œ ¨¨°uÐ&=Ó>Ó@‘øÜ!ò /Ø6°v°h¸hÐG�Ü)¨#Ó.Ð.ð/úñ ð
	/üs=   ‚B„3 ˜B³BÁA!Á BÁ!A<Á<BÁ?BÂBÂBc                 ó$   — | j                  d«      S )Nr   ©r€   r4   s    r)   r   zProtocol.trainy  s   € Ø×!Ñ! 'Ó*Ð*r+   c                 ó$   — | j                  d«      S )Nr   r‚   r4   s    r)   r   zProtocol.development|  s   € Ø×!Ñ! -Ó0Ð0r+   c                 ó$   — | j                  d«      S )Nr   r‚   r4   s    r)   r   zProtocol.test  s   € Ø×!Ñ! &Ó)Ð)r+   c              #   óô   ‡ ‡K  — ddl m} t        «       }dD ]]  Št        ‰ ‰«      sŒˆˆ fd„} |«       D ]>  }d|vrŒ|j	                  «       D ]$  } ||«      }||v rŒ|–— |j                  |«       Œ& Œ@ Œ_ y­w)z$Iterate over all files in `protocol`r   )Úget_unique_identifier)	r   Údevelopment_enrolmentÚdevelopment_trialr   Útest_enrolmentÚ
test_trialr   Útrain_enrolmentÚtrain_trialc               3   ón   •K  — 	  t        ‰‰«      «       D ]  } | –— Œ y # t        t        f$ r Y y w xY w­wr   )r|   r}   rr   )r   Úmethodr&   s    €€r)   ÚiteratezProtocol.files.<locals>.iterate™  s>   øè ø€ ðØ 5¤¨¨fÓ 5Ö 7˜Ø"›
ñ !8øä&Ô(;Ð<ò Ùðüs   ƒ5…  Ÿ5 2¯5±2²5rM   N)Úpyannote.database.utilr†   r   ÚhasattrrX   Úadd)r&   r†   Úyielded_urisr�   rc   Úcurrent_file_rM   rŽ   s   `      @r)   rX   zProtocol.files‚  s�   ùè ø€ õ 	Aä“uˆó

ˆFô ˜4 Ô(Øõñ !(¦	�ð  Ñ,Øà%1×%7Ñ%7Ö%9�Mñ 0°Ó>�CØ˜lÑ*Ø à'Ò'à ×$Ñ$ SÕ)ñ &:ñ !*ñ-

ùs   „A4A8r   )rY   rZ   r[   r\   r	   ÚPreprocessorsr*   r   r   r   rm   ro   r   rs   rv   rx   ÚSubsetr€   r   r   r   rX   Ú__classcell__)rk   s   @r)   r_   r_   õ   sí   ø„ ñBñH& h¨}Ñ&=õ &ð4C u¨T°<Ð-?Ñ'@ð CÀ\ó Còð$˜H U¨4°Ð+=Ñ%>Ñ?ó $ð$ (¨5°°|Ð1CÑ+DÑ"Eó $ð$˜8 E¨$°Ð*<Ñ$=Ñ>ó $ð( Fð (¨x¸Ñ/Eó (ð&+�x Ñ-ó +ð1˜X lÑ3ó 1ð*�h˜|Ñ,ó *ð0*�x Ñ-÷ 0*r+   r_   )r\   r7   r#   r    rO   Útypingr   r   r   r   r   r   r	   r
   ÚImportErrorÚtyping_extensionsr–   r~   ÚScopeÚPreprocessorr•   ÚabcÚMutableMappingr   r_   r]   r+   r)   Ú<module>rŸ      s©   ðñ:ó Û Û Û ß G× GÑ Gð*Ýð 
Ð/Ñ	0€Ø"'¸ÀuÑMÐ ØÐ,Ñ-€à˜Ð(¨#Ð-Ñ.€Ø�T˜<Ð'Ñ(€ô{;�;—?‘?×1Ñ1ô {;÷|}*ò }*øðS ò *ß)ð*ús   ¦A5 Á5BÂB