-
Notifications
You must be signed in to change notification settings - Fork 0
/
CIForm_AnnData.py
291 lines (232 loc) · 8.71 KB
/
CIForm_AnnData.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import torch
import torch.nn as nn
import warnings
warnings.filterwarnings('ignore')
import math
import pandas as pd
import scanpy as sc
import os
from tqdm.auto import tqdm
from sklearn.metrics import accuracy_score ,f1_score ,recall_score ,precision_score
from torch.utils.data import (DataLoader ,Dataset)
torch.set_default_tensor_type(torch.DoubleTensor)
import numpy as np
import random
from sklearn import preprocessing
def same_seeds(seed):
random.seed(seed)
# Numpy
np.random.seed(seed)
# Torch
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
same_seeds(2021)
def getXY(gap, adata, y_trains):
X = adata.X
if not isinstance(X, np.ndarray):
X = X.todense()
X = np.asarray(X)
single_cell_list = []
for single_cell in X:
feature = []
length = len(single_cell)
for k in range(0, length, gap):
if (k + gap <= length):
a = single_cell[k:k + gap]
else:
a = single_cell[length - gap:length]
a = preprocessing.scale(a)
feature.append(a)
feature = np.asarray(feature)
single_cell_list.append(feature)
single_cell_list = np.asarray(single_cell_list)
cell_types = []
if(len(y_trains) > 0):
for i in y_trains:
i = str(i).upper()
if (not cell_types.__contains__(i)):
cell_types.append(i)
return single_cell_list, y_trains, cell_types
else:
return single_cell_list
def getNewData(cells, cell_types):
labels = []
for i in range(len(cells)):
cell = cells[i]
cell = str(cell).upper()
if (cell_types.__contains__(cell)):
indexs = cell_types.index(cell)
labels.append(indexs + 1)
else:
labels.append(0) # 0 denotes the unknowns cell types
return np.asarray(labels)
class TrainDataSet(Dataset):
def __init__(self, data, label):
self.data = data
self.label = label
self.length = len(data)
def __len__(self):
return self.length
def __getitem__(self, index):
data = torch.from_numpy(self.data)
label = torch.from_numpy(self.label)
return data[index], label[index]
class TestDataSet(Dataset):
def __init__(self, data):
self.data = data
self.length = len(data)
def __len__(self):
return self.length
def __getitem__(self, index):
data = torch.from_numpy(self.data)
return data[index]
class PositionalEncoding(nn.Module):
def __init__(self, d_model, dropout=0.1, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0).transpose(0, 1)
self.register_buffer('pe', pe)
def forward(self, x):
x = x + self.pe[:x.size(0), :]
return self.dropout(x)
class CIForm(nn.Module):
def __init__(self, input_dim, nhead=2, d_model=80, num_classes=2, dropout=0.1):
super().__init__()
self.encoder_layer = nn.TransformerEncoderLayer(
d_model=d_model, dim_feedforward=1024, nhead=nhead, dropout=dropout
)
self.positionalEncoding = PositionalEncoding(d_model=d_model, dropout=dropout)
self.pred_layer = nn.Sequential(
nn.Linear(d_model, d_model),
nn.ReLU(),
nn.Linear(d_model, num_classes)
)
def forward(self, mels):
out = mels.permute(1, 0, 2)
out = self.positionalEncoding(out)
out = self.encoder_layer(out)
out = out.transpose(0, 1)
out = out.mean(dim=1)
out = self.pred_layer(out)
return out
def ciForm(s ,Train_adata ,train_labels ,Test_adata,n_epochs=20):
gap = s
d_models = s
heads = 64
lr = 0.0001
dp = 0.1
batch_sizes = 256
n_epochs = n_epochs
train_data, train_cells, train_cellTypes = getXY(gap,Train_adata,train_labels)
print("train_cellTypes",train_cellTypes)
cell_types = train_cellTypes
Train_labels = getNewData(train_cells, cell_types)
labels = Train_labels
cell_types = np.unique(train_labels)
num_classes = len(cell_types) + 1
query_data = getXY(gap, Test_adata, [])
model = CIForm(input_dim=d_models, nhead=heads, d_model=d_models,
num_classes=num_classes ,dropout=dp)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=1e-5)
train_dataset = TrainDataSet(data=train_data, label=labels)
train_loader = DataLoader(train_dataset, batch_size=batch_sizes, shuffle=True,
pin_memory=True)
test_dataset = TestDataSet(data=query_data)
test_loader = DataLoader(test_dataset, batch_size=batch_sizes, shuffle=False,
pin_memory=True)
new_cellTypes = []
new_cellTypes.append("unassigned")
new_cellTypes.extend(cell_types)
model.train()
for epoch in range(n_epochs):
# model.train()
# These are used to record information in training.
train_loss = []
train_accs = []
train_f1s = []
for batch in tqdm(train_loader):
# A batch consists of image data and corresponding labels.
data, labels = batch
logits = model(data)
labels = torch.tensor(labels, dtype=torch.long)
loss = criterion(logits, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
preds = logits.argmax(1)
preds = preds.cpu().numpy()
labels = labels.cpu().numpy()
acc = accuracy_score(labels, preds)
f1 = f1_score(labels ,preds ,average='macro')
train_loss.append(loss.item())
train_accs.append(acc)
train_f1s.append(f1)
train_loss = sum(train_loss) / len(train_loss)
train_acc = sum(train_accs) / len(train_accs)
train_f1 = sum(train_f1s) / len(train_f1s)
print \
(f"[ Train | {epoch + 1:03d}/{n_epochs:03d} ] loss = {train_loss:.5f}, acc = {train_acc:.5f}, f1 = {train_f1:.5f}")
model.eval()
y_predict = []
for batch in tqdm(test_loader):
data = batch
with torch.no_grad():
logits = model(data)
preds = logits.argmax(1)
preds = preds.cpu().numpy().tolist()
y_predict.extend(preds)
return y_predicts
s = 1024
import os
import time as tm
from sklearn.metrics import *
from sklearn.model_selection import train_test_split
tissues = "Root"
path = "../../Datasets/Arabidopsis thaliana/" + tissues + "/"
data_name = "03SRP330542"
test_paths = [path]
test_names = [data_name + ".h5ad"]
topgenes = 2000
adata_mod1 = sc.read_h5ad(test_paths[0] + test_names[0])
adata_mod1.var_names_make_unique()
adata_mod1.obs['domain_id'] = 0
sc.pp.highly_variable_genes(adata_mod1, n_top_genes=topgenes)
adata_mod1 = adata_mod1[:, adata_mod1.var['highly_variable']]
X = adata_mod1.X # obsm['X_pca']#.todense()
if not isinstance(X, np.ndarray):
X = X.todense()
X = np.asarray(X)
print("X.shape", X.shape)
Y = adata_mod1.obs['Celltype'].to_numpy()
print("Y.shape", Y.shape)
test_size = 0.99
ref_adata, query_adata, y_train, y_test = train_test_split(adata_mod1, Y, test_size=test_size, random_state=2024)
print("ref_adata",ref_adata)
print("query_adata",query_adata)
start = tm.time()
preds = ciForm(s ,ref_adata ,y_train ,query_adata,n_epochs=50)
end = tm.time()
all_time = end - start
acc = accuracy_score(y_test, preds)
f1 = f1_score(y_test, preds, average='macro')
print("acc", acc, "f1", f1)
log_txt = "log/Intra_split/"+ tissues + "/" + data_name + "/"
last_path = log_txt + "test_size=" + str(test_size)
if (not os.path.isdir(log_txt)):
os.makedirs(log_txt)
np.save(last_path + "_pred", preds)
with open(log_txt + "end_test.txt", "a") as f:
f.writelines("log_dir:" + last_path + "\n")
f.writelines("acc:" + str(acc) + "\n")
f.writelines("f1:" + str(f1) + "\n")
f.writelines("all_time:" + str(all_time) + "\n")