Completed
Pull Request — master (#165)
by
unknown
33s
created

LogArgumentTemplate.unfold()   A

Complexity

Conditions 3

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
1
import re
2
import numpy as np
3
import numpy.random as npr
4
from collections import OrderedDict
5
6
7
def build_argument_templates_dictionnary():
8
    # Order matter, if some regex is more greedy than another, the it should go after
9
    argument_templates = OrderedDict()
10
    argument_templates[RangeArgumentTemplate.__name__] = RangeArgumentTemplate()
11
    argument_templates[ListArgumentTemplate.__name__] = ListArgumentTemplate()
12
    return argument_templates
13
14
15
class ArgumentTemplate(object):
16
    def __init__(self):
17
        self.regex = ""
18
19
    def unfold(self, match):
20
        raise NotImplementedError("Subclass must implement method `unfold(self, match)`!")
21
22
23
class ListArgumentTemplate(ArgumentTemplate):
24
    def __init__(self):
25
        self.regex = "\[[^]]*\]"
26
27
    def unfold(self, match):
28
        return match[1:-1].split(' ')
29
30
31
class RangeArgumentTemplate(ArgumentTemplate):
32
    def __init__(self):
33
        self.regex = "\[(\d+):(\d+)(?::(\d+))?\]"
34
35
    def unfold(self, match):
36
        groups = re.search(self.regex, match).groups()
37
        start = int(groups[0])
38
        end = int(groups[1])
39
        step = 1 if groups[2] is None else int(groups[2])
40
        return map(str, range(start, end, step))
41
42
43
class LinearArgumentTemplate(ArgumentTemplate):
44
    def __init__(self):
45
        self.regex = "(Lin)\[(-?\d+)\,(-?\d+)\,(\d+)\]"
46
47
    def unfold(self, match):
48
        groups = re.search(regex, match).groups()
49
        start, stop, npoints = [int(el) for el in groups[1:]]
50
        return map(str, np.linspace(start, stop, npoints))
51
52
53
class LogArgumentTemplate(ArgumentTemplate):
54
    def __init__(self):
55
        self.regex = "(Log)\[(-?\d+)\,(-?\d+)\,(\d+)(?:,(\d+))?\]"
56
57
    def unfold(self, match):
58
        groups = re.search(regex, match).groups()
59
        start, stop, npoints = [int(el) for el in groups[1:-1]]
60
        base = 10 if groups[-1] is None else int(groups[-1])
61
        return map(str, np.logspace(start, stop, npoints, base=base))
62
63
64
class UniformArgumentTemplate(ArgumentTemplate):
65
    def __init__(self):
66
        self.regex = "(U)\[(-?\d+)\,(-?\d+),(-?\d+)\]"
67
68
    def unfold(self, match):
69
        groups = re.search(self.regex, match).groups()
70
        low, high, nsamples = [int(el) for el in groups[1:]]
71
        return map(str, npr.uniform(low, high, size=(nsamples, )))
72
73
74
class NormalArgumentTemplate(ArgumentTemplate):
75
    def __init__(self):
76
        self.regex = "(N)\[(-?\d+)\,(\d+),(-?\d+)\]"
77
78
    def unfold(self, match):
79
        groups = re.search(self.regex, match).groups()
80
        loc, scale, nsamples = [int(el) for el in groups[1:]]
81
        return map(str, npr.normal(loc, scale, size=(nsamples, )))
82
83
argument_templates = build_argument_templates_dictionnary()
84