ó
ú£Õ\c           @   së   d  Z  d d l Z d Z d d d d d d	 g Z d
 d „ Z d
 d d „ Z d d
 d d „ Z d
 d d d d d d „ Z	 e	 Z
 d
 d d d d „ Z d
 d d d d „ Z d
 d d d „ Z d
 d „ Z d
 d „ Z d d „ Z d „  Z d S(   sF   
******
Layout
******

Node positioning algorithms for graph drawing.
iÿÿÿÿNs?   Aric Hagberg (hagberg@lanl.gov)
Dan Schult(dschult@colgate.edu)t   circular_layoutt   random_layoutt   shell_layoutt   spring_layoutt   spectral_layoutt   fruchterman_reingold_layouti   c         C   sy   y d d l  } Wn t k
 r/ t d ƒ ‚ n Xt |  ƒ } | j | j j | | f ƒ d | j ƒ} t t |  | ƒ ƒ S(   sJ  Position nodes uniformly at random in the unit square.

    For every node, a position is generated by choosing each of dim
    coordinates uniformly at random on the interval [0.0, 1.0).

    NumPy (http://scipy.org) is required for this function.

    Parameters
    ----------
    G : NetworkX graph
       A position will be assigned to every node in G.

    dim : int
       Dimension of layout.

    Returns
    -------
    dict :
       A dictionary of positions keyed by node

    Examples
    --------
    >>> G = nx.lollipop_graph(4, 3)
    >>> pos = nx.random_layout(G)

    iÿÿÿÿNs2   random_layout() requires numpy: http://scipy.org/ t   dtype(   t   numpyt   ImportErrort   lent   asarrayt   randomt   float32t   dictt   zip(   t   Gt   dimt   npt   nt   pos(    (    sO   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/layout.pyR      s    *i   c         C   sü   y d d l  } Wn t k
 r/ t d ƒ ‚ n Xt |  ƒ d k rF i  St |  ƒ d k rq i d	 | |  j ƒ  d 6S| j d d | j d | j t |  ƒ d | j ƒ} | j | j | j	 | ƒ | j
 | ƒ g ƒ ƒ } t | d | ƒ} t t |  | ƒ ƒ S(
   s  Position nodes on a circle.

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

    dim : int
       Dimension of layout, currently only dim=2 is supported

    scale : float
        Scale factor for positions

    Returns
    -------
    dict :
       A dictionary of positions keyed by node

    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> pos=nx.circular_layout(G)

    Notes
    ------
    This algorithm currently only works in two dimensions and does not
    try to minimize edge crossings.

    iÿÿÿÿNs4   circular_layout() requires numpy: http://scipy.org/ i    i   g       @R   t   scale(   i   (   R   R   R	   t   nodest   aranget   piR   t	   transposet   arrayt   cost   sint   _rescale_layoutR   R   (   R   R   R   R   t   tR   (    (    sO   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/layout.pyR    ;   s    60c   
      C   s]  y d d l  } Wn t k
 r/ t d ƒ ‚ n Xt |  ƒ d k rF i  St |  ƒ d k rq i d
 | |  j ƒ  d 6S| d k r |  j ƒ  g } n  t | d ƒ d k r® d } n d } i  } xœ | D]” } | j d d | j d | j t | ƒ d	 | j ƒ} | j | j	 | | j
 | ƒ | | j | ƒ g ƒ ƒ }	 | j t | |	 ƒ ƒ | d 7} qÁ W| S(   sv  Position nodes in concentric circles.

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

    nlist : list of lists
       List of node lists for each shell.

    dim : int
       Dimension of layout, currently only dim=2 is supported

    scale : float
        Scale factor for positions

    Returns
    -------
    dict :
       A dictionary of positions keyed by node

    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> shells=[[0],[1,2,3]]
    >>> pos=nx.shell_layout(G,shells)

    Notes
    ------
    This algorithm currently only works in two dimensions and does not
    try to minimize edge crossings.

    iÿÿÿÿNs1   shell_layout() requires numpy: http://scipy.org/ i    i   g        g      ð?g       @R   (   i   (   R   R   R	   R   t   NoneR   R   R   R   R   R   R   t   updateR   (
   R   t   nlistR   R   R   t   radiust   nposR   R   R   (    (    sO   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/layout.pyR   f   s(    !	68i2   t   weightc         C   sý  y d d l  } Wn t k
 r/ t d ƒ ‚ n X| d k	 r† t t |  t t |  ƒ ƒ ƒ ƒ } | j g  | D] }	 | |	 ^ qj ƒ } n  | d k	 rÿ | j | j j t |  ƒ | f ƒ ƒ }
 xI t	 |  ƒ D]2 \ } } | | k rÆ | j | | ƒ |
 | <qÆ qÆ Wn d }
 t |  ƒ d k ri  St |  ƒ d k rFi d	 | |  j
 ƒ  d 6SyL t |  ƒ d k  rdt ‚ n  t j |  d | ƒ} t | | |
 | | ƒ } Wn4 t j |  d | ƒ} t | | |
 | | ƒ } n X| d k rêt | d | ƒ} n  t t |  | ƒ ƒ S(
   s6  Position nodes using Fruchterman-Reingold force-directed algorithm. 

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

    dim : int
       Dimension of layout

    pos : dict or None  optional (default=None)
       Initial positions for nodes as a dictionary with node as keys
       and values as a list or tuple.  If None, then nuse random initial
       positions.

    fixed : list or None  optional (default=None)
      Nodes to keep fixed at initial position.

    iterations : int  optional (default=50)
       Number of iterations of spring-force relaxation

    weight : string or None   optional (default='weight')
        The edge attribute that holds the numerical value used for
        the edge weight.  If None, then all edge weights are 1.

    scale : float
        Scale factor for positions

    Returns
    -------
    dict :
       A dictionary of positions keyed by node

    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> pos=nx.spring_layout(G)

    # The same using longer function name
    >>> pos=nx.fruchterman_reingold_layout(G)
    iÿÿÿÿNs@   fruchterman_reingold_layout() requires numpy: http://scipy.org/ i    i   iô  R#   R   (   i   (   R   R   R   R   R   t   rangeR	   R
   R   t	   enumerateR   t
   ValueErrort   nxt   to_scipy_sparse_matrixt   _sparse_fruchterman_reingoldt   to_numpy_matrixt   _fruchterman_reingoldR   (   R   R   R   t   fixedt
   iterationsR#   R   R   t   nfixedt   vt   pos_arrt   iR   t   A(    (    sO   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/layout.pyR   ¢   s8    .!)'!	c         C   s£  y d d  l  } Wn t k
 r/ t d ƒ ‚ n Xy |  j \ } } Wn  t k
 re t j d ƒ ‚ n X| j |  ƒ }  | d  k r® | j | j j | | f ƒ d |  j	 ƒ} n | j
 |  j	 ƒ } | j d | ƒ } d }	 |	 t | d ƒ }
 | j | j d | j d | j d f d |  j	 ƒ} xyt | ƒ D]k} xa t | j d ƒ D]L } | d  d  … | d  f | d  d  … | f | d  d  … d  d  … | f <qJW| j | d	 j d
 d ƒ ƒ } | j | d k  d | ƒ } | j | j | ƒ | | | d	 |  | | ƒ j d
 d ƒ } | j | d	 j d
 d ƒ ƒ } | j | d k  d | ƒ } | j | j | ƒ |	 | ƒ } | d  k	 r‡d | | <n  | | 7} |	 |
 8}	 q0W| S(   Niÿÿÿÿs:   _fruchterman_reingold() requires numpy: http://scipy.org/ s9   fruchterman_reingold() takes an adjacency matrix as inputR   g      ð?gš™™™™™¹?i   i    i   t   axisg{®Gáz„?g        (   R   R   t   shapet   AttributeErrorR'   t   NetworkXErrorR
   R   R   R   t   astypet   sqrtt   floatt   zerosR$   t   sumt   whereR   (   R2   R   R   R,   R-   R   t   nnodest   _t   kR   t   dtt   deltat	   iterationR1   t   distancet   displacementt   lengtht	   delta_pos(    (    sO   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/layout.pyR+   ô   sB    -6J%	 
c      	   C   sÉ  y d d  l  } Wn t k
 r/ t d ƒ ‚ n Xy |  j \ } } Wn  t k
 re t j d ƒ ‚ n Xy d d l m } m }	 Wn t k
 rŸ t d ƒ ‚ n Xy |  j	 ƒ  }  Wn |	 |  ƒ j	 ƒ  }  n X| d  k r| j | j j | | f ƒ d |  j ƒ} n | j |  j ƒ } | d  k r,g  } n  | j d | ƒ }
 d } | t | d	 ƒ } | j | | f ƒ } xTt | ƒ D]F} | d
 9} xÚ t |  j d
 ƒ D]Å } | | k r·qŸn  | | | j } | j | d j d d
 ƒ ƒ } | j | d k  d | ƒ } | j |  j | ƒ j ƒ  ƒ } | d  d  … | f c | |
 |
 | d | | |
 j d d	 ƒ 7<qŸW| j | d j d d
 ƒ ƒ } | j | d k  d | ƒ } | | | | j 7} | | 8} q{W| S(   NiÿÿÿÿsA   _sparse_fruchterman_reingold() requires numpy: http://scipy.org/ s9   fruchterman_reingold() takes an adjacency matrix as input(   t   spdiagst
   coo_matrixs>   _sparse_fruchterman_reingold() scipy numpy: http://scipy.org/ R   g      ð?gš™™™™™¹?i   i    i   R3   g{®Gáz„?(   R   R   R4   R5   R'   R6   t   scipy.sparseRG   RH   t   tolilR   R
   R   R   R7   R8   R9   R:   R$   t   TR;   R<   t
   getrowviewt   toarray(   R2   R   R   R,   R-   R   R=   R>   RG   RH   R?   R   R@   RD   RB   R1   RA   RC   t   AiRE   (    (    sO   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/layout.pyR)   1  sT    -	
2c         C   s´  y d d l  } Wn t k
 r/ t d ƒ ‚ n Xt |  ƒ d k rÇ t |  ƒ d k rf | j g  ƒ } nN t |  ƒ d k r“ | j d d g g ƒ } n! | j d d g d d g g ƒ } t t |  | ƒ ƒ Syk t |  ƒ d k  rå t ‚ n  t j |  d	 | d
 d ƒ} |  j	 ƒ  r"| | j
 | ƒ } n  t | | ƒ } Wn] t t f k
 r‘t j |  d	 | ƒ} |  j	 ƒ  r| | j
 | ƒ } n  t | | ƒ } n Xt | | ƒ } t t |  | ƒ ƒ S(   s  Position nodes using the eigenvectors of the graph Laplacian. 

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

    dim : int
       Dimension of layout

    weight : string or None   optional (default='weight')
        The edge attribute that holds the numerical value used for
        the edge weight.  If None, then all edge weights are 1.

    scale : float
        Scale factor for positions

    Returns
    -------
    dict :
       A dictionary of positions keyed by node

    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> pos=nx.spectral_layout(G)

    Notes
    -----
    Directed graphs will be considered as unidrected graphs when
    positioning the nodes.

    For larger graphs (>500 nodes) this will use the SciPy sparse
    eigenvalue solver (ARPACK).
    iÿÿÿÿNs4   spectral_layout() requires numpy: http://scipy.org/ i   i    i   g      à?iô  R#   R   t   f(   R   R   R	   R   R   R   R&   R'   R(   t   is_directedR   t   _sparse_spectralR*   t	   _spectralR   (   R   R   R#   R   R   R   R2   (    (    sO   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/layout.pyR   x  s2    $!	c         C   sÿ   y d d  l  } Wn t k
 r/ t d ƒ ‚ n Xy |  j \ } } Wn  t k
 re t j d ƒ ‚ n X| j |  ƒ }  | j | d |  j ƒ} | | j	 |  d d ƒ} | |  } | j
 j | ƒ \ } }	 | j | ƒ d | d !}
 | j |	 d  d  … |
 f ƒ S(   Niÿÿÿÿs4   spectral_layout() requires numpy: http://scipy.org/ s-   spectral() takes an adjacency matrix as inputR   R3   i   (   R   R   R4   R5   R'   R6   R
   t   identityR   R;   t   linalgt   eigt   argsortt   real(   R2   R   R   R=   R>   t   It   Dt   Lt   eigenvaluest   eigenvectorst   index(    (    sO   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/layout.pyRR   ½  s     
c         C   so  y  d d  l  } d d l m } Wn t k
 r? t d ƒ ‚ n Xy d d l m } Wn! t k
 rw d d l m } n Xy |  j \ } } Wn  t	 k
 r­ t
 j d ƒ ‚ n X| j |  j d d ƒ j ƒ } | | d	 | | ƒ } | |  }	 | d }
 t d
 |
 d t | j | ƒ ƒ ƒ } | |	 |
 d d d | ƒ\ } } | j | ƒ d |
 !} | j | d  d  … | f ƒ S(   Niÿÿÿÿ(   RG   s=   _sparse_spectral() requires scipy & numpy: http://scipy.org/ (   t   eigsh(   t   eigen_symmetrics4   sparse_spectral() takes an adjacency matrix as inputR3   i   i    i   t   whicht   SMt   ncv(   R   RI   RG   R   t   scipy.sparse.linalg.eigenR^   t   scipy.sparse.linalgR_   R4   R5   R'   R6   R
   R;   RK   t   maxt   intR8   RV   RW   (   R2   R   R   RG   R^   R=   R>   t   dataRY   RZ   R?   Rb   R[   R\   R]   (    (    sO   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/layout.pyRQ   Ö  s,    

&!c         C   s½   d } xr t  |  j d ƒ D]] } |  d  d  … | f c |  d  d  … | f j ƒ  8<t |  d  d  … | f j ƒ  | ƒ } q Wx; t  |  j d ƒ D]& } |  d  d  … | f c | | 9<q W|  S(   Ni    i   (   R$   R4   t   minRe   (   R   R   t   limR1   (    (    sO   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/layout.pyR   ø  s    2)$c         C   s`   d d l  m } y d d  l } Wn | d ƒ ‚ n Xy d d  l } Wn | d ƒ ‚ n Xd  S(   Niÿÿÿÿ(   t   SkipTests   NumPy not availables   SciPy not available(   t   noseRj   R   t   scipy(   t   moduleRj   R   Rl   (    (    sO   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/layout.pyt   setup_module  s    (   t   __doc__t   networkxR'   t
   __author__t   __all__R   R    R   R   R   R   R+   R)   R   RR   RQ   R   Rn   (    (    (    sO   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/drawing/layout.pyt   <module>   s2   	$+<K=GE"