a
    MˆcÎK  ã                   @   s~   d dl 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mZ G dd„ dƒZG dd„ deƒZG d	d
„ d
eƒZdS )é    N)ÚFillTypeÚLineTypeé   )Úfilled_to_mpl_pathsÚlines_to_mpl_pathsÚmpl_codes_to_offsetsc                   @   s�   e Zd ZdZd+dd„Z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d0dd „Zd!d"„ Zd#d$„ Zd1d%d&„Zd2d)d*„ZdS )3ÚMplRenderera£  Utility renderer using Matplotlib 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, default ``(9, 9)``.
        show_frame (bool, optional): Whether to show frame and axes ticks, default ``True``.
        backend (str, optional): Matplotlib backend to use or ``None`` for default backend.
            Default ``None``.
        gridspec_kw (dict, optional): Gridspec keyword arguments to pass to ``plt.subplots``,
            default None.
    r   ©é	   r
   TNc                 C   s’   |d urdd l }| |¡ t|dddd�}|d ur<||d< ntdd�|d< tj||fi |¤Ž\| _}	|	 ¡ | _|sˆ| jD ]}
|
 d	¡ qxd| _	d S )
Nr   FT)ÚfigsizeZsqueezeZsharexZshareyÚgridspec_kwÚequal)ZaspectZ
subplot_kwÚoff)
Ú
matplotlibZuseÚdictÚpltZsubplotsÚ_figÚflattenÚ_axesZaxisÚ_want_tight)ÚselfÚnrowsÚncolsr   Ú
show_frameÚbackendr   r   ÚkwargsZaxesÚax© r   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/contourpy/util/mpl_renderer.pyÚ__init__   s    



zMplRenderer.__init__c                 C   s   t | dƒrt | j¡ d S )Nr   )Úhasattrr   Úcloser   ©r   r   r   r   Ú__del__.   s    
zMplRenderer.__del__c                 C   sL   | j D ]"}t|ddƒr|jdd� d|_q| jrHt| j ƒdkrH| j ¡  d S )NÚ_need_autoscaleFT)Ztightr   )r   ÚgetattrZautoscale_viewr$   r   Úlenr   Ztight_layout©r   r   r   r   r   Ú
_autoscale2   s    
zMplRenderer._autoscalec                 C   s   t |tƒr| j| }|S ©N)Ú
isinstanceÚintr   r'   r   r   r   Ú_get_ax=   s    

zMplRenderer._get_axc                 C   s6   t  |¡}t  |¡}|jdkr.t  ||¡\}}||fS )Nr   )ÚnpÚasarrayÚndimZmeshgrid)r   ÚxÚyr   r   r   Ú_grid_as_2dB   s
    


zMplRenderer._grid_as_2dr   ÚC0çffffffæ?c                 C   s<   |   |¡}t||ƒ}tj||dd|d�}| |¡ d|_dS )aí  Plot filled contours on a single Axes.

        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 Maplotlib Axes, optional): Which axes to plot on, 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
                ``tab10`` colormap. Default ``"C0"``.
            alpha (float, optional): Opacity to plot with, default ``0.7``.
        Únoner   ©Z
facecolorsZ
edgecolorsZlwÚalphaTN)r,   r   ÚmcollectionsÚPathCollectionÚadd_collectionr$   )r   ÚfilledÚ	fill_typer   Úcolorr7   ÚpathsÚ
collectionr   r   r   r;   I   s    


ÿ
zMplRenderer.filledÚblackçš™™™™™¹?c                 C   s  |   |¡}|  ||¡\}}t||d�}|j|||j|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   }
||d< |jt |dd…dd…f |	|dd…dd…f f¡ d¡t |dd…dd…f |
|dd…dd…f f¡ d¡t |dd…dd…f |	|dd…dd…f f¡ d¡t |dd…dd…f |
|dd…dd…f f¡ d¡fi |¤Ž |du�rú|j||||d	d
� d|_	dS )aü  Plot quad grid lines on a single Axes.

        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 Matplotlib Axes, optional): Which Axes to plot on, 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 ``tab10`` colormap.

        Warning:
            ``quad_as_tri_alpha > 0`` plots all quads as though they are unmasked.
        )r=   r7   r   g      Ð?Néÿÿÿÿr   r7   )é   rB   Úo)r=   r7   ÚmarkerT)
r,   r2   r   ÚplotÚTr-   ÚstackZreshapeZscatterr$   )r   r0   r1   r   r=   r7   Úpoint_colorZquad_as_tri_alphar   ÚxmidÚymidr   r   r   Úgrid^   s&    

TT4444üû
zMplRenderer.gridç      ð?c           	      C   s<   |   |¡}t||ƒ}tj|d|||d�}| |¡ d|_dS )a6  Plot contour lines on a single Axes.

        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 Matplotlib Axes, optional): Which Axes to plot on, 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
                ``tab10`` colormap. Default ``"C0"``.
            alpha (float, optional): Opacity to plot lines with, default ``1.0``.
            linewidth (float, optional): Width of lines, default ``1``.
        r5   r6   TN)r,   r   r8   r9   r:   r$   )	r   ÚlinesÚ	line_typer   r=   r7   Ú	linewidthr>   r?   r   r   r   rN   „   s    


ÿ
zMplRenderer.linesc                 C   sT   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 Axes.

        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 Matplotlib Axes, optional): Which Axes to plot on, default ``0``.
            color (str, optional): Circle color, default ``"black"``.
        NrD   )Úc)r-   ÚmaZgetmaskZnomaskr,   r2   rF   )r   r0   r1   Úzr   r=   Úmaskr   r   r   rT   š   s    

zMplRenderer.maskFc                 C   s   |   ¡  | jj||d� dS )zÞSave plots to SVG or PNG file.

        Args:
            filename (str): Filename to save to.
            transparent (bool, optional): Whether background should be transparent, default
                ``False``.
        )ÚtransparentN)r(   r   Úsavefig)r   ÚfilenamerU   r   r   r   Úsave«   s    zMplRenderer.savec                 C   s.   |   ¡  t ¡ }| jj|dd� | d¡ |S )zhSave plots to an ``io.BytesIO`` buffer.

        Return:
            BytesIO: PNG image buffer.
        Zpng)Úformatr   )r(   ÚioÚBytesIOr   rV   Úseek)r   Úbufr   r   r   Úsave_to_buffer¶   s
    
zMplRenderer.save_to_bufferc                 C   s   |   ¡  t ¡  dS )zMShow plots in an interactive window, in the usual Matplotlib manner.
        N)r(   r   Úshowr"   r   r   r   r_   Â   s    zMplRenderer.showc                 C   s.   |r|   |¡j||d� n|   |¡ |¡ dS )aË  Set the title of a single Axes.

        Args:
            title (str): Title text.
            ax (int or Matplotlib Axes, optional): Which Axes 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
                ``tab10`` colormap. Default ``None`` which is ``black``.
        )r=   N)r,   Ú	set_title)r   Útitler   r=   r   r   r   ra   È   s    
zMplRenderer.titleÚgreenú.1fc                 C   s2  |   |¡}|  ||¡\}}t |¡}|j\}}	t|ƒD ]H}
t|	ƒD ]:}|j||
|f ||
|f ||
|f |› ›dd|dd� qBq6|�r.t|d ƒD ]š}
t|	d ƒD ]ˆ}t ||
|
d …||d …f ¡}t ||
|
d …||d …f ¡}t ||
|
d …||d …f ¡}|j||||› ›dd|dd� q¢q’dS )a°  Show ``z`` values on a single Axes.

        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 Matplotlib Axes, optional): Which Axes to plot on, 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
                ``tab10`` colormap. 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`` centers
                of quads.

        Warning:
            ``quad_as_tri=True`` shows z-values for all quads, even if masked.
        ÚcenterT©ZhaÚvar=   Zclip_onr   é   N)r,   r2   r-   r.   ÚshapeÚrangeÚtextÚmean)r   r0   r1   rS   r   r=   ÚfmtZquad_as_triÚnyÚnxÚjÚiÚxxÚyyÚzzr   r   r   Úz_values×   s$    


,ÿ
"""ÿzMplRenderer.z_values)r   r   r	   TNN)r   r3   r4   )r   r@   rA   Nr   )r   r3   rM   r   )r   r@   )F)r   N)r   rb   rc   F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r(   r,   r2   r;   rL   rN   rT   rX   r^   r_   ra   rt   r   r   r   r   r      s     ÿ


&



r   c                       s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚMplTestRendererz¾Test renderer implemented using Matplotlib.

    No whitespace around plots and no spines/ticks displayed.
    Uses Agg backend, so can only save to file/buffer, cannot call ``show()``.
    r   r	   c                    sh   dddddddœ}t ƒ j|||dd|d� | jD ],}| d¡ | d¡ | g ¡ | g ¡ q0d| _d S )	Ng{®Gáz„?g®Gáz®ï?)ÚleftÚrightÚtopÚbottomZwspaceZhspaceTZAgg)r   r   r   ç        F)Úsuperr   r   Zset_xmarginZset_ymarginZ
set_xticksZ
set_yticksr   )r   r   r   r   Zgridspecr   ©Ú	__class__r   r   r     s     úÿ



zMplTestRenderer.__init__)r   r   r	   )ru   rv   rw   rx   r   Ú__classcell__r   r   r€   r   ry   û   s   ry   c                       sd   e Zd ZdZd‡ fdd„	Zdd„ Zd‡ fdd„	Zd‡ fdd„	Zd dd„Zd!dd„Z	d"dd„Z
‡  ZS )#ÚMplDebugRendererz¤Debug renderer implemented using Matplotlib.

    Extends ``MplRenderer`` to add extra information to help in debugging such as markers, arrows,
    text, etc.
    r   r	   Tc                    s   t ƒ  ||||¡ d S r)   )r   r   )r   r   r   r   r   r€   r   r   r     s    zMplDebugRenderer.__init__c                 C   s®   d||  }|| }|t  t  ||¡¡ }t  |d |d  f¡}	t  ||d |	 |  ||d |  ||d |	 |  f¡}
|j|
d d …df |
d d …df d||d� d S )Ng      à?r   r   ú-©rQ   r7   )r-   ÚsqrtÚdotr.   rH   rF   )r   r   Z
line_startZline_endr=   r7   Ú
arrow_sizeZmidZalongr{   Zarrowr   r   r   Ú_arrow   s    ýzMplDebugRenderer._arrowr   ÚC1r4   r3   ÚredrA   c                    s–  t ƒ  |||||¡ |d u r(|d u r(d S |  |¡}|tjkrZ|d }dd„ |d D ƒ}�n |tjkrŒdd„ |d D ƒ}dd„ |d D ƒ}�nn|tjkrª|d }|d }�nP|tjkrÜdd„ |d D ƒ}dd„ |d D ƒ}�n|tjk�rPg }g }t	|Ž D ]T\}}}|d u �rqø|t
 ||dd	… ¡7 }t
 ||dd	… ¡}|d
d„ |D ƒ7 }qønª|tjk�rêg }g }t	|Ž D ]z\}}}|d u �r„�qltt|ƒd ƒD ]N}||| ||d  d … }| ||d |d	 … ¡ | ||d  ¡ �q”�qlntd|› d�ƒ‚|d u�r¼t	||ƒD ]¬\}}t	|d d	… |dd … ƒD ]†\}}|||… }|j|d d …df |d d …df ||d� |
dk�r0t|ƒ}t|d ƒD ]&}|  ||| ||d  |||
¡ �qŒ�q0�q|d u�r’t	||ƒD ]À\}}t
j|d	 td�}d||dd … d < |	d u�r|d d	… }d||< |j|d d …df | |d d …df | d||d� |	d u�rÐ|j|d d …df | |d d …df | d|	|d� �qÐd S )Nr   c                 S   s   g | ]}t |ƒ‘qS r   ©r   ©Ú.0Úcodesr   r   r   Ú
<listcomp>7  ó    z+MplDebugRenderer.filled.<locals>.<listcomp>r   c                 S   s   g | ]}|d ur|‘qS r)   r   ©rŽ   Úpointsr   r   r   r�   9  r‘   c                 S   s   g | ]}|d urt |ƒ‘qS r)   rŒ   r�   r   r   r   r�   :  r‘   c                 S   s   g | ]}|d ur|‘qS r)   r   r’   r   r   r   r�   ?  r‘   c                 S   s   g | ]}|d ur|‘qS r)   r   )rŽ   Úoffsetsr   r   r   r�   @  r‘   rB   c                 S   s   g | ]}t |ƒ‘qS r   rŒ   r�   r   r   r   r�   I  r‘   zRendering FillType ú not implementedr…   r~   )ZdtypeFrD   )r   r;   r,   r   Z	OuterCodeÚChunkCombinedCodeZOuterOffsetÚChunkCombinedOffsetZChunkCombinedCodeOffsetÚzipr-   ÚsplitZChunkCombinedOffsetOffsetri   r&   ÚappendÚRuntimeErrorrF   r‰   ZonesÚbool)r   r;   r<   r   r=   r7   Z
line_colorZ
line_alpharI   Ústart_point_colorrˆ   Z
all_pointsZall_offsetsr“   r�   Zouter_offsetsr”   rp   ZoffsÚstartÚendZxysÚnrT   Zstart_indicesr€   r   r   r;   ,  sv    







"*
,

*ÿ
*ÿzMplDebugRenderer.filledrM   c
              
      s4  t ƒ  ||||||¡ |	dkr*|d u r*d S |  |¡}|tjkrD|}
nê|tjkrX|d }
nÖ|tjkr¾g }
t|Ž D ]L\}}|d urnt|ƒ}t	t
|ƒd ƒD ]"}|
 ||| ||d  … ¡ q–qnnp|tjk�rg }
t|Ž D ]D\}}|d urÖt	t
|ƒd ƒD ]"}|
 ||| ||d  … ¡ qöqÖntd|› d�ƒ‚|	dk�r||
D ]>}t	t
|ƒd ƒD ]&}|  ||| ||d  |||	¡ �qP�q<|d u�r0|
D ]¤}d}t
|ƒ}|d u�r |j|d |d d||d	� d}|d d |d
 d k�r |d d |d
 d k�r |d8 }|j|||…df |||…df d||d	� �qŠd S )Nr~   r   r   zRendering LineType r•   )r   r   )r   r   rD   r…   rB   )r   rN   r,   r   ZSeparateZSeparateCoder–   r˜   r   ri   r&   rš   r—   r›   r‰   rF   )r   rN   rO   r   r=   r7   rP   rI   r�   rˆ   Z	all_linesr“   r�   r”   rp   ÚlineÚstart_indexZ	end_indexr€   r   r   rN   r  sL    




$$
(

4"ÿzMplDebugRenderer.linesc                 C   s†   |   |¡}|  ||¡\}}t |¡}|j\}}t|ƒD ]J}t|ƒD ]<}	|	||  }
|j|||	f |||	f t|
ƒdd|dd� qBq6d S )Nr{   r|   Tre   )r,   r2   r-   r.   rh   ri   rj   Ústr)r   r0   r1   rS   r   r=   rm   rn   ro   rp   Úquadr   r   r   Úpoint_numbers   s    


$ÿzMplDebugRenderer.point_numbersÚbluec                 C   sÊ   |   |¡}|  ||¡\}}t |¡}|j\}}td|ƒD ]Œ}td|ƒD ]|}	|	||  }
||d |d …|	d |	d …f  ¡ }||d |d …|	d |	d …f  ¡ }|j||t|
ƒdd|dd� qFq8d S )Nr   rd   Tre   )	r,   r2   r-   r.   rh   ri   rk   rj   r£   )r   r0   r1   rS   r   r=   rm   rn   ro   rp   r¤   rJ   rK   r   r   r   Úquad_numbers«  s    


((zMplDebugRenderer.quad_numbersNrb   c                 C   sª   |   |¡}|  ||¡\}}t |¡}|j\}}	t|ƒD ]n}
t|	ƒD ]`}||
|f }|d urh||krhd}n||krvd}nd}|j||
|f ||
|f |dd|dd� qBq6d S )Nrg   r   r   rz   r}   Tre   )r,   r2   r-   r.   rh   ri   rj   )r   r0   r1   rS   Zlower_levelZupper_levelr   r=   rm   rn   ro   rp   rs   Zz_levelr   r   r   Úz_levels·  s    


 ÿzMplDebugRenderer.z_levels)r   r   r	   T)r   rŠ   r4   r3   r4   r3   r‹   rA   )r   r3   rM   r   r3   r‹   rA   )r   r‹   )r   r¦   )Nr   rb   )ru   rv   rw   rx   r   r‰   r;   rN   r¥   r§   r¨   r‚   r   r   r€   r   rƒ     s     ÿF  ÿ.

rƒ   )rZ   Zmatplotlib.collectionsÚcollectionsr8   Zmatplotlib.pyplotZpyplotr   Únumpyr-   Z	contourpyr   r   Zmpl_utilr   r   r   r   ry   rƒ   r   r   r   r   Ú<module>   s    p