test_csc.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import operator
  2. import sys
  3. import platform
  4. import numpy as np
  5. from numpy.testing import assert_array_almost_equal, assert_
  6. from scipy.sparse import csr_matrix, csc_matrix, lil_matrix, csr_array, csc_array
  7. import pytest
  8. LINUX_INTEL = (sys.platform == 'linux') and (platform.machine() == 'x86_64')
  9. def test_csc_getrow():
  10. N = 10
  11. np.random.seed(0)
  12. X = np.random.random((N, N))
  13. X[X > 0.7] = 0
  14. Xcsc = csc_matrix(X)
  15. for i in range(N):
  16. arr_row = X[i:i + 1, :]
  17. csc_row = Xcsc.getrow(i)
  18. assert_array_almost_equal(arr_row, csc_row.toarray())
  19. assert_(type(csc_row) is csr_matrix)
  20. def test_csc_getcol():
  21. N = 10
  22. np.random.seed(0)
  23. X = np.random.random((N, N))
  24. X[X > 0.7] = 0
  25. Xcsc = csc_matrix(X)
  26. for i in range(N):
  27. arr_col = X[:, i:i + 1]
  28. csc_col = Xcsc.getcol(i)
  29. assert_array_almost_equal(arr_col, csc_col.toarray())
  30. assert_(type(csc_col) is csc_matrix)
  31. @pytest.mark.parametrize("matrix_input, axis, expected_shape",
  32. [(csc_matrix([[1, 0],
  33. [0, 0],
  34. [0, 2]]),
  35. 0, (0, 2)),
  36. (csc_matrix([[1, 0],
  37. [0, 0],
  38. [0, 2]]),
  39. 1, (3, 0)),
  40. (csc_matrix([[1, 0],
  41. [0, 0],
  42. [0, 2]]),
  43. 'both', (0, 0)),
  44. (csc_matrix([[0, 1, 0, 0, 0, 0],
  45. [0, 0, 0, 0, 0, 0],
  46. [0, 0, 2, 3, 0, 1]]),
  47. 0, (0, 6))])
  48. def test_csc_empty_slices(matrix_input, axis, expected_shape):
  49. # see gh-11127 for related discussion
  50. slice_1 = matrix_input.toarray().shape[0] - 1
  51. slice_2 = slice_1
  52. slice_3 = slice_2 - 1
  53. if axis == 0:
  54. actual_shape_1 = matrix_input[slice_1:slice_2, :].toarray().shape
  55. actual_shape_2 = matrix_input[slice_1:slice_3, :].toarray().shape
  56. elif axis == 1:
  57. actual_shape_1 = matrix_input[:, slice_1:slice_2].toarray().shape
  58. actual_shape_2 = matrix_input[:, slice_1:slice_3].toarray().shape
  59. elif axis == 'both':
  60. actual_shape_1 = matrix_input[slice_1:slice_2, slice_1:slice_2].toarray().shape
  61. actual_shape_2 = matrix_input[slice_1:slice_3, slice_1:slice_3].toarray().shape
  62. assert actual_shape_1 == expected_shape
  63. assert actual_shape_1 == actual_shape_2
  64. @pytest.mark.parametrize('ax', (-2, -1, 0, 1, None))
  65. def test_argmax_overflow(ax):
  66. # See gh-13646: Windows integer overflow for large sparse matrices.
  67. dim = (100000, 100000)
  68. A = lil_matrix(dim)
  69. A[-2, -2] = 42
  70. A[-3, -3] = 0.1234
  71. A = csc_matrix(A)
  72. idx = A.argmax(axis=ax)
  73. if ax is None:
  74. # idx is a single flattened index
  75. # that we need to convert to a 2d index pair;
  76. # can't do this with np.unravel_index because
  77. # the dimensions are too large
  78. ii = idx % dim[0]
  79. jj = idx // dim[0]
  80. else:
  81. # idx is an array of size of A.shape[ax];
  82. # check the max index to make sure no overflows
  83. # we encountered
  84. assert np.count_nonzero(idx) == A.nnz
  85. ii, jj = np.max(idx), np.argmax(idx)
  86. assert A[ii, jj] == A[-2, -2]
  87. @pytest.mark.skipif(not LINUX_INTEL, reason="avoid variations due to OS, see gh-23826")
  88. @pytest.mark.timeout(2) # only slow when broken (when spurious conversion occurs)
  89. @pytest.mark.parametrize("op", (operator.ne, operator.lt, operator.gt,
  90. operator.add, operator.sub, operator.mul,
  91. # truediv, eq, ge, le make dense output so not tested
  92. lambda x, y: x.minimum(y), lambda x, y: x.maximum(y)))
  93. def test_compressed_rc_conversion_mixup(op):
  94. # see gh-23826 for related discussion
  95. num_minor_axis = np.iinfo(np.uint32).max + 1
  96. minor_axis_index = np.array([num_minor_axis - 1])
  97. major_axis_index = np.array([10])
  98. row_cols = (minor_axis_index, major_axis_index)
  99. col_rows = (major_axis_index, minor_axis_index)
  100. X = csc_array((np.array([10]), row_cols), shape=(num_minor_axis, 20))
  101. X_2 = X.copy()
  102. # causes timeout error upon large memory alloc only if conversion to CSR occurs
  103. op(X_2, X)
  104. Z = csr_array((np.array([10]), col_rows), shape=(20, num_minor_axis))
  105. Z_2 = Z.copy()
  106. # causes timeout error upon large memory alloc only if conversion to CSC occurs
  107. op(Z_2, Z)