yulun-rayn / graphVCI

This repository implements Graph Variational Causal Inference (graphVCI), a framework that integrates prior knowledge of relational information into variational causal inference for the prediction of perturbation effect on gene expressions at single-cell and marginal level.
MIT License
15 stars 3 forks source link

How to generate gene expression #5

Closed LIAO-Weny closed 2 months ago

LIAO-Weny commented 2 months ago

Hello~, if i want to use the trained model to predict the gene expressions, which function should i use, and could you kindly give some explanations about the parameters of the function?

Thanks~

yulun-rayn commented 2 months ago

Hi! you can use the predict method or generate method to get counterfactual gene expressions. The difference is: predict uses the mean of latent distribution as input feature representation to the decoder while generate uses a sample from the latent distribution as input feature representation to the decoder.

As for the arguments of these two functions, outcomes, treatments, and covariates are your factual observations, i.e. the observed gene expressions, factually used drug/knockout, and cell type. cf_treatments is the intervention (i.e. alternative drug/knockout) you are interested in.

LIAO-Weny commented 2 months ago

I found two files including these two functions (vci and gvci). Which file should I use and what are the differences between them? Thanks~

yulun-rayn commented 2 months ago

It depends on which model (vci or gvci) you trained, and you should use the methods of the model that you trained. If you used this repo and used GRN, then you trained a gvci model, you can load the model by

import torch
from gvci.model import load_graphVCI

state_dict, args, _ = torch.load(*path/to/saved/model.pt*)
model = load_graphVCI(args, state_dict)

then use model.predict or model.generate. If you trained a vci instead of gvci (you did not use GRN), use load_VCI function (from vci.model import load_VCI) instead of load_graphVCI.

LIAO-Weny commented 2 months ago

If i want to use the data you provided (_marsonprepped.h5ad) , how should I write the predict and generate functions?😊

yulun-rayn commented 2 months ago

I'm not sure I understood your question. You don't need to write predict and generate functions. You can use the prepare function (https://github.com/yulun-rayn/variational-causal-inference/blob/961741b8704cc2db9d91ba8b5a8ae7799b0bf832/vci/train/train.py#L21) to create the model and the dataloader for marson, then simply load a batch from dataloader and feed them to the predict or generate methods of the model:

for data in datasets["loader_tr"]:
    genes, perts, cf_perts, covariates = data[0], data[1], data[3], data[4:]
    model.predict(gene, perts, cf_perts, covariates)
LIAO-Weny commented 2 months ago

I mean use the _marsonprepped.h5ad as new data, and use the model trained before to predict the genes expression. How can I assign the corresponding value of this data to these four values: outcomes, treatments, cf_treatments, covariates~

yulun-rayn commented 2 months ago

Hi, please refer to my response in the previous comment. Thank you!

LIAO-Weny commented 2 months ago

Oh~ I got it ,thank you!

LIAO-Weny commented 1 month ago

A new problem: I find the outputs of _model.predict(gene, perts, cfperts, covariates) does not have the names of genes, how can I get the predicted results of each cell'genes and know the results belongs to which cell'genes~

yulun-rayn commented 1 month ago

It's the same dimensions and same order as your input genes.