
    hq6                         S r SSKrSSKrSSKr/ SQr\R                  " SSS9SS j5       rS rS r	\R                  " S	SSS
9S 5       r
SS jrSS jr\R                  R                  SS5       SS j5       rg)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_pygraphvizT)graphsreturns_graphc                 6   Uc  U R                  5       (       a7  U R                  5       (       a  [        R                  nOG[        R                  nO6U R                  5       (       a  [        R
                  nO[        R                  n[        R                  " SU5      nU R                  b  U R                  Ul        UR                  R                  U R                  5        U R                  5        HW  nUR                  R                  5        VVs0 s H  u  pE[        U5      U_M     nnnUR                   " [        U5      40 UD6  MY     U R#                  5        H  n[        US   5      [        US   5      pX[%        UR                  5      n	U	R                  5        VVs0 s H  u  pE[        U5      U_M     nnnUR'                  5       (       d2  UR                  b  UR                  US'   UR(                  " UW40 UD6  M  UR(                  " UW4SUR                  0UD6  M     [%        U R                  5      UR                  S'   [%        U R*                  5      UR                  S'   [%        U R,                  5      UR                  S'   U$ s  snnf s  snnf )am  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.

r      keygraphnodeedge)is_directed	is_strictnxDiGraphMultiDiGraphGraph
MultiGraphempty_graphnamer   update
graph_attrnodesattritemsstradd_nodeedgesdictis_multigraphadd_edge	node_attr	edge_attr)
Acreate_usingNnkvstr_attreur   s
             L/var/www/html/env/lib/python3.13/site-packages/networkx/drawing/nx_agraph.pyr   r   %   s   @ ==??{{}}!zz!{{}}!xx!}} 	q,'Avv GGNN1<<  WWY*+&&,,.9.$!CFAI.9	

3q6&X& 
 WWY1Q4y#ad)1AFF|*.**,7,$!CFAI,7  vv!"#&&JJq!(x(JJq!4484  ALL)AGGG1;;'AGGFO1;;'AGGFOH) : 8s   J!Jc           	      T    SSK nU R                  5       n[        R                  " U 5      S:H  =(       a    U R                  5       (       + nUR                  U R                  XCS9nUR                  R                  U R                  R                  S0 5      5        UR                  R                  U R                  R                  S0 5      5        UR                  R                  U R                  R                  S0 5      5        UR                  R                  S U R                  R                  5        5       5        U R                  S	S
9 H{  u  pgUR!                  U5        UR#                  U5      nUR                  5        H@  u  pU	S:X  a  U
S    SU
S    S3UR$                  S'   M(  ['        U
5      UR$                  U	'   MB     M}     U R                  5       (       a  U R)                  S	S	S9 H  u  ppUR                  5        VVs0 s H  u  pUS:w  d  M  U['        U5      _M     nnnUR+                  UW['        U	5      S9  UR-                  X5      nUR$                  R                  U5        M     U$ U R)                  S	S
9 Hr  u  pnUR                  5        VVs0 s H  u  pU['        U5      _M     nnnUR+                  UW5        UR-                  X5      nUR$                  R                  U5        Mt     U$ ! [         a  n[        S5      UeSnAff = fs  snnf s  snnf )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   N0requires pygraphviz http://pygraphviz.github.io/)r   strictdirectedr   r   r   c              3   <   #    U  H  u  pUS ;  d  M  X4v   M     g7f))r   r   r   N ).0r,   r-   s      r1   	<genexpr>to_agraph.<locals>.<genexpr>   s!      *41a7P.P?s   
Tdatapos,r   !)r<   keysr   )r   )
pygraphvizImportErrorr   r   number_of_selfloopsr$   AGraphr   r   r   r   getr&   r'   r   r   r!   get_noder   r    r"   r%   get_edge)r*   rA   errr5   r4   r(   r+   nodedataar   valr0   r-   edgedatar,   str_edgedatas                   r1   r   r   r   s   (W }}H##A&!+EAOO4E0EFqvvfHA LLGR01KKqww{{62./KKqww{{62./LL 77==? 
 wwDw)	

1JJqM (HCe|#&q6(!CF81 5u!#hs	 )	 * 	#$7747#@A#2:..2BQ2B$!a5jIAs1vI2BLQJJq!SJ*

1 AFFMM,' $A H  gg4g0NA(2:..2BC2B$!As1vI2BLCJJq!

1 AFFMM,' 1 HY  WLMSVVW< R Ds)   L  
LL!L$ 
L
LLc                 \    [        U 5      nUR                  U5        UR                  5         g)a  Write NetworkX graph G to Graphviz dot format on path.

Parameters
----------
G : graph
   A networkx graph
path : filename
   Filename or file handle to write

Notes
-----
To use a specific graph layout, call ``A.layout`` prior to `write_dot`.
Note that some graphviz layouts are not guaranteed to be deterministic,
see https://gitlab.com/graphviz/graphviz/-/issues/1767 for more info.
N)r   writeclear)Gpathr(   s      r1   r   r      s$      	!AGGDMGGI
    agraph_read_dot)r   r
   r   c                      SSK nUR                  U S9n[        U5      nUR	                  5         U$ ! [         a  n[        S5      UeSnAff = f)zReturns 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)rA   rB   rD   r   rP   )rR   rA   rH   r(   grs        r1   r   r      s[    
 	t$A	QBGGII  I
	s   2 
AAAc                     [        XX#S9$ )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.

Note that some graphviz layouts are not guaranteed to be deterministic,
see https://gitlab.com/graphviz/graphviz/-/issues/1767 for more info.
)progrootargs)r   )rQ   rY   rZ   r[   s       r1   r   r      s    > Q@@rS   c                 d    SSK nUb  USU 3-  n[        U 5      nUR                  XS9  0 nU  HG  nUR	                  Xh5      n	 U	R
                  S   R                  S5      n
[        S U
 5       5      Xx'   MI     U$ ! [         a  n[        S5      UeSnAff = f!   [        S	U5        S
Xx'    M  = f)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()}

Note that some graphviz layouts are not guaranteed to be deterministic,
see https://gitlab.com/graphviz/graphviz/-/issues/1767 for more info.
r   Nr3   z-Groot=)rY   r[   r=   r>   c              3   8   #    U  H  n[        U5      v   M     g 7fN)float)r8   xs     r1   r9   $pygraphviz_layout.<locals>.<genexpr>9  s     5"Qa"s   zno position for node)        rb   )	rA   rB   r   layoutNoder   splittupleprint)rQ   rY   rZ   r[   rA   rH   r(   node_posr+   r   xss              r1   r   r     s    PW '$  !AHH$H"Hq$	%5!'',B5"55HK	  O  WLMSVVW	%(!,$HKs#   A< 2B<
BBBB/   zw+bc                 4  ^ ^ [        T 5      (       d  [        R                  " S5      e/ SQnU H$  nUT R                  ;  d  M  0 T R                  U'   M&     SS0n	SSSSSS	.n
0 nU 4S
 jnU 4S jnU" SU	5        U" SU
5        U" SU5        [	        T 5      nU" SU	5        U" SU
5        U" SU5        Tb  [        T5      (       d  U4S jnOTnT R                  5       (       aW  T R                  SSS9 HB  u  nnnnUR                  UU[        U5      5      n[        U" U5      5      UR                  S'   MD     OJT R                  SS9 H7  u  nnnUR                  UU5      n[        U" U5      5      UR                  S'   M9     Uc-  SnU(       a	  SU SU 3nOSU 3n[        R                  " USS9nO UR                  USX#S9  UR                  5         U(       a/  SSKJn  UR#                  UR$                  5      R'                  5         UR$                  U4$ )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 specifies 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.

Note that some graphviz layouts are not guaranteed to be deterministic,
see https://gitlab.com/graphviz/graphviz/-/issues/1767 for more info.

zAn empty graph cannot be drawn.)r   r   r   fontsize10filledz	#0000FF40z0.75circle)style	fillcolorheightwidthshapec                    > / nUR                  5        H=  u  p4UTR                  U    ;  d  M  UTR                  U    U'   UR                  U5        M?     g r^   )r   r   append)whichattrsaddedr,   r-   rQ   s        r1   update_attrs%view_pygraphviz.<locals>.update_attrs  sG    KKMDA&$%q!Q "rS   c                 |   > U H  nTR                   U    U	 M     TR                   U    (       d  TR                   U 	 g g r^   )r   )rw   ry   r   rQ   s      r1   clean_attrs$view_pygraphviz.<locals>.clean_attrs  s8    Dt$ wwu~ rS   r   r   r   Nc                 D   > SR                  S[        U T   5      S/5      $ )N z  )joinr    )r<   	edgelabels    r1   funcview_pygraphviz.<locals>.func  s"    wwc$y/&:DABBrS   T)r@   r<   labelr;   png_.F)suffixdelete)rR   formatrY   r[   r   )Image)lenr   NetworkXExceptionr   r   callabler$   r"   rG   r    r   tempfileNamedTemporaryFiledrawclosePILr   openr   show)rQ   r   rY   r[   r   rR   r   rx   r   
edge_attrs
node_attrsgraph_attrsrz   r}   r(   r   r0   r-   r   r<   r   extr   s   ``                     r1   r	   r	   @  s   ` q66""#DEE &EqwwAGGDM 
 d#J J K  $$+& 	!A 
#
#% 	""C D ??#$7747#@1c4zz!QC1%(d_		'" $A
  gg4g0
1dzz!Q'%(d_		'" 1 |#'FYF**&G 	 FFTF8JJL 

499""$99a<rS   r^   )neatoNr   )Ndotr   r   NT)__doc__osr   networkxr   __all___dispatchabler   r   r   r   r   r   utils	open_filer	   r7   rS   r1   <module>r      s   ( 
   T2I 3IXBJ, (TJ K(AD9x AuGKO OrS   