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

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

Open
3 tasks done
eroell opened this issue Oct 21, 2024 · 1 comment · May be fixed by #1724
Open
3 tasks done

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

eroell opened this issue Oct 21, 2024 · 1 comment · May be fixed by #1724
Labels

Comments

@eroell
Copy link
Contributor

eroell commented Oct 21, 2024

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 anndata.
  • (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.

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

@eroell eroell linked a pull request Oct 21, 2024 that will close this issue
3 tasks
@ilan-gold
Copy link
Contributor

ilan-gold commented Nov 4, 2024

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.

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

Successfully merging a pull request may close this issue.

2 participants