comparison env/lib/python3.9/site-packages/networkx/algorithms/centrality/tests/test_harmonic_centrality.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """
2 Tests for degree centrality.
3 """
4 import networkx as nx
5 from networkx.algorithms.centrality import harmonic_centrality
6 from networkx.testing import almost_equal
7
8
9 class TestClosenessCentrality:
10 @classmethod
11 def setup_class(cls):
12 cls.P3 = nx.path_graph(3)
13 cls.P4 = nx.path_graph(4)
14 cls.K5 = nx.complete_graph(5)
15
16 cls.C4 = nx.cycle_graph(4)
17 cls.C5 = nx.cycle_graph(5)
18
19 cls.T = nx.balanced_tree(r=2, h=2)
20
21 cls.Gb = nx.DiGraph()
22 cls.Gb.add_edges_from([(0, 1), (0, 2), (0, 4), (2, 1), (2, 3), (4, 3)])
23
24 def test_p3_harmonic(self):
25 c = harmonic_centrality(self.P3)
26 d = {0: 1.5, 1: 2, 2: 1.5}
27 for n in sorted(self.P3):
28 assert almost_equal(c[n], d[n], places=3)
29
30 def test_p4_harmonic(self):
31 c = harmonic_centrality(self.P4)
32 d = {0: 1.8333333, 1: 2.5, 2: 2.5, 3: 1.8333333}
33 for n in sorted(self.P4):
34 assert almost_equal(c[n], d[n], places=3)
35
36 def test_clique_complete(self):
37 c = harmonic_centrality(self.K5)
38 d = {0: 4, 1: 4, 2: 4, 3: 4, 4: 4}
39 for n in sorted(self.P3):
40 assert almost_equal(c[n], d[n], places=3)
41
42 def test_cycle_C4(self):
43 c = harmonic_centrality(self.C4)
44 d = {0: 2.5, 1: 2.5, 2: 2.5, 3: 2.5}
45 for n in sorted(self.C4):
46 assert almost_equal(c[n], d[n], places=3)
47
48 def test_cycle_C5(self):
49 c = harmonic_centrality(self.C5)
50 d = {0: 3, 1: 3, 2: 3, 3: 3, 4: 3, 5: 4}
51 for n in sorted(self.C5):
52 assert almost_equal(c[n], d[n], places=3)
53
54 def test_bal_tree(self):
55 c = harmonic_centrality(self.T)
56 d = {0: 4.0, 1: 4.1666, 2: 4.1666, 3: 2.8333, 4: 2.8333, 5: 2.8333, 6: 2.8333}
57 for n in sorted(self.T):
58 assert almost_equal(c[n], d[n], places=3)
59
60 def test_exampleGraph(self):
61 c = harmonic_centrality(self.Gb)
62 d = {0: 0, 1: 2, 2: 1, 3: 2.5, 4: 1}
63 for n in sorted(self.Gb):
64 assert almost_equal(c[n], d[n], places=3)
65
66 def test_weighted_harmonic(self):
67 XG = nx.DiGraph()
68 XG.add_weighted_edges_from(
69 [
70 ("a", "b", 10),
71 ("d", "c", 5),
72 ("a", "c", 1),
73 ("e", "f", 2),
74 ("f", "c", 1),
75 ("a", "f", 3),
76 ]
77 )
78 c = harmonic_centrality(XG, distance="weight")
79 d = {"a": 0, "b": 0.1, "c": 2.533, "d": 0, "e": 0, "f": 0.83333}
80 for n in sorted(XG):
81 assert almost_equal(c[n], d[n], places=3)
82
83 def test_empty(self):
84 G = nx.DiGraph()
85 c = harmonic_centrality(G, distance="weight")
86 d = {}
87 assert c == d
88
89 def test_singleton(self):
90 G = nx.DiGraph()
91 G.add_node(0)
92 c = harmonic_centrality(G, distance="weight")
93 d = {0: 0}
94 assert c == d