[x] I have checked that this issue has not already been reported.
[x] I have confirmed this bug exists on the latest version of anndata.
[x] (optional) I have confirmed this bug exists on the master branch of anndata.
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.
Please make sure these conditions are met
Report
When setting the
filename
of a backed anndata object toNone
, it's.X
gets lost.Reproducible example of how
to_memory()
works as expected:Example of how using the
.filename
setter looses X: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