forked from amaratariq/readmit-stgnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.py
executable file
·803 lines (786 loc) · 23.4 KB
/
args.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
import argparse
def str2bool(x):
if x == "true" or x == "True":
return True
else:
return False
def get_args():
parser = argparse.ArgumentParser(description="COVID spatiotemporal GNN")
# general args
parser.add_argument(
"--save_dir",
type=str,
default=None,
help="Directory to save the outputs and checkpoints.",
)
parser.add_argument(
"--load_model_path",
type=str,
default=None,
help="Model checkpoint to start training/testing from.",
)
parser.add_argument(
"--do_train",
default=False,
type=str2bool,
# action="store_true",
help="Whether perform training.",
)
parser.add_argument(
"--gpu_id",
default=0,
type=int,
help="Which GPU to use? If None, use default GPU.",
)
parser.add_argument("--rand_seed", type=int, default=123, help="Random seed.")
parser.add_argument(
"--patience",
type=int,
default=5,
help="Number of patience epochs before early stopping.",
)
parser.add_argument(
"--extract_embeddings",
default=False,
action="store_true",
help="Whether or not to extract node embeddings after training.",
)
# graph args
# parser.add_argument(
# "--edge_thresh",
# default=None,
# type=float,
# help="Edge threshold",
# )
parser.add_argument(
"--edge_top_perc",
default=None,
type=float,
help="Top percentage edges to be kept.",
)
parser.add_argument(
"--edge_sigma", default=None, type=float, help="Kernel size for edge weight."
)
parser.add_argument(
"--use_gauss_kernel",
default=False,
# action="store_true",
type=str2bool,
help="Whether or not to use thresholded Gaussian kernel for edges",
)
parser.add_argument(
"--max_seq_len",
type=int,
default=None,
help="Maximum sequence length (num of study dates).",
)
parser.add_argument(
"--max_seq_len_img",
type=int,
default=None,
help="Maximum sequence length for images.",
)
parser.add_argument(
"--max_seq_len_ehr",
type=int,
default=None,
help="Maximum sequence length for ehr.",
)
parser.add_argument(
"--label_cutoff", type=int, default=7, help="Cutoff days for binary labels."
)
parser.add_argument(
"--dist_measure",
type=str,
default="cosine",
choices=("cosine", "hamming", "euclidean", "correlation", "jaccard"),
help="Which distance measure? cosine, distance, or correlation.",
)
# parser.add_argument(
# "--by_study_date",
# default=False,
# action='store_true',
# help='Whether or not use one study date as one time step.'
# )
parser.add_argument(
"--img_by",
type=str,
default="cxr",
choices=("day", "cxr"),
help="Each time step is by one day or by one cxr.",
)
parser.add_argument(
"--label_name",
default="hospital_stay",
choices=("hospital_stay", "icu_admission", "multiclass"),
type=str,
help="Label name. hospital_stay or icu_admission.",
)
parser.add_argument(
"--node_by",
type=str,
default="hospital_admission",
choices=("patient", "icu_admission", "hospital_admission"),
help="Each node is one patient, one hospital admission, or one ICU admission.",
)
parser.add_argument(
"--filter_short_stay",
action="store_true",
default=False,
help="Whether or not to filter out hospital stays <= 1 day.",
)
parser.add_argument(
"--filter_preadmit",
action="store_true",
default=False,
help="Whehter or not to filter out preadmission CXRs.",
)
parser.add_argument(
"--edge_modality",
type=str,
nargs="+",
default=["demo", "cpt", "icd", "lab", "hospital_stay", "med", "admit_reason"],
help="Modalities used for constructing edges.",
)
# parser.add_argument(
# "--add_pxs",
# default=False,
# action='store_true',
# help='Whether to add PXS score when computing edges.'
# )
# parser.add_argument(
# "--add_cpt",
# default=False,
# action='store_true',
# help='Whether to add CPT when computing edges.'
# )
parser.add_argument(
"--dynamic_graph",
default=False,
action="store_true",
help="Whether to have one unique graph at each time step.",
)
# parser.add_argument(
# '--comor_dict_dir',
# default=None,
# type=str,
# help='Path to precomputed demo/comorbidity dictionary.'
# )
# parser.add_argument(
# '--demo_dict_dir',
# default=None,
# type=str,
# help='Path to precomputed demo dictionary.'
# )
# parser.add_argument(
# '--cpt_dict_dir',
# default=None,
# type=str,
# help='Path to precomputed cpt dictionary.'
# )
# parser.add_argument(
# '--icd_dict_dir',
# default=None,
# type=str,
# help='Path to precomputed icd dictionary.'
# )
# parser.add_argument(
# '--lab_dict_dir',
# default=None,
# type=str,
# help='Path to precomputed lab dictionary.'
# )
# parser.add_argument(
# '--admit_reason_dict_dir',
# default=None,
# type=str,
# help="Path to precomputed admission reason dictionary."
# )
parser.add_argument(
"--mask_by_admit_reason",
type=str2bool,
default=False,
help="Whether to mask the edges by admission reason.",
)
parser.add_argument(
"--feature_type",
default="imaging",
choices=("imaging", "non-imaging", "pxs", "multimodal", "raw_images"),
# choices=('imaging', 'pxs', 'cpt', 'comor', 'combined', 'node_embedding'),
type=str,
help="Options: imaging, pxs, cpt etc.",
)
parser.add_argument(
"--ehr_types",
default=["demo", "cpt", "icd", "lab", "med"],
nargs="+",
type=str,
help="Types of EHR for node features.",
)
parser.add_argument(
"--emb_dim",
type=int,
default=128,
help="Embedding dimension for early fusion model.",
)
# parser.add_argument(
# '--cat_emb_dim',
# type=int,
# default=None,
# help='Embedding dim for categorical variables'
# )
# parser.add_argument(
# "--feature_dim",
# type=int,
# default=1024,
# help="Image feature dim."
# )
# parser.add_argument(
# "--multimodal_in_dims",
# type=int,
# default=None,
# nargs="+",
# help="List of dims for multimodal inputs. Must be the same order as the inputs."
# )
# parser.add_argument(
# "--multimodal_emb_dim",
# type=int,
# default=None,
# help="Linear embedding dimension for multimodal inputs."
# )
# parser.add_argument(
# "--ehr_feature_dim",
# type=int,
# default=358,
# help="EHR feature dim."
# )
parser.add_argument(
"--demo_file",
type=str,
default=None,
help="Path to csv file containing patient demographics etc.",
)
parser.add_argument(
"--img_feature_files",
type=str,
nargs="+",
default=None,
help="Dir to imaging feature files (csv files).",
)
parser.add_argument(
"--ehr_feature_files",
type=str,
nargs="+",
default=None,
help="Dir to EHR feature files (csv files).",
)
parser.add_argument(
"--edge_ehr_files",
type=str,
nargs="+",
default=None,
help="Dir to EHR feature files (csv files) for graph edges.",
)
parser.add_argument(
"--dataset",
type=str,
default="mayo",
choices=("mayo", "emory", "stanford", "mimic"),
help="Name of dataset.",
)
parser.add_argument(
"--pack_padded_seq",
action="store_true",
default=False,
help="Whether to pack sequence for GRU/LSTM.",
)
parser.add_argument(
"--add_timedelta",
action="store_true",
default=False,
help="Whether to append timedelta to features.",
)
parser.add_argument(
"--standardize",
type=str2bool,
default=True,
help="Whether to standardize input to zero mean and unit variance.",
)
parser.add_argument(
"--tabnet_pretrain",
type=str2bool,
default=False,
help="Whether to pretrain TabNet.",
)
# model args
parser.add_argument(
"--model_name",
type=str,
default="stgcn",
choices=(
"stgcn",
"gat",
"multihead_gat",
"graphsage",
"gcn",
"densenet",
"mlp",
"gru",
"lstm",
"gaan",
"brits",
"graph_transformer",
"gin",
"transformer",
"tabnet",
"hippo",
"joint_fusion",
"early_fusion",
"late_fusion",
"joint_fusion_nontemporal",
"lstm_fusion",
"gru_fusion",
),
help="Name of the model.",
)
# parser.add_argument(
# "--img_encoder_name",
# type=str,
# default='stgcn',
# choices=('stgcn'),
# help="Name of image encoder."
# )
parser.add_argument(
"--ehr_encoder_name",
type=str,
default=None,
choices=("tabnet", "embedder", None),
help="Name of ehr encoder.",
)
parser.add_argument(
"--freeze_pretrained",
type=str2bool,
default="False",
help="Whether to freeze pretrained model.",
)
parser.add_argument(
"--hidden_dim", type=int, default=64, help="Hidden size of GCN layers."
)
parser.add_argument(
"--joint_hidden",
nargs="+",
type=int,
default=[128],
help="List of hidden dims for joint fusion model.",
)
parser.add_argument(
"--gaan_map_feats",
type=int,
default=128,
help="Hidden size of intermediate mapping layer for GaAN only.",
)
parser.add_argument(
"--num_gcn_layers", type=int, default=1, help="Number of GCN layers."
)
parser.add_argument(
"--g_conv",
type=str,
default="graphsage",
choices=("gcn", "graphsage", "gat", "multihead_gat", "gaan", "gin"),
help="Type of GRU layers.",
)
parser.add_argument(
"--num_rnn_layers", type=int, default=1, help="Number of RNN (GRU) layers."
)
parser.add_argument(
"--rnn_hidden_dim", type=int, default=64, help="Hidden size of RNN layers."
)
parser.add_argument(
"--add_bias",
# action='store_true',
type=str2bool,
default=False,
help="Whether to add bias to GraphGRU cell.",
)
parser.add_argument(
"--num_classes",
type=int,
default=1,
help="Number of output class. 1 for binary classification.",
)
parser.add_argument("--dropout", type=float, default=0.0, help="Dropout proba.")
parser.add_argument(
"--activation_fn",
type=str,
choices=("relu", "elu"),
default="relu",
help="Activation function name.",
)
parser.add_argument(
"--norm",
type=str,
choices=("", "batch", "layer"),
default="",
help="Normalization layer name.",
)
parser.add_argument(
"--aggregator_type",
type=str,
default="mean",
choices=("mean", "gcn", "pool", "lstm"),
help="Aggregator type. For GraphSAGE only.",
)
parser.add_argument(
"--num_heads",
type=int,
default=3,
help="Number of GAT heads. For Multihead GAT only.",
)
parser.add_argument(
"--num_mlp_layers", type=int, default=2, help="Number of MLP layers in GIN."
)
parser.add_argument(
"--learn_eps",
action="store_true",
default=False,
help="Whether to learn eps or keep it a constant.",
)
# parser.add_argument(
# "--multihead_merge",
# type=str,
# default="cat",
# choices=("mean", "cat"),
# help="How to merge multihead outputs. For Multihead GAT only."
# )
parser.add_argument(
"--final_pool",
type=str,
default="last",
choices=("last", "mean", "max", "cat"),
help="How to pool time step results?",
)
parser.add_argument(
"--t_model",
type=str,
default="gru",
choices=("gru", "lstm", "mgu", "rnn", "minimalrnn", "hippo-legs"),
help="Which temporal model to use?",
)
parser.add_argument(
"--memory_size", type=int, default=1, help="Memory size for HiPPO."
)
parser.add_argument(
"--memory_order", type=int, default=-1, help="Memory order for HiPPO."
)
parser.add_argument(
"--tcn_kernel_size", type=int, default=2, help="Kernel size for TCN."
)
parser.add_argument(
"--negative_slope",
type=float,
default=0.2,
help="Negative slope for LeakyReLU.",
)
parser.add_argument(
"--gat_residual",
action="store_true",
default=False,
help="Whether to add residual connection for GAT.",
)
# training args
parser.add_argument("--lr", type=float, default=1e-3, help="learning rate")
parser.add_argument("--num_epochs", type=int, default=50, help="Number of epochs.")
parser.add_argument(
"--eval_every", type=int, default=1, help="Evaluate on dev set every x epoch."
)
parser.add_argument(
"--metric_name",
type=str,
default="F1",
choices=("F1", "acc", "loss", "auroc", "aupr"),
help="Name of dev metric to determine best checkpoint.",
)
parser.add_argument("--l2_wd", type=float, default=5e-4, help="L2 weight decay.")
parser.add_argument(
"--pos_weight",
type=float,
nargs="+",
default=1,
help="Positive class weight or list of class weights to weigh the loss function.",
)
parser.add_argument(
"--thresh_search",
# action='store_true',
type=str2bool,
default=False,
help="Whether or not to perform threshold search on dev set.",
)
# parser.add_argument(
# '--undersample',
# action='store_true',
# default=False,
# help='Whether or not to subsample majority class.'
# )
parser.add_argument(
"--use_sampler",
action="store_true",
default=False,
help="Whether or not to upsample or undersample majority class.",
)
parser.add_argument(
"--num_samples",
type=int,
default=None,
help="Number of samples for random sampler.",
)
parser.add_argument(
"--data_augment",
# action='store_true',
type=str2bool,
default=False,
help="Whether to perform data augmentation.",
)
parser.add_argument(
"--feature_mask_prob",
type=float,
default=0.2,
help="Proba for masking node features.",
)
parser.add_argument(
"--impute_weight", type=float, default=0.3, help="Imputation loss weight."
)
# parser.add_argument(
# '--up_scale',
# type=float,
# default=0,
# help="Upsampling scale for minority class. For GraphSMOTE only."
# )
# parser.add_argument(
# '--opt_new_G',
# action='store_true',
# default=False,
# help='Whether or not to update the decoder with classification loss. \
# For GraphSMOTE only.'
# )
# parser.add_argument(
# '--graphsmote_setting',
# type=str,
# default='recon_newG',
# choices=('recon', 'newG_cls', 'recon_newG'),
# help='Choose recon for pretraining feature extractor. \
# newG_cls for node classification using pretrained model. \
# recon_newG for fine-tuning on pretrained feature extractor for node classification.'
# )
# parser.add_argument(
# '--rec_weight',
# type=float,
# default=1e-6,
# help="Weighting of L_edge. For GraphSMOTE only."
# )
parser.add_argument(
"--train_batch_size", type=int, default=64, help="Training batch size."
)
parser.add_argument(
"--test_batch_size", type=int, default=64, help="Test batch size."
)
parser.add_argument("--num_workers", type=int, default=8, help="Number of workers.")
parser.add_argument(
"--which_img",
type=str,
default="last",
choices=("last", "mean", "all"),
help="Which image to use for the patient for non-temporal models.",
)
parser.add_argument(
"--cnn_finetune",
default=False,
action="store_true",
help="Whether or not only fine-tune classifier for CNN.",
)
parser.add_argument(
"--loss_func",
type=str,
default="binary_cross_entropy",
choices=("binary_cross_entropy", "cross_entropy", "focal_loss"),
help="Loss function to use.",
)
parser.add_argument(
"--focal_alpha",
type=float,
default=0.25,
help="Alpha hyperparam for focal loss.",
)
parser.add_argument(
"--focal_gamma", type=float, default=2, help="Gamma hyperparam for focal loss."
)
parser.add_argument(
"--fanout", type=int, default=4, help="Neighbors to sample for each edge."
)
#### HiPPO args ####
parser.add_argument(
"--hippo_memory_size", type=int, default=1, help="Memory size for HiPPO model."
)
parser.add_argument(
"--hippo_memory_order",
type=int,
default=-1,
help="Memory order for HiPPO model. If -1, equal to hidden size.",
)
#### TabNet args ####
parser.add_argument(
"--pretraining_ratio",
type=float,
default=0.2,
help="Ratio to mask out for pretraining.",
)
parser.add_argument(
"--n_d", type=int, default=8, help="Dimension of prediction layer."
)
parser.add_argument(
"--n_a", type=int, default=8, help="Dimension of attention layer."
)
parser.add_argument(
"--n_steps", type=int, default=8, help="Number of successive steps in network."
)
parser.add_argument(
"--gamma",
type=float,
default=1.3,
help="Scaling factor for attention updates (typically between 1 and 2).",
)
parser.add_argument(
"--cat_emb_dim",
type=int,
# nargs="+",
default=1,
help="Size of the embedding of categorical features \
if int, all categorical features will have same embedding size \
if list of int, every corresponding feature will have specific size.",
)
parser.add_argument(
"--n_independent",
type=int,
default=2,
help="Number of independent GLU layer in each GLU block (default 2).",
)
parser.add_argument(
"--n_shared",
type=int,
default=2,
help="Number of shared GLU layer in each GLU block (default 2).",
)
parser.add_argument(
"--virtual_batch_size",
type=int,
default=128,
help="Batch size for Ghost Batch Normalization.",
)
parser.add_argument(
"--momentum",
type=float,
default=0.02,
help="Float value between 0 and 1 which will be used for momentum in all batch norm.",
)
parser.add_argument(
"--mask_type",
type=str,
default="sparsemax",
choices=("sparsemax", "entmax"),
help="Either 'sparsemax' or 'entmax' : this is the masking function to use",
)
parser.add_argument(
"--ehr_pretrain_path", default=None, type=str, help="Path to pretrained model."
)
#### End of TabNet args ####
#### GraphTransformer args ####
parser.add_argument(
"--trans_nhead", type=int, default=8, help="Number of transformer heads."
)
parser.add_argument(
"--trans_dim_feedforward",
type=int,
default=128,
help="Feedforward dim for transformer.",
)
parser.add_argument(
"--trans_activation",
type=str,
default="relu",
choices=("relu", "gelu"),
help="Activation function in transformer layers.",
)
parser.add_argument(
"--att_neighbor",
action="store_true",
default=False,
help="Whether to attend to node's neighbors in graph transformer.",
)
#### End of GraphTransformer args ####
#### BGRL args ####
# parser.add_argument(
# "--bgrl_aug_proba",
# type=float,
# nargs='+',
# help='List of probability for masking node features and dropping edges for BGRL. In the order of \
# p_f1, p_f2, p_e1, p_e2',
# default=[0.2, 0.1, 0.2, 0.3]
# )
# parser.add_argument(
# '--bgrl_pred_hidden',
# type=int,
# default=128,
# help='Predictor hidden size for BGRL.'
# )
# parser.add_argument(
# '--bgrl_ema_decay',
# type=float,
# default=0.99,
# help='Decay rate for moving average.'
# )
# parser.add_argument(
# "--bgrl_pretrain_only",
# action='store_true',
# default=False,
# help='Whether to only pretrain on BGRL self-supervised task.'
# )
# parser.add_argument(
# '--bgrl_loss_weight',
# type=float,
# default=1.,
# help='Weight for BGRL self-supervised loss.'
# )
# parser.add_argument(
# '--bgrl_finetune',
# action='store_true',
# default=False,
# help='Whether to only finetune on BGRL node embeddings.'
# )
# parser.add_argument(
# '--bgrl_node_embedding_dir',
# type=str,
# default=None,
# help='Dir to trained BGRL node embeddings.'
# )
args = parser.parse_args()
# if args.bgrl_pretrain_only:
# args.metric_name = 'loss'
# args.maximize_metric = False
# which metric to maximize
if args.metric_name == "loss":
# Best checkpoint is the one that minimizes loss
args.maximize_metric = False
elif args.metric_name in ("F1", "acc", "auroc", "aupr"):
# Best checkpoint is the one that maximizes F1 or acc
args.maximize_metric = True
else:
raise ValueError('Unrecognized metric name: "{}"'.format(args.metric_name))
# must provide load_model_path if testing only
if (args.load_model_path is None) and (not (args.do_train)):
raise ValueError(
"For prediction only, please provide trained model checkpoint in argument load_model_path."
)
# if (args.load_model_path is None) and args.bgrl_finetune:
# raise ValueError(
# "For finetuning, please provide pretrained BGRL checkpoint in argument load_model_path."
# )
return args