test_graphical.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import pytest
  2. import networkx as nx
  3. def test_valid_degree_sequence1():
  4. n = 100
  5. p = 0.3
  6. for i in range(10):
  7. G = nx.erdos_renyi_graph(n, p)
  8. deg = (d for n, d in G.degree())
  9. assert nx.is_graphical(deg, method="eg")
  10. assert nx.is_graphical(deg, method="hh")
  11. def test_valid_degree_sequence2():
  12. n = 100
  13. for i in range(10):
  14. G = nx.barabasi_albert_graph(n, 1)
  15. deg = (d for n, d in G.degree())
  16. assert nx.is_graphical(deg, method="eg")
  17. assert nx.is_graphical(deg, method="hh")
  18. def test_string_input():
  19. pytest.raises(nx.NetworkXException, nx.is_graphical, [], "foo")
  20. pytest.raises(nx.NetworkXException, nx.is_graphical, ["red"], "hh")
  21. pytest.raises(nx.NetworkXException, nx.is_graphical, ["red"], "eg")
  22. def test_non_integer_input():
  23. pytest.raises(nx.NetworkXException, nx.is_graphical, [72.5], "eg")
  24. pytest.raises(nx.NetworkXException, nx.is_graphical, [72.5], "hh")
  25. def test_negative_input():
  26. assert not nx.is_graphical([-1], "hh")
  27. assert not nx.is_graphical([-1], "eg")
  28. class TestAtlas:
  29. @classmethod
  30. def setup_class(cls):
  31. global atlas
  32. from networkx.generators import atlas
  33. cls.GAG = atlas.graph_atlas_g()
  34. def test_atlas(self):
  35. for graph in self.GAG:
  36. deg = (d for n, d in graph.degree())
  37. assert nx.is_graphical(deg, method="eg")
  38. assert nx.is_graphical(deg, method="hh")
  39. def test_small_graph_true():
  40. z = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
  41. assert nx.is_graphical(z, method="hh")
  42. assert nx.is_graphical(z, method="eg")
  43. z = [10, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2]
  44. assert nx.is_graphical(z, method="hh")
  45. assert nx.is_graphical(z, method="eg")
  46. z = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
  47. assert nx.is_graphical(z, method="hh")
  48. assert nx.is_graphical(z, method="eg")
  49. def test_small_graph_false():
  50. z = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
  51. assert not nx.is_graphical(z, method="hh")
  52. assert not nx.is_graphical(z, method="eg")
  53. z = [6, 5, 4, 4, 2, 1, 1, 1]
  54. assert not nx.is_graphical(z, method="hh")
  55. assert not nx.is_graphical(z, method="eg")
  56. z = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
  57. assert not nx.is_graphical(z, method="hh")
  58. assert not nx.is_graphical(z, method="eg")
  59. def test_directed_degree_sequence():
  60. # Test a range of valid directed degree sequences
  61. n, r = 100, 10
  62. p = 1.0 / r
  63. for i in range(r):
  64. G = nx.erdos_renyi_graph(n, p * (i + 1), None, True)
  65. din = (d for n, d in G.in_degree())
  66. dout = (d for n, d in G.out_degree())
  67. assert nx.is_digraphical(din, dout)
  68. def test_small_directed_sequences():
  69. dout = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
  70. din = [3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1]
  71. assert nx.is_digraphical(din, dout)
  72. # Test nongraphical directed sequence
  73. dout = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
  74. din = [103, 102, 102, 102, 102, 102, 102, 102, 102, 102]
  75. assert not nx.is_digraphical(din, dout)
  76. # Test digraphical small sequence
  77. dout = [1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
  78. din = [2, 2, 2, 2, 2, 2, 2, 2, 1, 1]
  79. assert nx.is_digraphical(din, dout)
  80. # Test nonmatching sum
  81. din = [2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1]
  82. assert not nx.is_digraphical(din, dout)
  83. # Test for negative integer in sequence
  84. din = [2, 2, 2, -2, 2, 2, 2, 2, 1, 1, 4]
  85. assert not nx.is_digraphical(din, dout)
  86. # Test for noninteger
  87. din = dout = [1, 1, 1.1, 1]
  88. assert not nx.is_digraphical(din, dout)
  89. din = dout = [1, 1, "rer", 1]
  90. assert not nx.is_digraphical(din, dout)
  91. def test_multi_sequence():
  92. # Test nongraphical multi sequence
  93. seq = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1]
  94. assert not nx.is_multigraphical(seq)
  95. # Test small graphical multi sequence
  96. seq = [6, 5, 4, 4, 2, 1, 1, 1]
  97. assert nx.is_multigraphical(seq)
  98. # Test for negative integer in sequence
  99. seq = [6, 5, 4, -4, 2, 1, 1, 1]
  100. assert not nx.is_multigraphical(seq)
  101. # Test for sequence with odd sum
  102. seq = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4]
  103. assert not nx.is_multigraphical(seq)
  104. # Test for noninteger
  105. seq = [1, 1, 1.1, 1]
  106. assert not nx.is_multigraphical(seq)
  107. seq = [1, 1, "rer", 1]
  108. assert not nx.is_multigraphical(seq)
  109. def test_pseudo_sequence():
  110. # Test small valid pseudo sequence
  111. seq = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1]
  112. assert nx.is_pseudographical(seq)
  113. # Test for sequence with odd sum
  114. seq = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
  115. assert not nx.is_pseudographical(seq)
  116. # Test for negative integer in sequence
  117. seq = [1000, 3, 3, 3, 3, 2, 2, -2, 1, 1]
  118. assert not nx.is_pseudographical(seq)
  119. # Test for noninteger
  120. seq = [1, 1, 1.1, 1]
  121. assert not nx.is_pseudographical(seq)
  122. seq = [1, 1, "rer", 1]
  123. assert not nx.is_pseudographical(seq)
  124. def test_numpy_degree_sequence():
  125. np = pytest.importorskip("numpy")
  126. ds = np.array([1, 2, 2, 2, 1], dtype=np.int64)
  127. assert nx.is_graphical(ds, "eg")
  128. assert nx.is_graphical(ds, "hh")
  129. ds = np.array([1, 2, 2, 2, 1], dtype=np.float64)
  130. assert nx.is_graphical(ds, "eg")
  131. assert nx.is_graphical(ds, "hh")
  132. ds = np.array([1.1, 2, 2, 2, 1], dtype=np.float64)
  133. pytest.raises(nx.NetworkXException, nx.is_graphical, ds, "eg")
  134. pytest.raises(nx.NetworkXException, nx.is_graphical, ds, "hh")