ó
|£*^c           @   sÍ   d  Z  d d l Z d d l Z d j d d d g ƒ Z d d d	 d
 d d d g Z d „  Z d e	 d „ Z
 d „  Z d „  Z d d „ Z d d „ Z d d d „ Z d e	 d „ Z d d „ Z d d „ Z d S(   sÝ  Functions to convert NetworkX graphs to and from other formats.

The preferred way of converting data to a NetworkX graph is through the
graph constuctor.  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
iÿÿÿÿNs   
s%   Aric Hagberg <aric.hagberg@gmail.com>s   Pieter Swart (swart@lanl.gov)s   Dan Schult(dschult@colgate.edu)t   to_networkx_grapht   from_dict_of_dictst   to_dict_of_dictst   from_dict_of_listst   to_dict_of_listst   from_edgelistt   to_edgelistc         C   s>   |  d k r t j ƒ  Sy |  j ƒ  Wn t d ƒ ‚ n X|  S(   s  Return a graph object ready to be populated.

    If create_using is None return the default (just networkx.Graph())
    If create_using.clear() works, assume it returns a graph object.
    Otherwise raise an exception because create_using is not a networkx graph.

    s(   Input graph is not a networkx graph typeN(   t   Nonet   nxt   Grapht   cleart	   TypeError(   t   create_using(    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pyt   _prep_create_using"   s    
c         C   sz  t  |  d ƒ rÐ y¥ t |  j d | d |  j ƒ  ƒ} t  |  d ƒ ri t |  j t ƒ ri |  j j ƒ  | _ n  t  |  d ƒ r¯ t |  j t ƒ r¯ t d „  |  j j	 ƒ  Dƒ ƒ | _ n  | SWqÐ t
 j d ƒ ‚ qÐ Xn  t  |  d ƒ ry t
 j j |  d | ƒSWqt
 j d	 ƒ ‚ qXn  t |  t ƒ ruy t |  d | d | ƒSWquy t |  d | ƒSWqrt d
 ƒ ‚ qrXquXn  t |  t ƒ s±t |  t ƒ s±t  |  d ƒ s±t  |  d ƒ ráy t |  d | ƒSWqát
 j d ƒ ‚ qáXn  y[ d d l } t |  | j ƒ r;y t
 j |  d | ƒSWq;d } t
 j | ƒ ‚ q;Xn  Wn' t k
 red } t j | t ƒ n Xyg d d l } t |  | j ƒ s™t |  | j ƒ rÌy t
 j |  d | ƒSWqÌt
 j d ƒ ‚ qÌXn  Wn! t k
 rðt j d t ƒ n XyR d d l } t  |  d ƒ rBy t
 j |  d | ƒSWqBt
 j d ƒ ‚ qBXn  Wn! t k
 rft j d t ƒ n Xt
 j d ƒ ‚ d S(   s¿  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 : a object to be converted
       Current known types are:
         any NetworkX graph
         dict-of-dicts
         dist-of-lists
         list of edges
         numpy matrix
         numpy ndarray
         scipy sparse matrix
         pygraphviz agraph

    create_using : NetworkX graph
       Use specified graph for result.  Otherwise a new graph is created.

    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.

    t   adjR   t   multigraph_inputt   grapht   nodec         s   s'   |  ] \ } } | | j  ƒ  f Vq d  S(   N(   t   copy(   t   .0t   nt   dd(    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pys	   <genexpr>_   s    s&   Input is not a correct NetworkX graph.t	   is_stricts(   Input is not a correct pygraphviz graph.s   Input is not known type.t   nextt   __next__s   Input is not a valid edge listiÿÿÿÿNs(   Input is not a correct Pandas DataFrame.s+   pandas not found, skipping conversion test.s-   Input is not a correct numpy matrix or array.s*   numpy not found, skipping conversion test.t   formats0   Input is not a correct scipy sparse matrix type.s*   scipy not found, skipping conversion test.s.   Input is not a known data type for conversion.(    t   hasattrR   R   t   is_multigrapht
   isinstanceR   t   dictR   R   t   itemsR   t   NetworkXErrort	   nx_agrapht   from_agraphR   R   t   listt   tupleR   t   pandast	   DataFramet   from_pandas_dataframet   ImportErrort   warningst   warnt   ImportWarningt   numpyt   matrixt   ndarrayt   from_numpy_matrixt   scipyt   from_scipy_sparse_matrix(   t   dataR   R   t   resultt   pdt   msgR+   R/   (    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pyR    2   sŽ    %!!%			c         C   s
   |  j  ƒ  S(   s6   Return a new undirected representation of the graph G.(   t   to_undirected(   t   G(    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pyt   convert_to_undirected¯   s    c         C   s
   |  j  ƒ  S(   s4   Return a new directed representation of the graph G.(   t   to_directed(   R6   (    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pyt   convert_to_directed´   s    c         C   sb   | d k r |  } n  i  } x@ | D]8 } g  |  j | ƒ D] } | | k r8 | ^ q8 | | <q" W| S(   s%  Return 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.

    N(   R   t	   neighbors(   R6   t   nodelistt   dR   t   nbr(    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pyR   ¹   s    	6c         C   s³   t  | ƒ } | j |  ƒ | j ƒ  r’ | j ƒ  r’ i  } xt |  j ƒ  D]F \ } } x- | D]% } | | k rX | j | | ƒ qX qX Wd | | <qE Wn | j d „  |  j ƒ  Dƒ ƒ | S(   s¯  Return a graph from a dictionary of lists.

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

    create_using : NetworkX graph
       Use specified graph for result.  Otherwise a new graph is created.

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

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

    i   c         s   s.   |  ]$ \ } } | D] } | | f Vq q d  S(   N(    (   R   R   t   nbrlistR=   (    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pys	   <genexpr>ò   s    	(   R   t   add_nodes_fromR   t   is_directedR   t   add_edget   add_edges_from(   R<   R   R6   t   seenR   R>   R=   (    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pyR   Ñ   s    c            s?  i  } ˆ  d k r„ | d k rN x` |  j ƒ  D] \ } } | j ƒ  | | <q+ Wq;xê |  j ƒ  D]" \ } } | j | | ƒ | | <q[ Wn· | d k rí x¨ ˆ  D]O } i  | | <x< ‡  f d †  |  | j ƒ  Dƒ D] \ } } | | | | <qÈ Wq— WnN xK ˆ  D]C } i  | | <x0 ‡  f d †  |  | Dƒ D] } | | | | <qWqô W| S(   sG  Return 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).
    c         3   s-   |  ]# \ } } | ˆ  k r | | f Vq d  S(   N(    (   R   t   vR1   (   R;   (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pys	   <genexpr>  s    c         3   s!   |  ] } | ˆ  k r | Vq d  S(   N(    (   R   RD   (   R;   (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pys	   <genexpr>  s    N(   R   t   adjacency_iterR   t   fromkeysR   (   R6   R;   t	   edge_datat   dodt   ut   nbrdictRD   R1   (    (   R;   sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pyR   ÷   s"    
-
!c            si  t  | ƒ } | j |  ƒ | r«| j ƒ  rw | j ƒ  rW | j d „  |  j ƒ  Dƒ ƒ q¨| j d „  |  j ƒ  Dƒ ƒ qe| j ƒ  rt ƒ  } x|  j ƒ  D]w \ ‰  } xh | j ƒ  D]Z \ ‰ } ˆ  ˆ f | k r² | j ‡  ‡ f d †  | j ƒ  Dƒ ƒ | j ˆ ˆ  f ƒ q² q² Wq™ Wqet ƒ  } xB|  j ƒ  D]w \ ‰  } xh | j ƒ  D]Z \ ‰ } ˆ  ˆ f | k rF| j ‡  ‡ f d †  | j ƒ  Dƒ ƒ | j ˆ ˆ  f ƒ qFqFWq-Wnº | j ƒ  rH| j ƒ  rHt ƒ  } x• |  j ƒ  D]g \ ‰  } xX | j ƒ  D]J \ ‰ } ˆ  ˆ f | k r*| j ˆ  ˆ d | ƒn  | j ˆ ˆ  f ƒ qóWqÚWn | j d „  |  j ƒ  Dƒ ƒ | S(   s·  Return a graph from a dictionary of dictionaries.

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

    create_using : NetworkX graph
       Use specified graph for result.  Otherwise a new graph is created.

    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

    c         s   sY   |  ]O \ } } | j  ƒ  D]6 \ } } | j  ƒ  D] \ } } | | | | f Vq/ q q d  S(   N(   R   (   R   RI   t   nbrsRD   t   datadictt   keyR1   (    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pys	   <genexpr>>  s   	c         s   sV   |  ]L \ } } | j  ƒ  D]3 \ } } | j  ƒ  D] \ } } | | | f Vq/ q q d  S(   N(   R   (   R   RI   RK   RD   RL   RM   R1   (    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pys	   <genexpr>D  s   	c         3   s'   |  ] \ } } ˆ  ˆ | | f Vq d  S(   N(    (   R   RM   R1   (   RI   RD   (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pys	   <genexpr>O  s   c         3   s$   |  ] \ } } ˆ  ˆ | f Vq d  S(   N(    (   R   RM   R1   (   RI   RD   (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pys	   <genexpr>X  s   t	   attr_dictc         s   s=   |  ]3 \ } } | j  ƒ  D] \ } } | | | f Vq q d  S(   N(   R   (   R   RI   RK   RD   R1   (    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pys	   <genexpr>h  s   	(	   R   R?   R@   R   RB   R   t   sett   addRA   (   R<   R   R   R6   RC   RK   RL   R1   (    (   RI   RD   sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pyR     sD    	!	!	c         C   s3   | d k r |  j d t ƒ S|  j | d t ƒSd S(   s°   Return a list of edges in the graph.

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

    nodelist : list
       Use only nodes specified in nodelist

    R1   N(   R   t   edgest   True(   R6   R;   (    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pyR   m  s    c         C   s   t  | ƒ } | j |  ƒ | S(   s“  Return a graph from a list of edges.

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

    create_using : NetworkX graph
       Use specified graph for result.  Otherwise a new graph is created.

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

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

    (   R   RB   (   t   edgelistR   R6   (    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pyR   ~  s    (   t   __doc__R(   t   networkxR   t   joint
   __author__t   __all__R   R   t   FalseR    R7   R9   R   R   R   R   R   R   (    (    (    sd   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/convert.pyt   <module>   s&   		}		&'O