Repository: scipy/scipy
File: scipy/sparse/tests/test_csc.py (Python)

Code before:

    if ax is None:
        # idx is a single flattened index
        # that we need to convert to a 2d index pair;
        # can't do this with np.unravel_index because
        # the dimensions are too large
        ii = idx % dim[0]
        jj = idx // dim[0]
    else:
        # idx is an array of size of A.shape[ax];
        # check the max index to make sure no overflows
        # we encountered
        assert np.count_nonzero(idx) == A.nnz
        ii, jj = np.max(idx), np.argmax(idx)

    assert A[ii, jj] == A[-2, -2]


# broken in 1.16: comparison, truediv, maximum, minimum.
#   truediv becomes dense for floats so has always broken with this kind of shape
@pytest.mark.timeout(2)  # only slow when broken (when spurious conversion occurs)
@pytest.mark.parametrize("op", (operator.ne, operator.lt, operator.gt, operator.mul,
                                operator.mul, operator.add, operator.sub,
                                # truediv, eq, ge, le make dense output so not tested
                                lambda x, y: x.minimum(y), lambda x, y: x.maximum(y)))
def test_compressed_rc_conversion_mixup(op):
    num_minor_axis = np.iinfo(np.uint32).max + 1
    minor_axis_index = np.array([num_minor_axis - 1])
    major_axis_index = np.array([10])
    row_cols = (minor_axis_index, major_axis_index)
    col_rows = (major_axis_index, minor_axis_index)

    X = csc_array((np.array([10]), row_cols), shape=(num_minor_axis, 20))
    X_2 = X.copy()
    # causes timeout error upon large memory alloc only if conversion to CSR occurs
    op(X_2, X)

    Z = csr_array((np.array([10]), col_rows), shape=(20, num_minor_axis))
    Z_2 = Z.copy()
    # causes timeout error upon large memory alloc only if conversion to CSC occurs
    op(Z_2, Z)


Review comment (bug):
For reference, when I revert the source patch this test does indeed fail this way, but takes a while--`5 failed, 15 passed in 58.05s` (via `spin test -t scipy/sparse/tests/test_csc.py`).

I tried using `pytest-memray` with `@pytest.mark.limit_memory("200 MB")` instead of the timeout as suggested in the ticket, but it was even slower--indeed, I lost patience and hit Ctrl+C after two tests reported an `M` status (presumably memory failures). I like the idea of failing a test on the basis of its apparent space complexity rather than the timing, but in practice this seems to be very slow rather than quickly terminating the in-progress allocation of mem. If it were really fast it might be worth considering that plugin, but in my hands it was way too sluggish. I suppose adding more fixtures/plugins also creates complexity for free-threading support...


Code after:
    dim = (100000, 100000)
    A = lil_matrix(dim)
    A[-2, -2] = 42
    A[-3, -3] = 0.1234
    A = csc_matrix(A)
    idx = A.argmax(axis=ax)

    if ax is None:
        # idx is a single flattened index
        # that we need to convert to a 2d index pair;
        # can't do this with np.unravel_index because
        # the dimensions are too large
        ii = idx % dim[0]
        jj = idx // dim[0]
    else:
        # idx is an array of size of A.shape[ax];
        # check the max index to make sure no overflows
        # we encountered
        assert np.count_nonzero(idx) == A.nnz
        ii, jj = np.max(idx), np.argmax(idx)

    assert A[ii, jj] == A[-2, -2]


@pytest.mark.skipif(not LINUX_INTEL, reason="avoid variations due to OS, see gh-23826")
@pytest.mark.timeout(2)  # only slow when broken (when spurious conversion occurs)
@pytest.mark.parametrize("op", (operator.ne, operator.lt, operator.gt,
                                operator.add, operator.sub, operator.mul,
                                # truediv, eq, ge, le make dense output so not tested
                                lambda x, y: x.minimum(y), lambda x, y: x.maximum(y)))
def test_compressed_rc_conversion_mixup(op):
    # see gh-23826 for related discussion
    num_minor_axis = np.iinfo(np.uint32).max + 1
    minor_axis_index = np.array([num_minor_axis - 1])
    major_axis_index = np.array([10])
    row_cols = (minor_axis_index, major_axis_index)
    col_rows = (major_axis_index, minor_axis_index)

    X = csc_array((np.array([10]), row_cols), shape=(num_minor_axis, 20))
    X_2 = X.copy()
    # causes timeout error upon large memory alloc only if conversion to CSR occurs
    op(X_2, X)

    Z = csr_array((np.array([10]), col_rows), shape=(20, num_minor_axis))
    Z_2 = Z.copy()
    # causes timeout error upon large memory alloc only if conversion to CSC occurs
    op(Z_2, Z)