forked from TencentAILabHealthcare/scBERT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
127 lines (115 loc) · 4.41 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -*- coding: utf-8 -*-
import os
import gc
import argparse
import json
import random
import math
import random
from functools import reduce
import numpy as np
import pandas as pd
from scipy import sparse
from sklearn.model_selection import train_test_split, ShuffleSplit, StratifiedShuffleSplit, StratifiedKFold
from sklearn.metrics import accuracy_score, f1_score, confusion_matrix, precision_recall_fscore_support, classification_report
import torch
from torch import nn
from torch.optim import Adam, SGD, AdamW
from torch.nn import functional as F
from performer_pytorch import PerformerLM
import scanpy as sc
import anndata as ad
from utils import *
import pickle as pkl
parser = argparse.ArgumentParser()
parser.add_argument("--bin_num", type=int, default=5, help='Number of bins.')
parser.add_argument("--gene_num", type=int, default=16906, help='Number of genes.')
parser.add_argument("--epoch", type=int, default=100, help='Number of epochs.')
parser.add_argument("--seed", type=int, default=2021, help='Random seed.')
parser.add_argument("--novel_type", type=bool, default=False, help='Novel cell tpye exists or not.')
parser.add_argument("--unassign_thres", type=float, default=0.5, help='The confidence score threshold for novel cell type annotation.')
parser.add_argument("--pos_embed", type=bool, default=True, help='Using Gene2vec encoding or not.')
parser.add_argument("--data_path", type=str, default='./data/Zheng68K.h5ad', help='Path of data for predicting.')
parser.add_argument("--model_path", type=str, default='./finetuned.pth', help='Path of finetuned model.')
args = parser.parse_args()
SEED = args.seed
EPOCHS = args.epoch
SEQ_LEN = args.gene_num + 1
UNASSIGN = args.novel_type
UNASSIGN_THRES = args.unassign_thres if UNASSIGN == True else 0
CLASS = args.bin_num + 2
POS_EMBED_USING = args.pos_embed
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Identity(torch.nn.Module):
def __init__(self, dropout = 0., h_dim = 100, out_dim = 10):
super(Identity, self).__init__()
self.conv1 = nn.Conv2d(1, 1, (1, 200))
self.act = nn.ReLU()
self.fc1 = nn.Linear(in_features=SEQ_LEN, out_features=512, bias=True)
self.act1 = nn.ReLU()
self.dropout1 = nn.Dropout(dropout)
self.fc2 = nn.Linear(in_features=512, out_features=h_dim, bias=True)
self.act2 = nn.ReLU()
self.dropout2 = nn.Dropout(dropout)
self.fc3 = nn.Linear(in_features=h_dim, out_features=out_dim, bias=True)
def forward(self, x):
x = x[:,None,:,:]
x = self.conv1(x)
x = self.act(x)
x = x.view(x.shape[0],-1)
x = self.fc1(x)
x = self.act1(x)
x = self.dropout1(x)
x = self.fc2(x)
x = self.act2(x)
x = self.dropout2(x)
x = self.fc3(x)
return x
data = sc.read_h5ad(args.data_path)
#load the label stored during the fine-tune stage
with open('label_dict', 'rb') as fp:
label_dict = pkl.load(fp)
with open('label', 'rb') as fp:
label = pkl.load(fp)
class_num = np.unique(label, return_counts=True)[1].tolist()
class_weight = torch.tensor([(1 - (x / sum(class_num))) ** 2 for x in class_num])
label = torch.from_numpy(label)
data = data.X
model = PerformerLM(
num_tokens = CLASS,
dim = 200,
depth = 6,
max_seq_len = SEQ_LEN,
heads = 10,
local_attn_heads = 0,
g2v_position_emb = True
)
model.to_out = Identity(dropout=0., h_dim=128, out_dim=label_dict.shape[0])
path = args.model_path
ckpt = torch.load(path)
model.load_state_dict(ckpt['model_state_dict'])
for param in model.parameters():
param.requires_grad = False
model = model.to(device)
batch_size = data.shape[0]
model.eval()
pred_finals = []
novel_indices = []
with torch.no_grad():
for index in range(batch_size):
full_seq = data[index].toarray()[0]
full_seq[full_seq > (CLASS - 2)] = CLASS - 2
full_seq = torch.from_numpy(full_seq).long()
full_seq = torch.cat((full_seq, torch.tensor([0]))).to(device)
full_seq = full_seq.unsqueeze(0)
pred_logits = model(full_seq)
softmax = nn.Softmax(dim=-1)
pred_prob = softmax(pred_logits)
pred_final = pred_prob.argmax(dim=-1).item()
if np.amax(np.array(pred_prob.cpu()), axis=-1) < UNASSIGN_THRES:
novel_indices.append(index)
pred_finals.append(pred_final)
pred_list = label_dict[pred_finals].tolist()
for index in novel_indices:
pred_list[index] = 'Unassigned'
print(pred_list)