-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py~
476 lines (380 loc) · 16 KB
/
ui.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# -*- coding: utf-8 -*-
"""
stellarPYL - python stellar spectra processing software
Copyright (c) 2015 Brunston Poon
@file: ui
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Full license in LICENCE.txt
"""
#IMPORT IMPORT IMPORT IMPORT IMPORT IMPORT IMPORT IMPORT IMPORT IMPORT IMPORT
#Let's make sure that the user has all the dependencies installed and that
#they are running the correct version of Python
import sys
import configparser
import time
toggle = True
version = sys.version_info[0]
if version != 3:
print("""
Please upgrade to Python3, preferably 3.4.* or greater, before continuing""")
toggle = False
sys.exit()
try:
import numpy as np
from PIL import Image
from matplotlib import pyplot
except ImportError:
print("""
numpy, PIL, and/or matplotlib are not installed.
Please install before continuing.""")
toggle = False
sys.exit()
#import the rest
import stellar as st
import tools as to
import text as txt
#TEMPORARY
import samplingsample as ss
#END IMPORT END IMPORT END IMPORT END IMPORT END IMPORT END IMPORT END IMPORT
if toggle == True:
config = configparser.ConfigParser()
config.read('settings.ini')
firstrun = config['CONTROL']['firstrun']
txt.welcome()
if firstrun in ["yes"]:
txt.firstrun()
userInput = input(">> ")
txt.commands()
config['CONTROL']['firstrun'] = "no"
with open('settings.ini', 'w') as cfile:
config.write(cfile)
while toggle == True:
#update settings
config.read('settings.ini')
defThresh = config['CONTROL']['defaultthreshold']
autoIntensity = config['CONTROL']['autointensity']
top = int(config['CONTROL']['manualtop'])
bottom = int(config['CONTROL']['manualbot'])
right = int(config['CONTROL']['manualright'])
left = int(config['CONTROL']['manualleft'])
step = float(config['CONTROL']['r'])
verbose = config['CONTROL']['verbose']
showthresh = config['CONTROL']['showthresh']
margin = int(config['CONTROL']['margin'])
prgmCommands = ("pixel_d", "image_regression", \
"intensity_n", "intensity_saa", \
"pd", "imgreg", "saa", "n", "crop", "show_threshold",\
"show_regression", "show_walks", "dev_cgrowth", "saaw")
#REMOVE DEV_CGROWTH AND SAAW
userInput = input(">> ")
#TEXT COMMANDS NEXT
if userInput in ["about"]:
txt.about()
if userInput in ["help"]:
txt.help()
if userInput in ["commands"]:
txt.commands()
if userInput in ["licence", "license"]:
txt.licence()
#AUTOPROCESS
if userInput in ['autoProcess', 'auto']:
print("""
We need a file. Place it in the same directory as this script and give the name.
""")
path = input("enter filename> ")
timeStart = time.time()
threshI = int(defThresh)
if threshI >= 0:
print("converting. please wait...")
img = Image.open(path)
dataArray = to.converter(path)
timePause0 = time.time()
if showthresh == "yes":
to.showThreshold(dataArray, threshI)
#TESTFORGEOFFCODE
# test = Image.open("cropped.tiff")
# tdataArray = to.converter("cropped.tiff")
# tregTup = st.regression(test)
# intensity = st.intensityWHERE(test,tdataArray,tregTup,\
# threshI,step)
timePause0s = time.time()
print("working on crop. please wait...")
cropped = st.cropN(img, threshI, top, bottom, left, right, margin)
to.restorer(cropped, 'cropped')
print("cropped image saved to cropped.tiff")
croppedimg = Image.open('cropped.tiff')
print("converting cropped image. please wait...")
dataArray = to.converter('cropped.tiff')
regTup = st.regression(croppedimg)
timePause1 = time.time()
to.showRegression(croppedimg,regTup)
timePause1s = time.time()
if autoIntensity in ['saa']:
print("working on intensity_saa. please wait...")
intensity = st.intensitySAAN(croppedimg,dataArray,regTup,\
threshI,step)
timePause2 = time.time()
to.plotIntensity(intensity)
timePause2s = time.time()
if autoIntensity in ['saanb']:
print("working on intensity_saanb. please wait...")
sys.stdout = open("foo.log", "w")
intensity = st.intensitySAANB(croppedimg,dataArray,regTup,\
threshI,step,10)
timePause2 = time.time()
to.plotIntensity(intensity)
timePause2s = time.time()
sys.stdout = sys.__stdout__
if autoIntensity in ['saans']:
print("working on intensity_saans. please wait...")
sys.stdout = open("foo.log", "w")
intensity = st.intensitySAANS(croppedimg,dataArray,regTup,\
threshI,step,10)
sys.stdout = sys.__stdout__
if autoIntensity in ['saaw']:
print("working on intensity_saaw. please wait...")
#sys.stdout = open("foo.log", "w")
# np.set_printoptions(suppress=True,threshold=65536)
#TEMPORARY
# intensity = ss.intensityWHERE(croppedimg,dataArray,regTup,\
# threshI,step,10)
intensity = st.intensitySAAW(croppedimg,dataArray,regTup,\
threshI,step,10)
timePause2 = time.time()
to.plotIntensityW(intensity)
to.plotSamples(croppedimg,intensity,regTup)
timePause2s = time.time()
#TEMPORARY
# np.set_printoptions(edgeitems=3,infstr='inf',\
# linewidth=75,nanstr='nan',precision=8,\
# suppress=False,threshold=1000,formatter=None)
#sys.stdout = sys.__stdout__
if autoIntensity in ['n']:
print("working on intensity_n. please wait...")
intensity = st.intensityN(croppedimg,dataArray,regTup,\
threshI,step)
timePause2 = time.time()
to.plotIntensity(intensity)
timePause2s = time.time()
timeEnd = time.time()
print("Total time required:", timeEnd-(timePause0s-timePause0)\
-(timePause1s-timePause1)\
-(timePause2s-timePause2)-timeStart)
else:
print("defaultThreshold not set. aborting.")
#HOUSEKEEPING COMMANDS NEXT
if userInput in ["settings_default"]:
while True:
print("ARE YOU SURE YOU WANT TO RESET SETTINGS? CANNOT BE UNDONE!")
query = input("type 'yes'/'no'> ")
if query in ['yes']:
to.configDefault()
print("Set settings to default.")
break
elif query in ['no']:
print("Keeping settings as is.")
break
else:
print("please type 'yes' or 'no'")
if userInput in ["settings_intensity"]:
print("""
sets default intensity processing method for the autoProcess feature.
AVAILABLE OPTIONS:
'saa' (spatial anti-aliasing),
'n' (naive),
'saanb',
'saans' (dev, do not use),
'saaw' (width-adjustable).
Default is 'saa'.
""")
query = input("Set default autoProcess intensity> ")
if query in ['saa', 'n', 'saanb','saans','saaw']:
config['CONTROL']['autointensity'] = query
with open('settings.ini', 'w') as cfile:
config.write(cfile)
print("Set new setting of: ",query)
else:
print("not an acceptable value. no value set.")
if userInput in ["settings_threshold"]:
print("""
sets a default threshold for any function of this program requiring a threshold.
If you would like the program to ask each time, set threshold as -1.
Else, set as an integer between 0 and 765. Defaults to -1 (ask every time)
""")
query = input("Set default threshold> ")
if (int(query) >= -1) and (int(query) <= 765):
config['CONTROL']['defaultthreshold'] = query
with open('settings.ini', 'w') as cfile:
config.write(cfile)
print("Set new setting of: ",query)
else:
print("not an acceptable value. no value set.")
if userInput in ['settings_step']:
print("""
sets default step value along the spectral trace (and thus resolution of
resulting intensity plot). default is 1 pixel-equivalence.
""")
query = input("Set step value> ")
if float(query) > 0:
config['CONTROL']['r'] = query
with open('settings.ini', 'w') as cfile:
config.write(cfile)
print("Set new setting of: ", query)
else:
print("value must be greater than zero. no value set.")
if userInput in ["settings_cropoverride"]:
print("""
sets manual overrides for automatic cropping on the top, bottom, and sides
of an image. The default value is -1 (which is equivalent to no override)
for all values.
""")
query = input("value to set (integer)?> ")
value = query
print("Answer using 'top','bottom','left', or 'right'.")
query = input("Set override for which side?> ")
if query in ['top']:
config['CONTROL']['manualtop'] = value
with open('settings.ini', 'w') as cfile:
config.write(cfile)
print("Set new setting of: ",value, "to ", query)
elif query in ['bottom']:
config['CONTROL']['manualbot'] = value
with open('settings.ini', 'w') as cfile:
config.write(cfile)
print("Set new setting of: ",value, "to ", query)
elif query in ['left']:
config['CONTROL']['manualleft'] = value
with open('settings.ini', 'w') as cfile:
config.write(cfile)
print("Set new setting of: ",value, "to ", query)
elif query in ['right']:
config['CONTROL']['manualright'] = value
with open('settings.ini', 'w') as cfile:
config.write(cfile)
print("Set new setting of: ",value, "to ", query)
else:
print("not an understood side name. no value set.")
if userInput in ['settings_verbose']:
print("""
sets verbose printing of debug statements. default is 'no'
""")
query = input("Set verbose 'yes'/'no'> ")
if query in ['yes', 'no']:
config['CONTROL']['verbose'] = query
with open('settings.ini', 'w') as cfile:
config.write(cfile)
print("Set new setting of: ", query)
else:
print("not an acceptable value. no value set.")
if userInput in ['settings_showthreshold']:
print("""
showThreshold takes a while to run. Set to 'no' for a faster autoProcess
run time. Default is 'yes'
""")
query = input("Set verbose 'yes'/'no'> ")
if query in ['yes', 'no']:
config['CONTROL']['showthresh'] = query
with open('settings.ini', 'w') as cfile:
config.write(cfile)
print("Set new setting of: ", query)
else:
print("not an acceptable value. no value set.")
if userInput in ['settings_margin']:
print("""
sets margin for cropping. default margin is 5 pixels
""")
query = input("Set margin (integer)> ")
if int(query) >= 0:
config['CONTROL']['margin'] = query
with open('settings.ini', 'w') as cfile:
config.write(cfile)
print("Set new setting of: ", query)
else:
print("not an acceptable value. no value set.")
if userInput in ['view_settings']:
txt.viewSettings(config)
#PROGRAM COMMANDS NEXT
if userInput in prgmCommands:
print("""
We need a file. Place it in the same directory as this script and give the name.
""")
path = input("enter filename> ")
print("converting. please wait...")
img = Image.open(path)
dataArray = to.converter(path)
if defThresh in ['-1']:
print("""
Your answer to the following depends for all commands except 'pixel_d'.
What threshold would you like to use as differentiator?
""")
thresh = input("enter threshold> ")
threshI = int(thresh)
else:
threshI = int(defThresh)
if userInput in ["intensity_saa", "saa"]:
print("working on intensity_saa. please wait...")
regTup = st.regression(img)
intensity = st.intensitySAAN(img,dataArray,regTup,threshI,step)
to.plotIntensity(intensity)
#TEMPORARY, need to document
if userInput in ["saaw"]:
print("working on intensity_saaw. please wait...")
regTup = st.regression(img)
# sys.stdout = open("foo.log", "w")
# np.set_printoptions(suppress=True,threshold=65536)
#TEMPORARY
# intensity = ss.intensityWHERE(croppedimg,dataArray,regTup,\
# threshI,step,10)
intensity = st.intensitySAAW(img,dataArray,regTup,\
threshI,step,10, plot=True)
timePause2 = time.time()
to.plotIntensityW(intensity)
timePause2s = time.time()
#TEMPORARY
# np.set_printoptions(edgeitems=3,infstr='inf',\
# linewidth=75,nanstr='nan',precision=8,\
# suppress=False,threshold=1000,formatter=None)
# sys.stdout = sys.__stdout__
if userInput in ["intensity_n", "n"]:
print("working on intensity_n. please wait...")
regTup = st.regression(img)
intensity = st.intensityN(img,dataArray,regTup,threshI,step)
to.plotIntensity(intensity)
if userInput in ["image_regression", "imgreg"]:
print("working on image_regression. please wait...")
regTup = st.regression(img)
to.plotRegression(regTup)
if userInput in ["pixel_d", "pd"]:
print("working on pixel_distribution. please wait...")
to.pixelDistribution(dataArray)
if userInput in ["crop"]:
print("working on crop. please wait...")
cropped = st.cropN(img, threshI, top, bottom, left, right, margin)
filename = input("filename for cropped? DO NOT ADD EXTENSION> ")
to.restorer(cropped, filename)
print("file has been created at: ", filename + ".tiff")
if userInput in ["show_threshold"]:
print("working on show_threshold")
to.showThreshold(dataArray, threshI)
if userInput in ["show_regression"]:
print("working on show_regression")
regTup = st.regression(img)
to.showRegression(img,regTup)
if userInput in ["show_walks"]:
print("working on show_walks")
regTup = st.regression(img)
to.showWalks(img,regTup,r=step)
if userInput in ["dev_cgrowth"]:
print("devmode: curve of growth")
regTup = st.regression(img)
intensity = st.intensitySAAN(img,dataArray,regTup,threshI,step)
#rehashing of command lists
txt.rehash()
elif userInput in ["quit", "exit"]:
sys.exit()
elif userInput in ["jellyfish"]:
txt.jellyfish()
else:
print("Please enter a command.")