Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/hdf5array/Hdf5CompressedSparseMatrixSeed.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from typing import Optional, Sequence, Tuple, Callable
from typing import Optional, Sequence, Tuple, Callable, Literal
from delayedarray import extract_dense_array, extract_sparse_array, chunk_shape, DelayedArray, wrap, is_sparse, SparseNdarray, is_masked
from h5py import File
import numpy
from numpy import ndarray, dtype, integer, zeros, issubdtype, array
from bisect import bisect_left

from biocutils.package_utils import is_package_installed

__author__ = "LTLA"
__copyright__ = "LTLA"
__license__ = "MIT"
Expand Down Expand Up @@ -458,3 +460,28 @@ def wrap_Hdf5CompressedSparseMatrixSeed(x: Hdf5CompressedSparseMatrixSeed):
def is_masked_Hdf5CompressedSparseMatrixSeed(x: Hdf5CompressedSparseMatrixSeed) -> bool:
"""See :py:meth:`~delayedarray.is_masked.is_masked`."""
return False

if is_package_installed("scipy"):
import scipy.sparse
from delayedarray.to_scipy_sparse_matrix import to_scipy_sparse_matrix

@to_scipy_sparse_matrix.register
def to_scipy_sparse_matrix_from_Hdf5CompressedSparseMatrix(x: Hdf5CompressedSparseMatrix, format: Literal["coo", "csr", "csc"] = "csc") -> scipy.sparse.spmatrix:
"""See :py:func:`delayedarray.to_scipy_sparse_matrix.to_scipy_sparse_matrix`."""

with File(x.path, "r") as handle:
_data = handle[x.data_name][:]
_indices = handle[x.indices_name][:]
_indptr = handle[x.indptr_name][:]

if x.by_column:
_matrix = scipy.sparse.csc_matrix((_data, _indices, _indptr), shape=x.shape, dtype=x.dtype)
else:
_matrix = scipy.sparse.csr_matrix((_data, _indices, _indptr), shape=x.shape, dtype=x.dtype)

if format == "csc":
return _matrix.tocsc()
elif format == "csr":
return _matrix.tocsr()
else:
return _matrix.tocoo()
15 changes: 15 additions & 0 deletions tests/test_Hdf5CompressedSparseMatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,18 @@ def test_Hdf5CompressedSparseMatrix_properties():

rewrap = delayedarray.wrap(arr.seed)
assert isinstance(rewrap, Hdf5CompressedSparseMatrix)

def test_Hdf5CompressedSparseMatrix_to_sparse():
shape = (100, 200)
y = scipy.sparse.random(*shape, 0.1).tocsr()
path, group = _mockup(y)
arr = Hdf5CompressedSparseMatrix(path, group, shape=shape, by_column=False)

_to_csr = delayedarray.to_scipy_sparse_matrix(arr, "csr")
assert isinstance(_to_csr, scipy.sparse.csr_matrix)

_to_csc = delayedarray.to_scipy_sparse_matrix(arr, "csc")
assert isinstance(_to_csc, scipy.sparse.csc_matrix)

_to_coo = delayedarray.to_scipy_sparse_matrix(arr, "coo")
assert isinstance(_to_coo, scipy.sparse.coo_matrix)