-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
337 lines (313 loc) · 12.5 KB
/
main.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from data import PerfusionDataSet, NoDataError
from models import home_SGD, home_PA
from controls import print_controls, ctrls
import sys
import numpy as np
home_options = [
'Evaluate perfusion parameter.',
'Plot from existing results.',
'Exit program.'
]
def evaluate_param():
controls = ctrls()
home = True
while True:
if home:
print '#################################'
print '## Parameter Evaluation - Home ##'
print '#################################'
home = False
###############################################################
# Load data
###############################################################
p_data = PerfusionDataSet()
### Enter perfusion parameter ###
perfusion_params = p_data.perfusion_params
param_val = None
param_val_range = [i for i in xrange(len(perfusion_params))]
while True:
print 'Enter which perfusion parameter to evaluate.'
for i, p in enumerate(perfusion_params):
print str(i) + ': ' + p
param_val = raw_input('Enter value: ')
if param_val == controls['Skip']:
print 'Perfusion parameter is required.'
elif param_val == controls['Quit']:
return
elif param_val == controls['Help']:
print_controls()
elif param_val == controls['Home']:
home = True
break
else:
try:
param_val = int(param_val)
if param_val not in param_val_range:
print ('Invalid value. Enter the value corresponding '
'to the parameter name.')
else:
break
except:
print ('Invalid value. Enter the value corresponding to '
'the parameter name.')
if home == True:
continue
param_name = perfusion_params[param_val]
### Enter patch radius ###
patch_rad = None
patch_rad_range = p_data.patch_radiuses
while True:
patch_rad = raw_input('Enter patch radius (0-6): ')
if patch_rad == controls['Skip']:
print 'Patch radius is required.'
elif patch_rad == controls['Quit']:
return
elif patch_rad == controls['Help']:
print_controls()
elif patch_rad == controls['Home']:
home = True
break
else:
try:
patch_rad = int(patch_rad)
if patch_rad not in patch_rad_range:
print 'Invalid patch radius. Try again.'
else:
break
except:
print 'Invalid patch radius. Try again.'
if home == True:
continue
print 'Loading data...',
sys.stdout.flush()
p_data.load(
perfusion_params[param_val],
patch_rad,
PerfusionDataSet.DataType.TRAIN,
)
p_data.load(
perfusion_params[param_val],
patch_rad,
PerfusionDataSet.DataType.TEST,
)
print 'Done'
if home == True:
continue
print
###############################################################
# Visualize data
###############################################################
plots = {
'Tissue and AIF Curve' : 'ctc',
'Tissue Curve' : 'tissue_curve',
'Arterial Input Function (AIF)' : 'aif',
'2D Slices' : '2d-slices',
'Distribution of ' + param_name : 'label_distribution',
'Scatter Matrix' : 'scatter_matrix',
'Statistics' : 'stats'
}
while True:
visualize = raw_input('Visualize data? [Y/n] ')
if visualize == controls['Skip'] or visualize == 'n':
break
elif visualize == controls['Quit']:
return
elif visualize == controls['Help']:
print_controls()
elif visualize == controls['Home']:
home = True
break
elif visualize == 'Y':
for name, f_str in plots.iteritems():
display = raw_input('Display ' + name + '? [Y/n] ')
if display == 'n' or display == controls['Skip']:
continue
elif display == controls['Quit']:
return
elif display == controls['Help']:
print_controls()
elif display == controls['Home']:
home = True
break
elif display == 'Y':
dts = {
1 : PerfusionDataSet.DataType.TRAIN,
2 : PerfusionDataSet.DataType.VALIDATION,
3 : PerfusionDataSet.DataType.TEST
}
while True:
print 'Which data to display ' + name + ' for?'
print '1: Training'
print '2: Validation'
print '3: Test'
data_type = raw_input('Enter value: ')
if data_type == controls['Quit']:
return
elif data_type == controls['Help']:
print_controls()
elif data_type == controls['Home']:
home = True
break
else:
try:
data_type = int(data_type)
if data_type not in dts.keys():
print ('Invalid value. Enter the value'
' corresponding to the data '
'type.')
else:
p_data.plot(f_str, dts[data_type])
while True:
again = raw_input('Display ' +
name + ' for another type of '
'data? [Y/n] ')
if again == controls['Skip'] or \
again == 'n':
again = False
break
elif again == controls['Quit']:
return
elif again == controls['Help']:
print_controls()
elif again == controls['Home']:
home = True
break
elif again == 'Y':
again = True
break
else:
print ('Invalid response. '
'Try again.')
except ValueError:
print ('Invalid value. Enter the value '
'corresponding to the data type.')
again = True
except NoDataError:
print ('No data loaded to display. '
'Try another type.')
again = True
if not again or home == True:
break
if home == True:
break
else:
print 'Invalid response. Try again.'
break
else:
print 'Invalid response. Try again.'
if home == True:
continue
print
###############################################################
# Run models
###############################################################
### Stochastic Gradient Descent Regressor ###
while True:
exe = raw_input('Run Stochastic Gradient Descent? [Y/n] ')
if exe == 'n' or exe == controls['Skip']:
break
elif exe == controls['Quit']:
return
elif exe == controls['Help']:
print_controls()
elif exe == controls['Home']:
home = True
break
elif exe == 'Y':
kwargs = {
'parameter' : param_name,
'patch_radius' : patch_rad
}
home_SGD(p_data.X, p_data.y, **kwargs)
break
else:
print 'Invalid response. Try again.'
if home == True:
continue
print
### Passive-Aggressive Regressor ###
while True:
exe = raw_input('Run Passive-Aggressive (PA)? [Y/n] ')
if exe == 'n' or exe == controls['Skip']:
break
elif exe == controls['Quit']:
return
elif exe == controls['Help']:
print_controls()
elif exe == controls['Home']:
home = True
break
elif exe == 'Y':
kwargs = {
'parameter' : param_name,
'patch_radius' : patch_rad
}
home_PA(p_data.X, p_data.y, **kwargs)
break
else:
print 'Invalid response. Try again.'
print
while True:
resp = raw_input('Run on another data set? [Y/n] ')
if resp == controls['Quit'] or resp == 'n' or \
resp == controls['Skip']:
return
elif resp == controls['Help']:
print_controls()
elif resp == controls['Home']:
home = True
break
elif resp == 'Y':
break
else:
print 'Invalid response. Try again.'
def plot():
print 'Plot not implemented yet. Returning home.'
def main():
print 'Welcome to Lesion Growth Predictor!'
print_controls()
controls = ctrls()
suc = True
while True:
if suc:
print '#################'
print '## MAIN - Home ##'
print '#################'
print 'What would you like to do?'
for i, op in enumerate(home_options):
print str(i + 1) + ': ' + op
home_op = raw_input('Enter value: ')
if home_op == controls['Skip']:
print ('Unable to skip. Press ' + controls['Quit'] +
' to exit program.')
suc = False
elif home_op == controls['Quit']:
print 'Exiting program.'
sys.exit()
elif home_op == controls['Help']:
print_controls()
suc = False
elif home_op == controls['Home']:
suc = False
else:
try:
home_op = int(home_op)
if home_op not in xrange(1, len(home_options) + 1):
print 'Invalid value. Try again.'
else:
print
if home_op == 1:
evaluate_param()
suc = True
elif home_op == 2:
plot()
suc = True
elif home_op == 3:
print 'Exiting program.'
sys.exit()
print
except ValueError:
suc = False
print 'Invalid value.'
if __name__ == "__main__" :
main()