This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
argsmanaging.py
253 lines (190 loc) · 6.84 KB
/
argsmanaging.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/python
from enum import Enum
import numpy
import benchmarks
import utils.printing_utils as pu
class ArgsError(Enum):
NO_ERROR = 0
UNKNOWN_BENCHMARK = 1
INT_CAST_ERROR = 2
INT_INVALID_VALUE = 3
REGRESSOR_ERROR = 4
CLASSIFIER_ERROR = 5
class Regressor(Enum):
NEURAL_NETWORK = 'NN'
class Classifier(Enum):
DECISION_TREE = 'DT'
class ArgumentsHolder:
def __init__(self, benchmark: benchmarks.Benchmark = None, exp: float = 0,
regressor: Regressor = Regressor.NEURAL_NETWORK, classifier: Classifier = Classifier.DECISION_TREE,
dataset_index: int = 0, min_bits: int = 4, max_bits: int = 53, large_error_threshold: float = .9):
self.__benchmark = benchmark
self.__exp = exp
self.__regressor = regressor
self.__classifier = classifier
self.__datasetIndex = dataset_index
self.__minBitsNumber = min_bits
self.__maxBitsNumber = max_bits
self.__largeErrorThreshold = large_error_threshold
def get__benchmark(self):
return self.__benchmark
def set_benchmark(self, bm):
self.__benchmark = bm
def get_exponent(self):
return self.__exp
def get_error(self):
return numpy.float_power(10, -self.__exp)
def get_error_log(self):
return -numpy.log10(self.get_error())
def set_error_exp(self, exp):
self.__exp = exp
def get_regressor(self):
return self.__regressor
def set_regressor(self, regressor):
self.__regressor = regressor
def get_classifier(self):
return self.__classifier
def set_classifier(self, classifier):
self.__classifier = classifier
def get_dataset_index(self):
return self.__datasetIndex
def set_dataset_index(self, index):
self.__datasetIndex = index
def get_min_bits_number(self):
return self.__minBitsNumber
def set_min_bits_number(self, min_bits):
self.__minBitsNumber = min_bits
def get_max_bits_number(self):
return self.__maxBitsNumber
def set_max_bits_number(self, max_bits):
self.__maxBitsNumber = max_bits
def get_large_error_threshold(self):
return self.__largeErrorThreshold
def set_large_error_threshold(self, large_error_threshold):
self.__largeErrorThreshold = large_error_threshold
def is_legal(self):
return self.__benchmark is not None and self.__exp != 0
def __str__(self):
return "Benchmark {} ({}, vars in {}). {} regressor and {} classifier"\
.format(pu.param(self.__benchmark), pu.param_e(self.get_error()),
pu.param("[{:d}, {:d}]".format(self.__minBitsNumber, self.__maxBitsNumber)),
pu.param(self.__regressor.name),
pu.param(self.__classifier.name))
args = ArgumentsHolder()
def __int_value(value):
error = ArgsError.NO_ERROR
v = 0
try:
v = int(value)
if 0 >= v:
error = ArgsError.INT_INVALID_VALUE
except ValueError:
error = ArgsError.INT_CAST_ERROR
return error, v
def __benchmark(value):
error = ArgsError.NO_ERROR
if not benchmarks.exists(value):
error = ArgsError.UNKNOWN_BENCHMARK
if ArgsError.NO_ERROR == error:
args.set_benchmark(value)
return error, value
def _exp(value):
error, v = __int_value(value)
if ArgsError.NO_ERROR == error:
args.set_error_exp(v)
return error, args.get_error_log()
def __regressor(value):
error = ArgsError.NO_ERROR
if value not in [reg.value for reg in Regressor]:
error = ArgsError.REGRESSOR_ERROR
if ArgsError.NO_ERROR == error:
args.set_regressor(Regressor(value))
return error, value
def __classifier(value):
error = ArgsError.NO_ERROR
if value not in [cl.value for cl in Classifier]:
error = ArgsError.CLASSIFIER_ERROR
if ArgsError.NO_ERROR == error:
args.set_classifier(Classifier(value))
return error, value
def __dataset(value):
error, v = __int_value(value)
if ArgsError.NO_ERROR == error:
args.set_dataset_index(v)
return error, v
def __min_bits(value):
error, v = __int_value(value)
if ArgsError.NO_ERROR == error:
args.set_min_bits_number(v)
return error, v
def __max_bits(value):
error, v = __int_value(value)
if ArgsError.NO_ERROR == error:
args.set_max_bits_number(v)
return error, v
def error_handler(e, param, value):
assert isinstance(param, str)
param = param.replace('-', '')
err_str = "There was an input error"
if ArgsError.UNKNOWN_BENCHMARK == e:
err_str = "Can't find a benchmark called {}".format(value)
elif ArgsError.INT_CAST_ERROR == e:
err_str = "Expected an integer value, found '{}' as {}".format(value, param)
elif ArgsError.INT_INVALID_VALUE == e:
err_str = "{} value must be greater than 0".format(param)
elif ArgsError.REGRESSOR_ERROR == e:
s = ''
for reg in Regressor:
s += reg.value + ', '
s = s[:len(s) - 2]
err_str = "Invalid regressor {}. Possible values are: {}".format(value, s)
elif ArgsError.CLASSIFIER_ERROR == e:
s = ''
for cl in Classifier:
s += cl.value + ', '
s = s[:len(s) - 2]
err_str = "Invalid classifier {}. Possible values are: {}".format(value, s)
if ArgsError.NO_ERROR is not e:
print(pu.fatal(err_str))
exit(e.value)
__args = {
'-bm': __benchmark,
'-exp': _exp,
'-r': __regressor,
'-c': __classifier,
'-dataset': __dataset,
'-minb': __min_bits,
'-maxb': __max_bits
}
def handle_args(argv):
"""
Handles the arguments passed as parameters at the start of the program. It can cause the entire program to quit
if any error is encountered (see ArgsError for the possible error types).
:param argv: arguments array. from the starting script, it is the system arguments list slice from the second
argument onward.
:return: an ArgumentHolder if all arguments are legal. If any of them is not, the program quits.
"""
if 0 == len(argv):
print("Some parameters are mandatory. Use -help to see all possible parameter names.")
exit(-1)
if argv[0] == '-help':
s = ''
for a in __args.keys():
s += a + ', '
s = s[:len(s) - 2]
print("Possible parameters: {}".format(s))
exit(0)
for i in range(0, len(argv) - 1, 2):
p = argv[i]
v = None
if not p.startswith('-'):
print("Invalid parameter name {}".format(p))
exit(1)
if i + 1 < len(argv):
v = argv[i + 1]
error, value = __args[p](v)
error_handler(error, p, v)
if not args.is_legal():
print(pu.fatal("Benchmark and error exponent are mandatory"))
exit(1)
return args