File size: 1,514 Bytes
4c62147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import argparse

from . import logger


cmdline_strs = None


def parse_args(parser, args_dict):
    global cmdline_strs

    def dict_to_cmdline(key, value):
        if len(key) == 1:
            key = '-{}'.format(key)
        else:
            key = '--{}'.format(re.sub(r'_', '-', key))
        value = str(value)
        return key, value

    cmdline_strs = [dict_to_cmdline(key, value) for key, value in args_dict.items()]
    cmdline_strs = ['{0} = {1}'.format(param[0], param[1]) for param in cmdline_strs]

    cmdline_args = (dict_to_cmdline(key, value) for key, value in args_dict.items())
    cmdline_args = list(sum(cmdline_args, ()))

    return parser.parse_args(cmdline_args)


def print_args():
    global cmdline_strs
    logger.log_info('Experiment args: \n  {0}\n'.format('\n  '.join(cmdline_strs)))


def str2bool(v):
    if v.lower() in ('yes', 'true', 't', 'y', '1'):
        return True
    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
        return False
    else:
        logger.log_err('str2bool requires a boolean value, but got {0}\n'.format(v))


def str2intlist(v):
    v = v.replace('[', '').replace(']', '').replace('(', '').replace(')', '').split(',')
    int_list = []
    for i in v:
        int_list.append(int(i.strip()))

    return int_list


def str2floatlist(v):
    v = v.replace('[', '').replace(']', '').replace('(', '').replace(')', '').split(',')
    float_list = []
    for f in v:
        float_list.append(float(f.strip()))

    return float_list