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

Code before:
    assert actual_shape_1 == actual_shape_2


def test_csr_bool_indexing():
    data = csr_matrix([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
    list_indices1 = [False, True, False]
    array_indices1 = np.array(list_indices1)
    list_indices2 = [[False, True, False], [False, True, False], [False, True, False]]
    array_indices2 = np.array(list_indices2)
    list_indices3 = ([False, True, False], [False, True, False])
    array_indices3 = (np.array(list_indices3[0]), np.array(list_indices3[1]))
    slice_list1 = data[list_indices1].toarray()
    slice_array1 = data[array_indices1].toarray()
    slice_list2 = data[list_indices2]
    slice_array2 = data[array_indices2]
    slice_list3 = data[list_indices3]
    slice_array3 = data[array_indices3]
    assert (slice_list1 == slice_array1).all()
    assert (slice_list2 == slice_array2).all()
    assert (slice_list3 == slice_array3).all()


@pytest.mark.timeout(2)  # only slow when broken (conversion to 2d index arrays)
@pytest.mark.parametrize("cls", [csr_matrix, csr_array, csc_matrix, csc_array])
def test_fancy_indexing_broadcasts_without_making_dense_2d(cls):
    I = np.arange(46340).reshape((46340, 1))
    J = I.T
    S = cls((46340, 46340))
    # testing nnz, but really testing indexing. Should blow up memory needs
    assert S[I, J].nnz == 0


def test_csr_hstack_int64():
    """
    Tests if hstack properly promotes to indices and indptr arrays to np.int64
    when using np.int32 during concatenation would result in either array
    overflowing.
    """
    max_int32 = np.iinfo(np.int32).max

    # First case: indices would overflow with int32
    data = [1.0]
    row = [0]

    max_indices_1 = max_int32 - 1
    max_indices_2 = 3

    # Individual indices arrays are representable with int32
    col_1 = [max_indices_1 - 1]
    col_2 = [max_indices_2 - 1]


Review comment (bug):
Looking at the CI failure, maybe just add `@pytest.mark.xfail_on_32bit`? We use that all over the place in our testsuite. It looks like some `sparse` tests actually combine that and `check_free_memory()` when they have substantial memory reqs, so maybe using both would be safest to avoid issues long term.

I checked that indeed this test fails before/passes after the patch locally. I'm assuming this method of checking for memory explosion is the one previously agreed upon (I believe there was a very recent discussion about this in another of your PRs).

> It would be nice to have this in a bugfix release since it fixes the behavior that broke in 1.17.0.

Sounds good. We're getting close to 20 merged backport PRs now. `1.17.1` will probably be a pretty big bug fix release, and we may also try to switch over to the separate release repo and do a trusted release.. lots planned there.


Code after:
    assert actual_shape_1 == actual_shape_2


def test_csr_bool_indexing():
    data = csr_matrix([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
    list_indices1 = [False, True, False]
    array_indices1 = np.array(list_indices1)
    list_indices2 = [[False, True, False], [False, True, False], [False, True, False]]
    array_indices2 = np.array(list_indices2)
    list_indices3 = ([False, True, False], [False, True, False])
    array_indices3 = (np.array(list_indices3[0]), np.array(list_indices3[1]))
    slice_list1 = data[list_indices1].toarray()
    slice_array1 = data[array_indices1].toarray()
    slice_list2 = data[list_indices2]
    slice_array2 = data[array_indices2]
    slice_list3 = data[list_indices3]
    slice_array3 = data[array_indices3]
    assert (slice_list1 == slice_array1).all()
    assert (slice_list2 == slice_array2).all()
    assert (slice_list3 == slice_array3).all()


@pytest.mark.xfail_on_32bit("Can't create large array for test")
@pytest.mark.timeout(2)  # only slow when broken (conversion to 2d index arrays)
@pytest.mark.parametrize("cls", [csr_matrix, csr_array, csc_matrix, csc_array])
def test_fancy_indexing_broadcasts_without_making_dense_2d(cls):
    I = np.arange(100_000).reshape((100_000, 1))
    J = I.T
    S = cls((100_000, 100_000))
    # testing nnz, but really testing indexing. Should blow up memory needs
    assert S[I, J].nnz == 0


def test_csr_hstack_int64():
    """
    Tests if hstack properly promotes to indices and indptr arrays to np.int64
    when using np.int32 during concatenation would result in either array
    overflowing.
    """
    max_int32 = np.iinfo(np.int32).max

    # First case: indices would overflow with int32
    data = [1.0]
    row = [0]

    max_indices_1 = max_int32 - 1
    max_indices_2 = 3

    # Individual indices arrays are representable with int32
    col_1 = [max_indices_1 - 1]
    col_2 = [max_indices_2 - 1]