test_reordering.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from __future__ import division, print_function, absolute_import
  2. import numpy as np
  3. from numpy.testing import assert_equal
  4. from scipy.sparse.csgraph import (reverse_cuthill_mckee,
  5. maximum_bipartite_matching, structural_rank)
  6. from scipy.sparse import diags, csc_matrix, csr_matrix, coo_matrix
  7. def test_graph_reverse_cuthill_mckee():
  8. A = np.array([[1, 0, 0, 0, 1, 0, 0, 0],
  9. [0, 1, 1, 0, 0, 1, 0, 1],
  10. [0, 1, 1, 0, 1, 0, 0, 0],
  11. [0, 0, 0, 1, 0, 0, 1, 0],
  12. [1, 0, 1, 0, 1, 0, 0, 0],
  13. [0, 1, 0, 0, 0, 1, 0, 1],
  14. [0, 0, 0, 1, 0, 0, 1, 0],
  15. [0, 1, 0, 0, 0, 1, 0, 1]], dtype=int)
  16. graph = csr_matrix(A)
  17. perm = reverse_cuthill_mckee(graph)
  18. correct_perm = np.array([6, 3, 7, 5, 1, 2, 4, 0])
  19. assert_equal(perm, correct_perm)
  20. # Test int64 indices input
  21. graph.indices = graph.indices.astype('int64')
  22. graph.indptr = graph.indptr.astype('int64')
  23. perm = reverse_cuthill_mckee(graph, True)
  24. assert_equal(perm, correct_perm)
  25. def test_graph_reverse_cuthill_mckee_ordering():
  26. data = np.ones(63,dtype=int)
  27. rows = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2,
  28. 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
  29. 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9,
  30. 9, 10, 10, 10, 10, 10, 11, 11, 11, 11,
  31. 12, 12, 12, 13, 13, 13, 13, 14, 14, 14,
  32. 14, 15, 15, 15, 15, 15])
  33. cols = np.array([0, 2, 5, 8, 10, 1, 3, 9, 11, 0, 2,
  34. 7, 10, 1, 3, 11, 4, 6, 12, 14, 0, 7, 13,
  35. 15, 4, 6, 14, 2, 5, 7, 15, 0, 8, 10, 13,
  36. 1, 9, 11, 0, 2, 8, 10, 15, 1, 3, 9, 11,
  37. 4, 12, 14, 5, 8, 13, 15, 4, 6, 12, 14,
  38. 5, 7, 10, 13, 15])
  39. graph = coo_matrix((data, (rows,cols))).tocsr()
  40. perm = reverse_cuthill_mckee(graph)
  41. correct_perm = np.array([12, 14, 4, 6, 10, 8, 2, 15,
  42. 0, 13, 7, 5, 9, 11, 1, 3])
  43. assert_equal(perm, correct_perm)
  44. def test_graph_maximum_bipartite_matching():
  45. A = diags(np.ones(25), offsets=0, format='csc')
  46. rand_perm = np.random.permutation(25)
  47. rand_perm2 = np.random.permutation(25)
  48. Rrow = np.arange(25)
  49. Rcol = rand_perm
  50. Rdata = np.ones(25,dtype=int)
  51. Rmat = coo_matrix((Rdata,(Rrow,Rcol))).tocsc()
  52. Crow = rand_perm2
  53. Ccol = np.arange(25)
  54. Cdata = np.ones(25,dtype=int)
  55. Cmat = coo_matrix((Cdata,(Crow,Ccol))).tocsc()
  56. # Randomly permute identity matrix
  57. B = Rmat*A*Cmat
  58. # Row permute
  59. perm = maximum_bipartite_matching(B,perm_type='row')
  60. Rrow = np.arange(25)
  61. Rcol = perm
  62. Rdata = np.ones(25,dtype=int)
  63. Rmat = coo_matrix((Rdata,(Rrow,Rcol))).tocsc()
  64. C1 = Rmat*B
  65. # Column permute
  66. perm2 = maximum_bipartite_matching(B,perm_type='column')
  67. Crow = perm2
  68. Ccol = np.arange(25)
  69. Cdata = np.ones(25,dtype=int)
  70. Cmat = coo_matrix((Cdata,(Crow,Ccol))).tocsc()
  71. C2 = B*Cmat
  72. # Should get identity matrix back
  73. assert_equal(any(C1.diagonal() == 0), False)
  74. assert_equal(any(C2.diagonal() == 0), False)
  75. # Test int64 indices input
  76. B.indices = B.indices.astype('int64')
  77. B.indptr = B.indptr.astype('int64')
  78. perm = maximum_bipartite_matching(B,perm_type='row')
  79. Rrow = np.arange(25)
  80. Rcol = perm
  81. Rdata = np.ones(25,dtype=int)
  82. Rmat = coo_matrix((Rdata,(Rrow,Rcol))).tocsc()
  83. C3 = Rmat*B
  84. assert_equal(any(C3.diagonal() == 0), False)
  85. def test_graph_structural_rank():
  86. # Test square matrix #1
  87. A = csc_matrix([[1, 1, 0],
  88. [1, 0, 1],
  89. [0, 1, 0]])
  90. assert_equal(structural_rank(A), 3)
  91. # Test square matrix #2
  92. rows = np.array([0,0,0,0,0,1,1,2,2,3,3,3,3,3,3,4,4,5,5,6,6,7,7])
  93. cols = np.array([0,1,2,3,4,2,5,2,6,0,1,3,5,6,7,4,5,5,6,2,6,2,4])
  94. data = np.ones_like(rows)
  95. B = coo_matrix((data,(rows,cols)), shape=(8,8))
  96. assert_equal(structural_rank(B), 6)
  97. #Test non-square matrix
  98. C = csc_matrix([[1, 0, 2, 0],
  99. [2, 0, 4, 0]])
  100. assert_equal(structural_rank(C), 2)
  101. #Test tall matrix
  102. assert_equal(structural_rank(C.T), 2)