a
    
d3                     @  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$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)
FillReturn
LineReturnc                
   @  s:  e Zd ZU ded< ded< ded< ded< dHdddddddddZdddddZdddddZdIdd ddd!dd"d#d$ZdJd(d(ddd!d)d!dd*d+d,ZdKd.d/ddd!d!dd0d1d2Z	dLd(d(d3dddd4d5d6Z
dMdddd7d8d9Zd:d;d<d=Zdd;d>d?ZdNddd)dd@dAdBZdOd(d(d(ddddddEdFdGZd'S )P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 r6   f/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/contourpy/util/bokeh_renderer.py__init__0   s$    


zBokehRenderer.__init__str)colorr'   c                 C  s4   t |tr0|d dkr0t|dd  }| j| }|S )Nr   Cr   )
isinstancer9   r    r   )r2   r:   indexr6   r6   r7   _convert_colorM   s    
zBokehRenderer._convert_colorzfigure | intr   )axr'   c                 C  s   t |tr| j| }|S )N)r<   r    r   )r2   r?   r6   r6   r7   _get_figureS   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)r@   r>   r   lenZmulti_polygons)	r2   rD   rE   r?   r:   rF   r5   rG   rH   r6   r6   r7   rD   X   s
    

zBokehRenderer.filledblack皙?Nr   z
str | None)xyr?   r:   rF   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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 r6   r6   .0rowr6   r6   r7   
<listcomp>       z&BokehRenderer.grid.<locals>.<listcomp>c                 S  s   g | ]}|qS r6   r6   rQ   r6   r6   r7   rT      rU   )
line_colorrF   r   g      ?Nr   c                 S  s   g | ]}|qS r6   r6   rQ   r6   r6   r7   rT      rU   )r1   c                 S  s   g | ]}|qS r6   r6   rQ   r6   r6   r7   rT      rU   c                 S  s   g | ]}|qS r6   r6   rQ   r6   r6   r7   rT      rU   c                 S  s   g | ]}|qS r6   r6   rQ   r6   r6   r7   rT      rU      )rM   rN   
fill_colorrV   rF   size)	r@   _grid_as_2dTdict
multi_lineZravelr-   stackcircle)r2   rM   rN   r?   r:   rF   rO   rP   r5   rG   rH   kwargsZxmidZymidr6   r6   r7   grids   s2    

XXDDDD
zBokehRenderer.grid      ?r   r   )lines	line_typer?   r:   rF   	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_alpharI   N)r@   r>   r   rJ   r^   )
r2   rd   re   r?   r:   rF   rf   r5   rG   rH   r6   r6   r7   rd      s
    

zBokehRenderer.linesz'ArrayLike | np.ma.MaskedArray[Any, Any])rM   rN   zr?   r:   r'   c                 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(   )rY   rZ   )r-   maZgetmaskZnomaskr@   r>   r[   r`   )r2   rM   rN   rg   r?   r:   maskr5   r6   r6   r7   ri      s    

zBokehRenderer.mask)filenametransparentr'   c                 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)rj   )r   Zbackground_fill_colorZborder_fill_colorr   r   r   r   )r2   rj   rk   r5   r6   r6   r7   save   s    
zBokehRenderer.savez
io.BytesIO)r'   c                 C  s"   t | j}t }||d |S )zhSave plots to an ``io.BytesIO`` buffer.

        Return:
            BytesIO: PNG image buffer.
        Zpng)r   r   ioBytesIOrl   )r2   imagebufferr6   r6   r7   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   )r2   r6   r6   r7   r      s    zBokehRenderer.show)titler?   r:   r'   c                 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@   rr   Zalignr>   
text_color)r2   rr   r?   r:   r5   r6   r6   r7   rr     s
    

zBokehRenderer.titlegreen.1f)rM   rN   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.
        rs   Zmiddle)rt   Z
text_alignZtext_baseline)rM   rN   textr      N)r@   r>   r[   r-   r.   shaper]   r/   Z
add_layoutr
   mean)r2   rM   rN   rg   r?   r:   rw   rx   r5   nyZnxra   jixxyyzzr6   r6   r7   z_values  s     



>"""zBokehRenderer.z_values)r   r   r   TF)r   rA   rB   )r   rK   rL   Nr   )r   rA   rc   r   )r   rK   )F)r   N)r   ru   rv   F)__name__
__module____qualname____annotations__r8   r>   r@   rD   rb   rd   ri   rl   rq   r   rr   r   r6   r6   r6   r7   r      sN   
     	        7    %      r   )'
__future__r   rm   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   numpyr-   Z	contourpyr   r   Zcontourpy.util.bokeh_utilr   r   Zcontourpy.util.rendererr   Zbokeh.modelsr   r   Znumpy.typingr   Zcontourpy._contourpyr   r   r   r6   r6   r6   r7   <module>   s$   