Repository navigation
fix(io): support CSR, CSC, and DOK formats in save_npz and load_npz - #960
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
load_npz can raise uncaught KeyError for corrupted/tagged archives, causing inconsistent exception behavior versus the legacy loading path.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1
What changed in this PR
This PR fixes save_npz/load_npz round-tripping for additional sparse formats by adding explicit format tagging to .npz archives and implementing robust (non-pickled) serialization for DOK, while maintaining compatibility with legacy archives that lack a format tag.
Changes:
- Updated
save_npzto correctly detect and serializeCSR,CSC, andGCXSviaisinstance, and added explicit"format"tags to saved archives. - Implemented
DOKserialization via canonical COO coordinates/data and added corresponding reconstruction logic inload_npz. - Expanded unit tests to cover all supported formats, fill values, multiple dimensions, legacy archives, and error cases.
| File | Description |
|---|---|
sparse/numba_backend/_io.py |
Adds format-tagged serialization/deserialization for COO/GCXS/CSR/CSC/DOK and improves legacy compatibility paths. |
sparse/numba_backend/tests/test_io.py |
Extends coverage to new formats, dimensions, fill values, legacy archives, and invalid/corrupted inputs. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
hameerabbasi
left a comment
There was a problem hiding this comment.
We can replace this whole chain by something like the following:
import sparse
getattr(sparse, format.upper())(**fp)
Some massaging may be required to pass tests (e.g. for shape and fill_value).
We can catch TypeErrors, and convert them to RuntimeErrors with raise ... from ...
DOK should not be serializable at all, that's by design. It's a Python dict, not a bunch of NumPy arrays.
|
hello @hameerabbasi, i have made the changes as requested |
hameerabbasi
left a comment
There was a problem hiding this comment.
Thanks a lot for the contribution! I think there's a lot of room for improvement.
hameerabbasi
left a comment
There was a problem hiding this comment.
Code is elegant if it can do the same thing with less lines of code. 😉
|
hello @hameerabbasi, thank you for the review. |

What type of PR is this? (check all applicable)
Related issues
Checklist
Please explain your changes below.
Bug
save_npzusedtype(matrix) is GCXSto detect compressed formats. BecauseCSRandCSCare subclasses ofGCXS, this check evaluated toFalse, causingCSR/CSCarrays to fall through and save without their indices and pointer arrays.DOKmatrices pickled internal Python dictionaries into object arrays, which fail on modern NumPy (allow_pickle=False) and lacked reconstruction logic inload_npz.Fix
isinstance(matrix, (CSR, CSC))andisinstance(matrix, GCXS).DOKvia canonicalmatrix.to_coo()coordinates and data, avoiding object pickling while preserving sparsity structure and fill values."coo","gcxs","csr","csc","dok") in.npzarchives and reconstructed the specific matrix type inload_npz.load_npzfor legacy.npzfiles lacking a format tag.