scvi.external.DIAGVI#

class scvi.external.DIAGVI(adatas, guidance_graph=None, mapping_df=None, n_latent=50, n_hidden=256, n_layers=2, dropout_rate=0.1, **model_kwargs)[source]#

Diagonal Multi-Modal Integration Variational Inference (DIAGVI) model.

Integrates multi-modal single-cell data using a guidance graph and supports semi-supervised learning and GMM priors.

The model architecture is inspired by GLUE (Cao & Gao, 2022). This scvi-tools implementation is based on GimVI. Handling of continuous data in decoder is inspired by CytoVI.

Parameters:
  • adatas (dict[str, AnnData] | MuData) – Dictionary mapping input names to AnnData objects.

  • guidance_graph (Data | None (default: None)) – Precomputed guidance graph. If None, it will be constructed from the data by using overlapping feature names.

  • mapping_df (DataFrame | None (default: None)) – DataFrame specifying feature correspondences between modalities (used to compute the guidance graph).

  • n_latent (int (default: 50)) – Dimensionality of the latent space.

  • n_hidden (int (default: 256)) – Number of nodes per hidden layer.

  • n_layers (int (default: 2)) – Number of hidden layers used for encoder and decoder NNs.

  • dropout_rate (float (default: 0.1)) – Dropout rate for neural networks.

  • **model_kwargs – Additional keyword arguments for DIAGVAE.

Examples

>>> adatas = {"rna_data": adata_rna, "protein_data": adata_protein}
>>> model = DIAGVI(adatas)
>>> model.train()

Attributes table#

adata

Data attached to model instance.

adata_manager

Manager instance associated with self.adata.

device

The current device that the module's params are on.

get_normalized_function_name

What the get normalized functions name is

history

Returns computed metrics during training.

is_trained

Whether the model has been trained.

registry

Data attached to model instance.

run_id

Returns the run id of the model.

run_name

Returns the run name of the model.

summary_string

Summary string of the model.

test_indices

Observations that are in test set.

train_indices

Observations that are in train set.

validation_indices

Observations that are in validation set.

Methods table#

construct_custom_guidance_graph(input_dict, ...)

Constructs a custom guidance graph defined by the mapping_df.

convert_legacy_save(dir_path, output_dir_path)

Converts a legacy saved model (<v0.15.0) to the updated save format.

data_registry(registry_key)

Returns the object in AnnData associated with the key in the data registry.

deregister_manager([adata])

Deregisters the AnnDataManager instance associated with adata.

differential_abundance(*args, **kwargs)

Not implemented for this model class.

get_aggregated_posterior([adata, indices, ...])

Compute the aggregated posterior over the u latent representations.

get_anndata_manager(adata[, required])

Retrieves the AnnDataManager for a given AnnData object.

get_elbo([adata, indices, batch_size, ...])

Compute the evidence lower bound (ELBO) on the data.

get_from_registry(adata, registry_key)

Returns the object in AnnData associated with the key in the data registry.

get_imputed_values(query_name[, ...])

Return imputed values for a given query modality.

get_latent_representation([adatas, indices, ...])

Return the latent space embedding for each dataset.

get_marginal_ll([adata, indices, ...])

Compute the marginal log-likehood of the data.

get_normalized_expression(*args, **kwargs)

Not implemented for this model class.

get_reconstruction_error([adata, indices, ...])

Compute the reconstruction error on the data.

get_setup_arg(setup_arg)

Returns the string provided to setup of a specific setup_arg.

get_state_registry(registry_key)

Returns the state registry for the AnnDataField registered with this instance.

get_var_names([legacy_mudata_format])

Variable names of input data.

load(dir_path[, adatas, accelerator, ...])

Load a saved DIAGVI model from disk.

load_registry(dir_path[, prefix])

Return the full registry saved with the model.

posterior_predictive_sample([adatas, ...])

Generate posterior predictive samples for each modality.

predict_celltype(labeled_modality[, ...])

Predict cell type labels using a trained classifier.

register_manager(adata_manager)

Registers an AnnDataManager instance with this model class.

save(dir_path[, prefix, overwrite, ...])

Save the DIAGVI model and optionally the AnnData objects.

setup_anndata(adata[, layer, batch_key, ...])

Sets up the AnnData object for this model.

setup_mudata(mdata, modalities[, batch_key, ...])

Register a MuData object for use with DIAGVI.

to_device(device)

Move the model to the device.

train([max_epochs, lr, batch_size, ...])

Train the DIAGVI model.

transfer_fields(adata, **kwargs)

Transfer fields from a model to an AnnData object.

update_setup_method_args(setup_method_args)

Update setup method args.

view_anndata_setup([adata, ...])

Print summary of the setup for the initial AnnData or a given AnnData object.

view_registry([hide_state_registries])

Prints summary of the registry.

view_setup_args(dir_path[, prefix])

Print args used to setup a saved model.

view_setup_method_args()

Prints setup kwargs used to produce a given registry.

Attributes#

DIAGVI.adata[source]#

Data attached to model instance.

DIAGVI.adata_manager[source]#

Manager instance associated with self.adata.

DIAGVI.device[source]#

The current device that the module’s params are on.

DIAGVI.get_normalized_function_name[source]#

What the get normalized functions name is

DIAGVI.history[source]#

Returns computed metrics during training.

DIAGVI.is_trained[source]#

Whether the model has been trained.

DIAGVI.registry[source]#

Data attached to model instance.

DIAGVI.run_id[source]#

Returns the run id of the model. Used in MLFlow

DIAGVI.run_name[source]#

Returns the run name of the model. Used in MLFlow

DIAGVI.summary_string[source]#

Summary string of the model.

DIAGVI.test_indices[source]#

Observations that are in test set.

DIAGVI.train_indices[source]#

Observations that are in train set.

DIAGVI.validation_indices[source]#

Observations that are in validation set.

Methods#

static DIAGVI.construct_custom_guidance_graph(input_dict, mapping_df, weight=1.0, sign=1.0)[source]#

Constructs a custom guidance graph defined by the mapping_df.

Parameters:
  • input_dict (dict[str, AnnData]) – Dictionary mapping modality names (e.g., “scRNAseq”, “seqFISH”) to their respective AnnData objects. The keys in this dictionary must match the column names in mapping_df.

  • mapping_df (DataFrame) – DataFrame specifying feature correspondences between modalities. Each column should correspond to a modality name in input_dict, and each row defines a feature pair.

  • weight (float (default: 1.0)) – Edge weight assigned to cross-modality edges.

  • sign (float (default: 1.0)) – Edge sign assigned to cross-modality edges.

Return type:

Data

Returns:

A PyTorch Geometric Data object representing the guidance graph, including node features, edge indices, edge weights, edge signs, and modality-specific feature indices.

Notes

  • Features are renamed to include the modality name as a suffix.

  • Self-loops are added for all features with weight=1.0 and sign=1.0.

  • Missing features in the mapping are silently skipped.

classmethod DIAGVI.convert_legacy_save(dir_path, output_dir_path, overwrite=False, prefix=None, **save_kwargs)[source]#

Converts a legacy saved model (<v0.15.0) to the updated save format.

Parameters:
  • dir_path (str) – Path to the directory where the legacy model is saved.

  • output_dir_path (str) – Path to save converted save files.

  • overwrite (bool (default: False)) – Overwrite existing data or not. If False and directory already exists at output_dir_path, an error will be raised.

  • prefix (str | None (default: None)) – Prefix of saved file names.

  • **save_kwargs – Keyword arguments passed into save().

Return type:

None

DIAGVI.data_registry(registry_key)[source]#

Returns the object in AnnData associated with the key in the data registry.

Parameters:

registry_key (str) – key of an object to get from self.data_registry

Return type:

ndarray | DataFrame

Returns:

The requested data.

DIAGVI.deregister_manager(adata=None)[source]#

Deregisters the AnnDataManager instance associated with adata.

If adata is None, deregisters all AnnDataManager instances in both the class and instance-specific manager stores, except for the one associated with this model instance.

DIAGVI.differential_abundance(*args, **kwargs)[source]#

Not implemented for this model class.

Available in models that inherit from VAEMixin.

Raises:

NotImplementedError

DIAGVI.get_aggregated_posterior(adata=None, indices=None, batch_size=None, dof=3.0)[source]#

Compute the aggregated posterior over the u latent representations.

Parameters:
  • adata (default: None) – AnnData object to use. Defaults to the AnnData object used to initialize the model.

  • indices (default: None) – Indices of cells to use.

  • batch_size (default: None) – Batch size to use for computing the latent representation.

  • dof (default: 3.0) – Degrees of freedom for the Student’s t-distribution components. If None, components are Normal.

Returns:

A mixture distribution of the aggregated posterior.

DIAGVI.get_anndata_manager(adata, required=False)[source]#

Retrieves the AnnDataManager for a given AnnData object.

Requires self.id has been set. Checks for an AnnDataManager specific to this model instance.

Parameters:
  • adata (AnnData | MuData) – AnnData object to find a manager instance for.

  • required (bool (default: False)) – If True, errors on missing manager. Otherwise, returns None when manager is missing.

Return type:

AnnDataManager | None

DIAGVI.get_elbo(adata=None, indices=None, batch_size=None, dataloader=None, return_mean=True, data_loader_kwargs=None, **kwargs)[source]#

Compute the evidence lower bound (ELBO) on the data.

The ELBO is the reconstruction error plus the Kullback-Leibler (KL) divergences between the variational distributions and the priors. It is different from the marginal log-likelihood; specifically, it is a lower bound on the marginal log-likelihood plus a term that is constant with respect to the variational distribution. It still gives good insights on the modeling of the data and is fast to compute.

Parameters:
  • adata (AnnData | None (default: None)) – AnnData object with var_names in the same order as the ones used to train the model. If None and dataloader is also None, it defaults to the object used to initialize the model.

  • indices (Sequence[int] | None (default: None)) – Indices of observations in adata to use. If None, defaults to all observations. Ignored if dataloader is not None.

  • batch_size (int | None (default: None)) – Minibatch size for the forward pass. If None, defaults to scvi.settings.batch_size. Ignored if dataloader is not None.

  • dataloader (Iterator[dict[str, Tensor | None]] | None (default: None)) – An iterator over minibatches of data on which to compute the metric. The minibatches should be formatted as a dictionary of Tensor with keys as expected by the model. If None, a dataloader is created from adata.

  • return_mean (bool (default: True)) – Whether to return the mean of the ELBO or the ELBO for each observation.

  • data_loader_kwargs (dict | None (default: None)) – Keyword args for data loader, in dict form.

  • **kwargs – Additional keyword arguments to pass into the forward method of the module.

Return type:

float

Returns:

Evidence lower bound (ELBO) of the data.

Notes

This is not the negative ELBO, so higher is better.

DIAGVI.get_from_registry(adata, registry_key)[source]#

Returns the object in AnnData associated with the key in the data registry.

AnnData object should be registered with the model prior to calling this function via the self._validate_anndata method.

Parameters:
  • registry_key (str) – key of object to get from the data registry.

  • adata (AnnData | MuData) – AnnData to pull data from.

Return type:

ndarray

Returns:

The requested data as a NumPy array.

DIAGVI.get_imputed_values(query_name, query_adata=None, indices=None, deterministic=True, batch_size=2048, reference_batch=None, reference_libsize=None)[source]#

Return imputed values for a given query modality.

Parameters:
  • query_name (str) – Name of the query modality.

  • query_adata (AnnData | None (default: None)) – AnnData object for the query modality. If None, uses the registered AnnData.

  • indices (Sequence[int] | None (default: None)) – Indices of cells to use. If None, all cells are used.

  • deterministic (bool (default: True)) – Whether to use deterministic inference (posterior mean).

  • batch_size (int (default: 2048)) – Minibatch size for data loading.

  • reference_batch (int | str | ndarray | None (default: None)) – Reference batch index or array for imputation. Can be a scalar, a length-1 array, or an array with length equal to the number of queried cells.

  • reference_libsize (float | ndarray | None (default: None)) – Reference library size(s) for imputation. Can be a scalar, a length-1 array, or an array with length equal to the number of queried cells.

Return type:

ndarray

Returns:

Imputed values.

DIAGVI.get_latent_representation(adatas=None, indices=None, batch_size=1024)[source]#

Return the latent space embedding for each dataset.

Parameters:
  • adatas (dict[str, AnnData] | list[AnnData] | None (default: None)) – Modalities to compute embeddings for. If None, uses all modalities.

  • indices (dict[str, Sequence[int]] | None (default: None)) – Dictionary mapping modality names to indices of cells to use. If None, all cells are used for each modality.

  • batch_size (int (default: 1024)) – Minibatch size for data loading.

Return type:

dict[str, ndarray]

Returns:

Dictionary mapping modality names to latent embeddings.

DIAGVI.get_marginal_ll(adata=None, indices=None, n_mc_samples=1000, batch_size=None, return_mean=True, dataloader=None, data_loader_kwargs=None, **kwargs)[source]#

Compute the marginal log-likehood of the data.

The computation here is a biased estimator of the marginal log-likelihood of the data.

Parameters:
  • adata (AnnData | None (default: None)) – AnnData object with var_names in the same order as the ones used to train the model. If None and dataloader is also None, it defaults to the object used to initialize the model.

  • indices (Sequence[int] | None (default: None)) – Indices of observations in adata to use. If None, defaults to all observations. Ignored if dataloader is not None.

  • n_mc_samples (int (default: 1000)) – Number of Monte Carlo samples to use for the estimator. Passed into the module’s marginal_ll method.

  • batch_size (int | None (default: None)) – Minibatch size for the forward pass. If None, defaults to scvi.settings.batch_size. Ignored if dataloader is not None.

  • return_mean (bool (default: True)) – Whether to return the mean of the marginal log-likelihood or the marginal-log likelihood for each observation.

  • dataloader (Iterator[dict[str, Tensor | None]] (default: None)) – An iterator over minibatches of data on which to compute the metric. The minibatches should be formatted as a dictionary of Tensor with keys as expected by the model. If None, a dataloader is created from adata.

  • data_loader_kwargs (dict | None (default: None)) – Keyword args for data loader, in dict form.

  • **kwargs – Additional keyword arguments to pass into the module’s marginal_ll method.

Return type:

float | Tensor

Returns:

If True, returns the mean marginal log-likelihood. Otherwise returns a tensor of shape (n_obs,) with the marginal log-likelihood for each observation.

Notes

This is not the negative log-likelihood, so higher is better.

DIAGVI.get_normalized_expression(*args, **kwargs)[source]#

Not implemented for this model class.

Available in RNA models that inherit from RNASeqMixin.

Raises:

NotImplementedError

DIAGVI.get_reconstruction_error(adata=None, indices=None, batch_size=None, dataloader=None, return_mean=True, data_loader_kwargs=None, **kwargs)[source]#

Compute the reconstruction error on the data.

The reconstruction error is the negative log likelihood of the data given the latent variables. It is different from the marginal log-likelihood, but still gives good insights on the modeling of the data and is fast to compute. This is typically written as \(p(x \mid z)\), the likelihood term given one posterior sample.

Parameters:
  • adata (AnnData | None (default: None)) – AnnData object with var_names in the same order as the ones used to train the model. If None and dataloader is also None, it defaults to the object used to initialize the model.

  • indices (Sequence[int] | None (default: None)) – Indices of observations in adata to use. If None, defaults to all observations. Ignored if dataloader is not None

  • batch_size (int | None (default: None)) – Minibatch size for the forward pass. If None, defaults to scvi.settings.batch_size. Ignored if dataloader is not None

  • dataloader (Iterator[dict[str, Tensor | None]] | None (default: None)) – An iterator over minibatches of data on which to compute the metric. The minibatches should be formatted as a dictionary of Tensor with keys as expected by the model. If None, a dataloader is created from adata.

  • return_mean (bool (default: True)) – Whether to return the mean reconstruction loss or the reconstruction loss for each observation.

  • data_loader_kwargs (dict | None (default: None)) – Keyword args for data loader, in dict form.

  • **kwargs – Additional keyword arguments to pass into the forward method of the module.

Return type:

dict[str, float]

Returns:

Reconstruction error for the data.

Notes

This is not the negative reconstruction error, so higher is better.

DIAGVI.get_setup_arg(setup_arg)[source]#

Returns the string provided to setup of a specific setup_arg.

Return type:

attrdict

DIAGVI.get_state_registry(registry_key)[source]#

Returns the state registry for the AnnDataField registered with this instance.

Return type:

attrdict

DIAGVI.get_var_names(legacy_mudata_format=False)[source]#

Variable names of input data.

Return type:

dict

classmethod DIAGVI.load(dir_path, adatas=None, accelerator='auto', device='auto', prefix=None, backup_url=None)[source]#

Load a saved DIAGVI model from disk.

Parameters:
  • dir_path (str) – Directory path where the model was saved.

  • adatas (dict[str, AnnData] | None (default: None)) – Dictionary mapping modality names to AnnData objects. Required when the model was saved with save_anndata=False.

  • 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.

  • prefix (str | None (default: None)) – Prefix used when saving the model.

  • backup_url (str | None (default: None)) – URL to download the model from if not found locally.

Return type:

DIAGVI

Returns:

Loaded DIAGVI model instance.

Raises:

ValueError – If the saved model is from a different class or missing setup inputs.

static DIAGVI.load_registry(dir_path, prefix=None)[source]#

Return the full registry saved with the model.

Parameters:
  • dir_path (str) – Path to saved outputs.

  • prefix (str | None (default: None)) – Prefix of saved file names.

Return type:

dict

Returns:

The full registry saved with the model

DIAGVI.posterior_predictive_sample(adatas=None, indices=None, n_samples=1, batch_size=None)[source]#

Generate posterior predictive samples for each modality.

This method generates samples from the posterior predictive distribution p(x̂|x) for each modality, which can be used for model criticism via PosteriorPredictiveCheck.

Parameters:
  • adatas (dict[str, AnnData] | None (default: None)) – Dictionary mapping modality names to AnnData objects. If None, defaults to the AnnData objects used to initialize the model.

  • indices (dict[str, Sequence[int]] | None (default: None)) – Dictionary mapping modality names to indices of cells to use. If None, all cells are used for each modality.

  • n_samples (int (default: 1)) – Number of posterior predictive samples to generate per cell.

  • batch_size (int | None (default: None)) – Minibatch size for data loading. Defaults to scvi.settings.batch_size.

Return type:

dict[str, ndarray]

Returns:

Dictionary mapping modality names to arrays of shape (n_cells, n_features, n_samples) if n_samples > 1, else (n_cells, n_features).

DIAGVI.predict_celltype(labeled_modality, target_modality=None, target_adata=None, indices=None, batch_size=2048, deterministic=True)[source]#

Predict cell type labels using a trained classifier.

Parameters:
  • labeled_modality (str) – Name of the modality for which the classifier was trained. A classifier must have been trained for this modality (i.e., labels_key must have been provided during setup_anndata for this modality).

  • target_modality (str | None (default: None)) – Name of the modality to predict cell types for. If None, defaults to the other modality (not labeled_modality) for cross-modal label transfer.

  • target_adata (AnnData | None (default: None)) – AnnData object for the target modality. If None, uses the registered AnnData.

  • indices (Sequence[int] | None (default: None)) – Indices of cells to predict. If None, all cells are used.

  • batch_size (int (default: 2048)) – Minibatch size for data loading.

Return type:

dict[str, ndarray]

Returns:

Dictionary with keys: - “predictions”: Array of predicted label categories (strings) - “probabilities”: Array of class probabilities (n_cells, n_classes) - “confidence”: Array of maximum probability for each cell

Raises:

ValueError – If labeled_modality is not a valid modality name. If target_modality is not a valid modality name. If no classifier was trained for the labeled modality.

classmethod DIAGVI.register_manager(adata_manager)[source]#

Registers an AnnDataManager instance with this model class.

Stores the AnnDataManager reference in a class-specific manager store. Intended for use in the setup_anndata() class method followed up by retrieval of the AnnDataManager via the _get_most_recent_anndata_manager() method in the model init method.

Notes

Subsequent calls to this method with an AnnDataManager instance referring to the same underlying AnnData object will overwrite the reference to previous AnnDataManager.

DIAGVI.save(dir_path, prefix=None, overwrite=False, save_anndata=False, save_kwargs=None)[source]#

Save the DIAGVI model and optionally the AnnData objects.

Parameters:
  • dir_path (str) – Directory path to save the model and AnnData objects.

  • prefix (str | None (default: None)) – Prefix for the saved model file name.

  • overwrite (bool (default: False)) – Whether to overwrite the directory if it already exists.

  • save_anndata (bool (default: False)) – Whether to save the AnnData objects used to train the model.

  • save_kwargs (dict | None (default: None)) – Additional keyword arguments for torch.save.

Raises:

ValueError – If dir_path exists and overwrite is False.

classmethod DIAGVI.setup_anndata(adata, layer=None, batch_key=None, labels_key=None, likelihood='nb', normalize_lib=True, gmm_prior=False, n_mixture_components=10, unlabeled_category='unknown', **kwargs)[source]#

Sets up the AnnData object for this model.

A mapping will be created between data fields used by this model to their respective locations in adata. None of the data in adata are modified. Only adds fields to adata.

Parameters:
  • adata (AnnData) – AnnData object to register.

  • batch_key (str | None (default: None)) – key in adata.obs for batch information. Categories will automatically be converted into integer categories and saved to adata.obs[‘_scvi_batch’]. If None, assigns the same batch to all the data.

  • labels_key (str | None (default: None)) – key in adata.obs for label information. Categories will automatically be converted into integer categories and saved to adata.obs[‘_scvi_labels’]. If None, assigns the same label to all the data.

  • layer (str | None (default: None)) – if not None, uses this as the key in adata.layers for raw count data.

  • likelihood (Literal['nb', 'zinb', 'nbmixture', 'normal', 'log1pnormal', 'ziln', 'zig'] (default: 'nb')) – Likelihood model for this modality (default: ‘nb’). One of: - ‘nb’ : Negative Binomial - ‘zinb’ : Zero-Inflated Negative Binomial - ‘nbmixture’ : Negative Binomial Mixture (for protein data with background/foreground) - ‘normal’ : Normal distribution - ‘log1pnormal’ : Log1p Normal distribution - ‘ziln’ : Zero-Inflated Log Normal distribution - ‘zig’ : Zero-Inflated Gamma distribution

  • normalize_lib (bool (default: True)) – Whether to normalize counts with library size in the model.

  • gmm_prior (bool (default: False)) – Whether to use a GMM prior for this modality.

  • n_mixture_components (int (default: 10)) – Number of mixture components for the GMM prior. If labels_key is provided, this parameter is ignored and set to the number of unique labels in labels_key.

  • unlabeled_category (str (default: 'unknown')) – Category for unlabeled cells in labels_key.

  • **kwargs – Additional keyword arguments.

classmethod DIAGVI.setup_mudata(mdata, modalities, batch_key=None, labels_key=None, layer=None, likelihood='nb', normalize_lib=True, gmm_prior=False, n_mixture_components=10, unlabeled_category='unknown', **kwargs)[source]#

Register a MuData object for use with DIAGVI.

Parameters:
  • mdata (MuData) – MuData object containing multiple modalities.

  • modalities (list[str]) – List of two modality names from mdata.mod to use.

  • batch_key (dict[str, str] | str | None (default: None)) – Key(s) in adata.obs for batch annotation. Can be a single string (applied to all modalities) or a dict mapping modality names to keys.

  • labels_key (dict[str, str] | str | None (default: None)) – Key(s) in adata.obs for cell type labels.

  • layer (dict[str, str | None] | str | None (default: None)) – Layer(s) in adata to use as input.

  • likelihood (dict[str, Literal['nb', 'zinb', 'nbmixture', 'normal']] | str (default: 'nb')) – Likelihood model(s) for each modality. Options: ‘nb’, ‘zinb’, ‘nbmixture’, ‘normal’. Default is ‘nb’.

  • normalize_lib (dict[str, bool] | bool (default: True)) – Whether to normalize counts with library size in the model for each modality.

  • gmm_prior (dict[str, bool] | bool (default: False)) – Whether to use GMM prior for each modality. Default is False.

  • n_mixture_components (dict[str, int] | int (default: 10)) – Number of mixture components for the GMM prior for each modality. If labels_key is provided, this parameter is ignored and set to the number of unique labels in labels_key.

  • unlabeled_category (dict[str, str] | str (default: 'unknown')) – Category name for unlabeled cells. Default is ‘unknown’.

  • **kwargs – Additional keyword arguments passed to setup_anndata.

DIAGVI.to_device(device)[source]#

Move the model to the device.

Parameters:

device (str | int | device) – Device to move model to. Options: ‘cpu’ for CPU, integer GPU index (e.g., 0), ‘cuda:X’ where X is the GPU index (e.g. ‘cuda:0’), or a torch.device object (including XLA devices for TPU). See torch.device for more info.

Examples

>>> adata = scvi.data.synthetic_iid()
>>> model = scvi.model.SCVI(adata)
>>> model.to_device("cpu")  # moves model to CPU
>>> model.to_device("cuda:0")  # moves model to GPU 0
>>> model.to_device(0)  # also moves model to GPU 0
DIAGVI.train(max_epochs=None, lr=0.001, batch_size=256, train_size=0.9, accelerator='auto', devices='auto', shuffle_set_split=True, datasplitter_kwargs=None, plan_kwargs=None, **kwargs)[source]#

Train the DIAGVI model.

Parameters:
  • max_epochs (int | None (default: None)) – Maximum number of training epochs. If None, a heuristic is used.

  • lr (float (default: 0.001)) – Learning rate for optimization.

  • batch_size (int (default: 256)) – Minibatch size for training.

  • accelerator (str (default: 'auto')) – Supports passing different accelerator types (“cpu”, “gpu”, “tpu”, “ipu”, “hpu”, “mps, “auto”) as well as custom accelerator instances.

  • devices (int | list[int] | str (default: 'auto')) – The devices to use. Can be set to a non-negative index (int or str), a sequence of device indices (list or comma-separated str), the value -1 to indicate all available devices, or “auto” for automatic selection based on the chosen accelerator. If set to “auto” and accelerator is not determined to be “cpu”, then devices will be set to the first available device.

  • train_size (float (default: 0.9)) – Proportion of data to use for training (rest for validation).

  • shuffle_set_split (bool (default: True)) – Whether to shuffle data before splitting into train/validation.

  • datasplitter_kwargs (dict | None (default: None)) –

    Additional keyword arguments for the DataSplitter and DataLoaders. Can be either: - A dict of shared kwargs applied to all modalities - A nested dict mapping modality names to their specific kwargs,

    e.g., {"rna": {"external_indexing": [train, val, test]}, "protein": {...}}

  • plan_kwargs (dict | None (default: None)) – Additional keyword arguments for the training plan.

  • **kwargs – Additional keyword arguments for the Trainer.

DIAGVI.transfer_fields(adata, **kwargs)[source]#

Transfer fields from a model to an AnnData object.

Return type:

AnnData

DIAGVI.update_setup_method_args(setup_method_args)[source]#

Update setup method args.

Parameters:

setup_method_args (dict) – This is a bit of a misnomer, this is a dict representing kwargs of the setup method that will be used to update the existing values in the registry of this instance.

DIAGVI.view_anndata_setup(adata=None, hide_state_registries=False)[source]#

Print summary of the setup for the initial AnnData or a given AnnData object.

Parameters:
  • adata (AnnData | MuData | None (default: None)) – AnnData object setup with setup_anndata or transfer_fields().

  • hide_state_registries (bool (default: False)) – If True, prints a shortened summary without details of each state registry.

Return type:

None

DIAGVI.view_registry(hide_state_registries=False)[source]#

Prints summary of the registry.

Parameters:

hide_state_registries (bool (default: False)) – If True, prints a shortened summary without details of each state registry.

Return type:

None

static DIAGVI.view_setup_args(dir_path, prefix=None)[source]#

Print args used to setup a saved model.

Parameters:
  • dir_path (str) – Path to saved outputs.

  • prefix (str | None (default: None)) – Prefix of saved file names.

Return type:

None

DIAGVI.view_setup_method_args()[source]#

Prints setup kwargs used to produce a given registry.

Parameters:

registry – Registry produced by an AnnDataManager.

Return type:

None