<s> from nose import SkipTest <EOL> from nose . tools import assert_raises , assert_true , assert_equal <EOL> import networkx as nx <EOL> from networkx . generators . classic import barbell_graph , cycle_graph , path_graph <EOL> class TestConvertNumpy ( object ) : <EOL> numpy = <NUM_LIT:1> <EOL> @ classmethod <EOL> def setupClass ( cls ) : <EOL> global np <EOL> global np_assert_equal <EOL> try : <EOL> import numpy as np <EOL> np_assert_equal = np . testing . assert_equal <EOL> except ImportError : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> self . G1 = barbell_graph ( <NUM_LIT:10> , <NUM_LIT:3> ) <EOL> self . G2 = cycle_graph ( <NUM_LIT:10> , create_using = nx . DiGraph ( ) ) <EOL> self . G3 = self . create_weighted ( nx . Graph ( ) ) <EOL> self . G4 = self . create_weighted ( nx . DiGraph ( ) ) <EOL> def create_weighted ( self , G ) : <EOL> g = cycle_graph ( <NUM_LIT:4> ) <EOL> e = g . edges ( ) <EOL> source = [ u for u , v in e ] <EOL> dest = [ v for u , v in e ] <EOL> weight = [ s + <NUM_LIT:10> for s in source ] <EOL> ex = zip ( source , dest , weight ) <EOL> G . add_weighted_edges_from ( ex ) <EOL> return G <EOL> def assert_equal ( self , G1 , G2 ) : <EOL> assert_true ( sorted ( G1 . nodes ( ) ) == sorted ( G2 . nodes ( ) ) ) <EOL> assert_true ( sorted ( G1 . edges ( ) ) == sorted ( G2 . edges ( ) ) ) <EOL> def identity_conversion ( self , G , A , create_using ) : <EOL> GG = nx . from_numpy_matrix ( A , create_using = create_using ) <EOL> self . assert_equal ( G , GG ) <EOL> GW = nx . to_networkx_graph ( A , create_using = create_using ) <EOL> self . assert_equal ( G , GW ) <EOL> GI = create_using . __class__ ( A ) <EOL> self . assert_equal ( G , GI ) <EOL> def test_shape ( self ) : <EOL> "<STR_LIT>" <EOL> A = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ] ) <EOL> assert_raises ( nx . NetworkXError , nx . from_numpy_matrix , A ) <EOL> def test_identity_graph_matrix ( self ) : <EOL> "<STR_LIT>" <EOL> A = nx . to_numpy_matrix ( self . G1 ) <EOL> self . identity_conversion ( self . G1 , A , nx . Graph ( ) ) <EOL> def test_identity_graph_array ( self ) : <EOL> "<STR_LIT>" <EOL> A = nx . to_numpy_matrix ( self . G1 ) <EOL> A = np . asarray ( A ) <EOL> self . identity_conversion ( self . G1 , A , nx . Graph ( ) ) <EOL> def test_identity_digraph_matrix ( self ) : <EOL> """<STR_LIT>""" <EOL> A = nx . to_numpy_matrix ( self . G2 ) <EOL> self . identity_conversion ( self . G2 , A , nx . DiGraph ( ) ) <EOL> def test_identity_digraph_array ( self ) : <EOL> """<STR_LIT>""" <EOL> A = nx . to_numpy_matrix ( self . G2 ) <EOL> A = np . asarray ( A ) <EOL> self . identity_conversion ( self . G2 , A , nx . DiGraph ( ) ) <EOL> def test_identity_weighted_graph_matrix ( self ) : <EOL> """<STR_LIT>""" <EOL> A = nx . to_numpy_matrix ( self . G3 ) <EOL> self . identity_conversion ( self . G3 , A , nx . Graph ( ) ) <EOL> def test_identity_weighted_graph_array ( self ) : <EOL> """<STR_LIT>""" <EOL> A = nx . to_numpy_matrix ( self . G3 ) <EOL> A = np . asarray ( A ) <EOL> self . identity_conversion ( self . G3 , A , nx . Graph ( ) ) <EOL> def test_identity_weighted_digraph_matrix ( self ) : <EOL> """<STR_LIT>""" <EOL> A = nx . to_numpy_matrix ( self . G4 ) <EOL> self . identity_conversion ( self . G4 , A , nx . DiGraph ( ) ) <EOL> def test_identity_weighted_digraph_array ( self ) : <EOL> """<STR_LIT>""" <EOL> A = nx . to_numpy_matrix ( self . G4 ) <EOL> A = np . asarray ( A ) <EOL> self . identity_conversion ( self . G4 , A , nx . DiGraph ( ) ) <EOL> def test_nodelist ( self ) : <EOL> """<STR_LIT>""" <EOL> P4 = path_graph ( <NUM_LIT:4> ) <EOL> P3 = path_graph ( <NUM_LIT:3> ) <EOL> nodelist = P3 . nodes ( ) <EOL> A = nx . to_numpy_matrix ( P4 , nodelist = nodelist ) <EOL> GA = nx . Graph ( A ) <EOL> self . assert_equal ( GA , P3 ) <EOL> nodelist += [ nodelist [ <NUM_LIT:0> ] ] <EOL> assert_raises ( nx . NetworkXError , nx . to_numpy_matrix , P3 , nodelist = nodelist ) <EOL> def test_weight_keyword ( self ) : <EOL> WP4 = nx . Graph ( ) <EOL> WP4 . add_edges_from ( ( n , n + <NUM_LIT:1> , dict ( weight = <NUM_LIT:0.5> , other = <NUM_LIT> ) ) for n in range ( <NUM_LIT:3> ) ) <EOL> P4 = path_graph ( <NUM_LIT:4> ) <EOL> A = nx . to_numpy_matrix ( P4 ) <EOL> np_assert_equal ( A , nx . to_numpy_matrix ( WP4 , weight = None ) ) <EOL> np_assert_equal ( <NUM_LIT:0.5> * A , nx . to_numpy_matrix ( WP4 ) ) <EOL> np_assert_equal ( <NUM_LIT> * A , nx . to_numpy_matrix ( WP4 , weight = '<STR_LIT>' ) ) <EOL> def test_from_numpy_matrix_type ( self ) : <EOL> A = np . matrix ( [ [ <NUM_LIT:1> ] ] ) <EOL> G = nx . from_numpy_matrix ( A ) <EOL> assert_equal ( type ( G [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) , int ) <EOL> A = np . matrix ( [ [ <NUM_LIT:1> ] ] ) . astype ( np . float ) <EOL> G = nx . from_numpy_matrix ( A ) <EOL> assert_equal ( type ( G [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) , float ) <EOL> A = np . matrix ( [ [ <NUM_LIT:1> ] ] ) . astype ( np . str ) <EOL> G = nx . from_numpy_matrix ( A ) <EOL> assert_equal ( type ( G [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) , str ) <EOL> A = np . matrix ( [ [ <NUM_LIT:1> ] ] ) . astype ( np . bool ) <EOL> G = nx . from_numpy_matrix ( A ) <EOL> assert_equal ( type ( G [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) , bool ) <EOL> A = np . matrix ( [ [ <NUM_LIT:1> ] ] ) . astype ( np . complex ) <EOL> G = nx . from_numpy_matrix ( A ) <EOL> assert_equal ( type ( G [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) , complex ) <EOL> A = np . matrix ( [ [ <NUM_LIT:1> ] ] ) . astype ( np . object ) <EOL> assert_raises ( TypeError , nx . from_numpy_matrix , A ) <EOL> def test_from_numpy_matrix_dtype ( self ) : <EOL> dt = [ ( '<STR_LIT>' , float ) , ( '<STR_LIT>' , int ) ] <EOL> A = np . matrix ( [ [ ( <NUM_LIT:1.0> , <NUM_LIT:2> ) ] ] , dtype = dt ) <EOL> G = nx . from_numpy_matrix ( A ) <EOL> assert_equal ( type ( G [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) , float ) <EOL> assert_equal ( type ( G [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) , int ) <EOL> assert_equal ( G [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> assert_equal ( G [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <NUM_LIT:1.0> ) <EOL> def test_to_numpy_recarray ( self ) : <EOL> G = nx . Graph ( ) <EOL> G . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> , weight = <NUM_LIT> , cost = <NUM_LIT:5> ) <EOL> A = nx . to_numpy_recarray ( G , dtype = [ ( '<STR_LIT>' , float ) , ( '<STR_LIT>' , int ) ] ) <EOL> assert_equal ( sorted ( A . dtype . names ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert_equal ( A . weight [ <NUM_LIT:0> , <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> assert_equal ( A . weight [ <NUM_LIT:0> , <NUM_LIT:0> ] , <NUM_LIT:0.0> ) <EOL> assert_equal ( A . cost [ <NUM_LIT:0> , <NUM_LIT:1> ] , <NUM_LIT:5> ) <EOL> assert_equal ( A . cost [ <NUM_LIT:0> , <NUM_LIT:0> ] , <NUM_LIT:0> ) <EOL> def test_numpy_multigraph ( self ) : <EOL> G = nx . MultiGraph ( ) <EOL> G . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> , weight = <NUM_LIT:7> ) <EOL> G . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> , weight = <NUM_LIT> ) <EOL> A = nx . to_numpy_matrix ( G ) <EOL> assert_equal ( A [ <NUM_LIT:1> , <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> A = nx . to_numpy_matrix ( G , multigraph_weight = min ) <EOL> assert_equal ( A [ <NUM_LIT:1> , <NUM_LIT:0> ] , <NUM_LIT:7> ) <EOL> A = nx . to_numpy_matrix ( G , multigraph_weight = max ) <EOL> assert_equal ( A [ <NUM_LIT:1> , <NUM_LIT:0> ] , <NUM_LIT> ) </s>
