3
d                 @   s   d Z ddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZ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 )a  Read and write graphs in GEXF format.

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 https://gephi.org/gexf/format/schema.html for the
specification and https://gephi.org/gexf/format/basic.html for examples.
    N)	open_file)ElementElementTree
SubElementtostringregister_namespace
write_gexf	read_gexfrelabel_gexf_graphgenerate_gexf   wb)modeutf-8T1.2draftc             C   s&   t |||d}|j|  |j| dS )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.

    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.

    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, https://gephi.org/gexf/format/
    .. [2] GEXF viz schema 1.1, https://gephi.org/gexf/1.1draft/viz
    )encodingprettyprintversionN)
GEXFWriter	add_graphwrite)Gpathr   r   r   writer r   ;/tmp/pip-build-7vycvbft/networkx/networkx/readwrite/gexf.pyr      s    0
c             c   s.   t |||d}|j|  t|j 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 https://gephi.org/gexf/format/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))  # doctest: +SKIP
    >>> 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   r   r   N)r   r   str
splitlines)r   r   r   r   r   r   r   r   r   T   s    (
rbFc             C   s*   t ||d}|rt|| }n|| }|S )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
       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 https://gephi.org/gexf/format/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, https://gephi.org/gexf/format/
    )	node_typer   )
GEXFReaderr
   )r   r   Zrelabelr   readerr   r   r   r   r	      s
    $c               @   sv  e Zd Zi ZddddjddgddZeed< d	d
ddjd	dgddZeed< edfedfedfedfe	dfe
dfedfedfedfedfg
ZyddlZW n ek
r   Y nX ejdfejdfejdfejdfejdfejdfejdfejdfejdfejdfejdfejdfejdfejdfejdfejdfge Ze
eZe
dd e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.xsdz1.1)NS_GEXFNS_VIZNS_XSISCHEMALOCATIONVERSIONz1.1draftzhttp://www.gexf.net/1.2draftz http://www.gexf.net/1.2draft/vizz%http://www.gexf.net/1.2draft/gexf.xsdz1.2z1.2draftintegerfloatdoublebooleanstringlongZ
liststringZanyURIr   Nintc             c   s   | ]}t |V  qd S )N)reversed).0ar   r   r   	<genexpr>   s    zGEXF.<genexpr>TF)truefalseTrueFalse0r   1r   c             C   sb   | j j|}|d kr&tj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   )selfr   dr   r   r   set_version   s    




zGEXF.set_version)#__name__
__module____qualname__r;   joinr@   r/   r*   boollistdictr   typesnumpynpImportErrorZfloat64Zfloat32Zfloat16Zfloat_int_Zint8Zint16Zint32Zint64Zuint8Zuint16Zuint32Zuint64ZintcZintpxml_typepython_typeconvert_boolrA   r   r   r   r   r"      sr   

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   Nutf-8T1.2draftc             C   s  || _ || _| j| td| j| j| j| jd| _td}dt	j
 }|t|d_|jdtjd | jj| td| j tj | _tj | _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| j| d S )NZgexf)xmlnsz	xmlns:xsizxsi:schemaLocationr   metaz	NetworkX ZcreatorZlastmodifieddatez%Y-%m-%dviznodeedgedynamicstatic)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   )r?   graphr   r   r   Zmeta_elementZsubelement_textr   r   r   __init__	  s6    




zGEXFWriter.__init__c             C   s(   | j r| j| j t| jj| j}|S )N)r   indentrZ   r   decoder   )r?   sr   r   r   __str__3  s    zGEXFWriter.__str__c       
      C   s   x<|j ddD ],\}}}|jd}|d k	r| jjt| qW |jjddkrTd}nd}|j rfd}nd}|jjd	d
}td|||d}	|	| _| j	||	 | j
||	 | jj|	 d S )NT)dataidr   rX   rY   directed
undirectedname rg   )defaultedgetyper   rq   )edgesr<   re   addr   rg   is_directedr   graph_element	add_nodes	add_edgesrZ   r`   )
r?   r   uvddZeidr   defaultrq   rw   r   r   r   r   9  s     
zGEXFWriter.add_graphc             C   s  t d}xf|jddD ]T\}}|j }t|jd|}d|i}t|jd|}	|	|d< y|jd}
t|
|d< W n tk
r   Y nX y$|jd}t||d< | j| W n tk
r   Y nX y$|jd}t||d< | j| W n tk
r   Y nX t d|}|jjd
i }| j	||}| j
dkr<| j||}n| j||}| j||}| jd	|||}|j| qW |j| d S )NnodesT)rm   rn   labelpidstartendrV   node_defaultz1.1)rV   )r   r~   copyr   popKeyErroralter_graph_mode_timeformatrg   r<   add_parentsr(   
add_slices
add_spellsadd_vizadd_attributesr`   )r?   r   rw   nodes_elementrV   rm   	node_datanode_idkwr   r   r   r   node_elementr}   r   r   r   rx   P  sD    



zGEXFWriter.add_nodesc          .      s   fdd}t d}x||D ]\}}}}dt|i}	y|jd}
t|
|	d< W n tk
rh   Y nX y|jd}t||	d< W n tk
r   Y nX y|jd}t||	d< W n tk
r   Y nX y$|jd}t||	d<  j| W n tk
r   Y nX y$|jd	}t||	d	<  j| W n tk
r@   Y nX t|j| jd|}t|j| jd|}t d||d|	}|jjdi } jdkr j	||}n j
||} j||} jd
|||}|j| q W |j| d S )Nc             3   s  | j  rx| jdddD ]z\}}}}|j }|j|d |jdd }|d krt j}xt| jkrrt j}qXW  jj	t| ||||fV  qW nx~| jddD ]n\}}}|j }|jdd }|d krt j}xt| jkrt j}qW  jj	t| ||||fV  qW d S )NT)rm   keys)keyrn   )rm   )
Zis_multigraphrt   r   updater   nextrc   r   re   ru   )r   rz   r{   r   rm   	edge_datarc   )r?   r   r   edge_key_datax  s(    


z+GEXFWriter.add_edges.<locals>.edge_key_datart   rn   r   weighttyper   r   rW   )sourcetargetedge_defaultz1.1)rW   )r   r   r   r   r   r~   r<   rg   r(   r   r   r   r   r`   )r?   r   rw   r   edges_elementrz   r{   r   r   r   
edge_labelZedge_weightZ	edge_typer   r   Z	source_idZ	target_idedge_elementr}   r   )r?   r   ry   w  sT    




zGEXFWriter.add_edgesc             C   sp  t d}t|dkr|S d}xB|j D ]4\}}|dkr>d}t|}	|	| jkr^td|	 t|trxD|D ]<\}
}}t|
}	|d k	s|d k	rpd}| j| | j| P qpW | j	t
|| j|	 |||}x|D ]\}
}}t d}||jd	< t
|
|jd
< |	tkrX|jd
 dkr"d|jd
< n6|jd
 dkr>d|jd
< n|jd
 dkrXd|jd
< |d k	rpt
||jd< |d k	rt
||jd< |j| qW q(d}| j	t
|| j|	 |||}t d}||jd	< t|trt
|j |jd
< njt
||jd
< |	tkrT|jd
 dkrd|jd
< n6|jd
 dkr:d|jd
< n|jd
 dkrTd|jd
< |j| q(W |j| |S )N	attvaluesr   rY   r   networkx_keyz%attribute value type is not allowed: rX   ZattvalueforvalueinfZINFnanNaNz-infz-INFr   r   )r   lenitemsr   rN   	TypeError
isinstancerG   r   get_attr_idr   attribr*   r`   rF   lower)r?   Znode_or_edgeZxml_objrm   r}   r   r   kr{   Zval_typevalr   r   rd   er   r   r   r     sl    











zGEXFWriter.add_attributesc             C   s
  y| j | | | S  tk
r   tt| j}|| j | | |< |||d}td
|}|j|}	|	d k	rtd}
t|	|
_|j|
 d }x>| j	j
dD ].}|jd}|jdd}||kr||kr|}qW |d kr||d}td|}| j	jd	| |j| Y nX |S )N)rn   titler   	attributer}   
attributesclassr   rY   )r   r   r   )r   )r   )rf   r   r   r   rd   r   r<   r\   r`   rw   findallinsert)r?   r   Z	attr_typeZedge_or_noder}   r   Znew_idZattr_kwargsr   Zdefault_titleZdefault_elementattributes_elementr2   Za_classZa_moder   r   r   r     s0    






zGEXFWriter.get_attr_idc       
      C   s  |j dd}|r|jd}|d k	r| jdkrhtd| j dt|jdt|jdt|jd	d
}nDtd| j dt|jdt|jdt|jd	t|jdd}|j| |jd}|d k	rtd| j dt|d}|j| |jd}|d k	r$td| j dt|d}|j| |jd}|d k	r|jdrbtd| j ddt|d}ntd| j dt|d}|j| |jd}	|	d k	rtd| j dt|	jdt|	jdt|	jdd}|j| |S )NrU   Fcolorz1.1{z}colorrgb)r   r   r   r2   )r   r   r   r2   sizez}size)r   	thicknessz
}thicknessshapehttpz}shapeimage)r   uripositionz	}positionxyz)r   r   r   )r   r<   r(   r   r%   r   r`   
startswith)
r?   elementr   rU   r   r   r   r   r   r   r   r   r   r     sR    













zGEXFWriter.add_vizc             C   sT   |j dd}|rPtd}x,|D ]$}td}t||jd< |j| qW |j| |S )NparentsFparentr   )r   r   r   r   r`   )r?   r   r   r   parents_elementpr   r   r   r   r   R  s    

zGEXFWriter.add_parentsc             C   sX   |j dd}|rTtd}x0|D ](\}}tdt|t|d}|j| qW |j| |S )NslicesFslice)r   r   )r   r   r   r`   )r?   node_or_edge_elementnode_or_edge_datar   slices_elementr   r   r   r   r   r   r   ]  s    
zGEXFWriter.add_slicesc             C   s   |j dd}|rtd}xb|D ]Z\}}td}|d k	rNt||jd< | j| |d k	rnt||jd< | j| |j| qW |j| |S )NspellsFZspellr   r   )r   r   r   r   r   r`   )r?   r   r   r   spells_elementr   r   r   r   r   r   r   g  s    


zGEXFWriter.add_spellsc             C   sr   | j jddkrn|d k	rnt|tr(d}n*t|tr8d}nt|trHd}n
tjd| j jd| | j jdd d S )	Nr   rY   dater+   r.   z2timeformat should be of the type int, float or str
timeformatrX   )	rw   r<   r   r   r*   r/   r=   r>   r]   )r?   Zstart_or_endr   r   r   r   r   w  s    


z&GEXFWriter.alter_graph_mode_timeformatc             C   s2   | j r| j| j t| j}|j|| jdd d S )NT)r   xml_declaration)r   ri   rZ   r   r   r   )r?   fhdocumentr   r   r   r     s    
zGEXFWriter.writer   c             C   s   dd|  }t |r|j s(|jj  r2|d |_|j sF|jj  rL||_x|D ]}| j||d  qRW |j s~|jj  r||_n|r|j s|jj  r||_d S )N
z  r   )r   r\   striptailri   )r?   elemlevelir   r   r   ri     s    

zGEXFWriter.indent)NrQ   TrR   )r   )rB   rC   rD   rh   rl   r   rx   ry   r   r   r   r   r   r   r   r   ri   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    N1.2draftc             C   s   || _ d| _| j| d S )NT)r   simple_graphrA   )r?   r   r   r   r   r   rh     s    zGEXFReader.__init__c             C   s   t |d| _| jjd| j d}|d k	r4| j|S x@| jD ]6}| j| | jjd| j d}|d k	r<| j|S q<W tjdd S )N)filer   z}graphz No <graph> element in GEXF file.)	r   rZ   findr$   
make_graphr;   rA   r=   r>   )r?   streamr   r   r   r   r   __call__  s    

zGEXFReader.__call__c             C   sf  |j dd }|dkrtj }ntj }|j dd}|dkrD||jd< |j d}|d k	r`||jd< |j d}|d k	r|||jd< |j dd}|dkrd|jd< n
d	|jd< |j d
| _| jdkrd| _|jd| j d}i }	i }
i }i }x|D ]}|j d}|dkr2| j|\}}|	j	| |
j	| |
|jd< q|dkrj| j|\}}|j	| |j	| ||jd< q qW ddd	ddi}i }|j	| |j	| ||jd< |j
d| j d}|d k	rx,|jd| j dD ]}| j|||	 qW |j
d| j d}|d k	r:x,|jd| j dD ]}| j||| q"W | jrb|j rXtj|}n
tj|}|S )Nrs   ro   rq   rr   r   r   r   rX   rY   r   r   r-   r   z}attributesr   rV   r   rW   r   r   r+   )r   r   r   z}nodesz}nodez}edgesz}edge)r<   r=   ZMultiDiGraphZ
MultiGraphrg   r   r   r$   find_gexf_attributesr   r   add_nodeadd_edger   rv   ZDiGraphZGraph)r?   Z	graph_xmlZedgedefaultr   Z
graph_nameZgraph_startZ	graph_endZ
graph_modeZattributes_elements	node_attrr   	edge_attrr   r2   Z
attr_classnaZndZeaZedr   node_xmlr   Zedge_xmlr   r   r   r     sp    






















zGEXFReader.make_graphc       	      C   s   | j ||}| j||}| jdkr0| j||}n| j||}| j||}| j||}|jd}| jd k	rr| j|}|jd}||d< |jd|}|d k	r||d< |j	d| j
 d}|d k	rx.|jd| j
 dD ]}| j||||d qW |j|f| d S )	Nz1.1rn   r   r   r   z}nodesz}node)node_pid)decode_attr_elementsr   r(   r   r   r   add_start_endr<   r   r   r$   r   r   )	r?   r   r   r   r   rm   r   Z
node_labelZsubnodesr   r   r   r     s(    




zGEXFReader.add_nodec             C   sR   | j }|jd}|d k	r*| j| ||d< |jd}|d k	rN| j| ||d< |S )Nr   r   )r   r<   rO   )r?   rm   rZ   ttypeZ
node_startZnode_endr   r   r   r   )  s    

zGEXFReader.add_start_endc       	      C   s  i }|j d| j d}|d k	r| jdkrZt|jdt|jdt|jdd|d< n<t|jdt|jdt|jdt|jd	d
d|d< |j d| j d}|d k	rt|jd|d< |j d| j d}|d k	rt|jd|d< |j d| j d}|d k	r:|jd|d< |d dkr:|jd|d< |j d| j d}|d k	rt|jddt|jddt|jddd|d< t|dkr||d< |S )Nr   z}colorz1.1r   r   r   )r   r   r   r   r2   r   )r   r   r   r2   z}sizer   r   z
}thicknessr   z}shaper   r   r   z	}positionr   r   r   r   )r   r   r   r   rU   )r   r%   r(   r/   r<   r*   r   )	r?   rm   r   rU   r   r   r   r   r   r   r   r   r   4  s<    


zGEXFReader.add_vizc             C   s^   |j d| j d}|d k	rZg |d< x4|jd| j dD ]}|jd}|d j| q:W |S )Nr   z}parentsr   z}parentr   )r   r$   r   r<   r`   )r?   rm   r   r   r   r   r   r   r   r   a  s    
zGEXFReader.add_parentsc             C   sl   |j d| j d}|d k	rhg |d< xB|jd| j dD ]*}|jd}|jd}|d j||f q:W |S )Nr   z}slicesr   z}slicer   r   )r   r$   r   r<   r`   )r?   rm   node_or_edge_xmlr   rk   r   r   r   r   r   r   j  s    

zGEXFReader.add_slicesc             C   s   |j d| j d}|d k	rg |d< | j}xV|jd| j dD ]>}| j| |jd}| j| |jd}|d j||f q@W |S )Nr   z}spellsr   z}spellr   r   )r   r$   r   r   rO   r<   r`   )r?   rm   r   r   r   rk   r   r   r   r   r   r   t  s    zGEXFReader.add_spellsc             C   sl  |j d}|j r$|dkr$tjd|j  r@|dkr@tjd|j d}|j d}| jd k	rr| j|}| j|}| j||}| j||}| jdkr| j||}n| j	||}|j d	}|d k	r||d	< |j
d
d }	|	d k	r|	}|j d}
|
d k	rt|
|d< |j d}|d k	r||d< |j||r.d| _|j||fd|i| |dkrh|j||fd|i| d S )Nr   rp   z(Undirected edge found in directed graph.ro   z(Directed edge found in undirected graph.r   r   z1.1rn   r   r   r   Fr   Zmutual)r<   rv   r=   r>   r   r   r   r(   r   r   r   r*   Zhas_edger   r   )r?   r   r   r   Zedge_directionr   r   rm   rc   Zmultigraph_keyr   r   r   r   r   r     s@    













zGEXFReader.add_edgec             C   sL  i }|j d| j d}|d k	rHx"|jd| j dD ]}|jd}y|| d }W n6 tk
r } ztjd| d|W Y d d }~X nX || d }	|jd	}
|	d
kr| j|
 }
n| j|	 |
}
|| d dkr<| j	}| j| |jd}| j| |jd}||kr*|| j
|
||f n|
||fg||< q:|
||< q:W |S )Nr   z
}attvaluesz	}attvaluer   r   zNo attribute defined for=r:   r   r   r,   r   rX   r   r   )r   r$   r   r<   r   r=   r>   rP   rO   r   r`   )r?   Z	gexf_keysZobj_xmlrf   Zattr_elementr2   r   r   r   atyper   r   r   r   r   r   r   r     s.    

&

zGEXFReader.decode_attr_elementsc             C   s   i }i }|j d}x|jd| j dD ]|}|j d}|j d}|j d}|||d||< |jd| j d}	|	d k	r(|d	kr| j|	j }
n| j| |	j}
|
||< q(W ||fS )
Nr   r   z
}attributern   r   r   )r   r   r   z}defaultr,   )r<   r   r$   r   rP   r\   rO   )r?   r   attrsdefaultsr   r   rd   r   r   r}   r   r   r   r   r     s    



zGEXFReader.find_gexf_attributes)Nr   )N)rB   rC   rD   rh   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s   
O
#-	
4"r    c                s  y fdd D W n. t k
rD } ztjd|W Y dd}~X nX t \}}tt|t krptjdttj }x D ]}| }||j| d< |j| j	d d|j| krވ j| d  |j| d< d	|j| krfd
d j| d	 D |j| d	< qW |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 relabed 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~   )r1   rz   )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.rn   r   r   r   c                s   g | ]} | qS r   r   )r1   r   )mappingr   r   r     s    )
r   r=   r>   zipr   r]   rH   Zrelabel_nodesr~   r   )r   r   r   r   Hnmr   )r   r   r   r
     s*    
*)r   Tr   )r   Tr   )NFr   )__doc__ra   r^   Znetworkxr=   Znetworkx.utilsr   xml.etree.ElementTreer   r   r   r   r   __all__r   r   r	   r"   r   r    r
   r   r   r   r   <module>   s(   
4
-
+Y     K