3
dP4                 @   s   d Z ddlZddlZddlmZmZmZ dddddd	d
gZdddZ	dddZ
dddZdddZdddZddd
Zddd	ZdS )a  Functions to convert NetworkX graphs to and from other formats.

The preferred way of converting data to a NetworkX graph is through the
graph constructor.  The constructor calls the to_networkx_graph() function
which attempts to guess the input type and convert it automatically.

Examples
--------
Create a graph with a single edge from a dictionary of dictionaries

>>> d = {0: {1: 1}}  # dict-of-dicts single edge (0,1)
>>> G = nx.Graph(d)

See Also
--------
nx_agraph, nx_pydot
    N)
Collection	GeneratorIteratorto_networkx_graphfrom_dict_of_dictsto_dict_of_dictsfrom_dict_of_liststo_dict_of_listsfrom_edgelistto_edgelistFc          |   C   s  t | drybt| j|| j d}t | dr8|jj| j t | drjx&| jj D ]\}}|j| j| qNW |S  t	k
r } zt
jd|W Y dd}~X nX t | dryt
jj| |dS  t	k
r } zt
jd	|W Y dd}~X nX t| trJyt| ||dS    yt| |dS  t	k
rB } ztd
|W Y dd}~X nX Y nX yddl}t| |jr| jd | jd kryt
j| |dS  t	k
r } zd}t
j||W Y dd}~X nX nFyt
j| d|dS  t	k
r } zd}t
j||W Y dd}~X nX W n& tk
r,   d}tj|t Y nX y`ddl}	t| |	j|	jfryt
j| |dS  t	k
r } zt
jd|W Y dd}~X nX W n" tk
r   tjdt Y nX yXddl}
t | dryt
j | |dS  t	k
r } zt
jd|W Y dd}~X nX W n" tk
r,   tjdt Y nX t| t!t"t#fr~yt$| |dS  t	k
r| } zt
jd|W Y dd}~X nX t
jddS )a  Make a NetworkX graph from a known data structure.

    The preferred way to call this is automatically
    from the class constructor

    >>> d = {0: {1: {"weight": 1}}}  # dict-of-dicts single edge (0,1)
    >>> G = nx.Graph(d)

    instead of the equivalent

    >>> G = nx.from_dict_of_dicts(d)

    Parameters
    ----------
    data : object to be converted

        Current known types are:
         any NetworkX graph
         dict-of-dicts
         dict-of-lists
         container (e.g. set, list, tuple) of edges
         iterator (e.g. itertools.chain) that produces edges
         generator of edges
         Pandas DataFrame (row per edge)
         numpy matrix
         numpy ndarray
         scipy sparse matrix
         pygraphviz agraph

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
        Graph type to create. If graph instance, then cleared before populated.

    multigraph_input : bool (default False)
        If True and  data is a dict_of_dicts,
        try to create a multigraph assuming dict_of_dict_of_lists.
        If data and create_using are both multigraphs then create
        a multigraph from a multigraph.

    adj)create_usingmultigraph_inputgraphnodesz&Input is not a correct NetworkX graph.NZ	is_strict)r   z(Input is not a correct pygraphviz graph.zInput is not known type.r      z9Input is not a correct Pandas DataFrame adjacency matrix.T)Z	edge_attrr   z2Input is not a correct Pandas DataFrame edge-list.z+pandas not found, skipping conversion test.z-Input is not a correct numpy matrix or array.z*numpy not found, skipping conversion test.formatz0Input is not a correct scipy sparse matrix type.z*scipy not found, skipping conversion test.zInput is not a valid edge listz.Input is not a known data type for conversion.)%hasattrr   r   is_multigraphr   updater   items_node	ExceptionnxZNetworkXErrorZ	nx_agraphZfrom_agraph
isinstancedictr   	TypeErrorZpandasZ	DataFrameshapeZfrom_pandas_adjacencyZfrom_pandas_edgelistImportErrorwarningswarnImportWarningnumpyZmatrixZndarrayZfrom_numpy_matrixscipyZfrom_scipy_sparse_matrixr   r   r   r
   )datar   r   resultnddepdmsgr"   r#    r+   4/tmp/pip-build-7vycvbft/networkx/networkx/convert.pyr   !   s    )



" "c                s>    dkr|  i }x( D ] } fdd| j |D ||< qW |S )a&  Returns adjacency representation of graph as a dictionary of lists.

    Parameters
    ----------
    G : graph
       A NetworkX graph

    nodelist : list
       Use only nodes specified in nodelist

    Notes
    -----
    Completely ignores edge data for MultiGraph and MultiDiGraph.

    Nc                s   g | ]}| kr|qS r+   r+   ).0nbr)nodelistr+   r,   
<listcomp>   s    z$to_dict_of_lists.<locals>.<listcomp>)Z	neighbors)Gr/   dr&   r+   )r/   r,   r	      s    
 c             C   s   t jd|}|j|  |j rn|j  rni }xX| j D ]2\}}x |D ]}||krD|j|| qDW d||< q6W n|jdd | j D  |S )a  Returns a graph from a dictionary of lists.

    Parameters
    ----------
    d : dictionary of lists
      A dictionary of lists adjacency representation.

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
        Graph type to create. If graph instance, then cleared before populated.

    Examples
    --------
    >>> dol = {0: [1]}  # single edge (0,1)
    >>> G = nx.from_dict_of_lists(dol)

    or

    >>> G = nx.Graph(dol)  # use Graph constructor

    r   r   c             s   s$   | ]\}}|D ]}||fV  qqd S )Nr+   )r-   nodenbrlistr.   r+   r+   r,   	<genexpr>   s    z%from_dict_of_lists.<locals>.<genexpr>)r   empty_graphadd_nodes_fromr   is_directedr   add_edgeadd_edges_from)r2   r   r1   seenr3   r4   r.   r+   r+   r,   r      s    

c                s   i } dkr`|dkr8xH| j  D ]\}}|j ||< qW qx| j  D ]\}}|j||||< qBW n|dkrx D ]@}i ||< x2 fdd| | j D D ]\}}||| |< qW qnW nBx@ D ]8}i ||< x* fdd| | D D ]}||| |< qW qW |S )aH  Returns adjacency representation of graph as a dictionary of dictionaries.

    Parameters
    ----------
    G : graph
       A NetworkX graph

    nodelist : list
       Use only nodes specified in nodelist

    edge_data : list, optional
       If provided,  the value of the dictionary will be
       set to edge_data for all edges.  This is useful to make
       an adjacency matrix type representation with 1 as the edge data.
       If edgedata is None, the edgedata in G is used to fill the values.
       If G is a multigraph, the edgedata is a dict for each pair (u,v).
    Nc             3   s"   | ]\}}| kr||fV  qd S )Nr+   )r-   vr$   )r/   r+   r,   r5     s    z#to_dict_of_dicts.<locals>.<genexpr>c             3   s   | ]}| kr|V  qd S )Nr+   )r-   r<   )r/   r+   r,   r5     s    )Z	adjacencycopyfromkeysr   )r1   r/   Z	edge_dataZdoduZnbrdictr<   r$   r+   )r/   r,   r      s"    
$
c                s  t jd|}|j|  |r@|j r`|j rF|jdd | j D  n|jdd | j D  n|j rt }x| j D ]V\ }xL|j D ]@\} f|kr|j fdd|j D  |j f qW qxW njt }xb| j D ]V\ }xL|j D ]@\} f|kr|j fdd|j D  |j f qW qW n|j r|j  rt }x| j D ]d\ }xX|j D ]L\} f|kr|j	 dd |   d j
| |j f qxW qfW n|jdd | j D  |S )	a  Returns a graph from a dictionary of dictionaries.

    Parameters
    ----------
    d : dictionary of dictionaries
      A dictionary of dictionaries adjacency representation.

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
        Graph type to create. If graph instance, then cleared before populated.

    multigraph_input : bool (default False)
       When True, the values of the inner dict are assumed
       to be containers of edge data for multiple edges.
       Otherwise this routine assumes the edge data are singletons.

    Examples
    --------
    >>> dod = {0: {1: {"weight": 1}}}  # single edge (0,1)
    >>> G = nx.from_dict_of_dicts(dod)

    or

    >>> G = nx.Graph(dod)  # use Graph constructor

    r   c             s   sB   | ]:\}}|j  D ](\}}|j  D ]\}}||||fV  q"qqd S )N)r   )r-   r?   nbrsr<   datadictkeyr$   r+   r+   r,   r5   ;  s   z%from_dict_of_dicts.<locals>.<genexpr>c             s   s@   | ]8\}}|j  D ]&\}}|j  D ]\}}|||fV  q"qqd S )N)r   )r-   r?   r@   r<   rA   rB   r$   r+   r+   r,   r5   B  s   c             3   s   | ]\}} ||fV  qd S )Nr+   )r-   rB   r$   )r?   r<   r+   r,   r5   N  s    c             3   s   | ]\}} |fV  qd S )Nr+   )r-   rB   r$   )r?   r<   r+   r,   r5   W  s    )rB   c             s   s.   | ]&\}}|j  D ]\}}|||fV  qqd S )N)r   )r-   r?   r@   r<   r$   r+   r+   r,   r5   i  s    )r   r6   r7   r8   r   r:   r   setaddr9   r   )r2   r   r   r1   r;   r@   rA   r$   r+   )r?   r<   r,   r     sJ    
c             C   s"   |dkr| j ddS | j |ddS )zReturns a list of edges in the graph.

    Parameters
    ----------
    G : graph
       A NetworkX graph

    nodelist : list
       Use only nodes specified in nodelist

    NT)r$   )edges)r1   r/   r+   r+   r,   r   n  s    c             C   s   t jd|}|j|  |S )a  Returns a graph from a list of edges.

    Parameters
    ----------
    edgelist : list or iterator
      Edge tuples

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
        Graph type to create. If graph instance, then cleared before populated.

    Examples
    --------
    >>> edgelist = [(0, 1)]  # single edge (0,1)
    >>> G = nx.from_edgelist(edgelist)

    or

    >>> G = nx.Graph(edgelist)  # use Graph constructor

    r   )r   r6   r:   )Zedgelistr   r1   r+   r+   r,   r
     s    
)NF)N)N)NN)NF)N)N)__doc__r   Znetworkxr   collections.abcr   r   r   __all__r   r	   r   r   r   r   r
   r+   r+   r+   r,   <module>   s$   
 

(
(
U
