<s> import unittest <EOL> from zen import * <EOL> import random <EOL> import time <EOL> import gc <EOL> import sys <EOL> use_networkx = False <EOL> try : <EOL> import networkx <EOL> use_networkx = True <EOL> except : <EOL> pass <EOL> def timer ( fxn , args ) : <EOL> gc . disable ( ) <EOL> t1 = time . time ( ) <EOL> R = fxn ( * args ) <EOL> t2 = time . time ( ) <EOL> gc . enable ( ) <EOL> return R , ( t2 - t1 ) <EOL> class TestEigenvectorCentrality ( unittest . TestCase ) : <EOL> pass <EOL> class TestBetweennessCentrality ( unittest . TestCase ) : <EOL> def test_random_dir_weights ( self ) : <EOL> if not use_networkx : <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> return <EOL> Gnx = networkx . DiGraph ( ) <EOL> G = DiGraph ( ) <EOL> nodes = range ( <NUM_LIT> ) <EOL> random . shuffle ( nodes ) <EOL> while len ( nodes ) > <NUM_LIT:0> : <EOL> n1 = nodes . pop ( ) <EOL> n2 = nodes . pop ( ) <EOL> w = random . randint ( <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> eidx = G . add_edge ( n1 , n2 ) <EOL> G . set_weight_ ( eidx , w ) <EOL> Gnx . add_edge ( n1 , n2 , weight = w ) <EOL> nodes = range ( <NUM_LIT> ) <EOL> random . shuffle ( nodes ) <EOL> while len ( nodes ) > <NUM_LIT:0> : <EOL> n1 = nodes . pop ( ) <EOL> n2 = nodes . pop ( ) <EOL> if not G . has_edge ( n1 , n2 ) : <EOL> w = random . randint ( <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> eidx = G . add_edge ( n1 , n2 ) <EOL> G . set_weight_ ( eidx , w ) <EOL> Gnx . add_edge ( n1 , n2 , weight = w ) <EOL> R = betweenness_centrality_ ( G , True , True ) <EOL> Rnx = networkx . betweenness_centrality ( Gnx , normalized = True , weight = '<STR_LIT>' ) <EOL> for n in G . nodes_iter ( ) : <EOL> self . assertAlmostEqual ( Rnx [ n ] , R [ G . node_idx ( n ) ] ) <EOL> def test_random_dir_noweights ( self ) : <EOL> if not use_networkx : <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> return <EOL> Gnx = networkx . DiGraph ( ) <EOL> G = DiGraph ( ) <EOL> nodes = range ( <NUM_LIT> ) <EOL> random . shuffle ( nodes ) <EOL> while len ( nodes ) > <NUM_LIT:0> : <EOL> n1 = nodes . pop ( ) <EOL> n2 = nodes . pop ( ) <EOL> w = random . randint ( <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> eidx = G . add_edge ( n1 , n2 ) <EOL> G . set_weight_ ( eidx , w ) <EOL> Gnx . add_edge ( n1 , n2 , weight = w ) <EOL> nodes = range ( <NUM_LIT> ) <EOL> random . shuffle ( nodes ) <EOL> while len ( nodes ) > <NUM_LIT:0> : <EOL> n1 = nodes . pop ( ) <EOL> n2 = nodes . pop ( ) <EOL> if not G . has_edge ( n1 , n2 ) : <EOL> w = random . randint ( <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> eidx = G . add_edge ( n1 , n2 ) <EOL> G . set_weight_ ( eidx , w ) <EOL> Gnx . add_edge ( n1 , n2 , weight = w ) <EOL> R = betweenness_centrality_ ( G , True , False ) <EOL> Rnx = networkx . betweenness_centrality ( Gnx , normalized = True , weight = None ) <EOL> for n in G . nodes_iter ( ) : <EOL> self . assertAlmostEqual ( Rnx [ n ] , R [ G . node_idx ( n ) ] ) <EOL> def test_random_udir_weights ( self ) : <EOL> if not use_networkx : <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> return <EOL> Gnx = networkx . Graph ( ) <EOL> G = Graph ( ) <EOL> nodes = range ( <NUM_LIT> ) <EOL> random . shuffle ( nodes ) <EOL> while len ( nodes ) > <NUM_LIT:0> : <EOL> n1 = nodes . pop ( ) <EOL> n2 = nodes . pop ( ) <EOL> w = random . randint ( <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> eidx = G . add_edge ( n1 , n2 ) <EOL> G . set_weight_ ( eidx , w ) <EOL> Gnx . add_edge ( n1 , n2 , weight = w ) <EOL> nodes = range ( <NUM_LIT> ) <EOL> random . shuffle ( nodes ) <EOL> while len ( nodes ) > <NUM_LIT:0> : <EOL> n1 = nodes . pop ( ) <EOL> n2 = nodes . pop ( ) <EOL> if not G . has_edge ( n1 , n2 ) : <EOL> w = random . randint ( <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> eidx = G . add_edge ( n1 , n2 ) <EOL> G . set_weight_ ( eidx , w ) <EOL> Gnx . add_edge ( n1 , n2 , weight = w ) <EOL> R = betweenness_centrality_ ( G , True , True ) <EOL> Rnx = networkx . betweenness_centrality ( Gnx , normalized = True , weight = '<STR_LIT>' ) <EOL> for n in G . nodes_iter ( ) : <EOL> self . assertAlmostEqual ( Rnx [ n ] , R [ G . node_idx ( n ) ] ) <EOL> def test_random_udir_noweights ( self ) : <EOL> if not use_networkx : <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> return <EOL> Gnx = networkx . Graph ( ) <EOL> G = Graph ( ) <EOL> nodes = range ( <NUM_LIT> ) <EOL> random . shuffle ( nodes ) <EOL> while len ( nodes ) > <NUM_LIT:0> : <EOL> n1 = nodes . pop ( ) <EOL> n2 = nodes . pop ( ) <EOL> w = random . randint ( <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> eidx = G . add_edge ( n1 , n2 ) <EOL> G . set_weight_ ( eidx , w ) <EOL> Gnx . add_edge ( n1 , n2 , weight = w ) <EOL> nodes = range ( <NUM_LIT> ) <EOL> random . shuffle ( nodes ) <EOL> while len ( nodes ) > <NUM_LIT:0> : <EOL> n1 = nodes . pop ( ) <EOL> n2 = nodes . pop ( ) <EOL> if not G . has_edge ( n1 , n2 ) : <EOL> w = random . randint ( <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> eidx = G . add_edge ( n1 , n2 ) <EOL> G . set_weight_ ( eidx , w ) <EOL> Gnx . add_edge ( n1 , n2 , weight = w ) <EOL> R = betweenness_centrality_ ( G , True , False ) <EOL> Rnx = networkx . betweenness_centrality ( Gnx , normalized = True , weight = None ) <EOL> for n in G . nodes_iter ( ) : <EOL> self . assertAlmostEqual ( Rnx [ n ] , R [ G . node_idx ( n ) ] ) <EOL> def test_brandes_betweenness_ud ( self ) : <EOL> G = Graph ( ) <EOL> G . set_weight_ ( G . add_edge ( <NUM_LIT:0> , <NUM_LIT:1> ) , <NUM_LIT:3> ) <EOL> G . set_weight_ ( G . add_edge ( <NUM_LIT:0> , <NUM_LIT:2> ) , <NUM_LIT:2> ) <EOL> G . set_weight_ ( G . add_edge ( <NUM_LIT:0> , <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> G . set_weight_ ( G . add_edge ( <NUM_LIT:0> , <NUM_LIT:4> ) , <NUM_LIT:4> ) <EOL> G . set_weight_ ( G . add_edge ( <NUM_LIT:1> , <NUM_LIT:3> ) , <NUM_LIT:5> ) <EOL> G . set_weight_ ( G . add_edge ( <NUM_LIT:1> , <NUM_LIT:5> ) , <NUM_LIT:5> ) <EOL> G . set_weight_ ( G . add_edge ( <NUM_LIT:2> , <NUM_LIT:4> ) , <NUM_LIT:1> ) <EOL> G . set_weight_ ( G . add_edge ( <NUM_LIT:3> , <NUM_LIT:4> ) , <NUM_LIT:2> ) <EOL> G . set_weight_ ( G . add_edge ( <NUM_LIT:3> , <NUM_LIT:5> ) , <NUM_LIT:1> ) <EOL> G . set_weight_ ( G . add_edge ( <NUM_LIT:4> , <NUM_LIT:5> ) , <NUM_LIT:4> ) <EOL> self . G = G <EOL> self . exact_weighted = { G . node_idx ( <NUM_LIT:0> ) : <NUM_LIT> , G . node_idx ( <NUM_LIT:1> ) : <NUM_LIT:0.0> , G . node_idx ( <NUM_LIT:2> ) : <NUM_LIT> , G . node_idx ( <NUM_LIT:3> ) : <NUM_LIT> , G . node_idx ( <NUM_LIT:4> ) : <NUM_LIT> , G . node_idx ( <NUM_LIT:5> ) : <NUM_LIT:0.0> } <EOL> b = betweenness_centrality_ ( self . G , weighted = True , <EOL> normalized = False ) <EOL> for n in sorted ( self . G . nodes_ ( ) ) : <EOL> self . assertEqual ( b [ n ] , self . exact_weighted [ n ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
