<s> from nose . tools import * <EOL> from nose import SkipTest <EOL> import networkx <EOL> from nose . plugins . attrib import attr <EOL> from networkx import edge_current_flow_betweenness_centrality as edge_current_flow <EOL> from networkx import approximate_current_flow_betweenness_centrality as approximate_cfbc <EOL> class TestFlowBetweennessCentrality ( object ) : <EOL> numpy = <NUM_LIT:1> <EOL> @ classmethod <EOL> def setupClass ( cls ) : <EOL> global np <EOL> try : <EOL> import numpy as np <EOL> import scipy <EOL> except ImportError : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> def test_K4_normalized ( self ) : <EOL> """<STR_LIT>""" <EOL> G = networkx . complete_graph ( <NUM_LIT:4> ) <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = True ) <EOL> b_answer = { <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:1> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT> } <EOL> for n in sorted ( G ) : <EOL> assert_almost_equal ( b [ n ] , b_answer [ n ] ) <EOL> G . add_edge ( <NUM_LIT:0> , <NUM_LIT:1> , { '<STR_LIT>' : <NUM_LIT:0.5> , '<STR_LIT>' : <NUM_LIT> } ) <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = True , weight = None ) <EOL> for n in sorted ( G ) : <EOL> assert_almost_equal ( b [ n ] , b_answer [ n ] ) <EOL> wb_answer = { <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:1> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT> } <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = True ) <EOL> for n in sorted ( G ) : <EOL> assert_almost_equal ( b [ n ] , wb_answer [ n ] ) <EOL> wb_answer = { <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:1> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT> } <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = True , weight = '<STR_LIT>' ) <EOL> for n in sorted ( G ) : <EOL> assert_almost_equal ( b [ n ] , wb_answer [ n ] ) <EOL> def test_K4 ( self ) : <EOL> """<STR_LIT>""" <EOL> G = networkx . complete_graph ( <NUM_LIT:4> ) <EOL> for solver in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = False , <EOL> solver = solver ) <EOL> b_answer = { <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:1> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT> } <EOL> for n in sorted ( G ) : <EOL> assert_almost_equal ( b [ n ] , b_answer [ n ] ) <EOL> def test_P4_normalized ( self ) : <EOL> """<STR_LIT>""" <EOL> G = networkx . path_graph ( <NUM_LIT:4> ) <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = True ) <EOL> b_answer = { <NUM_LIT:0> : <NUM_LIT:0> , <NUM_LIT:1> : <NUM_LIT> / <NUM_LIT:3> , <NUM_LIT:2> : <NUM_LIT> / <NUM_LIT:3> , <NUM_LIT:3> : <NUM_LIT:0> } <EOL> for n in sorted ( G ) : <EOL> assert_almost_equal ( b [ n ] , b_answer [ n ] ) <EOL> def test_P4 ( self ) : <EOL> """<STR_LIT>""" <EOL> G = networkx . path_graph ( <NUM_LIT:4> ) <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = False ) <EOL> b_answer = { <NUM_LIT:0> : <NUM_LIT:0> , <NUM_LIT:1> : <NUM_LIT:2> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:0> } <EOL> for n in sorted ( G ) : <EOL> assert_almost_equal ( b [ n ] , b_answer [ n ] ) <EOL> def test_star ( self ) : <EOL> """<STR_LIT>""" <EOL> G = networkx . Graph ( ) <EOL> G . add_star ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' ] ) <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = True ) <EOL> b_answer = { '<STR_LIT:a>' : <NUM_LIT:1.0> , '<STR_LIT:b>' : <NUM_LIT:0.0> , '<STR_LIT:c>' : <NUM_LIT:0.0> , '<STR_LIT:d>' : <NUM_LIT:0.0> } <EOL> for n in sorted ( G ) : <EOL> assert_almost_equal ( b [ n ] , b_answer [ n ] ) <EOL> def test_solers ( self ) : <EOL> """<STR_LIT>""" <EOL> G = networkx . complete_graph ( <NUM_LIT:4> ) <EOL> for solver in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = False , <EOL> solver = solver ) <EOL> b_answer = { <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:1> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT> } <EOL> for n in sorted ( G ) : <EOL> assert_almost_equal ( b [ n ] , b_answer [ n ] ) <EOL> class TestApproximateFlowBetweennessCentrality ( object ) : <EOL> numpy = <NUM_LIT:1> <EOL> @ classmethod <EOL> def setupClass ( cls ) : <EOL> global np <EOL> global assert_allclose <EOL> try : <EOL> import numpy as np <EOL> import scipy <EOL> from numpy . testing import assert_allclose <EOL> except ImportError : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> def test_K4_normalized ( self ) : <EOL> "<STR_LIT>" <EOL> G = networkx . complete_graph ( <NUM_LIT:4> ) <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = True ) <EOL> epsilon = <NUM_LIT:0.1> <EOL> ba = approximate_cfbc ( G , normalized = True , epsilon = <NUM_LIT:0.5> * epsilon ) <EOL> for n in sorted ( G ) : <EOL> assert_allclose ( b [ n ] , ba [ n ] , atol = epsilon ) <EOL> def test_K4 ( self ) : <EOL> "<STR_LIT>" <EOL> G = networkx . complete_graph ( <NUM_LIT:4> ) <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = False ) <EOL> epsilon = <NUM_LIT:0.1> <EOL> ba = approximate_cfbc ( G , normalized = False , epsilon = <NUM_LIT:0.5> * epsilon ) <EOL> for n in sorted ( G ) : <EOL> assert_allclose ( b [ n ] , ba [ n ] , atol = epsilon * len ( G ) ** <NUM_LIT:2> ) <EOL> def test_star ( self ) : <EOL> "<STR_LIT>" <EOL> G = networkx . Graph ( ) <EOL> G . add_star ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' ] ) <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = True ) <EOL> epsilon = <NUM_LIT:0.1> <EOL> ba = approximate_cfbc ( G , normalized = True , epsilon = <NUM_LIT:0.5> * epsilon ) <EOL> for n in sorted ( G ) : <EOL> assert_allclose ( b [ n ] , ba [ n ] , atol = epsilon ) <EOL> def test_grid ( self ) : <EOL> "<STR_LIT>" <EOL> G = networkx . grid_2d_graph ( <NUM_LIT:4> , <NUM_LIT:4> ) <EOL> b = networkx . current_flow_betweenness_centrality ( G , normalized = True ) <EOL> epsilon = <NUM_LIT:0.1> <EOL> ba = approximate_cfbc ( G , normalized = True , epsilon = <NUM_LIT:0.5> * epsilon ) <EOL> for n in sorted ( G ) : <EOL> assert_allclose ( b [ n ] , ba [ n ] , atol = epsilon ) <EOL> def test_solvers ( self ) : <EOL> "<STR_LIT>" <EOL> G = networkx . complete_graph ( <NUM_LIT:4> ) <EOL> epsilon = <NUM_LIT:0.1> <EOL> for solver in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> b = approximate_cfbc ( G , normalized = False , solver = solver , <EOL> epsilon = <NUM_LIT:0.5> * epsilon ) <EOL> b_answer = { <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:1> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT> } <EOL> for n in sorted ( G ) : <EOL> assert_allclose ( b [ n ] , b_answer [ n ] , atol = epsilon ) <EOL> class TestWeightedFlowBetweennessCentrality ( object ) : <EOL> pass <EOL> class TestEdgeFlowBetweennessCentrality ( object ) : <EOL> numpy = <NUM_LIT:1> <EOL> @ classmethod <EOL> def setupClass ( cls ) : <EOL> global np <EOL> try : <EOL> import numpy as np <EOL> import scipy <EOL> except ImportError : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> def test_K4 ( self ) : <EOL> """<STR_LIT>""" <EOL> G = networkx . complete_graph ( <NUM_LIT:4> ) <EOL> b = edge_current_flow ( G , normalized = True ) <EOL> b_answer = dict . fromkeys ( G . edges ( ) , <NUM_LIT> ) <EOL> for ( s , t ) , v1 in b_answer . items ( ) : <EOL> v2 = b . get ( ( s , t ) , b . get ( ( t , s ) ) ) <EOL> assert_almost_equal ( v1 , v2 ) <EOL> def test_K4_normalized ( self ) : <EOL> """<STR_LIT>""" <EOL> G = networkx . complete_graph ( <NUM_LIT:4> ) <EOL> b = edge_current_flow ( G , normalized = False ) <EOL> b_answer = dict . fromkeys ( G . edges ( ) , <NUM_LIT> ) <EOL> for ( s , t ) , v1 in b_answer . items ( ) : <EOL> v2 = b . get ( ( s , t ) , b . get ( ( t , s ) ) ) <EOL> assert_almost_equal ( v1 , v2 ) <EOL> def test_C4 ( self ) : <EOL> """<STR_LIT>""" <EOL> G = networkx . cycle_graph ( <NUM_LIT:4> ) <EOL> b = edge_current_flow ( G , normalized = False ) <EOL> b_answer = { ( <NUM_LIT:0> , <NUM_LIT:1> ) : <NUM_LIT> , ( <NUM_LIT:0> , <NUM_LIT:3> ) : <NUM_LIT> , ( <NUM_LIT:1> , <NUM_LIT:2> ) : <NUM_LIT> , ( <NUM_LIT:2> , <NUM_LIT:3> ) : <NUM_LIT> } <EOL> for ( s , t ) , v1 in b_answer . items ( ) : <EOL> v2 = b . get ( ( s , t ) , b . get ( ( t , s ) ) ) <EOL> assert_almost_equal ( v1 , v2 ) <EOL> def test_P4 ( self ) : <EOL> """<STR_LIT>""" <EOL> G = networkx . path_graph ( <NUM_LIT:4> ) <EOL> b = edge_current_flow ( G , normalized = False ) <EOL> b_answer = { ( <NUM_LIT:0> , <NUM_LIT:1> ) : <NUM_LIT> , ( <NUM_LIT:1> , <NUM_LIT:2> ) : <NUM_LIT> , ( <NUM_LIT:2> , <NUM_LIT:3> ) : <NUM_LIT> } <EOL> for ( s , t ) , v1 in b_answer . items ( ) : <EOL> v2 = b . get ( ( s , t ) , b . get ( ( t , s ) ) ) <EOL> assert_almost_equal ( v1 , v2 ) </s>
