o
    pi+                     @   s   d Z ddlZddlZddlmZmZmZmZmZ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 G d	d
 d
ejjjZedkrOddlZe  dS dS )zi
########
Features
########

See :class:`pyannote.core.SlidingWindowFeature` for the complete reference.
    N)TupleOptionalUnionIteratorListText)	Alignment   )Segment)SlidingWindow)Timelinec                   @   sH  e Zd ZdZ	d3dejdedee fddZ	dd	 Z
ed
d Zedd Zdd Zdd Zdd ZdedejfddZdd Zdeeejf fddZdd Z	d4dee deeeejef ejf  fd d!Z	"		#d5d$eeef d%ed&ee d'edeejd f f
d(d)Z d*d+ Z!eje"j#fZ$dejfd,d-Z%d.d/ Z&d6d1d2Z'dS )7SlidingWindowFeaturezPeriodic feature vectors

    Parameters
    ----------
    data : (n_frames, n_features) numpy array
    sliding_window : SlidingWindow
    labels : list, optional
        Textual description of each dimension.
    Ndatasliding_windowlabelsc                 C   s   || _ || _|| _d| _d S N)r   r   r   _SlidingWindowFeature__i)selfr   r   r    r   I/home/ubuntu/.local/lib/python3.10/site-packages/pyannote/core/feature.py__init__<   s   
zSlidingWindowFeature.__init__c                 C      | j jd S )zNumber of feature vectorsr   r   shaper   r   r   r   __len__D   s   zSlidingWindowFeature.__len__c                 C   s   | j dt| S )Nr   )r   range_to_segmentlenr   r   r   r   extentH   s   zSlidingWindowFeature.extentc                 C   r   )zDimension of feature vectorsr	   r   r   r   r   r   	dimensionL   s   zSlidingWindowFeature.dimensionc                 C   s   t dt | jjd S )Nz(This is deprecated in favor of `__len__`r   )warningswarnDeprecationWarningr   r   r   r   r   r   	getNumberQ   s   zSlidingWindowFeature.getNumberc                 C      t dt | jS )Nz3This is deprecated in favor of `dimension` property)r!   r"   r#   r    r   r   r   r   getDimensionU      z!SlidingWindowFeature.getDimensionc                 C   r%   )Nz0This is deprecated in favor of `extent` property)r!   r"   r#   r   r   r   r   r   	getExtent[   r'   zSlidingWindowFeature.getExtentireturnc                 C   s
   | j | S )zGet ith feature vectorr   )r   r)   r   r   r   __getitem__a   s   
z SlidingWindowFeature.__getitem__c                 C   s
   d| _ | S r   )r   r   r   r   r   __iter__e   s   zSlidingWindowFeature.__iter__c              
   C   sH   |  j d7  _ z| j| j  | j| j  fW S  ty# } zt d }~ww )Nr	   )r   r   r   
IndexErrorStopIteration)r   er   r   r   __next__i   s   zSlidingWindowFeature.__next__c                 C   s   |   S N)r1   r   r   r   r   nextp   s   zSlidingWindowFeature.nextFwindowc                 c   sH    | j jd }t|D ]}|r| j | | j| fV  q| j | V  qdS )zFeature vector iterator

        Parameters
        ----------
        window : bool, optional
            When True, yield both feature vector and corresponding window.
            Default is to only yield feature vector

        r   N)r   r   ranger   )r   r4   	n_samplesr)   r   r   r   iterfeaturess   s   z!SlidingWindowFeature.iterfeatureslooseTfocusmodefixedreturn_datac              
      s  |st |tsd}t||s|durd}t| jj|||dd} jjd }t jjd }g dd}	}
}|D ]2\}}|
t|dt|d 7 }
|t	||t	|| 7 }|dk s_||kr`q;|	t	|dt||gg7 }	q;|	r}t
 fdd	|	D }nd
 jjdd  }t
|}|durt
t
 jd |
fd|  |t
 j|d  |fd|  g}|r|S t j|	d d  j jj jjd}t|| jdS )a@  Extract frames

        Parameters
        ----------
        focus : Segment or Timeline
        mode : {'loose', 'strict', 'center'}, optional
            In 'strict' mode, only frames fully included in 'focus' support are
            returned. In 'loose' mode, any intersecting frames are returned. In
            'center' mode, first and last frames are chosen to be the ones
            whose centers are the closest to 'focus' start and end times.
            Defaults to 'loose'.
        fixed : float, optional
            Overrides `Segment` 'focus' duration and ensures that the number of
            returned frames is fixed (which might otherwise not be the case
            because of rounding errors).
        return_data : bool, optional
            Return a numpy array (default). For `Segment` 'focus', setting it
            to False will return a `SlidingWindowFeature` instance.

        Returns
        -------
        data : `numpy.ndarray` or `SlidingWindowFeature`
            Frame features.

        See also
        --------
        SlidingWindow.crop

        zG"focus" must be a "Segment" instance when "return_data"is set to False.Nz9"fixed" cannot be set when "return_data" is set to False.T)r:   r;   return_rangesr   r	   c                    s&   g | ]\}} j ||d d f qS r2   r+   ).0startendr   r   r   
<listcomp>   s   & z-SlidingWindowFeature.crop.<locals>.<listcomp>)r   )r	   )r?   durationstepr   )
isinstancer
   
ValueErrorr   cropr   r   r   minmaxnpvstackemptytiler   r?   rB   rC   r   r   )r   r9   r:   r;   r<   msgrangesr6   n_dimensionsclipped_rangesrepeat_firstrepeat_lastr?   r@   r   r   r   r   r   r   rG      sP   %
zSlidingWindowFeature.cropc                 C   sB   ddl m}m} |st|j| jjd d S ddl m} || S )Nr	   )MATPLOTLIB_IS_AVAILABLEMATPLOTLIB_WARNING)klass)repr_feature)	notebookrT   rU   r!   r"   format	__class____name__rW   )r   rT   rU   rW   r   r   r   
_repr_png_   s   zSlidingWindowFeature._repr_png_c                 C   s   | j S r2   r+   r   r   r   r   	__array__   s   zSlidingWindowFeature.__array__c                    s   | dd}|| D ]}t| jtf st  S q
tdd |D }|r0tdd |D |d< t|||i |}t|tu rKt fdd|D S |dkrQd S t | j j	dS )	Noutr   c                 s   $    | ]}t |tr|jn|V  qd S r2   rE   r   r   r>   xr   r   r   	<genexpr>      
z7SlidingWindowFeature.__array_ufunc__.<locals>.<genexpr>c                 s   r_   r2   r`   ra   r   r   r   rc     rd   c                 3   s&    | ]}t  | j jd V  qdS )rD   N)typer   r   ra   r   r   r   rc     s    
atrD   )
getrE   _HANDLED_TYPESr   NotImplementedtuplegetattrre   r   r   )r   ufuncmethodinputskwargsr^   rb   r   r   r   r   __array_ufunc__  s(   z$SlidingWindowFeature.__array_ufunc__toc                    s   | j j}| j j}| j j}t| }|d|  t||  |j j}|j j}|j j}t|}	|d|  t|	|   t fdd| jj	D }
t
|
|j | jdS )a
  Align features by linear temporal interpolation

        Parameters
        ----------
        to : SlidingWindowFeature
            Features to align with.

        Returns
        -------
        aligned : SlidingWindowFeature
            Aligned features
        g      ?c                    s(   g | ]}t  |d d t jf qS r2   )rJ   interpnewaxis)r>   old_datanew_told_tr   r   rA   >  s    z.SlidingWindowFeature.align.<locals>.<listcomp>rD   )r   r?   rC   rB   r   rJ   arangehstackr   Tr   r   )r   rq   	old_startold_stepold_durationold_samples	new_startnew_stepnew_durationnew_samplesnew_datar   ru   r   align#  s    zSlidingWindowFeature.alignr2   )F)r8   NT)rq   r   r*   r   )(r[   
__module____qualname____doc__rJ   ndarrayr   r   r   r   r   propertyr   r    r$   r&   r(   intr,   r-   r   r
   r1   r3   r   boolr   r   r7   r   r   floatrG   r\   numbersNumberrh   r]   rp   r   r   r   r   r   r   1   s^    





m r   __main__)r   r   r!   typingr   r   r   r   r   r   numpyrJ   pyannote.core.utils.typesr   segmentr
   r   timeliner   libmixinsNDArrayOperatorsMixinr   r[   doctesttestmodr   r   r   r   <module>   s       