test_csc.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import numpy as np
  2. from numpy.testing import assert_array_almost_equal, assert_
  3. from scipy.sparse import csr_matrix, csc_matrix, lil_matrix
  4. import pytest
  5. def test_csc_getrow():
  6. N = 10
  7. np.random.seed(0)
  8. X = np.random.random((N, N))
  9. X[X > 0.7] = 0
  10. Xcsc = csc_matrix(X)
  11. for i in range(N):
  12. arr_row = X[i:i + 1, :]
  13. csc_row = Xcsc.getrow(i)
  14. assert_array_almost_equal(arr_row, csc_row.toarray())
  15. assert_(type(csc_row) is csr_matrix)
  16. def test_csc_getcol():
  17. N = 10
  18. np.random.seed(0)
  19. X = np.random.random((N, N))
  20. X[X > 0.7] = 0
  21. Xcsc = csc_matrix(X)
  22. for i in range(N):
  23. arr_col = X[:, i:i + 1]
  24. csc_col = Xcsc.getcol(i)
  25. assert_array_almost_equal(arr_col, csc_col.toarray())
  26. assert_(type(csc_col) is csc_matrix)
  27. @pytest.mark.parametrize("matrix_input, axis, expected_shape",
  28. [(csc_matrix([[1, 0],
  29. [0, 0],
  30. [0, 2]]),
  31. 0, (0, 2)),
  32. (csc_matrix([[1, 0],
  33. [0, 0],
  34. [0, 2]]),
  35. 1, (3, 0)),
  36. (csc_matrix([[1, 0],
  37. [0, 0],
  38. [0, 2]]),
  39. 'both', (0, 0)),
  40. (csc_matrix([[0, 1, 0, 0, 0, 0],
  41. [0, 0, 0, 0, 0, 0],
  42. [0, 0, 2, 3, 0, 1]]),
  43. 0, (0, 6))])
  44. def test_csc_empty_slices(matrix_input, axis, expected_shape):
  45. # see gh-11127 for related discussion
  46. slice_1 = matrix_input.A.shape[0] - 1
  47. slice_2 = slice_1
  48. slice_3 = slice_2 - 1
  49. if axis == 0:
  50. actual_shape_1 = matrix_input[slice_1:slice_2, :].A.shape
  51. actual_shape_2 = matrix_input[slice_1:slice_3, :].A.shape
  52. elif axis == 1:
  53. actual_shape_1 = matrix_input[:, slice_1:slice_2].A.shape
  54. actual_shape_2 = matrix_input[:, slice_1:slice_3].A.shape
  55. elif axis == 'both':
  56. actual_shape_1 = matrix_input[slice_1:slice_2, slice_1:slice_2].A.shape
  57. actual_shape_2 = matrix_input[slice_1:slice_3, slice_1:slice_3].A.shape
  58. assert actual_shape_1 == expected_shape
  59. assert actual_shape_1 == actual_shape_2
  60. @pytest.mark.parametrize('ax', (-2, -1, 0, 1, None))
  61. def test_argmax_overflow(ax):
  62. # See gh-13646: Windows integer overflow for large sparse matrices.
  63. dim = (100000, 100000)
  64. A = lil_matrix(dim)
  65. A[-2, -2] = 42
  66. A[-3, -3] = 0.1234
  67. A = csc_matrix(A)
  68. idx = A.argmax(axis=ax)
  69. if ax is None:
  70. # idx is a single flattened index
  71. # that we need to convert to a 2d index pair;
  72. # can't do this with np.unravel_index because
  73. # the dimensions are too large
  74. ii = idx % dim[0]
  75. jj = idx // dim[0]
  76. else:
  77. # idx is an array of size of A.shape[ax];
  78. # check the max index to make sure no overflows
  79. # we encountered
  80. assert np.count_nonzero(idx) == A.nnz
  81. ii, jj = np.max(idx), np.argmax(idx)
  82. assert A[ii, jj] == A[-2, -2]