Skip to content

sparse — COO / CSR Conversion

Source: crates/fastdpplot-core/src/sparse.rs


to_coo

pub fn to_coo(grid: &BinGrid) -> (Vec<u32>, Vec<u32>, Vec<f32>)

Convert a BinGrid to COO format (rows, cols, data).

Only bins where count > 0 are included. The result is suitable for:

  • scipy.sparse.coo_matrix((data, (rows, cols)), shape=(height, width))
  • PyArrow / pandas COO → dense conversion.

Example (Rust)

use fastdpplot_core::sparse::to_coo;

let (rows, cols, data) = to_coo(&grid);

Example (Python, via PyO3)

from fastdpplot._rs import to_coo
rows, cols, data = to_coo(counts, values, width, height)

to_csr

pub fn to_csr(grid: &BinGrid) -> (Vec<u32>, Vec<u32>, Vec<f32>)

Convert a BinGrid to CSR format (indptr, indices, data).

Only bins where count > 0 are included. Pass directly to scipy:

import scipy.sparse as sp
M = sp.csr_matrix((data, indices, indptr), shape=(height, width))

CSR vs COO

CSR is more efficient for row slicing; COO is more convenient for column-based operations. Both are available in Rust; only to_coo is currently exposed via PyO3. Use to_scipy_sparse() in Python for an automatic CSR result.


Python helper

The fastdpplot.sparse_convert.to_scipy_sparse Python function calls to_coo internally and wraps the result in a scipy.sparse.csr_matrix. See the Python API — sparse_convert page.