Skip to content

Commit 481b92e

Browse files
moinfarclaudeori-kron-wispre-commit-ci[bot]
authored
Add DRVI (#3866)
## Add DRVI This PR adds **DRVI** as `scvi.external.DRVI`. This is a rewrite of #3699, reusing scvi-tools functionality and removing extras as much as possible. ### Design: `DRVI` now subclasses `scvi.model.SCVI` directly and only changes what is actually different: - Decoder is replaced by a splitting + additive decoder + pooling (`DecoderDRVI` extends `DecoderSCVI`, `SplitFCLayers` extends `FCLayers`). Disentanglement is induced in the decoder — the latent is reshaped into `n_split_latent` independent groups, each decoded separately, then aggregated. - Adds log-space likelihood for negative binomial. - Adds latent stats and interpretability mixins to interpret latent dimensions (plotting functions are removed). - Reuses everything else from SCVI unchanged: encoder, prior, covariate/batch handling (including `batch_representation="embedding"`), RNASeqMixin, size factors, minified mode, scArches transfer, training via a datamodule, and `setup_anndata`. To enable the decoder swap without forking `FCLayers`, `scvi.nn.FCLayers` gained a few small overridable seams (`_build_layer`, `_apply_layer`, `_apply_batch_norm`). These are behavior-preserving for existing models (SCVI, ...) —`SplitFCLayers` overrides them to use per-split weights. ### Likelihoods In addition to scvi's `nb`, `zinb` and `poisson`, DRVI adds log-space nb: - `pnb` (default of DRVI) — parametrized NB, same mean as `nb` but evaluated in log space via the new `LogNegativeBinomial` distribution (should be numerically more stable but equivalent to scvi's `NegativeBinomial`). - `normal` / `normal_unit_var` — Gaussian with the mean modeled directly (no library/softmax), for continuous / log-normalized input. ### Interpretability Because the decoder is additive over splits, each latent dimension's contribution to every gene can be measured directly. The included methods — `set_latent_dimension_stats`, `calculate_interpretability_scores`, `get_interpretability_scores` — quantify these contributions and extract top genes per dimension. ### Dependencies Adds `stacked-linear>=0.2.0` (used for the parallel per-split linear layers). That package requires no dependencies other than PyTorch. Also, the same logic can be included as a snippet instead. --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: ori-kron-wis <ori.kronfeld@weizmann.ac.il> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a5857bf commit 481b92e

21 files changed

Lines changed: 2573 additions & 9 deletions

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ to [Semantic Versioning]. The full commit history is available in the [commit lo
99

1010
#### Added
1111

12-
- Add scvi-tools MCP package that gives any MCP-compatible LLM access to scvi-tools knowledge
12+
- Add [scvi-tools MCP](https://scvi-tools-mcp.readthedocs.io/en/latest/index.html) package
13+
that gives any MCP-compatible LLM access to scvi-tools knowledge.
1314
- Add {class}`~scvi.dataloaders.AnnbatchDataModule` for out-of-core dataloading via `annbatch`,
1415
enabling memory-efficient training on large-scale datasets stored as sharded Zarr collections,
1516
with support for batch covariates, {pr}`3620`.
1617
- Add support for rapids-singlecell, {pr}`3811`.
18+
- Add {class}`scvi.external.DRVI` for unsupervised disentangled representation learning of
19+
single-cell omics, {pr}`3866`.
1720
- Add {class}`scvi.external.JointEmbeddingSCVI`, a self-supervised SCVI variant using binomial
1821
thinning and a cross-correlation objective (CCO) for robust embeddings, {pr}`3883`.
1922
- Add {class}`scvi.external.CYTOVI` KNN imputation backend option to be cuML, {pr}`3821`.

docs/api/developer.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ Module classes in the external API with respective generative and inference proc
197197
external.scviva.NicheLossOutput
198198
external.sysvi.SysVAE
199199
external.diagvi.DIAGVAE
200+
external.drvi.DRVIModule
200201
external.JointEmbeddingVAE
201202
```
202203

docs/api/user.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import scvi
6767
external.SysVI
6868
external.SCVIVA
6969
external.DIAGVI
70+
external.DRVI
7071
external.JointEmbeddingSCVI
7172
external.Tangram
7273
```

docs/references.bib

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,16 @@ @article{Weinberger26
387387
publisher={Nature Publishing Group}
388388
}
389389

390+
@article{Moinfar2024,
391+
title = {Disentangling cellular heterogeneity into interpretable biological factors through structured latent representations},
392+
author = {Moinfar, Amir Ali and Theis, Fabian J},
393+
doi = {10.1101/2024.11.06.622266},
394+
year = {2024},
395+
month = nov,
396+
journal = {bioRxiv},
397+
publisher = {Cold Spring Harbor Laboratory}
398+
}
399+
390400
@article{Svensson26,
391401
title={Improving SCVI for low-count cells through self-supervised augmentation},
392402
author={Valentine Svensson},

docs/tutorials/index_r.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Perform basic Python operations in an R environment
1919
```
2020

2121
```{customcard}
22-
:path: notebooks/quick_start/api_overview_in_R
22+
:path: notebooks/r/api_overview_in_R
2323
:tags: Analysis, Differential-comparison, Dimensionality-reduction, Removal-of-variance, Dev, Integration
2424
2525
Go through the typical steps of an scvi-tools workflow in R

docs/tutorials/index_scrna.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ notebooks/scrna/linear_decoder
1616
notebooks/scrna/amortized_lda
1717
notebooks/scrna/AutoZI_tutorial
1818
notebooks/scrna/sysVI
19+
notebooks/scrna/JointEmbeddingSCVI_tutorial
20+
notebooks/scrna/DRVI_pipeline
1921
notebooks/scrna/decipher_tutorial
2022
notebooks/scrna/velovi
21-
notebooks/scrna/JointEmbeddingSCVI_tutorial
2223
notebooks/scrna/Tahoe100_mrVI
2324
```
2425

@@ -36,12 +37,6 @@ Perform integration of multiple scRNA-seq datasets both with and without cell ty
3637
Analyze multi-sample scRNA-seq data with MrVI
3738
```
3839

39-
```{customcard}
40-
:path: notebooks/scrna/MrVI_tutorial_torch
41-
:tags: Analysis, Differential-comparison, Dimensionality-reduction, Removal-of-variance
42-
43-
Analyze multi-sample scRNA-seq data with MrVI in PyTorch
44-
```
4540

4641
```{customcard}
4742
:path: notebooks/scrna/scanvi_fix
@@ -113,6 +108,13 @@ Use the AutoZI model to enable gene-specific treatment of zero-inflation
113108
Integrate scRNA-seq datasets with substantial batch effects.
114109
```
115110

111+
```{customcard}
112+
:path: notebooks/scrna/DRVI_pipeline
113+
:tags: Analysis, Dimensionality-reduction, Interpretability, Factor Analysis
114+
115+
Learn an interpretable, disentangled representation of scRNA-seq data with DRVI and link latent dimensions to genes.
116+
```
117+
116118
```{customcard}
117119
:path: notebooks/scrna/decipher_tutorial
118120
:tags: Analysis, Integration

docs/user_guide/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ scvi-tools is composed of models that can perform one or many analysis tasks. In
5454
* - :doc:`/user_guide/models/velovi`
5555
- Deep generative modeling of transcriptional dynamics for RNA velocity analysis in single cells
5656
- :cite:p:`GayosoWeiler23`
57+
* - :doc:`/user_guide/models/drvi`
58+
- Unsupervised deep disentangled representation learning of single-cell omics
59+
- :cite:p:`Moinfar2024`
5760
* - :doc:`/user_guide/models/jointembeddingscvi`
5861
- Improving SCVI for low-count cells through self-supervised augmentation
5962
- :cite:p:`Svensson26`

docs/user_guide/models/drvi.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# DRVI
2+
3+
**DRVI** {cite:p}`Moinfar2024` (Disentangled Representation Variational Inference,
4+
Python class {class}`~scvi.external.DRVI`)
5+
is an unsupervised deep generative model that learns an **interpretable, disentangled**
6+
latent representation of single-cell omics data.
7+
8+
The advantages of DRVI are:
9+
10+
- Disentanglement: DRVI disentangled cell identity, signaling pathways, stress responses, developmental trajectories, and perturbation effects into distinct factors.
11+
- Interpretability: Each factor (latent dimension) can be linked back to genes via the built-in interpretability analyses.
12+
- Built on scVI: {class}`~scvi.external.DRVI` subclasses {class}`~scvi.model.SCVI` and only swaps
13+
the decoder, so it inherits the full SCVI interface — covariates, batch embedding
14+
(`batch_representation="embedding"`), size factors, minified mode, scArches transfer learning,
15+
out-of-core training via a datamodule, and the standard likelihoods (plus DRVI's log-space `pnb`).
16+
17+
18+
```{topic} Tutorials:
19+
20+
- {doc}`/tutorials/notebooks/scrna/DRVI_pipeline`
21+
```
22+
23+
24+
## Method background
25+
26+
DRVI is a variational autoencoder (VAE): the integrated, disentangled representation corresponds to
27+
the latent-space embedding of the cells. It shares the encoder, prior (`N(0, I)`) and reconstruction
28+
likelihoods with {class}`~scvi.module.VAE`. Disentanglement is induced **in the decoder** rather
29+
than through the prior.
30+
31+
### Disentanglement via an additive split decoder
32+
33+
The latent vector of dimension `K` is mapped into `n_split_latent` independent groups ("splits").
34+
Each split is decoded **independently** and the per-split outputs are aggregated to form the final
35+
reconstruction parameters. Because every split can only influence the output through this shared
36+
aggregation, the model is encouraged to allocate independent factors of variation to different
37+
latent dimensions.
38+
39+
Two aggregations over the split dimension are available:
40+
41+
- `logsumexp` (default): aggregation in log space, which makes the decoder additive in the rate.
42+
- `mean`: average of the per-split parameters.
43+
44+
Two latent-to-split mappings are available (`n_latent` should be divisible by `n_split_latent`):
45+
46+
- `split_map` (default): the latent is reshaped into `n_split_latent` chunks and each chunk is passed through a learned per-split linear projection.
47+
- `split_mask`: latent is split into chunks and padded with zeros so, each split `i` keeps only its own chunk.
48+
49+
50+
### Likelihoods modeled in log space
51+
52+
DRVI models the generative parameters in **log space**, which is what makes the `logsumexp`
53+
aggregation a genuine additive decoder (summing the per-split contributions of the rate). In
54+
addition to scvi's `nb`, `zinb` and `poisson`, DRVI adds:
55+
56+
- `pnb`*parametrized negative binomial*: the same mean as `nb` (``library * softmax``) but
57+
parametrized and evaluated in log space via {class}`~scvi.external.drvi.LogNegativeBinomial`,
58+
which is numerically stable and composes the per-split log contributions directly.
59+
- `normal` / `normal_unit_var` — a Gaussian whose mean is modeled **directly** (no library/softmax),
60+
with the per-gene variance modeled in log space (fixed to one for `normal_unit_var`); intended for
61+
continuous, e.g. log-normalized, input.
62+
63+
### Interpretability
64+
65+
Because the decoder is additive over splits, the contribution of each split to the reconstructed
66+
expression of every gene can be measured directly. DRVI exposes per-split parameters in an
67+
inspection mode and provides methods to quantify these contributions both in-distribution (over the
68+
observed cells) and out-of-distribution (by traversing each latent dimension), and to extract the
69+
top genes per latent dimension. See {meth}`~scvi.external.DRVI.set_latent_dimension_stats`,
70+
{meth}`~scvi.external.DRVI.calculate_interpretability_scores` and
71+
{meth}`~scvi.external.DRVI.get_interpretability_scores`.
72+
73+
## Scope: core model only
74+
75+
Only the **core DRVI model** and its **core interpretability methods** are included in scvi-tools
76+
(the model, the additive decoder and log-sum-exp pooling, the log-space likelihoods, and the interpretability methods.).
77+
78+
The **utility functions for latent-space visualization and analysis are not** part of scvi-tools.
79+
For those, install the full [`drvi-py`](https://github.com/theislab/drvi) package:
80+
81+
```bash
82+
pip install drvi-py
83+
```

docs/user_guide/models/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ cytovi
1111
decipher
1212
destvi
1313
diagvi
14+
drvi
1415
gimvi
1516
jointembeddingscvi
1617
linearscvi

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ dependencies = [
4747
"scikit-learn",
4848
"scipy",
4949
"sparse>=0.14.0",
50+
"stacked-linear>=0.2.0",
5051
"tensorboard",
5152
"torch",
5253
"torchmetrics",

0 commit comments

Comments
 (0)