Skip to content

chore: add sparse index dtype checks to assert_equal#2362

Merged
ilan-gold merged 5 commits into
scverse:mainfrom
LiudengZhang:fix/assert-equal-sparse-dtype
Mar 19, 2026
Merged

chore: add sparse index dtype checks to assert_equal#2362
ilan-gold merged 5 commits into
scverse:mainfrom
LiudengZhang:fix/assert-equal-sparse-dtype

Conversation

@LiudengZhang
Copy link
Copy Markdown
Contributor

Summary

  • Enhanced assert_equal for sparse matrices to check indptr and indices dtype consistency when exact=True
  • Previously, sparse matrices were converted to dense via asarray() before comparison, silently losing index dtype information (e.g. int32 vs int64 mismatches went undetected)
  • Added test verifying the new check catches dtype mismatches

Changes

  • src/anndata/tests/helpers.py: Added indptr/indices dtype assertions in assert_equal_sparse before dense conversion, gated on exact=True and both operands being sparse
  • tests/test_helpers.py: Added test_assert_equal_sparse_index_dtype — creates CSR matrices with identical values but different index dtypes, verifies exact comparison catches the mismatch

Test plan

  • pytest tests/test_helpers.py::test_assert_equal_sparse_index_dtype — passes
  • All non-awkward tests in test_helpers.py pass (awkward failures are pre-existing env issue)

Fixes #860

When exact=True, assert_equal now checks that indptr and indices dtypes
match between sparse matrices, catching silent precision mismatches
(e.g. int32 vs int64) that were previously lost during dense conversion.

Fixes scverse#860
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.39%. Comparing base (ef466f2) to head (5119b20).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2362      +/-   ##
==========================================
- Coverage   87.35%   85.39%   -1.96%     
==========================================
  Files          49       49              
  Lines        7784     7787       +3     
==========================================
- Hits         6800     6650     -150     
- Misses        984     1137     +153     
Files with missing lines Coverage Δ
src/anndata/tests/helpers.py 83.80% <100.00%> (-9.12%) ⬇️

... and 7 files with indirect coverage changes

@ilan-gold ilan-gold added this to the 0.12.11 milestone Mar 12, 2026
@ilan-gold
Copy link
Copy Markdown
Contributor

Please update your PR title to be chore: ... :)

Comment thread src/anndata/tests/helpers.py Outdated
Comment on lines +692 to +697
assert a.indptr.dtype == b.indptr.dtype, (
f"{elem_name}: indptr dtype mismatch: {a.indptr.dtype} vs {b.indptr.dtype}"
)
assert a.indices.dtype == b.indices.dtype, (
f"{elem_name}: indices dtype mismatch: {a.indices.dtype} vs {b.indices.dtype}"
)
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.

Suggested change
assert a.indptr.dtype == b.indptr.dtype, (
f"{elem_name}: indptr dtype mismatch: {a.indptr.dtype} vs {b.indptr.dtype}"
)
assert a.indices.dtype == b.indices.dtype, (
f"{elem_name}: indices dtype mismatch: {a.indices.dtype} vs {b.indices.dtype}"
)
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"
)

I think the mismatch's contents are already reported by assert

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.

Done — simplified the assert messages and included elem_name for context. Updated in 6cb2b35.

Comment thread tests/test_helpers.py Outdated
Comment on lines +255 to +256
b.indptr = b.indptr.astype(np.int64)
b.indices = b.indices.astype(np.int64)
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.

Please parametrize this test to change each of these one-at-a-time and then have the match check that indices or indptr is in the message accordingly

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.

Done — parametrized with @pytest.mark.parametrize("attr", ["indices", "indptr"]), each tested one-at-a-time with match=attr. Updated in 6cb2b35.

@LiudengZhang LiudengZhang changed the title Add sparse index dtype checks to assert_equal chore: add sparse index dtype checks to assert_equal Mar 13, 2026
LiudengZhang and others added 3 commits March 13, 2026 13:08
Address review feedback: use elem_name-only messages in asserts
and parametrize test by attribute (indices/indptr).
Comment thread tests/test_helpers.py Outdated
"""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.

@ilan-gold ilan-gold merged commit 753f058 into scverse:main Mar 19, 2026
23 of 24 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/anndata that referenced this pull request Mar 19, 2026
ilan-gold pushed a commit that referenced this pull request Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

assert_equal fails to detect differences in dtype of sparse_matrix dtype

2 participants