brainevent.csr_to_csc_index

brainevent.csr_to_csc_index#

brainevent.csr_to_csc_index(csr_indptr, csr_indices, *, shape, include_perm=True, method='coo', column_block_size=4096)[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. The default method="coo" preserves the legacy CSR -> COO -> CSC behavior.

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.

  • include_perm (bool) – If True (default), return the permutation that maps CSC slots back to CSR data positions. If False, return None for the third result while still constructing the CSC structure.

  • method (str) – Conversion algorithm. "coo" expands CSR to COO first and then converts COO to CSC; "numpy" computes the structure on CPU with NumPy; "gpu_column_block" builds consecutive CSC column blocks with CUDA kernels and falls back to "numpy" when CUDA is not available.

  • column_block_size (int) – Number of CSC columns per CUDA block for method="gpu_column_block".

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 returned post_positions permutation array can be used to reorder a CSR data array into CSC order. Across all methods csc_indices are always int32 (secondary-axis coordinates), while csc_indptr / post_positions auto-promote to int64 when the nnz exceeds the int32 range (gated on jax_enable_x64).

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)
... )