Exemplo n.º 1
0
 def test_p3_harmonic(self):
     c = harmonic_centrality(self.P3)
     d = {0: 1.5,
          1: 2,
          2: 1.5}
     for n in sorted(self.P3):
         assert_almost_equal(c[n], d[n], places=3)
Exemplo n.º 2
0
 def test_cycle_c4_directed(self):
     c = harmonic_centrality(self.C4_directed,
                             nbunch=[0, 1],
                             sources=[1, 2])
     d = {0: 0.833, 1: 0.333}
     for n in [0, 1]:
         assert almost_equal(c[n], d[n], places=3)
Exemplo n.º 3
0
 def test_cycle_c4_directed(self):
     c = harmonic_centrality(self.C4_directed,
                             nbunch=[0, 1],
                             sources=[1, 2])
     d = {0: 0.833, 1: 0.333}
     for n in [0, 1]:
         assert c[n] == pytest.approx(d[n], abs=1e-3)
Exemplo n.º 4
0
 def test_cycle_C4(self):
     c = harmonic_centrality(self.C4)
     d = {0: 2.5,
          1: 2.5,
          2: 2.5,
          3: 2.5, }
     for n in sorted(self.C4):
         assert_almost_equal(c[n], d[n], places=3)
Exemplo n.º 5
0
 def test_p4_harmonic(self):
     c = harmonic_centrality(self.P4)
     d = {0: 1.8333333,
          1: 2.5,
          2: 2.5,
          3: 1.8333333}
     for n in sorted(self.P4):
         assert_almost_equal(c[n], d[n], places=3)
Exemplo n.º 6
0
 def test_exampleGraph(self):
     c = harmonic_centrality(self.Gb)
     d = {0: 0,
          1: 2,
          2: 1,
          3: 2.5,
          4: 1}
     for n in sorted(self.Gb):
         assert_almost_equal(c[n], d[n], places=3)
Exemplo n.º 7
0
 def test_clique_complete(self):
     c = harmonic_centrality(self.K5)
     d = {0: 4,
          1: 4,
          2: 4,
          3: 4,
          4: 4}
     for n in sorted(self.P3):
         assert_almost_equal(c[n], d[n], places=3)
Exemplo n.º 8
0
 def test_cycle_C5(self):
     c = harmonic_centrality(self.C5)
     d = {0: 3,
          1: 3,
          2: 3,
          3: 3,
          4: 3,
          5: 4}
     for n in sorted(self.C5):
         assert_almost_equal(c[n], d[n], places=3)
 def test_cycle_C4(self):
     c = harmonic_centrality(self.C4)
     d = {
         0: 2.5,
         1: 2.5,
         2: 2.5,
         3: 2.5,
     }
     for n in sorted(self.C4):
         assert_almost_equal(c[n], d[n], places=3)
Exemplo n.º 10
0
 def test_bal_tree(self):
     c = harmonic_centrality(self.T)
     d = {0: 4.0,
          1: 4.1666,
          2: 4.1666,
          3: 2.8333,
          4: 2.8333,
          5: 2.8333,
          6: 2.8333}
     for n in sorted(self.T):
         assert_almost_equal(c[n], d[n], places=3)
Exemplo n.º 11
0
 def test_bal_tree(self):
     c = harmonic_centrality(self.T)
     d = {
         0: 4.0,
         1: 4.1666,
         2: 4.1666,
         3: 2.8333,
         4: 2.8333,
         5: 2.8333,
         6: 2.8333
     }
     for n in sorted(self.T):
         assert almost_equal(c[n], d[n], places=3)
Exemplo n.º 12
0
 def test_bal_tree(self):
     c = harmonic_centrality(self.T)
     d = {
         0: 4.0,
         1: 4.1666,
         2: 4.1666,
         3: 2.8333,
         4: 2.8333,
         5: 2.8333,
         6: 2.8333
     }
     for n in sorted(self.T):
         assert c[n] == pytest.approx(d[n], abs=1e-3)
Exemplo n.º 13
0
 def test_weighted_harmonic(self):
     XG = nx.DiGraph()
     XG.add_weighted_edges_from([('a', 'b', 10), ('d', 'c', 5), ('a', 'c', 1),
                                 ('e', 'f', 2), ('f', 'c', 1), ('a', 'f', 3),
                                 ])
     c = harmonic_centrality(XG, distance='weight')
     d = {'a': 0,
          'b': 0.1,
          'c': 2.533,
          'd': 0,
          'e': 0,
          'f': 0.83333}
     for n in sorted(XG):
         assert_almost_equal(c[n], d[n], places=3)
Exemplo n.º 14
0
 def test_weighted_harmonic(self):
     XG = nx.DiGraph()
     XG.add_weighted_edges_from([
         ("a", "b", 10),
         ("d", "c", 5),
         ("a", "c", 1),
         ("e", "f", 2),
         ("f", "c", 1),
         ("a", "f", 3),
     ])
     c = harmonic_centrality(XG, distance="weight")
     d = {"a": 0, "b": 0.1, "c": 2.533, "d": 0, "e": 0, "f": 0.83333}
     for n in sorted(XG):
         assert almost_equal(c[n], d[n], places=3)
 def test_weighted_harmonic(self):
     XG = nx.DiGraph()
     XG.add_weighted_edges_from([
         ('a', 'b', 10),
         ('d', 'c', 5),
         ('a', 'c', 1),
         ('e', 'f', 2),
         ('f', 'c', 1),
         ('a', 'f', 3),
     ])
     c = harmonic_centrality(XG, distance='weight')
     d = {'a': 0, 'b': 0.1, 'c': 2.533, 'd': 0, 'e': 0, 'f': 0.83333}
     for n in sorted(XG):
         assert_almost_equal(c[n], d[n], places=3)
Exemplo n.º 16
0
 def test_weighted_harmonic(self):
     XG = nx.DiGraph()
     XG.add_weighted_edges_from([
         ("a", "b", 10),
         ("d", "c", 5),
         ("a", "c", 1),
         ("e", "f", 2),
         ("f", "c", 1),
         ("a", "f", 3),
     ])
     c = harmonic_centrality(XG, distance="weight")
     d = {"a": 0, "b": 0.1, "c": 2.533, "d": 0, "e": 0, "f": 0.83333}
     for n in sorted(XG):
         assert c[n] == pytest.approx(d[n], abs=1e-3)
Exemplo n.º 17
0
 def test_p3_harmonic_subset(self):
     c = harmonic_centrality(self.P3, sources=[0, 1])
     d = {0: 1, 1: 1, 2: 1.5}
     for n in self.P3:
         assert almost_equal(c[n], d[n], places=3)
Exemplo n.º 18
0
 def test_p4_harmonic_subset(self):
     c = harmonic_centrality(self.P4, nbunch=[2, 3], sources=[0, 1])
     d = {2: 1.5, 3: 0.8333333}
     for n in [2, 3]:
         assert c[n] == pytest.approx(d[n], abs=1e-3)
 def test_singleton(self):
     G = nx.DiGraph()
     G.add_node(0)
     c = harmonic_centrality(G, distance='weight')
     d = {0: 0}
     assert_equal(c, d)
Exemplo n.º 20
0
 def test_clique_complete(self):
     c = harmonic_centrality(self.K5)
     d = {0: 4, 1: 4, 2: 4, 3: 4, 4: 4}
     for n in sorted(self.P3):
         assert c[n] == pytest.approx(d[n], abs=1e-3)
Exemplo n.º 21
0
 def test_exampleGraph(self):
     c = harmonic_centrality(self.Gb)
     d = {0: 0, 1: 2, 2: 1, 3: 2.5, 4: 1}
     for n in sorted(self.Gb):
         assert almost_equal(c[n], d[n], places=3)
Exemplo n.º 22
0
 def test_cycle_C5(self):
     c = harmonic_centrality(self.C5)
     d = {0: 3, 1: 3, 2: 3, 3: 3, 4: 3, 5: 4}
     for n in sorted(self.C5):
         assert almost_equal(c[n], d[n], places=3)
Exemplo n.º 23
0
 def test_p4_harmonic(self):
     c = harmonic_centrality(self.P4)
     d = {0: 1.8333333, 1: 2.5, 2: 2.5, 3: 1.8333333}
     for n in sorted(self.P4):
         assert almost_equal(c[n], d[n], places=3)
Exemplo n.º 24
0
    def compute_features(self):
        # Degree centrality
        degree_centrality = lambda graph: list(
            centrality.degree_centrality(graph).values())
        self.add_feature(
            "degree centrality",
            degree_centrality,
            "The degree centrality distribution",
            InterpretabilityScore(5),
            statistics="centrality",
        )

        # Betweenness Centrality
        betweenness_centrality = lambda graph: list(
            centrality.betweenness_centrality(graph).values())
        self.add_feature(
            "betweenness centrality",
            betweenness_centrality,
            "Betweenness centrality of a node v is the sum of the fraction of \
            all-pairs shortest paths that pass through v",
            InterpretabilityScore(5),
            statistics="centrality",
        )

        # Closeness centrality
        closeness_centrality = lambda graph: list(
            centrality.closeness_centrality(graph).values())
        self.add_feature(
            "closeness centrality",
            closeness_centrality,
            "Closeness is the reciprocal of the average shortest path distance",
            InterpretabilityScore(5),
            statistics="centrality",
        )

        # Edge betweenness centrality
        def edge_betweenness_centrality(graph):
            if graph.edges:
                return list(
                    centrality.edge_betweenness_centrality(graph).values())
            return [np.nan]

        self.add_feature(
            "edge betweenness centrality",
            edge_betweenness_centrality,
            "Betweenness centrality of an edge e is the sum of the fraction of \
            all-pairs shortest paths that pass through e",
            InterpretabilityScore(4),
            statistics="centrality",
        )

        # Harmonic centrality
        harmonic_centrality = lambda graph: list(
            centrality.harmonic_centrality(graph).values())
        self.add_feature(
            "harmonic centrality",
            harmonic_centrality,
            "Harmonic centrality of a node u is the sum of the reciprocal \
            of the shortest path distances from all other nodes to u",
            InterpretabilityScore(4),
            statistics="centrality",
        )

        # Subgraph centrality
        subgraph_centrality = lambda graph: list(
            centrality.subgraph_centrality(graph).values())
        self.add_feature(
            "subgraph centrality",
            subgraph_centrality,
            "The subgraph centrality for a node is the sum of weighted closed walks \
            of all lengths starting and ending at that node.",
            InterpretabilityScore(3),
            statistics="centrality",
        )

        # Second order centrality
        second_order_centrality = lambda graph: list(
            centrality.second_order_centrality(utils.ensure_connected(graph)).
            values())

        self.add_feature(
            "second order centrality",
            second_order_centrality,
            "The second order centrality of a given node is the standard deviation \
            of the return times to that node of a perpetual random walk on G",
            InterpretabilityScore(4),
            statistics="centrality",
        )

        # Eigenvector centrality
        eigenvector_centrality = lambda graph: list(
            centrality.eigenvector_centrality_numpy(
                utils.ensure_connected(graph)).values())
        self.add_feature(
            "eigenvector centrality",
            eigenvector_centrality,
            "Eigenvector centrality computes the centrality for a node based \
            on the centrality of its neighbors",
            InterpretabilityScore(4),
            statistics="centrality",
        )

        # Katz centrality
        katz_centrality = lambda graph: list(
            centrality.katz_centrality_numpy(utils.ensure_connected(graph)).
            values())
        self.add_feature(
            "katz centrality",
            katz_centrality,
            "Generalisation of eigenvector centrality - Katz centrality computes the \
            centrality for a node based on the centrality of its neighbors",
            InterpretabilityScore(4),
            statistics="centrality",
        )

        # Page Rank
        pagerank = lambda graph: list(nx.pagerank_numpy(graph).values())
        self.add_feature(
            "pagerank",
            pagerank,
            "The pagerank computes a ranking of the nodes in the graph based on \
            the structure of the incoming links. ",
            InterpretabilityScore(4),
            statistics="centrality",
        )
Exemplo n.º 25
0
 def test_singleton(self):
     G = nx.DiGraph()
     G.add_node(0)
     c = harmonic_centrality(G, distance='weight')
     d = {0: 0}
     assert_equal(c, d)
Exemplo n.º 26
0
 def test_empty(self):
     G = nx.DiGraph()
     c = harmonic_centrality(G, distance="weight")
     d = {}
     assert c == d
Exemplo n.º 27
0
 def test_cycle_C5(self):
     c = harmonic_centrality(self.C5)
     d = {0: 3, 1: 3, 2: 3, 3: 3, 4: 3, 5: 4}
     for n in sorted(self.C5):
         assert c[n] == pytest.approx(d[n], abs=1e-3)
Exemplo n.º 28
0
 def test_cycle_C4(self):
     c = harmonic_centrality(self.C4)
     d = {0: 2.5, 1: 2.5, 2: 2.5, 3: 2.5}
     for n in sorted(self.C4):
         assert c[n] == pytest.approx(d[n], abs=1e-3)
Exemplo n.º 29
0
 def test_p4_harmonic_subset(self):
     c = harmonic_centrality(self.P4, nbunch=[2, 3], sources=[0, 1])
     d = {2: 1.5, 3: 0.8333333}
     for n in [2, 3]:
         assert almost_equal(c[n], d[n], places=3)
Exemplo n.º 30
0
 def test_p3_harmonic(self):
     c = harmonic_centrality(self.P3)
     d = {0: 1.5, 1: 2, 2: 1.5}
     for n in sorted(self.P3):
         assert almost_equal(c[n], d[n], places=3)
Exemplo n.º 31
0
def harmonic_centrality(graph):
    """harmonic_centrality"""
    return list(centrality.harmonic_centrality(graph).values())
Exemplo n.º 32
0
 def test_clique_complete(self):
     c = harmonic_centrality(self.K5)
     d = {0: 4, 1: 4, 2: 4, 3: 4, 4: 4}
     for n in sorted(self.P3):
         assert almost_equal(c[n], d[n], places=3)
Exemplo n.º 33
0
 def test_p3_harmonic(self):
     c = harmonic_centrality(self.P3)
     d = {0: 1.5, 1: 2, 2: 1.5}
     for n in sorted(self.P3):
         assert c[n] == pytest.approx(d[n], abs=1e-3)
Exemplo n.º 34
0
 def test_p3_harmonic_subset(self):
     c = harmonic_centrality(self.P3, sources=[0, 1])
     d = {0: 1, 1: 1, 2: 1.5}
     for n in self.P3:
         assert c[n] == pytest.approx(d[n], abs=1e-3)
 def test_empty(self):
     G = nx.DiGraph()
     c = harmonic_centrality(G, distance='weight')
     d = {}
     assert_equal(c, d)
Exemplo n.º 36
0
 def test_exampleGraph(self):
     c = harmonic_centrality(self.Gb)
     d = {0: 0, 1: 2, 2: 1, 3: 2.5, 4: 1}
     for n in sorted(self.Gb):
         assert c[n] == pytest.approx(d[n], abs=1e-3)
Exemplo n.º 37
0
 def test_p4_harmonic(self):
     c = harmonic_centrality(self.P4)
     d = {0: 1.8333333, 1: 2.5, 2: 2.5, 3: 1.8333333}
     for n in sorted(self.P4):
         assert c[n] == pytest.approx(d[n], abs=1e-3)
Exemplo n.º 38
0
 def test_singleton(self):
     G = nx.DiGraph()
     G.add_node(0)
     c = harmonic_centrality(G, distance="weight")
     d = {0: 0}
     assert c == d
Exemplo n.º 39
0
 def test_empty(self):
     G = nx.DiGraph()
     c = harmonic_centrality(G, distance='weight')
     d = {}
     assert_equal(c, d)