-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.py
More file actions
229 lines (187 loc) · 8.67 KB
/
validation.py
File metadata and controls
229 lines (187 loc) · 8.67 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
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
import sys
from os import path
# TODO: eventually replace conrad submodule with installed conrad?
sys.path.append(path.join(path.abspath(path.dirname(__file__)),'conrad'))
from conrad.defs import SOLVER_OPTIONS, MAX_VERBOSITY
class InputValidator(object):
def __init__(self, case):
self.case = case
self.SOLVER_OPTIONS = SOLVER_OPTIONS
self.MAX_VERBOSITY = MAX_VERBOSITY
@staticmethod
def __str_is_float(string):
return string.replace('.', '').replace(
'e-', '').replace('e+', '').isdigit()
@staticmethod
def __boolable(argument):
out = isinstance(argument, (int, bool))
if isinstance(argument, str):
out |= argument in ('true', 'True', 'false', 'False')
out |= argument.isdigit()
return out
@staticmethod
def __intable(argument):
out = isinstance(argument, (int, bool, float))
if isinstance(argument, str):
out |= argument.isdigit()
return out
def validate_structure_label(self, label):
out = {'valid': True, 'message': ''}
try:
if label is None:
out['message'] += 'no label provided\n'
elif isinstance(label, float):
label = int(label)
elif isinstance(label, str):
if not label.isdigit:
out['message'] += 'label is not integer\n'
else:
label = int(label)
if not label in self.case.structures:
out['message'] += 'label does not match a structure in case\n'
# no message -> valid
if out['message'] == '': return int(label)
else: return out
except:
out['valid'] = False
out['message'] += 'exception occurred while parsing inputs\n'
return out
def validate_constraintID(self, cid):
return cid in self.case.active_constraint_IDs
def validate_dvh_constraint(self, dose, percentile, fraction, direction):
out = {'valid': True, 'message': ''}
directions = ['<', '<=', '>', '>=']
try:
if dose is None and percentile is None and fraction is None and direction is None:
out['message'] += 'No changes specified'
if dose is not None:
if isinstance(dose, (int, float)):
dose = float(dose)
elif self.__str_is_float(dose):
dose = float(dose)
else:
dose = None
out['message'] += 'argument "dose" must be a float or None/null\n'
if dose is not None:
if dose < 0:
out['message'] += 'argument "dose" must be in range [0, +inf)\n'
if percentile is not None:
if isinstance(percentile, (int, float)):
fraction = percentile / 100.
elif self.__str_is_float(percentile):
percentile = float(percentile)
fraction = percentile / 100.
else:
fraction = None
out['message'] += 'argument "percentile" must be a float or None/null\n'
if percentile is not None:
if percentile < 0 or percentile > 100:
out['message'] += 'argument "percentile" must be in range [0, 100]\n'
elif fraction is not None:
if isinstance(fraction, (int, float)):
fraction = float(fraction)
elif self.__str_is_float(fraction):
fraction = float(fraction)
else:
fraction = None
out['message'] += 'argument "fraction" must be a float or None/null\n'
if fraction is not None:
if fraction < 0 or fraction > 1:
out['message'] += 'argument "fraction" must be in range [0, 1]\n'
if direction is not None:
if direction not in directions:
out['message'] += 'argument "direction must be one of {} or None/null\n'.format(directions)
else:
direction = direction.replace('=', '')
out['valid'] = out['message'] == ''
if out['valid']:
out['dose'] = dose
out['fraction'] = fraction
out['direction'] = direction
return out
except:
out['valid'] = False
out['message'] += 'exception occurred while parsing inputs\n'
return out
def validate_objective(self, label, dose, w_under, w_over):
out = {'valid': True, 'message': ''}
try:
if not label in self.case.structures:
out['message'] += 'label does not match a structure in case\n'
if dose is None and w_under is None and w_over is None:
out['message'] += 'No changes specified\n'
elif not self.case.structures[label].is_target and w_over is None:
out['message'] += str('No changes specified: label corresponds'
' to non-target structure, argument "w_over" is None')
if dose is not None:
if isinstance(dose, (int, float)):
dose = float(dose)
elif self.__str_is_float(dose):
dose = float(dose)
else:
dose = None
out['message'] += 'argument "dose" must be a float or None/null\n'
if dose is not None:
if dose < 0:
out['message'] += 'argument "dose" must be in range [0, +inf)\n'
if w_under is not None:
if isinstance(w_under, (int, float)):
w_under = float(w_under)
elif self.__str_is_float(w_under):
w_under = float(w_under)
else:
w_under = None
out['message'] += 'argument "w_under" must be a float or None/null\n'
if w_under is not None:
if w_under < 0:
out['message'] += 'argument "w_under" must be in range [0, +inf)\n'
if w_over is not None:
if isinstance(w_over, (int, float)):
w_over = float(w_over)
elif self.__str_is_float(w_over):
w_over = float(w_over)
else:
w_over = None
out['message'] += 'argument "w_over" must be a float or None/null\n'
if w_over is not None:
if w_over < 0:
out['message'] += 'argument "w_over" must be in range [0, +inf)\n'
out['valid'] = out['message'] == ''
if out['valid']:
out['dose'] = dose
out['w_under'] = w_under
out['w_over'] = w_over
return out
except:
out['valid'] = False
out['message'] += 'exception occurred while parsing inputs\n'
return out
def validate_solve(self, use_2pass, use_slack, solver, verbose):
out = {'valid': True, 'message': ''}
try:
if not self.__boolable(use_2pass):
out['message'] += 'argument "use_2pass" must be convertable to bool\n'
else:
use_2pass = bool(use_2pass)
if not self.__boolable(use_slack):
out['message'] += 'argument "use_slack" must be convertable to bool\n'
else:
use_slack = bool(use_slack)
if not solver in self.SOLVER_OPTIONS:
out['message'] += 'argument "solver" must be one of: {}\n'.format(SOLVER_OPTIONS)
if verbose in ('true', 'True'): verbose = 1
elif verbose in ('false', 'False'): verbose = 0
if not self.__intable(verbose):
out['message'] += 'argument "verbose" must be convertable to int\n'
verbose = min(int(float(verbose)), self.MAX_VERBOSITY)
out['valid'] = out['message'] == ''
if out['valid']:
out['use_2pass'] = use_2pass
out['use_slack'] = use_slack
out['solver'] = solver
out['verbose'] = verbose
return out
except:
out['valid'] = False
out['message'] += 'exception occurred while parsing inputs\n'
return out