Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for scipy.csr_array/scipy.csc_array #3423

Open
2 of 3 tasks
alam-shahul opened this issue Jan 1, 2025 · 4 comments
Open
2 of 3 tasks

Support for scipy.csr_array/scipy.csc_array #3423

alam-shahul opened this issue Jan 1, 2025 · 4 comments

Comments

@alam-shahul
Copy link

alam-shahul commented Jan 1, 2025

Please make sure these conditions are met

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of scanpy.
  • (optional) I have confirmed this bug exists on the main branch of scanpy.

What happened?

It seems like sc.pp.pca doesn't work when .X is a csr_array (but it works fine when it's a csr_matrix); seems like some low level matrix multiplication subroutine is not implemented for csr_arrays.

Minimal code sample

import scanpy as sc
import anndata as ad
import numpy as np
from scipy.sparse import csr_array, csr_matrix

data = np.random.random((2, 5))
dataset = ad.AnnData(
    X=csr_array(data),
    # X=csr_matrix(data),
)
sc.pp.pca(dataset)

Error output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/env/lib/python3.10/site-packages/scanpy/preprocessing/_pca.py", line 285, in pca
    X_pca, pca_ = _pca_with_sparse(
  File "/env/lib/python3.10/site-packages/scanpy/preprocessing/_pca.py", line 453, in _pca_with_sparse
    total_var = _get_mean_var(X)[1].sum()
  File "/env/lib/python3.10/site-packages/scanpy/preprocessing/_utils.py", line 40, in _get_mean_var
    mean_sq = axis_mean(elem_mul(X, X), axis=axis, dtype=np.float64)
  File "/env/lib/python3.10/functools.py", line 889, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/env/lib/python3.10/site-packages/scanpy/_utils/__init__.py", line 546, in elem_mul
    raise NotImplementedError
NotImplementedError

Versions

1.10.4
@alam-shahul alam-shahul added the Triage 🩺 This issue needs to be triaged by a maintainer label Jan 1, 2025
@flying-sheep flying-sheep added Needs info❔ More information needed and removed Triage 🩺 This issue needs to be triaged by a maintainer labels Jan 9, 2025
@flying-sheep
Copy link
Member

you forgot to fill out the version information. Please do that. It’s likely that this is true, but I still need that info.

@alam-shahul
Copy link
Author

Hmm... well, I just tried it again on my current installation, and I got a different error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/work/magroup/shahula/bin/miniconda3/envs/popari_distribution/lib/python3.10/site-packages/scanpy/preprocessing/_pca.py", line 200, in pca
    output = _pca_with_sparse(
  File "/work/magroup/shahula/bin/miniconda3/envs/popari_distribution/lib/python3.10/site-packages/scanpy/preprocessing/_pca.py", line 271, in _pca_with_sparse
    mu = X.mean(0).A.flatten()[None, :]
AttributeError: 'numpy.ndarray' object has no attribute 'A'

This is for version 1.9.3. I thought that the previously reported error was also in version 1.9.3, but since the error is different now I'm not sure...

@alam-shahul
Copy link
Author

alam-shahul commented Jan 9, 2025

Oh, I just reproduced the original error in 1.10.4 (updated above).

@flying-sheep flying-sheep removed the Needs info❔ More information needed label Jan 10, 2025
@flying-sheep flying-sheep changed the title Sparse PCA doesn't work when .X is csr_array Support for scipy.csr_array/scipy.csc_array Jan 10, 2025
@flying-sheep
Copy link
Member

OK, I’ll fix this more generally than just for PCA I think.

I made #3431 to prepare the scene for this fix for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants