BSVAE: Gaussian Mixture VAE for Module Discovery
BSVAE is a PyTorch package for feature-level module discovery in omics data. The main model, GMMModuleVAE, learns latent structure across features such as genes, transcripts, or proteins, then supports downstream network extraction, module assignment, latent analysis, and simulation-based benchmarking.
What The Package Provides
bsvae-trainfor training a two-phase GMM-VAEbsvae-sweep-kfor selecting the number of modules (K) with held-out validation and optional stability replicatesbsvae-networksfor post-training network extraction, module extraction (with gene coherence and an isoform-specific residual scan at transcript level), targeted-gene tests, latent export, and latent analysisbsvae-simulatefor synthetic data generation, scenario-grid simulation, and module-recovery benchmarkingbsvae-benchmarkfor head-to-head runs of BSVAE, WGCNA, acorde, usage networks, ICA/NMF and TWN on simulated scenario grids
Installation
Install from PyPI:
pip install bsvae
Install from source:
git clone https://github.com/heart-gen/BSVAE.git
cd BSVAE
pip install -e .
Optional dependencies:
anndatais only needed for.h5adinput or.h5adlatent export helperspyyamlis only needed to read YAML scenario configs (JSON configs need nothing)- R with
WGCNA(plusglassofor TWN) is only needed for the WGCNA, acorde and TWN methods ofbsvae-benchmark; check withbsvae-benchmark check-env
pip install "bsvae[anndata,yaml]"
For development, use Poetry: poetry install --with dev --extras "anndata yaml".
Input Data Contract
Training and most downstream commands expect an expression matrix in features x samples orientation:
- rows are features such as genes, transcripts, or proteins
- columns are sample IDs
- the first column is the feature index when using CSV/TSV
Supported loaders:
.csv/.csv.gz.tsv/.tsv.gz.h5/.hdf5.h5adwith optionalanndata
CLI Entry Points
bsvae-trainbsvae-sweep-kbsvae-networksbsvae-simulatebsvae-benchmark(BSVAE vs WGCNA, transcript-usage networks, ICA/NMF, acorde and TWN on simulated data; needs R withWGCNA, plusglassofor TWN)
At transcript level, extract-modules returns two independent outputs: coherent module assignments (the gene-coherence policy is the default whenever --expr and --tx2gene are given) and the isoform-specific residual scan over every multi-isoform transcript, so module membership and isoform-specific divergence are reported and validated separately. Further transcript-level commands in bsvae-networks: consensus-modules (majority vote over seeds with a per-feature stability score), isoform-specific (isoforms whose expression tracks a module other than their gene's), target-genes (calibrated membership tests for candidate genes or GWAS loci from a gene list, transcript list or BED file, with a case/control interaction test), target-genes-combine (pool those tests across cohorts or brain regions), module-quality (kME, enrichment, preservation in a second dataset) and regress-covariates (remove batch and other known covariates before training).
bsvae-networks subcommands
| Subcommand | Purpose |
|---|---|
extract-networks |
feature-feature networks (mu_cosine, gamma_knn) |
extract-modules |
post-processed modules, eigengenes, kME; at transcript level also gene coherence and the isoform-specific scan |
consensus-modules |
majority vote over seeds with a per-feature stability score |
isoform-specific |
isoforms whose expression tracks a module other than their gene's, optionally cross-fitted (--folds) |
isoform-residuals |
the gene-programme-removed residual matrix |
target-genes, target-genes-combine |
calibrated tests for candidate genes or GWAS loci, and pooling across cohorts |
module-quality |
kME, GMT enrichment, preservation in a second dataset |
regress-covariates |
remove batch and other known covariates before training |
export-latents, latent-analysis |
latent arrays, clustering and embeddings |
Flags worth knowing at transcript level: --tx2gene, --covariates, --loo-eigengene {approx,exact,off}, --max-target-corr, --samples / --exclude-samples (train fold models for cross-fitting), --aggregate-to-gene, and --amp on bsvae-train for mixed precision. See the CLI reference for every flag and default.
Quickstart
Train a small model:
bsvae-train pilot_run \
--dataset data/expression.csv \
--epochs 50 \
--n-modules 12 \
--latent-dim 16
Recommended production flow: train with a generous number of GMM components, about three times the number of modules you expect (at least 20), and let extract-modules merge components whose eigengenes correlate (see Choosing K). bsvae-sweep-k is an optional check of that component count by held-out ELBO and, with --stability-reps, by seed stability; it retrains the selected model so the steps below can start from it.
bsvae-sweep-k sweep1 \
--dataset data/expression.csv \
--k-grid 8,12,16,24,32 \
--sweep-epochs 60 \
--stability-reps 5 \
--val-frac 0.1
This writes sweep outputs under results/sweep1/sweep_k/ and retrains the selected model under results/sweep1/final_k<K>/.
Extract module assignments from the final model (post-processing adds a grey class, a minimum module size and eigengene merging):
bsvae-networks extract-modules \
--model-path results/sweep1/final_k16 \
--dataset data/expression.csv \
--output-dir results/sweep1/final_k16/modules \
--expr data/expression.csv \
--soft-eigengenes
With transcript-level data, pass --tx2gene as well. extract-modules then produces two independent outputs: module assignments annotated by the gene-coherence policy (default; --no-gene-coherence to disable) and the isoform-specific residual scan (--no-isoform-calls to skip, --loo-eigengene to change the leave-one-gene-out eigengene correction). A transcript's module label never decides whether it is tested. For calibrated calls, pass technical covariates (--covariates) and, where modules were learned on the same samples, cross-fit over sample folds (isoform-specific --folds).
bsvae-networks extract-modules \
--model-path results/iso1 \
--dataset data/transcripts.tsv.gz \
--output-dir results/iso1/modules \
--expr data/transcripts.tsv.gz \
--tx2gene data/tx2gene.tsv
This adds modules_raw.csv, gene_coherence.csv and isoform_specific.csv next to modules.csv; see the CLI reference for the columns.
Export latents:
bsvae-networks export-latents \
--model-path results/sweep1/final_k16 \
--dataset data/expression.csv \
--output results/sweep1/final_k16/latents
This command writes results/sweep1/final_k16/latents.npz containing mu, logvar, gamma, and feature_ids.
Extract feature-feature networks:
bsvae-networks extract-networks \
--model-path results/sweep1/final_k16 \
--dataset data/expression.csv \
--output-dir results/sweep1/final_k16/networks \
--methods mu_cosine gamma_knn \
--top-k 50
Run latent-space analysis:
bsvae-networks latent-analysis \
--model-path results/sweep1/final_k16 \
--dataset data/expression.csv \
--output-dir results/sweep1/final_k16/latent_analysis \
--kmeans-k 16 \
--umap
Worked Example: Transcript-Level Workflow On Simulated Data
This end-to-end example uses the same simulator as the benchmark study, so every file below has a known truth to compare against. It runs in minutes on a CPU. The real-data version of the same chain (residualise, bsvae-sweep-k, extract-modules, extract-networks, export-latents) is in the tutorial.
# 1. simulate one transcript-level scenario (500 genes, 6 modules, 150 samples)
bsvae-simulate init-config --output iso.json --isoform # edit n_genes, n_modules, grid as needed
bsvae-simulate generate-grid --config iso.json --outdir sim --reps 1 --base-seed 13
SCEN=$(ls -d sim/scenarios/*/rep_000 | head -1)
# 2. choose K, then retrain the selected model (transcript-to-gene map enables the hierarchical loss)
bsvae-sweep-k iso1 --dataset $SCEN/expr/features_x_samples.tsv.gz \
--k-grid 12,18,24 --sweep-epochs 60 --stability-reps 2 --val-frac 0.1 \
--tx2gene $SCEN/truth/tx2gene.tsv --hier-strength 0.2 --isoform-stratified
# 3. two outputs: gene-coherent modules and the isoform-specific residual scan
bsvae-networks extract-modules --model-path results/iso1/final_k<K> \
--dataset $SCEN/expr/features_x_samples.tsv.gz --expr $SCEN/expr/features_x_samples.tsv.gz \
--tx2gene $SCEN/truth/tx2gene.tsv --covariates $SCEN/covariates.tsv.gz \
--loo-eigengene approx --max-target-corr 0.4 --soft-eigengenes \
--output-dir results/iso1/modules
results/iso1/modules/ now holds modules.csv (gene-coherent assignments, grey is -1), modules_raw.csv, gene_coherence.csv, isoform_specific.csv, eigengenes.csv, kME.csv and gamma.npz. Score the modules against the simulator's truth:
import pandas as pd
from sklearn.metrics import adjusted_rand_score
pred = pd.read_csv("results/iso1/modules/modules.csv", index_col="feature_id")["module"]
truth = pd.read_csv(f"{scen}/truth/modules_hard.csv", index_col="feature_id")["module"]
print(adjusted_rand_score(truth.loc[pred.index], pred)) # grey (-1) counted as its own label
calls = pd.read_csv("results/iso1/modules/isoform_specific.csv")
sw = pd.read_csv(f"{scen}/truth/switch_table.tsv.gz", sep="\t", index_col=0)
print(calls["called"].sum(), "calls;", sw["is_switched"].sum(), "truly switched isoforms")
For calibrated calls when modules were learned on the same samples, train one fold model per sample split and cross-fit:
bsvae-networks isoform-specific --dataset $SCEN/expr/features_x_samples.tsv.gz \
--tx2gene $SCEN/truth/tx2gene.tsv --covariates $SCEN/covariates.tsv.gz \
--modules fold0/modules/modules_raw.csv fold1/modules/modules_raw.csv --folds folds.tsv \
--max-target-corr 0.4 --output results/iso1/isoform_specific_crossfit.tsv
Then test candidate genes against the same modules, and pool several cohorts:
bsvae-networks target-genes --dataset data/transcripts.tsv.gz --tx2gene data/tx2gene.tsv \
--modules results/iso1/modules/modules.csv --genes candidates.txt --output-dir results/iso1/targets
bsvae-networks target-genes-combine \
--inputs DLPFC=results/dlpfc/targets/target_isoforms.tsv HIPP=results/hipp/targets/target_isoforms.tsv \
--n-samples DLPFC=160 HIPP=150 --output results/targets_combined.tsv
Training Outputs
bsvae-train writes to results/<experiment>/ by default:
model.ptspecs.jsontrain_losses.csvmodel-<epoch>.ptcheckpoints when--checkpoint-everyis greater than zero
bsvae-sweep-k writes:
results/<name>/sweep_k/sweep_results.csvresults/<name>/sweep_k/sweep_summary.jsonresults/<name>/sweep_k/k<K>/rep_<rep>/...per-sweep run outputsresults/<name>/final_k<K>/...for the final retrained model when--train-finalis enabled
Simulation and Benchmarking
Generate one synthetic dataset:
bsvae-simulate generate \
--output data/sim_expr.csv \
--save-ground-truth data/sim_truth.csv
Generate a scenario grid:
bsvae-simulate init-config --output sim.yaml
bsvae-simulate generate-grid \
--config sim.yaml \
--outdir results/sim_pub_v1 \
--reps 30 \
--base-seed 13
Validate the generated grid:
bsvae-simulate validate-grid --grid-dir results/sim_pub_v1
Each scenario replicate writes method-ready files under results/sim_pub_v1/scenarios/<scenario_id>/rep_<rep>/, including:
expr/features_x_samples.tsv.gzexpr/samples_x_features.tsv.gztruth/modules_hard.csv(null features are-1)truth/tx2gene.tsvandtruth/modules_gene.csvfor the transcript-level family (bsvae-simulate init-config --isoform)method_inputs.json
Run BSVAE and WGCNA head-to-head on a grid and summarise grey-aware ARI/NMI, best-match Jaccard, and gene-level recovery:
bsvae-benchmark check-env # torch, Rscript, WGCNA
bsvae-benchmark run --config iso.json --outdir results/bench --methods bsvae,wgcna --seeds 0,1,2
bsvae-benchmark summarize --results results/bench/results.tsv --by scenario_id,method
run is resumable and can be split across a cluster with --rep-ids and --scenario-ids. Comparison methods are bsvae_raw, bsvae_hier, bsvae_isoform, wgcna_raw, acorde, twn, usage_network, ica and nmf. In the companion study (10 replicates, transcript-level scenarios, 150 samples), mean all-feature ARI was:
| Method | ARI (all features) | Unassigned |
|---|---|---|
bsvae (gene coherence) |
0.78 | 10% |
bsvae_raw |
0.70 | 10% |
wgcna (gene coherence) |
0.65 | 0.3% |
wgcna_raw |
0.54 | 0.3% |
acorde |
0.44 | 35% |
These are simulation results; see Benchmarking for the pipeline, output files, matched-coverage metrics and caveats, including that the residual-scan defaults were not validated in every simulated cell.
Documentation
Full documentation is at bsvae.readthedocs.io:
- Installation and Quick Start
- Tutorial (real-data workflow and the simulated example)
- CLI reference, network workflows and hyperparameters
- Benchmarking and HPC
The same pages are in docs/ in this repository. Release notes are in CHANGELOG.md.
License
This project is licensed under the GNU General Public License v3.0.
Metadata
Release files for bsvae 0.4.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bsvae-0.4.2.tar.gz | 165.5 kB | With provenance | Details for bsvae-0.4.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bsvae-0.4.2-py3-none-any.whl | Python 3 | none | any | Details for bsvae-0.4.2-py3-none-any.whl |
Total release size: 354.0 kB
Release files / bsvae-0.4.2.tar.gz
| Download URL | bsvae-0.4.2.tar.gz |
|---|---|
| Size | 165.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cde118d0577c552880782c63c5ce7d4a5e388564aaaee9f291aa7cf4c02243a3
|
|
BLAKE2b-256 checksum How to use checksums |
6c6e0368c2bbcf8263ea3776ef8321b65bcb63318b8791a92d703d57b83b35bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logRelease files / bsvae-0.4.2-py3-none-any.whl
| Download URL | bsvae-0.4.2-py3-none-any.whl |
|---|---|
| Size | 188.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4945b1354e1108d27cc9839eeb02e57d8c6f4c9dde863d7eeb18286882197a50
|
|
BLAKE2b-256 checksum How to use checksums |
89d435a831d5955f0d4be4618e44272800f729027ddff4ab1306d39d16170b64
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logSecurity for bsvae 0.4.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for bsvae-0.4.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.