anndata icon indicating copy to clipboard operation
anndata copied to clipboard

Cannot set dense .X attribute with sparse matrix when adata is a view

Open MxMstrmn opened this issue 2 years ago • 6 comments

It is not possible to set the .X attribute when adata is a view. The resulting error does not hint at this and I would expect anndata to handle this in the background or give some sort of warining. It is related to this part of the code in [anndata/_core/anndata.py].(https://github.com/scverse/anndata/blob/master/anndata/_core/anndata.py#L683-L688)

Minimal reproducible example

import anndata
import numpy
from scipy.sparse import csr_matrix

x = np.zeros((100, 30))
adata = anndata.AnnData(x)[:30]
adata.X = csr_matrix(x[:30])

while adata.X = x[:30] works just fine.

Traceback

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: float() argument must be a string or a number, not 'csr_matrix'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
/tmp/ipykernel_8909/1816563358.py in <module>
      5 x = np.zeros((100, 30))
      6 _adata = anndata.AnnData(x)[:30]
----> 7 _adata.X = csr_matrix(x[:30])

~/miniconda3/envs/scib-pipeline-R4/lib/python3.7/site-packages/anndata/_core/anndata.py in X(self, value)
    686                     ):
    687                         value = sparse.coo_matrix(value)
--> 688                     self._adata_ref._X[oidx, vidx] = value
    689                 else:
    690                     self._X = value

ValueError: setting an array element with a sequence.

with @michalk8.

MxMstrmn avatar May 04 '22 19:05 MxMstrmn

What were you expecting to happen to adata.X here? Were you expecting adata to be a view or an acutal AnnData? Were you expecting adata.X to be sparse or dense?


What AnnData is trying to do here is actually do an in-place assignment on the AnnData created by anndata.AnnData(x), not the view created when you assigned it. It just happens that assigning a sparse array here doesn't work.

ivirshup avatar Jun 21 '22 20:06 ivirshup

What were you expecting to happen to adata.X here? Were you expecting adata to be a view or an acutal AnnData? Were you expecting adata.X to be sparse or dense?

For me it would be to maintain the consistency, since adata.X = csr_matrix(x[:30]).A is possible.

michalk8 avatar Jun 21 '22 21:06 michalk8

I agree that should work. I guess I was asking which behavior were you expecting it to be consistent with?

For

a = AnnData(x)
v = a[:30]
v.X = x[-30:]

Were you expecting the assignment to v to update a?

ivirshup avatar Jun 22 '22 14:06 ivirshup

I guess I was expected to create a copy from the view in that case and not alter a. Not perfectly sure if this is consistent. Does v.X = csr_matrix(x[:30]).A alter a?

MxMstrmn avatar Jun 27 '22 14:06 MxMstrmn

This issue has been automatically marked as stale because it has not had recent activity. Please add a comment if you want to keep the issue open. Thank you for your contributions!

github-actions[bot] avatar Nov 18 '22 11:11 github-actions[bot]

This issue has been automatically marked as stale because it has not had recent activity. Please add a comment if you want to keep the issue open. Thank you for your contributions!

github-actions[bot] avatar Jun 17 '23 02:06 github-actions[bot]