<s> from __future__ import division , print_function , absolute_import <EOL> import numpy as np <EOL> from numpy . testing import ( run_module_suite , assert_allclose , <EOL> assert_array_almost_equal , assert_raises ) <EOL> from scipy import sparse <EOL> from scipy . sparse import csgraph <EOL> def _explicit_laplacian ( x , normed = False ) : <EOL> if sparse . issparse ( x ) : <EOL> x = x . todense ( ) <EOL> x = np . asarray ( x ) <EOL> y = - <NUM_LIT:1.0> * x <EOL> for j in range ( y . shape [ <NUM_LIT:0> ] ) : <EOL> y [ j , j ] = x [ j , j + <NUM_LIT:1> : ] . sum ( ) + x [ j , : j ] . sum ( ) <EOL> if normed : <EOL> d = np . diag ( y ) . copy ( ) <EOL> d [ d == <NUM_LIT:0> ] = <NUM_LIT:1.0> <EOL> y /= d [ : , None ] ** <NUM_LIT> <EOL> y /= d [ None , : ] ** <NUM_LIT> <EOL> return y <EOL> def _check_symmetric_graph_laplacian ( mat , normed ) : <EOL> if not hasattr ( mat , '<STR_LIT>' ) : <EOL> mat = eval ( mat , dict ( np = np , sparse = sparse ) ) <EOL> if sparse . issparse ( mat ) : <EOL> sp_mat = mat <EOL> mat = sp_mat . todense ( ) <EOL> else : <EOL> sp_mat = sparse . csr_matrix ( mat ) <EOL> laplacian = csgraph . laplacian ( mat , normed = normed ) <EOL> n_nodes = mat . shape [ <NUM_LIT:0> ] <EOL> if not normed : <EOL> assert_array_almost_equal ( laplacian . sum ( axis = <NUM_LIT:0> ) , np . zeros ( n_nodes ) ) <EOL> assert_array_almost_equal ( laplacian . T , laplacian ) <EOL> assert_array_almost_equal ( laplacian , <EOL> csgraph . laplacian ( sp_mat , normed = normed ) . todense ( ) ) <EOL> assert_array_almost_equal ( laplacian , <EOL> _explicit_laplacian ( mat , normed = normed ) ) <EOL> def test_laplacian_value_error ( ) : <EOL> for t in int , float , complex : <EOL> for m in ( [ <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> [ [ [ <NUM_LIT:1> ] ] ] , <EOL> [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ] , <EOL> [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:5> ] ] ) : <EOL> A = np . array ( m , dtype = t ) <EOL> assert_raises ( ValueError , csgraph . laplacian , A ) <EOL> def test_symmetric_graph_laplacian ( ) : <EOL> symmetric_mats = ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> for mat_str in symmetric_mats : <EOL> for normed in True , False : <EOL> yield _check_symmetric_graph_laplacian , mat_str , normed <EOL> def _assert_allclose_sparse ( a , b , ** kwargs ) : <EOL> if sparse . issparse ( a ) : <EOL> a = a . toarray ( ) <EOL> if sparse . issparse ( b ) : <EOL> b = a . toarray ( ) <EOL> assert_allclose ( a , b , ** kwargs ) <EOL> def _check_laplacian ( A , desired_L , desired_d , normed , use_out_degree ) : <EOL> for arr_type in np . array , sparse . csr_matrix , sparse . coo_matrix : <EOL> for t in int , float , complex : <EOL> adj = arr_type ( A , dtype = t ) <EOL> L = csgraph . laplacian ( adj , normed = normed , return_diag = False , <EOL> use_out_degree = use_out_degree ) <EOL> _assert_allclose_sparse ( L , desired_L , atol = <NUM_LIT> ) <EOL> L , d = csgraph . laplacian ( adj , normed = normed , return_diag = True , <EOL> use_out_degree = use_out_degree ) <EOL> _assert_allclose_sparse ( L , desired_L , atol = <NUM_LIT> ) <EOL> _assert_allclose_sparse ( d , desired_d , atol = <NUM_LIT> ) <EOL> def test_asymmetric_laplacian ( ) : <EOL> A = [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ] <EOL> L = [ [ <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ - <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ] <EOL> d = [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:0> ] <EOL> _check_laplacian ( A , L , d , normed = False , use_out_degree = True ) <EOL> L = [ [ <NUM_LIT:1> , - <NUM_LIT:0.5> , <NUM_LIT:0> ] , <EOL> [ - <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ] <EOL> d = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> ] <EOL> _check_laplacian ( A , L , d , normed = True , use_out_degree = True ) <EOL> L = [ [ <NUM_LIT:4> , - <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ - <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ] <EOL> d = [ <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:0> ] <EOL> _check_laplacian ( A , L , d , normed = False , use_out_degree = False ) <EOL> L = [ [ <NUM_LIT:1> , - <NUM_LIT:0.5> , <NUM_LIT:0> ] , <EOL> [ - <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ] <EOL> d = [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> _check_laplacian ( A , L , d , normed = True , use_out_degree = False ) <EOL> def test_sparse_formats ( ) : <EOL> for fmt in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> mat = sparse . diags ( [ <NUM_LIT:1> , <NUM_LIT:1> ] , [ - <NUM_LIT:1> , <NUM_LIT:1> ] , shape = ( <NUM_LIT:4> , <NUM_LIT:4> ) , format = fmt ) <EOL> for normed in True , False : <EOL> yield _check_symmetric_graph_laplacian , mat , normed <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run_module_suite ( ) </s>
