U
    :vfÿš  ã                   @   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 ddddgZed	d
dddd„ƒZddd„Ze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   ú;/tmp/pip-unpacked-wheel-_lngutwb/networkx/readwrite/gexf.pyr   #   s    2
c                 c   s.   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   Z   s    (
ÚrbFc                 C   s*   t ||d}|rt|| ƒƒ}n|| ƒ}|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    Zrelabelr   Úreaderr   r   r   r   r	   ‡   s
    $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k
r`   Y nŠX |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|jd
fg| }t|ƒ| _tdd„ |D ƒƒ| _d S )NÚintegerÚfloatÚdoubleÚbooleanÚstringÚlongZ
liststringZanyURIr   Úintc                 s   s   | ]}t |ƒV  qd S ©N)Úreversed)Ú.0Úar   r   r   Ú	<genexpr>ø   s     z'GEXF.construct_types.<locals>.<genexpr>)r1   r,   ÚboolÚlistÚdictr   ZnumpyÚImportErrorZfloat64Zfloat32Zfloat16Zfloat_Úint_Zint8Zint16Zint32Zint64Zuint8Zuint16Zuint32Zuint64ZintcZ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 k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   )r>   r   Údr   r   r   Úset_version  s    




zGEXF.set_versionN)Ú__name__Ú
__module__Ú__qualname__ÚjoinrI   rA   Úconvert_boolrN   r   r   r   r   r#   ³   s@   þÿöþÿöó-ø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 k	r|  |¡ d S )NZgexf)Úxmlnsz	xmlns:xsizxsi:schemaLocationr   Úmetaz	NetworkX ZcreatorZlastmodifieddatez%Y-%m-%dÚvizÚnodeÚedgeÚdynamicÚstatic)rA   r   r   rN   r   r&   r(   r)   r*   ÚxmlrK   Ú__version__r   ÚtextÚsetÚtimeÚstrftimeÚappendr   r'   Ú	itertoolsÚcountÚedge_idÚattr_idÚall_edge_idsÚattrr   )r>   Úgraphr   r   r   Zmeta_elementZsubelement_textr   r   r   Ú__init__  s<    
üþ




zGEXFWriter.__init__c                 C   s(   | j r|  | j¡ t| jƒ | j¡}|S r2   )r   Úindentr[   r   Údecoder   )r>   Úsr   r   r   Ú__str__@  s    zGEXFWriter.__str__c           
      C   s²   |j ddD ],\}}}| d¡}|d k	r| j t|ƒ¡ q|j d¡dkrPd}nd}| ¡ rbd}nd}|j d	d
¡}td|||d}	|	| _|  	||	¡ |  
||	¡ | j |	¡ d S )NT©ÚdataÚidr   rY   rZ   ÚdirectedÚ
undirectedÚnameÚ rh   )Údefaultedgetyper   rs   )ÚedgesrJ   rf   Úaddr   rh   Úis_directedr   Úgraph_elementÚ	add_nodesÚ	add_edgesr[   ra   )
r>   r   ÚuÚvÚddZeidr   Údefaultrs   ry   r   r   r   r   F  s     
zGEXFWriter.add_graphc              	   C   sz  t dƒ}|jddD ]T\}}| ¡ }t| d|¡ƒ}d|i}t| d|¡ƒ}	|	|d< z| d¡}
t|
ƒ|d< W n tk
r„   Y nX z$| d¡}t|ƒ|d< |  |¡ W n tk
r¾   Y nX z$| d¡}t|ƒ|d< |  |¡ W n tk
rø   Y nX t d|Ž}|j d
i ¡}|  	||¡}| j
dkr8|  ||¡}n|  ||¡}|  ||¡}|  d	|||¡}| |¡ q| |¡ d S )NÚnodesTrn   rp   ÚlabelÚpidÚstartÚendrW   Únode_defaultr%   )rW   )r   r€   Úcopyr   ÚpopÚKeyErrorÚalter_graph_mode_timeformatrh   rJ   Úadd_parentsr*   Ú
add_slicesÚ
add_spellsÚadd_vizÚadd_attributesra   )r>   r   ry   Únodes_elementrW   ro   Ú	node_dataÚnode_idÚkwr   r‚   rƒ   r„   Únode_elementr   r   r   r   rz   ]  sD    



zGEXFWriter.add_nodesc              	      sæ  ‡ fdd„}t dƒ}||ƒD ]¸\}}}}dt|ƒi}	z| d¡}
t|
ƒ|	d< W n tk
rd   Y nX z| d¡}t|ƒ|	d< W n tk
r”   Y nX z| d¡}t|ƒ|	d< W n tk
rÄ   Y nX z$| d¡}t|ƒ|	d< ˆ  |¡ W n tk
rþ   Y nX z$| d	¡}t|ƒ|	d	< ˆ  |¡ W n tk
r:   Y nX t|j|  d|¡ƒ}t|j|  d|¡ƒ}t d||dœ|	—Ž}|j di ¡}ˆ jdkr¤ˆ  	||¡}nˆ  
||¡}ˆ  ||¡}ˆ  d
|||¡}| |¡ q| |¡ d S )Nc                 3   s
  |   ¡ r| jdddD ]v\}}}}| ¡ }|j|d | dd ¡}|d kr~tˆ jƒ}t|ƒˆ jkrntˆ jƒ}qTˆ j 	t|ƒ¡ ||||fV  qnv| jddD ]h\}}}| ¡ }| dd ¡}|d krötˆ jƒ}t|ƒˆ jkrætˆ jƒ}qÌˆ j 	t|ƒ¡ ||||fV  qœd S )NT)ro   Úkeys)Úkeyrp   rn   )
Zis_multigraphrv   r†   Úupdater‡   Únextrd   r   rf   rw   )r   r|   r}   r•   ro   Ú	edge_datard   ©r>   r   r   Úedge_key_data…  s(    

z+GEXFWriter.add_edges.<locals>.edge_key_datarv   rp   r   ÚweightÚtyperƒ   r„   rX   )ÚsourceÚtargetÚedge_defaultr%   )rX   )r   r   r‡   rˆ   r‰   r€   rJ   rh   r*   r‹   rŒ   r   rŽ   ra   )r>   r   ry   rš   Úedges_elementr|   r}   r•   r˜   r’   Ú
edge_labelZedge_weightZ	edge_typerƒ   r„   Z	source_idZ	target_idÚedge_elementr   r   r™   r   r{   „  sT    




zGEXFWriter.add_edgesc                 C   sd  t dƒ}t|ƒdkr|S d}| ¡ D ].\}}|dkr:d}t|ƒ}	|	| jkrZtd|	› ƒ‚t|tƒrŽ|D ]>\}
}}t|
ƒ}	|d k	sŒ|d k	rjd}|  |¡ |  |¡  qªqj|  	t
|ƒ| j|	 |||¡}|D ]À\}
}}t dƒ}||jd	< t
|
ƒ|jd
< |	tkrP|jd
 dkrd|jd
< n6|jd
 dkr6d|jd
< n|jd
 dkrPd|jd
< |d k	rht
|ƒ|jd< |d k	r€t
|ƒ|jd< | |¡ qÊq$d}|  	t
|ƒ| j|	 |||¡}t dƒ}||jd	< t|tƒràt
|ƒ ¡ |jd
< njt
|ƒ|jd
< |	tkrJ|jd
 dkrd|jd
< n6|jd
 dkr0d|jd
< n|jd
 dkrJd|jd
< | |¡ q$| |¡ |S )NÚ	attvaluesr   rZ   r•   Únetworkx_keyz%attribute value type is not allowed: rY   ZattvalueÚforÚvalueÚinfZINFÚnanÚNaNz-infz-INFrƒ   r„   )r   ÚlenÚitemsrœ   r<   Ú	TypeErrorÚ
isinstancer8   r‰   Úget_attr_idr   Úattribr,   ra   r7   Úlower)r>   Znode_or_edgeZxml_objro   r   r£   r   Úkr}   Zval_typeÚvalrƒ   r„   re   Úer   r   r   rŽ   È  s€    


    ÿ




    ÿ



zGEXFWriter.add_attributesc                 C   s  z| j | | | W S  tk
r   tt| jƒƒ}|| j | | |< |||dœ}td
|Ž}| |¡}	|	d k	rŠtdƒ}
t|	ƒ|
_| |
¡ d }| j	 
d¡D ].}| d¡}| dd¡}||krš||krš|}qš|d krô||dœ}td|Ž}| j	 d	|¡ | |¡ Y nX |S )N)rp   Útitlerœ   Ú	attributer   Ú
attributesÚclassr   rZ   )r   r·   r   )rµ   )r¶   )rg   rˆ   r   r—   re   r   rJ   r]   ra   ry   ÚfindallÚinsert)r>   r´   Z	attr_typeZedge_or_noder   r   Znew_idZattr_kwargsrµ   Zdefault_titleZdefault_elementÚattributes_elementr5   Za_classZa_moder   r   r   r®   
  s0    






zGEXFWriter.get_attr_idc           
   
   C   sâ  |  dd¡}|rÞ| d¡}|d k	r¸| jdkrhtd| j› dt| d¡ƒt| d¡ƒt| d	¡ƒd
}nFtd| j› dt| d¡ƒt| d¡ƒt| d	¡ƒt| dd¡ƒd}| |¡ | d¡}|d k	rîtd| j› dt|ƒd}| |¡ | d¡}|d k	r&td| j› dt|ƒd}| |¡ | d¡}|d k	rˆ| d¡rdtd| j› ddt|ƒd}ntd| j› dt|ƒd}| |¡ | d¡}	|	d k	rÞtd| j› dt|	 d¡ƒt|	 d¡ƒt|	 d¡ƒd}| |¡ |S )NrV   FÚcolorr%   Ú{ú}colorÚrÚgÚb©r¾   r¿   rÀ   r5   g      ð?©r¾   r¿   rÀ   r5   Úsizeú}size)r¦   Ú	thicknessú
}thicknessÚshapeÚhttpú}shapeÚimage)r¦   ÚuriÚpositionú	}positionÚxÚyÚz©rÎ   rÏ   rÐ   )r‡   rJ   r*   r   r'   r   ra   Ú
startswith)
r>   Úelementr   rV   r»   r³   rÃ   rÅ   rÇ   rÌ   r   r   r   r   *  s^    

üû







  ÿ


ü
zGEXFWriter.add_vizc                 C   sP   |  dd¡}|rLtdƒ}|D ]$}tdƒ}t|ƒ|jd< | |¡ q| |¡ |S )NÚparentsFÚparentr¥   )r‡   r   r   r¯   ra   )r>   r“   r   rÔ   Úparents_elementÚpr³   r   r   r   rŠ   _  s    
zGEXFWriter.add_parentsc                 C   sT   |  dd¡}|rPtdƒ}|D ](\}}tdt|ƒt|ƒd}| |¡ q| |¡ |S )NÚslicesFÚslice)rƒ   r„   )r‡   r   r   ra   )r>   Únode_or_edge_elementÚnode_or_edge_datarØ   Úslices_elementrƒ   r„   r³   r   r   r   r‹   j  s    
zGEXFWriter.add_slicesc                 C   s†   |  dd¡}|r‚tdƒ}|D ]Z\}}tdƒ}|d k	rLt|ƒ|jd< |  |¡ |d k	rlt|ƒ|jd< |  |¡ | |¡ q| |¡ |S )NÚspellsFZspellrƒ   r„   )r‡   r   r   r¯   r‰   ra   )r>   rÚ   rÛ   rÝ   Úspells_elementrƒ   r„   r³   r   r   r   rŒ   t  s    


zGEXFWriter.add_spellsc                 C   sr   | j  d¡dkrn|d k	rnt|tƒr(d}n*t|tƒr8d}nt|tƒrHd}n
t d¡‚| j  d|¡ | j  dd¡ d S )	Nr   rZ   Údater-   r0   z2timeformat should be of the type int, float or strÚ
timeformatrY   )	ry   rJ   r­   r   r,   r1   rK   rL   r^   )r>   Z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   rj   r[   r   r   r   )r>   ÚfhÚdocumentr   r   r   r   –  s    
zGEXFWriter.writer   c                 C   s”   dd|  }t |ƒrv|jr$|j ¡ s.|d |_|jr>|j ¡ sD||_|D ]}|  ||d ¡ qH|jrn|j ¡ s||_n|r|jrŠ|j ¡ s||_d S )NÚ
z  r   )rª   r]   ÚstripÚtailrj   )r>   ÚelemÚlevelÚir   r   r   rj     s    
zGEXFWriter.indent)Nr   Tr   )r   )rO   rP   rQ   ri   rm   r   rz   r{   rŽ   r®   r   rŠ   r‹   rŒ   r‰   r   rj   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)rA   r    Úsimple_graphrN   )r>   r    r   r   r   r   ri   ±  s    zGEXFReader.__init__c                 C   s„   t |d| _| j d| j› d¡}|d k	r4|  |¡S | jD ]:}|  |¡ | j d| j› d¡}|d k	r:|  |¡  S q:t d¡‚d S )N)Úfiler¼   z}graphz No <graph> element in GEXF file.)	r   r[   Úfindr&   Ú
make_graphrI   rN   rK   rL   )r>   Ústreamr¿   r   r   r   r   Ú__call__¸  s    


zGEXFReader.__call__c                 C   sZ  |  dd ¡}|dkrt ¡ }nt ¡ }|  dd¡}|dkrD||jd< |  d¡}|d k	r`||jd< |  d¡}|d k	r|||jd< |  dd¡}|dkrœd|jd< n
d	|jd< |  d
¡| _| jdkrÂd| _| d| j› d¡}i }	i }
i }i }|D ]€}|  d¡}|dkr0|  |¡\}}|	 	|¡ |
 	|¡ |
|jd< qê|dkrh|  |¡\}}| 	|¡ | 	|¡ ||jd< qê‚ qêddd	ddœi}i }| 	|¡ | 	|¡ ||jd< | 
d| j› d¡}|d k	ræ| d| j› d¡D ]}|  |||	¡ qÐ| 
d| j› d¡}|d k	r.| d| j› d¡D ]}|  |||¡ q| jrV| ¡ rLt |¡}n
t |¡}|S )Nru   rq   rs   rt   rƒ   r„   r   rY   rZ   rà   rß   r/   r¼   z}attributesr·   rW   r…   rX   rŸ   r›   r-   )rœ   r   r´   ú}nodesú}nodez}edgesz}edge)rJ   rK   ZMultiDiGraphZ
MultiGraphrh   rà   r¸   r&   Úfind_gexf_attributesr–   rì   Úadd_nodeÚadd_edgerê   rx   ZDiGraphZGraph)r>   Z	graph_xmlZedgedefaultr   Z
graph_nameZgraph_startZ	graph_endZ
graph_modeZattributes_elementsÚ	node_attrr…   Ú	edge_attrrŸ   r5   Z
attr_classÚnaZndZeaZedr   Únode_xmlr    Zedge_xmlr   r   r   rí   Å  sp    





















zGEXFReader.make_graphc           	      C   sú   |   ||¡}|  ||¡}| jdkr0|  ||¡}n|  ||¡}|  ||¡}|  ||¡}| d¡}| jd k	rr|  |¡}| d¡}||d< | d|¡}|d k	r ||d< | 	d| j
› d¡}|d k	rè| d| j
› d¡D ]}| j||||d qÐ|j|f|Ž d S )	Nr%   rp   r   r‚   r¼   rð   rñ   )Únode_pid)Údecode_attr_elementsrŠ   r*   r‹   rŒ   r   Úadd_start_endrJ   r    rì   r&   r¸   ró   )	r>   r   rø   rõ   rù   ro   r‘   Z
node_labelZsubnodesr   r   r   ró     s(    




zGEXFReader.add_nodec                 C   sR   | j }| d¡}|d k	r*| j| |ƒ|d< | d¡}|d k	rN| j| |ƒ|d< |S )Nrƒ   r„   )rà   rJ   r=   )r>   ro   r[   ÚttypeZ
node_startZnode_endr   r   r   rû   7  s    

zGEXFReader.add_start_endc           	      C   s¦  i }|  d| j› d¡}|d k	r–| jdkrZt| d¡ƒt| d¡ƒt| d¡ƒdœ|d< n<t| d¡ƒt| d¡ƒt| d¡ƒt| d	d
¡ƒdœ|d< |  d| j› d¡}|d k	rÄt| d¡ƒ|d< |  d| j› d¡}|d k	ròt| d¡ƒ|d< |  d| j› d¡}|d k	r:| d¡|d< |d dkr:| d¡|d< |  d| j› d¡}|d k	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»   r5   r   rÂ   rÄ   r¦   rÃ   rÆ   rÅ   rÉ   rÇ   rÊ   rË   rÍ   rÎ   r   rÏ   rÐ   rÑ   rÌ   rV   )rì   r'   r*   r1   rJ   r,   rª   )	r>   ro   rø   rV   r»   rÃ   rÅ   rÇ   rÌ   r   r   r   r   B  sB    
ýü


ý
zGEXFReader.add_vizc                 C   sZ   |  d| j› d¡}|d k	rVg |d< | d| j› d¡D ]}| d¡}|d  |¡ q8|S )Nr¼   z}parentsrÔ   z}parentr¥   ©rì   r&   r¸   rJ   ra   )r>   ro   rø   rÖ   r×   rÕ   r   r   r   rŠ   o  s    
zGEXFReader.add_parentsc                 C   sh   |  d| j› d¡}|d k	rdg |d< | d| j› d¡D ]*}| d¡}| d¡}|d  ||f¡ q8|S )Nr¼   z}slicesrØ   z}slicerƒ   r„   rý   )r>   ro   Únode_or_edge_xmlrÜ   rl   rƒ   r„   r   r   r   r‹   x  s    

zGEXFReader.add_slicesc                 C   s‚   |  d| j› d¡}|d k	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¸   r=   rJ   ra   )r>   ro   rþ   rÞ   rü   rl   rƒ   r„   r   r   r   rŒ   ‚  s    zGEXFReader.add_spellsc                 C   sj  |  d¡}| ¡ r$|dkr$t d¡‚| ¡ s>|dkr>t d¡‚|  d¡}|  d¡}| jd k	rp|  |¡}|  |¡}|  ||¡}|  ||¡}| jdkr |  ||¡}n|  	||¡}|  d	¡}|d k	rÆ||d	< | 
d
d ¡}	|	d k	rÞ|	}|  d¡}
|
d k	rüt|
ƒ|d< |  d¡}|d k	r||d< | ||¡r,d| _|j||fd|i|—Ž |dkrf|j||fd|i|—Ž d S )Nrœ   rr   z(Undirected edge found in directed graph.rq   z(Directed edge found in undirected graph.r   rž   r%   rp   r¤   r›   r   Fr•   Zmutual)rJ   rx   rK   rL   r    rú   rû   r*   r‹   rŒ   r‡   r,   Zhas_edgerê   rô   )r>   r   r¢   rö   Zedge_directionr   rž   ro   rd   Zmultigraph_keyr›   r¡   r   r   r   rô     s@    













zGEXFReader.add_edgec                 C   sF  i }|  d| j› d¡}|d k	rB| d| j› d¡D ]}| d¡}z|| d }W n6 tk
rŒ } zt d|› d¡|‚W 5 d }~X Y nX || d }	| d	¡}
|	d
kr¸| j|
 }
n| j|	 |
ƒ}
|| d dkr8| j	}| j| | d¡ƒ}| j| | d¡ƒ}||kr&||  
|
||f¡ n|
||fg||< q6|
||< q6|S )Nr¼   z
}attvaluesz	}attvaluer¥   r´   zNo attribute defined for=rH   rœ   r¦   r.   r   rY   rƒ   r„   )rì   r&   r¸   rJ   rˆ   rK   rL   rS   r=   rà   ra   )r>   Z	gexf_keysZobj_xmlrg   Zattr_elementr5   r•   r´   ÚerrÚatyper¦   rü   rƒ   r„   r   r   r   rú   Á  s.    

&


zGEXFReader.decode_attr_elementsc                 C   s¬   i }i }|  d¡}| d| j› d¡D ]|}|  d¡}|  d¡}|  d¡}|||dœ||< | d| j› d¡}	|	d k	r&|d	krŠ| j|	j }
n| j| |	jƒ}
|
||< q&||fS )
Nr   r¼   z
}attributerp   r´   rœ   )r´   rœ   r   z}defaultr.   )rJ   r¸   r&   rì   rS   r]   r=   )r>   rº   ÚattrsÚdefaultsr   r±   re   r´   r   r   r¦   r   r   r   rò   ã  s    




zGEXFReader.find_gexf_attributes)Nr   )N)rO   rP   rQ   ri   rï   rí   ró   rû   r   rŠ   r‹   rŒ   rô   rú   rò   r   r   r   r   r!   ®  s   
O
#-	
4"r!   c              
      s  z‡ fdd„ˆ D ƒ‰W n. t k
rD } zt d¡|‚W 5 d}~X Y nX tˆŽ \}}tt|ƒƒtˆ ƒkrpt d¡‚tˆƒ‰t ˆ ˆ¡}ˆ D ]ˆ}ˆ| }||j| d< |j|  	d¡ d|j| krÜˆˆ j| d  |j| d< d	|j| k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€   )r4   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.rp   r   r‚   rÔ   c                    s   g | ]}ˆ | ‘qS r   r   )r4   r×   )Úmappingr   r   r  (  s     )
rˆ   rK   rL   Úziprª   r^   r9   Zrelabel_nodesr€   r‡   )r   rÿ   rÎ   rÏ   ÚHÚnÚmr   )r   r  r   r
   ø  s0    ÿþÿ()r   Tr   )r   Tr   )NFr   )Ú__doc__rb   r_   Úxml.etree.ElementTreer   r   r   r   r   ZnetworkxrK   Znetworkx.utilsr   Ú__all__r   r   r	   r#   r   r!   r
   r   r   r   r   Ú<module>   s(   
6
-
+_     L