test_louvain.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import networkx as nx
  2. def test_modularity_increase():
  3. G = nx.LFR_benchmark_graph(
  4. 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
  5. )
  6. partition = [{u} for u in G.nodes()]
  7. mod = nx.community.modularity(G, partition)
  8. partition = nx.community.louvain_communities(G)
  9. assert nx.community.modularity(G, partition) > mod
  10. def test_valid_partition():
  11. G = nx.LFR_benchmark_graph(
  12. 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
  13. )
  14. H = G.to_directed()
  15. partition = nx.community.louvain_communities(G)
  16. partition2 = nx.community.louvain_communities(H)
  17. assert nx.community.is_partition(G, partition)
  18. assert nx.community.is_partition(H, partition2)
  19. def test_karate_club_partition():
  20. G = nx.karate_club_graph()
  21. part = [
  22. {0, 1, 2, 3, 7, 9, 11, 12, 13, 17, 19, 21},
  23. {16, 4, 5, 6, 10},
  24. {23, 25, 27, 28, 24, 31},
  25. {32, 33, 8, 14, 15, 18, 20, 22, 26, 29, 30},
  26. ]
  27. partition = nx.community.louvain_communities(G, seed=2, weight=None)
  28. assert part == partition
  29. def test_partition_iterator():
  30. G = nx.path_graph(15)
  31. parts_iter = nx.community.louvain_partitions(G, seed=42)
  32. first_part = next(parts_iter)
  33. first_copy = [s.copy() for s in first_part]
  34. # gh-5901 reports sets changing after next partition is yielded
  35. assert first_copy[0] == first_part[0]
  36. second_part = next(parts_iter)
  37. assert first_copy[0] == first_part[0]
  38. def test_directed_partition():
  39. """
  40. Test 2 cases that were looping infinitely
  41. from issues #5175 and #5704
  42. """
  43. G = nx.DiGraph()
  44. H = nx.DiGraph()
  45. G.add_nodes_from(range(10))
  46. H.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
  47. G_edges = [
  48. (0, 2),
  49. (0, 1),
  50. (1, 0),
  51. (2, 1),
  52. (2, 0),
  53. (3, 4),
  54. (4, 3),
  55. (7, 8),
  56. (8, 7),
  57. (9, 10),
  58. (10, 9),
  59. ]
  60. H_edges = [
  61. (1, 2),
  62. (1, 6),
  63. (1, 9),
  64. (2, 3),
  65. (2, 4),
  66. (2, 5),
  67. (3, 4),
  68. (4, 3),
  69. (4, 5),
  70. (5, 4),
  71. (6, 7),
  72. (6, 8),
  73. (9, 10),
  74. (9, 11),
  75. (10, 11),
  76. (11, 10),
  77. ]
  78. G.add_edges_from(G_edges)
  79. H.add_edges_from(H_edges)
  80. G_expected_partition = [{0, 1, 2}, {3, 4}, {5}, {6}, {8, 7}, {9, 10}]
  81. G_partition = nx.community.louvain_communities(G, seed=123, weight=None)
  82. H_expected_partition = [{2, 3, 4, 5}, {8, 1, 6, 7}, {9, 10, 11}]
  83. H_partition = nx.community.louvain_communities(H, seed=123, weight=None)
  84. assert G_partition == G_expected_partition
  85. assert H_partition == H_expected_partition
  86. def test_none_weight_param():
  87. G = nx.karate_club_graph()
  88. nx.set_edge_attributes(
  89. G, {edge: i * i for i, edge in enumerate(G.edges)}, name="foo"
  90. )
  91. part = [
  92. {0, 1, 2, 3, 7, 9, 11, 12, 13, 17, 19, 21},
  93. {16, 4, 5, 6, 10},
  94. {23, 25, 27, 28, 24, 31},
  95. {32, 33, 8, 14, 15, 18, 20, 22, 26, 29, 30},
  96. ]
  97. partition1 = nx.community.louvain_communities(G, weight=None, seed=2)
  98. partition2 = nx.community.louvain_communities(G, weight="foo", seed=2)
  99. partition3 = nx.community.louvain_communities(G, weight="weight", seed=2)
  100. assert part == partition1
  101. assert part != partition2
  102. assert part != partition3
  103. assert partition2 != partition3
  104. def test_quality():
  105. G = nx.LFR_benchmark_graph(
  106. 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
  107. )
  108. H = nx.gn_graph(200, seed=1234)
  109. I = nx.MultiGraph(G)
  110. J = nx.MultiDiGraph(H)
  111. partition = nx.community.louvain_communities(G)
  112. partition2 = nx.community.louvain_communities(H)
  113. partition3 = nx.community.louvain_communities(I)
  114. partition4 = nx.community.louvain_communities(J)
  115. quality = nx.community.partition_quality(G, partition)[0]
  116. quality2 = nx.community.partition_quality(H, partition2)[0]
  117. quality3 = nx.community.partition_quality(I, partition3)[0]
  118. quality4 = nx.community.partition_quality(J, partition4)[0]
  119. assert quality >= 0.65
  120. assert quality2 >= 0.65
  121. assert quality3 >= 0.65
  122. assert quality4 >= 0.65
  123. def test_multigraph():
  124. G = nx.karate_club_graph()
  125. H = nx.MultiGraph(G)
  126. G.add_edge(0, 1, weight=10)
  127. H.add_edge(0, 1, weight=9)
  128. G.add_edge(0, 9, foo=20)
  129. H.add_edge(0, 9, foo=20)
  130. partition1 = nx.community.louvain_communities(G, seed=1234)
  131. partition2 = nx.community.louvain_communities(H, seed=1234)
  132. partition3 = nx.community.louvain_communities(H, weight="foo", seed=1234)
  133. assert partition1 == partition2 != partition3
  134. def test_resolution():
  135. G = nx.LFR_benchmark_graph(
  136. 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
  137. )
  138. partition1 = nx.community.louvain_communities(G, resolution=0.5, seed=12)
  139. partition2 = nx.community.louvain_communities(G, seed=12)
  140. partition3 = nx.community.louvain_communities(G, resolution=2, seed=12)
  141. assert len(partition1) <= len(partition2) <= len(partition3)
  142. def test_threshold():
  143. G = nx.LFR_benchmark_graph(
  144. 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
  145. )
  146. partition1 = nx.community.louvain_communities(G, threshold=0.3, seed=2)
  147. partition2 = nx.community.louvain_communities(G, seed=2)
  148. mod1 = nx.community.modularity(G, partition1)
  149. mod2 = nx.community.modularity(G, partition2)
  150. assert mod1 < mod2