forked from koc-lab/scGraPhT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinetune_annotation_v0.py
1301 lines (1054 loc) · 45.4 KB
/
finetune_annotation_v0.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# %%
import copy
import gc
import json
import os
from pathlib import Path
import shutil
import sys
import time
import traceback
from typing import List, Tuple, Dict, Union, Optional
import warnings
import pandas as pd
# from . import asyn
import pickle
import torch
from anndata import AnnData
import scanpy as sc
import seaborn as sns
import numpy as np
from scipy.sparse import issparse
import matplotlib.pyplot as plt
from torch import nn
from torch.nn import functional as F
from torch.utils.data import Dataset, DataLoader
from sklearn.model_selection import train_test_split
from sklearn.metrics import adjusted_rand_score, normalized_mutual_info_score
from torchtext.vocab import Vocab
from torchtext._torchtext import (
Vocab as VocabPybind,
)
from sklearn.metrics import confusion_matrix
sys.path.insert(0, "../")
import scgpt as scg
from scgpt.model.model_default import TransformerModel, AdversarialDiscriminator
from scgpt.tokenizer import tokenize_and_pad_batch, random_mask_value
from scgpt.loss import (
masked_mse_loss,
masked_relative_error,
criterion_neg_log_bernoulli,
)
from scgpt.tokenizer.gene_tokenizer import GeneVocab
from scgpt.preprocess import Preprocessor
from scgpt import SubsetsBatchSampler
from scgpt.utils import set_seed, category_str2int, eval_scib_metrics
import random
sc.set_figure_params(figsize=(6, 6))
os.environ["KMP_WARNINGS"] = "off"
warnings.filterwarnings('ignore')
random.seed(20)
#$torch.cuda.empty_cache()
# ## Step1: Specify hyper-parameter setup for cell-type annotation task
# Listed below are some hyper-parameter recommendations for the cell-type task. Note that the CLS objective is on to facilitate cell-type classification.
print("Imported all the required files")
# Set the device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
torch.cuda.set_device(0)
hyperparameter_defaults = dict(
seed=0,
dataset_name="pancreas", #changed dataset here, originally "ms"
do_train=True,
load_model="../save/scGPT_human",
mask_ratio=0.0,
epochs=1,
n_bins=51,
MVC=False, # Masked value prediction for cell embedding
ecs_thres=0.0, # Elastic cell similarity objective, 0.0 to 1.0, 0.0 to disable
dab_weight=0.0,
lr=1e-4,
batch_size=64,
layer_size=128,
nlayers=12, # number of nn.TransformerEncoderLayer in nn.TransformerEncoder
nhead=8, # number of heads in nn.MultiheadAttention
dropout=0.2, # dropout probability
schedule_ratio=0.9, # ratio of epochs for learning rate schedule
save_eval_interval=5,
fast_transformer=True,
pre_norm=False,
amp=True, # Automatic Mixed Precision
include_zero_gene = True, #False, ##WAS FALSE ORIGINALLY
freeze = False, #freeze
DSBN = False, # Domain-spec batchnorm
)
print("Read and print the hyperparameters.")
# In[ ]:
config = hyperparameter_defaults
print(config)
from operator import attrgetter
class DotDict(dict):
def __getattr__(self, attr):
return self[attr]
# Example usage
config = DotDict(hyperparameter_defaults)
print(config.seed) # Accessing the 'seed' key using dot notation
set_seed(config.seed)
#####################################
# Check if CUDA is available
print("Is CUDA available:", torch.cuda.is_available())
# Print CUDA version
print("CUDA version:", torch.version.cuda)
# Initialize CUDA
if torch.cuda.is_available():
torch.cuda.init()
try:
device_props = torch.cuda.get_device_properties(device)
except Exception as e:
print(f"CUDA error: {e}")
# settings for input and preprocessing
pad_token = "<pad>"
special_tokens = [pad_token, "<cls>", "<eoc>"]
mask_ratio = config.mask_ratio
mask_value = "auto" # for masked values, now it should always be auto
include_zero_gene = config.include_zero_gene # if True, include zero genes among hvgs in the training
max_seq_len = 3000
n_bins = config.n_bins
# input/output representation
input_style = "binned" # "normed_raw", "log1p", or "binned"
output_style = "binned" # "normed_raw", "log1p", or "binned"
# settings for training
MLM = False # whether to use masked language modeling, currently it is always on.
CLS = True # celltype classification objective
ADV = False # Adversarial training for batch correction
CCE = False # Contrastive cell embedding objective
MVC = config.MVC # Masked value prediction for cell embedding
ECS = config.ecs_thres > 0 # Elastic cell similarity objective
DAB = False # Domain adaptation by reverse backpropagation, set to 2 for separate optimizer
INPUT_BATCH_LABELS = False # TODO: have these help MLM and MVC, while not to classifier
input_emb_style = "continuous" # "category" or "continuous" or "scaling"
cell_emb_style = "cls" # "avg-pool" or "w-pool" or "cls"
adv_E_delay_epochs = 0 # delay adversarial training on encoder for a few epochs
adv_D_delay_epochs = 0
mvc_decoder_style = "inner product"
ecs_threshold = config.ecs_thres
dab_weight = config.dab_weight
explicit_zero_prob = MLM and include_zero_gene # whether explicit bernoulli for zeros
do_sample_in_train = False and explicit_zero_prob # sample the bernoulli in training
per_seq_batch_sample = False
# settings for optimizer
lr = config.lr # TODO: test learning rate ratio between two tasks
lr_ADV = 1e-3 # learning rate for discriminator, used when ADV is True
batch_size = config.batch_size
eval_batch_size = config.batch_size
epochs = config.epochs
schedule_interval = 1
# settings for the model
fast_transformer = config.fast_transformer
fast_transformer_backend = "flash" # "linear" or "flash"
embsize = config.layer_size # embedding dimension
d_hid = config.layer_size # dimension of the feedforward network in TransformerEncoder
nlayers = config.nlayers # number of TransformerEncoderLayer in TransformerEncoder
nhead = config.nhead # number of heads in nn.MultiheadAttention
dropout = config.dropout # dropout probability
# logging
log_interval = 100 # iterations
save_eval_interval = config.save_eval_interval # epochs
do_eval_scib_metrics = True
# In[ ]:
# %% validate settings
assert input_style in ["normed_raw", "log1p", "binned"]
assert output_style in ["normed_raw", "log1p", "binned"]
assert input_emb_style in ["category", "continuous", "scaling"]
if input_style == "binned":
if input_emb_style == "scaling":
raise ValueError("input_emb_style `scaling` is not supported for binned input.")
elif input_style == "log1p" or input_style == "normed_raw":
if input_emb_style == "category":
raise ValueError(
"input_emb_style `category` is not supported for log1p or normed_raw input."
)
if input_emb_style == "category":
mask_value = n_bins + 1
pad_value = n_bins # for padding gene expr values
n_input_bins = n_bins + 2
else:
mask_value = -1
pad_value = -2
n_input_bins = n_bins
if ADV and DAB:
raise ValueError("ADV and DAB cannot be both True.")
DAB_separate_optim = True if DAB > 1 else False
# In[ ]:
dataset_name = config.dataset_name
save_dir = Path(f"./save/dev_{dataset_name}-{time.strftime('%b%d-%H-%M')}/")
save_dir.mkdir(parents=True, exist_ok=True)
print(f"save to {save_dir}")
logger = scg.logger
scg.utils.add_file_handler(logger, save_dir / "run.log")
# ## Step 2: Load and pre-process data
# We follow the standard scGPT data pre-processing pipelines for the cell-type annotation task. Note that since now we have two datasets at hand (i.e., reference and query data), the same pre-prpocessing steps need to be applied to both of them.
# In[ ]:
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 == "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])
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])
# make the batch category column
batch_id_labels = adata.obs["str_batch"].astype("category").cat.codes.values
adata.obs["batch_id"] = batch_id_labels
celltype_id_labels = adata.obs["celltype"].astype("category").cat.codes.values
celltypes = adata.obs["celltype"].unique()
num_types = len(np.unique(celltype_id_labels))
print(f"Number of unique celltypes:{num_types}")
id2type = dict(enumerate(adata.obs["celltype"].astype("category").cat.categories))
adata.obs["celltype_id"] = celltype_id_labels
adata.var["gene_name"] = adata.var.index.tolist()
# In[ ]:
if config.load_model is not None:
model_dir = Path(config.load_model)
model_config_file = model_dir / "args.json"
model_file = model_dir / "best_model.pt"
vocab_file = model_dir / "vocab.json"
vocab = GeneVocab.from_file(vocab_file)
shutil.copy(vocab_file, save_dir / "vocab.json")
for s in special_tokens:
if s not in vocab:
vocab.append_token(s)
adata.var["id_in_vocab"] = [
1 if gene in vocab else -1 for gene in adata.var["gene_name"]
]
gene_ids_in_vocab = np.array(adata.var["id_in_vocab"])
logger.info(
f"match {np.sum(gene_ids_in_vocab >= 0)}/{len(gene_ids_in_vocab)} genes "
f"in vocabulary of size {len(vocab)}."
)
adata = adata[:, adata.var["id_in_vocab"] >= 0]
# model
with open(model_config_file, "r") as f:
model_configs = json.load(f)
logger.info(
f"Resume model from {model_file}, the model args will override the "
f"config {model_config_file}."
)
embsize = model_configs["embsize"]
nhead = model_configs["nheads"]
d_hid = model_configs["d_hid"]
nlayers = model_configs["nlayers"]
n_layers_cls = model_configs["n_layers_cls"]
# In[ ]:
# set up the preprocessor, use the args to config the workflow
preprocessor = Preprocessor(
use_key="X", # the key in adata.layers to use as raw data
filter_gene_by_counts=filter_gene_by_counts, # step 1
filter_cell_by_counts=False, # step 2
normalize_total=1e4, # 3. whether to normalize the raw data and to what sum
result_normed_key="X_normed", # the key in adata.layers to store the normalized data
log1p=data_is_raw, # 4. whether to log1p the normalized data
result_log1p_key="X_log1p",
subset_hvg=False, # 5. whether to subset the raw data to highly variable genes
hvg_flavor="seurat_v3" if data_is_raw else "cell_ranger",
binning=n_bins, # 6. whether to bin the raw data and to what number of bins
result_binned_key="X_binned", # the key in adata.layers to store the binned data
)
adata_test = adata[adata.obs["str_batch"] == "1"]
adata = adata[adata.obs["str_batch"] == "0"]
preprocessor(adata, batch_key=None)
preprocessor(adata_test, batch_key=None)
##############################################################
import anndata
merged_ann = anndata.concat([adata, adata_test], axis=0)
cur_dir = os.getcwd()
expr_dir= cur_dir+"/processed_data"
# Check if the directory exists
if not os.path.exists(expr_dir):
# Create the directory if it doesn't exist
os.makedirs(expr_dir)
print(f"Directory '{expr_dir}' created.")
else:
print(f"Directory '{expr_dir}' already exists.")
# If the AnnData objects have different variable names, you may need to adjust them
# merged_ann.var_names_make_unique(
processed_dataname= dataset_name+".h5ad"
import anndata
merged_ann = anndata.concat([adata, adata_test], axis=0)
# If the AnnData objects have different variable names, you may need to adjust them
# merged_ann.var_names_make_unique(
merged_ann.write_h5ad(expr_dir+"/"+processed_dataname)
##############################################################
# In[ ]:
input_layer_key = { # the values of this map coorespond to the keys in preprocessing
"normed_raw": "X_normed",
"log1p": "X_normed",
"binned": "X_binned",
}[input_style]
all_counts = (
adata.layers[input_layer_key].A
if issparse(adata.layers[input_layer_key])
else adata.layers[input_layer_key]
)
genes = adata.var["gene_name"].tolist()
celltypes_labels = adata.obs["celltype_id"].tolist() # make sure count from 0
celltypes_labels = np.array(celltypes_labels)
batch_ids = adata.obs["batch_id"].tolist()
num_batch_types = len(set(batch_ids))
batch_ids = np.array(batch_ids)
train_valid_cell_indices= np.array(adata.obs["indices"].tolist())
test_indices= torch.tensor(np.array(merged_ann.obs["indices"][max(train_valid_cell_indices)+1:len(merged_ann.obs["indices"])]))
(
train_data,
valid_data,
train_celltype_labels,
valid_celltype_labels,
train_batch_labels,
valid_batch_labels,
train_indices,
valid_indices
) = train_test_split(
all_counts, celltypes_labels, batch_ids, train_valid_cell_indices, test_size=0.1, shuffle=True, random_state=random.randint(0, 42)
)
# Save the tuple of arrays to a .npz file ( train and validation indices to save)
np.savez(save_dir / 'indices.npz', tr_indices=train_indices, val_indices=valid_indices)
# In[ ]:
if config.load_model is None:
vocab = Vocab(
VocabPybind(genes + special_tokens, None)
) # bidirectional lookup [gene <-> int]
vocab.set_default_index(vocab["<pad>"])
gene_ids = np.array(vocab(genes), dtype=int)
# In[ ]:
tokenized_train = tokenize_and_pad_batch(
train_data,
gene_ids,
max_len=max_seq_len,
vocab=vocab,
pad_token=pad_token,
pad_value=pad_value,
append_cls=True, # append <cls> token at the beginning
include_zero_gene=include_zero_gene,)
tokenized_valid = tokenize_and_pad_batch(
valid_data,
gene_ids,
max_len=max_seq_len,
vocab=vocab,
pad_token=pad_token,
pad_value=pad_value,
append_cls=True,
include_zero_gene=include_zero_gene,
)
logger.info(
f"train set number of samples: {tokenized_train['genes'].shape[0]}, "
f"\n\t feature length: {tokenized_train['genes'].shape[1]}"
)
logger.info(
f"valid set number of samples: {tokenized_valid['genes'].shape[0]}, "
f"\n\t feature length: {tokenized_valid['genes'].shape[1]}"
)
def prepare_data(sort_seq_batch=False) -> Tuple[Dict[str, torch.Tensor]]:
masked_values_train = random_mask_value(
tokenized_train["values"],
mask_ratio=mask_ratio,
mask_value=mask_value,
pad_value=pad_value,
)
masked_values_valid = random_mask_value(
tokenized_valid["values"],
mask_ratio=mask_ratio,
mask_value=mask_value,
pad_value=pad_value,
)
print(
f"random masking at epoch {epoch:3d}, ratio of masked values in train: ",
f"{(masked_values_train == mask_value).sum() / (masked_values_train - pad_value).count_nonzero():.4f}",
)
input_gene_ids_train, input_gene_ids_valid = (
tokenized_train["genes"],
tokenized_valid["genes"],
)
input_values_train, input_values_valid = masked_values_train, masked_values_valid
target_values_train, target_values_valid = (
tokenized_train["values"],
tokenized_valid["values"],
)
tensor_batch_labels_train = torch.from_numpy(train_batch_labels).long()
tensor_batch_labels_valid = torch.from_numpy(valid_batch_labels).long()
tensor_celltype_labels_train = torch.from_numpy(train_celltype_labels).long()
tensor_celltype_labels_valid = torch.from_numpy(valid_celltype_labels).long()
if sort_seq_batch: # TODO: update to random pick seq source in each traning batch
train_sort_ids = np.argsort(train_batch_labels)
input_gene_ids_train = input_gene_ids_train[train_sort_ids]
input_values_train = input_values_train[train_sort_ids]
target_values_train = target_values_train[train_sort_ids]
tensor_batch_labels_train = tensor_batch_labels_train[train_sort_ids]
tensor_celltype_labels_train = tensor_celltype_labels_train[train_sort_ids]
valid_sort_ids = np.argsort(valid_batch_labels)
input_gene_ids_valid = input_gene_ids_valid[valid_sort_ids]
input_values_valid = input_values_valid[valid_sort_ids]
target_values_valid = target_values_valid[valid_sort_ids]
tensor_batch_labels_valid = tensor_batch_labels_valid[valid_sort_ids]
tensor_celltype_labels_valid = tensor_celltype_labels_valid[valid_sort_ids]
train_data_pt = {
"gene_ids": input_gene_ids_train,
"values": input_values_train,
"target_values": target_values_train,
"batch_labels": tensor_batch_labels_train,
"celltype_labels": tensor_celltype_labels_train,
"train_indices":torch.tensor(train_indices) # We dont use them
}
valid_data_pt = {
"gene_ids": input_gene_ids_valid,
"values": input_values_valid,
"target_values": target_values_valid,
"batch_labels": tensor_batch_labels_valid,
"celltype_labels": tensor_celltype_labels_valid,
"valid_indices":torch.tensor(valid_indices) # We dont use them
}
return train_data_pt, valid_data_pt
# dataset
class SeqDataset(Dataset):
def __init__(self, data: Dict[str, torch.Tensor]):
self.data = data
def __len__(self):
return self.data["gene_ids"].shape[0]
def __getitem__(self, idx):
return {k: v[idx] for k, v in self.data.items()}
# data_loader
def prepare_dataloader(
data_pt: Dict[str, torch.Tensor],
batch_size: int,
shuffle: bool = False,
intra_domain_shuffle: bool = False,
drop_last: bool = False,
num_workers: int = 0,
) -> DataLoader:
if num_workers == 0:
num_workers = min(len(os.sched_getaffinity(0)), batch_size // 2)
dataset = SeqDataset(data_pt)
if per_seq_batch_sample: # I dont use it in annotation tasks
# find the indices of samples in each seq batch
subsets = []
batch_labels_array = data_pt["batch_labels"].numpy()
for batch_label in np.unique(batch_labels_array):
batch_indices = np.where(batch_labels_array == batch_label)[0].tolist()
subsets.append(batch_indices)
data_loader = DataLoader(
dataset=dataset,
batch_sampler=SubsetsBatchSampler(
subsets,
batch_size,
intra_subset_shuffle=intra_domain_shuffle,
inter_subset_shuffle=shuffle,
drop_last=drop_last,
),
num_workers=num_workers,
pin_memory=True,
)
return data_loader
data_loader = DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=shuffle,
drop_last=drop_last,
num_workers=num_workers,
pin_memory=True,
)
return data_loader
# ## Step 3: Load the pre-trained scGPT model
# In[ ]:
ntokens = len(vocab) # size of vocabulary
model = TransformerModel(
ntokens,
embsize, #EMBSIZE ORIGINALLY
nhead,
d_hid,
nlayers,
nlayers_cls=3,
n_cls=num_types if CLS else 1,
vocab=vocab,
dropout=dropout,
pad_token=pad_token,
pad_value=pad_value,
do_mvc=MVC,
do_dab=DAB,
use_batch_labels=INPUT_BATCH_LABELS,
num_batch_labels=num_batch_types,
domain_spec_batchnorm=config.DSBN,
input_emb_style=input_emb_style,
n_input_bins=n_input_bins,
cell_emb_style=cell_emb_style,
mvc_decoder_style=mvc_decoder_style,
ecs_threshold=ecs_threshold,
explicit_zero_prob=explicit_zero_prob,
use_fast_transformer=fast_transformer,
fast_transformer_backend=fast_transformer_backend,
pre_norm=config.pre_norm,
)
print(model)
# Save the model here, below you will save the state dict of best model
torch.save(model,save_dir / f"{dataset_name}_model.pt")
if config.load_model is not None:
try:
model.load_state_dict(torch.load(model_file))
logger.info(f"Loading all model params from {model_file}")
except:
# only load params that are in the model and match the size
model_dict = model.state_dict()
pretrained_dict = torch.load(model_file)
pretrained_dict = {
k: v
for k, v in pretrained_dict.items()
if k in model_dict and v.shape == model_dict[k].shape
}
for k, v in pretrained_dict.items():
logger.info(f"Loading params {k} with shape {v.shape}")
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
pre_freeze_param_count = sum(dict((p.data_ptr(), p.numel()) for p in model.parameters() if p.requires_grad).values())
# Freeze all pre-decoder weights
for name, para in model.named_parameters():
print("-"*20)
print(f"name: {name}")
print(para.dtype)
if config.freeze and "encoder" in name and "transformer_encoder" not in name:
# if config.freeze and "encoder" in name:
print(f"freezing weights for: {name}")
para.requires_grad = False
post_freeze_param_count = sum(dict((p.data_ptr(), p.numel()) for p in model.parameters() if p.requires_grad).values())
logger.info(f"Total Pre freeze Params {(pre_freeze_param_count )}")
logger.info(f"Total Post freeze Params {(post_freeze_param_count )}")
model.to(device)
if ADV:
discriminator = AdversarialDiscriminator(
d_model=embsize,
n_cls=num_batch_types,
).to(device)
# In[ ]:
criterion = masked_mse_loss
criterion_cls = nn.CrossEntropyLoss()
criterion_dab = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(
model.parameters(), lr=lr, eps=1e-4 if config.amp else 1e-8
)
scheduler = torch.optim.lr_scheduler.StepLR(
optimizer, schedule_interval, gamma=config.schedule_ratio
)
if DAB_separate_optim:
optimizer_dab = torch.optim.Adam(model.parameters(), lr=lr)
scheduler_dab = torch.optim.lr_scheduler.StepLR(
optimizer_dab, schedule_interval, gamma=config.schedule_ratio
)
if ADV:
criterion_adv = nn.CrossEntropyLoss() # consider using label smoothing
optimizer_E = torch.optim.Adam(model.parameters(), lr=lr_ADV)
scheduler_E = torch.optim.lr_scheduler.StepLR(
optimizer_E, schedule_interval, gamma=config.schedule_ratio
)
optimizer_D = torch.optim.Adam(discriminator.parameters(), lr=lr_ADV)
scheduler_D = torch.optim.lr_scheduler.StepLR(
optimizer_D, schedule_interval, gamma=config.schedule_ratio
)
scaler = torch.cuda.amp.GradScaler(enabled=config.amp)
# In[ ]:
cell_emb_dict= {"train":[],"valid":[],"test":[]}
cls_logit_dict= {"train":[],"valid":[],"test":[]}
def train(model: nn.Module, loader: DataLoader) -> None:
"""
Train the model for one epoch.
"""
cell_emb_dict["train"]=[]
cls_logit_dict["train"]=[]
model.train()
(
total_loss,
total_mse,
total_cls,
total_cce,
total_mvc,
total_ecs,
total_dab,
total_adv_E,
total_adv_D,
total_zero_log_prob,
total_mvc_zero_log_prob,
) = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
total_error = 0.0
start_time = time.time()
### CELL EMBEDDINGS
num_batches = len(loader)
for batch, batch_data in enumerate(loader):
input_gene_ids = batch_data["gene_ids"].to(device)
input_values = batch_data["values"].to(device)
target_values = batch_data["target_values"].to(device)
batch_labels = batch_data["batch_labels"].to(device)
celltype_labels = batch_data["celltype_labels"].to(device)
src_key_padding_mask = input_gene_ids.eq(vocab[pad_token])
with torch.cuda.amp.autocast(enabled=config.amp):
output_dict = model(
input_gene_ids,
input_values,
src_key_padding_mask=src_key_padding_mask,
batch_labels=batch_labels if INPUT_BATCH_LABELS or config.DSBN else None,
CLS=CLS,
CCE=CCE,
MVC=MVC,
ECS=ECS,
do_sample=do_sample_in_train,
#generative_training=False
)
masked_positions = input_values.eq(mask_value) # the postions to predict
loss = 0.0
metrics_to_log = {}
if MLM:
loss_mse = criterion(
output_dict["mlm_output"], target_values, masked_positions
)
loss = loss + loss_mse
metrics_to_log = {"train/mse": loss_mse.item()}
if explicit_zero_prob:
loss_zero_log_prob = criterion_neg_log_bernoulli(
output_dict["mlm_zero_probs"], target_values, masked_positions
)
loss = loss + loss_zero_log_prob
metrics_to_log.update({"train/nzlp": loss_zero_log_prob.item()})
if CLS:
loss_cls = criterion_cls(output_dict["cls_output"], celltype_labels)
loss = loss + loss_cls
metrics_to_log.update({"train/cls": loss_cls.item()})
######## EMBEDDING AND LOGITS ARE HERE ##########3
cell_emb_dict["train"].append(output_dict["cell_emb"].detach().cpu())
cls_logit_dict["train"].append(output_dict["cls_output"].detach().cpu())
######## EMBEDDING AND LOGITS ARE HERE ##########3
error_rate = 1 - (
(output_dict["cls_output"].argmax(1) == celltype_labels)
.sum()
.item()
) / celltype_labels.size(0)
if CCE:
loss_cce = 10 * output_dict["loss_cce"]
loss = loss + loss_cce
metrics_to_log.update({"train/cce": loss_cce.item()})
if MVC:
loss_mvc = criterion(
output_dict["mvc_output"], target_values, masked_positions
)
loss = loss + loss_mvc
metrics_to_log.update({"train/mvc": loss_mvc.item()})
if MVC and explicit_zero_prob:
loss_mvc_zero_log_prob = criterion_neg_log_bernoulli(
output_dict["mvc_zero_probs"], target_values, masked_positions
)
loss = loss + loss_mvc_zero_log_prob
metrics_to_log.update({"train/mvc_nzlp": loss_mvc_zero_log_prob.item()})
if ECS:
loss_ecs = 10 * output_dict["loss_ecs"]
loss = loss + loss_ecs
metrics_to_log.update({"train/ecs": loss_ecs.item()})
if DAB:
# try weighting and separate optimizer
loss_dab = criterion_dab(output_dict["dab_output"], batch_labels)
loss = loss + dab_weight * loss_dab
metrics_to_log.update({"train/dab": loss_dab.item()})
model.zero_grad()
scaler.scale(loss).backward()
scaler.unscale_(optimizer)
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings("always")
torch.nn.utils.clip_grad_norm_(
model.parameters(),
1.0,
error_if_nonfinite=False if scaler.is_enabled() else True,
)
if len(w) > 0:
logger.warning(
f"Found infinite gradient. This may be caused by the gradient "
f"scaler. The current scale is {scaler.get_scale()}. This warning "
"can be ignored if no longer occurs after autoscaling of the scaler."
)
scaler.step(optimizer)
scaler.update()
if ADV:
# rerun the model for adversarial training
output_dict = model(
input_gene_ids,
input_values,
src_key_padding_mask=src_key_padding_mask,
batch_labels=batch_labels if INPUT_BATCH_LABELS or config.DSBN else None,
CLS=CLS,
CCE=CCE,
MVC=MVC,
ECS=ECS,
do_sample=do_sample_in_train,
#generative_training=False
)
# TRAINING DISCRIMINATOR
loss_adv_D = criterion_adv(
discriminator(output_dict["cell_emb"].detach()), batch_labels
)
if epoch > adv_D_delay_epochs:
discriminator.zero_grad()
loss_adv_D.backward()
optimizer_D.step()
# TRAINING ENCODER
loss_adv_E = -criterion_adv(
discriminator(output_dict["cell_emb"]), batch_labels
)
# NOTE: the loss is negative here because we want to maximize
# the cross_entropy_loss, in other words, disguise against the discriminator
if epoch > adv_E_delay_epochs:
model.zero_grad()
discriminator.zero_grad()
loss_adv_E.backward()
optimizer_E.step()
total_loss += loss.item()
total_mse += loss_mse.item() if MLM else 0.0
total_cls += loss_cls.item() if CLS else 0.0
total_cce += loss_cce.item() if CCE else 0.0
total_mvc += loss_mvc.item() if MVC else 0.0
total_ecs += loss_ecs.item() if ECS else 0.0
total_dab += loss_dab.item() if DAB else 0.0
total_adv_E += loss_adv_E.item() if ADV else 0.0
total_adv_D += loss_adv_D.item() if ADV else 0.0
total_zero_log_prob += loss_zero_log_prob.item() if explicit_zero_prob else 0.0
total_mvc_zero_log_prob += (
loss_mvc_zero_log_prob.item() if MVC and explicit_zero_prob else 0.0
)
total_error += error_rate
if batch % log_interval == 0 and batch > 0:
lr = scheduler.get_last_lr()[0]
ms_per_batch = (time.time() - start_time) * 1000 / log_interval
cur_loss = total_loss / log_interval
cur_mse = total_mse / log_interval
cur_cls = total_cls / log_interval if CLS else 0.0
cur_cce = total_cce / log_interval if CCE else 0.0
cur_mvc = total_mvc / log_interval if MVC else 0.0
cur_ecs = total_ecs / log_interval if ECS else 0.0
cur_dab = total_dab / log_interval if DAB else 0.0
cur_adv_E = total_adv_E / log_interval if ADV else 0.0
cur_adv_D = total_adv_D / log_interval if ADV else 0.0
cur_zero_log_prob = (
total_zero_log_prob / log_interval if explicit_zero_prob else 0.0
)
cur_mvc_zero_log_prob = (
total_mvc_zero_log_prob / log_interval
if MVC and explicit_zero_prob
else 0.0
)
cur_error = total_error / log_interval
# ppl = math.exp(cur_loss)
logger.info(
f"| epoch {epoch:3d} | {batch:3d}/{num_batches:3d} batches | "
f"lr {lr:05.4f} | ms/batch {ms_per_batch:5.2f} | "
f"loss {cur_loss:5.2f} | "
+ (f"mse {cur_mse:5.2f} | mre {cur_error:5.2f} |" if MLM else "")
+ (f"cls {cur_cls:5.2f} | " if CLS else "")
+ (f"err {cur_error:5.2f} | " if CLS else "")
+ (f"cce {cur_cce:5.2f} |" if CCE else "")
+ (f"mvc {cur_mvc:5.2f} |" if MVC else "")
+ (f"ecs {cur_ecs:5.2f} |" if ECS else "")
+ (f"dab {cur_dab:5.2f} |" if DAB else "")
+ (f"adv_E {cur_adv_E:5.2f} |" if ADV else "")
+ (f"adv_D {cur_adv_D:5.2f} |" if ADV else "")
+ (f"nzlp {cur_zero_log_prob:5.2f} |" if explicit_zero_prob else "")
+ (
f"mvc_nzlp {cur_mvc_zero_log_prob:5.2f} |"
if MVC and explicit_zero_prob
else ""
)
)
total_loss = 0
total_mse = 0
total_cls = 0
total_cce = 0
total_mvc = 0
total_ecs = 0
total_dab = 0
total_adv_E = 0
total_adv_D = 0
total_zero_log_prob = 0
total_mvc_zero_log_prob = 0
total_error = 0
start_time = time.time()
def evaluate(model: nn.Module, loader: DataLoader, return_raw: bool = False) -> float:
"""
Evaluate the model on the evaluation data.
"""
if return_raw:
cell_emb_dict["test"]=[]
cls_logit_dict["test"]=[]
else:
cell_emb_dict["valid"]=[]
cls_logit_dict["valid"]=[]
model.eval()
total_loss = 0.0
total_error = 0.0
total_dab = 0.0
total_num = 0
predictions = []
with torch.no_grad():
for batch_data in loader:
input_gene_ids = batch_data["gene_ids"].to(device)