a
    #©bk9  ã                   @   s€   d Z ddlZddlZddlZg d¢Zddd„Zdd„ Zdd	„ Zd
d„ Z	ddd„Z
ddd„Zej dd¡ddd„ƒZddd„ZdS )au  
***************
Graphviz AGraph
***************

Interface to pygraphviz AGraph class.

Examples
--------
>>> G = nx.complete_graph(5)
>>> A = nx.nx_agraph.to_agraph(G)
>>> H = nx.nx_agraph.from_agraph(A)

See Also
--------
 - Pygraphviz: http://pygraphviz.github.io/
 - Graphviz:      https://www.graphviz.org
 - DOT Language:  http://www.graphviz.org/doc/info/lang.html
é    N)Úfrom_agraphÚ	to_agraphÚ	write_dotÚread_dotÚgraphviz_layoutÚpygraphviz_layoutÚview_pygraphvizc           	      C   sj  |du r>|   ¡ r(|  ¡ r tj}q>tj}n|  ¡ r8tj}ntj}t d|¡}| jdur\| j|_|j	 
| j¡ |  ¡ D ].}dd„ |j ¡ D ƒ}|jt|ƒfi |¤Ž qr|  ¡ D ]Š}t|d ƒt|d ƒ }}t|jƒ}dd„ | ¡ D ƒ}| ¡ �s|jdu�r|j|d< |j||fi |¤Ž qª|j||fd|ji|¤Ž qªt| jƒ|j	d< t| jƒ|j	d	< t| jƒ|j	d
< |S )aÅ  Returns a NetworkX Graph or DiGraph from a PyGraphviz graph.

    Parameters
    ----------
    A : PyGraphviz AGraph
      A graph created with PyGraphviz

    create_using : NetworkX graph constructor, optional (default=None)
       Graph type to create. If graph instance, then cleared before populated.
       If `None`, then the appropriate Graph type is inferred from `A`.

    Examples
    --------
    >>> K5 = nx.complete_graph(5)
    >>> A = nx.nx_agraph.to_agraph(K5)
    >>> G = nx.nx_agraph.from_agraph(A)

    Notes
    -----
    The Graph G will have a dictionary G.graph_attr containing
    the default graphviz attributes for graphs, nodes and edges.

    Default node attributes will be in the dictionary G.node_attr
    which is keyed by node.

    Edge attributes will be returned as edge data in G.  With
    edge_attr=False the edge data will be the Graphviz edge weight
    attribute or the value 1 if no edge weight attribute is found.

    Nr   c                 S   s   i | ]\}}t |ƒ|“qS © ©Ústr©Ú.0ÚkÚvr	   r	   úc/home/yuhw/wc/wire-cell-python-pr-11/pr11/lib/python3.9/site-packages/networkx/drawing/nx_agraph.pyÚ
<dictcomp>Y   ó    zfrom_agraph.<locals>.<dictcomp>é   c                 S   s   i | ]\}}t |ƒ|“qS r	   r
   r   r	   r	   r   r   `   r   ÚkeyÚgraphÚnodeÚedge)Úis_directedZ	is_strictÚnxZDiGraphZMultiDiGraphZGraphZ
MultiGraphZempty_graphÚnamer   ÚupdateÚ
graph_attrÚnodesÚattrÚitemsÚadd_noder   ÚedgesÚdictÚis_multigraphÚadd_edgeÚ	node_attrÚ	edge_attr)	ÚAZcreate_usingÚNÚnZstr_attrÚeÚur   r   r	   r	   r   r   $   s8    



r   c              
   C   sÌ  zddl }W n. ty: } ztdƒ|‚W Y d}~n
d}~0 0 |  ¡ }t | ¡dkoZ|  ¡  }|j| j||d�}|j 	| j
 di ¡¡ |j 	| j
 di ¡¡ |j 	| j
 di ¡¡ |j 	dd	„ | j
 ¡ D ƒ¡ | jd
d�D ]6\}}| |¡ | |¡}|j 	dd„ | ¡ D ƒ¡ qØ|  ¡ �rx| jd
d
d�D ]L\}	}
}}dd„ | ¡ D ƒ}|j|	|
t|ƒd� | |	|
¡}|j 	|¡ �q(nP| jd
d�D ]B\}	}
}dd„ | ¡ D ƒ}| |	|
¡ | |	|
¡}|j 	|¡ �q„|S )aÃ  Returns a pygraphviz graph from a NetworkX graph N.

    Parameters
    ----------
    N : NetworkX graph
      A graph created with NetworkX

    Examples
    --------
    >>> K5 = nx.complete_graph(5)
    >>> A = nx.nx_agraph.to_agraph(K5)

    Notes
    -----
    If N has an dict N.graph_attr an attempt will be made first
    to copy properties attached to the graph (see from_agraph)
    and then updated with the calling arguments if any.

    r   Nú0requires pygraphviz http://pygraphviz.github.io/)r   ÚstrictÚdirectedr   r   r   c                 s   s"   | ]\}}|d vr||fV  qdS ))r   r   r   Nr	   r   r	   r	   r   Ú	<genexpr>“   s   zto_agraph.<locals>.<genexpr>T©Údatac                 S   s   i | ]\}}|t |ƒ“qS r	   r
   r   r	   r	   r   r   œ   r   zto_agraph.<locals>.<dictcomp>)r1   Úkeysc                 S   s"   i | ]\}}|d kr|t |ƒ“qS ©r   r
   r   r	   r	   r   r   ¡   r   r3   c                 S   s   i | ]\}}|t |ƒ“qS r	   r
   r   r	   r	   r   r   ©   r   )Ú
pygraphvizÚImportErrorr   r   Znumber_of_selfloopsr#   ÚAGraphr   r   r   r   Úgetr%   r&   r   r   r    Zget_noder   r!   r$   r   Úget_edge)r(   r4   Úerrr.   r-   r'   r)   ZnodedataÚar+   r   r   ZedgedataZstr_edgedatar	   r	   r   r   p   sB    ÿþÿ


r   c                 C   s   t | ƒ}| |¡ | ¡  dS )z¼Write NetworkX graph G to Graphviz dot format on path.

    Parameters
    ----------
    G : graph
       A networkx graph
    path : filename
       Filename or file handle to write
    N)r   ÚwriteÚclear)ÚGÚpathr'   r	   r	   r   r   ²   s    

r   c              
   C   s\   zddl }W n. ty: } ztdƒ|‚W Y d}~n
d}~0 0 |j| d�}t|ƒ}| ¡  |S )z˜Returns a NetworkX graph from a dot file on path.

    Parameters
    ----------
    path : file or string
       File name or file handle to read.
    r   Nz;read_dot() requires pygraphviz http://pygraphviz.github.io/)Úfile)r4   r5   r6   r   r<   )r>   r4   r9   r'   Zgrr	   r	   r   r   Â   s    ÿþr   ÚneatoÚ c                 C   s   t | |||d�S )a}  Create node positions for G using Graphviz.

    Parameters
    ----------
    G : NetworkX graph
      A graph created with NetworkX
    prog : string
      Name of Graphviz layout program
    root : string, optional
      Root node for twopi layout
    args : string, optional
      Extra arguments to Graphviz layout program

    Returns
    -------
      Dictionary of x, y, positions keyed by node.

    Examples
    --------
    >>> G = nx.petersen_graph()
    >>> pos = nx.nx_agraph.graphviz_layout(G)
    >>> pos = nx.nx_agraph.graphviz_layout(G, prog="dot")

    Notes
    -----
    This is a wrapper for pygraphviz_layout.
    )ÚprogÚrootÚargs)r   )r=   rB   rC   rD   r	   r	   r   r   Ö   s    r   c              
   C   sÐ   zddl }W n. ty: } ztdƒ|‚W Y d}~n
d}~0 0 |durR|d|› �7 }t| ƒ}|j||d� i }| D ]Z}| ||¡}	z*|	jd  d¡}
tdd	„ |
D ƒƒ||< W qp   td
|ƒ d||< Y qp0 qp|S )aŸ  Create node positions for G using Graphviz.

    Parameters
    ----------
    G : NetworkX graph
      A graph created with NetworkX
    prog : string
      Name of Graphviz layout program
    root : string, optional
      Root node for twopi layout
    args : string, optional
      Extra arguments to Graphviz layout program

    Returns
    -------
    node_pos : dict
      Dictionary of x, y, positions keyed by node.

    Examples
    --------
    >>> G = nx.petersen_graph()
    >>> pos = nx.nx_agraph.graphviz_layout(G)
    >>> pos = nx.nx_agraph.graphviz_layout(G, prog="dot")

    Notes
    -----
    If you use complex node objects, they may have the same string
    representation and GraphViz could treat them as the same node.
    The layout may assign both nodes a single location. See Issue #1568
    If this occurs in your case, consider relabeling the nodes just
    for the layout computation using something similar to::

        >>> H = nx.convert_node_labels_to_integers(G, label_attribute="node_label")
        >>> H_layout = nx.nx_agraph.pygraphviz_layout(G, prog="dot")
        >>> G_layout = {H.nodes[n]["node_label"]: p for n, p in H_layout.items()}

    r   Nr,   z-Groot=)rB   rD   Úposú,c                 s   s   | ]}t |ƒV  qd S ©N)Úfloat)r   Úxr	   r	   r   r/   *  r   z$pygraphviz_layout.<locals>.<genexpr>zno position for node)ç        rJ   )	r4   r5   r   ZlayoutÚNoder   ÚsplitÚtupleÚprint)r=   rB   rC   rD   r4   r9   r'   Znode_posr)   r   Úxsr	   r	   r   r   õ   s,    &ÿþ
r   é   zw+bÚdotTc                    sÞ  t ˆ ƒst d¡‚g d¢}|D ]}|ˆ jvri ˆ j|< qddi}	dddddd	œ}
i }‡ fd
d„}‡ fdd„}|d|	ƒ |d|
ƒ |d|ƒ tˆ ƒ}|d|	ƒ |d|
ƒ |d|ƒ ˆdu�rXtˆƒsÐ‡fdd„}nˆ}ˆ  ¡ �r ˆ jddd�D ]0\}}}}| ||t	|ƒ¡}t	||ƒƒ|j
d< qìn8ˆ jdd�D ]*\}}}| ||¡}t	||ƒƒ|j
d< �q,|du �r˜d}|�r~d|› d|› �}n
d|› �}tj|dd�}n |j|d||d� | ¡  |�rÔddlm} | |j¡ ¡  |j|fS ) a,  Views the graph G using the specified layout algorithm.

    Parameters
    ----------
    G : NetworkX graph
        The machine to draw.
    edgelabel : str, callable, None
        If a string, then it specifes the edge attribute to be displayed
        on the edge labels. If a callable, then it is called for each
        edge and it should return the string to be displayed on the edges.
        The function signature of `edgelabel` should be edgelabel(data),
        where `data` is the edge attribute dictionary.
    prog : string
        Name of Graphviz layout program.
    args : str
        Additional arguments to pass to the Graphviz layout program.
    suffix : str
        If `filename` is None, we save to a temporary file.  The value of
        `suffix` will appear at the tail end of the temporary filename.
    path : str, None
        The filename used to save the image.  If None, save to a temporary
        file.  File formats are the same as those from pygraphviz.agraph.draw.
    show : bool, default = True
        Whether to display the graph with :mod:`PIL.Image.show`,
        default is `True`. If `False`, the rendered graph is still available
        at `path`.

    Returns
    -------
    path : str
        The filename of the generated image.
    A : PyGraphviz graph
        The PyGraphviz graph instance used to generate the image.

    Notes
    -----
    If this function is called in succession too quickly, sometimes the
    image is not displayed. So you might consider time.sleep(.5) between
    calls if you experience problems.

    zAn empty graph cannot be drawn.)r   r   r   ZfontsizeÚ10Zfilledz	#0000FF40z0.75Zcircle)ÚstyleZ	fillcolorÚheightÚwidthÚshapec                    s@   g }|  ¡ D ].\}}|ˆ j|  vr|ˆ j|  |< | |¡ qd S rG   )r   r   Úappend)ÚwhichÚattrsÚaddedr   r   ©r=   r	   r   Úupdate_attrsx  s
    z%view_pygraphviz.<locals>.update_attrsc                    s,   |D ]}ˆ j |  |= qˆ j |  s(ˆ j | = d S rG   )r   )rX   rZ   r   r[   r	   r   Úclean_attrs€  s    
z$view_pygraphviz.<locals>.clean_attrsr   r   r   Nc                    s   d  dt| ˆ  ƒdg¡S )NrA   z  )Újoinr   r0   )Ú	edgelabelr	   r   Úfunc˜  s    zview_pygraphviz.<locals>.funcT)r2   r1   Úlabelr0   ZpngÚ_Ú.F)ÚsuffixÚdelete)r>   ÚformatrB   rD   r   ©ÚImage)Úlenr   ZNetworkXExceptionr   r   Úcallabler#   r!   r8   r   r   ÚtempfileÚNamedTemporaryFileÚdrawÚcloseÚPILrh   Úopenr   Úshow)r=   r_   rB   rD   rd   r>   rq   rY   r   Z
edge_attrsZ
node_attrsZgraph_attrsr\   r]   r'   r`   r+   r   r   r1   r   Úextrh   r	   )r=   r_   r   r   1  s\    -
	
û









r   c                 C   s|   ddl }ddlm} | dt¡ |du rJ|j}tj |¡d  	¡ dd… }|sRd}|  
||||¡ | ¡  | |¡ ¡  dS )aý  Internal function to display a graph in OS dependent manner.

    Parameters
    ----------
    graph : PyGraphviz graph
        A PyGraphviz AGraph instance.
    path :  file object
        An already opened file object that will be closed.
    format : str, None
        An attempt is made to guess the output format based on the extension
        of the filename. If that fails, the value of `format` is used.
    prog : string
        Name of Graphviz layout program.
    args : str
        Additional arguments to pass to the Graphviz layout program.

    Notes
    -----
    If this function is called in succession too quickly, sometimes the
    image is not displayed. So you might consider time.sleep(.5) between
    calls if you experience problems.

    r   Nrg   a  display_pygraphviz is deprecated and will be removed in NetworkX 3.0. To view a graph G using pygraphviz, use nx.nx_agraph.view_pygraphviz(G). To view a graph from file, consider an image processing libary like `Pillow`, e.g. ``PIL.Image.open(path.name).show()``r   )Úwarningsro   rh   ÚwarnÚDeprecationWarningr   Úosr>   ÚsplitextÚlowerrm   rn   rp   rq   )r   r>   rf   rB   rD   rs   rh   Úfilenamer	   r	   r   Údisplay_pygraphvizÁ  s    ûrz   )N)r@   NrA   )r@   NrA   )NrQ   rA   rA   NT)NNrA   )Ú__doc__rv   rk   Znetworkxr   Ú__all__r   r   r   r   r   r   ÚutilsÚ	open_filer   rz   r	   r	   r	   r   Ú<module>   s   
LB

< ÿ 