a
    Mˆc8-  ã                   @   s‚   d dl Z d dl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Zdd	lmZmZ G d
d„ dƒZdS )é    N)Ú
export_pngÚ
export_svgÚshow)Úget_screenshot_as_png)Úgridplot)ÚLabel)Ú
Category10)Úfigureé   )Úfilled_to_bokehÚlines_to_bokehc                   @   sˆ   e Zd ZdZd)dd„Zdd	„ Zd
d„ Zdd„ Zd*dd„Zd+dd„Z	d,dd„Z
d-dd„Zd.dd„Zdd „ Zd!d"„ Zd/d#d$„Zd0d'd(„ZdS )1ÚBokehRendereraF  Utility renderer using Bokeh to render a grid of plots over the same (x, y) range.

    Args:
        nrows (int, optional): Number of rows of plots, default ``1``.
        ncols (int, optional): Number of columns of plots, default ``1``.
        figsize (tuple(float, float), optional): Figure size in inches (assuming 100 dpi), default
            ``(9, 9)``.
        show_frame (bool, optional): Whether to show frame and axes ticks, default ``True``.
        want_svg (bool, optional): Whether output is required in SVG format or not, default
            ``False``.

    Warning:
        :class:`~contourpy.util.bokeh_renderer.BokehRenderer`, unlike
        :class:`~contourpy.util.mpl_renderer.MplRenderer`, needs to be told in advance if output to
        SVG format will be required later, otherwise it will assume PNG output.
    r
   ©é	   r   TFc                 C   sª   || _ td | _dt |¡ }|| }g | _| j r6dnd}t|ƒD ]<}	t|d�}
d|
j_	d|
j
_	| j |
¡ |sBd |
_d|
j_	qBt| j|d |d | |d | d	�| _d S )
Né
   éd   ÚsvgZcanvas)Zoutput_backendFr   r
   )ÚncolsZtoolbar_locationÚwidthÚheight)Ú	_want_svgr   Ú_paletteÚnpÚasarrayÚ_figuresÚranger	   ZxgridZvisibleZygridÚappendZoutline_line_colorÚaxisr   Ú_layout)ÚselfZnrowsr   ZfigsizeZ
show_frameZwant_svgZ
total_sizeZnfiguresÚbackendÚ_Úfig© r#   ú`/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/contourpy/util/bokeh_renderer.pyÚ__init__   s$    


þzBokehRenderer.__init__c                 C   s4   t |tƒr0|d dkr0t|dd … ƒ}| j| }|S )Nr   ÚCr
   )Ú
isinstanceÚstrÚintr   )r   ÚcolorÚindexr#   r#   r$   Ú_convert_color5   s    
zBokehRenderer._convert_colorc                 C   s   t |tƒr| j| }|S )N)r'   r)   r   )r   Úaxr#   r#   r$   Ú_get_figure;   s    

zBokehRenderer._get_figurec                 C   s6   t  |¡}t  |¡}|jdkr.t  ||¡\}}||fS )Nr
   )r   r   ÚndimZmeshgrid)r   ÚxÚyr#   r#   r$   Ú_grid_as_2d@   s
    


zBokehRenderer._grid_as_2dr   ÚC0çffffffæ?c           	      C   sJ   |   |¡}|  |¡}t||ƒ\}}t|ƒdkrF|j|g|g||dd� dS )aë  Plot filled contours on a single plot.

        Args:
            filled (sequence of arrays): Filled contour data as returned by
                :func:`~contourpy.ContourGenerator.filled`.
            fill_type (FillType): Type of ``filled`` data, as returned by
                :attr:`~contourpy.ContourGenerator.fill_type`.
            ax (int or Bokeh Figure, optional): Which plot to use, default ``0``.
            color (str, optional): Color to plot with. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``Category10`` palette. Default ``"C0"``.
            alpha (float, optional): Opacity to plot with, default ``0.7``.
        r   )ÚxsÚysr*   Z
fill_alphaÚ
line_widthN)r.   r,   r   ÚlenZmulti_polygons)	r   ÚfilledZ	fill_typer-   r*   Úalphar"   r5   r6   r#   r#   r$   r9   G   s
    

zBokehRenderer.filledÚblackçš™™™™™¹?Nc              
   C   sŒ  |   |¡}|  ||¡\}}dd„ |D ƒdd„ |jD ƒ }	dd„ |D ƒdd„ |jD ƒ }
t||d�}|j|	|
fi |¤Ž |dk�r`d|dd…dd…f |d	d…dd…f  |dd…d	d…f  |d	d…d	d…f    ¡ }d|dd…dd…f |d	d…dd…f  |dd…d	d…f  |d	d…d	d…f    ¡ }|jd
d„ tj|dd…dd…f  ¡ ||d	d…d	d…f  ¡ fd	d�D ƒdd„ tj|dd…dd…f  ¡ ||d	d…d	d…f  ¡ fd	d�D ƒfi |¤Ž |jdd„ tj|dd…d	d…f  ¡ ||d	d…dd…f  ¡ fd	d�D ƒdd„ tj|dd…d	d…f  ¡ ||d	d…dd…f  ¡ fd	d�D ƒfi |¤Ž |du�rˆ|j| ¡ | ¡ |d|dd� dS )a  Plot quad grid lines on a single plot.

        Args:
            x (array-like of shape (ny, nx) or (nx,)): The x-coordinates of the grid points.
            y (array-like of shape (ny, nx) or (ny,)): The y-coordinates of the grid points.
            ax (int or Bokeh Figure, optional): Which plot to use, default ``0``.
            color (str, optional): Color to plot grid lines, default ``"black"``.
            alpha (float, optional): Opacity to plot lines with, default ``0.1``.
            point_color (str, optional): Color to plot grid points or ``None`` if grid points
                should not be plotted, default ``None``.
            quad_as_tri_alpha (float, optional): Opacity to plot ``quad_as_tri`` grid, default
                ``0``.

        Colors may be a string color or the letter ``"C"`` followed by an integer in the range
        ``"C0"`` to ``"C9"`` to use a color from the ``Category10`` palette.

        Warning:
            ``quad_as_tri_alpha > 0`` plots all quads as though they are unmasked.
        c                 S   s   g | ]}|‘qS r#   r#   ©Ú.0Úrowr#   r#   r$   Ú
<listcomp>q   ó    z&BokehRenderer.grid.<locals>.<listcomp>c                 S   s   g | ]}|‘qS r#   r#   r=   r#   r#   r$   r@   r   rA   )Ú
line_colorr:   r   g      Ð?Néÿÿÿÿr
   c                 S   s   g | ]}|‘qS r#   r#   r=   r#   r#   r$   r@   z   rA   )r   c                 S   s   g | ]}|‘qS r#   r#   r=   r#   r#   r$   r@   {   rA   c                 S   s   g | ]}|‘qS r#   r#   r=   r#   r#   r$   r@   ~   rA   c                 S   s   g | ]}|‘qS r#   r#   r=   r#   r#   r$   r@      rA   é   )r0   r1   Ú
fill_colorrB   r:   Úsize)	r.   r2   ÚTÚdictÚ
multi_lineZravelr   ÚstackÚcircle)r   r0   r1   r-   r*   r:   Zpoint_colorZquad_as_tri_alphar"   r5   r6   ÚkwargsZxmidZymidr#   r#   r$   Úgrid[   s2    

XXDDþýDDþý
ÿzBokehRenderer.gridç      ð?c           
      C   sF   |   |¡}|  |¡}t||ƒ\}}	t|ƒdkrB|j||	|||d� dS )aŒ  Plot contour lines on a single plot.

        Args:
            lines (sequence of arrays): Contour line data as returned by
                :func:`~contourpy.ContourGenerator.lines`.
            line_type (LineType): Type of ``lines`` data, as returned by
                :attr:`~contourpy.ContourGenerator.line_type`.
            ax (int or Bokeh Figure, optional): Which plot to use, default ``0``.
            color (str, optional): Color to plot lines. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``Category10`` palette. Default ``"C0"``.
            alpha (float, optional): Opacity to plot lines with, default ``1.0``.
            linewidth (float, optional): Width of lines, default ``1``.

        Note:
            Assumes all lines are open line strips not closed line loops.
        r   )rB   Z
line_alphar7   N)r.   r,   r   r8   rI   )
r   ÚlinesÚ	line_typer-   r*   r:   Z	linewidthr"   r5   r6   r#   r#   r$   rO   …   s
    

zBokehRenderer.linesc                 C   s^   t j |¡}|t jju rdS |  |¡}|  |¡}|  ||¡\}}|j|| || |dd� dS )aÛ  Plot masked out grid points as circles on a single plot.

        Args:
            x (array-like of shape (ny, nx) or (nx,)): The x-coordinates of the grid points.
            y (array-like of shape (ny, nx) or (ny,)): The y-coordinates of the grid points.
            z (masked array of shape (ny, nx): z-values.
            ax (int or Bokeh Figure, optional): Which plot to use, default ``0``.
            color (str, optional): Circle color, default ``"black"``.
        Nr   )rE   rF   )r   ÚmaZgetmaskZnomaskr.   r,   r2   rK   )r   r0   r1   Úzr-   r*   Úmaskr"   r#   r#   r$   rS   �   s    


zBokehRenderer.maskc                 C   sD   |r| j D ]}d|_d|_q
| jr2t| j|d� nt| j|d� dS )aO  Save plots to SVG or PNG file.

        Args:
            filename (str): Filename to save to.
            transparent (bool, optional): Whether background should be transparent, default
                ``False``.

        Warning:
            To output to SVG file, ``want_svg=True`` must have been passed to the constructor.
        N)Úfilename)r   Zbackground_fill_colorZborder_fill_colorr   r   r   r   )r   rT   Ztransparentr"   r#   r#   r$   Úsave¯   s    
zBokehRenderer.savec                 C   s"   t | jƒ}t ¡ }| |d¡ |S )zhSave plots to an ``io.BytesIO`` buffer.

        Return:
            BytesIO: PNG image buffer.
        Zpng)r   r   ÚioÚBytesIOrU   )r   ÚimageÚbufferr#   r#   r$   Úsave_to_bufferÄ   s    
zBokehRenderer.save_to_bufferc                 C   s   t | jƒ dS )z:Show plots in web browser, in usual Bokeh manner.
        N)r   r   )r   r#   r#   r$   r   Ï   s    zBokehRenderer.showc                 C   s2   |   |¡}||_d|j_|dur.|  |¡|j_dS )aÌ  Set the title of a single plot.

        Args:
            title (str): Title text.
            ax (int or Bokeh Figure, optional): Which plot to set the title of, default ``0``.
            color (str, optional): Color to set title. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``Category10`` palette. Default ``None`` which is ``black``.
        ÚcenterN)r.   ÚtitleZalignr,   Ú
text_color)r   r\   r-   r*   r"   r#   r#   r$   r\   Ô   s
    

zBokehRenderer.titleÚgreenú.1fc                 C   sR  |   |¡}|  |¡}|  ||¡\}}t |¡}|j\}	}
t|ddd�}t|	ƒD ]L}t|
ƒD ]>}| t	f |||f |||f |||f |› ›dœ|¤Ž¡ qZqN|�rNt|	d ƒD ]ž}t|
d ƒD ]Œ}t 
|||d …||d …f ¡}t 
|||d …||d …f ¡}t 
|||d …||d …f ¡}| t	f ||||› ›dœ|¤Ž¡ q¾q®dS )a­  Show ``z`` values on a single plot.

        Args:
            x (array-like of shape (ny, nx) or (nx,)): The x-coordinates of the grid points.
            y (array-like of shape (ny, nx) or (ny,)): The y-coordinates of the grid points.
            z (array-like of shape (ny, nx): z-values.
            ax (int or Bokeh Figure, optional): Which plot to use, default ``0``.
            color (str, optional): Color of added text. May be a string color or the letter ``"C"``
                followed by an integer in the range ``"C0"`` to ``"C9"`` to use a color from the
                ``Category10`` palette. Default ``"green"``.
            fmt (str, optional): Format to display z-values, default ``".1f"``.
            quad_as_tri (bool, optional): Whether to show z-values at the ``quad_as_tri`` centres
                of quads.

        Warning:
            ``quad_as_tri=True`` shows z-values for all quads, even if masked.
        r[   Zmiddle)r]   Z
text_alignZtext_baseline)r0   r1   Útextr
   é   N)r.   r,   r2   r   r   ÚshaperH   r   Z
add_layoutr   Úmean)r   r0   r1   rR   r-   r*   ÚfmtZquad_as_trir"   ÚnyZnxrL   ÚjÚiÚxxÚyyÚzzr#   r#   r$   Úz_valuesä   s     



>"""zBokehRenderer.z_values)r
   r
   r   TF)r   r3   r4   )r   r;   r<   Nr   )r   r3   rN   r
   )r   r;   )F)r   N)r   r^   r_   F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r,   r.   r2   r9   rM   rO   rS   rU   rZ   r   r\   rk   r#   r#   r#   r$   r      s   


*



r   )rV   Zbokeh.ior   r   r   Zbokeh.io.exportr   Zbokeh.layoutsr   Zbokeh.modelsr   Zbokeh.palettesr   Zbokeh.plottingr	   Únumpyr   Z
bokeh_utilr   r   r   r#   r#   r#   r$   Ú<module>   s   