-
Notifications
You must be signed in to change notification settings - Fork 1
/
type_run.py
353 lines (246 loc) · 13.6 KB
/
type_run.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from pathlib import Path
import pickle
import torch
from dataset_graph import Dataset, load_processed_dataset
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from configs import EncoderConfig, SearchParams, Type3Config, Type4Config, Type12Config, TypeInput, Type4Input
from graph_models import Type3, Type12, Type4
from trainers import TypeInput, TypeTrainer, Type4Trainer
from utils_funcs import get_A_s, get_variables,get_loaders,set_seeds,results_dict
from anndata import AnnData
import scanpy as sc
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import os
import gc
import numpy as np
# Set the device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def run_type12(dataset_name: str, model_type: str, path: str, params: SearchParams):
data = load_processed_dataset(dataset_name)
n_class = data.y.cpu().view(-1).unique().shape[0]
print(f"Number of unique classes is {n_class}")
x, _, fan_in, _ = get_variables(model_type, path, data)
config = Type12Config(fan_in=fan_in, fan_mid=params.fan_mid, fan_out=n_class, dropout=params.gcn_p)
model = Type12(config, get_A_s(data, path)).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=params.gcn_lr, weight_decay=params.wd)
t_input = TypeInput(x, get_A_s(data, path), data.y.to(device), data.train_ids, data.test_ids, data.valid_ids)
trainer = TypeTrainer(model, optimizer, t_input)
trainer.pipeline(params.max_epochs, params.patience)
return trainer
def run_type3(dataset_name: str, model_type: str, path: str, params: SearchParams):
data = load_processed_dataset(dataset_name)
n_class = data.y.cpu().view(-1).unique().shape[0]
print(f"Number of unique classes is {n_class}")
x, cls_logit, fan_in, _ = get_variables(model_type, path, data)
gcn_config = Type12Config(fan_in=fan_in, fan_mid=params.fan_mid, fan_out=n_class, dropout=params.gcn_p)
config = Type3Config(type12_config=gcn_config, cls_logit=cls_logit, lmbd=params.lmbd)
model = Type3(config, get_A_s(data, path)).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=params.gcn_lr, weight_decay=params.wd)
t_input = TypeInput(x, get_A_s(data, path), data.y.to(device), data.train_ids, data.test_ids, data.valid_ids)
trainer = TypeTrainer(model, optimizer, t_input)
trainer.pipeline(params.max_epochs, params.patience)
return trainer
def run_type4(dataset_name: str, model_type: str, path: str, params: SearchParams):
data = load_processed_dataset(dataset_name)
n_class = data.y.cpu().view(-1).unique().shape[0]
print(f"Number of unique classes is {n_class}")
x, cls_logits , fan_in, update_cls = get_variables(model_type, path, data)
gcn_config = Type12Config(fan_in=fan_in, fan_mid=params.fan_mid, fan_out=n_class, dropout=params.gcn_p)
encoder_config = EncoderConfig(model_name="scgpt", dataset_name=dataset_name, n_class=n_class, CLS=True, dropout=params.encoder_p)
config = Type4Config(type12_config=gcn_config, encoder_config=encoder_config, lmbd=params.lmbd,batch_size=params.batch_size)
model = Type4(config, get_A_s(data, path)).to(device)
loaders= get_loaders(dataset_name, config.batch_size)
optimizer = torch.optim.Adam(
[
{"params": model.encoder.parameters(), "lr": params.encoder_lr},
{"params": model.gcn.parameters(), "lr": params.gcn_lr},
],
weight_decay=params.wd,
)
t_input = Type4Input(x=x, A_s=get_A_s(data, path), loaders=loaders, train_ids=data.train_ids, valid_ids=data.valid_ids, test_ids=data.test_ids,y=data.y)
trainer = Type4Trainer(model, optimizer, t_input, update_cls=update_cls)
trainer.pipeline(params.max_epochs, params.patience)
return trainer
def run_type(dataset_name: str, model_type: str, path: str, params: SearchParams):
if model_type == "type1" or model_type == "type2":
return run_type12(dataset_name, model_type, path, params)
elif model_type == "type3":
return run_type3(dataset_name, model_type, path, params)
elif model_type == "type4":
return run_type4(dataset_name, model_type, path, params)
else:
raise ValueError("Undefined type!")
if __name__=="__main__":
cur_dir= os. getcwd()
save_dir= cur_dir+"/scgnn_merged"
if not os.path.exists(path=save_dir):
save_dir= os.path.join(cur_dir, 'scgnn_merged')
os.makedirs(save_dir)
print(f"Created directory: {save_dir}.")
else:
print(f"{save_dir} is already created.")
dataset_name="ms"
type_name="type3"
path_list= ["GG-CG","GC-CG","CG-CC","CC-CC"]
params = SearchParams(
fan_mid=256,
lmbd=0.7, # default
gcn_lr=0.0001, # Original gcn_lr=0.0001
gcn_p=0.2,
wd=0.00005,
patience=10,
max_epochs=25, #25, 3000 # if batch_size is 32, then use max_epochs as 50
encoder_lr=0.00001,
encoder_p=0.2, batch_size=16 #16
)
##################################################################################
if dataset_name == "ms":
data_dir = Path("../data/ms")
adata = sc.read(data_dir / "c_data.h5ad")
adata_test = sc.read(data_dir / "filtered_ms_adata.h5ad")
adata.obs["celltype"] = adata.obs["Factor Value[inferred cell type - authors labels]"].astype("category")
adata_test.obs["celltype"] = adata_test.obs["Factor Value[inferred cell type - authors labels]"].astype("category")
adata.obs["batch_id"] = adata.obs["str_batch"] = "0"
adata_test.obs["batch_id"] = adata_test.obs["str_batch"] = "1"
adata.var.set_index(adata.var["gene_name"], inplace=True)
adata_test.var.set_index(adata.var["gene_name"], inplace=True)
data_is_raw = False
filter_gene_by_counts = False
adata_test_raw = adata_test.copy()
adata = adata.concatenate(adata_test, batch_key="str_batch")
adata.obs["indices"]= np.arange(adata.obs.shape[0])
if dataset_name == "pancreas": #RB
data_dir = Path("../data/pancreas")
adata = sc.read(data_dir / "demo_train.h5ad")
adata_test = sc.read(data_dir / "demo_test.h5ad")
adata.obs["celltype"] = adata.obs["Celltype"].astype("category")
adata_test.obs["celltype"] = adata_test.obs["Celltype"].astype("category")
adata.obs["batch_id"] = adata.obs["str_batch"] = "0"
adata_test.obs["batch_id"] = adata_test.obs["str_batch"] = "1"
data_is_raw = False
filter_gene_by_counts = False
adata_test_raw = adata_test.copy()
adata = adata.concatenate(adata_test, batch_key="str_batch")
adata.obs["indices"]= np.arange(adata.obs.shape[0])
if dataset_name == "myeloid":
data_dir = Path("../data/mye/")
adata = sc.read(data_dir / "reference_adata.h5ad")
adata_test = sc.read(data_dir / "query_adata.h5ad")
adata.obs["celltype"] = adata.obs["cell_type"].astype("category")
adata_test.obs["celltype"] = adata_test.obs["cell_type"].astype("category")
adata.obs["batch_id"] = adata.obs["str_batch"] = "0"
adata_test.obs["batch_id"] = adata_test.obs["str_batch"] = "1"
adata_test_raw = adata_test.copy()
data_is_raw = False
filter_gene_by_counts = False
adata = adata.concatenate(adata_test, batch_key="str_batch")
adata.obs["indices"]= np.arange(adata.obs.shape[0])
##################################################################################
#### Take results from the save transformer model
file_path = os.path.join(f"/auto/k2/aykut3/scgpt/scGPT/scgpt_gcn/save_scgcn/scgpt_{dataset_name}_median/results.pkl")
with open(file_path, "rb") as file:
results= pickle.load(file)
seed_list=results["seed_numbers"]
##### CREATE DICTIONARY TO SAVE RESULTS
for pt in path_list:
print(pt)
d = results_dict()
d["path"].append(pt)
d["type"].append(type_name)
d["dataset"].append(dataset_name)
for i, seed in enumerate(seed_list):
if seed==15:
seed=0
set_seeds(seed)
trainer=run_type(dataset_name= dataset_name, model_type=type_name, path=pt, params=params)
d["test_acc"].append(100*trainer.metrics["test"]["acc"])
d["test_f1"].append(100*trainer.metrics["test"]["macro"])
d["test_precision"].append(100*trainer.metrics["test"]["precision"])
d["test_recall"].append(100*trainer.metrics["test"]["recall"])
d["avg_epoch_time"].append(trainer.avg_epoch_time)
d["test_preds"].append(trainer.y_test_preds) # these are numpyed values
d["test_true"].append(trainer.y_test_true) # these are numpyed values ( In all of the runs, I mistakenly wrote trainer.y_test_preds here, but do not worry. You can use results["labels"] as trainer.y_test_true
#print( d["test_acc"], d["test_f1"], d["test_recall"], d["test_precision"])
load_dir= save_dir+"/"+f"{dataset_name}/{type_name}/{pt}"
if not os.path.exists(path=load_dir):
load_dir= os.path.join(cur_dir, load_dir)
os.makedirs(load_dir)
print(f"Created directory: {load_dir}.")
else:
print(f"{load_dir} is already created.")
#If you would like to dumb models, comment out here!
result_dir= os.path.join(load_dir, f"dname_{dataset_name}_path_[{pt}]_type_{type_name}_seedid_{str(i)}_seed_{seed}")
# Save dictionary using pickle
with open(result_dir, 'wb') as pickle_file:
pickle.dump(d, pickle_file)
equal = np.array_equal(results["labels"],trainer.y_test_true)
assert equal
# Free up memory
del trainer
torch.cuda.empty_cache()
gc.collect()
"""
# This part will be used for further plotting, it can be used in a different file, no problem at all
id2type=results["id_maps"]
adata_test_raw.obs["predictions"]=[id2type[p] for p in trainer.y_test_preds]
palette_ = plt.rcParams["axes.prop_cycle"].by_key()["color"]
palette_ = plt.rcParams["axes.prop_cycle"].by_key()["color"] + plt.rcParams["axes.prop_cycle"].by_key()["color"] + plt.rcParams["axes.prop_cycle"].by_key()["color"]
palette_ = {c: palette_[i] for i, c in enumerate(adata.obs["celltype"].unique())}
#print(adata_test_raw.to_df())
with plt.rc_context({"figure.figsize": (2,2), "figure.dpi": (300),"axes.labelsize": 8, "axes.linewidth": 0.75}):
sc.pl.umap(
adata_test_raw,
color="celltype",
palette=palette_,
show=False,
legend_fontsize=3,
legend_loc="on data",
size=3,
title=""
)
if dataset_name=="ms":
fig_label="MS"
else:
fig_label=dataset_name.capitalize()
plt.xlabel("Annotated")
plt.ylabel(fig_label)
plt.savefig("results_annotated.png", dpi=300)
with plt.rc_context({"figure.figsize": (2,2), "figure.dpi": (300),"axes.labelsize": 8, "axes.linewidth": 0.75}):
sc.pl.umap(
adata_test_raw,
color="predictions",
palette=palette_,
show=False,
legend_fontsize=3,
legend_loc="on data",
size=3,
title=""
)
if dataset_name=="ms":
fig_label="MS"
else:
fig_label=dataset_name.capitalize()
plt.xlabel("Predicted")
plt.ylabel(fig_label)
plt.savefig("results_predicted.png", dpi=300)
with plt.rc_context({"figure.figsize": (2,2), "figure.dpi": (300),"axes.labelsize": 8, "axes.linewidth": 0.75}):
sc.pl.umap(
adata_test_raw,
color="celltype",
palette=palette_,
show=False,
legend_fontsize=1,
legend_loc=None,
size=3,
title=""
)
if dataset_name=="ms":
fig_label="MS"
else:
fig_label=dataset_name.capitalize()
plt.xlabel("Annotated")
plt.ylabel(fig_label)
plt.savefig("results_annotated.png", dpi=300)
"""