BayraktarLab / cell2location

Comprehensive mapping of tissue cell architecture via integrated single cell and spatial transcriptomics (cell2location model)
https://cell2location.readthedocs.io/en/latest/
Apache License 2.0
324 stars 58 forks source link

Error in export_posterior #346

Closed orange-3711 closed 10 months ago

orange-3711 commented 10 months ago

Hello, I'm having some problems. After the model is created, the next step is to run the error about the gene, because this part of the gene does not exist in ref data?

Minimal code sample (that we can run without your data, using public data)

adata_vis = mod.export_posterior(
    adata_vis, sample_kwargs={'num_samples': 100, 'batch_size': mod.adata.n_obs, 'use_gpu': False}
)
KeyError                                  Traceback (most recent call last)
Cell In[127], line 1
----> 1 adata_vis = mod.export_posterior(
      2     adata_vis, sample_kwargs={'num_samples': 100, 'batch_size': mod.adata.n_obs, 'use_gpu': False}
      3 )

File /dataprocess/jupyter/project_examples/Examples0008/venv/lib/python3.11/site-packages/cell2location/models/reference/_reference_model.py:258, in RegressionModel.export_posterior(self, adata, sample_kwargs, export_slot, add_to_varm, scale_average_detection, use_quantiles)
    256     sample_df = sample_df * self.samples[f"post_sample_{k}"]["detection_y_c"].mean()
    257 try:
--> 258     adata.varm[f"{k}_per_cluster_mu_fg"] = sample_df.loc[adata.var.index, :]
    259 except ValueError:
    260     # Catching weird error with obsm: `ValueError: value.index does not match parent’s axis 1 names`
    261     adata.var[sample_df.columns] = sample_df.loc[adata.var.index, :]

File /dataprocess/jupyter/project_examples/Examples0008/venv/lib/python3.11/site-packages/pandas/core/indexing.py:1067, in _LocationIndexer.__getitem__(self, key)
   1065     if self._is_scalar_access(key):
   1066         return self.obj._get_value(*key, takeable=self._takeable)
-> 1067     return self._getitem_tuple(key)
   1068 else:
   1069     # we by definition only have the 0th axis
   1070     axis = self.axis or 0

File /dataprocess/jupyter/project_examples/Examples0008/venv/lib/python3.11/site-packages/pandas/core/indexing.py:1256, in _LocIndexer._getitem_tuple(self, tup)
   1253 if self._multi_take_opportunity(tup):
   1254     return self._multi_take(tup)
-> 1256 return self._getitem_tuple_same_dim(tup)

File /dataprocess/jupyter/project_examples/Examples0008/venv/lib/python3.11/site-packages/pandas/core/indexing.py:924, in _LocationIndexer._getitem_tuple_same_dim(self, tup)
    921 if com.is_null_slice(key):
    922     continue
--> 924 retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
    925 # We should never have retval.ndim < self.ndim, as that should
    926 #  be handled by the _getitem_lowerdim call above.
    927 assert retval.ndim == self.ndim

File /dataprocess/jupyter/project_examples/Examples0008/venv/lib/python3.11/site-packages/pandas/core/indexing.py:1301, in _LocIndexer._getitem_axis(self, key, axis)
   1298     if hasattr(key, "ndim") and key.ndim > 1:
   1299         raise ValueError("Cannot index with multidimensional key")
-> 1301     return self._getitem_iterable(key, axis=axis)
   1303 # nested tuple slicing
   1304 if is_nested_tuple(key, labels):

File /dataprocess/jupyter/project_examples/Examples0008/venv/lib/python3.11/site-packages/pandas/core/indexing.py:1239, in _LocIndexer._getitem_iterable(self, key, axis)
   1236 self._validate_key(key, axis)
   1238 # A collection of keys
-> 1239 keyarr, indexer = self._get_listlike_indexer(key, axis)
   1240 return self.obj._reindex_with_indexers(
   1241     {axis: [keyarr, indexer]}, copy=True, allow_dups=True
   1242 )

File /dataprocess/jupyter/project_examples/Examples0008/venv/lib/python3.11/site-packages/pandas/core/indexing.py:1432, in _LocIndexer._get_listlike_indexer(self, key, axis)
   1429 ax = self.obj._get_axis(axis)
   1430 axis_name = self.obj._get_axis_name(axis)
-> 1432 keyarr, indexer = ax._get_indexer_strict(key, axis_name)
   1434 return keyarr, indexer

File /dataprocess/jupyter/project_examples/Examples0008/venv/lib/python3.11/site-packages/pandas/core/indexes/base.py:6070, in Index._get_indexer_strict(self, key, axis_name)
   6067 else:
   6068     keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr)
-> 6070 self._raise_if_missing(keyarr, indexer, axis_name)
   6072 keyarr = self.take(indexer)
   6073 if isinstance(key, Index):
   6074     # GH 42790 - Preserve name from an Index

File /dataprocess/jupyter/project_examples/Examples0008/venv/lib/python3.11/site-packages/pandas/core/indexes/base.py:6133, in Index._raise_if_missing(self, key, indexer, axis_name)
   6130     raise KeyError(f"None of [{key}] are in the [{axis_name}]")
   6132 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique())
-> 6133 raise KeyError(f"{not_found} not in index")

KeyError: "['ENSMUSG00000051951', 'ENSMUSG00000089699', 'ENSMUSG00000102331', 'ENSMUSG00000102343', 'ENSMUSG00000025900', 'ENSMUSG00000104238', 'ENSMUSG00000104328', 'ENSMUSG00000002459', 'ENSMUSG00000085623', 'ENSMUSG00000025905', 'ENSMUSG00000033774', 'ENSMUSG00000090031', 'ENSMUSG00000087247', 'ENSMUSG00000033740'
nsdelablancac commented 9 months ago

Hi!

I had the same error. Did you solve it?

Thanks in advance! Best