### comparison env/lib/python3.9/site-packages/networkx/algorithms/centrality/tests/test_group.py @ 0:4f3585e2f14bdraftdefaulttip

author shellac Mon, 22 Mar 2021 18:12:50 +0000
comparison
equal inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """
2 Tests for Group Centrality Measures
3 """
4
5
6 import pytest
7 import networkx as nx
8
9
10 class TestGroupBetweennessCentrality:
11 def test_group_betweenness_single_node(self):
12 """
13 Group betweenness centrality for single node group
14 """
15 G = nx.path_graph(5)
16 C = [1]
17 b = nx.group_betweenness_centrality(G, C, weight=None, normalized=False)
18 b_answer = 3.0
19 assert b == b_answer
20
21 def test_group_betweenness_normalized(self):
22 """
23 Group betweenness centrality for group with more than
24 1 node and normalized
25 """
26 G = nx.path_graph(5)
27 C = [1, 3]
28 b = nx.group_betweenness_centrality(G, C, weight=None, normalized=True)
29 b_answer = 1.0
30 assert b == b_answer
31
32 def test_group_betweenness_value_zero(self):
33 """
34 Group betweenness centrality value of 0
35 """
36 G = nx.cycle_graph(6)
37 C = [0, 1, 5]
38 b = nx.group_betweenness_centrality(G, C, weight=None)
39 b_answer = 0.0
40 assert b == b_answer
41
42 def test_group_betweenness_disconnected_graph(self):
43 """
44 Group betweenness centrality in a disconnected graph
45 """
46 G = nx.path_graph(5)
47 G.remove_edge(0, 1)
48 C = [1]
49 b = nx.group_betweenness_centrality(G, C, weight=None)
50 b_answer = 0.0
51 assert b == b_answer
52
53 def test_group_betweenness_node_not_in_graph(self):
54 """
55 Node(s) in C not in graph, raises NodeNotFound exception
56 """
57 with pytest.raises(nx.NodeNotFound):
58 b = nx.group_betweenness_centrality(nx.path_graph(5), [6, 7, 8])
59
60
61 class TestGroupClosenessCentrality:
62 def test_group_closeness_single_node(self):
63 """
64 Group closeness centrality for a single node group
65 """
66 G = nx.path_graph(5)
67 c = nx.group_closeness_centrality(G, [1])
68 c_answer = nx.closeness_centrality(G, 1)
69 assert c == c_answer
70
71 def test_group_closeness_disconnected(self):
72 """
73 Group closeness centrality for a disconnected graph
74 """
75 G = nx.Graph()
76 G.add_nodes_from([1, 2, 3, 4])
77 c = nx.group_closeness_centrality(G, [1, 2])
78 c_answer = 0
79 assert c == c_answer
80
81 def test_group_closeness_multiple_node(self):
82 """
83 Group closeness centrality for a group with more than
84 1 node
85 """
86 G = nx.path_graph(4)
87 c = nx.group_closeness_centrality(G, [1, 2])
88 c_answer = 1
89 assert c == c_answer
90
91 def test_group_closeness_node_not_in_graph(self):
92 """
93 Node(s) in S not in graph, raises NodeNotFound exception
94 """
95 with pytest.raises(nx.NodeNotFound):
96 c = nx.group_closeness_centrality(nx.path_graph(5), [6, 7, 8])
97
98
99 class TestGroupDegreeCentrality:
100 def test_group_degree_centrality_single_node(self):
101 """
102 Group degree centrality for a single node group
103 """
104 G = nx.path_graph(4)
105 d = nx.group_degree_centrality(G, [1])
106 d_answer = nx.degree_centrality(G)[1]
107 assert d == d_answer
108
109 def test_group_degree_centrality_multiple_node(self):
110 """
111 Group degree centrality for group with more than
112 1 node
113 """
114 G = nx.Graph()
115 G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8])
117 [(1, 2), (1, 3), (1, 6), (1, 7), (1, 8), (2, 3), (2, 4), (2, 5)]
118 )
119 d = nx.group_degree_centrality(G, [1, 2])
120 d_answer = 1
121 assert d == d_answer
122
123 def test_group_in_degree_centrality(self):
124 """
125 Group in-degree centrality in a DiGraph
126 """
127 G = nx.DiGraph()
128 G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8])
130 [(1, 2), (1, 3), (1, 6), (1, 7), (1, 8), (2, 3), (2, 4), (2, 5)]
131 )
132 d = nx.group_in_degree_centrality(G, [1, 2])
133 d_answer = 0
134 assert d == d_answer
135
136 def test_group_out_degree_centrality(self):
137 """
138 Group out-degree centrality in a DiGraph
139 """
140 G = nx.DiGraph()
141 G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8])