ó
|£*^c        	   @   s  d  Z  d d l Z d d l Z d d d d d d g Z d	 d
 d d „ Z d	 d
 d d „ Z d d	 d
 d d „ Z d	 d d d d d d
 d d „ Z	 e	 Z
 d	 d d d d d „ Z d	 d d d d d „ Z d	 d d
 d d „ Z d	 d „ Z d	 d „ Z d
 d „ Z d „  Z d S(   s  
******
Layout
******

Node positioning algorithms for graph drawing.

The default scales and centering for these layouts are
typically squares with side [0, 1] or [0, scale].
The two circular layout routines (circular_layout and
shell_layout) have size [-1, 1] or [-scale, scale].
iÿÿÿÿNt   circular_layoutt   random_layoutt   shell_layoutt   spring_layoutt   spectral_layoutt   fruchterman_reingold_layouti   g      ð?c         C   sq   d d l  } t |  ƒ | f } | j j | ƒ | } | d k	 r^ | | j | ƒ d | 7} n  t t |  | ƒ ƒ S(   sA  Position nodes uniformly at random.

    For every node, a position is generated by choosing each of dim
    coordinates uniformly at random on the default interval [0.0, 1.0),
    or on an interval of length `scale` centered at `center`.

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

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

    dim : int
       Dimension of layout.

    scale : float (default 1)
        Scale factor for positions

    center : array-like (default scale*0.5 in each dim)
       Coordinate around which to center the layout.

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

    Examples
    --------
    >>> G = nx.lollipop_graph(4, 3)
    >>> pos = nx.random_layout(G)
    iÿÿÿÿNg      à?(   t   numpyt   lent   randomt   Nonet   asarrayt   dictt   zip(   t   Gt   dimt   scalet   centert   npt   shapet   pos(    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/drawing/layout.pyR   !   s    !c         C   s®   d d l  } t |  ƒ d k r" i  Sd | j } | j d | | t |  ƒ ƒ } | j | j | ƒ | j | ƒ g ƒ | } | d k	 r› | | j | ƒ 7} n  t	 t
 |  | ƒ ƒ S(   s™  Position nodes on a circle.

    Parameters
    ----------
    G : NetworkX graph or list of nodes

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

    scale : float (default 1)
        Scale factor for positions, i.e. radius of circle.

    center : array-like (default origin)
       Coordinate around which to center the layout.

    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ÿÿÿÿNi    g       @(   R   R   t   pit   aranget   column_stackt   cost   sinR	   R
   R   R   (   R   R   R   R   R   t   twopit   thetaR   (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/drawing/layout.pyR    K   s     +c         C   sv  d d l  } t |  ƒ d k r" i  S| d k r@ t |  ƒ g } n  t | ƒ } t | d ƒ d k ru d } | d 8} n d } | r‹ | | n | } | | 9} i  }	 d | j }
 xx | D]p } | j d |
 |
 t | ƒ ƒ } | j | j | ƒ | j | ƒ g ƒ | } |	 j	 t
 | | ƒ ƒ | | 7} qµ W| d k	 rr| j | ƒ } x+ |	 j ƒ  D] \ } } | | |	 | <qQWn  |	 S(   s  Position nodes in concentric circles.

    Parameters
    ----------
    G : NetworkX graph or list of nodes

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

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

    scale : float (default 1)
        Scale factor for positions, i.e.radius of largest shell

    center : array-like (default origin)
       Coordinate around which to center the layout.

    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ÿÿÿÿNi    i   g        g      ð?g       @(   R   R   R	   t   listR   R   R   R   R   t   updateR   R
   t   items(   R   t   nlistR   R   R   R   t   numb_shellst   radiust   gapt   nposR   t   nodesR   R   t   nt   p(    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/drawing/layout.pyR   x   s0    $
+i2   t   weightc	         C   s~  d d l  }	 t |  ƒ d k r" i  S| d k	 r™ t t |  t t |  ƒ ƒ ƒ ƒ }
 |	 j g  | D] } |
 | ^ q\ ƒ } | d k r™ d } t | ƒ ‚ q™ n  | d k	 rT|	 j t	 | j
 ƒ  ƒ ƒ } | j d ƒ } | j d ƒ | } t |  ƒ | f } |	 j j | ƒ | | } xI t |  ƒ D]2 \ } } | | k r|	 j | | ƒ | | <qqWn d } | d k r”| d k	 r”| j ƒ  |	 j t |  ƒ ƒ } n  yU t |  ƒ d k  r²t ‚ n  t j |  d | d d ƒ} t | | | | | | ƒ } Wn7 t j |  d | ƒ} t | | | | | | ƒ } n X| d k rkt | | ƒ } | d k	 rk| |	 j | ƒ d	 | 7} qkn  t t |  | ƒ ƒ S(
   sU  Position nodes using Fruchterman-Reingold force-directed algorithm.

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

    dim : int
       Dimension of layout

    k : float (default=None)
       Optimal distance between nodes.  If None the distance is set to
       1/sqrt(n) where n is the number of nodes.  Increase this value
       to move nodes farther apart.

    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 use random initial
       positions.

    fixed : list or None  optional (default=None)
      Nodes to keep fixed at initial position.
      If any nodes are fixed, the scale and center features are not used.

    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 effective spring constant. If None, edge weights are 1.

    scale : float (default=1.0)
        Scale factor for positions. The nodes are positioned
        in a box of size `scale` in each dim centered at `center`.

    center : array-like (default scale/2 in each dim)
       Coordinate around which to center the layout.

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

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

    # this function has two names:
    # spring_layout and fruchterman_reingold_layout
    >>> pos=nx.fruchterman_reingold_layout(G)
    iÿÿÿÿNi    s4   Keyword pos must be specified if any nodes are fixediô  R&   t   dtypet   fg      à?(   R   R   R	   R   R   t   rangeR
   t
   ValueErrort   arrayR   t   valuest   mint   maxR   t	   enumeratet   sqrtt   nxt   to_scipy_sparse_matrixt   _sparse_fruchterman_reingoldt   to_numpy_matrixt   _fruchterman_reingoldt   _rescale_layout(   R   R   t   kR   t   fixedt
   iterationsR&   R   R   R   t   nfixedt   vt   msgt
   pos_coordst
   min_coordst   domain_sizeR   t   pos_arrt   iR$   t   A(    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/drawing/layout.pyR   Á   sD    :!&!"	!c         C   sò  d d  l  } y |  j \ } } Wn  t k
 rA t j d ƒ ‚ n X| j |  ƒ }  | d  k rŠ | j | j j | | f ƒ d |  j ƒ} n | j	 |  j ƒ } | d  k r¾ | j
 d | ƒ } n  t t | j d ƒ t | j d ƒ t | j d ƒ t | j d ƒ ƒ d }	 |	 t | d ƒ }
 | j | j d | j d | j d f d |  j ƒ} x”t | ƒ D]†} xa t | j d ƒ D]L } | d  d  … | d  f | d  d  … | f | d  d  … d  d  … | f <q~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}	 | d  k rdt | ƒ } qdqdW| S(   Niÿÿÿÿs9   fruchterman_reingold() takes an adjacency matrix as inputR'   g      ð?i    i   gš™™™™™¹?i   t   axisg{®Gáz„?g        (   R   R   t   AttributeErrorR1   t   NetworkXErrorR
   R	   R   R'   t   astypeR0   R.   t   TR-   t   floatt   zerosR)   t   sumt   wheret	   transposeR6   (   RB   R   R7   R   R8   R9   R   t   nnodest   _t   tt   dtt   deltat	   iterationRA   t   distancet   displacementt   lengtht	   delta_pos(    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/drawing/layout.pyR5   +  sB    -O6J%	 

c      	   C   s  d d  l  } y |  j \ } } Wn  t k
 rA 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  | d  k r*| j d | ƒ } n  t t | j d ƒ t | j d ƒ t | j d ƒ t | j d ƒ ƒ d	 } | t | d ƒ } | j | | f ƒ } xot | ƒ D]a} | 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} | d  k r¯t | ƒ } q¯q¯W| S(   Niÿÿÿÿs9   fruchterman_reingold() takes an adjacency matrix as input(   t   spdiagst
   coo_matrixs>   _sparse_fruchterman_reingold() scipy numpy: http://scipy.org/ R'   g      ð?i    i   gš™™™™™¹?i   RC   g{®Gáz„?(   R   R   RD   R1   RE   t   scipy.sparseRW   RX   t   ImportErrort   tolilR	   R
   R   R'   RF   R0   R.   RG   R-   RH   RI   R)   RJ   RK   t
   getrowviewt   toarrayR6   (   RB   R   R7   R   R8   R9   R   RM   RN   RW   RX   RO   RP   RT   RR   RA   RQ   RS   t   AiRU   (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/drawing/layout.pyR3   g  sT    -	O
2
c         C   s  d d l  } t |  ƒ d k rì t |  ƒ d k r4 i  St |  ƒ d k r„ | d k	 rd | j | ƒ } qÙ | j d | f ƒ | d } nU | j | j | ƒ | j | ƒ | g ƒ } | d k	 rÙ | | j | ƒ | d 7} n  t t |  | ƒ ƒ Syk t |  ƒ d k  r
t	 ‚ n  t
 j |  d | d	 d
 ƒ} |  j ƒ  rG| | j | ƒ } n  t | | ƒ } Wn] t t	 f k
 r¶t
 j |  d | ƒ} |  j ƒ  r¤| | j | ƒ } n  t | | ƒ } n Xt | | ƒ } | d k	 rð| | j | ƒ d | 7} n  t t |  | ƒ ƒ S(   s  Position nodes using the eigenvectors of the graph Laplacian.

    Parameters
    ----------
    G : NetworkX graph or list of nodes

    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 optional (default 1)
        Scale factor for positions, i.e. nodes placed in a box with
        side [0, scale] or centered on `center` if provided.

    center : array-like (default scale/2 in each dim)
       Coordinate around which to center the layout.

    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 undirected graphs when
    positioning the nodes.

    For larger graphs (>500 nodes) this will use the SciPy sparse
    eigenvalue solver (ARPACK).
    iÿÿÿÿNi   i    i   g      à?iô  R&   R'   t   d(   R   R   R	   R
   t   onesR+   RI   R   R   R*   R1   R2   t   is_directedRL   t   _sparse_spectralRZ   R4   t	   _spectralR6   (   R   R   R&   R   R   R   R   RB   (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/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 |  ƒ }  | 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'   RC   i   (   R   RZ   R   RD   R1   RE   R
   t   identityR'   RJ   t   linalgt   eigt   argsortt   real(   RB   R   R   RM   RN   t   It   Dt   Lt   eigenvaluest   eigenvectorst   index(    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/drawing/layout.pyRc   þ  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ÿÿÿÿ(   RW   s=   _sparse_spectral() requires scipy & numpy: http://scipy.org/ (   t   eigsh(   t   eigen_symmetrics4   sparse_spectral() takes an adjacency matrix as inputRC   i   i    i   t   whicht   SMt   ncv(   R   RY   RW   RZ   t   scipy.sparse.linalg.eigenRo   t   scipy.sparse.linalgRp   R   RD   R1   RE   R
   RJ   RG   R.   t   intR0   Rg   Rh   (   RB   R   R   RW   Ro   RM   RN   t   dataRj   Rk   R7   Rs   Rl   Rm   Rn   (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/drawing/layout.pyRb     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 W| d k rÈ x> t  |  j d ƒ D]& } |  d  d  … | f c | | 9<q› Wn  |  S(   Ni    i   (   R)   R   R-   R.   (   R   R   t   maxlimRA   (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/drawing/layout.pyR6   9  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   noseRy   R   t   scipy(   t   moduleRy   R   R{   (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/drawing/layout.pyt   setup_moduleG  s    (   t   __doc__t   collectionst   networkxR1   t   __all__R	   R   R    R   R   R   R5   R3   R   Rc   Rb   R6   R}   (    (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/drawing/layout.pyt   <module>   s6   
	*-Ib<GO"