K i'dZddlmZddlZddlmZgdZeddd Zedd ejd dd dZ ejdd dZ dZ ddZ ddZy)a ***** Pydot ***** Import and export NetworkX graphs in Graphviz dot format using pydot. Either this module or nx_agraph can be used to interface with graphviz. Examples -------- >>> G = nx.complete_graph(5) >>> PG = nx.nx_pydot.to_pydot(G) >>> H = nx.nx_pydot.from_pydot(PG) See Also -------- - pydot: https://github.com/erocarrera/pydot - Graphviz: https://www.graphviz.org - DOT Language: http://www.graphviz.org/doc/info/lang.html )getpreferredencodingN) open_file) write_dotread_dotgraphviz_layout pydot_layoutto_pydot from_pydotw)modecXt|}|j|jy)zWrite NetworkX graph G to Graphviz dot format on path. Parameters ---------- G : NetworkX graph path : string or file Filename or file handle for data output. Filenames ending in .gz or .bz2 will be compressed. N)r write to_string)GpathPs _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/drawing/nx_pydot.pyrr&s"  AJJq{{} rpydot_read_dotT)namegraphs returns_graphchddl}|j}|j|}t|dS)aReturns a NetworkX :class:`MultiGraph` or :class:`MultiDiGraph` from the dot file with the passed path. If this file contains multiple graphs, only the first such graph is returned. All graphs _except_ the first are silently ignored. Parameters ---------- path : str or file Filename or file handle to read. Filenames ending in .gz or .bz2 will be decompressed. Returns ------- G : MultiGraph or MultiDiGraph A :class:`MultiGraph` or :class:`MultiDiGraph`. Notes ----- Use `G = nx.Graph(nx.nx_pydot.read_dot(path))` to return a :class:`Graph` instead of a :class:`MultiGraph`. rN)pydotreadgraph_from_dot_datar )rrdataP_lists rrr7s52 99;D & &t ,F fQi  r)rrc |j}| }|jdk(r,|rtj}n@tj }n+|rtj }ntj}|jjd}|dk7r||_ |jD]G}|jjd}|dvr'|j|fi|jI|jD]}|j}|j!} |j} g} g} t#|t$r!| j'|jdn*|dD]"} | j'| jd$t#| t$r!| j'| jdn*| dD]"}| j'|jd$| D]}| D]}|j(||fi| |j}|r||j*d< |j-d|j*d< |j1d|j*d <|S#t$r|jd}YwxYw#t.tf$rYUwxYw#t.tf$rY|SwxYw) aReturns a NetworkX graph from a Pydot graph. Parameters ---------- P : Pydot graph A graph created with Pydot Returns ------- G : NetworkX multigraph A MultiGraph or MultiDiGraph. Examples -------- >>> K5 = nx.complete_graph(5) >>> A = nx.nx_pydot.to_pydot(K5) >>> G = nx.nx_pydot.from_pydot(A) # return MultiGraph # make a Graph instead of MultiGraph >>> G = nx.Graph(nx.nx_pydot.from_pydot(A)) Ngraph")noder"edgenodesrr%r&) get_strict TypeErrorget_typenx MultiGraphGraph MultiDiGraphDiGraphget_namestripr get_node_listadd_nodeget_attributes get_edge_list get_sourceget_destination isinstancestrappendadd_edger"get_node_defaults IndexErrorget_edge_defaults)rstrict multiedgesNrpneuvattrsdunodesvnodes source_nodedestination_nodepattrs rr r [s4$Jzz|w  A A !A A ::<  c "D rz__ , JJL  s # ) )  1+((*+ ,__ B LLN   !   a  HHQWWS\ "G* ,c*+ , a  HHQWWS\ "G* ,c*+ , BK$% B  ;(8ADA B B'B0    E   --/2 --/2 H $d#$r  "     "  H s5J J-* K J*)J*-J?>J?KKc ddl}|jrd}nd}tj|dk(xr|j }|j }|j jdi}|dk(r|jd||d|}n|jd|df||d|} |jdi|j d |jdi|j d |jd D]i\}}|jD cic]\} } t| t| } } } t|}|j|fi| } |j!| k|j r|j#d d D]\} } }}|jD cic] \} } | d k7s t| t| "}} } t| t } } |j$| | fd t|i|}|j'||S|j#d D]v\} } }|jD cic]\} } t| t| }} } t| t } } |j$| | fi|}|j'|x|S#t$rYwxYw#t$rYwxYwcc} } wcc} } wcc} } w)aReturns a pydot graph from a NetworkX graph N. Parameters ---------- N : NetworkX graph A graph created with NetworkX Examples -------- >>> K5 = nx.complete_graph(5) >>> P = nx.nx_pydot.to_pydot(K5) Notes ----- rNdigraphr"r$) graph_typer?r#r%r&T)r)rkeyskey)r$)r is_directedr+number_of_selfloops is_multigraphrr"getDotset_node_defaultsKeyErrorset_edge_defaultsr'itemsr9Noder3edgesEdger;)rArrQr?rgraph_defaultsrrCnodedatakrF str_nodedatarBrErSedgedata str_edgedatar&s rr r s" }}   # #A &! + EAOO4E0EF 66DWW[["-N rz EII QZ Q. Q EIIvQK $.v AO  .aggfo. .aggfo.wwDw) 83;>>3CD41aAAD D F EJJq )L ) 1   #$7747#@  Aq#x7?~~7GVtq!1PU:CFCFNVLVq63q6qA5::aACALAD JJt    H  gg4g0 NAq(7?~~7GHtq!CFCFNHLHq63q6qA5::a3l3D JJt    H5        EWIs<#JJ- J= K,K<K  J*)J*- J:9J:ct|||S)a=Create node positions using Pydot and Graphviz. Returns a dictionary of positions keyed by node. Parameters ---------- G : NetworkX Graph The graph for which the layout is computed. prog : string (default: 'neato') The name of the GraphViz program to use for layout. Options depend on GraphViz version but may include: 'dot', 'twopi', 'fdp', 'sfdp', 'circo' root : Node from G or None (default: None) The node of G from which to start some layout algorithms. Returns ------- Dictionary of (x, y) positions keyed by node. Examples -------- >>> G = nx.complete_graph(4) >>> pos = nx.nx_pydot.graphviz_layout(G) >>> pos = nx.nx_pydot.graphviz_layout(G, prog="dot") Notes ----- This is a wrapper for pydot_layout. rprogroot)rrhs rrrs< !$T 22rcddl}t|}||jdt||j |}t|t }|dk(rJt d|dt t d t d t d t d |d y|j|}t|dk(sJ|d}i} |jD]} t| } |j|j| } t| tr| d} | jdd} | Y| jd\}}t!|t!|f| | <| S)aCreate node positions using :mod:`pydot` and Graphviz. Parameters ---------- G : Graph NetworkX graph to be laid out. prog : string (default: 'neato') Name of the GraphViz command to use for layout. Options depend on GraphViz version but may include: 'dot', 'twopi', 'fdp', 'sfdp', 'circo' root : Node from G or None (default: None) The node of G from which to start some layout algorithms. Returns ------- dict Dictionary of positions keyed by node. Examples -------- >>> G = nx.complete_graph(4) >>> pos = nx.nx_pydot.pydot_layout(G) >>> pos = nx.nx_pydot.pydot_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_pydot.pydot_layout(H, prog="dot") G_layout = {H.nodes[n]["node_label"]: p for n, p in H_layout.items()} rNrj)ri)encodingr$zGraphviz layout with z failedzTo debug what happened try:zP = nx.nx_pydot.to_pydot(G)zP.write_dot("file.dot")z And then run z on file.dotr ,)rr setr9 create_dotrprintrlenr'get_nodequote_id_if_necessaryr8listget_possplitfloat)rrirjrrD_bytesDQ_listQnode_posrCstr_nr%posxxyys rrrs]L A  fc$i lll%G G245ABw %dV734  +, +, '( dV<01 & &q )F v;!   q AH WWY 1Azz%55e<= dD !7DllnQr" ?YYs^FB 9eBi0HQK 1 Or)neatoN)__doc__localernetworkxr+networkx.utilsr__all__r _dispatchablerr r rrrTrrrs,($  13     13'DI!J!DT2Z 3Z z> B3BNr