Skip to content

Grouped (per-condition) reductions: group_sum / group_nnz / group_mean (#81) - #104

Merged
aarmey merged 1 commit into
mainfrom
group-sum-81
Nov 23, 2026
Merged

aarmey merged 1 commit into
mainfrom
group-sum-81

Conversation

@aarmey

@aarmey aarmey commented Nov 23, 2026

Copy link
Copy Markdown
Member

Closes #81.

Summary

Adds grouped (per-sample/per-condition) reductions to raw VCSCArray/VCSRArray:

v.group_sum(condition_idxs, n_cond)               # (n_cond, n_cols) per-condition sums
v.group_nnz(condition_idxs, n_cond)               # (n_cond, n_cols) int64 stored-element counts
v.group_mean(condition_idxs, n_cond)              # (n_cond, n_cols) means, implicit zeros included
v.group_sum(condition_idxs, n_cond, total=True)   # (n_cond,) per-condition depth
v.group_sum(gene_set_idxs, n_sets, axis=1)        # (n_rows, n_sets) column groups
  • axis is the axis that group_idxs labels and that gets collapsed into groups. This generalizes sum(axis=...): with one group, v.group_sum(zeros, 1, axis=0)[0] == v.sum(axis=0).
  • total=True also reduces the other axis, giving one number per group.
  • Labels don't need to be sorted or contiguous, and empty groups are allowed (sum/nnz 0, mean nan). Arguments are validated: axis, non-negative n_groups, length, integer dtype and label range.

Implementation (src/vsparse/_grouped.py)

  • Major axis, total=True: one bincount over the per-major-slice totals. It never reads indices, like sum's major-axis fast path, as the issue asked. A test corrupts indices to confirm this.
  • Major axis, full:
    • With n_groups >= nthreads, slices are ordered by a stable argsort and each thread owns whole groups. Nothing is shared and no scratch memory is needed. For data already sorted by condition (as 4D-RISE stores it), this walks memory in order, so it covers the segmented fast path the issue suggested.
    • With fewer groups than threads, each thread gets a private (n_groups, n_minor) buffer, capped by the existing accumulator_threads budget.
  • Minor axis: each major slice owns its own output row, so it parallelizes over slices without scratch memory.
  • group_nnz shares these kernels through a count flag, so it never allocates an all-ones values copy.

Rough timing on a 200k × 20k Poisson matrix at 5% density, compared with indicator @ X in scipy:

groups group_sum total=True scipy indicator matmul
VCSR 4 0.027 s 0.010 s 0.79 s
VCSC 4 0.031 s 0.055 s 3.16 s
VCSR 64 0.046 s 0.011 s 0.83 s
VCSC 64 0.035 s 0.051 s 3.25 s

Documentation

  • NumPy-style docstrings on all three methods. group_sum's docstring has a runnable example, checked with doctest. They render in the API reference (Sphinx build checked locally).
  • docs/usage.md: new "Reductions" and "Grouped (per-condition) reductions" sections covering semantics, shapes, dtypes, empty groups and performance.
  • README quickstart: added a reductions snippet.

Testing

  • tests/test_grouped_reductions.py:
    • Hypothesis property tests against a dense reference for both classes, both axes and total on/off.
    • Both major-axis kernels exercised explicitly.
    • Equivalence with sum/mean/getnnz for a single group.
    • Sorted vs. shuffled labels give the same result.
    • Empty groups, return dtypes and invalid arguments.
  • Full suite: 2010 passed, 73 skipped.
  • Checks: ruff check/ruff format and codespell pass. xenon shows no new violations: _grouped.py averages complexity A. jscpd reports no new clones; the one it found in the kernels was extracted into an inline numba helper. vulture flags only the new public methods, at 60% confidence, the same as the existing getnnz/slice_norms.

Collapse rows (axis=0) or columns (axis=1) into n_groups bins given a
group label per row/column, e.g. per-condition sums, nonzero counts and
means. total=True also reduces the other axis; grouping along the major
axis with total=True reads only the value groups, never indices.

Kernels are race-free: groups along the major axis are processed group-
owned per thread (or with small per-thread buffers when there are fewer
groups than threads); groups along the minor axis are scattered per
major slice.
@aarmey
aarmey merged commit adf22be into main Nov 23, 2026
8 checks passed
@aarmey
aarmey deleted the group-sum-81 branch November 23, 2026 14:30
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.

Grouped (per-condition) reductions on raw VCSR/VCSC arrays

1 participant