-
Notifications
You must be signed in to change notification settings - Fork 2
/
parsers.py
654 lines (605 loc) · 19.8 KB
/
parsers.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
import argparse
##########################
#### TASK SPECIFIC PARSERS
##########################
def edit_distance_parser():
"""
Creates an argument parser for configuration related to Edit Distance Approximation and Closest String Retrieval Tasks.
Arguments:
- None
Returns:
- argparse.ArgumentParser: An argument parser object populated with edit distance-related arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--data",
type=str,
default="datasets/edit_distance/edit_qiita_large.pkl",
help="Edit Distance Approximation Dataset path",
)
parser.add_argument(
"--retrieval_data",
type=str,
default=None,
help="Retrieval Dataset path",
)
parser.add_argument(
"--scaling",
action=argparse.BooleanOptionalAction,
default=True,
help="Whether to scale edit distance by learned parameter",
)
parser.add_argument(
"--zero_shot_retrieval",
action=argparse.BooleanOptionalAction,
default=False,
help="Instead of training use combination of computed embeddings to retrive closest sequences",
)
parser.add_argument(
"--zero_shot_method",
type=str,
default="concat,sum,mean",
help="Method for zero shot retrieval, to choose from: mean, sum, concat, if multiple, separate by comma",
)
parser.add_argument(
"--distance_type",
type=str,
default="hyperbolic",
help="Distance type for retrieval, edit distance, hierarchical, to choose from: hyperbolic, manhattan, cosine, euclidean, square",
)
parser = general(parser)
parser = representations_general_parameters(parser)
parser = training_parameters(parser)
parser = mlp_model(parser)
parser = cnn_model(parser)
parser = word2vec_args(parser)
parser = node2vec_args(parser)
parser = ssgnn_args(parser)
return parser
def coding_metagenomics_parser():
"""
Creates an argument parser for configuration related to Gene Prediction Task.
Arguments:
- None
Returns:
- argparse.ArgumentParser: An argument parser object populated with edit distance-related arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--data",
type=str,
default="datasets/geneRFinder",
help="Datasets folder path",
)
parser.add_argument(
"--include_training_1",
action=argparse.BooleanOptionalAction,
default=True,
help="Whether to include training 1 in the test set",
)
parser.add_argument(
"--data_max_sequence_length",
type=int,
default=600,
help="Datasets folder path",
)
parser.add_argument(
"--only_CAMI_dataset",
action=argparse.BooleanOptionalAction,
default=False,
help="Whether to include only CAMI dataset",
)
parser.add_argument(
"--proportion_samples_to_keep_supervised_training",
type=float,
default=1,
help="If 1, keeps entire dataset for supervised training, if 0.1, keeps 0.1 of data for supervised training",
)
parser.add_argument(
"--dropout",
type=float,
default=0,
help="Dropout value for dense part fo the network",
)
parser = general(parser)
parser = representations_general_parameters(parser)
parser = training_parameters(parser)
parser = mlp_model(parser)
parser = cnn_model(parser)
parser = word2vec_args(parser)
parser = node2vec_args(parser)
parser = ssgnn_args(parser)
return parser
##########################
###### SHARED AMONG TASK
##########################
def general(parser):
parser.add_argument(
"--seed",
type=str,
default="2137",
help="set random seed values for which to train",
)
parser.add_argument(
"--wandb_dir",
type=str,
default="./wandb",
help="set directory for offline wandb logs",
)
parser.add_argument(
"--disable_tqdm",
action=argparse.BooleanOptionalAction,
default=True,
help="Whether to disable tqdm progress bar",
)
return parser
def representations_general_parameters(parser):
parser.add_argument(
"--include_val_test_unsupervised",
action=argparse.BooleanOptionalAction,
default=False,
help="Whether to include validation and test sets in dataset for unsupervised representation learning",
)
parser.add_argument(
"--representation_method",
type=str,
default="kmer_node2vec",
help="Name of Representation methods",
)
parser.add_argument(
"--random_representation",
action=argparse.BooleanOptionalAction,
default=False,
help="Works only when method is set to onehot - replaces one hot with random vectors",
)
parser.add_argument(
"--representation_k",
type=str,
default="3",
help="K-mer size for representation",
)
parser.add_argument(
"--representation_stride",
type=str,
default="1",
help="Stride for representation, support multiples, separated by commas",
)
parser.add_argument(
"--inference_stride", type=int, default=1, help="Stride for inference"
)
return parser
# downstream task training
def training_parameters(parser):
parser.add_argument(
"--model_class",
type=str,
default="mlp",
help="Either mlp or cnn1d for downstream task",
)
parser.add_argument(
"--accelerator",
type=str,
default="auto",
help="accelerator name for pytorch lightning",
)
parser.add_argument(
"--epochs",
type=int,
default=200,
help="Number of epochs for pytorch model training for downstream task",
)
parser.add_argument(
"--batch_size",
type=int,
default=128,
help="Batch Size for pytorch model training for downstream task",
)
parser.add_argument(
"--learning_rate",
type=float,
default=1e-4,
help="Learning rate for pytorch model training for downstream task",
)
parser.add_argument(
"--check_val_every_n_epoch",
type=int,
default=10,
help="How often evaluate for pytorch model training for downstream task",
)
parser.add_argument(
"--trainable_embedding",
action=argparse.BooleanOptionalAction,
default=False,
help="By default embedding layer is frozen, if set to true, also include embedding in backprop",
)
return parser
# downstream task model
def cnn_model(parser):
parser.add_argument(
"--channels",
type=str,
default="128,256,256,128",
help="Following channel sizes for each k branch, separated by commas",
)
parser.add_argument(
"--batch_norm",
action=argparse.BooleanOptionalAction,
default=True,
help="If batch normalisation should be applied",
)
parser.add_argument(
"--pooling",
type=str,
default="avg",
help="specify one of pooling: avg, max or None",
)
parser.add_argument(
"--kernel_size",
type=int,
default=3,
help="specify kernel size, if none the sama as representation size",
)
return parser
# downstream task model
def mlp_model(parser):
parser.add_argument(
"--network_layers_before_cat",
type=str,
default="",
help="Layers sizes for seperate #k paths in network",
)
parser.add_argument(
"--network_layers_after_cat",
type=str,
default="128",
help="Layers sizes after concatanated #k paths are concatanated",
)
parser.add_argument(
"--activation",
type=str,
default="ReLU",
help="Torch activation functions after linear layers, apart from the last one",
)
return parser
# kmer representation model
def word2vec_args(parser):
parser.add_argument(
"--representation_size",
type=int,
default=32,
help="Embedding Size for Word2vec and Node2Vec",
)
parser.add_argument(
"--window_size",
type=int,
default=5,
help="Window size, only for Word2vec and Node2Vec",
)
return parser
# kmer representation model
def node2vec_args(parser):
parser.add_argument(
"--walk_len",
type=int,
default=20,
help="Length of a single walk, only for Node2Vec",
)
parser.add_argument(
"--num_walks",
type=int,
default=200,
help="Number of Walks, only for Node2Vec",
)
parser.add_argument(
"--p",
type=float,
default=1,
help="the probability of a random walk getting back to the previous node, only for Node2Vec",
)
parser.add_argument(
"--q",
type=float,
default=1,
help="probability that a random walk can pass through a previously unseen part of the graph, only for Node2Vec",
)
return parser
# kmer representation model
def ssgnn_args(parser):
parser.add_argument(
"--representation_small_k",
)
parser.add_argument(
"--representation_ss_task",
type=str,
default="CL",
help="Name for Self supervised task for representation learning, to choose from: Contrastive Learning (CL), Autoencoder (AE)",
)
parser.add_argument(
"--representation_ss_edge_weight_loss_weight",
type=float,
default=1.0,
help="Weight for edge weight related loss",
)
parser.add_argument(
"--representation_ss_kmer_frequency_loss_weight",
type=float,
default=1.0,
help="Weight for kmer frequency related loss",
)
parser.add_argument(
"--representation_ss_edit_distance_loss_weight",
type=float,
default=0.0,
help="Weight for edit distance related loss",
)
parser.add_argument(
"--representation_ss_negative_sampling_loss_weight",
type=float,
default=1.0,
help="Weight for negative sampling loss",
)
parser.add_argument(
"--representation_ss_encoder",
type=str,
default="GCN",
help="Name for encoder for representation learning, to choose from: GCN, MLP, GAT, RGCN",
)
parser.add_argument(
"--representation_ss_hidden_channels",
type=str,
default="",
help="Hidden channels for encoder for representation learning, the formating is: 128_ED,128_DB,128_KF0, where the first number is number of channels, and the second is the edges type",
)
parser.add_argument(
"--representation_ss_last_layer_edge_type",
type=str,
default="DB",
help="Last layer edge type for representation learning, to choose from: ED (edit distance), DB, KF0, KF1, ... (Kmer Frequency)",
)
parser.add_argument(
"--representation_ss_activation",
type=str,
default="ReLU",
help="Activation for representation learning, to choose from: ReLU, LeakyReLU, ELU, Tanh, Sigmoid",
)
parser.add_argument(
"--representation_ss_lr",
type=float,
default=1e-3,
help="Learning rate for self supervised representation learning",
)
parser.add_argument(
"--representation_ss_epochs",
type=int,
default=10000,
help="Number of epochs for self supervised representation learning",
)
parser.add_argument(
"--representation_ss_probability_masking_edges",
type=float,
default=0.0,
help="Probability of masking an edge during self supervised autoencoder training",
)
parser.add_argument(
"--representation_ss_probability_masking_nodes",
type=float,
default=0.0,
help="Probability of masking a node during self supervised autoencoder training",
)
parser.add_argument(
"--representation_ss_sampling_walk_length",
type=int,
default=5,
help="Walk length for random walk sampling",
)
parser.add_argument(
"--representation_ss_sampling_num_walks",
type=int,
default=20,
help="Number of walks for random walk sampling",
)
parser.add_argument(
"--representation_ss_sampling_window_size",
type=int,
default=3,
help="Window size for random walk sampling",
)
parser.add_argument(
"--representation_ss_sampling_proportion_nodes_to_sample",
type=float,
default=1,
help="Proportion of nodes to sample for random walk sampling",
)
parser.add_argument(
"--representation_ss_resample_every_num_epochs",
type=int,
default=-1,
help="Resample every num epochs for random walk sampling",
)
parser.add_argument(
"--representation_ss_proportion_negative_to_positive_samples",
type=float,
default=1,
help="Proportion of negative to positive samples for random walk sampling",
)
parser.add_argument(
"--representation_ss_rw_p",
type=float,
default=1,
help="p for random walk sampling",
)
parser.add_argument(
"--representation_ss_rw_q",
type=float,
default=1,
help="q for random walk sampling",
)
parser.add_argument(
"--representation_ss_edges_threshold",
type=float,
default=0.0,
help="Threshold for similarity edges that are kept, helpful for reducing memory usage",
)
parser.add_argument(
"--representation_ss_edges_keep_top_k",
type=float,
default=None,
help="Keep top k most similar edges, helpful for reducing memory usage",
)
parser.add_argument(
"--representation_ss_initial_labels",
type=str,
default="kmer_frequency",
help="Type of labels to be initialised as nodes, with small_k helpfull for reducing memory usage",
)
parser.add_argument(
"--representation_ss_batch_size",
type=int,
default=64,
help="Batch size for self supervised representation learning",
)
parser.add_argument(
"--representation_ss_create_all_kmers",
action=argparse.BooleanOptionalAction,
default=False,
help="Whether to create all kmers for representation learning",
)
parser.add_argument(
"--representation_ss_normalise_embeddings",
action=argparse.BooleanOptionalAction,
default=False,
help="Whether to normalise embeddings for representation learning",
)
parser.add_argument(
"--representation_ss_faiss_ann",
action=argparse.BooleanOptionalAction,
default=False,
help="Whether to use faiss for approximate nearest neighbours for representation learning",
)
parser.add_argument(
"--representation_ss_faiss_index_type",
type=str,
default="IVFFlat",
help="Faiss index type for representation learning",
)
parser.add_argument(
"--representation_ss_faiss_distance",
type=str,
default="L2",
help="Faiss distance for representation learning",
)
parser.add_argument(
"--representation_ss_faiss_nlist",
type=int,
default=None,
help="Faiss nlist for representation learning",
)
parser.add_argument(
"--representation_ss_faiss_nprobe",
type=int,
default=None,
help="Faiss nprobe for representation learning",
)
parser.add_argument(
"--representation_ss_faiss_m",
type=int,
default=None,
help="Faiss m for representation learning",
)
parser.add_argument(
"--representation_ss_faiss_nbits",
type=int,
default=None,
help="Faiss nbits for representation learning",
)
return parser
def assert_arguments_correctness(args):
"""
Verifies the correctness and consistency of given arguments.
Parameters:
- args (object): An object, typically an instance of `argparse.Namespace`
, containing the command-line arguments or configurations to be checked.
Raises:
- AssertionError: If any of the conditions for argument correctness are not met.
"""
if hasattr(args, "representation_ss_faiss_ann"):
if args.representation_ss_faiss_ann:
assert (
args.representation_ss_task == "CL"
), "Faiss ANN only supported for CL task."
assert (
args.representation_ss_edges_keep_top_k
), "Faiss ANN only supported with keep_top_k."
if hasattr(args, "model_class"):
if args.network_layers_before_cat == "" and args.model_class == "mlp":
assert len(args.representation_k.split(",")) == 1
if hasattr(args, "representation_k"):
if (
args.representation_method != "kmer_ssgnn"
or args.representation_method != "kmer_ssgnn_miniBatch"
):
assert (
len(args.representation_stride.split(",")) == 1
), "Multiple strides only supported for ssgnn."
if hasattr(args, "representation_ss_hidden_channels"):
if args.representation_ss_hidden_channels != "":
if len(args.representation_ss_hidden_channels[0].split("_")) > 1:
assert (
max(
[
int(x.split("_")[1][-1])
for x in args.representation_ss_hidden_channels.split(",")
if x.split("_")[1][0:2] == "KF"
]
)
+ 1
) <= len(
args.representation_small_k.split(",")
), "Number of hidden channels for kmer frequency must be equal or smaller to number of small_k values."
def convert_string_args_to_list(args):
"""
Converts specific string attributes of the given argument object into lists.
The function inspects certain attributes of the `args` object which are expected
to be comma-separated strings. These attributes, if present and non-empty, are
split and converted to lists.
Parameters:
- args (object): An object, typically an instance of `argparse.Namespace`,
containing attributes whose string values need to be converted into lists.
Returns:
- object: The modified `args` object with the relevant attributes converted
from strings to lists.
"""
if hasattr(args, "channels"):
if args.channels == "":
args.channels = []
else:
args.channels = [int(x) for x in args.channels.split(",")]
if hasattr(args, "network_layers_before_cat"):
if args.network_layers_before_cat == "":
args.network_layers_before_cat = []
else:
args.network_layers_before_cat = [
int(x) for x in args.network_layers_before_cat.split(",")
]
if hasattr(args, "network_layers_after_cat"):
if args.network_layers_after_cat == "":
args.network_layers_after_cat = []
else:
args.network_layers_after_cat = [
int(x) for x in args.network_layers_after_cat.split(",")
]
if hasattr(args, "representation_ss_hidden_channels"):
if args.representation_ss_hidden_channels == "":
args.representation_ss_hidden_channels = []
else:
args.representation_ss_hidden_channels = [
str(x) for x in args.representation_ss_hidden_channels.split(",")
]
if hasattr(args, "representation_stride"):
if args.representation_stride == "":
args.representation_stride = []
else:
args.representation_stride = [
int(x) for x in args.representation_stride.split(",")
]
return args