def test_K4_normalized(self):
     """Betweenness centrality: K4"""
     G = nx.complete_graph(4)
     b = nx.current_flow_betweenness_centrality(G, normalized=True)
     b_answer = {0: 0.25, 1: 0.25, 2: 0.25, 3: 0.25}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
     G.add_edge(0, 1, weight=0.5, other=0.3)
     b = nx.current_flow_betweenness_centrality(G,
                                                normalized=True,
                                                weight=None)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
     wb_answer = {0: 0.2222222, 1: 0.2222222, 2: 0.30555555, 3: 0.30555555}
     b = nx.current_flow_betweenness_centrality(G,
                                                normalized=True,
                                                weight="weight")
     for n in sorted(G):
         assert almost_equal(b[n], wb_answer[n])
     wb_answer = {0: 0.2051282, 1: 0.2051282, 2: 0.33974358, 3: 0.33974358}
     b = nx.current_flow_betweenness_centrality(G,
                                                normalized=True,
                                                weight="other")
     for n in sorted(G):
         assert almost_equal(b[n], wb_answer[n])
Example #2
0
 def test_K4(self):
     """Betweenness centrality: K4"""
     G=networkx.complete_graph(4)
     b=networkx.current_flow_betweenness_centrality_subset(G,
                                                           list(G),
                                                           list(G),
                                                           normalized=True)
     b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True)
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
     # test weighted network
     G.add_edge(0,1,{'weight':0.5,'other':0.3})
     b=networkx.current_flow_betweenness_centrality_subset(G,
                                                           list(G),
                                                           list(G),
                                                           normalized=True,
                                                           weight=None)
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
     b=networkx.current_flow_betweenness_centrality_subset(G,
                                                           list(G),
                                                           list(G),
                                                           normalized=True)
     b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True)
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
     b=networkx.current_flow_betweenness_centrality_subset(G,
                                                           list(G),
                                                           list(G),
                                                           normalized=True,
                                                           weight='other')
     b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True,weight='other')
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
Example #3
0
 def test_P4(self):
     """Betweenness centrality: P4"""
     G = nx.path_graph(4)
     b = nx.current_flow_betweenness_centrality(G, normalized=False)
     b_answer = {0: 0, 1: 2, 2: 2, 3: 0}
     for n in sorted(G):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
Example #4
0
 def test_P4_normalized(self):
     """Betweenness centrality: P4 normalized"""
     G = networkx.path_graph(4)
     b = networkx.current_flow_betweenness_centrality(G, normalized=True)
     b_answer = {0: 0, 1: 2. / 3, 2: 2. / 3, 3: 0}
     for n in sorted(G):
         assert_almost_equal(b[n], b_answer[n])
def agregar_centralidad(G, criterio='degree', max_iter=100, tol=1e-3):
    if criterio == 'degree':
        nodes = list(G.nodes())
        degree = list(dict(nx.degree(G)).values())
        return nodes, degree
    if criterio == 'eigen':
        nodes = list(G.nodes())
        eigen = list(
            dict(nx.eigenvector_centrality(G, max_iter=max_iter,
                                           tol=tol)).values())
        return nodes, eigen
    if criterio == 'sub':
        nodes = list(G.nodes())
        sub = list(dict(nx.subgraph_centrality(G)).values())
        return nodes, sub
    if criterio == 'bet':
        nodes = list(G.nodes())
        bet = list(dict(nx.betweenness_centrality(G)).values())
        return nodes, bet
    if criterio == 'flow':
        nodes = list(G.nodes())
        flow = list(dict(nx.current_flow_betweenness_centrality(G)).values())
        return nodes, flow
    if criterio == 'random':
        nodes = list(G.nodes())
        value = random.choice(nodes)
        return value, nodes
 def test_K4_normalized(self):
     """Betweenness centrality: K4"""
     G = networkx.complete_graph(4)
     b = networkx.current_flow_betweenness_centrality_subset(G, G.nodes(), G.nodes(), normalized=True)
     b_answer = networkx.current_flow_betweenness_centrality(G, normalized=True)
     for n in sorted(G):
         assert_almost_equal(b[n], b_answer[n])
Example #7
0
def compute_centrality(star_dict, edge_dict):
    
    #build up a nx graph
    galaxy = networkx.Graph()
    for v, vertex in star_dict.iteritems():
        galaxy.add_node(v)
    
    for v, neighbors in edge_dict.iteritems():
        for n in neighbors:
            galaxy.add_edge(v,n)
            
    print "betweenness"
    betweenness_map = networkx.current_flow_betweenness_centrality(galaxy)
    betweenness_map = normalize(betweenness_map)
    
    for key, value in betweenness_map.iteritems():
        star_dict[key]['betweenness'] = value
        
    print "closeness"
    closeness_map = networkx.current_flow_closeness_centrality(galaxy)
    closeness_map = normalize(closeness_map)
    
    for key, value in closeness_map.iteritems():
        star_dict[key]['closeness'] = value
        

    print "pagerank"
    pagerank_map = networkx.pagerank_scipy(galaxy)
    pagerank_map = normalize(pagerank_map)
    
    for key, value in pagerank_map.iteritems():
        star_dict[key]['pagerank'] = value
 def test_P4(self):
     """Betweenness centrality: P4"""
     G=nx.path_graph(4)
     b=nx.current_flow_betweenness_centrality(G,normalized=False)
     b_answer={0: 0, 1: 2, 2: 2, 3: 0}
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
 def test_K4(self):
     """Betweenness centrality: K4"""
     G=networkx.complete_graph(4)
     b=networkx.current_flow_betweenness_centrality(G,normalized=False)
     b_answer={0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75}
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
Example #10
0
 def test_K4(self):
     """Betweenness centrality: K4"""
     G = networkx.complete_graph(4)
     b = networkx.current_flow_betweenness_centrality(G, normalized=False)
     b_answer = {0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75}
     for n in sorted(G):
         assert_almost_equal(b[n], b_answer[n])
 def test_P4_normalized(self):
     """Betweenness centrality: P4 normalized"""
     G=networkx.path_graph(4)
     b=networkx.current_flow_betweenness_centrality(G,normalized=True)
     b_answer={0: 0, 1: 2./3, 2: 2./3, 3:0}
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
 def test_P4(self):
     """Betweenness centrality: P4"""
     G = nx.path_graph(4)
     b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True)
     b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
     for n in sorted(G):
         assert_almost_equal(b[n], b_answer[n])
def centrality(net):
    values ={}
    close = nx.closeness_centrality(net, normalized= True)
    eigen = nx.eigenvector_centrality_numpy(net)
    page = nx.pagerank(net)
    bet = nx.betweenness_centrality(net,normalized= True)
    flow_c = nx.current_flow_closeness_centrality(net,normalized= True)
    flow_b = nx.current_flow_betweenness_centrality(net,normalized= True)
    load = nx.load_centrality(net, normalized = True)
    com_c = nx.communicability_centrality(net)
    com_b = nx.communicability_betweenness_centrality(net, normalized= True)
    degree = net.degree()
    
    file3 = open("bl.csv",'w')
    for xt in [bet,load,degree,page,flow_b,com_c,com_b,eigen,close,flow_c]:#[impo,bet,flow_b,load,com_c,com_b] :
        for yt in [bet,load,degree,page,flow_b,com_c,com_b,eigen,close,flow_c]:#[impo,bet,flow_b,load,com_c,com_b] :
            corr(xt.values(),yt.values(),file3)
        print
        file3.write("\n")
    file3.close()
    #plt.plot(x,y, 'o')
    #plt.plot(x, m*x + c, 'r', label='Fitted line')
    #plt.show()
    #for key,item in close.iteritems() :
        #values[key] = [impo.get(key),bet.get(key),flow_b.get(key), load.get(key),com_c.get(key),com_b.get(key)]
        
    return values
def centrality(net):
    values = {}
    close = nx.closeness_centrality(net, normalized=True)
    eigen = nx.eigenvector_centrality_numpy(net)
    page = nx.pagerank(net)
    bet = nx.betweenness_centrality(net, normalized=True)
    flow_c = nx.current_flow_closeness_centrality(net, normalized=True)
    flow_b = nx.current_flow_betweenness_centrality(net, normalized=True)
    load = nx.load_centrality(net, normalized=True)
    com_c = nx.communicability_centrality(net)
    com_b = nx.communicability_betweenness_centrality(net, normalized=True)
    degree = net.degree()

    file3 = open("bl.csv", 'w')
    for xt in [
            bet, load, degree, page, flow_b, com_c, com_b, eigen, close, flow_c
    ]:  #[impo,bet,flow_b,load,com_c,com_b] :
        for yt in [
                bet, load, degree, page, flow_b, com_c, com_b, eigen, close,
                flow_c
        ]:  #[impo,bet,flow_b,load,com_c,com_b] :
            corr(xt.values(), yt.values(), file3)
        print
        file3.write("\n")
    file3.close()
    #plt.plot(x,y, 'o')
    #plt.plot(x, m*x + c, 'r', label='Fitted line')
    #plt.show()
    #for key,item in close.iteritems() :
    #values[key] = [impo.get(key),bet.get(key),flow_b.get(key), load.get(key),com_c.get(key),com_b.get(key)]

    return values
Example #15
0
def centrality(G, measure, nodes_gdf, weight, normalized = False):
    """"
    The function computes several node centrality measures.
      
    Parameters
    ----------
    G: Networkx graph
    measure: string
    nodes_gdf: Point GeoDataFrame
        nodes (junctions) GeoDataFrame
    weight: string
        edges weight
    normalized: boolean
    
    Returns
    -------
    dictionary
    """     
    
    if measure == "betweenness_centrality": 
        c = nx.betweenness_centrality(G, weight = weight, normalized=normalized)
    elif measure == "straightness_centrality": 
        c = straightness_centrality(G, weight = weight, normalized=normalized)
    elif measure == "closeness_centrality": 
        c = nx.closeness_centrality(G, weight = weight, normalized=normalized)
    elif measure == "information_centrality": 
        c = nx.current_flow_betweenness_centrality(G, weight = weight, solver ="lu", normalized=normalized) 
    raise nameError("The name provided is not a valid centrality name associated with a function")
    
    return c
def describe(G, ny_tri, chems):
	global describeNetwork
	'''
	Describe the network: degrees, clustering, and centrality measures
	'''	
	# Degree
	# The number of connections a node has to other nodes.
	degrees= nx.degree(G)
	degrees_df = pd.DataFrame(degrees.items(), columns=['Facility', 'Degrees'])
	values = sorted(set(degrees.values())) 
	hist = [degrees.values().count(x) for x in values]
	plt.figure()
	plt.plot(values, hist,'ro-') # degree
	plt.xlabel('Degree')
	plt.ylabel('Number of nodes')
	plt.title('Degree Distribution')
	plt.savefig('output/degree_distribution.png')

	# Clustering coefficients
	# The bipartie clustering coefficient is a measure of local density of connections.
	clust_coefficients = nx.clustering(G)
	clust_coefficients_df = pd.DataFrame(clust_coefficients.items(), columns=['Facility', 'Clustering Coefficient'])
	clust_coefficients_df = clust_coefficients_df.sort('Clustering Coefficient', ascending=False)
	#print clust_coefficients_df

	# Node centrality measures
	FCG=list(nx.connected_component_subgraphs(G, copy=True))[0]
	# Current flow betweenness centrality
	# Current-flow betweenness centrality uses an electrical current model for information spreading 
	# in contrast to betweenness centrality which uses shortest paths.
	betweeness = nx.current_flow_betweenness_centrality(FCG)
	betweeness_df = pd.DataFrame(betweeness.items(), columns=['Facility', 'Betweeness'])
	betweeness_df = betweeness_df.sort('Betweeness', ascending=False)
	# Closeness centrality
	# The closeness of a node is the distance to all other nodes in the graph 
	# or in the case that the graph is not connected to all other nodes in the connected component containing that node.
	closeness = nx.closeness_centrality(FCG)
	closeness_df = pd.DataFrame(closeness.items(), columns=['Facility', 'Closeness'])
	closeness_df = closeness_df.sort('Closeness', ascending=False)
	# Eigenvector centrality
	# Eigenvector centrality computes the centrality for a node based on the centrality of its neighbors.
	# In other words, how connected a node is to other highly connected nodes.
	eigenvector = nx.eigenvector_centrality(FCG)
	eigenvector_df = pd.DataFrame(eigenvector.items(), columns=['Facility', 'Eigenvector'])
	eigenvector_df = eigenvector_df.sort('Eigenvector', ascending=False)

	# Create dataframe of facility info
	fac_info = ny_tri[['tri_facility_id','facility_name', 'primary_naics', 'parent_company_name']].drop_duplicates()
	fac_info.rename(columns={'facility_name':'Facility'}, inplace=True)

	# Merge everything
	describeNetwork = degrees_df.merge(
		clust_coefficients_df,on='Facility').merge(
		betweeness_df,on='Facility').merge(
		closeness_df, on='Facility').merge(
		eigenvector_df, on='Facility').merge(
		fac_info, on='Facility', how='left').merge(
		chems, on='Facility', how='left')
	describeNetwork = describeNetwork.sort('Degrees', ascending=False)
	describeNetwork.to_csv('output/describeNetwork.csv')
 def test_P4(self):
     """Betweenness centrality: P4"""
     G = nx.path_graph(4)
     b = nx.current_flow_betweenness_centrality(G, normalized=False)
     b_answer = {0: 0, 1: 2, 2: 2, 3: 0}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def test_star(self):
     """Betweenness centrality: star """
     G=nx.Graph()
     nx.add_star(G, ['a', 'b', 'c', 'd'])
     b=nx.current_flow_betweenness_centrality(G,normalized=True)
     b_answer={'a': 1.0, 'b': 0.0, 'c': 0.0, 'd':0.0}
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
 def test_star(self):
     """Betweenness centrality: star"""
     G = nx.Graph()
     nx.add_star(G, ["a", "b", "c", "d"])
     b = nx.current_flow_betweenness_centrality(G, normalized=True)
     b_answer = {"a": 1.0, "b": 0.0, "c": 0.0, "d": 0.0}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def test_star(self):
     """Betweenness centrality: star """
     G = nx.Graph()
     nx.add_star(G, ["a", "b", "c", "d"])
     b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True)
     b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
     for n in sorted(G):
         assert_almost_equal(b[n], b_answer[n])
 def test_K4(self):
     "Approximate current-flow betweenness centrality: K4"
     G = nx.complete_graph(4)
     b = nx.current_flow_betweenness_centrality(G, normalized=False)
     epsilon = 0.1
     ba = approximate_cfbc(G, normalized=False, epsilon=0.5 * epsilon)
     for n in sorted(G):
         np.testing.assert_allclose(b[n], ba[n], atol=epsilon * len(G)**2)
Example #22
0
 def test_star(self):
     """Betweenness centrality: star"""
     G = nx.Graph()
     nx.add_star(G, ["a", "b", "c", "d"])
     b = nx.current_flow_betweenness_centrality(G, normalized=True)
     b_answer = {"a": 1.0, "b": 0.0, "c": 0.0, "d": 0.0}
     for n in sorted(G):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
 def test_K4_normalized(self):
     "Approximate current-flow betweenness centrality: K4 normalized"
     G=networkx.complete_graph(4)
     b=networkx.current_flow_betweenness_centrality(G,normalized=True)
     epsilon=0.1
     ba = approximate_cfbc(G,normalized=True, epsilon=epsilon)
     for n in sorted(G):
         assert_allclose(b[n],ba[n],atol=epsilon)
Example #24
0
 def test_star(self):
     """Betweenness centrality: star """
     G = networkx.Graph()
     G.add_star(['a', 'b', 'c', 'd'])
     b = networkx.current_flow_betweenness_centrality(G, normalized=True)
     b_answer = {'a': 1.0, 'b': 0.0, 'c': 0.0, 'd': 0.0}
     for n in sorted(G):
         assert_almost_equal(b[n], b_answer[n])
 def test_grid(self):
     "Approximate current-flow betweenness centrality: 2d grid"
     G=nx.grid_2d_graph(4,4)
     b=nx.current_flow_betweenness_centrality(G,normalized=True)
     epsilon=0.1
     ba = approximate_cfbc(G,normalized=True, epsilon=0.5*epsilon)
     for n in sorted(G):
         assert_allclose(b[n],ba[n],atol=epsilon)
 def test_K4(self):
     "Approximate current-flow betweenness centrality: K4"
     G=nx.complete_graph(4)
     b=nx.current_flow_betweenness_centrality(G,normalized=False)
     epsilon=0.1
     ba = approximate_cfbc(G,normalized=False, epsilon=0.5*epsilon)
     for n in sorted(G):
         assert_allclose(b[n],ba[n],atol=epsilon*len(G)**2)
Example #27
0
def current_flow_betweenness(G):
    """Current-flow betweenness centrality"""
    G = G.to_undirected()
    G = invert_edge_weights(G)
    if nx.is_connected(G):
        return nx.current_flow_betweenness_centrality(G)
    else:
        return _aggregate_for_components(G, nx.current_flow_betweenness_centrality)
Example #28
0
 def test_K4_normalized(self):
     "Approximate current-flow betweenness centrality: K4 normalized"
     G = networkx.complete_graph(4)
     b = networkx.current_flow_betweenness_centrality(G, normalized=True)
     epsilon = 0.1
     ba = approximate_cfbc(G, normalized=True, epsilon=0.5 * epsilon)
     for n in sorted(G):
         assert_allclose(b[n], ba[n], atol=epsilon)
 def test_grid(self):
     "Approximate current-flow betweenness centrality: 2d grid"
     G = nx.grid_2d_graph(4, 4)
     b = nx.current_flow_betweenness_centrality(G, normalized=True)
     epsilon = 0.1
     ba = approximate_cfbc(G, normalized=True, epsilon=0.5 * epsilon)
     for n in sorted(G):
         np.testing.assert_allclose(b[n], ba[n], atol=epsilon)
 def test_star(self):
     "Approximate current-flow betweenness centrality: star"
     G=nx.Graph()
     nx.add_star(G, ['a', 'b', 'c', 'd'])
     b=nx.current_flow_betweenness_centrality(G,normalized=True)
     epsilon=0.1
     ba = approximate_cfbc(G,normalized=True, epsilon=0.5*epsilon)
     for n in sorted(G):
         assert_allclose(b[n],ba[n],atol=epsilon)
 def test_solers(self):
     """Betweenness centrality: alternate solvers"""
     G=nx.complete_graph(4)
     for solver in ['full','lu','cg']:
         b=nx.current_flow_betweenness_centrality(G,normalized=False,
                                                        solver=solver)
         b_answer={0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75}
         for n in sorted(G):
             assert_almost_equal(b[n],b_answer[n])
 def test_star(self):
     "Approximate current-flow betweenness centrality: star"
     G = nx.Graph()
     nx.add_star(G, ["a", "b", "c", "d"])
     b = nx.current_flow_betweenness_centrality(G, normalized=True)
     epsilon = 0.1
     ba = approximate_cfbc(G, normalized=True, epsilon=0.5 * epsilon)
     for n in sorted(G):
         np.testing.assert_allclose(b[n], ba[n], atol=epsilon)
Example #33
0
 def test_star(self):
     "Approximate current-flow betweenness centrality: star"
     G = networkx.Graph()
     G.add_star(['a', 'b', 'c', 'd'])
     b = networkx.current_flow_betweenness_centrality(G, normalized=True)
     epsilon = 0.1
     ba = approximate_cfbc(G, normalized=True, epsilon=0.5 * epsilon)
     for n in sorted(G):
         assert_allclose(b[n], ba[n], atol=epsilon)
 def test_P4(self):
     """Betweenness centrality: P4"""
     G = networkx.path_graph(4)
     b = networkx.current_flow_betweenness_centrality_subset(
         G, G.nodes(), G.nodes(), normalized=True)
     b_answer = networkx.current_flow_betweenness_centrality(
         G, normalized=True)
     for n in sorted(G):
         assert_almost_equal(b[n], b_answer[n])
Example #35
0
def current_flow_betweenness(G):
    """Current-flow betweenness centrality"""
    G = G.to_undirected()
    G = invert_edge_weights(G)
    if nx.is_connected(G):
        return nx.current_flow_betweenness_centrality(G)
    else:
        return _aggregate_for_components(
            G, nx.current_flow_betweenness_centrality)
 def test_solers(self):
     """Betweenness centrality: alternate solvers"""
     G = nx.complete_graph(4)
     for solver in ['full', 'lu', 'cg']:
         b = nx.current_flow_betweenness_centrality(G, normalized=False,
                                                    solver=solver)
         b_answer = {0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75}
         for n in sorted(G):
             assert_almost_equal(b[n], b_answer[n])
Example #37
0
 def test_P4(self):
     """Betweenness centrality: P4"""
     G = nx.path_graph(4)
     b = nx.current_flow_betweenness_centrality_subset(G,
                                                       list(G),
                                                       list(G),
                                                       normalized=True)
     b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
     for n in sorted(G):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
 def test_K4_normalized(self):
     """Betweenness centrality: K4"""
     G=nx.complete_graph(4)
     b=nx.current_flow_betweenness_centrality(G,normalized=True)
     b_answer={0: 0.25, 1: 0.25, 2: 0.25, 3: 0.25}
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
     G.add_edge(0,1,{'weight':0.5,'other':0.3})
     b=nx.current_flow_betweenness_centrality(G,normalized=True,weight=None)
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
     wb_answer={0: 0.2222222, 1: 0.2222222, 2: 0.30555555, 3: 0.30555555}
     b=nx.current_flow_betweenness_centrality(G,normalized=True)
     for n in sorted(G):
         assert_almost_equal(b[n],wb_answer[n])
     wb_answer={0: 0.2051282, 1: 0.2051282, 2: 0.33974358, 3: 0.33974358}
     b=nx.current_flow_betweenness_centrality(G,normalized=True,weight='other')
     for n in sorted(G):
         assert_almost_equal(b[n],wb_answer[n])
Example #39
0
 def test_solvers2(self):
     """Betweenness centrality: alternate solvers"""
     G = nx.complete_graph(4)
     for solver in ["full", "lu", "cg"]:
         b = nx.current_flow_betweenness_centrality(
             G, normalized=False, solver=solver
         )
         b_answer = {0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75}
         for n in sorted(G):
             assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
 def test_K4(self):
     """Betweenness centrality: K4"""
     G = nx.complete_graph(4)
     for solver in ["full", "lu", "cg"]:
         b = nx.current_flow_betweenness_centrality(G,
                                                    normalized=False,
                                                    solver=solver)
         b_answer = {0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75}
         for n in sorted(G):
             assert almost_equal(b[n], b_answer[n])
Example #41
0
 def test_P4_normalized(self):
     """Betweenness centrality: P4 normalized"""
     G = nx.path_graph(4)
     b = nx.current_flow_betweenness_centrality_subset(G,
                                                       list(G),
                                                       list(G),
                                                       normalized=True)
     b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
Example #42
0
 def test_K4_normalized(self):
     """Betweenness centrality: K4"""
     G=networkx.complete_graph(4)
     b=networkx.current_flow_betweenness_centrality_subset(G,
                                                           list(G),
                                                           list(G),
                                                           normalized=True)
     b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True)
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
Example #43
0
def network_algorithms(g, dfs):
	print("Calculating network algorithms")

	# iterate over graph components
	for i in dfs:
		metrics = []

		# find all edges of the subgraph and only keep the "offer" relationship
		id_ = i.select("id").map(lambda a: a.id)
		ids = id_.collect()

		edges_ = g.edges.rdd.filter(lambda a: a.src in ids).toDF()
		df = edges_.select("src", "dst").toPandas()
		edge_list = [tuple(x) for x in df.values]

		# generate a networkx graph
		G = nx.Graph()
		G.add_edges_from(edge_list)

		# calculate several network metrics for the graph
		metrics.append(result_to_pandas(dict(nx.degree(G)), "degree"))

		metrics.append(result_to_pandas(nx.closeness_centrality(G), "closeness_centrality"))

		metrics.append(result_to_pandas(nx.betweenness_centrality(G), "betweenness_centrality"))

		metrics.append(result_to_pandas(nx.current_flow_closeness_centrality(G), "current_flow_closeness_centrality"))

		metrics.append(result_to_pandas(nx.current_flow_betweenness_centrality(G), "current_flow_betweenness_centrality"))

		metrics.append(result_to_pandas(nx.katz_centrality_numpy(G), "katz_centrality"))

		metrics.append(result_to_pandas(nx.load_centrality(G), "load_centrality"))

		metrics.append(result_to_pandas(nx.pagerank(G), "pagerank"))

		# TypeError: Cannot use scipy.linalg.eig for sparse A with k >= N - 1. Use scipy.linalg.eig(A.toarray()) or reduce k.
		# metrics.append(result_to_pandas(nx.eigenvector_centrality_numpy(G), "eigenvector_centrality"))

		# join network metrics into one graph
		res = pd.concat(metrics, axis=1, sort=False)
		res = res.reset_index(drop=False)
		res.rename(columns={"index": "id"}, inplace=True)
		print(res)

		# convert the result into spark dataframe
		spark_df = sqlContext.createDataFrame(res)

		# create or add to big dataframe that contains all components
		try:
			out = out.unionAll(spark_df)
		except NameError:
			out = spark_df

	return out
 def f30(self):
     return "ND"
     start = 0
     try:
         c_vals = nx.current_flow_betweenness_centrality(self.G).values()
         res = sum(c_vals)
     except nx.NetworkXError:
         res = "ND"
     stop = 0
     # self.feature_time.append(stop - start)
     return res
 def test_star(self):
     """Betweenness centrality: star """
     G=networkx.Graph()
     G.add_star(['a','b','c','d'])
     b=networkx.current_flow_betweenness_centrality_subset(G,
                                                           G.nodes(),
                                                           G.nodes(),
                                                           normalized=True)
     b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True)
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
 def test_star(self):
     """Betweenness centrality: star """
     G = nx.Graph()
     nx.add_star(G, ['a', 'b', 'c', 'd'])
     b = nx.current_flow_betweenness_centrality_subset(G,
                                                       list(G),
                                                       list(G),
                                                       normalized=True)
     b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def f30(self):
     return "ND"
     start = 0
     try:
         c_vals = nx.current_flow_betweenness_centrality(self.G).values()
         res = sum(c_vals)
     except nx.NetworkXError:
         res = "ND"
     stop = 0
     # self.feature_time.append(stop - start)
     return res
Example #48
0
def DegreeOfInterestMIPs(mip, user, obj, alpha=0.3, beta=0.7):
    #compute apriori importance of node obj (considers effective conductance)
    current_flow_betweeness = nx.current_flow_betweenness_centrality(mip, True, 'weight');
    api_obj = current_flow_betweeness[obj]  #node centrality
#    print 'obj'
#    print obj
#    print 'api_obj'
#    print api_obj
    #compute proximity between user node and object node using Cycle-Free-Edge-Conductance from Koren et al. 2007
    cfec_user_obj = CFEC(user,obj,mip)
#    print 'cfec_user_obj'
#    print cfec_user_obj
    return alpha*api_obj+beta*cfec_user_obj
Example #49
0
 def DegreeOfInterestMIPs(self, user, obj, alpha=0.3, beta=0.7):
     #compute apriori importance of node obj (considers effective conductance)
     current_flow_betweeness = nx.current_flow_betweenness_centrality(True, 'weight');
     api_obj = current_flow_betweeness[obj]  #node centrality
 #    print 'obj'
 #    print obj
 #    print 'api_obj'
 #    print api_obj
     #compute proximity between user node and object node using Cycle-Free-Edge-Conductance from Koren et al. 2007
     cfec_user_obj = self.CFEC(user,obj)
 #    print 'cfec_user_obj'
 #    print cfec_user_obj
     return alpha*api_obj+beta*cfec_user_obj #TODO: check that scales work out for centrality and proximity, otherwise need some normalization
Example #50
0
def get_flow_betweenness(graph_data):
    graph = graph_data['net']
    w_label = graph_data['w_label']

    # sem peso e sem direção
    graph_simply = dir2undir(graph, w_label).simplify(
        multiple=True, loops=False, combine_edges=graph_data['simply_method2'])

    undir_unweig = networkx.current_flow_betweenness_centrality(
        graph_simply.to_networkx(), weight=None)

    # com peso
    weig = networkx.current_flow_betweenness_centrality(
        graph_simply.to_networkx(), weight=w_label)

    # com direção
    flow_in = networkx.current_flow_betweenness_centrality(graph.to_networkx(),
                                                           weight=None)

    # com peso e com direção
    flow_in_w = networkx.current_flow_betweenness_centrality(
        graph.to_networkx(), weight=w_label)

    return undir_unweig, weig, flow_in, flow_in_w
Example #51
0
 def test_K4(self):
     """Betweenness centrality: K4"""
     G = nx.complete_graph(4)
     b = nx.current_flow_betweenness_centrality_subset(G,
                                                       list(G),
                                                       list(G),
                                                       normalized=True)
     b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
     for n in sorted(G):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
     # test weighted network
     G.add_edge(0, 1, weight=0.5, other=0.3)
     b = nx.current_flow_betweenness_centrality_subset(G,
                                                       list(G),
                                                       list(G),
                                                       normalized=True,
                                                       weight=None)
     for n in sorted(G):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
     b = nx.current_flow_betweenness_centrality_subset(G,
                                                       list(G),
                                                       list(G),
                                                       normalized=True)
     b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
     for n in sorted(G):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
     b = nx.current_flow_betweenness_centrality_subset(G,
                                                       list(G),
                                                       list(G),
                                                       normalized=True,
                                                       weight="other")
     b_answer = nx.current_flow_betweenness_centrality(G,
                                                       normalized=True,
                                                       weight="other")
     for n in sorted(G):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def calculate_centrality(G):
	# dc_dumps = json.dumps(nx.degree_centrality(G).items(),sort_keys=True,indent=4)
	# dc_loads = json.loads(dc_dumps)
	dc_sorted = sorted(nx.degree_centrality(G).items(), key=itemgetter(0), reverse=True)
	bc_sorted = sorted(nx.betweenness_centrality(G).items(), key=itemgetter(0), reverse=True)
	clc_sorted = sorted(nx.closeness_centrality(G).items(), key=itemgetter(0), reverse=True)
	coc_sorted = sorted(nx.communicability_centrality(G).items(), key=itemgetter(0), reverse=True)
	lc_sorted = sorted(nx.load_centrality(G).items(), key=itemgetter(0), reverse=True)
	cfbc_sorted = sorted(nx.current_flow_betweenness_centrality(G).items(), key=itemgetter(0), reverse=True)
	cfcc_sorted = sorted(nx.current_flow_closeness_centrality(G).items(), key=itemgetter(0), reverse=True)
	# print ec_sorted[0]
	
	developer_centrality = []

	developer_file = file("public/wordpress/developer.json")
	developers = json.load(developer_file)
	for developer in developers:
		degree = 0
		betweenness = 0
		closeness = 0
		communicability = 0
		load = 0
		current_flow_betweenness = 0
		current_flow_closeness = 0
		for i in range (0, len(dc_sorted)):
			# if ( not dc_sorted[i][0] == bc_sorted[i][0] == clc_sorted[i][0] == coc_sorted[i][0] == lc_sorted[i][0] == cfbc_sorted[i][0]):
			# 	print 'false'
			if( developer['developer'] == dc_sorted[i][0]):
				degree = dc_sorted[i][1]
				betweenness = bc_sorted[i][1]
				closeness = clc_sorted[i][1]
				communicability = coc_sorted[i][1]
				load = lc_sorted[i][1]
				current_flow_betweenness = cfbc_sorted[i][1]
				current_flow_closeness = cfcc_sorted[i][1]

		developer_centrality.append({
			'name': developer['developer'],
		 	'degree': degree,
			'betweenness': betweenness,
			'closeness': closeness,
			'communicability': communicability,
			'load': load,
			'current_flow_betweenness': current_flow_betweenness,
			'current_flow_closeness':current_flow_closeness,
		 })

	return developer_centrality
Example #53
0
    def weightGraph(self, datacontacts, mi_threshold, time_treshold=0.6):
        if len(self.mol.get('resid', 'name CA')) != len(self.resids):
            raise Exception('The length of the protein doesn\'t match the Mutual Information data')
        contactcat = np.concatenate(datacontacts.dat)
        contacts_matrix = np.zeros([len(self.resids), len(self.resids)])
        for i in range(contactcat.shape[1]):
            counter = np.count_nonzero(contactcat[:, i])
            resid1 = self.residmap[self.mol.resid[datacontacts.description.atomIndexes[i][0]]]
            resid2 = self.residmap[self.mol.resid[datacontacts.description.atomIndexes[i][1]]]
            contacts_matrix[resid1][resid2] = counter

        self.graph_array = np.zeros([contacts_matrix.shape[0], contacts_matrix.shape[0]])
        mask = (self.mi_matrix > mi_threshold) & (contacts_matrix > (time_treshold * contactcat.shape[0]))
        self.graph_array[mask] = self.mi_matrix[mask]

        intermed = []
        for source in range(self.graph_array.shape[0]):
            for target in range(source, self.graph_array.shape[1]):
                if self.graph_array[source, target] != 0 and target > source:
                    intermed.append(
                        [int(self.resids[source]), int(self.resids[target]), float(self.graph_array[source, target])])
        import pandas as pd
        import networkx as nx
        from sklearn.cluster.spectral import SpectralClustering

        pd = pd.DataFrame(intermed, columns=['source', 'target', 'weight'])
        pd[['source', 'target']] = pd[['source', 'target']].astype(type('int', (int,), {}))
        pd['weight'] = pd['weight'].astype(type('float', (float,), {}))
        G = nx.from_pandas_dataframe(pd, 'source', 'target', ['weight'])
        ## setSegment
        segids = self.mol.get('segid', 'name CA')
        seg_res_dict = {key: value for (key, value) in zip(self.resids, segids) if
                        np.any(pd.loc[(pd['source'] == key)].index) or np.any(pd.loc[(pd['target'] == key)].index)}
        nx.set_node_attributes(G, 'Segment', seg_res_dict)
        ## set
        if not nx.is_connected(G):
            G = max(nx.connected_component_subgraphs(G), key=len)
        flow_cent = nx.current_flow_betweenness_centrality(G, weight='weight')
        nx.set_node_attributes(G, 'flowcent', flow_cent)
        Spectre = SpectralClustering(n_clusters=10, affinity='precomputed')
        model = Spectre.fit_predict(self.graph_array)
        model = model.astype(type('float', (float,), {}))
        spectral_dict = {key: value for (key, value) in zip(self.resids, model) if key in G.nodes()}
        nx.set_node_attributes(G, 'spectral', spectral_dict)
        self.graph = G
Example #54
0
def compute_metrics(G, w='weight'):
    for e in G.edges():
        G[e[0]][e[1]][w] = float(G[e[0]][e[1]][w])

    pr = nx.pagerank(G,weight=w, tol=0.000001)
    nx.set_node_attributes(G,'pagerank',pr)

    eig = nx.eigenvector_centrality_numpy(G)
    nx.set_node_attributes(G, 'eigencent',eig)

    ug = _di_to_undi(G)
    cf = nx.current_flow_betweenness_centrality(ug,weight=w)
    cf2 = dict.fromkeys(cf)
    for k in cf2.keys():
        cf2[k] = float(cf[k])
    nx.set_node_attributes(G,'currentflow',cf2)

    deg = G.degree()
    nx.set_node_attributes(G,'degree',deg)
    
    stren = G.degree(weight=w)
    nx.set_node_attributes(G,'strength',stren)
    def initializeMIP(self):
        self.pars.append({})
        userId = self.addUser(self.currentVersion.author)
        
        session = Session(self.currentVersion.author, self.currentVersion, len(self.log))
        partext = [a.text.encode('utf-8') for a in self.currentVersion.paragraphs]
#        print userId
        index = 0
        for par in self.currentVersion.paragraphs:
            parId = self.addPars(None, index)
#            print parId
            self.updateEdge(userId, parId,'u-p', self.sigIncrement)
            session.actions.append(Action(session.user, parId, 'sigEdit',partext[index], self.sigIncrement,1))
            index=index+1
            
        for i in range(0,len(self.currentVersion.paragraphs)):
            for j in range(i+1,len(self.currentVersion.paragraphs)):
                if i!=j:
                    self.updateEdge(self.pars[self.latestVersion-1][i], self.pars[self.latestVersion][j],'p-p', self.sigIncrement)
                    
        self.log.append(session)
        self.current_flow_betweeness = nx.current_flow_betweenness_centrality(self.mip,True, 'weight')
        print len(self.log)
Example #56
0
def add_current_flow_betweenness_node(graf):
    print "Adding current flow BETWEENNESS to nodes"
    cfb_dict = nx.current_flow_betweenness_centrality(graf)
    nx.set_node_attributes(graf, 'cfb', cfb_dict)
Example #57
0
stats = dict2column(stats, wdegree, 'wdegree')

#Current Closeness Centrality 
flowcloseness = nx.current_flow_closeness_centrality(mg, weight = 'exmptgross')
stats = dict2column(stats, flowcloseness, 'flowcloseness')

#Vertex Closeness
closeness = nx.closeness_centrality(mg)
stats = dict2column(stats, closeness, 'closeness')

#Vertex Betweenness
btwnness = nx.betweenness_centrality(mg)
stats = dict2column(stats, btwnness, 'btwnness')

#Current Betweenness
flowbtwnness = nx.current_flow_betweenness_centrality(mg, weight = 'exmptgross')
stats = dict2column(stats, flowbtwnness, 'flowbtwnness')

#Eigenvector Centrality
eigenvc = nx.eigenvector_centrality(mg)
stats = dict2column(stats, eigenvc, 'eigenvc')

#Weighted Eigenvector Centrality
weigenvc = we.eigenvector_centrality(mg, weight = 'exmptgross')
stats = dict2column(stats, weigenvc, 'weigenvc')

stats['shortname'] = stats['MSAName'].apply(lambda x: re.search('^.*?[-,]',x).group(0)[:-1] + re.search(', [A-Z][A-Z]',x).group(0))



#Try drawing
Example #58
0
					direct = C.get_edge_data(source, target)['resistance']
					endpoints[source] = endpoints.get(source,0) + 1
					endpoints[target] = endpoints.get(target,0) + 1
	with open('output/%s.csv' % path_code, 'wb') as f:
		f.write("Endpoint, Paths using %s as bridge\n" % code_to_name[path_code])
		for k,v in endpoints.iteritems():
			f.write("%s, %d\n" % (code_to_name[k], v))

# Find Centrality

print("Calculating shortest-path betweenness centrality for country graph")
results = nx.betweenness_centrality(C, weight="resistance")
nx.set_node_attributes(C, 'betweenness_centrality', results)

print("Calculating random-walk betweenness centrality for country graph")
results = nx.current_flow_betweenness_centrality(C, weight="weight")
nx.set_node_attributes(C, 'random_walk_betweenness_centrality', results)
#print("Calculating eigenvector centrality for country graph")
#results = nx.eigenvector_centrality(C, weight="weight")
#nx.set_node_attributes(C, 'eigenvector_centrality', results)
with open('output/countries.csv', 'wb') as f:
	f.write("Code, Name, Shortest Path Betweenness, Random Walk Betweenness\n")
	for node in sorted(C.nodes(data=True), key=lambda k: k[1]['betweenness_centrality'], reverse=True):
		f.write("%s, %s, %f, %f\n" % (node[0], node[1]['name'], node[1]['betweenness_centrality'], node[1]['random_walk_betweenness_centrality']))
		
# Create bipartite graph
print("Creating bipartite country-video graph")
G = graph.BiGraph()
for loc in locs.countries():
	name = loc.code
	if loc.code == '--':
def flow_between(net):
    return distriCentra(nx.current_flow_betweenness_centrality(net,normalized= True).values(),
                        nx.current_flow_betweenness_centrality(star(net),normalized= True).values(),
                        'current_flow_betweenness')