Ë
    ÿÍ:j'G  ã                   óÂ   — d Z ddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
m
Z
 ddl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  G d„ d«      Zd„ Zy)ah  
Pipeline

Usage:
  pyannote-pipeline train [options] [(--forever | --iterations=<iterations>)] <experiment_dir> <database.task.protocol>
  pyannote-pipeline best [options] <experiment_dir> <database.task.protocol>
  pyannote-pipeline apply [options] <train_dir> <database.task.protocol>
  pyannote-pipeline -h | --help
  pyannote-pipeline --version

Common options:
  <database.task.protocol>   Experimental protocol (e.g. "Etape.SpeakerDiarization.TV")
  --registry=<db.yml>        Path to, comma-separated, database configuration files.
                             [default: ~/.pyannote/db.yml]
  --subset=<subset>          Set subset. Defaults to 'development' in "train"
                             mode, and to 'test' in "apply" mode.

"train" mode:
  <experiment_dir>           Set experiment root directory. This script expects
                             a configuration file called "config.yml" to live
                             in this directory. See "Configuration file"
                             section below for more details.
  --iterations=<iterations>  Number of iterations. [default: 1]
  --forever                  Iterate forever.
  --sampler=<sampler>        Choose sampler between RandomSampler or TPESampler
                             [default: TPESampler].
  --pruner=<pruner>          Choose pruner between MedianPruner or
                             SuccessiveHalvingPruner. Defaults to no pruning.
  --pretrained=<train_dir>   Use parameters in existing training directory to
                             bootstrap the optimization process. In practice,
                             this will simply run a first trial with this set
                             of parameters.
  --average-case             Optimize for average case instead of worst case.

"apply" mode:
  <train_dir>                Path to the directory containing trained hyper-
                             parameters (i.e. the output of "train" mode).

  --use-filter               Apply pipeline only to files that pass the filter.

Configuration file:
    The configuration of each experiment is described in a file called
    <experiment_dir>/config.yml that describes the pipeline.

    ................... <experiment_dir>/config.yml ...................
    pipeline:
       name: Yin2018
       params:
          sad: tutorials/pipeline/sad
          scd: tutorials/pipeline/scd
          emb: tutorials/pipeline/emb
          metric: angular

    # preprocessors can be used to automatically add keys into
    # each (dict) file obtained from pyannote.database protocols.
    preprocessors:
       audio: ~/.pyannote/db.yml   # load template from YAML file
       video: ~/videos/{uri}.mp4   # define template directly

    # filters can be used to filter out some files from the protocol
    # (e.g. to only keep files with a specific number of speakers)
    filters:
        pyannote.audio.utils.protocol.FilterByNumberOfSpeakers:
            num_speakers: 2

    # one can freeze some hyper-parameters if needed (e.g. when
    # only part of the pipeline needs to be updated)
    freeze:
       speech_turn_segmentation:
          speech_activity_detection:
              onset: 0.5
              offset: 0.5

    # pyannote.audio pipelines will run on CPU by default.
    # use `device` key to send it to GPU.
    device: cuda
    ...................................................................

"train" mode:
    Tune the pipeline hyper-parameters
        <experiment_dir>/<database.task.protocol>.<subset>.yml

"best" mode:
    Display current best loss and corresponding hyper-paramters.

"apply" mode
    Apply the pipeline (with best set of hyper-parameters)

é    N)ÚOptional)ÚPath)Údocopt)Útqdm)Údatetime)Ú
FileFinder)Úregistry)Úget_annotated)Úget_class_by_nameé   )Ú	Optimizerc                   óÚ   ‡ — e Zd ZdZdZdZdZeddede	dd fd„«       Z
dd	ede	fˆ fd
„Z	 	 	 	 	 	 ddedee   dee   dedee   dee   de	fd„Zddedefd„Z	 	 ddededee   de	fd„Zˆ xZS )Ú
Experimentz¶Pipeline experiment

    Parameters
    ----------
    experiment_dir : `Path`
        Experiment root directory.
    training : `bool`, optional
        Switch to training mode
    z{experiment_dir}/config.ymlz*{experiment_dir}/train/{protocol}.{subset}z{train_dir}/apply/{date}Ú	train_dirÚtrainingÚreturnc                 óæ   — |j                   d   } | ||¬«      }|dz  }t        j                  t        j                  j                  |«      «      |_        |j                  j                  |«       |S )a6  Load pipeline from train directory

        Parameters
        ----------
        train_dir : `Path`
            Path to train directory
        training : `bool`, optional
            Switch to training mode.

        Returns
        -------
        xp : `Experiment`
            Pipeline experiment.
        r   ©r   ú
params.yml)	Úparentsr   ÚfromtimestampÚosÚpathÚgetmtimeÚmtime_Ú	pipeline_Úload_params)Úclsr   r   Úexperiment_dirÚxpÚ
params_ymls         úq/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/pyannote/pipeline/experiment.pyÚfrom_train_dirzExperiment.from_train_dirš   sb   € ð  #×*Ñ*¨1Ñ-ˆÙ�¨(Ô3ˆØ Ñ-ˆ
Ü×*Ñ*¬2¯7©7×+;Ñ+;¸JÓ+GÓHˆŒ	Ø
�‰× Ñ  Ô,Øˆ	ó    r   c           	      óÌ  •‡— t         ‰| �  «        || _        | j                  j	                  | j                  ¬«      }t        |d«      5 }t        j                  |t        j                  ¬«      | _	        d d d «       i }| j                  j                  di «      j                  «       D ]Q  \  }}t        |t        «      r,t        |d   d¬«      } |di |j                  di «      ¤Ž||<   ŒB	 t        |¬	«      ||<   ŒS || _        g Š| j                  j                  d
i «      j                  «       D ]'  \  }}t        |«      }‰j%                   |di |¤Ž«       Œ) dt&        fˆfd„}|| _        | j                  d   d   }t        |d¬«      } |di | j                  d   j                  di «      ¤Ž| _        d| j                  v r*| j                  d   }| j*                  j-                  |«       d| j                  v r>dd l}|j1                  | j                  d   «      }| j*                  j3                  |«       y y # 1 sw Y   �ŒÈxY w# t         $ r}	|}
|
||<   Y d }	~	�Œµd }	~	ww xY w)N)r   Úr©ÚLoaderÚpreprocessorsÚnamezpyannote.pipeline)Údefault_module_nameÚparams)Údatabase_ymlÚfiltersr   c                 ó.   •‡ — t        ˆ fd„‰D «       «      S )Nc              3   ó.   •K  — | ]  } |‰«      –— Œ y ­w)N© )Ú.0ÚfÚis     €r"   ú	<genexpr>z;Experiment.__init__.<locals>.all_filters.<locals>.<genexpr>ß   s   øè ø€ Ò- ‘q˜—tÑ-ùs   ƒ)Úall)r4   r.   s   `€r"   Úall_filtersz(Experiment.__init__.<locals>.all_filtersÞ   s   ù€ ÜÓ- WÔ-Ó-Ð-r$   Úpipelinezpyannote.pipeline.blocksÚfreezeÚdevicer   r1   )ÚsuperÚ__init__r   Ú
CONFIG_YMLÚformatÚopenÚyamlÚloadÚ
SafeLoaderÚconfig_ÚgetÚitemsÚ
isinstanceÚdictr   r   ÚFileNotFoundErrorÚpreprocessors_ÚappendÚboolÚfilters_r   r9   Útorchr:   Úto)Úselfr   r   Ú
config_ymlÚfpr)   ÚkeyÚpreprocessorÚKlassÚeÚtemplater,   r7   Úpipeline_namerM   r:   r.   Ú	__class__s                   @€r"   r<   zExperiment.__init__±   sK  ù€ Ü‰ÑÔà,ˆÔð —_‘_×+Ñ+¸4×;NÑ;NÐ+ÓOˆ
Ü�*˜cÓ"ð 	A bÜŸ9™9 R´·±Ô@ˆDŒL÷	Að ˆØ!%§¡×!1Ñ!1°/À2Ó!F×!LÑ!LÓ!Nò 	.ÑˆC�ô ˜,¬Ô-Ü)Ø  Ñ(Ð>Qô�ñ &+Ñ%L¨\×-=Ñ-=¸hÈÓ-KÑ%L�˜cÑ"Øð	.ô &0¸\Ô%J�˜cÒ"ð#	.ð2 ,ˆÔð ˆØŸ<™<×+Ñ+¨I°rÓ:×@Ñ@ÓBò 	,‰KˆC�Ü% cÓ*ˆEØ�N‰N™5™? 6™?Õ+ð	,ð	.œdõ 	.ð $ˆŒð Ÿ™ ZÑ0°Ñ8ˆÜ!ØÐ/Iô
ˆñ ÑL §¡¨jÑ!9×!=Ñ!=¸hÈÓ!KÑLˆŒð �t—|‘|Ñ#Ø—\‘\ (Ñ+ˆFØ�N‰N×!Ñ! &Ô)ð �t—|‘|Ñ#Ûà—\‘\ $§,¡,¨xÑ"8Ó9ˆFØ�N‰N×Ñ˜fÕ%ð	 $÷q	Añ 	Aûô0 %ò .ð (�Ø%-�˜c×"ûð	.ús$   Á
+H;Ã,IÈ;IÉ	I#ÉIÉI#Úprotocol_nameÚsubsetÚ
pretrainedÚn_iterationsÚsamplerÚprunerÚaverage_casec           
      óŠ  — t        | j                  j                  | j                  ||¬«      «      }|j	                  dd¬«       t        j                  || j                  ¬«      }	d}
t        | j                  |dz  |
|||¬«      }| j                  j                  «       dk(  rd	nd
}|dz  }t        ddd¬«      }|j                  d«       |j                  d«       |rF|dz  }t        |d¬«      5 }t        j                   |t        j"                  ¬«      }ddd«       d   }nd}t%        t'        | j(                   t+        |	|«      «       «      «      }|j-                  ||d¬«      }	 |j.                  }|dk  rt7        j8                  «       n
t;        |«      }t=        ||«      D ]g  \  }}|d   }||z  ||z  k  r%|d   }|}| j                  j?                  |||¬«       dd|z  d›d�}|j                  |¬«       |j                  d	«       Œi y# 1 sw Y   ŒûxY w# t0        $ r}|t2        j4                  z  }Y d}~ŒÈd}~ww xY w)ay  Train pipeline

        Parameters
        ----------
        protocol_name : `str`
            Name of pyannote.database protocol to use.
        subset : `str`, optional
            Use this subset for training. Defaults to 'development'.
        pretrained : Path, optional
            Use parameters in "pretrained" training directory to bootstrap the
            optimization process. In practice this will simply run a first trial
            with this set of parameters.
        n_iterations : `int`, optional
            Number of iterations. Defaults to 1.
        sampler : `str`, optional
            Choose sampler between RandomSampler and TPESampler
        pruner : `str`, optional
            Choose between MedianPruner or SuccessiveHalvingPruner.
        average_case : `bool`, optional
            Optimise for average case. Defaults to False (i.e. worst case).
        ©r   ÚprotocolrZ   T©r   Úexist_ok©r)   Údefaultútrials.journal)ÚdbÚ
study_namer]   r^   r_   Úminimizer   éÿÿÿÿr   Útrialr   )ÚunitÚpositionÚleavezFirst trial in progressr&   ©Úmoder'   Nr,   )Ú
warm_startÚshow_progressÚloss)r,   rt   zBest trial: éd   Úgú%)Údesc) r   Ú	TRAIN_DIRr>   r   Úmkdirr	   Úget_protocolrI   r   r   Úget_directionr   Úset_descriptionÚupdater?   r@   rA   rB   ÚlistÚfilterrL   ÚgetattrÚ	tune_iterÚ	best_lossÚ
ValueErrorÚnpÚinfÚ	itertoolsÚcountÚrangeÚzipÚdump_params)rO   rY   rZ   r[   r\   r]   r^   r_   r   rb   ri   Ú	optimizerÚ	directionr!   Úprogress_barÚpre_params_ymlrQ   Ú
pre_paramsrr   ÚinputsÚ
iterationsrƒ   rU   rˆ   r4   Ústatusrt   Úbest_paramsrx   s                                r"   ÚtrainzExperiment.trainö   sX  € ô> Ø�N‰N×!Ñ!Ø#×2Ñ2Ø&Øð "ó ó
ˆ	ð 	�‰ ¨tˆÔ4ä×(Ñ(Ø¨×)<Ñ)<ô
ˆð ˆ
ÜØ�N‰NØÐ+Ñ+Ø!ØØØ%ô
ˆ	ð Ÿ™×5Ñ5Ó7¸:ÒE‘AÈ2ˆ	à Ñ-ˆ
ä °1¸DÔAˆØ×$Ñ$Ð%>Ô?Ø×Ñ˜AÔáØ'¨,Ñ6ˆNÜ�n¨3Ô/ð C°2Ü!ŸY™Y r´$·/±/ÔB�
÷Cà# HÑ-‰Jð ˆJä”f˜TŸ]™]Ð,E¬G°H¸fÓ,EÓ,GÓHÓIˆà×(Ñ(Ø˜z¸ð )ó 
ˆ
ð	+Ø!×+Ñ+ˆIð &2°AÒ%5”	—‘Ô!¼5ÀÓ;Nˆä˜U JÓ/ò 	#‰IˆAˆvØ˜&‘>ˆDà˜4Ñ )¨iÑ"7Ò7Ø$ XÑ.�Ø �	Ø—‘×*Ñ*Ø {¸ð +ô ð
 " #¨	¡/°!Ð!4°AÐ6ˆDØ×(Ñ(¨dÐ(Ô3Ø×Ñ Õ"ñ	#÷'Cð Cûô ò 	+Ø!¤B§F¡FÑ*�Iûð	+ús$   Ã0&HÅ)H ÈHÈ	IÈ%H=È=Ic                 ó~  — t        | j                  j                  | j                  ||¬«      «      }d}t	        | j
                  |dz  |¬«      }	 |j                  }|j                  }t        dd|z  d	›d
�«       t        j                  |d¬«      }	t        |	«       y# t        $ r}t        d«       Y d}~yd}~ww xY w)a  Print current best pipeline

        Parameters
        ----------
        protocol_name : `str`
            Name of pyannote.database protocol used for training.
        subset : `str`, optional
            Subset used for training. Defaults to 'development'.
        ra   rf   rg   )rh   ri   z4Still waiting for at least one iteration to succeed.NzLoss = ru   rv   z&% with the following hyper-parameters:F)Údefault_flow_style)r   ry   r>   r   r   r   rƒ   r„   Úprintr”   r@   Údump)
rO   rY   rZ   r   ri   rŒ   rƒ   rU   r”   Úcontents
             r"   ÚbestzExperiment.bestX  sÁ   € ô Ø�N‰N×!Ñ!Ø#×2Ñ2Ø&Øð "ó ó
ˆ	ð ˆ
ÜØ�N‰N˜yÐ+;Ñ;È
ô
ˆ	ð	Ø!×+Ñ+ˆIð
  ×+Ñ+ˆä�˜˜i™¨Ð*Ð*PÐQÔRä—)‘)˜K¸EÔBˆÜˆg�øô ò 	ÜÐHÔIÜûð	ús   ÁB Â	B<Â'B7Â7B<Ú
output_dirÚ
use_filterc                 óH  — t        j                  || j                  ¬«      }	 | j                  j	                  «       }|j                  dd¬«       |r"||› d|› d| j                  j                  › �z  }n!||› d|› d| j                  j                  › �z  }t        |d¬«      5 }	t         t        ||«      «       «      }
|rt        | j                  |
«      }
d	|› d
|› d�}t        |
|d¬«      D ]^  }| j                  |«      }| j                  j                  |	|«       |j                  dd«      }|€d}|€ŒIt!        |«      } ||||¬«      }Œ` 	 ddd«       |j"                  dz  }|j%                  «       r|j'                  «        |j)                  |«       |€d|› d�}t+        |«       y|r||› d|› d�z  }n||› d|› d�z  }t        |d«      5 }	|	j                  t-        |«      «       ddd«       y# t
        $ r}d}Y d}~�ŒÇd}~ww xY w# 1 sw Y   Œ¿xY w# 1 sw Y   yxY w)z÷Apply current best pipeline

        Parameters
        ----------
        protocol_name : `str`
            Name of pyannote.database protocol to process.
        subset : `str`, optional
            Subset to process. Defaults to 'test'
        re   NTrc   ú.z_INCOMPLETE.Úwrp   zProcessing z (ú)Úfile)Úiterablerx   rm   Ú
annotation)ÚuemÚlatestzWFor some (possibly good) reason, the output of this pipeline could not be evaluated on z_INCOMPLETE.evalz.eval)r	   r{   rI   r   Ú
get_metricÚNotImplementedErrorrz   Úwrite_formatr?   r   r�   r€   rL   r   ÚwriterD   r
   ÚparentÚexistsÚunlinkÚ
symlink_tor˜   Ústr)rO   rY   rœ   rZ   r�   rb   ÚmetricrU   Ú
output_extrQ   Úfilesrx   Úcurrent_fileÚoutputÚ	referencer¥   Ú_r¦   ÚmsgÚoutput_evals                       r"   ÚapplyzExperiment.apply}  s_  € ô$ ×(Ñ(Ø¨×)<Ñ)<ô
ˆð
	Ø—^‘^×.Ñ.Ó0ˆFð 	×Ñ °ÐÔ5ÙàØ"�O 1 V H¨L¸¿¹×9TÑ9TÐ8UÐVñWñ ð  ˜¨a°¨x°q¸¿¹×9TÑ9TÐ8UÐVÑVð ô �* 3Ô'ð 	7¨2ÜÐ2œ ¨6Ó2Ó4Ó5ˆEÙÜ˜tŸ}™}¨eÓ4�à   ¨r°&°¸Ð;ˆDÜ $¨e¸$ÀVÔ Lò 7�àŸ™¨Ó5�Ø—‘×$Ñ$ R¨Ô0ð )×,Ñ,¨\¸4Ó@�	ØÐ$Ø!�Fð �>Øä# LÓ1�Ù˜9 f°#Ô6‘ñ7÷	7ð0 ×"Ñ" XÑ-ˆØ�=‰=Œ?Ø�M‰MŒOØ×Ñ˜*Ô%ð ˆ>ð6Ø6C°_ÀAðGð ô �#ŒJØáØ$¨-¨¸¸&¸ÐAQÐ'RÑR‰Kà$¨-¨¸¸&¸ÀÐ'GÑGˆKä�+˜sÓ#ð 	" rØ�H‰H”S˜“[Ô!÷	"ð 	"øôs #ò 	ØŽFûð	ú÷	7ð 	7ú÷V	"ð 	"ús0   £G3 Â"B+HÇHÇ3	H	Ç<HÈH	ÈHÈH!)F)ÚdevelopmentNr   NNF)rº   )ÚtestF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r=   ry   Ú	APPLY_DIRÚclassmethodr   rK   r#   r<   r¯   r   Úintr•   r›   r¹   Ú__classcell__)rX   s   @r"   r   r   ‹   s  ø„ ñð /€JØ<€IØ*€Iàñ tð °tð Èò ó ðñ,C& tð C&°tõ C&ðP !.Ø%)ØØ!%Ø $Ø"ñ`#àð`#ð ˜‘ð`#ð ˜T‘Nð	`#ð
 ð`#ð ˜#‘ð`#ð ˜‘ð`#ð ó`#ñD# #ð #¨só #ðR !'Ø ñS"àðS"ð ðS"ð ˜‘ð	S"ð
 ÷S"r$   r   c            	      óö  — t        t        d¬«      } | d   j                  d«      D ]  }t        j                  |«       Œ | d   }| d   }| d   r¬|€d}| d	   rd
}nt        | d   «      }| d   }| d   }| d   }|r)t        |«      j                  «       j                  d¬«      }| d   }t        | d   «      }	|	j                  «       j                  d¬«      }	t        |	d¬«      }
|
j                  |||||||¬«       | d   rR|€d}t        | d   «      }	|	j                  «       j                  d¬«      }	t        |	d¬«      }
|
j                  ||¬«       | d   r£|€d}| d   }t        | d   «      }|j                  «       j                  d¬«      }t        j                  |d¬«      }
t        |
j                  j                  ||
j                  j!                  d«      ¬«      «      }|
j#                  ||||¬«       y y )NzTunable pipelines)Úversionz
--registryú,z<database.task.protocol>z--subsetr•   rº   z	--foreverrk   z--iterationsz	--samplerz--prunerz--pretrainedT)Ústrictz--average-casez<experiment_dir>r   )rZ   r\   r[   r]   r^   r_   r›   F)rZ   r¹   r»   z--use-filterz<train_dir>z%Y%m%d-%H%M%S)r   Údate)rZ   r�   )r   r¿   Úsplitr	   Úload_databaserÂ   r   Ú
expanduserÚresolver   r•   r›   r#   rÀ   r>   r   Ústrftimer¹   )Ú	argumentsr-   rY   rZ   r’   r]   r^   r[   r_   r   Ú
experimentr�   r   rœ   s                 r"   ÚmainrÐ   Ó  sB  € Ü”wÐ(;Ô<€Ià! ,Ñ/×5Ñ5°cÓ:ò -ˆÜ×Ñ˜|Õ,ð-ð Ð8Ñ9€MØ�zÑ"€Fà�ÒØˆ>Ø"ˆFà�[Ò!Ø‰Jä˜Y ~Ñ6Ó7ˆJà˜KÑ(ˆØ˜:Ñ&ˆà˜~Ñ.ˆ
ÙÜ˜jÓ)×4Ñ4Ó6×>Ñ>ÀdÐ>ÓKˆJà Ð!1Ñ2ˆä˜iÐ(:Ñ;Ó<ˆØ'×2Ñ2Ó4×<Ñ<ÀDÐ<ÓIˆä ¸Ô>ˆ
Ø×ÑØØØ#Ø!ØØØ%ð 	ô 	
ð �ÒØˆ>Ø"ˆFä˜iÐ(:Ñ;Ó<ˆØ'×2Ñ2Ó4×<Ñ<ÀDÐ<ÓIˆä ¸Ô?ˆ
Ø�‰˜¨fˆÔ5à�ÒØˆ>ØˆFà˜~Ñ.ˆ
ä˜ =Ñ1Ó2ˆ	Ø×(Ñ(Ó*×2Ñ2¸$Ð2Ó?ˆ	Ü×.Ñ.¨yÀ5Ð.ÓIˆ
äØ× Ñ ×'Ñ'Ø#¨*×*;Ñ*;×*DÑ*DÀ_Ó*Uð (ó ó
ˆ
ð 	×ÑØ˜:¨fÀð 	õ 	
ð! r$   )r¿   r   Úos.pathr@   Únumpyr…   Útypingr   Úpathlibr   r   r‡   r   r   Úpyannote.databaser   r	   r
   Úpyannote.core.utils.helperr   rŒ   r   r   rÐ   r1   r$   r"   ú<module>r×      sN   ðñ:Xót 
Û Û Û Ý Ý Ý ã Ý Ý å (Ý &Ý +å 8Ý  ÷E"ñ E"óP
E
r$   