<s> """<STR_LIT>""" <EOL> __author__ = """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> import itertools <EOL> import networkx as nx <EOL> from networkx . algorithms . centrality . flow_matrix import * <EOL> def current_flow_betweenness_centrality_subset ( G , sources , targets , <EOL> normalized = True , <EOL> weight = '<STR_LIT>' , <EOL> dtype = float , solver = '<STR_LIT>' ) : <EOL> r"""<STR_LIT>""" <EOL> from networkx . utils import reverse_cuthill_mckee_ordering <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> raise ImportError ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> try : <EOL> import scipy <EOL> except ImportError : <EOL> raise ImportError ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if G . is_directed ( ) : <EOL> raise nx . NetworkXError ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if not nx . is_connected ( G ) : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> n = G . number_of_nodes ( ) <EOL> ordering = list ( reverse_cuthill_mckee_ordering ( G ) ) <EOL> mapping = dict ( zip ( ordering , range ( n ) ) ) <EOL> H = nx . relabel_nodes ( G , mapping ) <EOL> betweenness = dict . fromkeys ( H , <NUM_LIT:0.0> ) <EOL> for row , ( s , t ) in flow_matrix_row ( H , weight = weight , dtype = dtype , <EOL> solver = solver ) : <EOL> for ss in sources : <EOL> i = mapping [ ss ] <EOL> for tt in targets : <EOL> j = mapping [ tt ] <EOL> betweenness [ s ] += <NUM_LIT:0.5> * np . abs ( row [ i ] - row [ j ] ) <EOL> betweenness [ t ] += <NUM_LIT:0.5> * np . abs ( row [ i ] - row [ j ] ) <EOL> if normalized : <EOL> nb = ( n - <NUM_LIT:1.0> ) * ( n - <NUM_LIT> ) <EOL> else : <EOL> nb = <NUM_LIT> <EOL> for v in H : <EOL> betweenness [ v ] = betweenness [ v ] / nb + <NUM_LIT:1.0> / ( <NUM_LIT:2> - n ) <EOL> return dict ( ( ordering [ k ] , v ) for k , v in betweenness . items ( ) ) <EOL> def edge_current_flow_betweenness_centrality_subset ( G , sources , targets , <EOL> normalized = True , <EOL> weight = '<STR_LIT>' , <EOL> dtype = float , solver = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> from networkx . utils import reverse_cuthill_mckee_ordering <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> raise ImportError ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> try : <EOL> import scipy <EOL> except ImportError : <EOL> raise ImportError ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if G . is_directed ( ) : <EOL> raise nx . NetworkXError ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if not nx . is_connected ( G ) : <EOL> raise nx . NetworkXError ( "<STR_LIT>" ) <EOL> n = G . number_of_nodes ( ) <EOL> ordering = list ( reverse_cuthill_mckee_ordering ( G ) ) <EOL> mapping = dict ( zip ( ordering , range ( n ) ) ) <EOL> H = nx . relabel_nodes ( G , mapping ) <EOL> betweenness = ( dict . fromkeys ( H . edges ( ) , <NUM_LIT:0.0> ) ) <EOL> if normalized : <EOL> nb = ( n - <NUM_LIT:1.0> ) * ( n - <NUM_LIT> ) <EOL> else : <EOL> nb = <NUM_LIT> <EOL> for row , ( e ) in flow_matrix_row ( H , weight = weight , dtype = dtype , <EOL> solver = solver ) : <EOL> for ss in sources : <EOL> i = mapping [ ss ] <EOL> for tt in targets : <EOL> j = mapping [ tt ] <EOL> betweenness [ e ] += <NUM_LIT:0.5> * np . abs ( row [ i ] - row [ j ] ) <EOL> betweenness [ e ] /= nb <EOL> return dict ( ( ( ordering [ s ] , ordering [ t ] ) , v ) <EOL> for ( s , t ) , v in betweenness . items ( ) ) <EOL> def setup_module ( module ) : <EOL> from nose import SkipTest <EOL> try : <EOL> import numpy <EOL> import scipy <EOL> except : <EOL> raise SkipTest ( "<STR_LIT>" ) </s>
