Skip to content

fix: avoid dtype overflow in top_segment_proportions for sparse small ints - #4461

Open
pujitha24 wants to merge 1 commit into
scverse:mainfrom
pujitha24:auto/issue-4440
Open

pujitha24 wants to merge 1 commit into
scverse:mainfrom
pujitha24:auto/issue-4440

Conversation

@pujitha24

Copy link
Copy Markdown

LLM usage

Sums and accumulator in top_segment_proportions_sparse_csr are now float64 instead of the input dtype, fixing overflow (negative percentages) for small-int sparse data. The numba error for unsigned dtypes from the issue did not reproduce locally (numba version here), so only the overflow is demonstrated.


AI assistance: this change was drafted with Claude Code.

… ints

Motivation: top_segment_proportions_sparse_csr allocated the per-cell sums
and the running accumulator with the input dtype. For small signed integer
sparse data (e.g. int8/int16) these overflow, giving wrong (e.g. negative)
pct_counts_in_top_N_genes. The dense path upcasts and is unaffected. The
issue also reports a numba typing failure for unsigned dtypes; that did not
reproduce here (uint16/uint32 passed even before the change).

Approach: allocate sums and acc as float64 (values already is).

Validation: added test_top_segments_sparse_small_int, comparing the sparse
result to the dense result for int8/int16/uint8/uint16/uint32. With the
source change stashed it fails for int8, int16 and uint8; with it, all pass.
Ran `pytest tests/test_qc_metrics.py` (61 passed, 7 xfailed) and
`ruff check` / `ruff format --check` on changed files. Did not run the
rest of CI.

Report: scverse#4440
Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
Assisted-by: claude-sonnet-5-5 (via Claude Code)
@codecov

codecov Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.37%. Comparing base (1a421f2) to head (7d00d0b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4461   +/-   ##
=======================================
  Coverage   84.37%   84.37%           
=======================================
  Files         134      134           
  Lines       12750    12750           
=======================================
  Hits        10758    10758           
  Misses       1992     1992           
Flag Coverage Δ
hatch-test.low-vers 81.14% <ø> (ø)
hatch-test.pre 84.18% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/scanpy/preprocessing/_qc.py 95.86% <ø> (ø)

@read-the-docs-business

Copy link
Copy Markdown

Documentation build overview

📚 scanpy | 🛠️ Build #4654688 | 📁 Comparing 7d00d0b against latest (1a421f2)

  🔍 Preview build  

3 files changed
± release-notes/index.html
± api/generated/scanpy.pp.calculate_qc_metrics.html
± api/generated/scanpy.settings.html

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

calculate_qc_metrics gives negative pct_counts_in_top_N_genes for sparse int16 X and fails for sparse unsigned integer X

1 participant