Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/anndata/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,11 @@ def assert_equal_sparse(
exact: bool = False,
elem_name: str | None = None,
):
if exact and sparse.issparse(b) and hasattr(a, "indptr") and hasattr(b, "indptr"):
assert a.indptr.dtype == b.indptr.dtype, f"{elem_name}: indptr dtype mismatch"
assert a.indices.dtype == b.indices.dtype, (
f"{elem_name}: indices dtype mismatch"
)
a = asarray(a)
assert_equal(b, a, exact=exact, elem_name=elem_name)

Expand Down
15 changes: 15 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ def test_assert_equal_dask_arrays():
assert_equal(c, d)


@pytest.mark.parametrize("attr", ["indices", "indptr"])
def test_assert_equal_sparse_index_dtype(attr):
"""assert_equal(exact=True) should detect indptr/indices dtype mismatches."""
a = sparse.csr_matrix(np.eye(3))
b = sparse.csr_matrix(np.eye(3))
setattr(b, attr, getattr(b, attr).astype(np.int32))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6cb2b35#diff-08a6944653efc734f4c8cb856fc854b9f21d655ca69796af2b170c53bff7919eL255-L256 this si what you want?

Suggested change
setattr(b, attr, getattr(b, attr).astype(np.int32))
setattr(b, attr, getattr(b, attr).astype(np.int64))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — updated to int64. The default scipy index dtype is int32, so int32 was a no-op.


# Non-exact comparison should pass (values are identical)
assert_equal(a, b, exact=False)

# Exact comparison should catch the dtype mismatch
with pytest.raises(AssertionError, match=attr):
assert_equal(a, b, exact=True)


def test_assert_equal_dask_sparse_arrays():
import dask.array as da
from scipy import sparse
Expand Down
Loading