U
    Ñtœd%&  ã                   @   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|
Ž q„t	ˆ 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|ƒr¢dtt|j ¡ ƒƒkr¢dd„ |j 	¡ D ƒ‰ˆ 
¡  dd„ tˆƒD ƒ}i }	|j 	¡ D ]\}
}||d  |	|
< q†ndd„ t|ƒD ƒ}	| || ¡ ||d	�}|j 	¡ D ]6\}
}| ¡ D ]$\}}|dkrôqâ||j|	|
  |< qâqÒd
d„ |j 	¡ D ƒ}dd„ |D ƒ}g ‰ t||j|jfƒ�r°g |d< |jj	dd�D ]R\}}
}}ˆ  |	| |	|
 f¡ |D ]}||  | |¡¡ �q€|d  |¡ �qZnN|j 	¡ D ]B\}}
}ˆ  |	| |	|
 f¡ |D ]}||  | |¡¡ �qÞ�qºd|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
| | |
¡< qžqz|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 ]\}}||  || ¡ q°q„| |	|¡ |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