test_cluster.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import pytest
  2. import networkx as nx
  3. from networkx.algorithms import bipartite
  4. from networkx.algorithms.bipartite.cluster import cc_dot, cc_max, cc_min
  5. def test_pairwise_bipartite_cc_functions():
  6. # Test functions for different kinds of bipartite clustering coefficients
  7. # between pairs of nodes using 3 example graphs from figure 5 p. 40
  8. # Latapy et al (2008)
  9. G1 = nx.Graph([(0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 5), (1, 6), (1, 7)])
  10. G2 = nx.Graph([(0, 2), (0, 3), (0, 4), (1, 3), (1, 4), (1, 5)])
  11. G3 = nx.Graph(
  12. [(0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9)]
  13. )
  14. result = {
  15. 0: [1 / 3.0, 2 / 3.0, 2 / 5.0],
  16. 1: [1 / 2.0, 2 / 3.0, 2 / 3.0],
  17. 2: [2 / 8.0, 2 / 5.0, 2 / 5.0],
  18. }
  19. for i, G in enumerate([G1, G2, G3]):
  20. assert bipartite.is_bipartite(G)
  21. assert cc_dot(set(G[0]), set(G[1])) == result[i][0]
  22. assert cc_min(set(G[0]), set(G[1])) == result[i][1]
  23. assert cc_max(set(G[0]), set(G[1])) == result[i][2]
  24. def test_star_graph():
  25. G = nx.star_graph(3)
  26. # all modes are the same
  27. answer = {0: 0, 1: 1, 2: 1, 3: 1}
  28. assert bipartite.clustering(G, mode="dot") == answer
  29. assert bipartite.clustering(G, mode="min") == answer
  30. assert bipartite.clustering(G, mode="max") == answer
  31. def test_not_bipartite():
  32. with pytest.raises(nx.NetworkXError):
  33. bipartite.clustering(nx.complete_graph(4))
  34. def test_bad_mode():
  35. with pytest.raises(nx.NetworkXError):
  36. bipartite.clustering(nx.path_graph(4), mode="foo")
  37. def test_path_graph():
  38. G = nx.path_graph(4)
  39. answer = {0: 0.5, 1: 0.5, 2: 0.5, 3: 0.5}
  40. assert bipartite.clustering(G, mode="dot") == answer
  41. assert bipartite.clustering(G, mode="max") == answer
  42. answer = {0: 1, 1: 1, 2: 1, 3: 1}
  43. assert bipartite.clustering(G, mode="min") == answer
  44. def test_average_path_graph():
  45. G = nx.path_graph(4)
  46. assert bipartite.average_clustering(G, mode="dot") == 0.5
  47. assert bipartite.average_clustering(G, mode="max") == 0.5
  48. assert bipartite.average_clustering(G, mode="min") == 1
  49. def test_ra_clustering_davis():
  50. G = nx.davis_southern_women_graph()
  51. cc4 = round(bipartite.robins_alexander_clustering(G), 3)
  52. assert cc4 == 0.468
  53. def test_ra_clustering_square():
  54. G = nx.path_graph(4)
  55. G.add_edge(0, 3)
  56. assert bipartite.robins_alexander_clustering(G) == 1.0
  57. def test_ra_clustering_zero():
  58. G = nx.Graph()
  59. assert bipartite.robins_alexander_clustering(G) == 0
  60. G.add_nodes_from(range(4))
  61. assert bipartite.robins_alexander_clustering(G) == 0
  62. G.add_edges_from([(0, 1), (2, 3), (3, 4)])
  63. assert bipartite.robins_alexander_clustering(G) == 0
  64. G.add_edge(1, 2)
  65. assert bipartite.robins_alexander_clustering(G) == 0