Skip to content

fix(io): support CSR, CSC, and DOK formats in save_npz and load_npz - #960

Merged
hameerabbasi merged 6 commits into
pydata:mainfrom
rautaditya2606:fix-save-load-npz-formats
Sep 21, 2026
Merged

hameerabbasi merged 6 commits into
pydata:mainfrom
rautaditya2606:fix-save-load-npz-formats

Conversation

@rautaditya2606

Copy link
Copy Markdown
Contributor

What type of PR is this? (check all applicable)

  • 💾 Refactor
  • 🪄 Feature
  • 🐞 Bug Fix
  • 🔧 Optimization
  • 📚 Documentation
  • 🧪 Test
  • 🛠️ Other

Related issues

Checklist

  • Code follows style guide
  • Tests added
  • Documented the changes

Please explain your changes below.

Bug

  1. save_npz used type(matrix) is GCXS to detect compressed formats. Because CSR and CSC are subclasses of GCXS, this check evaluated to False, causing CSR/CSC arrays to fall through and save without their indices and pointer arrays.
  2. Saving DOK matrices pickled internal Python dictionaries into object arrays, which fail on modern NumPy (allow_pickle=False) and lacked reconstruction logic in load_npz.

Fix

  • Replaced strict type identity check with isinstance(matrix, (CSR, CSC)) and isinstance(matrix, GCXS).
  • Serialized DOK via canonical matrix.to_coo() coordinates and data, avoiding object pickling while preserving sparsity structure and fill values.
  • Stored explicit format tags ("coo", "gcxs", "csr", "csc", "dok") in .npz archives and reconstructed the specific matrix type in load_npz.
  • Preserved full backward compatibility in load_npz for legacy .npz files lacking a format tag.
  • Added comprehensive unit tests covering all formats, compression modes, dimensions (0D, 1D, 2D, 3D), fill values, and legacy archives.

Copilot AI lite review requested due to automatic review settings September 20, 2026 18:51
@github-actions github-actions Bot added the fix label Sep 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Medium severity

Open (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_npz to correctly detect and serialize CSR, CSC, and GCXS via isinstance, and added explicit "format" tags to saved archives.
  • Implemented DOK serialization via canonical COO coordinates/data and added corresponding reconstruction logic in load_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.

Comment thread sparse/numba_backend/_io.py Resolved

@hameerabbasi hameerabbasi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rautaditya2606

Copy link
Copy Markdown
Contributor Author

hello @hameerabbasi, i have made the changes as requested

@hameerabbasi hameerabbasi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the contribution! I think there's a lot of room for improvement.

Comment thread sparse/numba_backend/_io.py Resolved
Comment thread sparse/numba_backend/_io.py Resolved
Comment thread sparse/numba_backend/_io.py Resolved
Comment thread sparse/numba_backend/_io.py Resolved
Comment thread sparse/numba_backend/_io.py Resolved
Comment thread sparse/numba_backend/_io.py Resolved

@hameerabbasi hameerabbasi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is elegant if it can do the same thing with less lines of code. 😉

Comment thread sparse/numba_backend/tests/test_io.py Resolved
Comment thread sparse/numba_backend/_io.py Resolved
@rautaditya2606

Copy link
Copy Markdown
Contributor Author

hello @hameerabbasi, thank you for the review.
ive updated as requested

@hameerabbasi
hameerabbasi enabled auto-merge (squash) September 21, 2026 14:39
@hameerabbasi
hameerabbasi merged commit 7b075c7 into pydata:main Sep 21, 2026
11 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: save_npz and load_npz fail for CSR, CSC, and DOK formats

3 participants