The full description of
(a) To better collaborate information from different modalities, clusters and topic heads are trained based on mutually refined neighborhood information by encouraging consistent clustering assignments of mutual nearest neighbors of corresponding cells across modalities in the embedding space.
(b) ECR clusters gene embeddings
(c) Sparse linear decoders learn topic embeddings, gene embeddings, and sparse topic-gene dependencies during reconstruction, thus ensuring model interpretability.
Note: The complete installation process, including environment setup and dependency installation, typically takes around 1β1.5 hours.
conda create --name scE2TM_env python=3.8.8 -y
conda activate scE2TM_envpip install torch==1.9.1+cu102 torchvision==0.10.1+cu102 torchaudio==0.9.1 \
-f https://download.pytorch.org/whl/torch_stable.html| Hardware | Command |
|---|---|
| CPU | pip install scE2TM |
| GPU | pip install scE2TM[gpu] |
git clone https://github.com/nbnbhwyy/scE2TM.git
cd scE2TMThen install dependencies based on your hardware:
| Hardware | Command |
|---|---|
| CPU | pip install -r requirements-cpu.txt |
| GPU | pip install -r requirements-gpu.txt |
Label usage: scEΒ²TM is label-free by default. Cell-type annotations are not used for model training. The optional
--use_labelsflag, oruse_labels=Truein the Python API, only enables label-dependent evaluation metrics such as ARI, NMI, and Purity when ground-truth annotations are available.
- Gene expression matrix: cell-by-gene matrix (
*_HIGHPRE.csv) - Cell type annotations: ground-truth labels (
*_cell_anno.csv) (optional; used only for evaluation and not for model training) - Foundation-model embeddings: pre-trained cell embeddings (
*.csv)
We provide the Wang dataset as a default example to help users understand and debug the code.
python run.pyOn the provided Wang example dataset, the demo typically finishes within 1β2 minutes on an NVIDIA RTX 3090 GPU. We also thank the anonymous reviewer for testing the CPU-only workflow on macOS; based on the reviewerβs report, the same example may require approximately 10β15 minutes on CPU-only systems depending on hardware and software configuration.
python run.py --dataset_name Wang --num_topics 50# Run on GPU 0
python run.py --gpu_id 0
# Run on CPU
python run.py --gpu_id -1By default, scEΒ²TM runs without cell-type annotations. Adding --use_labels only computes label-dependent evaluation metrics and does not use labels during model training.
# Compute ARI, NMI, Purity, and other label-dependent metrics
python run.py --use_labelspython run.py \
--dataset_name Wang \
--num_topics 100 \
--num_neighbors 15 \
--num_top_genes 10 \
--tac_weight 1.0 \
--gpu_id 0 \
--data_dir ./data \
--output_dir ./output \
--use_labels Falsejupyter notebook scE2TM_demo_on_Wang_dataset.ipynbThe package provides a simple function scE2TM() that returns all results directly.
from scE2TM import scE2TM
# Basic labelβfree run (no cell type annotations needed)
results = scE2TM(
dataset_name='Wang', # dataset name
data_dir='./data', # directory containing CSV files
output_dir='./output', # where to save outputs
num_topics=100, # number of topics (K)
num_neighbors=15, # number of neighbors for graph construction
weight_loss_ECR=20.0, # weight for the ECR loss
epochs=500, # total training epochs
gpu_id=0, # GPU device; use -1 for CPU
)
# Label-free training with optional label-dependent evaluation
# Cell-type annotations are used only to compute ARI, NMI, and Purity.
# They are not used during model training.
results = scE2TM(
dataset_name='Wang',
use_labels=True, # compute label-dependent metrics only; labels are not used for training
num_topics=100,
num_neighbors=15,
weight_loss_ECR=20.0,
epochs=500,
gpu_id=0,
)
# Access the resulting matrices
beta = results['topic_gene_matrix']
theta = results['cell_topic_matrix'] # latent cell-topic scores (cells Γ topics)
topic_emb = results['topic_embeddings']
gene_emb = results['gene_embeddings']After successful execution, the following files are saved in output/Wang/:
| File | Description |
|---|---|
Wang.pth |
Trained model checkpoint |
Wang_topic_distribution.csv |
Cell-topic distribution (theta, Latent cell-topic scores (cells Γ topics)) |
Wang_topic_embedding.csv |
Topic embeddings |
Wang_gene_embedding.csv |
Gene embeddings |
Wang_tg.csv |
Topic-gene matrix (beta) |
We provide tutorials in the tutorial/ directory covering both basic usage and the main downstream analyses used in the paper.
| Tutorial | Description |
|---|---|
Prepare_foundation_embeddings_scGPT.ipynb |
Generate foundation-model embeddings using scGPT for input to scE2TM. |
Topic_number_selection_with_stability.ipynb |
Use topic stability, diversity, coherence, and clustering metrics (ARI/NMI, optional) to guide the choice of the optimal number of topics (K). |
Clustering and Interpretable Evaluation.ipynb |
Evaluate clustering performance and interpretability of the learned topics. |
Consistency between rare types and topics.ipynb |
Evaluate how well learned topics capture rare cell populations and their consistency with rare cell types. |
Pathway Enrichment.ipynb |
Perform pathway enrichment analysis on learned topics. |
Topic gene embedding.ipynb |
Visualize and analyze topic-gene embeddings. |
Topic perturbation experiment.ipynb |
Analyze the biological effects of perturbing topic intensities. |
We also provide tutorials for several relevant baselines in the baseline/ directory:
scVI.ipynbβ Variational inference for single-cell datascVI-LD.ipynbβ scVI with latent Dirichlet allocationscETM.ipynbβ Embedded topic model for single-cell datad-scIGM.ipynbβ Deep single-cell interpretable generative modelbaseline_louvain_clustering.ipynbβ Louvain clustering on PCAβreduced expression databaseline_cNMF.ipynbβ Consensus NMF topic modelingbaseline_SPECTRA.ipynbβ SPECTRA factor analysis
These notebooks are designed to help users reproduce baseline results in a consistent environment. After setting up the main
- any additional package installation required for that baseline,
- how to run the method on the provided example dataset, and
- how to obtain outputs for comparison with
$scE^2TM$ .
In general, the workflow is:
conda activate scE2TM_envThen open the corresponding notebook in baseline/ and follow the dependency installation and execution instructions provided there.
scE2TM/
βββ baseline/ # Baseline tutorials: scVI, scVI-LD, scETM, d-scIGM
βββ configs/ # Configuration files
βββ data/ # Example datasets and processed inputs
βββ models/ # Core model implementations
βββ runners/ # Training / running utilities
βββ tutorial/ # scE2TM tutorials and downstream analysis notebooks
βββ utils/ # Utility functions
βββ run.py # Main entry point
βββ requirements.txt
βββ LICENSE.txt
This project is licensed under the MIT License. See LICENSE.txt for details.
For questions or support, please open an issue on GitHub or contact:
