scverse / scvi-tools

Deep probabilistic analysis of single-cell and spatial omics data
http://scvi-tools.org/
BSD 3-Clause "New" or "Revised" License
1.2k stars 344 forks source link

Model trained on 0.14 and model load on 0.20, error shows: no "ind_x" #2338

Closed zhenxingjian closed 1 month ago

zhenxingjian commented 9 months ago

KeyError Traceback (most recent call last) Cell In[8], line 1 ----> 1 RNA_ATAC_model = scvi.model.MULTIVI.load(os.path.join(model_path, cur_best_model_path), adata=data_train)

File ~/miniconda3/envs/py3/lib/python3.10/site-packages/scvi/model/base/_base_model.py:660, in BaseModelClass.load(cls, dir_path, adata, use_gpu, prefix, backup_url) 656 # Calling setup_anndata method with the original arguments passed into 657 # the saved model. This enables simple backwards compatibility in the case of 658 # newly introduced fields or parameters. 659 method_name = registry.get(_SETUP_METHOD_NAME, "setup_anndata") --> 660 getattr(cls, method_name)( 661 adata, source_registry=registry, **registry[_SETUP_ARGS_KEY] 662 ) 664 model = _initialize_model(cls, adata, attr_dict) 665 model.module.on_load(model)

File ~/miniconda3/envs/py3/lib/python3.10/site-packages/scvi/model/_multivi.py:1088, in MULTIVI.setup_anndata(cls, adata, layer, batch_key, size_factor_key, categorical_covariate_keys, continuous_covariate_keys, protein_expression_obsm_key, protein_names_uns_key, kwargs) 1074 anndata_fields.append( 1075 ProteinObsmField( 1076 REGISTRY_KEYS.PROTEIN_EXP_KEY, (...) 1082 ) 1083 ) 1085 adata_manager = AnnDataManager( 1086 fields=anndata_fields, setup_method_args=setup_method_args 1087 ) -> 1088 adata_manager.register_fields(adata, kwargs) 1089 cls.register_manager(adata_manager)

File ~/miniconda3/envs/py3/lib/python3.10/site-packages/scvi/data/_manager.py:174, in AnnDataManager.register_fields(self, adata, source_registry, transfer_kwargs) 171 self._validate_anndata_object(adata) 173 for field in self.fields: --> 174 self._add_field( 175 field=field, 176 adata=adata, 177 source_registry=source_registry, 178 transfer_kwargs, 179 ) 181 # Save arguments for register_fields. 182 self._source_registry = deepcopy(source_registry)

File ~/miniconda3/envs/py3/lib/python3.10/site-packages/scvi/data/_manager.py:210, in AnnDataManager._add_field(self, field, adata, source_registry, transfer_kwargs) 206 if not field.is_empty: 207 # Transfer case: Source registry is used for validation and/or setup. 208 if source_registry is not None: 209 field_registry[_constants._STATE_REGISTRY_KEY] = field.transfer_field( --> 210 source_registry[_constants._FIELD_REGISTRIES_KEY][ 211 field.registry_key 212 ][_constants._STATE_REGISTRY_KEY], 213 adata, 214 transfer_kwargs, 215 ) 216 else: 217 field_registry[_constants._STATE_REGISTRY_KEY] = field.register_field( 218 adata 219 )

KeyError: 'ind_x'

RNA_ATAC_model = scvi.model.MULTIVI.load(model_path)

Versions:

0.20.3 model is trained on 0.14.6

martinkim0 commented 9 months ago

Hi, please use the convert_legacy_save method for models saved prior to 0.15.

zhenxingjian commented 9 months ago

Hi, I used scvi.model.MULTIVI.convert_legacy_save( os.path.join(model_path, cur_best_model_path), os.path.join(model_path, cur_best_model_path), overwrite=True, ) RNA_ATAC_model_raw = scvi.model.MULTIVI.load(os.path.join(model_path, cur_best_model_path))

But still get the same error. KeyError: 'ind_x'

canergen commented 1 month ago

I'm sorry you ran into this. MultiVI was reformatted after the initial release. We will update our release notes with scvi-tools 1.2. Specifically PR #1697 added indices to setup_anndata. This is a breaking change and models beforehand should be loaded with versions before this PR.