Repository navigation
Grouped (per-condition) reductions: group_sum / group_nnz / group_mean (#81) - #104
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #81.
Summary
Adds grouped (per-sample/per-condition) reductions to raw
VCSCArray/VCSRArray:axisis the axis thatgroup_idxslabels and that gets collapsed into groups. This generalizessum(axis=...): with one group,v.group_sum(zeros, 1, axis=0)[0] == v.sum(axis=0).total=Truealso reduces the other axis, giving one number per group.0, meannan). Arguments are validated: axis, non-negativen_groups, length, integer dtype and label range.Implementation (
src/vsparse/_grouped.py)total=True: onebincountover the per-major-slice totals. It never readsindices, likesum's major-axis fast path, as the issue asked. A test corruptsindicesto confirm this.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.(n_groups, n_minor)buffer, capped by the existingaccumulator_threadsbudget.group_nnzshares these kernels through acountflag, so it never allocates an all-onesvaluescopy.Rough timing on a 200k × 20k Poisson matrix at 5% density, compared with
indicator @ Xin scipy:group_sumtotal=TrueDocumentation
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.Testing
tests/test_grouped_reductions.py:totalon/off.sum/mean/getnnzfor a single group.ruff check/ruff formatandcodespellpass.xenonshows no new violations:_grouped.pyaverages complexity A.jscpdreports no new clones; the one it found in the kernels was extracted into an inline numba helper.vultureflags only the new public methods, at 60% confidence, the same as the existinggetnnz/slice_norms.