Skip to content

adata.write errors #52

Closed
Closed

Description

@huidongchen

Hi guys,

I'm really enjoying the efficiency and scalability of anndata. I have been using it to manage my large datasets. Excellent work!

But unfortunately i'm having bad luck in writing adata object to .h5ad-formatted hdf5 file.

adata.write(results_file)

It takes very long time and ends up with one of the errors below (I haven't managed to store adata object in file)

  1. IndexError: too many indices for array
    2)ValueError: name already used as a name or title

In adata object, I'm using different annotations including uns, obs, vars, obsm and it works smoothly without any errors. But each time i try to write it to file, the aforementioned errors are thrown out.

I will really appreciate your opinions. Thanks in advance!

Activity

  1. Koncopd commented on Aug 22, 2018

    @Koncopd
    Member

    Hi, @huidongchen.

    Do you write with sparse adata.X or dense?
    Do you work in backed or in-memory mode?

  2. huidongchen commented on Aug 27, 2018

    @huidongchen
    ContributorAuthor

    Hi ,

    sorry about the slow response. I'm using dense matrix and in-memory mode.

    It seems that it has something to do with 'uns' annotation. If I feed an anndata object to the unstructured annotation, when i save it, the writing process will get stuck and report error in the end.

    I'm attaching a test script here. Hope it will help.

    Thanks again.
    test.ipynb.zip

  3. huidongchen commented on Aug 28, 2018

    @huidongchen
    ContributorAuthor

    Hi,

    I'm just following up on adata.write. 'uns' annotation doesn't seemingly work for rpy2 object either. I got the following warning when trying to write adata to .h5ad-formatted hdf5 file. Is a python object required in order to write anndata object to files?

    UserWarning: Could not save field with key = "uns/X_r_obj" to hdf5 file.

    I will appreciate your help.

  4. falexwolf commented on Aug 28, 2018

    @falexwolf
    Member

    Hi!

    The line

    adata.uns['raw'] = adata.copy()
    

    is invalid. But unfortunately, this doesn't throw an error. The .uns attribute allows for simple data types, numpy arrays, sparse matrices and simple dictionaries.

    Use the .raw attribute of AnnData for doing what you want to do

    adata.raw = adata
    

    Let me think about whether there is an easy way of testing the type upon assignment.

    Alex

  5. flying-sheep commented on Aug 29, 2018

    @flying-sheep
    Member

    well, of course there is, right?

    def check_simple_type(obj):
        return any(check(obj) for check in checks)
    
    def check_simple_dict(obj):
        return isinstance(x, dict) and all(
            check_simple_type(o)
            for seq in [x.keys(), x.values()]
            for o in seq
        )
    
    checks = [
        lambda x: isinstance(x, int),
        lambda x: isinstance(x, str),
        ...,
        np.isarray,
        sp.sparse.issparse,
        check_simple_dict,
    ]
  6. huidongchen commented on Aug 29, 2018

    @huidongchen
    ContributorAuthor

    Thanks a lot @Koncopd @falexwolf . It's very helpful. I'm closing the issue.

  7. falexwolf commented on Sep 3, 2018

    @falexwolf
    Member

    Yes, of course, in a custom class - but that involves subclassing OrderedDict. It might be that this is not such a trouble as for DataFrames (also for arrays it wasn't trivial, as we saw). But that's the thing to be investigated before wrapping .uns's __setitem__ as you prototyped, right?

  8. huidongchen commented on Sep 10, 2018

    @huidongchen
    ContributorAuthor

    Hi, sorry that i have to reopen the issue. I'm having some trouble again when writing anndata object to a file.

    I'm using dense matrix and in-memory mode.

    When I ran adata.write(results_file), i got the error ''RuntimeError: Unable to create link (message type not found)"

    5py/_objects.pyx in h5py._objects.with_phil.wrapper()
    
    h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
    
    h5py/h5o.pyx in h5py.h5o.link()
    
    RuntimeError: Unable to create link (message type not found)`
    

    But if I run adata.write_loom(results_file), it works well.

  9. falexwolf commented on Sep 12, 2018

    @falexwolf
    Member

    I'm sorry but with this amount of information, I cannot say anything.

  10. huidongchen commented on Sep 13, 2018

    @huidongchen
    ContributorAuthor

    Hi Alex,

    I'm sorry about the limited information. I was running a big dataset so it was a bit difficult to provide an easily reproducible script.

    But I guess i finally figured it out. I stored a dictionary into anndata unstructured annotation .uns. One keyword contains a slash symbol '/' , which resulted in the error 'RuntimeError: Unable to create link (message type not found)'`

    'repr'+'eval' strategy helps solve this issue.

  11. falexwolf commented on Sep 17, 2018

    @falexwolf
    Member

    Ah, thank you for this information! Indeed, HDF5 has problems with slashes. I'll reopen this issue so that we can print out a warning in the future.

  12. jorvis commented on Jan 15, 2019

    @jorvis

    I ran into this too:

    /opt/Python-3.7.0/lib/python3.7/site-packages/anndata/readwrite/write.py:301: UserWarning: Could not save field with key = "uns/cell_type_categories" to hdf5 file.
      'to hdf5 file.'.format(key))
    
  13. jorvis commented on Jan 16, 2019

    @jorvis

    Mine actually happened because there were non-unicode values in the cells of the cell_type column. Just a note.

  14. falexwolf commented on Jan 21, 2019

    @falexwolf
    Member

    Interesting, how come you have non-unicode values? Do you need them? Unicode is already super rich. A previous version of anndata only supported ASCII, which was limiting in some cases...

  15. jorvis commented on Jan 21, 2019

    @jorvis

    We absolutely don't need them, no. It happened by chance because user copy/pasted a block from a web site into their input. I corrected this and we were fine.

  16. flying-sheep commented on Jan 21, 2019

    @flying-sheep
    Member

    Can you please give a reproducible example? Python does unicode by default, there should be almost no effort to fix this.

    The only reason we don’t have tests for this is that I wouldn’t think it’d ever be a problem.

  17. jorvis commented on Jan 21, 2019

    @jorvis

    Attaching a demo dataset with test conversion script.
    issue_52_demo.tar.gz

    For me, it errors like this:

    $ ./demo_load_maggie.py 
    Variable names are not unique. To make them unique, call `.var_names_make_unique`.
    /opt/Python-3.7.0/lib/python3.7/site-packages/anndata/readwrite/write.py:301: UserWarning: Could not save field with key = "uns/condition_categories" to hdf5 file.
      'to hdf5 file.'.format(key))
    
  18. tantk commented on Aug 8, 2019

    @tantk

    Hi all, i have been using scanpy library for three months now, just discovered that adata.write will give
    "ValueError: name already used as a name or title" error if adata.obs contains a column name 'index'.

    Just sharing in case anyone else has this problem too.

  19. jeffhsu3 commented on Feb 12, 2020

    @jeffhsu3

    I get a write error:
    Above error raised while writing key 0 of <class 'h5py._hl.group.Group'> from /.
    if a name of the index to the var table is an int. Setting the name to a string fixes this.

  20. flying-sheep commented on Feb 12, 2020

    @flying-sheep
    Member

    Yeah, this is not supported. We should improve the error in this case though

  21. ivirshup commented on Feb 13, 2020

    @ivirshup
    Member

    I'm going to close this, since new writing problems should open up new issues.

  22. zznx commented on Oct 13, 2020

    @zznx

    I get a write error:
    Above error raised while writing key 0 of <class 'h5py._hl.group.Group'> from /.
    if a name of the index to the var table is an int. Setting the name to a string fixes this.

    I have the same problem. How did you solve it? Thanks in advance!

  23. yebinase commented on Jan 21, 2021

    @yebinase

    Hi, for me it has something to do with duplicated column ( or duplication between column and index), you might want to remove that

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions