o
    pi4                  
   @   s.  d Z ddlmZmZmZ ddlmZmZmZ zddl	m
Z
 W n ey0 Z zW Y dZ[ndZ[ww ddlZddlmZmZmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ zddlZW n eyj   dZY nw dZdZG dd dZ e  Z!defddZ"defddZ#defddZ$defddZ%dS )aV  
#############
Visualization
#############

:class:`pyannote.core.Segment`, :class:`pyannote.core.Timeline`,
:class:`pyannote.core.Annotation` and :class:`pyannote.core.SlidingWindowFeature`
instances can be directly visualized in notebooks.

You will however need to install ``pyannote.core``'s additional dependencies
for notebook representations (namely, matplotlib):


.. code-block:: bash

    pip install pyannote.core[notebook]


Segments
--------

.. code-block:: ipython

  In [1]: from pyannote.core import Segment

  In [2]: segment = Segment(start=5, end=15)
    ....: segment

.. plot:: pyplots/segment.py


Timelines
---------

.. code-block:: ipython

  In [25]: from pyannote.core import Timeline, Segment

  In [26]: timeline = Timeline()
     ....: timeline.add(Segment(1, 5))
     ....: timeline.add(Segment(6, 8))
     ....: timeline.add(Segment(12, 18))
     ....: timeline.add(Segment(7, 20))
     ....: timeline

.. plot:: pyplots/timeline.py


Annotations
-----------


.. code-block:: ipython

    In [1]: from pyannote.core import Annotation, Segment

    In [6]: annotation = Annotation()
       ...: annotation[Segment(1, 5)] = 'Carol'
       ...: annotation[Segment(6, 8)] = 'Bob'
       ...: annotation[Segment(12, 18)] = 'Carol'
       ...: annotation[Segment(7, 20)] = 'Alice'
       ...: annotation

.. plot:: pyplots/annotation.py

    )IterableDictOptional   )Label
LabelStyleResource)print_figureN)cycleproductgroupby)SegmentSlidingWindow)Timeline)
Annotation)SlidingWindowFeatureFTzCouldn't import matplotlib to render the vizualization for object {klass}. To enable, install the required dependencies with 'pip install pyannore.core[notebook]'c                   @   s  e Zd Zdd Zdd Zedd ZejdefddZej	d	d Zed
d Z
e
jdefddZ
e
j	dd Z
dedefddZd.ddZd/defddZdee dejfddZd0deded efd!d"Zd/d#d$Zd/d%efd&d'Zd1d(efd)d*Z	d2d+efd,d-ZdS )3Notebookc                 C   s   |    d S N)resetself r   J/home/ubuntu/.local/lib/python3.10/site-packages/pyannote/core/notebook.py__init__}   s   zNotebook.__init__c                    sb   ddl m} ddg}g d}|d  fddtd	D }tt|||| _d d
i| _| `| `d S )Nr   )get_cmap   r   )soliddasheddottedSet1c                    s   g | ]
} d | d qS )      ?   r   ).0icmr   r   
<listcomp>   s    z"Notebook.reset.<locals>.<listcomp>	   )r   r   )        r(   r(   )	matplotlib.pyplotr   ranger
   r   _style_generator_stylecropwidth)r   r   	linewidth	linestylecolorsr   r$   r   r      s   zNotebook.resetc                 C      | j S )zThe crop property._cropr   r   r   r   r-         zNotebook.cropsegmentc                 C   
   || _ d S r   r3   )r   r6   r   r   r   r-         
c                 C   s
   d | _ d S r   r3   r   r   r   r   r-      r8   c                 C   r2   )zThe width property_widthr   r   r   r   r.      r5   zNotebook.widthvaluec                 C   r7   r   r9   )r   r;   r   r   r   r.      r8   c                 C   s
   d| _ d S )N   r9   r   r   r   r   r.      r8   labelreturnc                 C   s$   || j vrt| j| j |< | j | S )z Get line style for a given label)r,   nextr+   )r   r=   r   r   r   __getitem__   s   

zNotebook.__getitem__Nr   r   FTc                 C   s`   dd l m} |d u r| }|| j |r|d n|g  || |j	 
| |S )Nr   Time)r)   pyplotgcaset_xlimr-   
set_xlabelset_xticklabelsset_ylimaxes	get_yaxisset_visible)r   axylimyaxistimepltr   r   r   setup   s   

zNotebook.setupc           	   	   C   s   |sd S | | \}}}|j ||j|j||||d |r;|j|j|d |d |ddd |j|j|d |d |ddd |d u rAd S d S )N)r/   r0   r=   g?r   r   )r/   r0   )hlinesstartendvlines)	r   rL   r6   yr=   
boundariesr0   r/   colorr   r   r   draw_segment   s,   	zNotebook.draw_segmentsegmentsc                 C   s   t j g}g }|D ]/}d}t|D ]\}}|j|kr(d}|| |j||<  nq|s8|t| ||j q	ddt|d  dt |   }|S )z

        Parameters
        ----------
        segments : Iterable
            `Segment` iterable (sorted)

        Returns
        -------
        y : np.array
            y coordinates of each segment

        FTr    r   )npinf	enumeraterS   appendrT   lenarray)r   rZ   up_torV   r6   foundr#   ur   r   r   get_y   s"   



"zNotebook.get_yresourcerO   legendc                 C   sv   t |tr| j||d d S t |tr| j||d d S t |tr+| j|||d d S t |tr9| j||d d S d S )N)rO   )rO   rf   )	
isinstancer   plot_segmentr   plot_timeliner   plot_annotationr   plot_feature)r   re   rO   rf   r   r   r   __call__  s   



zNotebook.__call__c                 C   s,   | j s|| _ | j||d}| ||d d S )NrL   rO   g      ?)r-   rQ   rY   )r   r6   rL   rO   r   r   r   rh     s   zNotebook.plot_segmenttimelinec                 C   s^   | j s
|r
| | _ |j | j dd}| j||d}t|| |D ]\}}| ||| q!d S )Nloosemoderm   )r-   extentrQ   ziprd   rY   )r   rn   rL   rO   croppedr6   rV   r   r   r   ri     s   

zNotebook.plot_timeline
annotationc              	   C   s   | j s|jdd | _ |j | j dd}| }dd | D }| j||d}t|jdd	| |D ]\\}}	}
}| j||||
d
 q4|r}|	 \}}|sQd S t
tt||dd ddd d}ttdd |D  \}}|j||dddddd d S d S )NF)copyintersectionrp   c                 S   s   g | ]\}}|qS r   r   )r"   s_r   r   r   r&   3  s    z,Notebook.plot_annotation.<locals>.<listcomp>rm   T)yield_label)r=   c                 S   s   | d S )Nr   r   )h_lr   r   r   <lambda>F  s    z*Notebook.plot_annotation.<locals>.<lambda>)keyc                 s   s$    | ]\}}t |d  |fV  qdS )r   N)r?   )r"   lr{   r   r   r   	<genexpr>H  s   " z+Notebook.plot_annotation.<locals>.<genexpr>rA   r      r(   )bbox_to_anchorlocncolborderaxespadframeon)r-   get_timelinerr   labels
itertracksrQ   rs   rd   rY   get_legend_handles_labelsr   sortedlistrf   )r   ru   rL   rO   rf   rt   r   rZ   r6   trackr=   rV   HLHLr   r   r   rj   ,  s8   
zNotebook.plot_annotationfeaturec                 C   s   | j s|j| _ |j}|jj\}}|j | j ddd\\}}	|| j||	 jf}
td|}t|	|}	|d j|jt	
||	  }|||	 }|d u rbt	|}t	|}|d||   |d||   f}| j|d||d}||| ||
 d S )Nro   T)rq   return_rangesr   g?F)rL   rN   rM   rO   )r-   rr   sliding_windowdatashapemiddlemaxminstepr[   arangenanminnanmaxrQ   plotrE   )r   r   rL   rO   rM   windown	dimensionrS   stopxlimtr   mMr   r   r   rk   S  s"   



 zNotebook.plot_feature)NrA   FT)NT)TT)NTT)NTN)__name__
__module____qualname__r   r   propertyr-   setterr   deleterr.   intr   r   r@   rQ   rY   r   r[   ndarrayrd   r   boolrl   rh   r   ri   r   rj   r   rk   r   r   r   r   r   |   s6    




-
(r   r6   c                 C   b   ddl m} |jd }tjdf|jd< | \}}tj| |d t|d}|| ||jd< |S )zGet `png` data for `segment`r   Nfigure.figsizer   rL   png)	r)   rC   rcParamsnotebookr.   subplotsrh   r	   close)r6   rP   figsizefigrL   r   r   r   r   repr_segmentq     



r   rn   c                 C   r   )zGet `png` data for `timeline`r   Nr   r   r   r   )	r)   rC   r   r   r.   r   ri   r	   r   )rn   rP   r   r   rL   r   r   r   r   repr_timeline  r   r   ru   c                 C   r   )zGet `png` data for `annotation`r   Nr      r   r   )	r)   rC   r   r   r.   r   rj   r	   r   )ru   rP   r   r   rL   r   r   r   r   repr_annotation  r   r   r   c                 C   s  ddl m} |jd }| jjdkr1tjdf|jd< | \}}tj| |d t	|d}|
| n| jjdkrt| }tjdu rPt| jj| j|d  jd	t_n	| jtjd
dd} t| jj| jj d }t||}tjd| f|jd< |j|ddd\}}| }t| jt| j}	}
|	d|
|	   |
d|
|	   f}t| D ]9\}\}}|||  }|jt|  }}t|j||d}t||| jd}tj|||| |d k|d |d qt	|d}|
| ||jd< |S )zGet `png` data for `feature`r   Nr   r   r   r   r   r   )rS   rT   ro   F)rq   return_datag      ?)nrowsncolssqueezeg?)rS   r   duration)r   )rL   rO   rM   )r)   rC   r   r   ndimr   r.   r   rk   r	   r   r_   r-   r   r   rS   rT   roundr   r   r   flattenr[   r   r   r]   r   r   r   set_prop_cycle)r   rP   r   r   rL   r   
num_chunksnum_overlaprI   minimaxirM   cr   r   r   frameswindow_featurer   r   r   repr_feature  sP   




 


r   )&__doc__typingr   r   r   utils.typesr   r   r   IPython.core.pylabtoolsr	   	Exceptionenumpyr[   	itertoolsr
   r   r   r6   r   r   rn   r   ru   r   r   r   
matplotlibImportErrorMATPLOTLIB_IS_AVAILABLEMATPLOTLIB_WARNINGr   r   r   r   r   r   r   r   r   r   <module>   s<   B s