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
321 stars 58 forks source link

KeyError: 'post_sample_means' #239

Open BenjaminDEMAILLE opened 1 year ago

BenjaminDEMAILLE commented 1 year ago

Hi ! I got this error while running mod.plot_QC()

The error is :

Output exceeds the [size limit](command:workbench.action.openSettings?[). Open the full output data [in a text editor](command:workbench.action.openLargeOutput?89aeb18b-f0f2-42da-919f-f9b1921d31d3)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[19], line 1
----> 1 mod.plot_QC()

File /opt/homebrew/lib/python3.10/site-packages/cell2location/models/reference/_reference_model.py:294, in RegressionModel.plot_QC(self, summary_name, use_n_obs, scale_average_detection)
    265 def plot_QC(
    266     self,
    267     summary_name: str = "means",
    268     use_n_obs: int = 1000,
    269     scale_average_detection: bool = True,
    270 ):
    271     """
    272     Show quality control plots:
    273     1. Reconstruction accuracy to assess if there are any issues with model training.
   (...)
    291 
    292     """
--> 294     super().plot_QC(summary_name=summary_name, use_n_obs=use_n_obs)
    295     plt.show()
    297     inf_aver = self.samples[f"post_sample_{summary_name}"]["per_cluster_mu_fg"].T

File /opt/homebrew/lib/python3.10/site-packages/cell2location/models/base/_pyro_mixin.py:575, in PltExportMixin.plot_QC(self, summary_name, use_n_obs)
    571 else:
    572     ind_x = None
...
    576 )
    577 x_data = self.adata_manager.get_from_registry(REGISTRY_KEYS.X_KEY)[ind_x, :]
    578 if issparse(x_data):

KeyError: 'post_sample_means'

How can I correct it ?

liangyu02 commented 1 year ago

The same mistake,why?

parkjooyoung99 commented 1 year ago

The same mistake here too

vitkl commented 1 year ago

This function only works after you exported results.

You need to check which slots exist here adata_vis.uns['mod'].keys().

Its possible that you don't have the slot for means only for quantiles so you need to change mod.plot_QC() argument summary_name: str = "means".

In general, please provide more details as suggested in the template including which steps have already been performed, whether you are loading results after training or continuing a single notebook session, etc.

roskcl commented 1 year ago

I am also experiencing the same error even tough adata_ref.uns['mod'].keys() returns 'post_sample_means'

How can I solve it?

Thank you!

adata_ref = mod.export_posterior( adata_ref, use_quantiles=True,

choose quantiles

add_to_varm=['means', "q05","q50", "q95", "q0001"],
sample_kwargs={'batch_size': 2500}

) mod.plot_QC()

KeyError Traceback (most recent call last) Cell In[52], line 1 ----> 1 mod.plot_QC()

File C:\Users\Public\Anaconda3\envs\cell2loc_env\lib\site-packages\cell2location\models\reference_reference_model.py:294, in RegressionModel.plot_QC(self, summary_name, use_n_obs, scale_average_detection) 265 def plot_QC( 266 self, 267 summary_name: str = "means", 268 use_n_obs: int = 1000, 269 scale_average_detection: bool = True, 270 ): 271 """ 272 Show quality control plots: 273 1. Reconstruction accuracy to assess if there are any issues with model training. (...) 291 292 """ --> 294 super().plot_QC(summary_name=summary_name, use_n_obs=use_n_obs) 295 plt.show() 297 inf_aver = self.samples[f"postsample{summary_name}"]["per_cluster_mu_fg"].T

File C:\Users\Public\Anaconda3\envs\cell2loc_env\lib\site-packages\cell2location\models\base_pyro_mixin.py:575, in PltExportMixin.plot_QC(self, summary_name, use_n_obs) 571 else: 572 ind_x = None 574 self.expected_nb_param = self.module.model.compute_expected( --> 575 self.samples[f"postsample{summary_name}"], self.adata_manager, ind_x=ind_x 576 ) 577 x_data = self.adata_manager.get_from_registry(REGISTRY_KEYS.X_KEY)[ind_x, :] 578 if issparse(x_data):

KeyError: 'post_sample_means'

mod RegressionBackgroundDetectionTech model with the following params: n_factors: 25 n_batch: 1 Training status: Trained Model's adata is minified?: False

adata_ref.uns['mod'].keys() dict_keys(['model_name', 'date', 'factor_filter', 'factor_names', 'var_names', 'obs_names', 'post_sample_means', 'post_sample_stds', 'post_sample_q05', 'post_sample_q50', 'post_sample_q95', 'post_sample_q0001'])

vitkl commented 11 months ago

Using the use_quantiles=True option mod.export_posterior only computes quantiles ["q05", "q50", "q95", "q0001"]. You need to tell mod.plot_QC(summary_name="q50") to use the median (summary_name="q50") or other quantiles.