U
    ½mœ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rà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)Ú
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xgridÚvisibleZygridÚappendZoutline_line_colorÚaxisr	   r   )Úselfr"   r#   r$   r%   r&   Ú
total_sizeZnfiguresÚbackendÚ_Úfig© r8   úV/home/sam/Atlas/atlas_env/lib/python3.8/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   )Ú
isinstancer;   r    r   )r3   r<   Úindexr8   r8   r9   Ú_convert_colorM   s    
zBokehRenderer._convert_colorzfigure | intr   )Úaxr'   c                 C  s   t |tƒr| j| }|S )N)r>   r    r   )r3   rA   r8   r8   r9   Ú_get_figureS   s    

zBokehRenderer._get_figurer   ÚC0çffffffæ?r   r   Úfloat)ÚfilledÚ	fill_typerA   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)rB   r@   r   ÚlenZmulti_polygons)	r3   rF   rG   rA   r<   rH   r7   rI   rJ   r8   r8   r9   rF   X   s
    

zBokehRenderer.filledÚblackçš™™™™™¹?Nr   z
str | None)ÚxÚyrA   r<   rH   Ú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 r8   r8   ©Ú.0Úrowr8   r8   r9   Ú
<listcomp>’   s     z&BokehRenderer.grid.<locals>.<listcomp>c                 S  s   g | ]}|‘qS r8   r8   rS   r8   r8   r9   rV   “   s     )Ú
line_colorrH   r   g      Ð?Néÿÿÿÿr   c                 S  s   g | ]}|‘qS r8   r8   rS   r8   r8   r9   rV   ›   s     )r2   c                 S  s   g | ]}|‘qS r8   r8   rS   r8   r8   r9   rV   œ   s     c                 S  s   g | ]}|‘qS r8   r8   rS   r8   r8   r9   rV   Ÿ   s     c                 S  s   g | ]}|‘qS r8   r8   rS   r8   r8   r9   rV       s     é   )rO   rP   Ú
fill_colorrW   rH   Úsize)	rB   Ú_grid_as_2dÚTÚdictÚ
multi_lineZravelr-   ÚstackÚcircle)r3   rO   rP   rA   r<   rH   rQ   rR   r7   rI   rJ   ÚkwargsZxmidZymidr8   r8   r9   Úgrids   s<    

XXDDþýDDþý
     ÿzBokehRenderer.gridç      ð?r   r   )ÚlinesÚ	line_typerA   r<   rH   Ú	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   )rW   Z
line_alpharK   N)rB   r@   r   rL   r_   )
r3   re   rf   rA   r<   rH   rg   r7   rI   rJ   r8   r8   r9   re   ¦   s
    

zBokehRenderer.linesz'ArrayLike | np.ma.MaskedArray[Any, Any])rO   rP   ÚzrA   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(   )rZ   r[   )r-   ÚmaZgetmaskZnomaskrB   r@   r\   ra   )r3   rO   rP   rh   rA   r<   Úmaskr7   r8   r8   r9   rj   Æ   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)rk   )r   Zbackground_fill_colorZborder_fill_colorr   r   r   r   )r3   rk   rl   r7   r8   r8   r9   Ú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ÚBytesIOrm   )r3   ÚimageÚbufferr8   r8   r9   Ú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   )r3   r8   r8   r9   r   ÿ   s    zBokehRenderer.show)ÚtitlerA   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)rB   rs   Úalignr@   Ú
text_color)r3   rs   rA   r<   r7   r8   r8   r9   rs     s
    

zBokehRenderer.titleÚgreenú.1f)rO   rP   rh   rA   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œ|—Ž¡ 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.
        rt   Úmiddle)rv   Z
text_alignZtext_baseline)rO   rP   Útextr   é   N)rB   r@   r\   r-   r.   Úshaper^   r/   Z
add_layoutr
   Zmean)r3   rO   rP   rh   rA   r<   ry   rz   r7   ÚnyÚnxrb   ÚjÚiZxxÚyyÚzzr8   r8   r9   Úz_values  s     



>"""zBokehRenderer.z_values)r   r   r   TF)r   rC   rD   )r   rM   rN   Nr   )r   rC   rd   r   )r   rM   )F)r   N)r   rw   rx   F)Ú__name__Ú
__module__Ú__qualname__Ú__annotations__r:   r@   rB   rF   rc   re   rj   rm   rr   r   rs   r…   r8   r8   r8   r9   r      sN   
     ú	   ú     ø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   Ú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   r8   r8   r8   r9   Ú<module>   s$   