scvi-tools icon indicating copy to clipboard operation
scvi-tools copied to clipboard

MultiVI, MuData, and Mono-modal Use Case: Issues Encountered

Open s-canchi opened this issue 1 year ago • 1 comments

Problem

I am running joint analysis on snRNA-seq data from mouse and human samples, focusing on integration and cross-species annotation transfer. I am interested in MultiVI specifically because my data exhibit significant technical bias (e.g., variation in nUMI-depth, batch, species), and the adversarial integration achieved by the model’s GAN component is appealing for assessing tightness of integration, even in mono-modal scenarios.

When using MULTIVI.setup_mudata, I encounter errors such as:

ValueError: Modalities cannot be None.
KeyError: 'batch not found in adata.obs.'
(Note: this column does exist as verified by .columns)

This suggests the model may require two modalities or there's a problem with reading metadata from the MuData/AnnData objects.

As a workaround, I tried using setup_anndata (with an AnnData input; RNA only), which completes setup but fails with a tensor dimension mismatch error during training. The same AnnData object works perfectly with scVI/scANVI.

All datasets, MuData, and AnnData objects are constructed following best practices (batch/species/UMI columns present and non-missing, feature sets harmonized, etc).

Code (abbreviated for clarity)

import scanpy as sc
import scvi
import numpy as np
import scipy.sparse

# Load harmonized mouse+human AnnData, only shared genes, CSR matrix
adata = sc.read_h5ad("combined_rna_only.h5ad")
# Check input
print(adata.shape)  # (N_cells, N_shared_genes)
for k in ["species", "batch", "log1p_nUMI"]:
    print(f"{k} dtype: ", adata.obs[k].dtype, "n missing: ", adata.obs[k].isnull().sum())
print("CSR:", scipy.sparse.isspmatrix_csr(adata.X))

scvi.model.MULTIVI.setup_anndata(
    adata,
    batch_key="batch",
    categorical_covariate_keys=["species"],
    continuous_covariate_keys=["log1p_nUMI"]
)

model = scvi.model.MULTIVI(
    adata,
    n_genes=adata.shape[1],
    n_regions=0,        # RNA only as per docs
    n_latent=n_latent,
    dropout_rate=dropout_rate,
    dispersion="gene-batch"
)

model.train(max_epochs=epoch, batch_size=batch_size)

Error message (truncated)

RuntimeError: The size of tensor a (13187) must match the size of tensor b (143) at non-singleton dimension 1
...
  File ".../scvi/distributions/_negative_binomial.py", line 406, in __init__
    mu, theta = broadcast_all(mu, theta)

Here, 13187 matches my number of features and I suspect the 143 dimension may stem from a placeholder or uninitialized array for a second modality, even though my data only contains RNA.

What I tried

  • Using the exact same AnnData, scVI and scANVI models run and train without issue.
  • I also verified:
    • Only shared genes, in the same order, in .var_names
    • CSR matrix for .X
    • No missing values in covariates; all columns are correct type
    • All obs columns harmonized
  • This suggests a MULTIVI-specific code path expects higher-dimensional input even when n_regions=0 (mono-modal).
  • Removing the explicit n_genes and n_regions arguments, letting the model infer shape; same error.
  • Subsetting and filtering various obs/var; error persists

Environment

scvi-tools 1.3.0
scanpy 1.11.1
Python 3.12.10
scipy 1.15.2
Platform: x86_64, Red Hat Enterprise Linux 8.10 (Ootpa)
GPU: NVIDIA (cluster node dependent)

Question

From the documentation and examples, it’s unclear whether MultiVI is officially supported or stable for single-modality (RNA-only) data, as there are no explicit mono-modal tutorials. Is there a recommended way to use MultiVI with only RNA data (to leverage adversarial integration and related features)? Or, in single-modality scenarios, is it preferable to use scVI/scANVI instead?

Thank you for your work on this toolkit!

s-canchi avatar Sep 18 '25 22:09 s-canchi

Hey @s-canchi , As for scvi-tools v.1.4 (most recent), Multivi does not support using anndata, so I suggest using mudata with single modality, like you initially tried, as it should work.

Let's see a naiveexample:

mdata = synthetic_iid(return_mudata=True,n_regions=0,n_proteins=0)
mdata["rna"].layers["counts"] = mdata["rna"].X.copy()
mdata.update()
mdata

MuData object with n_obs × n_vars = 400 × 100 obs: 'batch', 'labels' 1 modality rna: 400 x 100 layers: 'counts'

MULTIVI.setup_mudata(
       mdata,
       batch_key="batch",
       modalities={"rna_layer": "rna"},
  )
model = MULTIVI(mdata, n_latent=n_latent)
model.train(1, check_val_every_n_epoch=1)

Epoch 1/1: 100%|██████████| 1/1 [00:00<00:00, 21.82it/s, v_num=1, train_loss=3.5e+3]

You will have to replace the synthetic_iid data creation with the creation of mudata using your RNA anndata, but the rest should be the same. e.g: mdata = MuData({"rna": adata})

In previous versions, there will still be support for anndata, but we also saw that the workaround for making this work can be problematic, as you also saw and thus I suggest moving on to use mudata.

ori-kron-wis avatar Sep 21 '25 09:09 ori-kron-wis