test_traversal.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import warnings
  2. import numpy as np
  3. import pytest
  4. from numpy.testing import assert_array_almost_equal
  5. from scipy.sparse import csr_array, csr_matrix, coo_array, coo_matrix
  6. from scipy.sparse.csgraph import (breadth_first_tree, depth_first_tree,
  7. csgraph_to_dense, csgraph_from_dense, csgraph_masked_from_dense)
  8. def test_graph_breadth_first():
  9. csgraph = np.array([[0, 1, 2, 0, 0],
  10. [1, 0, 0, 0, 3],
  11. [2, 0, 0, 7, 0],
  12. [0, 0, 7, 0, 1],
  13. [0, 3, 0, 1, 0]])
  14. csgraph = csgraph_from_dense(csgraph, null_value=0)
  15. bfirst = np.array([[0, 1, 2, 0, 0],
  16. [0, 0, 0, 0, 3],
  17. [0, 0, 0, 7, 0],
  18. [0, 0, 0, 0, 0],
  19. [0, 0, 0, 0, 0]])
  20. for directed in [True, False]:
  21. bfirst_test = breadth_first_tree(csgraph, 0, directed)
  22. assert_array_almost_equal(csgraph_to_dense(bfirst_test),
  23. bfirst)
  24. def test_graph_depth_first():
  25. csgraph = np.array([[0, 1, 2, 0, 0],
  26. [1, 0, 0, 0, 3],
  27. [2, 0, 0, 7, 0],
  28. [0, 0, 7, 0, 1],
  29. [0, 3, 0, 1, 0]])
  30. csgraph = csgraph_from_dense(csgraph, null_value=0)
  31. dfirst = np.array([[0, 1, 0, 0, 0],
  32. [0, 0, 0, 0, 3],
  33. [0, 0, 0, 0, 0],
  34. [0, 0, 7, 0, 0],
  35. [0, 0, 0, 1, 0]])
  36. for directed in [True, False]:
  37. dfirst_test = depth_first_tree(csgraph, 0, directed)
  38. assert_array_almost_equal(csgraph_to_dense(dfirst_test), dfirst)
  39. def test_return_type():
  40. from .._laplacian import laplacian
  41. from .._min_spanning_tree import minimum_spanning_tree
  42. np_csgraph = np.array([[0, 1, 2, 0, 0],
  43. [1, 0, 0, 0, 3],
  44. [2, 0, 0, 7, 0],
  45. [0, 0, 7, 0, 1],
  46. [0, 3, 0, 1, 0]])
  47. csgraph = csr_array(np_csgraph)
  48. assert isinstance(laplacian(csgraph), coo_array)
  49. assert isinstance(minimum_spanning_tree(csgraph), csr_array)
  50. for directed in [True, False]:
  51. assert isinstance(depth_first_tree(csgraph, 0, directed), csr_array)
  52. assert isinstance(breadth_first_tree(csgraph, 0, directed), csr_array)
  53. csgraph = csgraph_from_dense(np_csgraph, null_value=0)
  54. assert isinstance(csgraph, csr_array)
  55. assert isinstance(laplacian(csgraph), coo_array)
  56. assert isinstance(minimum_spanning_tree(csgraph), csr_array)
  57. for directed in [True, False]:
  58. assert isinstance(depth_first_tree(csgraph, 0, directed), csr_array)
  59. assert isinstance(breadth_first_tree(csgraph, 0, directed), csr_array)
  60. csgraph = csgraph_masked_from_dense(np_csgraph, null_value=0)
  61. assert isinstance(csgraph, np.ma.MaskedArray)
  62. assert csgraph._baseclass is np.ndarray
  63. # laplacian doesnt work with masked arrays so not here
  64. assert isinstance(minimum_spanning_tree(csgraph), csr_array)
  65. for directed in [True, False]:
  66. assert isinstance(depth_first_tree(csgraph, 0, directed), csr_array)
  67. assert isinstance(breadth_first_tree(csgraph, 0, directed), csr_array)
  68. # start of testing with matrix/spmatrix types
  69. with warnings.catch_warnings():
  70. warnings.filterwarnings("ignore", "the matrix subclass.*", DeprecationWarning)
  71. warnings.filterwarnings(
  72. "ignore", "the matrix subclass.*", PendingDeprecationWarning)
  73. nm_csgraph = np.matrix([[0, 1, 2, 0, 0],
  74. [1, 0, 0, 0, 3],
  75. [2, 0, 0, 7, 0],
  76. [0, 0, 7, 0, 1],
  77. [0, 3, 0, 1, 0]])
  78. csgraph = csr_matrix(nm_csgraph)
  79. assert isinstance(laplacian(csgraph), coo_matrix)
  80. assert isinstance(minimum_spanning_tree(csgraph), csr_matrix)
  81. for directed in [True, False]:
  82. assert isinstance(depth_first_tree(csgraph, 0, directed), csr_matrix)
  83. assert isinstance(breadth_first_tree(csgraph, 0, directed), csr_matrix)
  84. csgraph = csgraph_from_dense(nm_csgraph, null_value=0)
  85. assert isinstance(csgraph, csr_matrix)
  86. assert isinstance(laplacian(csgraph), coo_matrix)
  87. assert isinstance(minimum_spanning_tree(csgraph), csr_matrix)
  88. for directed in [True, False]:
  89. assert isinstance(depth_first_tree(csgraph, 0, directed), csr_matrix)
  90. assert isinstance(breadth_first_tree(csgraph, 0, directed), csr_matrix)
  91. mm_csgraph = csgraph_masked_from_dense(nm_csgraph, null_value=0)
  92. assert isinstance(mm_csgraph, np.ma.MaskedArray)
  93. # laplacian doesnt work with masked arrays so not here
  94. assert isinstance(minimum_spanning_tree(csgraph), csr_matrix)
  95. for directed in [True, False]:
  96. assert isinstance(depth_first_tree(csgraph, 0, directed), csr_matrix)
  97. assert isinstance(breadth_first_tree(csgraph, 0, directed), csr_matrix)
  98. # end of testing with matrix/spmatrix types
  99. def test_graph_breadth_first_trivial_graph():
  100. csgraph = np.array([[0]])
  101. csgraph = csgraph_from_dense(csgraph, null_value=0)
  102. bfirst = np.array([[0]])
  103. for directed in [True, False]:
  104. bfirst_test = breadth_first_tree(csgraph, 0, directed)
  105. assert_array_almost_equal(csgraph_to_dense(bfirst_test), bfirst)
  106. def test_graph_depth_first_trivial_graph():
  107. csgraph = np.array([[0]])
  108. csgraph = csgraph_from_dense(csgraph, null_value=0)
  109. bfirst = np.array([[0]])
  110. for directed in [True, False]:
  111. bfirst_test = depth_first_tree(csgraph, 0, directed)
  112. assert_array_almost_equal(csgraph_to_dense(bfirst_test),
  113. bfirst)
  114. @pytest.mark.parametrize('directed', [True, False])
  115. @pytest.mark.parametrize('tree_func', [breadth_first_tree, depth_first_tree])
  116. def test_int64_indices(tree_func, directed):
  117. # See https://github.com/scipy/scipy/issues/18716
  118. g = csr_array(([1], np.array([[0], [1]], dtype=np.int64)), shape=(2, 2))
  119. assert g.indices.dtype == np.int64
  120. tree = tree_func(g, 0, directed=directed)
  121. assert_array_almost_equal(csgraph_to_dense(tree), [[0, 1], [0, 0]])