brainevent.csr_to_csc_index

brainevent.csr_to_csc_index#

brainevent.csr_to_csc_index(csr_indptr, csr_indices, *, shape)[source]#

Convert CSR format index arrays to CSC format.

Transforms the sparse matrix representation from Compressed Sparse Row (CSR) format to Compressed Sparse Column (CSC) format. Internally converts to COO format as an intermediate step via csr_to_coo_index(), then to CSC via coo_to_csc_index().

Parameters:
  • csr_indptr (Array | ndarray) – Row pointer array in CSR format. For a matrix with m rows, this has length m + 1.

  • csr_indices (Array | ndarray) – Column index array in CSR format. Contains the column index for each non-zero element.

  • shape (Tuple[int, int]) – A (n_rows, n_cols) tuple specifying the dimensions of the sparse matrix. Keyword-only argument.

Returns:

  • csc_indptr (jax.Array or numpy.ndarray) – Column pointer array in CSC format.

  • csc_indices (jax.Array or numpy.ndarray) – Row index array in CSC format.

  • post_positions (jax.Array or numpy.ndarray) – Permutation array that reorders data values from CSR order to CSC order. If data is the CSR data array, then data[post_positions] gives the values in CSC order.

Raises:

AssertionError – If shape is not a tuple or list, does not have exactly two elements, or contains non-positive dimensions.

See also

csr_to_coo_index

Convert CSR indices to COO indices.

coo_to_csc_index

Convert COO indices to CSC indices.

Notes

The conversion is performed in two steps: CSR is first expanded to COO via csr_to_coo_index(), then the COO representation is sorted by column via coo_to_csc_index(). The returned post_positions permutation array can be used to reorder a CSR data array into CSC order.

Examples

>>> import numpy as np
>>> from brainevent._misc import csr_to_csc_index
>>> indptr = np.array([0, 2, 3, 5])
>>> indices = np.array([0, 2, 1, 0, 3])
>>> csc_indptr, csc_indices, perm = csr_to_csc_index(
...     indptr, indices, shape=(3, 4)
... )