<s> from nose . tools import assert_true <EOL> from nose . tools import assert_equal <EOL> from scipy . sparse import csr_matrix <EOL> from scipy . sparse import csc_matrix <EOL> from scipy . sparse import isspmatrix <EOL> import scipy . sparse as sp <EOL> import numpy as np <EOL> from numpy . testing import assert_array_almost_equal , assert_allclose <EOL> import warnings <EOL> from nose . tools import assert_raises <EOL> from nose . plugins . skip import SkipTest <EOL> from megaman . embedding . spectral_embedding import SpectralEmbedding , spectral_embedding , _graph_is_connected <EOL> import megaman . geometry . geometry as geom <EOL> from sklearn . metrics import normalized_mutual_info_score <EOL> from sklearn . datasets . samples_generator import make_blobs <EOL> centers = np . array ( [ <EOL> [ <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] , <EOL> [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] , <EOL> [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT:1.0> ] , <EOL> ] ) <EOL> n_samples = <NUM_LIT:1000> <EOL> n_clusters , n_features = centers . shape <EOL> S , true_labels = make_blobs ( n_samples = n_samples , centers = centers , <EOL> cluster_std = <NUM_LIT:1.> , random_state = <NUM_LIT> ) <EOL> def _check_with_col_sign_flipping ( A , B , tol = <NUM_LIT:0.0> ) : <EOL> """<STR_LIT>""" <EOL> sign = True <EOL> for column_idx in range ( A . shape [ <NUM_LIT:1> ] ) : <EOL> sign = sign and ( ( ( ( A [ : , column_idx ] - <EOL> B [ : , column_idx ] ) ** <NUM_LIT:2> ) . mean ( ) <= tol ** <NUM_LIT:2> ) or <EOL> ( ( ( A [ : , column_idx ] + <EOL> B [ : , column_idx ] ) ** <NUM_LIT:2> ) . mean ( ) <= tol ** <NUM_LIT:2> ) ) <EOL> if not sign : <EOL> return False <EOL> return True <EOL> def test_spectral_embedding_two_components ( seed = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> random_state = np . random . RandomState ( seed ) <EOL> n_sample = <NUM_LIT:100> <EOL> affinity = np . zeros ( shape = [ n_sample * <NUM_LIT:2> , <EOL> n_sample * <NUM_LIT:2> ] ) <EOL> affinity [ <NUM_LIT:0> : n_sample , <EOL> <NUM_LIT:0> : n_sample ] = np . abs ( random_state . randn ( n_sample , n_sample ) ) + <NUM_LIT:2> <EOL> affinity [ n_sample : : , <EOL> n_sample : : ] = np . abs ( random_state . randn ( n_sample , n_sample ) ) + <NUM_LIT:2> <EOL> affinity [ <NUM_LIT:0> , n_sample + <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> affinity [ n_sample + <NUM_LIT:1> , <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> affinity . flat [ : : <NUM_LIT:2> * n_sample + <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> affinity = <NUM_LIT:0.5> * ( affinity + affinity . T ) <EOL> true_label = np . zeros ( shape = <NUM_LIT:2> * n_sample ) <EOL> true_label [ <NUM_LIT:0> : n_sample ] = <NUM_LIT:1> <EOL> se_precomp = SpectralEmbedding ( n_components = <NUM_LIT:1> , <EOL> random_state = np . random . RandomState ( seed ) , <EOL> eigen_solver = '<STR_LIT>' ) <EOL> embedded_coordinate = se_precomp . fit_transform ( affinity , <EOL> input_type = '<STR_LIT>' ) <EOL> label_ = np . array ( embedded_coordinate . ravel ( ) < <NUM_LIT:0> , dtype = "<STR_LIT:float>" ) <EOL> assert_equal ( normalized_mutual_info_score ( true_label , label_ ) , <NUM_LIT:1.0> ) <EOL> def test_spectral_embedding_precomputed_affinity ( seed = <NUM_LIT> , almost_equal_decimals = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> radius = <NUM_LIT> <EOL> se_precomp = SpectralEmbedding ( n_components = <NUM_LIT:2> , <EOL> random_state = np . random . RandomState ( seed ) ) <EOL> geom_params = { '<STR_LIT>' : { '<STR_LIT>' : radius } , '<STR_LIT>' : { '<STR_LIT>' : radius } , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> se_rbf = SpectralEmbedding ( n_components = <NUM_LIT:2> , random_state = np . random . RandomState ( seed ) , <EOL> geom = geom_params ) <EOL> G = geom . Geometry ( adjacency_method = '<STR_LIT>' , adjacency_kwds = { '<STR_LIT>' : radius } , <EOL> affinity_kwds = { '<STR_LIT>' : radius } ) <EOL> G . set_data_matrix ( S ) <EOL> A = G . compute_affinity_matrix ( ) <EOL> embed_precomp = se_precomp . fit_transform ( A , input_type = '<STR_LIT>' ) <EOL> embed_rbf = se_rbf . fit_transform ( S , input_type = '<STR_LIT:data>' ) <EOL> assert_array_almost_equal ( <EOL> se_precomp . affinity_matrix_ . todense ( ) , se_rbf . affinity_matrix_ . todense ( ) , <EOL> almost_equal_decimals ) <EOL> assert_true ( _check_with_col_sign_flipping ( embed_precomp , embed_rbf , <NUM_LIT> ) ) <EOL> def test_spectral_embedding_amg_solver ( seed = <NUM_LIT:20> ) : <EOL> """<STR_LIT>""" <EOL> radius = <NUM_LIT> <EOL> geom_params = { '<STR_LIT>' : { '<STR_LIT>' : radius } , '<STR_LIT>' : { '<STR_LIT>' : radius } , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> try : <EOL> import pyamg <EOL> except ImportError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> se_amg = SpectralEmbedding ( n_components = <NUM_LIT:2> , eigen_solver = "<STR_LIT>" , <EOL> random_state = np . random . RandomState ( seed ) , geom = geom_params ) <EOL> se_arpack = SpectralEmbedding ( n_components = <NUM_LIT:2> , eigen_solver = "<STR_LIT>" , geom = geom_params , <EOL> random_state = np . random . RandomState ( seed ) ) <EOL> embed_amg = se_amg . fit_transform ( S ) <EOL> embed_arpack = se_arpack . fit_transform ( S ) <EOL> assert_true ( _check_with_col_sign_flipping ( embed_amg , embed_arpack , <NUM_LIT> ) ) <EOL> def test_spectral_embedding_symmetrzation ( seed = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> radius = <NUM_LIT> <EOL> geom_params = { '<STR_LIT>' : { '<STR_LIT>' : radius } , '<STR_LIT>' : { '<STR_LIT>' : radius } , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> try : <EOL> import pyamg <EOL> except ImportError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> se_amg = SpectralEmbedding ( n_components = <NUM_LIT:2> , eigen_solver = "<STR_LIT>" , <EOL> random_state = np . random . RandomState ( seed ) , geom = geom_params ) <EOL> se_arpack = SpectralEmbedding ( n_components = <NUM_LIT:2> , eigen_solver = "<STR_LIT>" , geom = geom_params , <EOL> random_state = np . random . RandomState ( seed ) ) <EOL> embed_amg = se_amg . fit_transform ( S ) <EOL> embed_arpack = se_arpack . fit_transform ( S ) <EOL> assert_true ( _check_with_col_sign_flipping ( embed_amg , embed_arpack , <NUM_LIT> ) ) <EOL> def test_spectral_embedding_unknown_eigensolver ( seed = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> se = SpectralEmbedding ( n_components = <NUM_LIT:1> , <EOL> random_state = np . random . RandomState ( seed ) , <EOL> eigen_solver = "<STR_LIT>" ) <EOL> assert_raises ( ValueError , se . fit , S ) <EOL> def test_connectivity ( seed = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> graph = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> assert_equal ( _graph_is_connected ( graph ) , False ) <EOL> assert_equal ( _graph_is_connected ( csr_matrix ( graph ) ) , False ) <EOL> assert_equal ( _graph_is_connected ( csc_matrix ( graph ) ) , False ) <EOL> graph = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> assert_equal ( _graph_is_connected ( graph ) , True ) <EOL> assert_equal ( _graph_is_connected ( csr_matrix ( graph ) ) , True ) <EOL> assert_equal ( _graph_is_connected ( csc_matrix ( graph ) ) , True ) </s>
