STOmics / Stereopy

A toolkit of spatial transcriptomic analysis.
MIT License
184 stars 64 forks source link

AnnDataReadError: Above error raised while reading key '/layers' of type <class 'h5py._hl.group.Group'> from /. #173

Closed Biomamba closed 11 months ago

Biomamba commented 11 months ago

Dear author: Thank u for your contribution. When I run Multi-slice part of tutorials, I met the following error. `import sys import os from natsort import natsorted import stereo as st from stereo.core.ms_data import MSData from stereo.core.ms_pipeline import slice_generator

import warnings warnings.filterwarnings('ignore')

prepara for input directory

data_dir = './demo.data/3D_AnnData_0.8.0/'

data_list=[] for fn in os.listdir(data_dir): data_list.append(os.path.join(data_dir, fn))

ensure data order by naming them regularly

data_list = natsorted(data_list)

construct MSData object

ms_data = MSData(_relationship='other', _var_type='intersect')

when come to loaded data object

ms_data = MSData(_data_list=[data1, data2], _names=['s1', 's2'], _relationship='other', _var_type='intersect')

add all slices into MSData

for slice in data_list: ms_data += st.io.read_h5ad(file_path=slice, bin_type='bins', bin_size=1)`

KeyError Traceback (most recent call last) File ~/miniconda3/envs/stereopy/lib/python3.8/site-packages/anndata/_io/utils.py:156, in report_read_key_on_error..func_wrapper(elem, *args, *kwargs) 155 try: --> 156 return func(elem, args, **kwargs) 157 except Exception as e:

File ~/miniconda3/envs/stereopy/lib/python3.8/site-packages/anndata/_io/h5ad.py:532, in read_group(group) 531 if encoding_type: --> 532 EncodingVersions[encoding_type].check( 533 group.name, group.attrs["encoding-version"] 534 ) 535 if encoding_type in {None, "raw"}:

File ~/miniconda3/envs/stereopy/lib/python3.8/enum.py:387, in EnumMeta.getitem(cls, name) 386 def getitem(cls, name): --> 387 return cls._membermap[name]

KeyError: 'dict'

During handling of the above exception, another exception occurred:

AnnDataReadError Traceback (most recent call last) Cell In[34], line 30 25 # when come to loaded data object 26 # ms_data = MSData(_data_list=[data1, data2], _names=['s1', 's2'], _relationship='other', _var_type='intersect') 27 28 # add all slices into MSData 29 for slice in data_list: ---> 30 ms_data += st.io.read_h5ad(file_path=slice, bin_type='bins', bin_size=1)

File ~/miniconda3/envs/stereopy/lib/python3.8/site-packages/stereo/utils/read_write_utils.py:37, in ReadWriteUtils.check_file_exists..wrapped(*args, *kwargs) 35 else: 36 raise FileNotFoundError("Please ensure there is a file") ---> 37 return func(args, **kwargs)

File ~/miniconda3/envs/stereopy/lib/python3.8/site-packages/stereo/io/reader.py:589, in read_h5ad(file_path, flavor, kwargs) 587 return read_stereo_h5ad(file_path, kwargs) 588 elif flavor == 'scanpy': --> 589 return AnnBasedStereoExpData(file_path) 590 else: 591 raise ValueError("Invalid value for 'flavor'")

File ~/miniconda3/envs/stereopy/lib/python3.8/site-packages/stereo/core/stereo_exp_data.py:593, in AnnBasedStereoExpData.init(self, h5ad_file_path, based_ann_data, *args, **kwargs) 591 self._ann_data = based_ann_data 592 else: --> 593 self._ann_data = anndata.read_h5ad(h5ad_file_path) 594 self._genes = AnnBasedGene(self._ann_data, self._genes.gene_name) 595 self._cells = AnnBasedCell(self._ann_data, self._cells.cell_name)

File ~/miniconda3/envs/stereopy/lib/python3.8/site-packages/anndata/_io/h5ad.py:426, in read_h5ad(filename, backed, as_sparse, as_sparse_fmt, chunk_size) 424 d[k] = read_dataframe(f[k]) 425 else: # Base case --> 426 d[k] = read_attribute(f[k]) 428 d["raw"] = _read_raw(f, as_sparse, rdasp) 430 X_dset = f.get("X", None)

File ~/miniconda3/envs/stereopy/lib/python3.8/functools.py:875, in singledispatch..wrapper(*args, *kw) 871 if not args: 872 raise TypeError(f'{funcname} requires at least ' 873 '1 positional argument') --> 875 return dispatch(args[0].class)(args, **kw)

File ~/miniconda3/envs/stereopy/lib/python3.8/site-packages/anndata/_io/utils.py:162, in report_read_key_on_error..func_wrapper(elem, *args, **kwargs) 160 else: 161 parent = _get_parent(elem) --> 162 raise AnnDataReadError( 163 f"Above error raised while reading key {elem.name!r} of " 164 f"type {type(elem)} from {parent}." 165 )

AnnDataReadError: Above error raised while reading key '/layers' of type <class 'h5py._hl.group.Group'> from /.

Cloud you please tell me how to solve it?

Biomamba commented 11 months ago

I have solved this problem by updating the anndata:
pip install anndata==0.8.0

tanliwei-coder commented 11 months ago

Because some hitorical reasons, the anndata on which stereopy depends must be the version 0.7.5, but the 0.8.0 anndata is not compatible with 0.7.5 and the demo data you used were generated by 0.8.0 anndata, therefore, you met this error, you can download '3D_AnnData_0.7.5' demo data to use to avoid this error, of course, upgrading anndata to 0.8.0 is also worked.

Biomamba commented 11 months ago

Because some hitorical reasons, the anndata on which stereopy depends must be the version 0.7.5, but the 0.8.0 anndata is not compatible with 0.7.5 and the demo data you used were generated by 0.8.0 anndata, therefore, you met this error, you can download '3D_AnnData_0.7.5' demo data to use to avoid this error, of course, upgrading anndata to 0.8.0 is also worked.

Thank you for your reply. I have solved this problem by updating the anndata.