Repository navigation
Expand file tree
/
Copy pathrun_opt.py
More file actions
106 lines (93 loc) · 4.25 KB
/
Copy pathrun_opt.py
File metadata and controls
106 lines (93 loc) · 4.25 KB
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
""" run_opt.py
"""
import foam.base_parsing as base_parsing
import foam.base_run_opt as base_run_opt
import foam.opt_graph_ga_fc.graph_ga_fc as graph_ga_fc
import warnings
warnings.filterwarnings('ignore')
import sys
import yaml
def get_args():
base_opt_parser = base_parsing.opt_baseparser()
parser = base_opt_parser.add_argument_group("Optimizer Args")
parser.add_argument(
"--population-size", type=int, help="Size of population", default=100
)
parser.add_argument(
"--offspring-size", type=int, help="Number of offspring", default=100
)
parser.add_argument(
"--starting-seed-size", type=int, help="Number of seeds to start with. if -1 (or negative), use all seeds available.", default=200
)
parser.add_argument(
"--num-islands", type=int, help="Number of islands", default=10
)
parser.add_argument(
"--mutation-rate", type=float, help="Mutation rate", default=0.07
)
parser.add_argument(
"--iceberg-param", type=float, help="ICEBERG score weight, use with sa-param", default=0.8
)
parser.add_argument(
"--sa-param", type=float, help="SA score weight, use with iceberg-param", default=0.2
)
parser.add_argument(
"--truncate", nargs="?", const=0.4, default=False, help="Truncate the population"
)
parser.add_argument(
"--selection-sorting-type", default="cand_crowding", action="store", help="Type of sorting to apply during selection"
)
parser.add_argument(
"--mutate-parents", default=False, action="store_true", help="Whether to mutate parents as additional offspring"
)
parser.add_argument(
"--parent-tiebreak", default="cand_crowding", action="store", help="Type of tiebreaking to apply during binary tournament selection"
)
parser.add_argument(
"--pubchem-seeds", default=False, action="store", help="Whether to use PubChem seeds in addition to the starting seeds"
)
parser.add_argument(
"--extra-seeds", default=None, type=str, help="Path to additional set of seeds provided if desired, in addition to an existing retrieval library already specified. Should be a JSON (key: List[Inchis]); only supports MassSpecGym IDs."
)
# Parse args twice to detect which were explicitly set
default_args, _ = base_opt_parser.parse_known_args([])
provided_args, unknown = base_opt_parser.parse_known_args()
if any(['use-multi-node' in f for f in sys.argv]):
turn_off_rpc = True
else:
turn_off_rpc = False
# TODO: won't override flags on CLI that are the same as teh default, even if the config is different.
## option 1
# if provided_args.config:
# with open(provided_args.config, 'r') as f:
# config_dict = yaml.safe_load(f)
# for key, value in config_dict.items():
# config_attr_name = key.replace("-", "_")
# if hasattr(provided_args, config_attr_name):
# # Only set config value if command line arg wasn't explicitly provided
# if getattr(provided_args, config_attr_name) == getattr(default_args, config_attr_name):
# setattr(provided_args, config_attr_name, value)
# # check if key is a valid arg! should throw error.
# else:
# raise ValueError(f"Invalid argument {key} in config file. Please check the config file.")
# if turn_off_rpc:
# provided_args.use_multi_node = False
# return provided_args
# option 2
if provided_args.config:
with open(provided_args.config, 'r') as f:
config_dict = yaml.safe_load(f)
valid_args = set(action.dest for action in base_opt_parser._actions)
for key, value in config_dict.items():
config_attr_name = key.replace("-", "_")
if config_attr_name not in valid_args:
raise ValueError(f"Invalid argument '{key}' in config file.")
base_opt_parser.set_defaults(**{k.replace("-", "_"): v for k, v in config_dict.items()})
args = base_opt_parser.parse_args()
return args
def run_opt():
args = get_args()
opt_cls = graph_ga_fc.GraphGAFCOptimizer
base_run_opt.run_optimization(opt_cls=opt_cls, args=args)
if __name__=="__main__":
run_opt()