ReconST API Reference¶
ReconST provides a lightweight, autoencoder-based framework for gene panel selection in spatial transcriptomics.
This page summarizes the key classes and functions, organized by workflow: data preparation, model definition, training, evaluation, and gene selection.
1. Model¶
FeatureScreeningAutoencoder¶
Autoencoder with a learnable feature-importance layer for gene selection.
Architecture
- Learnable gene-importance layer
- Encoder: input_size → 512 → 256 → embedding_size
- Decoder: embedding_size → 256 → 512 → input_size
Parameters
- input_size (int): Number of genes
- embedding_size (int): Latent dimension
- dp (float): Dropout probability
- lk (float): LeakyReLU negative slope
Attributes
- feature_importance: Learnable gene weights
- encoder, decoder: Sequential modules
Example
import torch
from reconst import FeatureScreeningAutoencoder
model = FeatureScreeningAutoencoder(input_size=2000, embedding_size=64)
x = torch.randn(32, 2000)
screened, latent, recon = model(x)
2. Data Utilities¶
create_data_loader(adata, batch_size=256, train_split=0.8, shuffle=True)¶
Create training and test DataLoaders from an AnnData object.
Example
import scanpy as sc
from reconst import create_data_loader
adata = sc.read_h5ad("data.h5ad")
train_loader, test_loader = create_data_loader(adata, batch_size=128)
prepare_common_genes(adata1, adata2)¶
Find and align common genes between two datasets.
3. Training¶
train_model(model, train_loader, test_loader, num_epochs=20, lr=1e-3, weight_decay=1e-5, l_lambda=1e-4, device='cpu')¶
Train the autoencoder with MSE + L1 sparsity penalty.
Example
from reconst import train_model, FeatureScreeningAutoencoder
model = FeatureScreeningAutoencoder(2000, 64).to('cuda')
train_losses, test_losses = train_model(
model, train_loader, test_loader,
num_epochs=50, lr=1e-3, l_lambda=1e-4, device='cuda'
)
4. Evaluation¶
evaluate_model(model, test_loader, gene_mask=None, device='cpu')¶
Compute reconstruction MSE with all genes or a selected subset.
Example
loss = evaluate_model(model, test_loader)
5. Gene Selection¶
select_genes(model, threshold=0.001)¶
Extract selected genes based on learned feature importance.
Example
gene_mask, importances = select_genes(model, threshold=0.01)
selected_names = adata.var_names[gene_mask]
Summary¶
ReconST exposes a streamlined, end-to-end workflow:
- Load and align AnnData
- Build DataLoaders
- Initialize the autoencoder
- Train with sparsity
- Evaluate
- Select informative genes