scvi.model.base.PyroSampleMixin#

class scvi.model.base.PyroSampleMixin[source]#

Mixin class for generating samples from posterior distribution.

Works using both minibatches and full data.

Methods table#

sample_posterior([adata, input_dl, indices, ...])

Summarise posterior distribution.

Methods#

PyroSampleMixin.sample_posterior(adata=None, input_dl=None, indices=None, num_samples=1000, return_sites=None, accelerator='auto', device='auto', batch_size=None, return_observed=False, return_samples=False, summary_frequency=None, summary_fun=None, **sample_kwargs)[source]#

Summarise posterior distribution.

Generate samples from posterior distribution for each parameter and compute mean, 5th/95th quantiles, standard deviation.

Parameters:
adata anndata.AnnData | None (default: None)

AnnData object to use for sampling. If None, uses the registered AnnData object.

input_dl AnnDataLoader | None (default: None)

DataLoader object to use for sampling. If None, creates one based on adata.

indices ndarray | None (default: None)

Indices of cells to use for sampling. If None, uses all cells.

num_samples int (default: 1000)

Number of posterior samples to generate.

return_sites list | None (default: None)

List of variables for which to generate posterior samples, defaults to all variables.

accelerator str (default: 'auto')

Supports passing different accelerator types ("cpu", "gpu", "tpu", "ipu", "hpu", "mps, "auto") as well as custom accelerator instances.

device int | str (default: 'auto')

The device to use. Can be set to a non-negative index (int or str) or "auto" for automatic selection based on the chosen accelerator. If set to "auto" and accelerator is not determined to be "cpu", then device will be set to the first available device.

batch_size int | None (default: None)

Minibatch size for data loading into model. Defaults to scvi.settings.batch_size.

return_observed bool (default: False)

Return observed sites/variables? Observed count matrix can be very large so not returned by default.

return_samples bool (default: False)

Return all generated posterior samples in addition to sample mean, 5th/95th quantile and SD?

summary_frequency int | None (default: None)

Compute summary_fn after summary_frequency batches. Reduces memory footprint.

summary_fun dict[str, Callable] | None (default: None)

a dict in the form {“means”: np.mean, “std”: np.std} which specifies posterior distribution summaries to compute and which names to use. See below for default returns.

sample_kwargs

Keyword arguments for _get_posterior_samples.

Returns:

post_sample_meansdict[str, numpy.ndarray]

Mean of the posterior distribution for each variable, a dictionary of numpy arrays for each variable;

post_sample_q05dict[str, numpy.ndarray]

5th quantile of the posterior distribution for each variable;

post_sample_q95dict[str, numpy.ndarray]

95th quantile of the posterior distribution for each variable;

post_sample_stdsdict[str, numpy.ndarray]

Standard deviation of the posterior distribution for each variable;

posterior_samplesdict[str, numpy.ndarray], optional

Posterior distribution samples for each variable as numpy arrays of shape (n_samples, ...).

Notes

Note for developers: requires overwritten list_obs_plate_vars property, which lists observation/minibatch plate name and variables. See list_obs_plate_vars for details of the variables it should contain. This dictionary can be returned by model class property self.module.model.list_obs_plate_vars to keep all model-specific variables in one place.