K i6dZddlZddlZgdZej ddddZdZdZej d dd d Z dd Z dd Z ejjdd ddZy)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_dotread_dotgraphviz_layoutpygraphviz_layoutview_pygraphvizT)graphs returns_graphc|s|jr2|jrtj}nBtj}n1|jrtj }ntj }tjd|}|j|j|_|jj|j|jD]T}|jjDcic]\}}t||}}}|j t|fi|V|j#D]}t|dt|d}}t%|j} | jDcic]\}}t||}}}|j's0|j|j|d<|j(|fi||j(|fd|ji|t%|j|jd<t%|j*|jd<t%|j,|jd<|Scc}}wcc}}w)aReturns 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. rkeygraphnodeedge) is_directed is_strictnxDiGraph MultiDiGraphGraph MultiGraph empty_graphnamerupdate graph_attrnodesattritemsstradd_nodeedgesdict is_multigraphadd_edge node_attr edge_attr) A create_usingNnkvstr_attreurs `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/drawing/nx_agraph.pyrr$s@ ==?{{}!zz ! {{}!xx !}}  q,'AvvGGNN1<< WWY'*+&&,,.9$!QCFAI99 3q6&X&' WWY 51Q4y#ad)1AFF|*.**,7$!QCFAI77 vv!"#&& AJJq! (x ( AJJq! 4 48 4 5ALL)AGGG1;;'AGGFO1;;'AGGFO H):8s >I/ I5c B ddl}|j}tj|dk(xr|j }|j |j||}|jj|jjdi|jj|jjdi|jj|jjdi|jjd|jjD|jd D]x\}}|j!||j#|}|jD]>\} } | d k(r| dd | d d|j$d <'t'| |j$| <@z|j r|j)d d D]\} } } } | jD cic]\}} |dk7s |t'| }}} |j+|  t'| |j-| | }|j$j||S|j)d D]q\} } } | jD cic]\}} |t'| }}} |j+|  |j-| | }|j$j|s|S#t$r}td|d}~wwxYwcc} }wcc} }w)aReturns 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. rN0requires pygraphviz http://pygraphviz.github.io/)rstrictdirectedrrrc34K|]\}}|dvs ||fyw))rrrN).0r,r-s r1 zto_agraph..s%1aa7P.PAs  Tdatapos,r !)r;keysr)r) pygraphviz ImportErrorrrnumber_of_selfloopsr$AGraphrrrrgetr&r'rrr!get_noderr r"r%get_edge)r*r@errr5r4r(r+nodedataarvalr0r-edgedatar, str_edgedatas r1rrqs(W}}H  # #A &! + EAOO4E0EFqvvfxHALL GR01KKqww{{62./KKqww{{62./LL77==? wwDw)' 8 1 JJqM ( 'HCe|#&q6(!CF81 5u !#hs  ' ' #$7747#@ ( Aq#x2:..2BQ$!Qa5jAs1vIQLQ JJq!SJ * 1a A FFMM, '  ( H gg4g0 (NAq(2:..2BC$!QAs1vICLC JJq!  1a A FFMM, '  ( HY WLMSVVW<RDs)K8 LLL8 L L  Lc\t|}|j||jy)aWrite 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)rwriteclear)Gpathr(s r1rrs$ ! AGGDMGGI agraph_read_dot)rr r c ddl}|j|}t|}|j |S#t$r}td|d}~wwxYw)zReturns a NetworkX graph from a dot file on path. Parameters ---------- path : file or string File name or file handle to read. rNz;read_dot() requires pygraphviz http://pygraphviz.github.io/)file)r@rArCrrO)rQr@rGr(grs r1rrs[ t$A QBGGI I  I  s5 A A  Ac t||||S)aCreate 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. )progrootargs)r)rPrXrYrZs r1rrs> QT4 @@rRch ddl}||d|z }t|}|j||i}|D]H}|j ||} | j dj d} td| D||<J|S#t$r}td|d}~wwxYw#td |d ||<YxYw) a5Create 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(H, 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. rNr3z-Groot=)rXrZr<r=c32K|]}t|ywN)float)r8xs r1r9z$pygraphviz_layout..8s5Qa5szno position for node)r`) r@rArlayoutNodersplittupleprint) rPrXrYrZr@rGr(node_posr+rxss r1rrsPW  '$  ! AHH$TH"H %q!$ %5!'',B5"55HQK % O WLMSVVW % (! ,$HQKs#A?3B? B BBB1zw+bctstjdgd}|D] }|jvsij|<"ddi} dddddd } i} fd } fd } | d | | d | | d| t }| d | | d | | d| t sfd}n}j rXjddD]A\}}}}|j||t|}t|||jd<CnKjdD]6\}}}|j||}t|||jd<8|*d}|r d|d|}nd|}tj|d}n |j|d|||j|r/ddlm}|j#|j$j'|j$|fS)aViews 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. Filenames ending in .gz or .bz2 will be compressed. 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. zAn empty graph cannot be drawn.)rrrfontsize10filledz #0000FF40z0.75circle)style fillcolorheightwidthshapecg}|jD]:\}}|j|vs|j||<|j|<yr])rrappend)whichattrsaddedr,r-rPs r1 update_attrsz%view_pygraphviz..update_attrssNKKM DAq&$%q! Q rRcn|D]}j||=j|sj|=yyr])r)rurwrrPs r1 clean_attrsz$view_pygraphviz..clean_attrss= %Dt$ %wwu~rRrrrNcDdjdt|dgS)Nz )joinr )r; edgelabels r1funczview_pygraphviz..funcs"wwc$y/&:DABBrRT)r?r;labelr:png_.F)suffixdelete)rQformatrXrZr)Image)lenrNetworkXExceptionrrcallabler$r"rFr rtempfileNamedTemporaryFiledrawclosePILropenrshow)rPr~rXrZrrQrrvr edge_attrs node_attrs graph_attrsrxrzr(rr0r-rr;rextrs`` r1r r ?sb q6""#DEE &E qww AGGDM d#J  JK $$+& ! A # #% " CD ?? #$7747#@ 51c4zz!QC1%(d_ '" 5  gg4g0 5 1dzz!Q'%(d_ '" 5 | #'FYF**&G FFT4F8JJL  499""$ 99a<rRr])neatoNr|)Ndotr|r|NT)__doc__rnetworkxr__all__ _dispatchablerrrrrrutils open_filer r7rRr1rs( T2I 3I XB J ,(TJK(AD9xAuGKPPrR