Skip to content

Fix Reindexer._apply_to_dask_array densifying a sparse zero-axis input - #2737

Open
SID-6921 wants to merge 1 commit into
scverse:mainfrom
SID-6921:fix/dask-sparse-reindex-zero-major-axis-v2
Open

SID-6921 wants to merge 1 commit into
scverse:mainfrom
SID-6921:fix/dask-sparse-reindex-zero-major-axis-v2

Conversation

@SID-6921

@SID-6921 SID-6921 commented Nov 5, 2026

Copy link
Copy Markdown

What

The zero-shape branch of Reindexer._apply_to_dask_array (el.shape[axis] == 0) always returned da.broadcast_to(fill_value, shape), a plain dense array, regardless of el's dtype/format. The sibling _apply_to_sparse (used for plain, non-dask sparse inputs) handles the identical scenario — reindex-and-pad a zero-length axis during an outer join — by explicitly reconstructing a sparse result (csr_cls(shape) when fill_value is 0, or a sparse-wrapped broadcast otherwise). The sparse-aware "fast path" earlier in this same method only covers the case where the reindexed axis is the array's unchunked minor axis; a CSR-backed dask array reindexed along its major (chunked) axis with a zero-length input — e.g. one input of an outer-join anndata.concat that contributes 0 obs along the concatenation axis — falls through to the generic zero-shape branch and gets silently densified.

Verification

Verified against the real anndata/dask/scipy machinery, not just a standalone reimplementation: copied the current _apply_to_dask_array body verbatim and ran it against a real Reindexer with a 0-row CSR-backed dask array reindexed along its major axis during an outer join — confirmed the pre-fix code returns a dense numpy-backed dask array (result._meta is a numpy.ndarray), while the sibling _apply_to_sparse on the equivalent plain-sparse input returns a real scipy.sparse.csr_matrix for the identical shape and content. Also verified a nonzero fill_value on the same zero-axis sparse case still produces the correct filled values, and a normal (non-zero-axis) dense reindex is unaffected by this change.

Fix

When the zero-length-axis input's dask _meta is sparse (CSArray/CSMatrix), materialize it (cheap — it's empty along this axis regardless of the other axis's size) and delegate to the already-correct _apply_to_sparse zero-shape branch, then wrap the result back into a dask array via da.from_array. Falls through to the existing da.broadcast_to path unchanged for dense inputs.

The zero-shape branch of _apply_to_dask_array (el.shape[axis] == 0)
always returned da.broadcast_to(fill_value, shape), a plain dense
array, regardless of el's dtype/format. The sibling _apply_to_sparse
(used for plain, non-dask sparse inputs) handles the identical
scenario -- reindex-and-pad a zero-length axis during an outer join --
by explicitly reconstructing a sparse result (csr_cls(shape) when
fill_value is 0, or a sparse-wrapped broadcast otherwise). The
sparse-aware "fast path" earlier in this same method only covers the
case where the reindexed axis is the array's unchunked minor axis; a
CSR-backed dask array reindexed along its major (chunked) axis with a
zero-length input -- e.g. one input of an outer-join anndata.concat
that contributes 0 obs along the concatenation axis -- falls through
to the generic zero-shape branch and gets silently densified.

Verified against the real anndata/dask/scipy machinery (not just a
standalone reimplementation): copied the current _apply_to_dask_array
body verbatim and ran it against a real Reindexer with a 0-row
CSR-backed dask array reindexed along its major axis during an outer
join -- confirmed the pre-fix code returns a dense numpy-backed dask
array (result._meta is a numpy.ndarray), while the sibling
_apply_to_sparse on the equivalent plain-sparse input returns a real
scipy.sparse.csr_matrix for the identical shape and content. Also
verified: a nonzero fill_value on the same zero-axis sparse case still
produces the correct filled values, and a normal (non-zero-axis) dense
reindex is unaffected by this change.

Fix: when the zero-length-axis input's dask _meta is sparse
(CSArray/CSMatrix), materialize it (cheap -- it's empty along this
axis regardless of the other axis's size) and delegate to the already-
correct _apply_to_sparse zero-shape branch, then wrap the result back
into a dask array via da.from_array. Falls through to the existing
da.broadcast_to path unchanged for dense inputs.

Signed-off-by: Siddhardha Nanda <99672439+SID-6921@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 5, 2026 15:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.74%. Comparing base (0569b0c) to head (1d8e722).
⚠️ Report is 103 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2737      +/-   ##
==========================================
- Coverage   87.66%   85.74%   -1.93%     
==========================================
  Files          49       49              
  Lines        7729     8240     +511     
==========================================
+ Hits         6776     7065     +289     
- Misses        953     1175     +222     
Files with missing lines Coverage Δ
src/anndata/_core/merge.py 84.95% <100.00%> (-8.85%) ⬇️

... and 39 files with indirect coverage changes

@SID-6921

SID-6921 commented Nov 5, 2026 •

Copy link
Copy Markdown
Author

Checked all 4 failing checks, none require a code change from this PR:

  • codecov/project: the bot's own comment notes the report is "103 commits behind head on main"; this fork branch was built off a stale fork base; codecov/patch (this diff's own coverage) is green ("All modified and coverable lines are covered by tests"). The project-wide 58.48%/80% gap is pre-existing, not from this change.
  • Triage: Check if GPU tests are allowed to run and Triage: Check PR title, milestone, and labels: both are maintainer-gated checks (no labels/milestone set yet, GPU tests need approval for first-time/external PRs); nothing I can push to satisfy these.
  • docs/readthedocs.com:icb-anndata: build finished in 38s with no error text, which looks like the common Read the Docs limitation building fork/external PRs rather than a doc content issue, but I don't have access to the full build log to confirm.

The real test suite (hatch-test across the matrix, including dask_distributed and array_api variants) and pre-commit are passing where completed. Happy to address anything specific if I'm missing something.

@SID-6921

SID-6921 commented Nov 5, 2026 •

Copy link
Copy Markdown
Author

The 2 real test failures (test_dask_view_mem.py::test_modify_view_X_memory[layers-no_give_chunks] and [obsm-give_chunks], "Test was limited to 1.5MiB but allocated 1.9MiB") are unrelated to this diff; this PR only touches Reindexer._apply_to_dask_array's sparse zero-axis handling in merge.py, not the view/copy-on-write memory path those tests exercise (X/layers/obsm after modifying a dask-backed view). Tight memory-budget tests like this are sensitive to incidental allocation changes from dependency versions; the same test has come up in at least one other unrelated PR's discussion (#2596). 5545 other tests pass.

The two Triage checks (PR title/milestone/labels, GPU test approval) and the docs/readthedocs.com failure are maintainer/infra-gated as noted in my earlier comment and don't need a code change either.

@SID-6921

Copy link
Copy Markdown
Author

The two red test jobs here (hatch-test.min/hatch-test.pre/hatch-test.stable) are both failing on tests/test_dask_view_mem.py::test_modify_view_X_memory, a memory-ceiling assertion ("Test was limited to 1.5MiB but allocated 1.9MiB"). This file isn't touched by this PR's diff (only src/anndata/_core/merge.py's _apply_to_dask_array), and the same job is currently failing on main itself too, so this looks unrelated to this change. The "Triage" checks (milestone/labels) need a maintainer action I don't have permission for.

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.

2 participants