test_sputils.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. """unit tests for sparse utility functions"""
  2. import numpy as np
  3. from numpy.testing import assert_equal
  4. from pytest import raises as assert_raises
  5. from scipy.sparse import _sputils as sputils
  6. from scipy.sparse._sputils import matrix
  7. class TestSparseUtils:
  8. def test_upcast(self):
  9. assert_equal(sputils.upcast('intc'), np.intc)
  10. assert_equal(sputils.upcast('int32', 'float32'), np.float64)
  11. assert_equal(sputils.upcast('bool', complex, float), np.complex128)
  12. assert_equal(sputils.upcast('i', 'd'), np.float64)
  13. def test_getdtype(self):
  14. A = np.array([1], dtype='int8')
  15. assert_equal(sputils.getdtype(None, default=float), float)
  16. assert_equal(sputils.getdtype(None, a=A), np.int8)
  17. with assert_raises(
  18. ValueError,
  19. match="object dtype is not supported by sparse matrices",
  20. ):
  21. sputils.getdtype("O")
  22. def test_isscalarlike(self):
  23. assert_equal(sputils.isscalarlike(3.0), True)
  24. assert_equal(sputils.isscalarlike(-4), True)
  25. assert_equal(sputils.isscalarlike(2.5), True)
  26. assert_equal(sputils.isscalarlike(1 + 3j), True)
  27. assert_equal(sputils.isscalarlike(np.array(3)), True)
  28. assert_equal(sputils.isscalarlike("16"), True)
  29. assert_equal(sputils.isscalarlike(np.array([3])), False)
  30. assert_equal(sputils.isscalarlike([[3]]), False)
  31. assert_equal(sputils.isscalarlike((1,)), False)
  32. assert_equal(sputils.isscalarlike((1, 2)), False)
  33. def test_isintlike(self):
  34. assert_equal(sputils.isintlike(-4), True)
  35. assert_equal(sputils.isintlike(np.array(3)), True)
  36. assert_equal(sputils.isintlike(np.array([3])), False)
  37. with assert_raises(
  38. ValueError,
  39. match="Inexact indices into sparse matrices are not allowed"
  40. ):
  41. sputils.isintlike(3.0)
  42. assert_equal(sputils.isintlike(2.5), False)
  43. assert_equal(sputils.isintlike(1 + 3j), False)
  44. assert_equal(sputils.isintlike((1,)), False)
  45. assert_equal(sputils.isintlike((1, 2)), False)
  46. def test_isshape(self):
  47. assert_equal(sputils.isshape((1, 2)), True)
  48. assert_equal(sputils.isshape((5, 2)), True)
  49. assert_equal(sputils.isshape((1.5, 2)), False)
  50. assert_equal(sputils.isshape((2, 2, 2)), False)
  51. assert_equal(sputils.isshape(([2], 2)), False)
  52. assert_equal(sputils.isshape((-1, 2), nonneg=False),True)
  53. assert_equal(sputils.isshape((2, -1), nonneg=False),True)
  54. assert_equal(sputils.isshape((-1, 2), nonneg=True),False)
  55. assert_equal(sputils.isshape((2, -1), nonneg=True),False)
  56. def test_issequence(self):
  57. assert_equal(sputils.issequence((1,)), True)
  58. assert_equal(sputils.issequence((1, 2, 3)), True)
  59. assert_equal(sputils.issequence([1]), True)
  60. assert_equal(sputils.issequence([1, 2, 3]), True)
  61. assert_equal(sputils.issequence(np.array([1, 2, 3])), True)
  62. assert_equal(sputils.issequence(np.array([[1], [2], [3]])), False)
  63. assert_equal(sputils.issequence(3), False)
  64. def test_ismatrix(self):
  65. assert_equal(sputils.ismatrix(((),)), True)
  66. assert_equal(sputils.ismatrix([[1], [2]]), True)
  67. assert_equal(sputils.ismatrix(np.arange(3)[None]), True)
  68. assert_equal(sputils.ismatrix([1, 2]), False)
  69. assert_equal(sputils.ismatrix(np.arange(3)), False)
  70. assert_equal(sputils.ismatrix([[[1]]]), False)
  71. assert_equal(sputils.ismatrix(3), False)
  72. def test_isdense(self):
  73. assert_equal(sputils.isdense(np.array([1])), True)
  74. assert_equal(sputils.isdense(matrix([1])), True)
  75. def test_validateaxis(self):
  76. assert_raises(TypeError, sputils.validateaxis, (0, 1))
  77. assert_raises(TypeError, sputils.validateaxis, 1.5)
  78. assert_raises(ValueError, sputils.validateaxis, 3)
  79. # These function calls should not raise errors
  80. for axis in (-2, -1, 0, 1, None):
  81. sputils.validateaxis(axis)
  82. def test_get_index_dtype(self):
  83. imax = np.int64(np.iinfo(np.int32).max)
  84. too_big = imax + 1
  85. # Check that uint32's with no values too large doesn't return
  86. # int64
  87. a1 = np.ones(90, dtype='uint32')
  88. a2 = np.ones(90, dtype='uint32')
  89. assert_equal(
  90. np.dtype(sputils.get_index_dtype((a1, a2), check_contents=True)),
  91. np.dtype('int32')
  92. )
  93. # Check that if we can not convert but all values are less than or
  94. # equal to max that we can just convert to int32
  95. a1[-1] = imax
  96. assert_equal(
  97. np.dtype(sputils.get_index_dtype((a1, a2), check_contents=True)),
  98. np.dtype('int32')
  99. )
  100. # Check that if it can not convert directly and the contents are
  101. # too large that we return int64
  102. a1[-1] = too_big
  103. assert_equal(
  104. np.dtype(sputils.get_index_dtype((a1, a2), check_contents=True)),
  105. np.dtype('int64')
  106. )
  107. # test that if can not convert and didn't specify to check_contents
  108. # we return int64
  109. a1 = np.ones(89, dtype='uint32')
  110. a2 = np.ones(89, dtype='uint32')
  111. assert_equal(
  112. np.dtype(sputils.get_index_dtype((a1, a2))),
  113. np.dtype('int64')
  114. )
  115. # Check that even if we have arrays that can be converted directly
  116. # that if we specify a maxval directly it takes precedence
  117. a1 = np.ones(12, dtype='uint32')
  118. a2 = np.ones(12, dtype='uint32')
  119. assert_equal(
  120. np.dtype(sputils.get_index_dtype(
  121. (a1, a2), maxval=too_big, check_contents=True
  122. )),
  123. np.dtype('int64')
  124. )
  125. # Check that an array with a too max size and maxval set
  126. # still returns int64
  127. a1[-1] = too_big
  128. assert_equal(
  129. np.dtype(sputils.get_index_dtype((a1, a2), maxval=too_big)),
  130. np.dtype('int64')
  131. )
  132. def test_check_shape_overflow(self):
  133. new_shape = sputils.check_shape([(10, -1)], (65535, 131070))
  134. assert_equal(new_shape, (10, 858967245))
  135. def test_matrix(self):
  136. a = [[1, 2, 3]]
  137. b = np.array(a)
  138. assert isinstance(sputils.matrix(a), np.matrix)
  139. assert isinstance(sputils.matrix(b), np.matrix)
  140. c = sputils.matrix(b)
  141. c[:, :] = 123
  142. assert_equal(b, a)
  143. c = sputils.matrix(b, copy=False)
  144. c[:, :] = 123
  145. assert_equal(b, [[123, 123, 123]])
  146. def test_asmatrix(self):
  147. a = [[1, 2, 3]]
  148. b = np.array(a)
  149. assert isinstance(sputils.asmatrix(a), np.matrix)
  150. assert isinstance(sputils.asmatrix(b), np.matrix)
  151. c = sputils.asmatrix(b)
  152. c[:, :] = 123
  153. assert_equal(b, [[123, 123, 123]])