
      iM                         d dl mZmZmZ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Zd d	lZd d
lmZ d dlmZ d dlmZ  G d d      Zy	)    )OptionalTextIOUnionDictAny)Path)OrderedDict   )PipelineInput)PipelineOutput)	Direction)FileLockN)Timeline)
Annotation)Trialc            	          e Zd ZdZd Zed        Zej                  d        Zd Zd Z	d Z
d Z	 d(d	ee   d
ee   defdZdedefdZdedefdZ	 	 	 d)dee   d	ee   d
ee   defdZd Zdedd fdZdedd fdZed        Z	 	 d*dedee   dee   defdZdedd fdZdedefdZd+dZ de!fd Z"ded!edefd"Z#ed#        Z$d$e%d!efd%Z&d$e%d!e'e(e)f   fd&Z*d$e%d!e'e(e)f   fd'Z+y),PipelinezBase tunable pipelinec                 l    t               | _        t               | _        t               | _        d| _        y )NF)r	   _parameters_instantiated
_pipelinestrainingselfs    o/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pyannote/pipeline/pipeline.py__init__zPipeline.__init__0   s.     2= .9] 0;}     c                     | j                   S N)	_trainingr   s    r   r   zPipeline.training>   s    ~~r   c                 d    || _         | j                  j                         D ]  \  }}||_         y r   )r    r   itemsr   )r   r   _pipelines       r   r   zPipeline.trainingB   s,    !??002KAx (H 3r   c           	          | j                  d      }t        t        t        | j	                  |      j                                           S )NT)frozen)
parametershashtuplesorted_flattenr"   )r   r&   s     r   __hash__zPipeline.__hash__I   s9    -E&v!6!<!<!>?@AAr   c                 F   d| j                   v r| j                   d   }||v r||   S d| j                   v r| j                   d   }||v r||   S d| j                   v r| j                   d   }||v r||   S dj                  t        |       j                  |      }t	        |      )z(Advanced) attribute getterr   r   r   z!'{}' object has no attribute '{}')__dict__formattype__name__AttributeError)r   namer   r   r   msgs         r   __getattr__zPipeline.__getattr__N   s     dmm+ MM/:M}$$T** DMM)--6K{""4(( 4==(|4Jz!!$''188d9L9LdSS!!r   c                    ddl m} fd}| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }t	        ||      r)|d}t        |       || j                  ||       ||<   yt	        |t              r)|d	}t        |       || j                  ||       ||<   y|
|v r||<   yt        j                  | |       y)
a?  (Advanced) attribute setter

        If `value` is an instance of `Parameter`, store it in `_parameters`.
        elif `value` is an instance of `Pipeline`, store it in `_pipelines`.
        elif `value` isn't an instance of `Parameter` and `name` is in `_parameters`,
        store `value` in `_instantiated`.
        r
   )	Parameterc                  $    | D ]
  }|v s|=  y r    )dictsdr3   s     r   remove_fromz)Pipeline.__setattr__.<locals>.remove_fromr   s    19$ r   r   r   r   Nz>cannot assign hyper-parameters before Pipeline.__init__() callz;cannot assign sub-pipelines before Pipeline.__init__() call)		parameterr7   r.   get
isinstancer2   r   object__setattr__)	r   r3   valuer7   r<   r   r   r   r4   s	    `       r   rA   zPipeline.__setattr__f   s     	)	 
 mm''6))/:]]&&|4
 eY'"W  %S))}jA %K eX&!V$S)){MB$Jt "t{':"'M$4u-r   c                     || j                   v r| j                   |= y || j                  v r| j                  |= y || j                  v r| j                  |= y t        j	                  | |       y r   )r   r   r   r@   __delattr__)r   r3   s     r   rD   zPipeline.__delattr__   sd    4###  &T'''""4(T__$% tT*r   r&   instantiatedreturnc                    ddl m} |r|rd}t        |      |rt        | j                        }n\|rE| j
                  j                         D ci c]  \  }}t        ||      s||j                  ! }}}nt        | j
                        }| j                  j                         D ]:  \  }}	|	j                  ||      }
|
j                         D ]  \  }}||| d| <    < |S c c}}w )aw  Get flattened dictionary of parameters

        Parameters
        ----------
        frozen : `bool`, optional
            Only return value of frozen parameters.
        instantiated : `bool`, optional
            Only return value of instantiated parameters.

        Returns
        -------
        params : `dict`
            Flattened dictionary of parameters.
        r
   Frozenz4one must choose between `frozen` and `instantiated`.r&   rE   >)r=   rI   
ValueErrordictr   r   r"   r?   rB   r   _flattened_parameters)r   r&   rE   rI   r4   paramsnppipeline_namer$   pipeline_paramsr3   rB   s                r   rN   zPipeline._flattened_parameters   s    & 	&lHCS/! $,,-F'+'7'7'='='?'?tq!:aQWCX177
'?  
 $**+F (,'<'<'>#M8&<<L = O  /446e49-$01  7	 (? s   C%$C%nested_paramsc                     t               }|j                         D ]L  \  }}t        |t               r2| j                  |      j                         D ]  \  }}||| d| <    H|||<   N |S )a  Convert nested dictionary to flattened dictionary

        For instance, a nested dictionary like this one:

            ~~~~~~~~~~~~~~~~~~~~~
            param: value1
            pipeline:
                param: value2
                subpipeline:
                    param: value3
            ~~~~~~~~~~~~~~~~~~~~~

        becomes the following flattened dictionary:

            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            param                       : value1
            pipeline>param              : value2
            pipeline>subpipeline>param  : value3
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        Parameter
        ---------
        nested_params : `dict`

        Returns
        -------
        flattened_params : `dict`
        rK   )rM   r"   r?   r+   )r   rT   flattened_paramsr3   rB   subnamesubvalues          r   r+   zPipeline._flatten   sz    :  6(..0KD%%&)-u)=)C)C)E%GX<D$vQwi%89 *F */ & 1  r   rV   c                 v   i }| j                   D ci c]  }|i  }}|j                         D ]K  \  }}|j                  d      }t        |      dkD  r"|d   }dj	                  |dd       }|||   |<   G|||<   M | j                   j                         D ]  \  }}	|	j                  ||         ||<    |S c c}w )a  Convert flattened dictionary to nested dictionary

        For instance, a flattened dictionary like this one:

            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            param                       : value1
            pipeline>param              : value2
            pipeline>subpipeline>param  : value3
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        becomes the following nested dictionary:

            ~~~~~~~~~~~~~~~~~~~~~
            param: value1
            pipeline:
                param: value2
                subpipeline:
                    param: value3
            ~~~~~~~~~~~~~~~~~~~~~

        Parameter
        ---------
        flattened_params : `dict`

        Returns
        -------
        nested_params : `dict`
        rK   r
   r   N)r   r"   splitlenjoin
_unflatten)
r   rV   rT   r3   rS   rB   tokensrR   
param_namer$   s
             r   r]   zPipeline._unflatten   s    < 04@48@+113KD% ZZ_F6{Q &q	 XXfQRj1
=B.z:
 ',d# 4$ #oo335ND("*"5"5od6K"LM$ 6 - As   
B6Ntrialc           	          |s|r|d}t        |      | j                  ||      }|*|j                         D ci c]  \  }}| |||       }}}| j                  |      S c c}}w )ag  Returns nested dictionary of (optionnaly instantiated) parameters.

        For a pipeline with one `param`, one sub-pipeline with its own param
        and its own sub-pipeline, it will returns something like:

        ~~~~~~~~~~~~~~~~~~~~~
        param: value1
        pipeline:
            param: value2
            subpipeline:
                param: value3
        ~~~~~~~~~~~~~~~~~~~~~

        Parameter
        ---------
        trial : `Trial`, optional
            When provided, use trial to suggest new parameter values
            and return them.
        frozen : `bool`, optional
            Return frozen parameter value
        instantiated : `bool`, optional
            Return instantiated parameter values.

        Returns
        -------
        params : `dict`
            Nested dictionary of parameters. See above for the actual format.
        z<One must choose between `trial`, `instantiated`, or `frozen`rJ   )rL   rN   r"   r]   )r   r`   r&   rE   r4   rO   r3   params           r   r'   zPipeline.parameters2  s|    F F(9PCS/! ++6+UBH,,.Q.;4dE$...FQ v&& Rs   A$c                      y)z8Instantiate root pipeline with current set of parametersNr9   r   s    r   
initializezPipeline.initializec  s    r   rO   c                 B   ddl m} |j                         D ]  \  }}|| j                  v r@t	        |t
              sd| d}t        |      | j                  |   j                  |       T|| j                  v rt        | | ||             vd| d}t        |       | S )zRecursively freeze pipeline parameters

        Parameters
        ----------
        params : `dict`
            Nested dictionary of parameters.

        Returns
        -------
        self : `Pipeline`
            Pipeline.
        r
   rH   only parameters of 'z1' pipeline can be frozen (not the whole pipeline)parameter '' does not exist)
r=   rI   r"   r   r?   rM   rL   freezer   setattr)r   rO   rI   r3   rB   r4   s         r   ri   zPipeline.freezeg  s     	&!<<>KD% t&!%..tf 5= >  %S/)%,,U3 t'''dF5M2v%56CS/!' ** r   c                    ddl m} |j                         D ]  \  }}|| j                  v r@t	        |t
              sd| d}t        |      | j                  |   j                  |       T|| j                  v rlt        | |      }t	        ||      rF|j                  |k7  r7d| d|j                   d| d}t        j                  |       |j                  }t        | ||       d	| d
}t        |       | j                          | S )zRecursively instantiate all pipelines

        Parameters
        ----------
        params : `dict`
            Nested dictionary of parameters.

        Returns
        -------
        self : `Pipeline`
            Instantiated pipeline.
        r
   rH   rf   z7' pipeline can be instantiated (not the whole pipeline)zParameter 'z%' is frozen: using its frozen value (z) instead of the one provided (z).rg   rh   )r=   rI   r"   r   r?   rM   rL   instantiater   getattrrB   warningswarnrj   rd   )r   rO   rI   r3   rB   r4   rb   s          r   rl   zPipeline.instantiate  s    	&!<<>KD% t&!%..tf 5C D  %S/)%11%8 t'''d+eV,1E%dV ,!KK=(GwbR  MM#&!KKEdE*v%56CS/!9 *< 	r   c                     t        | j                  | j                                     }t        | j                  | j                  d                  }||k(  S )zEWhether pipeline has been instantiated (and therefore can be applied)TrE   )setr+   r'   )r   r'   rE   s      r   rE   zPipeline.instantiated  sG     t'89:
4==d)KLM\))r   
params_ymllossc                 8   || j                  d      }d|i}|||d<   t        j                  |d      }t        |j	                  d            5  t        |d	
      5 }|j                  |       ddd       ddd       |S # 1 sw Y   xY w# 1 sw Y   |S xY w)a  Dump parameters to disk

        Parameters
        ----------
        params_yml : `Path`
            Path to YAML file.
        params : `dict`, optional
            Nested Parameters. Defaults to pipeline current parameters.
        loss : `float`, optional
            Loss value. Defaults to not write loss to file.

        Returns
        -------
        content : `str`
            Content written in `param_yml`.
        NTrq   rO   rt   F)default_flow_stylez.lockwmode)r'   yamldumpr   with_suffixopenwrite)r   rs   rO   rt   contentcontent_ymlfps          r   dump_paramszPipeline.dump_params  s    . >__$_7FV$"GFO iiEB j,,W56js+r% , 7  ,+ 7 s$   BB1BB	BBc                     t        |d      5 }t        j                  |t        j                        }ddd       | j	                  d         S # 1 sw Y   xY w)zInstantiate pipeline using parameters from disk

        Parameters
        ----------
        param_yml : `Path`
            Path to YAML file.

        Returns
        -------
        self : `Pipeline`
            Instantiated pipeline

        rrx   )LoaderNrO   )r}   rz   load
SafeLoaderrl   )r   rs   r   rO   s       r   load_paramszPipeline.load_params  sH     *3'2YYr$//:F (x 011 ('s   &AAinputc                     t         )z-Apply pipeline on input and return its outputNotImplementedError)r   r   s     r   __call__zPipeline.__call__  s    !!r   c                     t               )a  Return new metric (from pyannote.metrics)

        When this method is implemented, the returned metric is used as a
        replacement for the loss method below.

        Returns
        -------
        metric : `pyannote.metrics.base.BaseMetric`
        r   r   s    r   
get_metriczPipeline.get_metric  s     "##r   c                      y)Nminimizer9   r   s    r   get_directionzPipeline.get_direction  s    r   outputc                     t               )a  Compute loss for given input/output pair

        Parameters
        ----------
        input : object
            Pipeline input.
        output : object
            Pipeline output

        Returns
        -------
        loss : `float`
            Loss value
        r   )r   r   r   s      r   rt   zPipeline.loss  s     "##r   c                      y)Nrttmr9   r   s    r   write_formatzPipeline.write_format(  s    r   filec                 B     t        | d| j                         ||      S )zWrite pipeline output to file

        Parameters
        ----------
        file : file object
        output : object
            Pipeline output
        write_)rm   r   )r   r   r   s      r   r~   zPipeline.write,  s(     ;wtvd&7&7%89:4HHr   c           
      h   t        |t              r|j                  d      }t        |t              r\|j	                  d      D ]F  \  }}}d|j
                   d|j                  dd|j                  dd	| d
	}|j                  |       H yd|j                  j                   d}t        |      )zWrite pipeline output to "rttm" file

        Parameters
        ----------
        file : file object
        output : `pyannote.core.Timeline` or `pyannote.core.Annotation`
            Pipeline output
        string)	generatorTyield_labelzSPEAKER z 1 .3f z <NA> <NA> z <NA> <NA>
NDumping z, instances to "rttm" files is not supported.)r?   r   to_annotationr   
itertracksuristartdurationr~   	__class__r1   r   )r   r   r   stlliner4   s           r   
write_rttmzPipeline.write_rttm8  s     fh'))H)=Ffj)!,,,>1avzzl#aggc]!AJJs;K L!!"<1  

4  ?  v''001 2  ! 	 "#&&r   c                    t        |t              rD|D ]>  }|j                   d|j                  dd|j                  dd}|j                  |       @ yt        |t              r^|j                  d      D ]H  \  }}}|j                   d|j                  dd|j                  dd| d| d
}|j                  |       J yd|j                  j                   d}t        |      )	zWrite pipeline output to "txt" file

        Parameters
        ----------
        file : file object
        output : `pyannote.core.Timeline` or `pyannote.core.Annotation`
            Pipeline output
        r   r   
NTr   r   z+ instances to "txt" files is not supported.)r?   r   r   r   endr~   r   r   r   r1   r   )r   r   r   r   r   r   r   r4   s           r   	write_txtzPipeline.write_txtT  s     fh' **Qqwwsm1QUU3KrB

4   fj)!,,,>1a **Qqwwsm1QUU3Kq1QCrJ

4  ?  v''001 2  ! 	 "#&&r   )FF)NFF)NN)rF   z pyannote.metrics.base.BaseMetric),r1   
__module____qualname____doc__r   propertyr   setterr,   r5   rA   rD   r   boolrM   rN   r+   r]   r   r'   rd   ri   rl   rE   r   floatstrr   r   r   r   r   r   r   r   rt   r   r   r~   r   r   r   r   r   r9   r   r   r   r   -   s      __) )B
"0/.b+ NS-tn-<DTN-	-^$ d $ t $ L64 6D 6t "&!&',	/'/' /' tn	/'
 
/'b&T &j &P1$ 1: 1f * * "& $	&& & uo	&
 
&P2d 2z 2&"m " "
$y $- $ $E $"  
I& 
I. 
I'v 'uXz5I/J '8'f 'eHj4H.I 'r   r   )typingr   r   r   r   r   pathlibr   collectionsr	   r   r   r   filelockr   rz   rn   pyannote.corer   r   optuna.trialr   r   r9   r   r   <module>r      s:   : 6 5  # ! "     " $ A	' A	'r   