scverse / anndata

Annotated data.
http://anndata.readthedocs.io
BSD 3-Clause "New" or "Revised" License
577 stars 154 forks source link

.X is lost when setting anndata.filename to None #1723

Open eroell opened 3 weeks ago

eroell commented 3 weeks ago

Please make sure these conditions are met

Report

When setting the filename of a backed anndata object to None, it's .X gets lost.

Reproducible example of how to_memory() works as expected:

import anndata as ad
adata = ad.AnnData(
    X = np.array([[1, 2], [3, 4]]),
    obs=pd.DataFrame({"cell_type": ["A", "B"]}),
)
adata.write_h5ad("sample_data.h5ad")

adata = ad.read_h5ad("sample_data.h5ad", backed="r")

adata_mem = adata.to_memory()

adata_mem.X
array([[1, 2],
       [3, 4]])

Example of how using the .filename setter looses X:

import anndata as ad
adata = ad.AnnData(
    X = np.array([[1, 2], [3, 4]]),
    obs=pd.DataFrame({"cell_type": ["A", "B"]}),
)
adata.write_h5ad("sample_data.h5ad")

adata = ad.read_h5ad("sample_data.h5ad", backed="r")

# This should just put the anndata object to memory mode I suppose
adata.filename = None

adata.X is None
True

Running this on the main branch.

might become irrelevant in the future, but the fix seems to be a simple bug in _to_memory_mode.

Versions

anndata 0.11.0rc4.dev4+g3260222 awkward 2.6.8 numpy 2.0.0 pandas 2.2.2 scanpy 1.10.0rc2.dev126+g48706caa session_info 1.0.0

ilan-gold commented 1 week ago

This should just put the anndata object to memory mode I suppose

Given our current rules (https://anndata.readthedocs.io/en/latest/generated/anndata.AnnData.filename.html), I would probably agree.