test_group.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. """
  2. Tests for Group Centrality Measures
  3. """
  4. import pytest
  5. import networkx as nx
  6. class TestGroupBetweennessCentrality:
  7. def test_group_betweenness_single_node(self):
  8. """
  9. Group betweenness centrality for single node group
  10. """
  11. G = nx.path_graph(5)
  12. C = [1]
  13. b = nx.group_betweenness_centrality(
  14. G, C, weight=None, normalized=False, endpoints=False
  15. )
  16. b_answer = 3.0
  17. assert b == b_answer
  18. def test_group_betweenness_with_endpoints(self):
  19. """
  20. Group betweenness centrality for single node group
  21. """
  22. G = nx.path_graph(5)
  23. C = [1]
  24. b = nx.group_betweenness_centrality(
  25. G, C, weight=None, normalized=False, endpoints=True
  26. )
  27. b_answer = 7.0
  28. assert b == b_answer
  29. def test_group_betweenness_normalized(self):
  30. """
  31. Group betweenness centrality for group with more than
  32. 1 node and normalized
  33. """
  34. G = nx.path_graph(5)
  35. C = [1, 3]
  36. b = nx.group_betweenness_centrality(
  37. G, C, weight=None, normalized=True, endpoints=False
  38. )
  39. b_answer = 1.0
  40. assert b == b_answer
  41. def test_two_group_betweenness_value_zero(self):
  42. """
  43. Group betweenness centrality value of 0
  44. """
  45. G = nx.cycle_graph(7)
  46. C = [[0, 1, 6], [0, 1, 5]]
  47. b = nx.group_betweenness_centrality(G, C, weight=None, normalized=False)
  48. b_answer = [0.0, 3.0]
  49. assert b == b_answer
  50. def test_group_betweenness_value_zero(self):
  51. """
  52. Group betweenness centrality value of 0
  53. """
  54. G = nx.cycle_graph(6)
  55. C = [0, 1, 5]
  56. b = nx.group_betweenness_centrality(G, C, weight=None, normalized=False)
  57. b_answer = 0.0
  58. assert b == b_answer
  59. def test_group_betweenness_disconnected_graph(self):
  60. """
  61. Group betweenness centrality in a disconnected graph
  62. """
  63. G = nx.path_graph(5)
  64. G.remove_edge(0, 1)
  65. C = [1]
  66. b = nx.group_betweenness_centrality(G, C, weight=None, normalized=False)
  67. b_answer = 0.0
  68. assert b == b_answer
  69. def test_group_betweenness_node_not_in_graph(self):
  70. """
  71. Node(s) in C not in graph, raises NodeNotFound exception
  72. """
  73. with pytest.raises(nx.NodeNotFound):
  74. nx.group_betweenness_centrality(nx.path_graph(5), [4, 7, 8])
  75. def test_group_betweenness_directed_weighted(self):
  76. """
  77. Group betweenness centrality in a directed and weighted graph
  78. """
  79. G = nx.DiGraph()
  80. G.add_edge(1, 0, weight=1)
  81. G.add_edge(0, 2, weight=2)
  82. G.add_edge(1, 2, weight=3)
  83. G.add_edge(3, 1, weight=4)
  84. G.add_edge(2, 3, weight=1)
  85. G.add_edge(4, 3, weight=6)
  86. G.add_edge(2, 4, weight=7)
  87. C = [1, 2]
  88. b = nx.group_betweenness_centrality(G, C, weight="weight", normalized=False)
  89. b_answer = 5.0
  90. assert b == b_answer
  91. class TestProminentGroup:
  92. np = pytest.importorskip("numpy")
  93. pd = pytest.importorskip("pandas")
  94. def test_prominent_group_single_node(self):
  95. """
  96. Prominent group for single node
  97. """
  98. G = nx.path_graph(5)
  99. k = 1
  100. b, g = nx.prominent_group(G, k, normalized=False, endpoints=False)
  101. b_answer, g_answer = 4.0, [2]
  102. assert b == b_answer and g == g_answer
  103. def test_prominent_group_with_c(self):
  104. """
  105. Prominent group without some nodes
  106. """
  107. G = nx.path_graph(5)
  108. k = 1
  109. b, g = nx.prominent_group(G, k, normalized=False, C=[2])
  110. b_answer, g_answer = 3.0, [1]
  111. assert b == b_answer and g == g_answer
  112. def test_prominent_group_normalized_endpoints(self):
  113. """
  114. Prominent group with normalized result, with endpoints
  115. """
  116. G = nx.cycle_graph(7)
  117. k = 2
  118. b, g = nx.prominent_group(G, k, normalized=True, endpoints=True)
  119. b_answer, g_answer = 1.7, [2, 5]
  120. assert b == b_answer and g == g_answer
  121. def test_prominent_group_disconnected_graph(self):
  122. """
  123. Prominent group of disconnected graph
  124. """
  125. G = nx.path_graph(6)
  126. G.remove_edge(0, 1)
  127. k = 1
  128. b, g = nx.prominent_group(G, k, weight=None, normalized=False)
  129. b_answer, g_answer = 4.0, [3]
  130. assert b == b_answer and g == g_answer
  131. def test_prominent_group_node_not_in_graph(self):
  132. """
  133. Node(s) in C not in graph, raises NodeNotFound exception
  134. """
  135. with pytest.raises(nx.NodeNotFound):
  136. nx.prominent_group(nx.path_graph(5), 1, C=[10])
  137. def test_group_betweenness_directed_weighted(self):
  138. """
  139. Group betweenness centrality in a directed and weighted graph
  140. """
  141. G = nx.DiGraph()
  142. G.add_edge(1, 0, weight=1)
  143. G.add_edge(0, 2, weight=2)
  144. G.add_edge(1, 2, weight=3)
  145. G.add_edge(3, 1, weight=4)
  146. G.add_edge(2, 3, weight=1)
  147. G.add_edge(4, 3, weight=6)
  148. G.add_edge(2, 4, weight=7)
  149. k = 2
  150. b, g = nx.prominent_group(G, k, weight="weight", normalized=False)
  151. b_answer, g_answer = 5.0, [1, 2]
  152. assert b == b_answer and g == g_answer
  153. def test_prominent_group_greedy_algorithm(self):
  154. """
  155. Group betweenness centrality in a greedy algorithm
  156. """
  157. G = nx.cycle_graph(7)
  158. k = 2
  159. b, g = nx.prominent_group(G, k, normalized=True, endpoints=True, greedy=True)
  160. b_answer, g_answer = 1.7, [6, 3]
  161. assert b == b_answer and g == g_answer
  162. class TestGroupClosenessCentrality:
  163. def test_group_closeness_single_node(self):
  164. """
  165. Group closeness centrality for a single node group
  166. """
  167. G = nx.path_graph(5)
  168. c = nx.group_closeness_centrality(G, [1])
  169. c_answer = nx.closeness_centrality(G, 1)
  170. assert c == c_answer
  171. def test_group_closeness_disconnected(self):
  172. """
  173. Group closeness centrality for a disconnected graph
  174. """
  175. G = nx.Graph()
  176. G.add_nodes_from([1, 2, 3, 4])
  177. c = nx.group_closeness_centrality(G, [1, 2])
  178. c_answer = 0
  179. assert c == c_answer
  180. def test_group_closeness_multiple_node(self):
  181. """
  182. Group closeness centrality for a group with more than
  183. 1 node
  184. """
  185. G = nx.path_graph(4)
  186. c = nx.group_closeness_centrality(G, [1, 2])
  187. c_answer = 1
  188. assert c == c_answer
  189. def test_group_closeness_node_not_in_graph(self):
  190. """
  191. Node(s) in S not in graph, raises NodeNotFound exception
  192. """
  193. with pytest.raises(nx.NodeNotFound):
  194. nx.group_closeness_centrality(nx.path_graph(5), [6, 7, 8])
  195. class TestGroupDegreeCentrality:
  196. def test_group_degree_centrality_single_node(self):
  197. """
  198. Group degree centrality for a single node group
  199. """
  200. G = nx.path_graph(4)
  201. d = nx.group_degree_centrality(G, [1])
  202. d_answer = nx.degree_centrality(G)[1]
  203. assert d == d_answer
  204. def test_group_degree_centrality_multiple_node(self):
  205. """
  206. Group degree centrality for group with more than
  207. 1 node
  208. """
  209. G = nx.Graph()
  210. G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8])
  211. G.add_edges_from(
  212. [(1, 2), (1, 3), (1, 6), (1, 7), (1, 8), (2, 3), (2, 4), (2, 5)]
  213. )
  214. d = nx.group_degree_centrality(G, [1, 2])
  215. d_answer = 1
  216. assert d == d_answer
  217. def test_group_in_degree_centrality(self):
  218. """
  219. Group in-degree centrality in a DiGraph
  220. """
  221. G = nx.DiGraph()
  222. G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8])
  223. G.add_edges_from(
  224. [(1, 2), (1, 3), (1, 6), (1, 7), (1, 8), (2, 3), (2, 4), (2, 5)]
  225. )
  226. d = nx.group_in_degree_centrality(G, [1, 2])
  227. d_answer = 0
  228. assert d == d_answer
  229. def test_group_out_degree_centrality(self):
  230. """
  231. Group out-degree centrality in a DiGraph
  232. """
  233. G = nx.DiGraph()
  234. G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8])
  235. G.add_edges_from(
  236. [(1, 2), (1, 3), (1, 6), (1, 7), (1, 8), (2, 3), (2, 4), (2, 5)]
  237. )
  238. d = nx.group_out_degree_centrality(G, [1, 2])
  239. d_answer = 1
  240. assert d == d_answer
  241. def test_group_degree_centrality_node_not_in_graph(self):
  242. """
  243. Node(s) in S not in graph, raises NetworkXError
  244. """
  245. with pytest.raises(nx.NetworkXError):
  246. nx.group_degree_centrality(nx.path_graph(5), [6, 7, 8])