
    9i                        S r SSKrSSKrSSKJrJrJrJrJr  SSK	r
SSKJr  / SQr\" SSS9SS
 j5       rSS jr\" SSS9\
R                   " SS	S9SS j5       5       r " S S5      r " S S\5      r " S S\5      rS rg)aS  Read and write graphs in GEXF format.

.. warning::
    This parser uses the standard xml library present in Python, which is
    insecure - see :external+python:mod:`xml` for additional information.
    Only parse GEFX files you trust.

GEXF (Graph Exchange XML Format) is a language for describing complex
network structures, their associated data and dynamics.

This implementation does not support mixed graphs (directed and
undirected edges together).

Format
------
GEXF is an XML format.  See http://gexf.net/schema.html for the
specification and http://gexf.net/basic.html for examples.
    N)ElementElementTree
SubElementregister_namespacetostring)	open_file)
write_gexf	read_gexfrelabel_gexf_graphgenerate_gexf   wb)modeTc                 \    [        X#US9nUR                  U 5        UR                  U5        g)a  Write G in GEXF format to path.

"GEXF (Graph Exchange XML Format) is a language for describing
complex networks structures, their associated data and dynamics" [1]_.

Node attributes are checked according to the version of the GEXF
schemas used for parameters which are not user defined,
e.g. visualization 'viz' [2]_. See example for usage.

.. warning::

   The `GEXF specification <https://gexf.net/schema.html>`_ reserves some
   keywords (e.g. ``id``, ``pid``, ``label``, etc.) for specifying node/edge
   metadata in the file format. Ensure NetworkX node/edge attribute names
   do not use these special keywords to guarantee all attributes are preserved
   as expected when roundtripping to/from GEXF format.

Parameters
----------
G : graph
   A NetworkX graph
path : file or string
   File or file name to write.
   File names ending in .gz or .bz2 will be compressed.
encoding : string (optional, default: 'utf-8')
   Encoding for text data.
prettyprint : bool (optional, default: True)
   If True use line breaks and indenting in output XML.
version: string (optional, default: '1.2draft')
   The version of GEXF to be used for nodes attributes checking

Examples
--------
>>> G = nx.path_graph(4)
>>> nx.write_gexf(G, "test.gexf")

# visualization data
>>> G.nodes[0]["viz"] = {"size": 54}
>>> G.nodes[0]["viz"]["position"] = {"x": 0, "y": 1}
>>> G.nodes[0]["viz"]["color"] = {"r": 0, "g": 0, "b": 256}


Notes
-----
This implementation does not support mixed graphs (directed and undirected
edges together).

The node id attribute is set to be the string of the node label.
If you want to specify an id use set it as node data, e.g.
node['a']['id']=1 to set the id of node 'a' to 1.

References
----------
.. [1] GEXF File Format, http://gexf.net/
.. [2] GEXF schema, http://gexf.net/schema.html
encodingprettyprintversionN)
GEXFWriter	add_graphwrite)Gpathr   r   r   writers         W/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/networkx/readwrite/gexf.pyr	   r	   $   s+    t GTF
Q
LL    c              #      #    [        XUS9nUR                  U 5        [        U5      R                  5        Sh  vN   g N7f)aP  Generate lines of GEXF format representation of G.

"GEXF (Graph Exchange XML Format) is a language for describing
complex networks structures, their associated data and dynamics" [1]_.

Parameters
----------
G : graph
A NetworkX graph
encoding : string (optional, default: 'utf-8')
Encoding for text data.
prettyprint : bool (optional, default: True)
If True use line breaks and indenting in output XML.
version : string (default: 1.2draft)
Version of GEFX File Format (see http://gexf.net/schema.html)
Supported values: "1.1draft", "1.2draft"


Examples
--------
>>> G = nx.path_graph(4)
>>> linefeed = chr(10)  # linefeed=

>>> s = linefeed.join(nx.generate_gexf(G))
>>> for line in nx.generate_gexf(G):  # doctest: +SKIP
...     print(line)

Notes
-----
This implementation does not support mixed graphs (directed and undirected
edges together).

The node id attribute is set to be the string of the node label.
If you want to specify an id use set it as node data, e.g.
node['a']['id']=1 to set the id of node 'a' to 1.

References
----------
.. [1] GEXF File Format, https://gephi.org/gexf/format/
r   N)r   r   str
splitlines)r   r   r   r   r   s        r   r   r   c   s8     P GTF
Q6{%%'''s   8AA Arb)graphsreturns_graphc                 \    [        XS9nU(       a  [        U" U 5      5      nU$ U" U 5      nU$ )a  Read graph in GEXF format from path.

"GEXF (Graph Exchange XML Format) is a language for describing
complex networks structures, their associated data and dynamics" [1]_.

Parameters
----------
path : file or string
   Filename or file handle to read.
   Filenames ending in .gz or .bz2 will be decompressed.
node_type: Python type (default: None)
   Convert node ids to this type if not None.
relabel : bool (default: False)
   If True relabel the nodes to use the GEXF node "label" attribute
   instead of the node "id" attribute as the NetworkX node label.
version : string (default: 1.2draft)
Version of GEFX File Format (see http://gexf.net/schema.html)
   Supported values: "1.1draft", "1.2draft"

Returns
-------
graph: NetworkX graph
    If no parallel edges are found a Graph or DiGraph is returned.
    Otherwise a MultiGraph or MultiDiGraph is returned.

Notes
-----
This implementation does not support mixed graphs (directed and undirected
edges together).

References
----------
.. [1] GEXF File Format, http://gexf.net/
)	node_typer   )
GEXFReaderr   )r   r$   relabelr   readerr   s         r   r
   r
      s7    J )=Fvd|, H 4LHr   c            	           \ rS rSrSSSSR	                  SS/5      SS.S	S
SSR	                  S	S/5      SS.SSSSR	                  SS/5      SS.S.rS rSSSSSSSSS.rS rSr	g)GEXF   zhttp://www.gexf.net/1.1draftz http://www.gexf.net/1.1draft/vizz)http://www.w3.org/2001/XMLSchema-instance z%http://www.gexf.net/1.1draft/gexf.xsd1.1)NS_GEXFNS_VIZNS_XSISCHEMALOCATIONVERSIONzhttp://www.gexf.net/1.2draftz http://www.gexf.net/1.2draft/vizz%http://www.gexf.net/1.2draft/gexf.xsdz1.2zhttp://gexf.net/1.3zhttp://gexf.net/1.3/vizz%http://w3.org/2001/XMLSchema-instancezhttp://gexf.net/1.3/gexf.xsd1.3)z1.1draft1.2draftr2   c                    [         S4[        S4[        S4[        S4[        S4[        S4[         S4[
        S4[
        S4[
        S4/
n S	S KnUR                  S4UR                  S4UR                  S4UR                  S
4UR                  S
4UR                  S
4UR                  S
4UR                  S
4UR                  S
4UR                   S
4UR"                  S
4UR$                  S
4UR                  S
4UR&                  S
4UR(                  S
4/U-   n[	        U5      U l        [	        S U 5       5      U l        g ! [*         a     N4f = f)Nintegerfloatdoublebooleanstringlong
liststringanyURIr   intc              3   8   #    U  H  n[        U5      v   M     g 7fN)reversed).0as     r   	<genexpr>'GEXF.construct_types.<locals>.<genexpr>  s     ;UUs   )r=   r6   boollistdictr   numpyfloat64float32float16int_int8int16int32int64uint8uint16uint32uint64intcintpImportErrorxml_typepython_type)selftypesnps      r   construct_typesGEXF.construct_types   sY   )GH988&M,(O(O
	 W%W%W%% % 5!5!5!5!E"E"E"% % %   !E$ U;U;;/  		s   
D> >
E
ETF)truefalseTrueFalse0r   1r   c                     U R                   R                  U5      nUc  [        R                  " SU S35      eUS   U l        US   U l        US   U l        US   U l        US   U l        Xl	        g )NzUnknown GEXF version .r-   r.   r/   r0   r1   )
versionsgetnxNetworkXErrorr-   r.   r/   r0   r1   r   )rZ   r   ds      r   set_versionGEXF.set_version  su    MMg&9""%:7)1#EFF|kk 01|r   )r-   r.   r/   r0   r1   rY   r   rX   N)
__name__
__module____qualname____firstlineno__joinrg   r]   convert_boolrl   __static_attributes__ r   r   r)   r)      s     68A!hh2; 
 68A!hh2; 
 -/=!hh)2 
3%HN(<X 	L	r   r)   c                   r    \ rS rSr SS jrS rS rS rS rS r	S	 r
S
 rS rS rS rS rS rSS jrSrg)r   i'  Nc                 p   U R                  5         X0l        X l        U R                  U5        [	        SU R
                  U R                  U R                  U R                  S.5      U l	        [	        S5      nS[        R                   3nU[        US5      l        UR                  S[        R                   " S5      5        U R                  R#                  U5        [%        SU R&                  5        [(        R*                  " 5       U l        [(        R*                  " 5       U l        [        5       U l        0 U l        0 U R2                  S	'   0 U R2                  S
'   0 U R2                  S	   S'   0 U R2                  S	   S'   0 U R2                  S
   S'   0 U R2                  S
   S'   Ub  U R5                  U5        g g )Ngexf)xmlnsz	xmlns:xsizxsi:schemaLocationr   metaz	NetworkX creatorlastmodifieddatez%Y-%m-%dviznodeedgedynamicstatic)r]   r   r   rl   r   r-   r/   r0   r1   xmlri   __version__r   textsettimestrftimeappendr   r.   	itertoolscountedge_idattr_idall_edge_idsattrr   )rZ   graphr   r   r   meta_elementsubelement_texts          r   __init__GEXFWriter.__init__*  sl    	& !![[&*&9&9<<	
 v%bnn%563B
<+0+T]]:-FG%5$++. !( (E			&		&')		&)$&(		&(#')		&)$&(		&(#NN5! r   c                     U R                   (       a  U R                  U R                  5        [        U R                  5      R	                  U R
                  5      nU$ r?   )r   indentr   r   decoder   )rZ   ss     r   __str__GEXFWriter.__str__U  s=    KK!TXX%%dmm4r   c                    UR                  SS9 H@  u  p#nUR                  S5      nUc  M  U R                  R                  [	        U5      5        MB     UR
                  R                  S5      S:X  a  SnOSnUR                  5       (       a  SnOSnUR
                  R                  S	S
5      n[        SXvUS9n	Xl        U R                  X5        U R                  X5        U R                  R                  U	5        g )NTdataidr   r   r   directed
undirectedname r   )defaultedgetyper   r   )edgesrh   r   addr   r   is_directedr   graph_element	add_nodes	add_edgesr   r   )
rZ   r   uvddeidr   defaultr   r   s
             r   r   GEXFWriter.add_graph[  s    T*HA"&&,C!!%%c#h/ +
 77;;v)+DD==?? G"Gww{{62&RVW*q(q(&r   c                    [        S5      nUR                  SS9 GHv  u  pEUR                  5       n[        UR	                  SU5      5      nSU0n[        UR	                  SU5      5      n	XS'    UR	                  S5      n
[        U
5      US'    UR	                  S5      n[        U5      US'   U R                  U5         UR	                  S5      n[        U5      US'   U R                  U5        [        S0 UD6nUR                  R                  S
0 5      nU R                  X5      nU R                  S:X  a  U R                  X5      nOU R                  X5      nU R                  X5      nU R                  S	XU5      nUR                  U5        GMy     UR                  U5        g ! [
         a     GN&f = f! [
         a     GNf = f! [
         a     Nf = f)NnodesTr   r   labelpidstartendr~   node_defaultr,   )r~   )r   r   copyr   popKeyErroralter_graph_mode_timeformatr   rh   add_parentsr1   
add_slices
add_spellsadd_vizadd_attributesr   )rZ   r   r   nodes_elementr~   r   	node_datanode_idkwr   r   r   r   node_elementr   s                  r   r   GEXFWriter.add_nodesr  s   (''t',JD		I)--d34GB	gt45EwKmmE*H5	!g.!%j7007mmE*H5	005 #0R0Lggkk."5G((AI||u$ OOLD	 OOLD	\=I++FLWUI  .E -F 	]+5      s6   0F(0F90G
(
F65F69
GG

GGc                   ^  U 4S jn[        S5      nU" U5       GH  u  pVpxS[        U5      0n	 UR                  S5      n
[        U
5      U	S'    UR                  S5      n[        U5      U	S'    UR                  S5      n[        U5      U	S'    UR                  S5      n[        U5      U	S'   T R	                  U5         UR                  S5      n[        U5      U	S'   T R	                  U5        [        UR
                  U   R                  SU5      5      n[        UR
                  U   R                  SU5      5      n[        SUUS
.U	D6nUR                  R                  S0 5      nT R                  S:X  a  T R                  UU5      nOT R                  UU5      nT R                  UU5      nT R                  S	UUU5      nUR                  U5        GM     UR                  U5        g ! [         a     GNf = f! [         a     GNf = f! [         a     GNf = f! [         a     GNnf = f! [         a     GNNf = f)Nc              3   r  >#    U R                  5       (       a  U R                  SSS9 H  u  pp4UR                  5       nUR                  US9  UR	                  SS 5      nUc  [        TR                  5      n[        U5      TR                  ;   a0  [        TR                  5      n[        U5      TR                  ;   a  M0  TR                  R                  [        U5      5        XXe4v   M     g U R                  SS9 H  u  pnUR                  5       nUR	                  SS 5      nUc  [        TR                  5      n[        U5      TR                  ;   a0  [        TR                  5      n[        U5      TR                  ;   a  M0  TR                  R                  [        U5      5        XXe4v   M     g 7f)NT)r   keys)keyr   r   )
is_multigraphr   r   updater   nextr   r   r   r   )r   r   r   r   r   	edge_datar   rZ   s          r   edge_key_data+GEXFWriter.add_edges.<locals>.edge_key_data  se      '(wwDtw'DOA# $		I$$$-'mmD$7G"&t||"4!'ld.?.??&*4<<&8G "'ld.?.??))--c'l;22 (E #$''t'"4JA$ $		I'mmD$7G"&t||"4!'ld.?.??&*4<<&8G "'ld.?.??))--c'l;22 #5s   B<F7CF7/F7r   r   r   weighttyper   r   r   )sourcetargetedge_defaultr,   )r   )r   r   r   r   r   r   rh   r   r1   r   r   r   r   r   )rZ   r   r   r   edges_elementr   r   r   r   r   
edge_labeledge_weight	edge_typer   r   	source_id	target_idedge_elementr   s   `                  r   r   GEXFWriter.add_edges  s+   	30  ($1!$4 A#C!B&]]73
!*o7'mmH5";/8%MM&1	 ^6
!g.!%j7007mmE*H5	005 AGGAJNN434IAGGAJNN434I"T)ITQSTLggkk."5G||u$ OOL)D	 OOL)D	\9=I++FL)WUI  .O %5P 	]+G  
  
      sY   G.G?0H0H!0H2.
G<;G<?
HH
HH!
H/.H/2
I ?I c                 &   [        S5      n[        U5      S:X  a  U$ SnUR                  5        GH  u  pxUS:X  a  Sn[        U5      n	XR                  ;  a  [        SU	 35      e[        U[        5      (       Gae  U H=  u  pn[        U
5      n	Uc  Uc  M  SnU R                  U5        U R                  U5          O   U R                  [        U5      U R                  U	   XU5      nU H  u  pn[        S5      nXR                  S	'   [        U
5      UR                  S
'   U	[        L ah  UR                  S
   S:X  a  SUR                  S
'   OEUR                  S
   S:X  a  SUR                  S
'   O"UR                  S
   S:X  a  SUR                  S
'   Ub  [        U5      UR                  S'   Ub  [        U5      UR                  S'   UR                  U5        M     GM  SnU R                  [        U5      U R                  U	   XU5      n[        S5      nXR                  S	'   [        U[        5      (       a'  [        U5      R                  5       UR                  S
'   O[        U5      UR                  S
'   U	[        L ah  UR                  S
   S:X  a  SUR                  S
'   OEUR                  S
   S:X  a  SUR                  S
'   O"UR                  S
   S:X  a  SUR                  S
'   UR                  U5        GM     UR                  U5        U$ )N	attvaluesr   r   r   networkx_keyz%attribute value type is not allowed: r   attvalueforvalueinfINFnanNaNz-infz-INFr   r   )r   lenitemsr   rX   	TypeError
isinstancerF   r   get_attr_idr   attribr6   r   rE   lower)rZ   node_or_edgexml_objr   r   r   r   kr   val_typevalr   r   r   es                  r   r   GEXFWriter.add_attributes  s   K(	t9>KJJLDAEz"AwH}},"Gz RSS!T""'(OC#CyH(CO(88?88= () **FDMM(3\D ()OC
+A&-HHUO(+CAHHW%5(88G,505AHHW-XXg.%705AHHW-XXg.&806AHHW-(,/J)*-c($$Q'! ()&  **FDMM(3\D J'")a&&(+AAHHW%(+AAHHW%5(88G,505AHHW-XXg.%705AHHW-XXg.&806AHHW-  #q !r 	y!r   c                     U R                   U   U   U   $ ! [         Ga    [        [        U R                  5      5      nX`R                   U   U   U'   XaUS.n[        S	0 UD6nUR                  U5      n	U	b,  [        S5      n
[        U	5      U
l        UR                  U
5        S nU R                  R                  S5       H6  nUR                  S5      nUR                  SS5      nX:X  d  M-  X:X  d  M4  UnM8     Uc+  XSS.n[        S
0 UD6nU R                  R                  SU5        UR                  U5         U$ f = f)N)r   titler   r   
attributesclassr   r   )r   r   r   )	attribute)r   )r   r   r   r   r   r   rh   r   r   r   findallinsert)rZ   r   	attr_typeedge_or_noder   r   new_idattr_kwargsr   default_titledefault_elementattributes_elementrB   a_classa_modes                  r   r   GEXFWriter.get_attr_id  s>   	199\*4077 	1dll+,F39IIl#D)%0!'KK;{;I#KK.M("))"4'*='9$  1!%''//=%%.vx0*v~)*& > ")'+C%,%I[%I"""))!-?@%%i05	1s    CD>.D>5AD>=D>c                    UR                  SS5      nU(       Ga  UR                  S5      nUGb   U R                  S:X  ac  [        SU R                   S3[        UR                  S5      5      [        UR                  S5      5      [        UR                  S	5      5      S
9nO|[        SU R                   S3[        UR                  S5      5      [        UR                  S5      5      [        UR                  S	5      5      [        UR                  SS5      5      S9nUR                  U5        UR                  S5      nUb2  [        SU R                   S3[        U5      S9nUR                  U5        UR                  S5      nUb2  [        SU R                   S3[        U5      S9nUR                  U5        UR                  S5      nUbk  UR                  S5      (       a#  [        SU R                   S3S[        U5      S9nO![        SU R                   S3[        U5      S9nUR                  U5        UR                  S5      n	U	bs  [        SU R                   S3[        U	R                  S5      5      [        U	R                  S5      5      [        U	R                  S5      5      S9nUR                  U5        U$ )Nr}   Fcolorr,   {}colorrgbr	  r
  r  rB   g      ?r	  r
  r  rB   size}size)r   	thickness
}thicknessshapehttp}shapeimage)r   uriposition	}positionxyzr  r  r  )r   rh   r1   r   r.   r   r   
startswith)
rZ   elementr   r}   r  r   r  r  r  r  s
             r   r   GEXFWriter.add_viz?  sL   mmE5)GGG$E <<5(T[[M1eiin-eiin-eiin-	A  T[[M1eiin-eiin-eiin-eiiS12A q!776?DbV4CIFq!,I$b[9YPq!GGG$E ##F++T[[M1c%jA  "T[[M 9ULAq!wwz*H#Z0(,,s+,(,,s+,(,,s+,	 q!r   c                     UR                  SS5      nU(       aY  [        S5      nU H7  n[        S5      n[        U5      UR                  S'   UR	                  U5        M9     UR	                  U5        U$ )NparentsFparentr   )r   r   r   r   r   )rZ   r   r   r!  parents_elementpr   s          r   r   GEXFWriter.add_parentst  sh    --	51%i0OH%"%a&&&q)  0r   c                     UR                  SS5      nU(       aU  [        S5      nU H3  u  pV[        S[        U5      [        U5      S9nUR                  U5        M5     UR                  U5        U$ )NslicesFslice)r   r   )r   r   r   r   )rZ   node_or_edge_elementnode_or_edge_datar'  slices_elementr   r   r   s           r   r   GEXFWriter.add_slices  sg    "&&x7$X.N$
G3u:3s8D%%a( % !''7  r   c                 n   UR                  SS5      nU(       a  [        S5      nU Hy  u  pV[        S5      nUb)  [        U5      UR                  S'   U R	                  U5        Ub)  [        U5      UR                  S'   U R	                  U5        UR                  U5        M{     UR                  U5        U$ )NspellsFspellr   r   )r   r   r   r   r   r   )rZ   r)  r*  r.  spells_elementr   r   r   s           r   r   GEXFWriter.add_spells  s    "&&x7$X.N$
G$$(+E
AHHW%44U;?&)#hAHHUO44S9%%a( % !''7  r   c                 x   Ub  [        U[        5      (       a  SnOF[        U[        5      (       a  SnO.[        U[        5      (       a  SnO[        R
                  " S5      eU R                  R                  SU5        U R                  R                  S5      S:X  a  U R                  R                  SS5        g g g )	Ndater7   r:   z2timeformat should be of the type int, float or str
timeformatr   r   r   )	r   r   r6   r=   ri   rj   r   r   rh   )rZ   start_or_endr4  s      r   r   &GEXFWriter.alter_graph_mode_timeformat  s    #,,,#
L%00%
L#..#
&&H  ""<<!!%%f-9""&&vy9 : $r   c                     U R                   (       a  U R                  U R                  5        [        U R                  5      nUR	                  XR
                  SS9  g )NT)r   xml_declaration)r   r   r   r   r   r   )rZ   fhdocuments      r   r   GEXFWriter.write  s=    KK!txx(rMM4Hr   c                 <   SSU-  -   n[        U5      (       a  UR                  (       a  UR                  R                  5       (       d
  US-   Ul        UR                  (       a  UR                  R                  5       (       d  X1l        U H  nU R	                  XS-   5        M     UR                  (       a  UR                  R                  5       (       d  X1l        g g U(       a8  UR                  (       a  UR                  R                  5       (       d  X1l        g g g )N
z  r   )r   r   striptailr   )rZ   elemlevelis       r   r   GEXFWriter.indent  s    4%<t9999DIIOO$5$5H	99DIIOO$5$5	D!), 99DIIOO$5$5	 %6 diityy/@/@	 0Aur   )r   r   r   r   r   r   r   r   )Nutf-8Tr3   )r   )rn   ro   rp   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rt   ru   r   r   r   r   '  sX     GQ)"V'.%,NB,H@D@3j	!! :$Ir   r   c                   d    \ rS rSrSS jrS rS rSS jrS rS r	S	 r
S
 rS rS rS rS rSrg)r%   i  Nc                 `    U R                  5         Xl        SU l        U R                  U5        g )NT)r]   r$   simple_graphrl   )rZ   r$   r   s      r   r   GEXFReader.__init__  s)    " !r   c                    [        US9U l        U R                  R                  SU R                   S35      nUb  U R	                  U5      $ U R
                   HS  nU R                  U5        U R                  R                  SU R                   S35      nUc  MB  U R	                  U5      s  $    [        R                  " S5      e)N)filer  z}graphz No <graph> element in GEXF file.)	r   r   findr-   
make_graphrg   rl   ri   rj   )rZ   streamr
  r   s       r   __call__GEXFReader.__call__  s    F+HHMMBt||nG45=??1%%}}GW%4<<.89A}q))	 %
 ABBr   c                 `   UR                  SS 5      nUS:X  a  [        R                  " 5       nO[        R                  " 5       nUR                  SS5      nUS:w  a  XCR                  S'   UR                  S5      nUb  XSR                  S'   UR                  S5      nUb  XcR                  S'   UR                  SS5      nUS:X  a  SUR                  S'   OS	UR                  S'   UR                  S
5      U l        U R
                  S:X  a  SU l        UR                  SU R                   S35      n0 n	0 n
0 n0 nU H  nUR                  S5      nUS:X  aF  U R                  U5      u  nnU	R                  U5        U
R                  U5        XR                  S'   M`  US:X  aF  U R                  U5      u  nnUR                  U5        UR                  U5        XR                  S'   M  e    SSS	SS.0n0 nUR                  U5        UR                  U5        XR                  S'   UR                  SU R                   S35      nUb9  UR                  SU R                   S35       H  nU R                  UUU	5        M     UR                  SU R                   S35      nUb9  UR                  SU R                   S35       H  nU R                  UUU5        M     U R                  (       aC  UR                  5       (       a  [        R                  " U5      nU$ [        R                   " U5      nU$ )Nr   r   r   r   r   r   r   r   r   r4  r3  r9   r  z}attributesr   r~   r   r   r   r   r7   )r   r   r   }nodes}nodez}edgesz}edge)rh   ri   MultiDiGraph
MultiGraphr   r4  r   r-   find_gexf_attributesr   rK  add_nodeadd_edgerG  r   DiGraphGraph)rZ   	graph_xmledgedefaultr   
graph_namegraph_start	graph_end
graph_modeattributes_elements	node_attrr   	edge_attrr   rB   
attr_classnandeaedr   node_xmlr   edge_xmls                          r   rL  GEXFReader.make_graph  s   mm$5t<*$!AA ]]62.
(GGFOmmG,"*GGGMM%(	 &GGEN]]62.
"'AGGFO&AGGFO $--5??f$&DO (//"T\\N,0OP		$AwJV#2215B  $##B'*6'v%2215B  $##B'*6' %" 8hOPB". "DLL>'AB$)11Bt||nF2KLa95 M "DLL>'AB$)11Bt||nF2KLa95 M }}JJqM  HHQKr   c                    U R                  X25      nU R                  XR5      nU R                  S:X  a  U R                  XR5      nOU R	                  XR5      nU R                  XR5      nU R                  XR5      nUR                  S5      nU R                  b  U R                  U5      nUR                  S5      nXuS'   UR                  SU5      nUb  XES'   UR                  SU R                   S35      nUb6  UR                  SU R                   S35       H  nU R                  XX6S9  M     UR                  " U40 UD6  g )	Nr,   r   r   r   r  rQ  rR  )node_pid)decode_attr_elementsr   r1   r   r   r   add_start_endrh   r$   rK  r-   r   rV  )	rZ   r   rh  ra  rl  r   r   
node_labelsubnodess	            r   rV  GEXFReader.add_node)  s1    ((=/<<5 ??42D??42D||D+!!$1 ,,t$>>%nnW-G \\'*
"W <<x0"K ==2dll^7!;<$,,r$,,v-FGa9G H 	


7#d#r   c                     U R                   nUR                  S5      nUb  U R                  U   " U5      US'   UR                  S5      nUb  U R                  U   " U5      US'   U$ )Nr   r   )r4  rh   rY   )rZ   r   r   ttype
node_startnode_ends         r   rn  GEXFReader.add_start_endL  si    WWW%
! ,,U3J?DM775>**51(;DKr   c           	         0 nUR                  SU R                   S35      nUb  U R                  S:X  aR  [        UR	                  S5      5      [        UR	                  S5      5      [        UR	                  S5      5      S.US'   Ok[        UR	                  S5      5      [        UR	                  S5      5      [        UR	                  S5      5      [        UR	                  S	S
5      5      S.US'   UR                  SU R                   S35      nUb  [        UR	                  S5      5      US'   UR                  SU R                   S35      nUb  [        UR	                  S5      5      US'   UR                  SU R                   S35      nUb1  UR	                  S5      US'   US   S:X  a  UR	                  S5      US'   UR                  SU R                   S35      nUbT  [        UR	                  SS5      5      [        UR	                  SS5      5      [        UR	                  SS5      5      S.US'   [        U5      S:  a  X1S'   U$ )Nr  r  r,   r	  r
  r  r  r  rB   r   r  r  r   r  r  r  r  r  r  r  r  r  r   r  r  r  r  r}   )rK  r.   r1   r=   rh   r6   r   )	rZ   r   rh  r}   r  r  r  r  r  s	            r   r   GEXFReader.add_vizW  s   4;;-w78||u$UYYs^,UYYs^,UYYs^, G UYYs^,UYYs^,UYYs^,uyya01	 G }}r$++f56 12CKMMBt{{m;"?@	 $Y]]7%;<C4;;-w78 99W-CL7|w&$yy/G==2dkk]*!=>8<<Q/08<<Q/08<<Q/0C
O s8a<Kr   c                     UR                  SU R                   S35      nUbP  / US'   UR                  SU R                   S35       H(  nUR                  S5      nUS   R	                  U5        M*     U$ )Nr  z}parentsr!  z}parentr   rK  r-   r   rh   r   )rZ   r   rh  r#  r$  r"  s         r   r   GEXFReader.add_parents  su    "--"T\\N)(DE& DO$,,r$,,x-HIuY&&v. J r   c                    UR                  SU R                   S35      nUbb  / US'   UR                  SU R                   S35       H:  nUR                  S5      nUR                  S5      nUS   R	                  XV45        M<     U$ )Nr  z}slicesr'  z}slicer   r   rz  )rZ   r   node_or_edge_xmlr+  r   r   r   s          r   r   GEXFReader.add_slices  s    )..DLL>/JK%DN#++bg,FGgeeElX%%ul3 H r   c                 r   UR                  SU R                   S35      nUb  / US'   U R                  nUR                  SU R                   S35       H`  nU R                  U   " UR                  S5      5      nU R                  U   " UR                  S5      5      nUS   R                  Xg45        Mb     U$ )Nr  z}spellsr.  z}spellr   r   )rK  r-   r4  r   rY   rh   r   )rZ   r   r}  r0  rs  r   r   r   s           r   r   GEXFReader.add_spells  s    )..DLL>/JK%DNOOE#++bg,FG((/g?&&u-aeeEl;X%%ul3 H r   c                    UR                  S5      nUR                  5       (       a  US:X  a  [        R                  " S5      eUR                  5       (       d  US:X  a  [        R                  " S5      eUR                  S5      nUR                  S5      nU R                  b"  U R	                  U5      nU R	                  U5      nU R                  X25      nU R                  Xr5      nU R                  S:X  a  U R                  Xr5      nOU R                  Xr5      nUR                  S	5      nUb  XS	'   UR                  S
S 5      n	U	b  U	nUR                  S5      n
U
b  [        U
5      US'   UR                  S5      nUb  XS'   UR                  XV5      (       a  SU l        UR                  " XV4SU0UD6  US:X  a  UR                  " Xe4SU0UD6  g g )Nr   r   z(Undirected edge found in directed graph.r   z(Directed edge found in undirected graph.r   r   r,   r   r   r   r   Fr   mutual)rh   r   ri   rj   r$   rm  rn  r1   r   r   r   r6   has_edgerG  rW  )rZ   r   r   rb  edge_directionr   r   r   r   multigraph_keyr   r   s               r   rW  GEXFReader.add_edge  s    &))&1==??~=""#MNN^z%A""#MNN !!(+!!(+>>%^^F+F^^F+F((A!!$5<<5 ??46D??46D
 ""4( J .$7%$G!!(+"6]DN!%%g.
!&M::f%% %D	

67w7$7X%JJv;7;d; &r   c                    0 nUR                  SU R                   S35      nUGb
  UR                  SU R                   S35       H  nUR                  S5      n X   S   nX   S   n	UR                  S	5      n
U	S
:X  a  U R                  U
   n
OU R                  U	   " U
5      n
X   S   S:X  ay  U R                  nU R                  U   " UR                  S5      5      nU R                  U   " UR                  S5      5      nXs;   a  X7   R                  XU45        M  XU4/X7'   M  XU'   M     U$ ! [         a   n[
        R                  " SU S35      UeS nAff = f)Nr  z
}attvaluesz	}attvaluer   r   zNo attribute defined for=rf   r   r   r8   r   r   r   r   )rK  r-   r   rh   r   ri   rj   rs   rY   r4  r   )rZ   	gexf_keysobj_xmlr   attr_elementrB   r   r   erratyper   rs  r   r   s                 r   rm  GEXFReader.decode_attr_elements  sq   ||bk$BC#!))Bt||nJ*GHeeElX%N73E "v.gI% --e4E ,,U3E:E>&)Y6 !OOE ,,U3AEE'NCE**51!%%,?C}**E#+>?(-c':&; #(K1 I2 +   X**-Fse1+MNTWWXs   D22
E<EEc                    0 n0 nUR                  S5      nUR                  SU R                   S35       H  nUR                  S5      nUR                  S5      nUR                  S5      nXxUS.X&'   UR                  SU R                   S35      n	U	c  Ma  US	:X  a  U R                  U	R
                     n
OU R                  U   " U	R
                  5      n
XU'   M     X#4$ )
Nr   r  z
}attributer   r   r   )r   r   r   z}defaultr8   )rh   r   r-   rK  rs   r   rY   )rZ   r  attrsdefaultsr   r   r   r   r  r   r   s              r   rU  GEXFReader.find_gexf_attributes  s    !%%f-#++bk,JKAeeDkGEE'NEEE&ME',TJENffr$,,y9:G"I% --gll;E ,,U3GLLAE"' L r   )r$   rG  r4  r   )Nr3   r?   )rn   ro   rp   rq   r   rN  rL  rV  rn  r   r   r   r   rW  rm  rU  rt   ru   r   r   r%   r%     sE    "CM^!$F	+Z	2<h Dr   r%   c                     U  Vs/ s H  oU R                   U   S   4PM     nn[	        U6 u  pE[        [        U5      5      [        U 5      :w  a  [        R                  " S5      e[        U5      n[        R                  " X5      nU  H  nX'   nXvR                   U   S'   UR                   U   R                  S5        SUR                   U   ;   a$  X R                   U   S      UR                   U   S'   SUR                   U   ;   d  M  U R                   U   S    V	s/ s H  oU	   PM	     sn	UR                   U   S'   M     U$ s  snf ! [         a  n[        R                  " S5      UeSnAff = fs  sn	f )a  Relabel graph using "label" node keyword for node label.

Parameters
----------
G : graph
   A NetworkX graph read from GEXF data

Returns
-------
H : graph
  A NetworkX graph with relabeled nodes

Raises
------
NetworkXError
    If node labels are missing or not unique while relabel=True.

Notes
-----
This function relabels the nodes in a NetworkX graph with the
"label" attribute.  It also handles relabeling the specific GEXF
node attributes "parents", and "pid".
r   zFFailed to relabel nodes: missing node labels found. Use relabel=False.NzHFailed to relabel nodes: duplicate node labels found. Use relabel=False.r   r   r!  )
r   r   ri   rj   zipr   r   rG   relabel_nodesr   )
r   r   mappingr  r  r  Hnmr$  s
             r   r   r     s`   2567Qqwwqz'*+Q7
 =DA
3q6{c!fV
 	
 7mG
$AJ
4	
wAGGAJ '
5(9 :AGGAJu
"9:I9N$O9NAQZ9N$OAGGAJy!  H+ 8 T
	& %Ps-   E EE E4E 
E1E,,E1)rD  Tr3   )NFr3   )__doc__r   r   xml.etree.ElementTreer   r   r   r   r   networkxri   networkx.utilsr   __all__r	   r   _dispatchabler
   r)   r   r%   r   ru   r   r   <module>r     s   &     $
L 14; ;|*(Z 14T2( 3 (Vg gTY YxG GT
/r   