cellrank: saving results fails

The adata file from the tutorial https://cellrank.readthedocs.io/en/stable/cellrank_basics.html can not be saved using adata.write(‘CellRank_Tutorial.h5ad’).

## after the tutorial https://cellrank.readthedocs.io/en/stable/cellrank_basics.html
adata.write('CellRank_Tutorial.h5ad')
...

adata.write('CellRank_Tutorial.h5ad')
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/anndata/_io/utils.py", line 209, in func_wrapper
    return func(elem, key, val, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/anndata/_io/h5ad.py", line 148, in write_not_implemented
    raise NotImplementedError(
NotImplementedError: Failed to write value for uns/coarse_fwd, since a writer for type <class 'anndata._core.anndata.AnnData'> has not been implemented yet.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/dist-packages/anndata/_core/anndata.py", line 1912, in write_h5ad
    _write_h5ad(
  File "/usr/local/lib/python3.8/dist-packages/anndata/_io/h5ad.py", line 118, in write_h5ad
    write_attribute(f, "uns", adata.uns, dataset_kwargs=dataset_kwargs)
  File "/usr/lib/python3.8/functools.py", line 875, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/usr/local/lib/python3.8/dist-packages/anndata/_io/h5ad.py", line 130, in write_attribute_h5ad
    _write_method(type(value))(f, key, value, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/anndata/_io/h5ad.py", line 294, in write_mapping
    write_attribute(f, f"{key}/{sub_key}", sub_value, dataset_kwargs=dataset_kwargs)
  File "/usr/lib/python3.8/functools.py", line 875, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/usr/local/lib/python3.8/dist-packages/anndata/_io/h5ad.py", line 130, in write_attribute_h5ad
    _write_method(type(value))(f, key, value, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/anndata/_io/utils.py", line 212, in func_wrapper
    raise type(e)(
NotImplementedError: Failed to write value for uns/coarse_fwd, since a writer for type <class 'anndata._core.anndata.AnnData'> has not been implemented yet.

Above error raised while writing key 'uns/coarse_fwd' of <class 'h5py._hl.files.File'> from /.
...

Versions:

cellrank==1.5.1 scanpy==1.7.2 anndata==0.7.8 numpy==1.21.2 numba==0.55.0 scipy==1.7.3 pandas==1.3.5 pygpcca==1.0.3 scikit-learn==1.0.2 statsmodels==0.13.1 python-igraph==0.8.3 scvelo==0.2.4 pygam==0.8.0 matplotlib==3.5.1 seaborn==0.11.2

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 30 (2 by maintainers)

Most upvoted comments

Should’ve be fixed on master long ago.

Hi @stela2502 ,

thanks for raising this issue, can reproduce. Quickest way to fix this is to upgrade to the newest version of AnnData as pip install git+https://github.com/theislab/anndata, though new release should be coming out soon. Not sure exactly why I’ve decided to store the coarse-grained transtions as AnnData (could be just pandas.DataFrame), so will change it later.

Hello All, I am also using Anndata=0.8.0. I am not able to save adata in h5ad format. Here is the sample code import anndata adata=anndata.read_h5ad("/home/Akila/input.h5ad") adata.write("/home/Akila/BA9.h5ad")

Error message: TypeError: No method has been defined for writing <class ‘numpy.matrix’> elements to <class ‘h5py._hl.group.Group’>

Above error raised while writing key ‘X’ of <class ‘h5py._hl.group.Group’> to /

Please advice

Thank you for that snippet, yes, I am able to save adata now. While an imperfect solution, it means that I can move forward with this line of analysis. Looking forward to future fixes, but for now I’m set, thanks for your time!

Thanks for your continued attention to this. My versions are now anndata==0.7.8, cellrank=git+https://github.com/theislab/cellrank@8efabc8bba90262638721405c7ef7e85d9301fd7, and I receive the following error:

NotImplementedError                       Traceback (most recent call last)
File ~/.conda/envs/scvelo/lib/python3.9/site-packages/anndata/_io/utils.py:209, in report_write_key_on_error.<locals>.func_wrapper(elem, key, val, *args, **kwargs)
    208 try:
--> 209     return func(elem, key, val, *args, **kwargs)
    210 except Exception as e:

File ~/.conda/envs/scvelo/lib/python3.9/site-packages/anndata/_io/h5ad.py:148, in write_not_implemented(f, key, value, dataset_kwargs)
    143 @report_write_key_on_error
    144 def write_not_implemented(f, key, value, dataset_kwargs=MappingProxyType({})):
    145     # If it’s not an array, try and make it an array. If that fails, pickle it.
    146     # Maybe rethink that, maybe this should just pickle,
    147     # and have explicit implementations for everything else
--> 148     raise NotImplementedError(
    149         f"Failed to write value for {key}, "
    150         f"since a writer for type {type(value)} has not been implemented yet."
    151     )

NotImplementedError: Failed to write value for uns/coarse_fwd/coarse_init_dist, since a writer for type <class 'pandas.core.series.Series'> has not been implemented yet.

The above exception was the direct cause of the following exception:

NotImplementedError                       Traceback (most recent call last)
Input In [12], in <cell line: 2>()
      1 import anndata
----> 2 adata.write("scvelo.cr.5.h5ad")

File ~/.conda/envs/scvelo/lib/python3.9/site-packages/anndata/_core/anndata.py:1912, in AnnData.write_h5ad(self, filename, compression, compression_opts, force_dense, as_dense)
   1909 if filename is None:
   1910     filename = self.filename
-> 1912 _write_h5ad(
   1913     Path(filename),
   1914     self,
   1915     compression=compression,
   1916     compression_opts=compression_opts,
   1917     force_dense=force_dense,
   1918     as_dense=as_dense,
   1919 )
   1921 if self.isbacked:
   1922     self.file.filename = filename

File ~/.conda/envs/scvelo/lib/python3.9/site-packages/anndata/_io/h5ad.py:118, in write_h5ad(filepath, adata, force_dense, as_dense, dataset_kwargs, **kwargs)
    116 write_attribute(f, "varp", adata.varp, dataset_kwargs=dataset_kwargs)
    117 write_attribute(f, "layers", adata.layers, dataset_kwargs=dataset_kwargs)
--> 118 write_attribute(f, "uns", adata.uns, dataset_kwargs=dataset_kwargs)

File ~/.conda/envs/scvelo/lib/python3.9/functools.py:888, in singledispatch.<locals>.wrapper(*args, **kw)
    884 if not args:
    885     raise TypeError(f'{funcname} requires at least '
    886                     '1 positional argument')
--> 888 return dispatch(args[0].__class__)(*args, **kw)

File ~/.conda/envs/scvelo/lib/python3.9/site-packages/anndata/_io/h5ad.py:130, in write_attribute_h5ad(f, key, value, *args, **kwargs)
    128 if key in f:
    129     del f[key]
--> 130 _write_method(type(value))(f, key, value, *args, **kwargs)

File ~/.conda/envs/scvelo/lib/python3.9/site-packages/anndata/_io/h5ad.py:294, in write_mapping(f, key, value, dataset_kwargs)
    292 def write_mapping(f, key, value, dataset_kwargs=MappingProxyType({})):
    293     for sub_key, sub_value in value.items():
--> 294         write_attribute(f, f"{key}/{sub_key}", sub_value, dataset_kwargs=dataset_kwargs)

File ~/.conda/envs/scvelo/lib/python3.9/functools.py:888, in singledispatch.<locals>.wrapper(*args, **kw)
    884 if not args:
    885     raise TypeError(f'{funcname} requires at least '
    886                     '1 positional argument')
--> 888 return dispatch(args[0].__class__)(*args, **kw)

File ~/.conda/envs/scvelo/lib/python3.9/site-packages/anndata/_io/h5ad.py:130, in write_attribute_h5ad(f, key, value, *args, **kwargs)
    128 if key in f:
    129     del f[key]
--> 130 _write_method(type(value))(f, key, value, *args, **kwargs)

File ~/.conda/envs/scvelo/lib/python3.9/site-packages/anndata/_io/h5ad.py:294, in write_mapping(f, key, value, dataset_kwargs)
    292 def write_mapping(f, key, value, dataset_kwargs=MappingProxyType({})):
    293     for sub_key, sub_value in value.items():
--> 294         write_attribute(f, f"{key}/{sub_key}", sub_value, dataset_kwargs=dataset_kwargs)

File ~/.conda/envs/scvelo/lib/python3.9/functools.py:888, in singledispatch.<locals>.wrapper(*args, **kw)
    884 if not args:
    885     raise TypeError(f'{funcname} requires at least '
    886                     '1 positional argument')
--> 888 return dispatch(args[0].__class__)(*args, **kw)

File ~/.conda/envs/scvelo/lib/python3.9/site-packages/anndata/_io/h5ad.py:130, in write_attribute_h5ad(f, key, value, *args, **kwargs)
    128 if key in f:
    129     del f[key]
--> 130 _write_method(type(value))(f, key, value, *args, **kwargs)

File ~/.conda/envs/scvelo/lib/python3.9/site-packages/anndata/_io/utils.py:212, in report_write_key_on_error.<locals>.func_wrapper(elem, key, val, *args, **kwargs)
    210 except Exception as e:
    211     parent = _get_parent(elem)
--> 212     raise type(e)(
    213         f"{e}\n\n"
    214         f"Above error raised while writing key {key!r} of {type(elem)}"
    215         f" from {parent}."
    216     ) from e

NotImplementedError: Failed to write value for uns/coarse_fwd/coarse_init_dist, since a writer for type <class 'pandas.core.series.Series'> has not been implemented yet.

Above error raised while writing key 'uns/coarse_fwd/coarse_init_dist' of <class 'h5py._hl.files.File'> from /.