STOmics / Stereopy

A toolkit of spatial transcriptomic analysis.
MIT License
180 stars 59 forks source link

the h5ad read error for st.io.read_h5ad('data1.h5ad') #229

Closed honghh2018 closed 5 months ago

honghh2018 commented 6 months ago

Hi thanks to developer the great tools for Huada spatial analysis.

The python version was 3.8 stereo version was 1.0.0

I am very confuse about the gef for preprocess analysis and than write to the disk with h5ad format file, but when i read it throught st.io.read_h5ad it occur below error. it very confuse for this. st.io.write_h5ad( data1, use_raw=True, use_result=True, key_record={'cluster':['louvain','annotation']}, output='./data1.h5ad' )

Above error raised while reading key '/annotation@cluster' of type <class 'h5py._hl.group.Group'> from /. detail error

IORegistryError Traceback (most recent call last) File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/_io/utils.py:202, in report_read_key_on_error..func_wrapper(*args, *kwargs) 201 try: --> 202 return func(args, **kwargs) 203 except Exception as e:

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/_io/specs/registry.py:230, in Reader.read_elem(self, elem, modifiers) 228 from functools import partial --> 230 read_func = self.registry.get_reader( 231 type(elem), get_spec(elem), frozenset(modifiers) 232 ) 233 read_func = partial(read_func, _reader=self)

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/_io/specs/registry.py:143, in IORegistry.get_reader(self, src_type, spec, modifiers) 142 else: --> 143 raise IORegistryError._from_read_parts( 144 "read", _REGISTRY.read, src_type, spec 145 )

IORegistryError: No read method registered for IOSpec(encoding_type='dataframe', encoding_version='') from <class 'h5py._hl.group.Group'>. You may need to update your installation of anndata.

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

AnnDataReadError Traceback (most recent call last) Cell In[67], line 3 1 ################读入朝阳的数据 2 # data1=st.io.stereo_h5ad('./customer_preprocess_h5ad/data1.h5ad') ----> 3 data1=st.io.read_h5ad('./customer_preprocess_h5ad/data1.h5ad')

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/stereopy-1.0.0-py3.8.egg/stereo/io/reader.py:683, in read_h5ad(file_path, anndata, flavor, bin_type, bin_size, kwargs) 681 if file_path is not None and anndata is not None: 682 raise Exception("'file_path' and 'anndata' only can input one of them") --> 683 return AnnBasedStereoExpData(h5ad_file_path=file_path, based_ann_data=anndata, bin_type=bin_type, 684 bin_size=bin_size, kwargs) 685 else: 686 raise ValueError("Invalid value for 'flavor'")

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/stereopy-1.0.0-py3.8.egg/stereo/core/stereo_exp_data.py:637, in AnnBasedStereoExpData.init(self, h5ad_file_path, based_ann_data, bin_type, bin_size, spatial_key, *args, **kwargs) 635 self._ann_data = based_ann_data 636 else: --> 637 self._ann_data = anndata.read_h5ad(h5ad_file_path) 638 self._genes = AnnBasedGene(self._ann_data, self._genes.gene_name) 639 self._cells = AnnBasedCell(self._ann_data, self._cells.cell_name)

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/_io/h5ad.py:243, in read_h5ad(filename, backed, as_sparse, as_sparse_fmt, chunk_size) 240 return read_dataframe(elem) 241 return func(elem) --> 243 adata = read_dispatched(f, callback=callback) 245 # Backwards compat (should figure out which version) 246 if "raw.X" in f:

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/experimental/init.py:58, in read_dispatched(elem, callback) 54 from anndata._io.specs import Reader, _REGISTRY 56 reader = Reader(_REGISTRY, callback=callback) ---> 58 return reader.read_elem(elem)

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/_io/utils.py:204, in report_read_key_on_error..func_wrapper(*args, *kwargs) 202 return func(args, **kwargs) 203 except Exception as e: --> 204 re_raise_error(e, elem)

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/_io/utils.py:185, in report_read_key_on_error..re_raise_error(e, elem) 183 def re_raise_error(e, elem): 184 if isinstance(e, AnnDataReadError): --> 185 raise e 186 else: 187 parent = _get_parent(elem)

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/_io/utils.py:202, in report_read_key_on_error..func_wrapper(*args, *kwargs) 200 break 201 try: --> 202 return func(args, **kwargs) 203 except Exception as e: 204 re_raise_error(e, elem)

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/_io/specs/registry.py:235, in Reader.read_elem(self, elem, modifiers) 233 read_func = partial(read_func, _reader=self) 234 if self.callback is not None: --> 235 return self.callback(read_func, elem.name, elem, iospec=get_spec(elem)) 236 else: 237 return read_func(elem)

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/_io/h5ad.py:224, in read_h5ad..callback(func, elem_name, elem, iospec) 221 def callback(func, elem_name: str, elem, iospec): 222 if iospec.encoding_type == "anndata" or elem_name.endswith("/"): 223 return AnnData( --> 224 **{ 225 # This is covering up backwards compat in the anndata initializer 226 # In most cases we should be able to call func(elen[k]) instead 227 k: read_dispatched(elem[k], callback) 228 for k in elem.keys() 229 if not k.startswith("raw.") 230 } 231 ) 232 elif elem_name.startswith("/raw."): 233 return None

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/_io/h5ad.py:227, in (.0) 221 def callback(func, elem_name: str, elem, iospec): 222 if iospec.encoding_type == "anndata" or elem_name.endswith("/"): 223 return AnnData( 224 **{ 225 # This is covering up backwards compat in the anndata initializer 226 # In most cases we should be able to call func(elen[k]) instead --> 227 k: read_dispatched(elem[k], callback) 228 for k in elem.keys() 229 if not k.startswith("raw.") 230 } 231 ) 232 elif elem_name.startswith("/raw."): 233 return None

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/experimental/init.py:58, in read_dispatched(elem, callback) 54 from anndata._io.specs import Reader, _REGISTRY 56 reader = Reader(_REGISTRY, callback=callback) ---> 58 return reader.read_elem(elem)

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/_io/utils.py:204, in report_read_key_on_error..func_wrapper(*args, *kwargs) 202 return func(args, **kwargs) 203 except Exception as e: --> 204 re_raise_error(e, elem)

File /share/apps/virtualEnv/miniconda3-py38/envs/huadaStereopy-st/lib/python3.8/site-packages/anndata/_io/utils.py:188, in report_read_key_on_error..re_raise_error(e, elem) 186 else: 187 parent = _get_parent(elem) --> 188 raise AnnDataReadError( 189 f"Above error raised while reading key {elem.name!r} of " 190 f"type {type(elem)} from {parent}." 191 ) from e

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

I want to read it from disk and than go process comply with the website document https://stereopy.readthedocs.io/en/latest/Tutorials%28Multi-sample%29/MSData_Clustering.html

But,unfortunately, it failed me, How to fix this issue? hope the reply Best, hanhuihong

tanliwei-coder commented 6 months ago

Sorry for making you confuse, the function write_h5ad is used to save data as a h5 format file which exclusively belongs to Stereopy, it is not a Anndata format, in this case, you need to set parameter flavor of read_h5ad to 'stereopy'.

tanliwei-coder commented 6 months ago

Futhermore, if you want to save data as an Anndata fromat, you can use st.io.stereo_to_anndata, api is here.