o
    œÇh›  ã                   @   sÊ   d Z ddlZddlZddlmZmZmZmZmZ ddl	Z
ddlmZ g d¢Zedddddd„ƒZddd„Zeddde
jdd
dddd„ƒƒZG dd„ dƒZG dd„ deƒZG dd„ deƒZdd„ ZdS )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)Úmodeúutf-8Tú1.2draftc                 C   s&   t |||d}| | ¡ | |¡ dS )a4  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.

    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   Úwriter© r   úk/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/networkx/readwrite/gexf.pyr   $   s   2
r   c                 c   s0    t |||d}| | ¡ t|ƒ ¡ E dH  dS )aÌ  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   r   r   r   r   [   s   €(
r   Úrb)ÚgraphsÚreturns_graphFc                 C   s,   t ||d}|rt|| ƒƒ}|S || ƒ}|S )aw  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
       File or file name to read.
       File names 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   r   r   r   r	   ˆ   s   %ÿr	   c                	   @   sh   e Zd Zdddd ddg¡ddœdd	dd dd
g¡ddœdœZdd„ ZdddddddddœZdd„ ZdS )ÚGEXFz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.2)z1.1draftr   c                 C   s  t dftdftdftdftdftdft dftdftdftdfg
}zd	d l}W n	 ty/   Y nAw |jdf|j	df|j
df|jd
f|jd
f|jd
f|jd
f|jd
f|jd
f|jd
f|jd
f|jd
f|jd
f|jd
f|jd
fg| }t|ƒ| _tdd„ |D ƒƒ| _d S )NÚintegerÚfloatÚdoubleÚbooleanÚstringÚlongÚ
liststringÚanyURIr   Úintc                 s   s    | ]}t |ƒV  qd S ©N)Úreversed)Ú.0Úar   r   r   Ú	<genexpr>ù   s   € z'GEXF.construct_types.<locals>.<genexpr>)r6   r/   ÚboolÚlistÚdictr   ÚnumpyÚImportErrorÚfloat64Úfloat32Úfloat16Úint_Úint8Úint16Úint32Úint64Úuint8Úuint16Úuint32Úuint64ÚintcÚintpÚxml_typeÚpython_type)ÚselfÚtypesÚnpr   r   r   Úconstruct_typesÑ   sH   öÿñð
zGEXF.construct_typesTF)ÚtrueÚfalseÚTrueÚFalseÚ0r   Ú1r   c                 C   sb   | j  |¡}|d u rt d|› d¡‚|d | _|d | _|d | _|d | _|d | _|| _	d S )NzUnknown GEXF version Ú.r)   r*   r+   r,   r-   )
ÚversionsÚgetÚnxÚNetworkXErrorr)   r*   r+   r,   r-   r   )rQ   r   Údr   r   r   Úset_version  s   





zGEXF.set_versionN)Ú__name__Ú
__module__Ú__qualname__Újoinr\   rT   Úconvert_boolra   r   r   r   r   r&   µ   sB    þÿöþÿöó,ør&   c                   @   s‚   e Zd Z	d"dd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd#d d!„ZdS )$r   Nr   Tr   c                 C   s  |   ¡  || _|| _|  |¡ td| j| j| j| jdœƒ| _	tdƒ}dt
j› }|t|dƒ_| dt d¡¡ | j	 |¡ td| jƒ t ¡ | _t ¡ | _tƒ | _i | _i | jd	< i | jd
< i | jd	 d< i | jd	 d< i | jd
 d< i | jd
 d< |d ur…|  |¡ d S d S )NÚgexf)Úxmlnsz	xmlns:xsizxsi:schemaLocationr   Úmetaz	NetworkX ÚcreatorÚlastmodifieddatez%Y-%m-%dÚvizÚnodeÚedgeÚdynamicÚstatic)rT   r   r   ra   r   r)   r+   r,   r-   Úxmlr^   Ú__version__r   ÚtextÚsetÚtimeÚstrftimeÚappendr   r*   Ú	itertoolsÚcountÚedge_idÚattr_idÚall_edge_idsÚattrr   )rQ   Úgraphr   r   r   Úmeta_elementÚsubelement_textr   r   r   Ú__init__  s>   
üþ



ÿzGEXFWriter.__init__c                 C   s(   | j r	|  | j¡ t| jƒ | j¡}|S r7   )r   Úindentrq   r   Údecoder   )rQ   Úsr   r   r   Ú__str__A  s   zGEXFWriter.__str__c           
      C   s²   |j ddD ]\}}}| d¡}|d ur| j t|ƒ¡ q|j d¡dkr(d}nd}| ¡ r1d}nd}|j d	d
¡}td|||d}	|	| _|  	||	¡ |  
||	¡ | j |	¡ d S )NT©ÚdataÚidr   ro   rp   ÚdirectedÚ
undirectedÚnameÚ r~   )Údefaultedgetyper   r‹   )Úedgesr]   r|   Úaddr   r~   Úis_directedr   Úgraph_elementÚ	add_nodesÚ	add_edgesrq   rw   )
rQ   r   ÚuÚvÚddÚeidr   Údefaultr‹   r‘   r   r   r   r   G  s"   
€zGEXFWriter.add_graphc              	   C   st  t dƒ}|jddD ]¨\}}| ¡ }t| d|¡ƒ}d|i}t| d|¡ƒ}	|	|d< z| d¡}
t|
ƒ|d< W n	 ty@   Y nw z| d¡}t|ƒ|d< |  |¡ W n	 ty\   Y nw z| d¡}t|ƒ|d< |  |¡ W n	 tyx   Y nw t di |¤Ž}|j d
i ¡}|  	||¡}| j
dkr™|  ||¡}n|  ||¡}|  ||¡}|  d	|||¡}| |¡ q
| |¡ d S )NÚnodesTr†   rˆ   ÚlabelÚpidÚstartÚendrm   Únode_defaultr(   )rm   )r   r™   Úcopyr   ÚpopÚKeyErrorÚalter_graph_mode_timeformatr~   r]   Úadd_parentsr-   Ú
add_slicesÚ
add_spellsÚadd_vizÚadd_attributesrw   )rQ   r   r‘   Únodes_elementrm   r‡   Ú	node_dataÚnode_idÚkwrš   r›   rœ   r   Únode_elementr˜   r   r   r   r’   ^  sJ   
ÿ
ÿ
ÿ
zGEXFWriter.add_nodesc              	      sÖ  ‡ fdd„}t dƒ}||ƒD ]Õ\}}}}dt|ƒi}	z| d¡}
t|
ƒ|	d< W n	 ty0   Y nw z| d¡}t|ƒ|	d< W n	 tyG   Y nw z| d¡}t|ƒ|	d< W n	 ty^   Y nw z| d¡}t|ƒ|	d< ˆ  |¡ W n	 tyz   Y nw z| d	¡}t|ƒ|	d	< ˆ  |¡ W n	 ty–   Y nw t|j|  d|¡ƒ}t|j|  d|¡ƒ}t d||dœ|	¤Ž}|j di ¡}ˆ jdkrÊˆ  	||¡}nˆ  
||¡}ˆ  ||¡}ˆ  d
|||¡}| |¡ q| |¡ d S )Nc                 3   s&   |   ¡ rP| jdddD ]A\}}}}| ¡ }|j|d | dd ¡}|d u rFtˆ jƒ}t|ƒˆ jv r>tˆ jƒ}t|ƒˆ jv s2ˆ j 	t|ƒ¡ ||||fV  qd S | jddD ]:\}}}| ¡ }| dd ¡}|d u r‰tˆ jƒ}t|ƒˆ jv rtˆ jƒ}t|ƒˆ jv suˆ j 	t|ƒ¡ ||||fV  qVd S )NT)r‡   Úkeys)Úkeyrˆ   r†   )
Úis_multigraphrŽ   rŸ   Úupdater    Únextrz   r   r|   r   )r   r”   r•   r®   r‡   Ú	edge_datarz   ©rQ   r   r   Úedge_key_data†  s2   €

ÿ÷

ÿøz+GEXFWriter.add_edges.<locals>.edge_key_datarŽ   rˆ   rš   ÚweightÚtyperœ   r   rn   )ÚsourceÚtargetÚedge_defaultr(   )rn   )r   r   r    r¡   r¢   r™   r]   r~   r-   r¤   r¥   r¦   r§   rw   )rQ   r   r‘   r´   Úedges_elementr”   r•   r®   r²   r«   Ú
edge_labelÚedge_weightÚ	edge_typerœ   r   Ú	source_idÚ	target_idÚedge_elementr˜   r   r³   r   r“   …  s^   
ÿ
ÿ
ÿ
ÿ
ÿ
zGEXFWriter.add_edgesc                 C   sT  t dƒ}t|ƒdkr|S d}| ¡ D ]\}}|dkrd}t|ƒ}	|	| jvr-td|	› ƒ‚t|tƒrÀ|D ]\}
}}t|
ƒ}	|d usE|d urSd}|  |¡ |  |¡  nq4|  	t
|ƒ| j|	 |||¡}|D ]Z\}
}}t dƒ}||jd	< t
|
ƒ|jd
< |	tkr£|jd
 dkrŠd|jd
< n|jd
 dkr—d|jd
< n|jd
 dkr£d|jd
< |d ur®t
|ƒ|jd< |d ur¹t
|ƒ|jd< | |¡ qdqd}|  	t
|ƒ| j|	 |||¡}t dƒ}||jd	< t|tƒrèt
|ƒ ¡ |jd
< n5t
|ƒ|jd
< |	tkr|jd
 dkrd|jd
< n|jd
 dkrd|jd
< n|jd
 dkrd|jd
< | |¡ q| |¡ |S )NÚ	attvaluesr   rp   r®   Únetworkx_keyz%attribute value type is not allowed: ro   ÚattvalueÚforÚvalueÚinfÚINFÚnanÚNaNz-infz-INFrœ   r   )r   ÚlenÚitemsr¶   rO   Ú	TypeErrorÚ
isinstancer=   r¢   Úget_attr_idr   Úattribr/   rw   r<   Úlower)rQ   Únode_or_edgeÚxml_objr‡   r˜   rÁ   r   Úkr•   Úval_typeÚvalrœ   r   r{   Úer   r   r   r§   É  st   



üÿ

ðÿ




zGEXFWriter.add_attributesc                 C   s
  z
| j | | | W S  ty„   tt| jƒƒ}|| j | | |< |||dœ}td
i |¤Ž}| |¡}	|	d urEtdƒ}
t|	ƒ|
_| |
¡ d }| j	 
d¡D ]}| d¡}| dd¡}||krd||krd|}qM|d u r|||dœ}tdi |¤Ž}| j	 d	|¡ | |¡ Y |S w )N)rˆ   Útitler¶   Ú	attributer˜   Ú
attributesÚclassr   rp   )r   rÚ   r   )rØ   )rÙ   )r}   r¡   r   r±   r{   r   r]   rs   rw   r‘   ÚfindallÚinsert)rQ   r×   Ú	attr_typeÚedge_or_noder˜   r   Únew_idÚattr_kwargsrØ   Údefault_titleÚdefault_elementÚattributes_elementr:   Úa_classÚa_moder   r   r   rÎ     s4   



€
æzGEXFWriter.get_attr_idc           
   
   C   sØ  |  dd¡}|rê| d¡}|d ur[| jdkr3td| j› dt| d¡ƒt| d¡ƒt| d	¡ƒd
}n#td| j› dt| d¡ƒt| d¡ƒt| d	¡ƒt| dd¡ƒd}| |¡ | d¡}|d urvtd| j› dt|ƒd}| |¡ | d¡}|d ur‘td| j› dt|ƒd}| |¡ | d¡}|d urÀ| d¡r®td| j› ddt|ƒd}ntd| j› dt|ƒd}| |¡ | d¡}	|	d urêtd| j› dt|	 d¡ƒt|	 d¡ƒt|	 d¡ƒd}| |¡ |S )Nrl   FÚcolorr(   Ú{ú}colorÚrÚgÚb©ré   rê   rë   r:   g      ð?©ré   rê   rë   r:   Úsizeú}size)rÅ   Ú	thicknessú
}thicknessÚshapeÚhttpú}shapeÚimage)rÅ   ÚuriÚpositionú	}positionÚxÚyÚz©rù   rú   rû   )r    r]   r-   r   r*   r   rw   Ú
startswith)
rQ   Úelementr©   rl   ræ   rÖ   rî   rð   rò   r÷   r   r   r   r¦   +  sZ   

üû






ÿ

ü
zGEXFWriter.add_vizc                 C   sP   |  dd¡}|r&tdƒ}|D ]}tdƒ}t|ƒ|jd< | |¡ q| |¡ |S )NÚparentsFÚparentrÄ   )r    r   r   rÏ   rw   )rQ   r¬   r©   rÿ   Úparents_elementÚprÖ   r   r   r   r£   `  s   
zGEXFWriter.add_parentsc                 C   sT   |  dd¡}|r(tdƒ}|D ]\}}tdt|ƒt|ƒd}| |¡ q| |¡ |S )NÚslicesFÚslice)rœ   r   )r    r   r   rw   )rQ   Únode_or_edge_elementÚnode_or_edge_datar  Úslices_elementrœ   r   rÖ   r   r   r   r¤   k  s   
zGEXFWriter.add_slicesc                 C   s†   |  dd¡}|rAtdƒ}|D ]-\}}tdƒ}|d ur&t|ƒ|jd< |  |¡ |d ur6t|ƒ|jd< |  |¡ | |¡ q| |¡ |S )NÚspellsFÚspellrœ   r   )r    r   r   rÏ   r¢   rw   )rQ   r  r  r  Úspells_elementrœ   r   rÖ   r   r   r   r¥   u  s   


zGEXFWriter.add_spellsc                 C   sz   | j  d¡dkr9|d ur;t|tƒrd}nt|tƒrd}nt|tƒr$d}nt d¡‚| j  d|¡ | j  dd¡ d S d S d S )	Nr   rp   Údater0   r3   z2timeformat should be of the type int, float or strÚ
timeformatro   )	r‘   r]   rÍ   r   r/   r6   r^   r_   rt   )rQ   Ústart_or_endr  r   r   r   r¢   …  s   


ÿóz&GEXFWriter.alter_graph_mode_timeformatc                 C   s2   | j r	|  | j¡ t| jƒ}|j|| jdd d S )NT)r   Úxml_declaration)r   r‚   rq   r   r   r   )rQ   ÚfhÚdocumentr   r   r   r   —  s   
zGEXFWriter.writer   c                 C   s¢   dd|  }t |ƒr>|jr|j ¡ s|d |_|jr|j ¡ s"||_|D ]
}|  ||d ¡ q$|jr7|j ¡ s<||_d S d S |rM|jrH|j ¡ sO||_d S d S d S )NÚ
z  r   )rÊ   rs   ÚstripÚtailr‚   )rQ   ÚelemÚlevelÚir   r   r   r‚   ž  s   

ÿ
ÿzGEXFWriter.indent)Nr   Tr   )r   )rb   rc   rd   r   r…   r   r’   r“   r§   rÎ   r¦   r£   r¤   r¥   r¢   r   r‚   r   r   r   r   r     s     
ÿ+'DB 5
r   c                   @   sp   e Zd Zddd„Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )r#   Nr   c                 C   s"   |   ¡  || _d| _|  |¡ d S )NT)rT   r"   Úsimple_graphra   )rQ   r"   r   r   r   r   r   ²  s   zGEXFReader.__init__c                 C   s€   t |d| _| j d| j› d¡}|d ur|  |¡S | jD ]}|  |¡ | j d| j› d¡}|d ur:|  |¡  S qt d¡‚)N)Úfilerç   z}graphz No <graph> element in GEXF file.)	r   rq   Úfindr)   Ú
make_graphr\   ra   r^   r_   )rQ   Ústreamrê   r   r   r   r   Ú__call__¹  s   


ÿ
zGEXFReader.__call__c                 C   sR  |  dd ¡}|dkrt ¡ }nt ¡ }|  dd¡}|dkr"||jd< |  d¡}|d ur0||jd< |  d¡}|d ur>||jd< |  dd¡}|dkrNd|jd< nd	|jd< |  d
¡| _| jdkrad| _| d| j› d¡}i }	i }
i }i }|D ]=}|  d¡}|dkr—|  |¡\}}|	 	|¡ |
 	|¡ |
|jd< qu|dkr²|  |¡\}}| 	|¡ | 	|¡ ||jd< qu‚ ddd	ddœi}i }| 	|¡ | 	|¡ ||jd< | 
d| j› d¡}|d urî| d| j› d¡D ]	}|  |||	¡ qä| 
d| j› d¡}|d ur| d| j› d¡D ]
}|  |||¡ q| jr'| ¡ r"t |¡}|S t |¡}|S )Nr   r‰   r‹   rŒ   rœ   r   r   ro   rp   r  r  r2   rç   z}attributesrÚ   rm   rž   rn   r¹   rµ   r0   )r¶   r   r×   ú}nodesú}nodez}edgesz}edge)r]   r^   ÚMultiDiGraphÚ
MultiGraphr~   r  rÛ   r)   Úfind_gexf_attributesr°   r  Úadd_nodeÚadd_edger  r   ÚDiGraphÚGraph)rQ   Ú	graph_xmlÚedgedefaultr   Ú
graph_nameÚgraph_startÚ	graph_endÚ
graph_modeÚattributes_elementsÚ	node_attrrž   Ú	edge_attrr¹   r:   Ú
attr_classÚnaÚndÚeaÚedr¨   Únode_xmlrº   Úedge_xmlr   r   r   r  Æ  sr   



















ÿzGEXFReader.make_graphc           	      C   sþ   |   ||¡}|  ||¡}| jdkr|  ||¡}n|  ||¡}|  ||¡}|  ||¡}| d¡}| jd ur9|  |¡}| d¡}||d< | d|¡}|d urP||d< | 	d| j
› d¡}|d urt| d| j
› d¡D ]}| j||||d qh|j|fi |¤Ž d S )	Nr(   rˆ   rš   r›   rç   r  r  )Únode_pid)Údecode_attr_elementsr£   r-   r¤   r¥   r¦   Úadd_start_endr]   r"   r  r)   rÛ   r"  )	rQ   r   r4  r-  r6  r‡   rª   Ú
node_labelÚsubnodesr   r   r   r"    s(   




zGEXFReader.add_nodec                 C   sR   | j }| d¡}|d ur| j| |ƒ|d< | d¡}|d ur'| j| |ƒ|d< |S )Nrœ   r   )r  r]   rP   )rQ   r‡   rq   ÚttypeÚ
node_startÚnode_endr   r   r   r8  8  s   

zGEXFReader.add_start_endc           	      C   sž  i }|  d| j› d¡}|d urK| jdkr-t| d¡ƒt| d¡ƒt| d¡ƒdœ|d< nt| d¡ƒt| d¡ƒt| d¡ƒt| d	d
¡ƒdœ|d< |  d| j› d¡}|d urbt| d¡ƒ|d< |  d| j› d¡}|d uryt| d¡ƒ|d< |  d| j› d¡}|d ur›| d¡|d< |d dkr›| d¡|d< |  d| j› d¡}|d urÃt| dd¡ƒt| dd¡ƒt| dd¡ƒdœ|d< t|ƒdkrÍ||d< |S )Nrç   rè   r(   ré   rê   rë   rì   ræ   r:   r   rí   rï   rÅ   rî   rñ   rð   rô   rò   rõ   rö   rø   rù   r   rú   rû   rü   r÷   rl   )r  r*   r-   r6   r]   r/   rÊ   )	rQ   r‡   r4  rl   ræ   rî   rð   rò   r÷   r   r   r   r¦   C  sB   
ý
ü
ýzGEXFReader.add_vizc                 C   sZ   |  d| j› d¡}|d ur+g |d< | d| j› d¡D ]}| d¡}|d  |¡ q|S )Nrç   z}parentsrÿ   z}parentrÄ   ©r  r)   rÛ   r]   rw   )rQ   r‡   r4  r  r  r   r   r   r   r£   p  s   
zGEXFReader.add_parentsc                 C   sh   |  d| j› d¡}|d ur2g |d< | d| j› d¡D ]}| d¡}| d¡}|d  ||f¡ q|S )Nrç   z}slicesr  z}slicerœ   r   r>  )rQ   r‡   Únode_or_edge_xmlr  r„   rœ   r   r   r   r   r¤   y  s   

zGEXFReader.add_slicesc                 C   s‚   |  d| j› d¡}|d ur?g |d< | j}| d| j› d¡D ]}| j| | d¡ƒ}| j| | d¡ƒ}|d  ||f¡ q|S )Nrç   z}spellsr  z}spellrœ   r   )r  r)   r  rÛ   rP   r]   rw   )rQ   r‡   r?  r
  r;  r„   rœ   r   r   r   r   r¥   ƒ  s   zGEXFReader.add_spellsc                 C   sh  |  d¡}| ¡ r|dkrt d¡‚| ¡ s|dkrt d¡‚|  d¡}|  d¡}| jd ur8|  |¡}|  |¡}|  ||¡}|  ||¡}| jdkrP|  ||¡}n|  	||¡}|  d	¡}|d urc||d	< | 
d
d ¡}	|	d uro|	}|  d¡}
|
d ur~t|
ƒ|d< |  d¡}|d ur‹||d< | ||¡r”d| _|j||fd|i|¤Ž |dkr²|j||fd|i|¤Ž d S d S )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)r]   r   r^   r_   r"   r7  r8  r-   r¤   r¥   r    r/   Úhas_edger  r#  )rQ   r   rÀ   r.  Úedge_directionr·   r¸   r‡   rz   Úmultigraph_keyrµ   r»   r   r   r   r#  Ž  sB   











ÿzGEXFReader.add_edgec                 C   s4  i }|  d| j› d¡}|d ur˜| d| j› d¡D ]}}| d¡}z|| d }W n ty? } z
t d|› d¡|‚d }~ww || d }	| d	¡}
|	d
krU| j|
 }
n| j|	 |
ƒ}
|| d dkr“| j	}| j| | d¡ƒ}| j| | d¡ƒ}||v rŠ||  
|
||f¡ q|
||fg||< q|
||< q|S )Nrç   z
}attvaluesz	}attvaluerÄ   r×   zNo attribute defined for=r[   r¶   rÅ   r1   r   ro   rœ   r   )r  r)   rÛ   r]   r¡   r^   r_   rf   rP   r  rw   )rQ   Ú	gexf_keysÚobj_xmlr}   Úattr_elementr:   r®   r×   ÚerrÚatyperÅ   r;  rœ   r   r   r   r   r7  Â  s2   
€ÿ

zGEXFReader.decode_attr_elementsc                 C   s¬   i }i }|  d¡}| d| j› d¡D ]>}|  d¡}|  d¡}|  d¡}|||dœ||< | d| j› d¡}	|	d urQ|d	krE| j|	j }
n| j| |	jƒ}
|
||< q||fS )
Nr   rç   z
}attributerˆ   r×   r¶   )r×   r¶   r   z}defaultr1   )r]   rÛ   r)   r  rf   rs   rP   )rQ   rã   ÚattrsÚdefaultsr   rÓ   r{   r×   rH  r˜   rÅ   r   r   r   r!  ä  s    



€zGEXFReader.find_gexf_attributes)Nr   r7   )rb   rc   rd   r   r  r  r"  r8  r¦   r£   r¤   r¥   r#  r7  r!  r   r   r   r   r#   ¯  s    

O#-	
4"r#   c              
      s  z‡ fdd„ˆ D ƒ‰W n t y } zt d¡|‚d}~ww tˆŽ \}}tt|ƒƒtˆ ƒkr3t d¡‚tˆƒ‰t ˆ ˆ¡}ˆ D ]D}ˆ| }||j| d< |j|  	d¡ d|j| v riˆˆ j| d  |j| d< d	|j| v rƒ‡fd
d„ˆ j| d	 D ƒ|j| d	< q?|S )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".
    c                    s   g | ]}|ˆ j | d  f‘qS )rš   )r™   )r9   r”   )r   r   r   Ú
<listcomp>  s    z&relabel_gexf_graph.<locals>.<listcomp>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ÿ   c                    s   g | ]}ˆ | ‘qS r   r   )r9   r  )Úmappingr   r   rK  )  s    )
r¡   r^   r_   ÚziprÊ   rt   r>   Úrelabel_nodesr™   r    )r   rG  rù   rú   ÚHÚnÚmr   )r   rL  r   r
   ù  s6   ÿþ€ÿÿ&€r
   )r   Tr   )NFr   )Ú__doc__rx   ru   Úxml.etree.ElementTreer   r   r   r   r   Únetworkxr^   Únetworkx.utilsr   Ú__all__r   r   Ú_dispatchabler	   r&   r   r#   r
   r   r   r   r   Ú<module>   s,    

6
-+^     L