def test_K5(self):
     """Edge betweenness centrality: K5"""
     G = nx.complete_graph(5)
     b = nx.edge_betweenness_centrality_subset(G, sources=[0],
                                               targets=[1, 3], weight=None)
     b_answer = dict.fromkeys(G.edges(), 0)
     b_answer[(0, 3)] = b_answer[(0, 1)] = 0.5
     for n in sorted(G.edges()):
         assert_almost_equal(b[n], b_answer[n])
Esempio n. 2
0
    def most_important_nodes_edges_subset(self, graph, source_nodes, target_nodes, T=0, normalized=False, directed=False):

        cv=check_graph_validity.Graphs()
        ret = cv.is_valid_most_important_graph(graph, source_nodes, target_nodes,T)
        if(not ret[0]):
            ret.append({})
            print (ret)
            return ret
      
        try:
            # construct networkx graph from given graph
            if (directed):
                G=nx.DiGraph()
            else:    
                G = nx.Graph()
            G.add_nodes_from(graph['nodes'])
            G.add_edges_from(graph['edges'])

            if 'weights' in graph:
                for i in range(len(graph['edges'])):
                    G[graph['edges'][i][0]][graph['edges'][i][1]]['weight'] = graph['weights'][i]
        except Exception as e:
            return ["False", str(e),{}]

        output={}
        # clearDict={}
       
        result = None

        if (T == 0):
            
            result=nx.betweenness_centrality_subset(G, source_nodes, target_nodes, normalized, weight='weights')
            #remove nodes that are either in source_node or in target_node
            # for key,val in result.items():
            #     if (not(key in source_nodes or key in target_nodes)):
            #      clearDict[key]=val
            # print(clearDict)

            
        elif (T == 1):
            result =nx.edge_betweenness_centrality_subset(G, source_nodes, target_nodes, normalized, weight='weights')
            # for key,val in result.items():
            #      #remove nodes that are either in source_node or in target_node
            #     if(not(key[0] in source_nodes or key[0] in target_nodes or key[1] in source_nodes or key[1] in target_nodes)):
            #         clearDict[key]=val
            # print(clearDict)

        # output["betweenness_centrality"] = list(max(clearDict.items(), key=lambda k: k[1]))

        highest = max(result.values())
        nodes_list = [k for k, v in result.items() if v == highest]
        output["betweenness_centrality"] = [nodes_list,highest]    #result

        print (output)


        return [True, 'success', output]
 def test_P5(self):
     """Edge betweenness centrality: P5"""
     G = nx.Graph()
     nx.add_path(G, range(5))
     b_answer = dict.fromkeys(G.edges(), 0)
     b_answer[(0, 1)] = b_answer[(1, 2)] = b_answer[(2, 3)] = 0.5
     b = nx.edge_betweenness_centrality_subset(G, sources=[0], targets=[3],
                                               weight=None)
     for n in sorted(G.edges()):
         assert_almost_equal(b[n], b_answer[n])
 def test_box(self):
     """Edge etweenness centrality: box"""
     G = nx.Graph()
     G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
     b_answer = dict.fromkeys(G.edges(), 0)
     b_answer[(0, 1)] = b_answer[(0, 2)] = 0.25
     b_answer[(1, 3)] = b_answer[(2, 3)] = 0.25
     b = nx.edge_betweenness_centrality_subset(G, sources=[0], targets=[3],
                                               weight=None)
     for n in sorted(G.edges()):
         assert_almost_equal(b[n], b_answer[n])
 def test_P5_directed(self):
     """Edge betweenness centrality: P5 directed"""
     G = networkx.DiGraph()
     G.add_path(range(5))
     b_answer = dict.fromkeys(G.edges(), 0)
     b_answer[(0, 1)] = 1
     b_answer[(1, 2)] = 1
     b_answer[(2, 3)] = 1
     b = edge_betweenness_centrality_subset(G, sources=[0], targets=[3], weighted_edges=False)
     for n in sorted(G.edges()):
         assert_almost_equal(b[n], b_answer[n])
 def test_P5_directed(self):
     """Edge betweenness subset centrality: P5 directed"""
     G = nx.DiGraph()
     nx.add_path(G, range(5))
     b_answer = dict.fromkeys(G.edges(), 0)
     b_answer[(0, 1)] = b_answer[(1, 2)] = b_answer[(2, 3)] = 1
     b = nx.edge_betweenness_centrality_subset(G,
                                               sources=[0],
                                               targets=[3],
                                               weight=None)
     for n in sorted(G.edges()):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
 def test_K5(self):
     """Edge betweenness centrality: K5"""
     G = networkx.complete_graph(5)
     b = edge_betweenness_centrality_subset(G,
                                            sources=[0],
                                            targets=[1, 3],
                                            weighted_edges=False)
     b_answer = dict.fromkeys(G.edges(), 0)
     b_answer[(0, 3)] = 0.5
     b_answer[(0, 1)] = 0.5
     for n in sorted(G.edges()):
         print(n, b[n])
         assert_almost_equal(b[n], b_answer[n])
 def test_box_and_path2(self):
     """Edge betweenness centrality: box and path multiple target"""
     G = nx.Graph()
     G.add_edges_from([(0, 1), (1, 2), (2, 3), (1, 20), (20, 3), (3, 4)])
     b_answer = dict.fromkeys(G.edges(), 0)
     b_answer[(0, 1)] = 1.0
     b_answer[(1, 20)] = b_answer[(3, 20)] = 0.5
     b_answer[(1, 2)] = b_answer[(2, 3)] = 0.5
     b_answer[(3, 4)] = 0.5
     b = nx.edge_betweenness_centrality_subset(G, sources=[0],
                                               targets=[3, 4], weight=None)
     for n in sorted(G.edges()):
         assert_almost_equal(b[n], b_answer[n])
 def test_P5(self):
     """Edge betweenness centrality: P5"""
     G = networkx.Graph()
     G.add_path(list(range(5)))
     b_answer = dict.fromkeys(G.edges(), 0)
     b_answer[(0, 1)] = 0.5
     b_answer[(1, 2)] = 0.5
     b_answer[(2, 3)] = 0.5
     b = edge_betweenness_centrality_subset(G,
                                            sources=[0],
                                            targets=[3],
                                            weighted_edges=False)
     for n in sorted(G.edges()):
         assert_almost_equal(b[n], b_answer[n])
 def test_box_and_path(self):
     """Edge betweenness subset centrality: box and path"""
     G = nx.Graph()
     G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (3, 4), (4, 5)])
     b_answer = dict.fromkeys(G.edges(), 0)
     b_answer[(0, 1)] = b_answer[(0, 2)] = 0.5
     b_answer[(1, 3)] = b_answer[(2, 3)] = 0.5
     b_answer[(3, 4)] = 0.5
     b = nx.edge_betweenness_centrality_subset(G,
                                               sources=[0],
                                               targets=[3, 4],
                                               weight=None)
     for n in sorted(G.edges()):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
 def test_P5_multiple_target(self):
     """Edge betweenness centrality: P5 multiple target"""
     G=networkx.Graph()
     G.add_path(list(range(5)))
     b_answer=dict.fromkeys(G.edges(),0)
     b_answer[(0,1)]=1
     b_answer[(1,2)]=1
     b_answer[(2,3)]=1
     b_answer[(3,4)]=0.5
     b=edge_betweenness_centrality_subset(G,
                                          sources=[0],
                                          targets=[3,4],
                                          weight=None)
     for n in sorted(G.edges()):
         assert_almost_equal(b[n],b_answer[n])
 def test_box_and_path(self):
     """Edge etweenness centrality: box and path"""
     G = networkx.Graph()
     G.add_edge(0, 1)
     G.add_edge(0, 2)
     G.add_edge(1, 3)
     G.add_edge(2, 3)
     G.add_edge(3, 4)
     G.add_edge(4, 5)
     b_answer = dict.fromkeys(G.edges(), 0)
     b_answer[(0, 1)] = 1.0 / 2
     b_answer[(0, 2)] = 1.0 / 2
     b_answer[(1, 3)] = 1.0 / 2
     b_answer[(2, 3)] = 1.0 / 2
     b_answer[(3, 4)] = 1.0 / 2
     b = edge_betweenness_centrality_subset(G, sources=[0], targets=[3, 4], weighted_edges=False)
     for n in sorted(G.edges()):
         assert_almost_equal(b[n], b_answer[n])
    def test_box(self):
        """Edge etweenness centrality: box"""
        G = networkx.Graph()
        G.add_edge(0, 1)
        G.add_edge(0, 2)
        G.add_edge(1, 3)
        G.add_edge(2, 3)
        b_answer = dict.fromkeys(G.edges(), 0)

        b_answer[(0, 1)] = 0.25
        b_answer[(0, 2)] = 0.25
        b_answer[(1, 3)] = 0.25
        b_answer[(2, 3)] = 0.25
        b = edge_betweenness_centrality_subset(G,
                                               sources=[0],
                                               targets=[3],
                                               weighted_edges=False)
        for n in sorted(G.edges()):
            assert_almost_equal(b[n], b_answer[n])
 def test_box_and_path(self):
     """Edge etweenness centrality: box and path"""
     G = nx.Graph()
     G.add_edge(0, 1)
     G.add_edge(0, 2)
     G.add_edge(1, 3)
     G.add_edge(2, 3)
     G.add_edge(3, 4)
     G.add_edge(4, 5)
     b_answer = dict.fromkeys(G.edges(), 0)
     b_answer[(0, 1)] = 1.0 / 2
     b_answer[(0, 2)] = 1.0 / 2
     b_answer[(1, 3)] = 1.0 / 2
     b_answer[(2, 3)] = 1.0 / 2
     b_answer[(3, 4)] = 1.0 / 2
     b = edge_betweenness_centrality_subset(G,
                                            sources=[0],
                                            targets=[3, 4],
                                            weight=None)
     for n in sorted(G.edges()):
         assert_almost_equal(b[n], b_answer[n])