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

TypeError when storing variable-length list of annotations for each row in obs and save to h5ad #888

Closed
gregjohnso opened this issue Jan 27, 2023 · 1 comment

Comments

@gregjohnso
Copy link

I'm trying to store a list of annotations for each row in an anndata object and save that object to h5ad.

The following snippet shows what I'm trying to do:

import anndata as ad
import numpy as np
import pandas as pd

n_cells = 100
n_genes = 50

adata = ad.AnnData(
    X=np.random.normal(size=(n_cells, n_genes)),
    obs=pd.DataFrame(index=[f"cell_{i}" for i in range(n_cells)]),
    var=pd.DataFrame(index=[f"gene_{i}" for i in range(n_genes)]),
)

# Generate 10 fake annotations
u_annotations = [f"a{i}" for i in range(10)]

# Randomly sample between 0 and 5 annotations for each cell
annotations = [
    np.random.choice(
        u_annotations,
        size=np.random.randint(0, 5),
        replace=False,
    ).tolist()
    for _ in range(n_cells)
]

# Add the annotations to the obs dataframe
adata.obs["annotation"] = annotations

SAVE_PATH = "./TEMP.h5ad"

# Save to h5ad
adata.write_h5ad(SAVE_PATH) #This is where the error gets thrown

# Load from h5ad
adata = ad.read_h5ad(SAVE_PATH)

Unfortunately this results in a TypeError:

TypeError: Can't implicitly convert non-string objects to strings

Above error raised while writing key 'annotation' of <class 'h5py._hl.group.Group'> to /
@ivirshup
Copy link
Member

I'm going to close this as a duplicate of #609. Let me know if you think this is different from that, and should be reopened.

We will have experimental support for this kind of data in the next release (which will have a release candidate soon) in the form of awkward arrays. If you want to try this out now, you can check out: #647.

@ivirshup ivirshup closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants