U
    td%&                     @   s6   de dddZde dddZdddZd	d
 Zd S )N_nx_name)vertex_attr_hashablec                    sV  ddl }|dkrD  r.  r&|jn|j}qH  r<|jn|j}n|} fdd  D }|f |}t||j|jf}t	 j
D ]>\}}	|	 }
||
d< ||
kr|
|}n|}|j|f|
 qt	 jD ]\}}| }||d< |rd|kr|jd |d< |  kr4 j
| |j } j
| |j }n|j}|j}|j||f| q|S )a  Converts the graph to networkx format.

    igraph has ordered vertices and edges, but networkx does not. To keep
    track of the original order, the '_igraph_index' vertex property is
    added to both vertices and edges.

    @param create_using: specifies which NetworkX graph class to use when
        constructing the graph. C{None} means to let igraph infer the most
        appropriate class based on whether the graph is directed and whether
        it has multi-edges.
    @param vertex_attr_hashable: vertex attribute used to name vertices
        in the exported network. The default "_nx_name" ensures round trip
        conversions to/from networkx are lossless.
        Nc                    s   i | ]}| | qS  r   ).0xgraphr   L/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/igraph/io/libraries.py
<dictcomp>   s      z-_export_graph_to_networkx.<locals>.<dictcomp>_igraph_index_nx_multiedge_keykey)networkxZhas_multipleis_directedMultiDiGraph
MultiGraphZDiGraphGraph
attributes
isinstance	enumeratevspopadd_nodeesvertex_attributessourcetargetadd_edge)r   Zcreate_usingr   nxclskwgZ
multigraphivvattrZhashableedgeeattrZhashable_sourceZhashable_targetr   r   r	   _export_graph_to_networkx   s8    
r'   c                    sz  ddl }t|j}t|j}||i}t|}t|rdtt|j krdd |j	 D 
  dd tD }i }	|j	 D ]\}
}||d  |	|
< qndd t|D }	| || ||d	}|j	 D ]6\}
}| D ]$\}}|dkrq||j|	|
  |< qqd
d |j	 D }dd |D }g  t||j|jfrg |d< |jj	ddD ]R\}}
}} |	| |	|
 f |D ]}|| || q|d | qZnN|j	 D ]B\}}
} |	| |	|
 f |D ]}|| || qސqd|krjdd t|d D }|j
dd d dd |D |d=  fddD  fdd| D }| | |S )a  Converts the graph from networkx

    Vertex names will be stored as a vertex_attr_hashable attribute (usually
    "_nx_name", but see below). Because igraph stored vertices in an
    ordered manner, vertices will get new IDs from 0 up. In case of
    multigraphs, each edge will have an "_nx_multiedge_key" attribute, to
    distinguish edges that connect the same two vertices.

    @param g: networkx Graph or DiGraph
    @param vertex_attr_hashable: attribute used to store the Python
        hashable used by networkx to identify each vertex. The default value
        '_nx_name' ensures lossless round trip conversions to/from networkx. An
        alternative choice is 'name': in that case, using strings for vertex
        names is recommended and, if the graph is re-exported to networkx,
        Graph.to_networkx(vertex_attr_hashable="name") must be used to recover
        the correct vertex nomenclature in the exported network.

    r   Nr   c                 S   s   g | ]\}}|d  qS )r   r   )r   r#   r   r   r   r	   
<listcomp>g   s     z2_construct_graph_from_networkx.<locals>.<listcomp>c                 S   s   i | ]\}}||qS r   r   r   r"   r   r   r   r	   r
   i   s      z2_construct_graph_from_networkx.<locals>.<dictcomp>c                 S   s   i | ]\}}||qS r   r   )r   r"   r#   r   r   r	   r
   o   s      )ndirectedgraph_attrsZvertex_attrsc                 S   s    h | ]\}}}|D ]}|qqS r   r   )r   _datanamer   r   r	   	<setcomp>   s       z1_construct_graph_from_networkx.<locals>.<setcomp>c                 S   s   i | ]
}|g qS r   r   r   r/   r   r   r	   r
      s      r   T)keysc                 S   s   g | ]\}}||fqS r   r   r)   r   r   r	   r(      s     c                 S   s   | d S )N   r   )r   r   r   r	   <lambda>       z0_construct_graph_from_networkx.<locals>.<lambda>)r   c                 S   s   g | ]\}}|qS r   r   r)   r   r   r	   r(      s     c                    s   g | ]} | qS r   r   r   r"   )edgesr   r	   r(      s     c                    s$   i | ]\} | fd dD qS )c                    s   g | ]} | qS r   r   r6   valr   r	   r(      s     z=_construct_graph_from_networkx.<locals>.<dictcomp>.<listcomp>r   )r   r   )idxr8   r	   r
      s      )r   dictr   listZnodeslennextitervaluesr.   sortr   r   itemsr   r7   r   r   r   appendget	add_edges)r   r!   r   r   gattrZvnamesr$   vcountZidx_dictZvdr#   Zdatumr   r   r9   eattr_namesr&   uZedgekeyr.   r/   Zsortdr   )r7   r:   r	   _construct_graph_from_networkxG   s^    

   
rJ   c                 C   sL  ddl }|j|  d}|  }|| |dk	rj| D ].\}}|t|}	|	|j|< | | |j|< q:|dk	r| D ]H\}}|	t||j
|< t|D ]"}
| j|
 | |j
| ||
< qqz|dk	r| D ]\}}|t||j|< q| jD ]N}||j|j}|dk	r| D ](\}}| |d}||j| |< qq|S )a  Converts the graph to graph-tool

    Data types: graph-tool only accepts specific data types. See the
    following web page for a list:

    https://graph-tool.skewed.de/static/doc/quickstart.html

    Note: because of the restricted data types in graph-tool, vertex and
    edge attributes require to be type-consistent across all vertices or
    edges. If you set the property for only some vertices/edges, the other
    will be tagged as None in igraph, so they can only be converted
    to graph-tool with the type 'object' and any other conversion will
    fail.

    @param graph_attributes: dictionary of graph attributes to transfer.
      Keys are attributes from the graph, values are data types (see
      below). C{None} means no graph attributes are transferred.
    @param vertex_attributes: dictionary of vertex attributes to transfer.
      Keys are attributes from the vertices, values are data types (see
      below). C{None} means no vertex attributes are transferred.
    @param edge_attributes: dictionary of edge attributes to transfer.
      Keys are attributes from the edges, values are data types (see
      below). C{None} means no vertex attributes are transferred.
    r   N)r+   )Z
graph_toolr   r   rG   Z
add_vertexrB   Znew_graph_propertystrgraph_propertiesZnew_vertex_propertyvertex_propertiesranger   ZvertexZnew_edge_propertyedge_propertiesr   r   r   r   r   rD   )r   Zgraph_attributesr   Zedge_attributesgtr!   vcr   ZdtypeZgpropr"   r%   epropr   r   r	   _export_graph_to_graph_tool   s0    

"
rT   c                 C   s   t |j}| }| || |d}|j D ]0\}}| }t|D ]}|| |j| |< qFq.g }	t	|j
}
dd |
D }| D ]H}|	t| t| f |j
 D ]\}}|| ||  qq||	| |S )zGConverts the graph from graph-tool

    @param g: graph-tool Graph
    )r*   r+   r,   c                 S   s   i | ]
}|g qS r   r   r1   r   r   r	   r
     s      z4_construct_graph_from_graph_tool.<locals>.<dictcomp>)r;   rL   Znum_verticesr   rM   rB   Z	get_arrayrN   r   r<   rO   r7   rC   intr   r   rE   )r   r!   rF   rG   r   r   r9   rS   r"   r7   rH   r&   rR   r/   Zattr_mapr   r   r	    _construct_graph_from_graph_tool   s     

rV   )Nr   )r   )NNN)rK   r'   rJ   rT   rV   r   r   r   r	   <module>   s      F`     
C