Coder Social home page Coder Social logo

invae's Introduction

inVAE

inVAE is a conditionally invariant variational autoencoder that identifies both spurious (distractors) and invariant features. It leverages domain variability to learn conditionally invariant representations. We show that inVAE captures biological variations in single-cell datasets obtained from diverse conditions and labs. inVAE incorporates biological covariates and mechanisms such as disease states, to learn an invariant data representation. This improves cell classification accuracy significantly.

Installation

  1. PyPI only
    pip install invae

  2. Development Version (latest version on github)
    git clone https://github.com/theislab/inVAE.git
    cd inVAE
    pip install .

Example

Integration of Human Lung Cell Atlas using both healthy and disease samples

Usage

  1. Load the data:
    adata = sc.read(path/to/data)
  2. Optional - Split the data into train, val, test (in supervised case for training classifier as well)
  3. Initialize the model, either Factorized or Non-Factorized:
from inVAE import FinVAE, NFinVAE`

inv_covar_keys = {
    'cont': [],
    'cat': ['cell_type', 'disease'] #set to the keys in the adata
}

spur_covar_keys = {
    'cont': [],
    'cat': ['batch'] #set to the keys in the adata
}

model = FinVAE(
    adata = adata_train,
    layer = 'counts', # The layer where the raw counts are stored in adata (None for adata.X: default)
    inv_covar_keys = inv_covar_keys,
    spur_covar_keys = spur_covar_keys,
    latent_dim_inv = 20, 
    latent_dim_spur = 5,
    device = 'cpu',
    decoder_dist = 'nb'
)

Set inject_covar_in_latent= True if you wish to add the spurious conditions directly to the latent (instead of learning the spurious latents). This gives you the most compatible version to SCVI.

For non-factorized model, use:

model = NFinVAE(
    adata = adata_train,
    layer = 'counts', # The layer where the raw counts are stored in adata (None for adata.X: default)
    inv_covar_keys = inv_covar_keys,
    spur_covar_keys = spur_covar_keys,
    latent_dim_inv = 20, 
    latent_dim_spur = 5,
    device = 'cpu',
    decoder_dist = 'nb'
)
  1. Train the generative model:
    model.train(n_epochs=500, lr_train=0.001, weight_decay=0.0001)
  2. Get the latent representation:
    latent = model.get_latent_representation(adata)
  3. Optional - Train the classifer (for cell types):
model.train_classifier(
    adata_val,
    batch_key = 'batch',
    label_key = 'cell_type',
)
  1. Optional - Predict cell types:
    pred_test = model.predict(adata_test, dataset_type='test')

  2. Optional - Saving and loading model:

model.save('./checkpoints/path.pt')
model.load('./checkpoints/path.pt')

Dependencies

  • scanpy==1.9.3
  • torch==2.0.1
  • tensorboard==2.13.0
  • anndata==0.8.0

Citation

H. Aliee, F. Kapl, S. Hediyeh-Zadeh, F. J. Theis, Conditionally Invariant Representation Learning for Disentangling Cellular Heterogeneity, 2023

invae's People

Contributors

fkapl avatar lila167 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.