Skip to main content

Crate sparse_npz

Crate sparse_npz 

Source
Expand description

Reader and writer for SciPy sparse matrices stored in the NumPy .npz format.

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 data 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), or with Zstandard via Compression::Zstd.

Only little-endian, C-order .npy data is handled, which is what NumPy and SciPy produce on the platforms targeted here.

Structs§

CscMatrix
A sparse matrix in compressed-sparse-column (CSC) form, matching SciPy’s csc_matrix attributes. col_ptr has length cols + 1; the entries of column j are row_indices[col_ptr[j]..col_ptr[j + 1]] with the parallel values in values. This is the crate’s internal orientation; CSR inputs are transposed into it on read and can be written back out as CSR.

Enums§

Compression
The ZIP compression method used for the .npy members of a written .npz. Deflate is what NumPy and SciPy emit and is universally readable; Zstd (ZIP method 93) is smaller and faster but is only readable by newer readers (numpy/scipy on Python 3.14 or later). Reading auto-detects either method.
Error
Anything that can go wrong while reading or writing a sparse .npz.
Format
The on-disk sparse orientation of a SciPy .npz: compressed by column (csc) or by row (csr).
Values
The stored (nonzero) values of a sparse matrix, tagged with their NumPy element dtype so it survives a read/write round trip. The vector length is the number of stored entries.