scviva.module.JVAE#

class scviva.module.JVAE(dim_input_list, total_genes, indices_mappings, gene_likelihoods, model_library_bools, library_log_means, library_log_vars, n_latent=10, n_layers_encoder_individual=1, n_layers_encoder_shared=1, dim_hidden_encoder=64, n_layers_decoder_individual=0, n_layers_decoder_shared=0, dim_hidden_decoder_individual=64, dim_hidden_decoder_shared=64, dropout_rate_encoder=0.2, dropout_rate_decoder=0.2, n_batch=0, n_labels=0, dispersion='gene-batch', log_variational=True)[source]#

Joint variational auto-encoder for imputing missing genes in spatial data.

Implementation of gimVI [Lopez et al., 2019].

Parameters:
dim_input_list list[int]

List of number of input genes for each dataset.

total_genes int

Total number of different genes.

indices_mappings list[ndarray | slice]

List of mappings from model inputs to model output locations.

gene_likelihoods list[str]

List of distributions: ‘zinb’, ‘nb’, or ‘poisson’.

model_library_bools list[bool]

Whether to model library size with a latent variable per dataset.

library_log_means list[ndarray | None]

List of 1 x n_batch arrays of log library size means.

library_log_vars list[ndarray | None]

List of 1 x n_batch arrays of log library size variances.

n_latent int

Dimension of latent space.

n_layers_encoder_individual int

Number of individual encoder layers.

n_layers_encoder_shared int

Number of shared encoder layers.

dim_hidden_encoder int

Hidden layer dimension for encoder.

n_layers_decoder_individual int

Number of individual decoder layers.

n_layers_decoder_shared int

Number of shared decoder layers.

dim_hidden_decoder_individual int

Hidden layer dimension for individual decoder.

dim_hidden_decoder_shared int

Hidden layer dimension for shared decoder.

dropout_rate_encoder float

Dropout rate for encoder.

dropout_rate_decoder float

Dropout rate for decoder.

n_batch int

Total number of batches.

n_labels int

Total number of labels.

dispersion str

Dispersion parameterization: ‘gene’, ‘gene-batch’, ‘gene-label’, or ‘gene-cell’.

log_variational bool

Log(data+1) prior to encoding for numerical stability.

Attributes table

Methods table

forward(tensors[, ...])

Forward pass through the network.

generative(z, library[, batch_index, y, ...])

Run the generative model.

get_sample_rate(x, batch_index, *_, **__)

Get the sample rate for the model.

inference(x[, mode, n_samples, batch_index])

Run the inference model.

loss(tensors, inference_outputs, ...[, ...])

Return the reconstruction loss and the Kullback divergences.

on_load(model, **kwargs)

Callback function run in load().

reconstruction_loss(x, px_rate, px_r, ...)

Compute the reconstruction loss.

sample(*args, **kwargs)

Generate samples from the learned model.

sample_from_posterior_l(x[, mode, deterministic])

Sample the tensor of library sizes from the posterior.

sample_from_posterior_z(x[, mode, deterministic])

Sample tensor of latent values from the posterior.

sample_rate(x, mode, batch_index[, y, ...])

Return the tensor of scaled frequencies of expression.

sample_scale(x, mode, batch_index[, y, ...])

Return the tensor of predicted frequencies of expression.

Attributes

JVAE.device#
JVAE.training: bool#

Methods

JVAE.forward(tensors, get_inference_input_kwargs=None, get_generative_input_kwargs=None, inference_kwargs=None, generative_kwargs=None, loss_kwargs=None, compute_loss=True)[source]#

Forward pass through the network.

Parameters:
tensors

tensors to pass through

get_inference_input_kwargs dict | None

Keyword args for _get_inference_input()

get_generative_input_kwargs dict | None

Keyword args for _get_generative_input()

inference_kwargs dict | None

Keyword args for inference()

generative_kwargs dict | None

Keyword args for generative()

loss_kwargs dict | None

Keyword args for loss()

compute_loss

Whether to compute loss on forward pass. This adds another return value.

Return type:

tuple[Tensor, Tensor] | tuple[Tensor, Tensor, LossOutput]

JVAE.generative(z, library, batch_index=None, y=None, mode=0, transform_batch=None)[source]#

Run the generative model.

Parameters:
z Tensor

Latent values with shape (n_cells, n_latent).

library Tensor

Log library size with shape (n_cells, 1).

batch_index Tensor | None

Batch index of each cell, with shape (n_cells, 1). Used by the decoder and, when dispersion="gene-batch", to select the dispersion.

y Tensor | None

Label of each cell, with shape (n_cells, 1). Used when dispersion="gene-label" to select the dispersion.

mode int | None

Index of the dataset whose decoder head is used. px_scale is normalized over the genes of this dataset.

transform_batch Tensor | None

If not None, the "batch_index" entry of the returned dictionary is replaced by this value. It does not affect the decoded quantities.

Return type:

dict

Returns:

Dictionary with keys "px_scale", "px", "px_r", "px_rate", "px_dropout" and "batch_index".

JVAE.get_sample_rate(x, batch_index, *_, **__)[source]#

Get the sample rate for the model.

Encodes and decodes x with dataset 0 (see sample_rate()).

Parameters:
x

Tensor of counts for dataset 0 with shape (n_cells, n_genes_0).

batch_index

Batch index of each cell, with shape (n_cells, 1).

*_

Ignored; accepted for interface compatibility.

**__

Ignored; accepted for interface compatibility.

JVAE.inference(x, mode=0, n_samples=1, batch_index=None)[source]#

Run the inference model.

Parameters:
x Tensor

Tensor of counts with shape (n_cells, n_genes_mode), where n_genes_mode is the number of genes of dataset mode.

mode int | None

Index of the dataset (encoder head) x comes from.

n_samples int | None

Number of samples of z to draw from the posterior. If greater than 1, z gains a leading sample dimension.

batch_index Tensor | None

Batch index of each cell. Unused; accepted for interface compatibility.

Return type:

dict

Returns:

Dictionary with keys "qz" (posterior over z), "z", "ql" (posterior over the log library size, or None if the library is not modeled) and "library" (sampled or observed log library size).

JVAE.loss(tensors, inference_outputs, generative_outputs, mode=None, kl_weight=1.0)[source]#

Return the reconstruction loss and the Kullback divergences.

Parameters:
tensors

Dictionary of minibatch tensors; uses the registered counts and batch index.

inference_outputs

Output of inference().

generative_outputs

Output of generative().

mode int | None

Index of the dataset the minibatch comes from. May be None only when the model has a single dataset, in which case 0 is used.

kl_weight float

Weight of the KL divergence terms (z and, if modeled, library size).

Return type:

LossOutput

Returns:

LossOutput with the total loss, per-cell reconstruction loss and per-cell local KL divergence.

JVAE.on_load(model, **kwargs)[source]#

Callback function run in load().

JVAE.reconstruction_loss(x, px_rate, px_r, px_dropout, mode)[source]#

Compute the reconstruction loss.

Parameters:
x Tensor

Tensor of counts with shape (n_cells, n_genes_mode), where n_genes_mode is the number of genes of dataset mode.

px_rate Tensor

Mean of the count distribution, restricted to the genes of dataset mode.

px_r Tensor

Inverse dispersion of the count distribution, restricted to the genes of dataset mode.

px_dropout Tensor

Logits of the zero-inflation probability, restricted to the genes of dataset mode. Only used when the gene likelihood is "zinb".

mode int

Index of the dataset; selects the gene likelihood from gene_likelihoods.

Return type:

Tensor

Returns:

Negative log-likelihood per cell with shape (n_cells,).

abstractmethod JVAE.sample(*args, **kwargs)[source]#

Generate samples from the learned model.

JVAE.sample_from_posterior_l(x, mode=None, deterministic=False)[source]#

Sample the tensor of library sizes from the posterior.

Parameters:
x Tensor

Tensor of counts with shape (n_cells, n_genes_mode), where n_genes_mode is the number of genes of dataset mode.

mode int

Index of the dataset (encoder head) x comes from.

deterministic bool

If True and the library size is modeled for this dataset, return the posterior mean of the log library size instead of a sample. If the library size is not modeled, the observed log library size is returned regardless.

Return type:

Tensor

Returns:

Tensor of log library sizes with shape (n_cells, 1).

JVAE.sample_from_posterior_z(x, mode=None, deterministic=False)[source]#

Sample tensor of latent values from the posterior.

Parameters:
x Tensor

Tensor of counts with shape (n_cells, n_genes_mode), where n_genes_mode is the number of genes of dataset mode.

mode int

Index of the dataset (encoder head) x comes from. May be None only when the model has a single dataset, in which case 0 is used.

deterministic bool

If True, return the posterior mean instead of a sample.

Return type:

Tensor

Returns:

Tensor of latent values with shape (n_cells, n_latent).

JVAE.sample_rate(x, mode, batch_index, y=None, deterministic=False, decode_mode=None)[source]#

Return the tensor of scaled frequencies of expression.

Parameters:
x Tensor

Tensor of counts with shape (n_cells, n_genes_mode), where n_genes_mode is the number of genes of dataset mode.

mode int

Index of the dataset (encoder head) x comes from.

batch_index Tensor

Batch index of each cell, with shape (n_cells, 1).

y Tensor | None

Label of each cell, with shape (n_cells, 1). Required when dispersion="gene-label".

deterministic bool

If True, use the posterior means of z and of the library size (when the library is modeled) instead of samples.

decode_mode int

Index of the dataset whose decoder head is used. If None, defaults to mode.

Return type:

Tensor

Returns:

Tensor of expected counts (px_scale times library size) with shape (n_cells, total_genes).

JVAE.sample_scale(x, mode, batch_index, y=None, deterministic=False, decode_mode=None)[source]#

Return the tensor of predicted frequencies of expression.

Parameters:
x Tensor

Tensor of counts with shape (n_cells, n_genes_mode), where n_genes_mode is the number of genes of dataset mode.

mode int

Index of the dataset (encoder head) x comes from.

batch_index Tensor

Batch index of each cell, with shape (n_cells, 1).

y Tensor | None

Label of each cell, with shape (n_cells, 1). Required when dispersion="gene-label".

deterministic bool

If True, use the posterior means of z and of the library size (when the library is modeled) instead of samples.

decode_mode int | None

Index of the dataset whose decoder head is used. If None, defaults to mode.

Return type:

Tensor

Returns:

Tensor of normalized expression frequencies with shape (n_cells, total_genes).