a
    |=ic8-                     @   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xgridvisibleZygridappendZoutline_line_coloraxisr   _layout)selfZnrowsr   ZfigsizeZ
show_frameZwant_svg
total_sizeZnfiguresbackend_fig r%   n/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/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   C0ffffff?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)r0   r.   r   lenZmulti_polygons)	r    filledZ	fill_typer/   r,   alphar$   r7   r8   r%   r%   r&   r;   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&   rB   r   rC   )
line_colorr<   r   g      ?Nr
   c                 S   s   g | ]}|qS r%   r%   r?   r%   r%   r&   rB   z   rC   )r   c                 S   s   g | ]}|qS r%   r%   r?   r%   r%   r&   rB   {   rC   c                 S   s   g | ]}|qS r%   r%   r?   r%   r%   r&   rB   ~   rC   c                 S   s   g | ]}|qS r%   r%   r?   r%   r%   r&   rB      rC      )r2   r3   
fill_colorrD   r<   size)	r0   r4   Tdict
multi_lineZravelr   stackcircle)r    r2   r3   r/   r,   r<   Zpoint_colorZquad_as_tri_alphar$   r7   r8   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   )rD   Z
line_alphar9   N)r0   r.   r   r:   rK   )
r    lines	line_typer/   r,   r<   Z	linewidthr$   r7   r8   r%   r%   r&   rQ      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   )rG   rH   )r   maZgetmaskZnomaskr0   r.   r4   rM   )r    r2   r3   zr/   r,   maskr$   r%   r%   r&   rU      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    rV   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BytesIOrX   )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)r0   title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| qqd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^   middle)ra   Z
text_alignZtext_baseline)r2   r3   textr
      N)r0   r.   r4   r   r   shaperJ   r   Z
add_layoutr   mean)r    r2   r3   rT   r/   r,   fmtZquad_as_trir$   nynxrN   jixxyyzzr%   r%   r&   z_values   s     



>"""zBokehRenderer.z_values)r
   r
   r   TF)r   r5   r6   )r   r=   r>   Nr   )r   r5   rP   r
   )r   r=   )F)r   N)r   rb   rc   F)__name__
__module____qualname____doc__r'   r.   r0   r4   r;   rO   rQ   rU   rX   r]   r   r_   rq   r%   r%   r%   r&   r      s   


*



r   )rY   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   