-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse_args.py
68 lines (56 loc) · 2.31 KB
/
parse_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
"""Argument parser helper functions."""
import argparse
from pathlib import Path
import logging
import yaml
import envs
logger = logging.getLogger(__name__)
def parse_args() -> argparse.Namespace:
"""Parse arguments for the gym environment and logging levels.
Returns:
The parsed arguments as a namespace.
"""
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--env",
help="Selects the gym environment",
choices=envs.available_envs,
required=True)
parser.add_argument('--loglvl',
help="Logger levels",
choices=["DEBUG", "INFO", "WARN", "ERROR"],
default="INFO")
parser.add_argument("--render",
help="Render flag. Only used for testing",
choices=["y", "n"],
default="y")
parser.add_argument("--record",
help="Record video flag. Only used for testing",
choices=["y", "n"],
default="n")
parser.add_argument("--ntests",
help="Number of evaluation runs. Only used for testing",
default=10,
type=int)
args = parser.parse_args()
expand_args(args)
return args
def expand_args(args: argparse.Namespace):
"""Expand the arguments namespace with settings from the main config file.
Config can be found at './config/experiment_config.yaml'. Each config must be named after their
gym name.
Args:
args: User provided arguments namespace.
"""
logging.basicConfig(level=logging.INFO)
path = Path(__file__).parent / "config" / "experiment_config.yaml"
with open(path, "r") as f:
config = yaml.load(f, yaml.SafeLoader)
if "Default" not in config.keys():
raise KeyError("Config file is missing required entry `Default`!")
for key, val in config["Default"].items():
setattr(args, key, val)
if args.env not in config.keys():
logger.info(f"No specific config for {args.env} found, using defaults for all settings.")
else:
for key, val in config[args.env].items():
setattr(args, key, val)