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

reloading data - RuntimeError: self.samples is missing, please run self.export_posterior() first #195

Open livyring opened 2 years ago

livyring commented 2 years ago

I try to import my model and h5ad that was already trained, it is able to import correctly but cannot be used for downstream analysis. For example, even when I attempt to plot QC, i return an error:

RuntimeError                              Traceback (most recent call last)
[<ipython-input-11-bee3b2ef217e>](https://localhost:8080/#) in <module>
----> 1 mod.plot_QC()

[/usr/local/lib/python3.7/dist-packages/cell2location/models/base/_pyro_mixin.py](https://localhost:8080/#) in plot_QC(self, summary_name, use_n_obs)
    516 
    517         if getattr(self, "samples", False) is False:
--> 518             raise RuntimeError("self.samples is missing, please run self.export_posterior() first")
    519         if use_n_obs is not None:
    520             ind_x = np.random.choice(

RuntimeError: self.samples is missing, please run self.export_posterior() first

What could be happening?

vitkl commented 2 years ago

For most of the downstream analysis, you don't need to load the scvi-tools model - only the anndata object. However, to run mod.plot_QC() you need to do exactly what the error asks: please run self.export_posterior() first meaning to run mod.export_posterior() with the same arguments as before.

livyring commented 2 years ago

Ok thank you. My other issue I am running into is the ability to do NCEM. When I run:

''' expected_dict = mod.module.model.compute_expected_per_cell_type( mod.samples["post_sample_q05"], mod.adata_manager )

for i, n in enumerate(mod.factornames): adata_vis.layers[n] = expected_dict['mu'][i]

adata_file = f"{run_name}/sp.h5ad" adata_vis.write(adata_file) adata_file

I get the error: RegressionBackgroundDetectionTech model with the following params: n_factors: 23 n_batch: 1 Training status: Trained


AttributeError Traceback (most recent call last) in 1 # Compute expected expression per cell type ----> 2 expected_dict = mod.module.model.compute_expected_per_cell_type( 3 mod.samples["post_sample_q05"], mod.adata_manager 4 ) 5

1 frames /usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in getattr(self, name) 1206 return modules[name] 1207 raise AttributeError("'{}' object has no attribute '{}'".format( -> 1208 type(self).name, name)) 1209 1210 def setattr(self, name: str, value: Union[Tensor, 'Module']) -> None:

AttributeError: 'RegressionBackgroundDetectionTechPyroModel' object has no attribute 'compute_expected_per_cell_type'

I was also getting a similar error last night saying cell2location has no attribute "samples"

vitkl commented 2 years ago

To generate cell-type specific RNA count predictions as below:

expected_dict = mod.module.model.compute_expected_per_cell_type(
mod.samples["post_sample_q05"], mod.adata_manager
)

you indeed need to runmod.export_posterior() after loading the model.

AttributeError: 'RegressionBackgroundDetectionTechPyroModel' object has no attribute 'compute_expected_per_cell_type'

Please make sure that you are loading the cell2location model using anndata object for Visium data - not the regression model with Visium anndata.

livyring commented 2 years ago

Thank you this worked well!