U
    i2e4                     @  s   d dl mZ d dlZd dlmZm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 d dlmZmZ d dlmZ erd dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z&m'Z' G dd deZ(dS )    )annotationsN)TYPE_CHECKINGAny)
export_png
export_svgshow)get_screenshot_as_png)gridplot)Label)
Category10)figure)FillTypeLineType)filled_to_bokehlines_to_bokeh)Renderer)GridPlot)Palette)	ArrayLike)	WebDriver)
FillReturn
LineReturnc                
   @  sJ  e Zd ZU ded< ded< ded< ded< dKdddddddddZdddddZdddddZdLdd ddd!dd"d#d$ZdMd(d(ddd!d)d!dd*d+d,ZdNd.d/ddd!d!dd0d1d2Z	dOd(d(d3dddd4d5d6Z
dPd'd7ddd8dd9d:d;Zd'd7d8d<d=d>d?Zdd@dAdBZdQddd)ddCdDdEZdRd(d(d(ddddddHdIdJZd'S )SBokehRendererzlist[figure]_figuresr   _layoutr   _palettebool	_want_svg   	   r    TFintztuple[float, float]None)nrowsncolsfigsize
show_framewant_svgreturnc                 C  s   || _ td | _dtj|td }|| }g | _| j r:dnd}t|D ]<}	t|d}
d|
j	_
d|
j_
| j|
 |sFd |
_d|
j_
qFt| j|d |d | |d	 | d
| _d S )N
   d   )ZdtypesvgZcanvas)Zoutput_backendFr   r   )r$   Ztoolbar_locationwidthheight)r   r   r   npasarrayr!   r   ranger   ZxgridZvisibleZygridappendZoutline_line_coloraxisr	   r   )selfr#   r$   r%   r&   r'   Z
total_sizeZnfiguresbackend_fig r7   A/tmp/pip-unpacked-wheel-_24pu9r8/contourpy/util/bokeh_renderer.py__init__1   s*    


  
 
zBokehRenderer.__init__str)colorr(   c                 C  s4   t |tr0|d dkr0t|dd  }| j| }|S )Nr   Cr   )
isinstancer:   r!   r   )r3   r;   indexr7   r7   r8   _convert_colorN   s    
zBokehRenderer._convert_colorzfigure | intr   )axr(   c                 C  s   t |tr| j| }|S )N)r=   r!   r   )r3   r@   r7   r7   r8   _get_figureT   s    

zBokehRenderer._get_figurer   C0ffffff?r   r   float)filled	fill_typer@   r;   alphar(   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)rA   r?   r   lenZmulti_polygons)	r3   rE   rF   r@   r;   rG   r6   rH   rI   r7   r7   r8   rE   Y   s
    

zBokehRenderer.filledblack皙?Nr   z
str | None)xyr@   r;   rG   point_colorquad_as_tri_alphar(   c              
   C  s  |  |}| ||\}}dd |D dd |jD  }	dd |D dd |jD  }
t||d}|j|	|
f| |dkrTd|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| |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| |dk	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 r7   r7   .0rowr7   r7   r8   
<listcomp>   s     z&BokehRenderer.grid.<locals>.<listcomp>c                 S  s   g | ]}|qS r7   r7   rR   r7   r7   r8   rU      s     )
line_colorrG   r   g      ?Nr   c                 S  s   g | ]}|qS r7   r7   rR   r7   r7   r8   rU      s     )r2   c                 S  s   g | ]}|qS r7   r7   rR   r7   r7   r8   rU      s     c                 S  s   g | ]}|qS r7   r7   rR   r7   r7   r8   rU      s     c                 S  s   g | ]}|qS r7   r7   rR   r7   r7   r8   rU      s        )rN   rO   
fill_colorrV   rG   size)	rA   _grid_as_2dTdict
multi_lineZravelr.   stackcircle)r3   rN   rO   r@   r;   rG   rP   rQ   r6   rH   rI   kwargsZxmidZymidr7   r7   r8   gridt   s<    

XXDDDD
     zBokehRenderer.grid      ?r   r   )lines	line_typer@   r;   rG   	linewidthr(   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   )rV   Z
line_alpharJ   N)rA   r?   r   rK   r^   )
r3   rd   re   r@   r;   rG   rf   r6   rH   rI   r7   r7   r8   rd      s
    

zBokehRenderer.linesz'ArrayLike | np.ma.MaskedArray[Any, Any])rN   rO   zr@   r;   r(   c                 C  s^   t j|}|t jjk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)   )rY   rZ   )r.   maZgetmaskZnomaskrA   r?   r[   r`   )r3   rN   rO   rg   r@   r;   maskr6   r7   r7   r8   ri      s    

zBokehRenderer.mask)	webdriverzWebDriver | None)filenametransparentrj   r(   c                C  sH   |r| j D ]}d|_d|_q
| jr4t| j||d nt| j||d dS )a  Save plots to SVG or PNG file.

        Args:
            filename (str): Filename to save to.
            transparent (bool, optional): Whether background should be transparent, default
                ``False``.
            webdriver (WebDriver, optional): Selenium WebDriver instance to use to create the image.

        Warning:
            To output to SVG file, ``want_svg=True`` must have been passed to the constructor.
        N)rk   rj   )r   Zbackground_fill_colorZborder_fill_colorr   r   r   r   )r3   rk   rl   rj   r6   r7   r7   r8   save   s    
zBokehRenderer.savez
io.BytesIO)rj   r(   c                C  s&   t | j|d}t }||d |S )zSave plots to an ``io.BytesIO`` buffer.

        Args:
            webdriver (WebDriver, optional): Selenium WebDriver instance to use to create the image.

        Return:
            BytesIO: PNG image buffer.
        )ZdriverZpng)r   r   ioBytesIOrm   )r3   rj   imagebufferr7   r7   r8   save_to_buffer   s    	zBokehRenderer.save_to_buffer)r(   c                 C  s   t | j dS )z:Show plots in web browser, in usual Bokeh manner.
        N)r   r   )r3   r7   r7   r8   r   
  s    zBokehRenderer.show)titler@   r;   r(   c                 C  s2   |  |}||_d|j_|dk	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)rA   rs   Zalignr?   
text_color)r3   rs   r@   r;   r6   r7   r7   r8   rs     s
    

zBokehRenderer.titlegreen.1f)rN   rO   rg   r@   r;   fmtquad_as_trir(   c                 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.
        rt   Zmiddle)ru   Z
text_alignZtext_baseline)rN   rO   textr      N)rA   r?   r[   r.   r/   shaper]   r0   Z
add_layoutr
   Zmean)r3   rN   rO   rg   r@   r;   rx   ry   r6   nyZnxra   jiZxxyyzzr7   r7   r8   z_values  s     



>"""zBokehRenderer.z_values)r   r   r   TF)r   rB   rC   )r   rL   rM   Nr   )r   rB   rc   r   )r   rL   )F)r   N)r   rv   rw   F)__name__
__module____qualname____annotations__r9   r?   rA   rE   rb   rd   ri   rm   rr   r   rs   r   r7   r7   r7   r8   r      sT   
     	        7    %       r   ))
__future__r   rn   typingr   r   Zbokeh.ior   r   r   Zbokeh.io.exportr   Zbokeh.layoutsr	   Zbokeh.models.annotations.labelsr
   Zbokeh.palettesr   Zbokeh.plottingr   Znumpyr.   Z	contourpyr   r   Zcontourpy.util.bokeh_utilr   r   Zcontourpy.util.rendererr   Zbokeh.modelsr   r   Znumpy.typingr   Z#selenium.webdriver.remote.webdriverr   Zcontourpy._contourpyr   r   r   r7   r7   r7   r8   <module>   s&   