fix(rag): handle indexed empty row in Ragged.to_numpy#68
Merged
Conversation
A length-0 row obtained by indexing a multi-dimensional Ragged (is_base=False) raised "cannot reshape array of size 0 into shape (0)". When fully indexed down to a single ragged axis, `leading` is empty, so the final reshape used (leading or (-1,)) -> data.reshape(-1, 0); numpy cannot infer -1 against a 0 dimension on a size-0 array. Compute an explicit row count in both the validate and trust-the-caller branches and use (leading or (n_rows,)) instead of -1. Empty rows now reshape cleanly to (n_rows, 0) and non-empty single-row results are unchanged (n_rows == 1). Closes #67 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
Merging this PR will improve performance by 68.96%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | test_bench_ragged_cres |
2,574.2 µs | 955.2 µs | ×2.7 |
| ⚡ | test_bench_baseline_ragged_short_alleles |
2.9 ms | 2.1 ms | +38.22% |
| ⚡ | test_bench_dense_batch |
5 ms | 3.8 ms | +29.5% |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing fix/to-numpy-indexed-empty-row (e66e259) with main (cbbdabf)1
Footnotes
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.
Summary
Fixes #67.
Ragged.to_numpy()raisedValueError: cannot reshape array of size 0 into shape (0)on an empty row (length 0) obtained by indexing a multi-dimensionalRagged:A directly-constructed empty row converted fine (it took the
is_basefast path); only the indexed, non-base empty row failed.Root cause
In
to_numpy(python/seqpro/rag/_core.py), when a Ragged is fully indexed down to a single ragged axis,leading(the leading dense dims) is empty, so the final reshape used(leading or (-1,))→data.reshape(-1, 0). numpy cannot infer-1against a0dimension on a size-0 array, so it raised.Fix
Compute an explicit row count
n_rowsin both thevalidate=Truebranch (lengths.size) and the trust-the-caller branch (already present), and use(leading or (n_rows,))instead of-1. Empty rows now reshape cleanly to(n_rows, 0); non-empty single-row results are unchanged (n_rows == 1).Tests
Added
test_to_numpy_indexed_empty_row(parametrized overvalidate=True/False) asserting the indexed empty row converts to shape(1, 0). Fails before the fix, passes after. Full ragged suite (263 tests) passes; ruff/pyrefly clean.No
SKILL.mdupdate required — bugfix with no public signature change.Context
Unblocks the genoray cleanup noted in the issue: removing the
INT64_MAXoffset sentinel workaround that exists specifically to detect-and-skip empty rows beforeto_numpy().🤖 Generated with Claude Code