# sparse_npz
Reader and writer for **SciPy sparse matrices** stored in the NumPy `.npz`
format, implemented directly in Rust.
`scipy.sparse.save_npz` writes a zip bundle of `.npy` arrays (`format`, `shape`,
`data`, `indices`, `indptr`). This crate reads those bundles, in either CSC or
CSR form, and writes them back in CSC form, exchanging the matrix as a
`CscMatrix`. The element dtype is preserved end to end: the reader decodes the
values into a typed `Values` (boolean; any signed or unsigned 8/16/32/64-bit
integer; or 32/64-bit float) and the writer emits that same dtype.
Members are read whether they are stored, DEFLATE-compressed, or
Zstandard-compressed. They are written DEFLATE-compressed by default (as SciPy
does, so the output is universally readable). The compression is selectable via
`write_npz_with`:
- `Compression::Deflate { level }` - `level` in `1..=264` (`1..=9` via flate2,
`10..=264` via Zopfli), `None` for the default of 6.
- `Compression::Zstd { level }` - `level` in roughly `-7..=22`, `None` for the
default of 3. Smaller, but only readable by newer readers (numpy/scipy on
Python 3.14 or later).
Only little-endian, C-order `.npy` data is handled, which is what NumPy and
SciPy produce on the platforms targeted here.
```rust
use sparse_npz::CscMatrix;
let m = CscMatrix::read_npz("matrix.npz")?;
println!("{}x{}, {} nonzeros", m.rows, m.cols, m.nnz());
m.write_npz("copy.npz")?;
```
## License
Licensed under either of Apache License, Version 2.0
([LICENSE-APACHE](LICENSE-APACHE)) or MIT license ([LICENSE-MIT](LICENSE-MIT)) at
your option.
## Authorship
This project, including its purpose and overall architecture, was conceived and
designed by its human authors. The implementation was generated in full with AI
assistance (Claude Code), under the human authors' direction.
## Note
This crate is an independent implementation of the on-disk formats used by NumPy
and SciPy. It is not affiliated with or endorsed by those projects; their names
are used only descriptively, to identify the formats it reads and writes.