forked from alexhernandezgarcia/ActiveLearningPipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nupack_sampler.py
125 lines (116 loc) · 3.64 KB
/
nupack_sampler.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
"""
Script to create data set of with nupack labels.
"""
from argparse import ArgumentParser
import os
import pickle
from pathlib import Path
import yaml
import time
import numpy as np
import pandas as pd
from tqdm import tqdm
from oracle import Oracle
from utils import get_config, namespace2dict, numpy2python
def add_args(parser):
"""
Adds command-line arguments to parser
Returns:
argparse.Namespace: the parsed arguments
"""
args2config = {}
parser.add_argument(
"-y",
"--yaml_config",
default=None,
type=str,
help="YAML configuration file",
)
args2config.update({"yaml_config": ["yaml_config"]})
parser.add_argument(
"--seed_toy",
type=int,
default=0,
)
args2config.update({"seed_toy": ["seeds", "toy_oracle"]})
parser.add_argument(
"--seed_dataset",
type=int,
default=0,
)
args2config.update({"seed_dataset": ["seeds", "dataset"]})
parser.add_argument(
"--oracle",
nargs="+",
default="nupack energy",
help="linear, potts, nupack energy, nupack pairs, nupack pins",
)
args2config.update({"oracle": ["dataset", "oracle"]})
parser.add_argument(
"--nalphabet",
type=int,
default=4,
help="Alphabet size",
)
args2config.update({"nalphabet": ["dataset", "dict_size"]})
parser.add_argument(
"--fixed_length",
dest="variable_length",
action="store_false",
default=True,
help="Models will sample within ranges set below",
)
args2config.update({"variable_length": ["dataset", "variable_length"]})
parser.add_argument("--min_length", type=int, default=10)
args2config.update({"min_length": ["dataset", "min_length"]})
parser.add_argument("--max_length", type=int, default=40)
args2config.update({"max_length": ["dataset", "max_length"]})
parser.add_argument(
"--nsamples",
type=int,
default=int(1e2),
help="Number of samples",
)
args2config.update({"nsamples": ["dataset", "init_length"]})
parser.add_argument(
"--no_indices",
dest="no_indices",
action="store_true",
default=False,
help="Omit indices in output CSV",
)
args2config.update({"no_indices": ["no_indices"]})
parser.add_argument(
"--output_csv",
type=str,
default=None,
help="Output CSV",
)
args2config.update({"output_csv": ["output"]})
return parser, args2config
def main(args):
oracle = Oracle(args)
samples_dict = oracle.initializeDataset(save=False, returnData=True)
scores = samples_dict["scores"]
samples_mat = samples_dict["samples"]
seq_letters = oracle.numbers2letters(samples_mat)
seq_ints = ["".join([str(el) for el in seq if el > 0]) for seq in samples_mat]
if isinstance(scores, dict):
scores.update({"letters": seq_letters, "indices": seq_ints})
df = pd.DataFrame(scores)
else:
df = pd.DataFrame({"letters": seq_letters, "indices": seq_ints, "scores": scores})
if args.output:
output_yml = Path(args.output).with_suffix(".yml")
with open(output_yml, "w") as f:
yaml.dump(numpy2python(namespace2dict(args)), f, default_flow_style=False)
if args.no_indices:
df.drop(columns="indices", inplace=True)
df.to_csv(args.output)
if __name__ == "__main__":
parser = ArgumentParser()
_, override_args = parser.parse_known_args()
parser, args2config = add_args(parser)
args = parser.parse_args()
config = get_config(args, override_args, args2config)
main(config)