fix(math/sparse): coo_to_csr bounds check + sparse/event/delay regression suite#845
Conversation
…elay correctness with tests - coo_to_csr silently produced a corrupt CSR (indptr[-1] != nse) on out-of-range pre_ids; now raises a clear ValueError (Medium) - Added regression tests comparing CSR/COO/event matvec+matmat and jitconn against dense references (incl. transpose and autodiff), plus TimeDelay / LengthDelay ring-buffer modulo-wrap tests, locking in prior correctness fixes Findings recorded in docs/issues-found-20260619-math-sparse-event.md
Reviewer's GuideAdds an eager bounds check to sparse.coo_to_csr to prevent structurally invalid CSR output on out-of-range pre_ids, and introduces a focused regression test suite covering sparse COO/CSR helpers, event-driven CSR matvec/matmat paths (including transpose and autodiff), pre_syn_post routing utilities, and TimeDelay/LengthDelay ring-buffer wrap behaviour. The PR also records the sparse/event audit findings in docs/issues-found-20260619-math-sparse-event.md. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="brainpy/math/sparse/utils_test.py" line_range="21" />
<code_context>
+# coo_to_csr
+# ---------------------------------------------------------------------------
+
+def test_coo_to_csr_valid_roundtrip():
+ # 3 x 4 matrix:
+ # [[0, 2, 0, 4],
</code_context>
<issue_to_address>
**suggestion (testing):** Add an explicit test for the empty-COO case in `coo_to_csr`.
There’s no test case for empty `pre_ids`/`post_ids` (e.g. `num_row > 0` with `nse == 0`), which is easy to implement incorrectly (e.g. `indptr` length/dtype or `indptr[-1]` not zero). Please add a test (e.g. `test_coo_to_csr_empty_matrix`) using `pre = post = np.array([], dtype=int)`, `num_row = 3`, and asserting that `indices` is empty and `indptr` is `np.array([0, 0, 0, 0], dtype=np.int32)` to cover structurally empty matrices.
</issue_to_address>
### Comment 2
<location path="brainpy/math/sparse/utils_test.py" line_range="85" />
<code_context>
+ return np.asarray(indptr), np.asarray(indices), np.asarray(order)
+
+
+def test_csr_to_coo_roundtrip():
+ rows = np.array([0, 0, 1, 2, 2])
+ cols = np.array([1, 3, 0, 1, 3])
</code_context>
<issue_to_address>
**suggestion (testing):** Consider adding a test for `csr_to_coo` with a completely empty CSR (no non-zeros).
You already cover a standard roundtrip and CSR with empty rows. It would be helpful to add a case where the CSR represents an all-zero matrix, e.g. `indptr = np.zeros(num_rows + 1, dtype=int)` and empty `indices`. A test like `test_csr_to_coo_all_zero_matrix` can assert `csr_to_coo` returns empty `rows`/`cols` with the correct dtype/shape and handles this corner case without errors.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| # coo_to_csr | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_coo_to_csr_valid_roundtrip(): |
There was a problem hiding this comment.
suggestion (testing): Add an explicit test for the empty-COO case in coo_to_csr.
There’s no test case for empty pre_ids/post_ids (e.g. num_row > 0 with nse == 0), which is easy to implement incorrectly (e.g. indptr length/dtype or indptr[-1] not zero). Please add a test (e.g. test_coo_to_csr_empty_matrix) using pre = post = np.array([], dtype=int), num_row = 3, and asserting that indices is empty and indptr is np.array([0, 0, 0, 0], dtype=np.int32) to cover structurally empty matrices.
| return np.asarray(indptr), np.asarray(indices), np.asarray(order) | ||
|
|
||
|
|
||
| def test_csr_to_coo_roundtrip(): |
There was a problem hiding this comment.
suggestion (testing): Consider adding a test for csr_to_coo with a completely empty CSR (no non-zeros).
You already cover a standard roundtrip and CSR with empty rows. It would be helpful to add a case where the CSR represents an all-zero matrix, e.g. indptr = np.zeros(num_rows + 1, dtype=int) and empty indices. A test like test_csr_to_coo_all_zero_matrix can assert csr_to_coo returns empty rows/cols with the correct dtype/shape and handles this corner case without errors.
Fresh review of
brainpy/mathsparse/event/jitconn operators + delay variables.coo_to_csrsilently produced a corrupt CSR (indptr[-1] != nse) on out-of-rangepre_ids; now raises a clearValueError.transpose=Trueand autodiff), and TimeDelay/LengthDelay ring-buffer modulo-wrap behavior. Prior-audit findings (csrmm transpose, TimeDelay modulo, coomv) verified already-fixed and now locked by tests.In-scope: 58 passed. Findings:
docs/issues-found-20260619-math-sparse-event.md.Summary by Sourcery
Tighten sparse COO-to-CSR conversion error handling and add regression coverage for sparse/event operations and delay variables.
Bug Fixes:
Enhancements:
Documentation:
Tests: