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

Split data then run cell2location #316

Open hahia opened 1 year ago

hahia commented 1 year ago

Hello, thank you for this valuable tool. I have a question regarding memory usage in Cell2Location. During the deconvolution step, I'm encountering an 'out of memory' issue with the following code

adata_vis = mod.export_posterior(
    adata_vis, sample_kwargs={'num_samples': 1000, 'batch_size': mod.adata.n_obs, 'use_gpu': True}
)

To address this, I'm considering splitting the data and running Cell2Location for each sub-data. Will the results be the same in this case?

vitkl commented 1 year ago

Hi @hahia

If you can train the model on full data, I would strongly recommend continuing to do so, but instead replacing the mod.export_posterior with a more memory-efficient alternative by computing the quantiles directly rather than using 1000 samples (default):

adata_vis = mod.export_posterior(
    adata_vis, use_quantiles=True,
    # choose quantiles
    add_to_obsm=["q05","q50", "q95", "q0001"],
    sample_kwargs={'batch_size': 2500, 'use_gpu': True}
)

note that you would also have to specify mod.plot_QC(summary_name= 'q50') instead of mod.plot_QC(summary_name= 'means') (default).

elise-smith commented 7 months ago

Hi @vitkl,

I'm having the same error as above, running out of memory when running mod.export_posterior.

I'm trying to run mod.export_posterior by computing quantiles directly, as explained in your reply above, however, I am getting the following error: TypeError: export_posterior() got an unexpected keyword argument 'use_quantiles'

Is there any way to fix this? Thank you!

Version of cell2location: 0.1

vitkl commented 6 months ago

For the quantiles option you need to install the latest cell2location version from GitHub

elise-smith commented 6 months ago

Great, thank you for your help, I'll try installing the latest version