<s> """<STR_LIT>""" <EOL> import networkx as nx <EOL> __author__ = """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def random_layout ( G , dim = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> n = len ( G ) <EOL> pos = np . asarray ( np . random . random ( ( n , dim ) ) , dtype = np . float32 ) <EOL> return dict ( zip ( G , pos ) ) <EOL> def circular_layout ( G , dim = <NUM_LIT:2> , scale = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> if len ( G ) == <NUM_LIT:0> : <EOL> return { } <EOL> if len ( G ) == <NUM_LIT:1> : <EOL> return { G . nodes ( ) [ <NUM_LIT:0> ] : ( <NUM_LIT:1> , ) * dim } <EOL> t = np . arange ( <NUM_LIT:0> , <NUM_LIT> * np . pi , <NUM_LIT> * np . pi / len ( G ) , dtype = np . float32 ) <EOL> pos = np . transpose ( np . array ( [ np . cos ( t ) , np . sin ( t ) ] ) ) <EOL> pos = _rescale_layout ( pos , scale = scale ) <EOL> return dict ( zip ( G , pos ) ) <EOL> def shell_layout ( G , nlist = None , dim = <NUM_LIT:2> , scale = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> if len ( G ) == <NUM_LIT:0> : <EOL> return { } <EOL> if len ( G ) == <NUM_LIT:1> : <EOL> return { G . nodes ( ) [ <NUM_LIT:0> ] : ( <NUM_LIT:1> , ) * dim } <EOL> if nlist == None : <EOL> nlist = [ G . nodes ( ) ] <EOL> if len ( nlist [ <NUM_LIT:0> ] ) == <NUM_LIT:1> : <EOL> radius = <NUM_LIT:0.0> <EOL> else : <EOL> radius = <NUM_LIT:1.0> <EOL> npos = { } <EOL> for nodes in nlist : <EOL> t = np . arange ( <NUM_LIT:0> , <NUM_LIT> * np . pi , <NUM_LIT> * np . pi / len ( nodes ) , dtype = np . float32 ) <EOL> pos = np . transpose ( np . array ( [ radius * np . cos ( t ) , radius * np . sin ( t ) ] ) ) <EOL> npos . update ( zip ( nodes , pos ) ) <EOL> radius += <NUM_LIT:1.0> <EOL> return npos <EOL> def fruchterman_reingold_layout ( G , dim = <NUM_LIT:2> , k = None , <EOL> pos = None , <EOL> fixed = None , <EOL> iterations = <NUM_LIT:50> , <EOL> weight = '<STR_LIT>' , <EOL> scale = <NUM_LIT:1.0> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> if fixed is not None : <EOL> nfixed = dict ( zip ( G , range ( len ( G ) ) ) ) <EOL> fixed = np . asarray ( [ nfixed [ v ] for v in fixed ] ) <EOL> if pos is not None : <EOL> pos_arr = np . asarray ( np . random . random ( ( len ( G ) , dim ) ) ) <EOL> for i , n in enumerate ( G ) : <EOL> if n in pos : <EOL> pos_arr [ i ] = np . asarray ( pos [ n ] ) <EOL> else : <EOL> pos_arr = None <EOL> if len ( G ) == <NUM_LIT:0> : <EOL> return { } <EOL> if len ( G ) == <NUM_LIT:1> : <EOL> return { G . nodes ( ) [ <NUM_LIT:0> ] : ( <NUM_LIT:1> , ) * dim } <EOL> try : <EOL> if len ( G ) < <NUM_LIT> : <EOL> raise ValueError <EOL> A = nx . to_scipy_sparse_matrix ( G , weight = weight , dtype = '<STR_LIT:f>' ) <EOL> pos = _sparse_fruchterman_reingold ( A , dim , k , pos_arr , fixed , iterations ) <EOL> except : <EOL> A = nx . to_numpy_matrix ( G , weight = weight ) <EOL> pos = _fruchterman_reingold ( A , dim , k , pos_arr , fixed , iterations ) <EOL> if fixed is None : <EOL> pos = _rescale_layout ( pos , scale = scale ) <EOL> return dict ( zip ( G , pos ) ) <EOL> spring_layout = fruchterman_reingold_layout <EOL> def _fruchterman_reingold ( A , dim = <NUM_LIT:2> , k = None , pos = None , fixed = None , <EOL> iterations = <NUM_LIT:50> ) : <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> try : <EOL> nnodes , _ = A . shape <EOL> except AttributeError : <EOL> raise nx . NetworkXError ( <EOL> "<STR_LIT>" ) <EOL> A = np . asarray ( A ) <EOL> if pos == None : <EOL> pos = np . asarray ( np . random . random ( ( nnodes , dim ) ) , dtype = A . dtype ) <EOL> else : <EOL> pos = pos . astype ( A . dtype ) <EOL> if k is None : <EOL> k = np . sqrt ( <NUM_LIT:1.0> / nnodes ) <EOL> t = <NUM_LIT:0.1> <EOL> dt = t / float ( iterations + <NUM_LIT:1> ) <EOL> delta = np . zeros ( ( pos . shape [ <NUM_LIT:0> ] , pos . shape [ <NUM_LIT:0> ] , pos . shape [ <NUM_LIT:1> ] ) , dtype = A . dtype ) <EOL> for iteration in range ( iterations ) : <EOL> for i in range ( pos . shape [ <NUM_LIT:1> ] ) : <EOL> delta [ : , : , i ] = pos [ : , i , None ] - pos [ : , i ] <EOL> distance = np . sqrt ( ( delta ** <NUM_LIT:2> ) . sum ( axis = - <NUM_LIT:1> ) ) <EOL> distance = np . where ( distance < <NUM_LIT> , <NUM_LIT> , distance ) <EOL> displacement = np . transpose ( np . transpose ( delta ) * ( k * k / distance ** <NUM_LIT:2> - A * distance / k ) ) . sum ( axis = <NUM_LIT:1> ) <EOL> length = np . sqrt ( ( displacement ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:1> ) ) <EOL> length = np . where ( length < <NUM_LIT> , <NUM_LIT:0.1> , length ) <EOL> delta_pos = np . transpose ( np . transpose ( displacement ) * t / length ) <EOL> if fixed is not None : <EOL> delta_pos [ fixed ] = <NUM_LIT:0.0> <EOL> pos += delta_pos <EOL> t -= dt <EOL> pos = _rescale_layout ( pos ) <EOL> return pos <EOL> def _sparse_fruchterman_reingold ( A , dim = <NUM_LIT:2> , k = None , pos = None , fixed = None , <EOL> iterations = <NUM_LIT:50> ) : <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> try : <EOL> nnodes , _ = A . shape <EOL> except AttributeError : <EOL> raise nx . NetworkXError ( <EOL> "<STR_LIT>" ) <EOL> try : <EOL> from scipy . sparse import spdiags , coo_matrix <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> try : <EOL> A = A . tolil ( ) <EOL> except : <EOL> A = ( coo_matrix ( A ) ) . tolil ( ) <EOL> if pos == None : <EOL> pos = np . asarray ( np . random . random ( ( nnodes , dim ) ) , dtype = A . dtype ) <EOL> else : <EOL> pos = pos . astype ( A . dtype ) <EOL> if fixed == None : <EOL> fixed = [ ] <EOL> if k is None : <EOL> k = np . sqrt ( <NUM_LIT:1.0> / nnodes ) <EOL> t = <NUM_LIT:0.1> <EOL> dt = t / float ( iterations + <NUM_LIT:1> ) <EOL> displacement = np . zeros ( ( dim , nnodes ) ) <EOL> for iteration in range ( iterations ) : <EOL> displacement *= <NUM_LIT:0> <EOL> for i in range ( A . shape [ <NUM_LIT:0> ] ) : <EOL> if i in fixed : <EOL> continue <EOL> delta = ( pos [ i ] - pos ) . T <EOL> distance = np . sqrt ( ( delta ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:0> ) ) <EOL> distance = np . where ( distance < <NUM_LIT> , <NUM_LIT> , distance ) <EOL> Ai = np . asarray ( A . getrowview ( i ) . toarray ( ) ) <EOL> displacement [ : , i ] += ( delta * ( k * k / distance ** <NUM_LIT:2> - Ai * distance / k ) ) . sum ( axis = <NUM_LIT:1> ) <EOL> length = np . sqrt ( ( displacement ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:0> ) ) <EOL> length = np . where ( length < <NUM_LIT> , <NUM_LIT:0.1> , length ) <EOL> pos += ( displacement * t / length ) . T <EOL> t -= dt <EOL> pos = _rescale_layout ( pos ) <EOL> return pos <EOL> def spectral_layout ( G , dim = <NUM_LIT:2> , weight = '<STR_LIT>' , scale = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> if len ( G ) <= <NUM_LIT:2> : <EOL> if len ( G ) == <NUM_LIT:0> : <EOL> pos = np . array ( [ ] ) <EOL> elif len ( G ) == <NUM_LIT:1> : <EOL> pos = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> else : <EOL> pos = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:0.5> ] , [ <NUM_LIT:1> , <NUM_LIT:0.5> ] ] ) <EOL> return dict ( zip ( G , pos ) ) <EOL> try : <EOL> if len ( G ) < <NUM_LIT> : <EOL> raise ValueError <EOL> A = nx . to_scipy_sparse_matrix ( G , weight = weight , dtype = '<STR_LIT:f>' ) <EOL> if G . is_directed ( ) : <EOL> A = A + np . transpose ( A ) <EOL> pos = _sparse_spectral ( A , dim ) <EOL> except ( ImportError , ValueError ) : <EOL> A = nx . to_numpy_matrix ( G , weight = weight ) <EOL> if G . is_directed ( ) : <EOL> A = A + np . transpose ( A ) <EOL> pos = _spectral ( A , dim ) <EOL> pos = _rescale_layout ( pos , scale ) <EOL> return dict ( zip ( G , pos ) ) <EOL> def _spectral ( A , dim = <NUM_LIT:2> ) : <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> try : <EOL> nnodes , _ = A . shape <EOL> except AttributeError : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> A = np . asarray ( A ) <EOL> I = np . identity ( nnodes , dtype = A . dtype ) <EOL> D = I * np . sum ( A , axis = <NUM_LIT:1> ) <EOL> L = D - A <EOL> eigenvalues , eigenvectors = np . linalg . eig ( L ) <EOL> index = np . argsort ( eigenvalues ) [ <NUM_LIT:1> : dim + <NUM_LIT:1> ] <EOL> return np . real ( eigenvectors [ : , index ] ) <EOL> def _sparse_spectral ( A , dim = <NUM_LIT:2> ) : <EOL> try : <EOL> import numpy as np <EOL> from scipy . sparse import spdiags <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> try : <EOL> from scipy . sparse . linalg . eigen import eigsh <EOL> except ImportError : <EOL> from scipy . sparse . linalg import eigen_symmetric as eigsh <EOL> try : <EOL> nnodes , _ = A . shape <EOL> except AttributeError : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> data = np . asarray ( A . sum ( axis = <NUM_LIT:1> ) . T ) <EOL> D = spdiags ( data , <NUM_LIT:0> , nnodes , nnodes ) <EOL> L = D - A <EOL> k = dim + <NUM_LIT:1> <EOL> ncv = max ( <NUM_LIT:2> * k + <NUM_LIT:1> , int ( np . sqrt ( nnodes ) ) ) <EOL> eigenvalues , eigenvectors = eigsh ( L , k , which = '<STR_LIT>' , ncv = ncv ) <EOL> index = np . argsort ( eigenvalues ) [ <NUM_LIT:1> : k ] <EOL> return np . real ( eigenvectors [ : , index ] ) <EOL> def _rescale_layout ( pos , scale = <NUM_LIT:1> ) : <EOL> lim = <NUM_LIT:0> <EOL> for i in range ( pos . shape [ <NUM_LIT:1> ] ) : <EOL> pos [ : , i ] -= pos [ : , i ] . min ( ) <EOL> lim = max ( pos [ : , i ] . max ( ) , lim ) <EOL> for i in range ( pos . shape [ <NUM_LIT:1> ] ) : <EOL> pos [ : , i ] *= scale / lim <EOL> return pos <EOL> def setup_module ( module ) : <EOL> from nose import SkipTest <EOL> try : <EOL> import numpy <EOL> except : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> try : <EOL> import scipy <EOL> except : <EOL> raise SkipTest ( "<STR_LIT>" ) </s>
