Skip to content

Allow storing heterogeneous lists in .uns (as JSON or so) #1979

Description

@jmzhang1911

Hello!

When I use write_h5ad to save the .h5ad file, some contents in uns are automatically converted to np.array objects. Is there any way to prevent this from happening? Thanks a lot!

import anndata
import numpy as np
import pandas as pd

X = np.random.rand(5, 3) 

obs = pd.DataFrame({
    'cell_type': ['T-cell', 'B-cell', 'Monocyte', 'T-cell', 'B-cell']
})


var = pd.DataFrame({
    'gene_name': ['Gene1', 'Gene2', 'Gene3']
})

adata = anndata.AnnData(X=X, obs=obs, var=var)

adata.uns['info'] = {'A': list(range(10))}

print(adata.uns['info'])

{'A': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}

adata.write_h5ad('test.h5ad')
adata = ad.read_h5ad('test.h5ad')
print(adata.uns['info'])

{'A': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])}

Activity

  1. ilan-gold commented on May 8, 2025

    @ilan-gold
    Contributor

    This is expected behavior since we do not store lists as such on disk, treating them as arrays. Technically we could override the behavior of the uns dictionary to understand setting with a list implies array. So I can understand better, is there a use case here for knowing something was a list when it was set before being written out?

    In the future, please use the issue template, bug or enhancement would have been fine.

  2. ilan-gold commented on May 8, 2025

    @ilan-gold
    Contributor

    I don't think we would store lists on disks marked as such, but we could override the behvaior of uns to not store lists in memory, instead using numpy object arrays (or similarly appropriate object).

  3. jmzhang1911 commented on May 12, 2025

    @jmzhang1911
    Author

    Thank you for your reply! My intention was to directly export the uns values as a JSON file. I achieved this goal using the following approach:

    import json
    import numpy as np
    
    class NumpyEncoder(json.JSONEncoder):
        def default(self, obj):
            if isinstance(obj, np.ndarray):
                return obj.tolist()
            return json.JSONEncoder.default(self, obj)
    

    Apologies for taking up your time, and thank you once again for your help!

  4. ilan-gold commented on May 12, 2025

    @ilan-gold
    Contributor

    @jmzhang1911 Without users like you, I would not be paid to have time for you to take up so, thanks for opening the issue. That is indeed actually an interesting use-case, especially for browsers where JSON is expected potentially. I am going to reopen this. What you have is nice for python but it might make sense to distinguish between lists actually for exactly this purpose. I am going to post a question on zulip and see what some people say: https://scverse.zulipchat.com/. If you're not there, check it out!

  5. ilan-gold commented on May 12, 2025

    @ilan-gold
    Contributor

    Writing this out, now that I think about, does not exactly fit the zarr spec at least. So this might require some thought. We have not stepped outside zarr for anything, but maybe there is a way to achieve this sort of thing

  6. added this to the 0.12.0 milestone on May 15, 2025
  7. modified the milestones: 0.12.0, 0.13.0 on May 23, 2025
  8. deleted a comment from github-actions on Jul 23, 2025
  9. changed the title [-]write_h5ad automatically converts data type in uns[/-] [+]Allow storing heterogeneous lists in .uns (as JSON or so)[/+] on Jul 23, 2025
  10. modified the milestones: 0.13.0, 0.14.0 on May 18, 2026
  11. flying-sheep commented on Aug 21, 2026

    @flying-sheep
    Member

    Currently getting bitten by this not existing 😢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions