forked from gbrammer/unicorn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect.py
executable file
·877 lines (664 loc) · 29.9 KB
/
inspect.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
#!/usr/bin/env python
"""
GUI tool for inspecting grism spectrum extractions
$Date$
$Rev$
Nov 11: For team inspections:
Instructions for working:
- Put gui_3dhst.py in the directory where you plan to work. This is where the output
files will be created. I recommend TEST_SPECTRA_v4.1/
- Open gui_3dhst.py in your favorite text editor.
- Go to line XXX below and edit the paths for RELEASE and RGB_PATH to the correct
location on your disk. Save.
- Open a teminal window.
- If running Ureka, type "ur_setup"
- Navigate to gui_3dhst.py directory.
Open python (type python or ipython in your terminal).
In Python:
import gui_3dhst
### The default behavior will pop out the ds9 window:
gui_3dhst.go_pointing(pointing = 'goodss-05')
### To avoid opening the ds9 window:
gui_3dhst.go_pointing(pointing = 'goodss-05',show_ds9=False)
"""
import sys
import shutil
import os
import glob
import Tkinter as tk
from PIL import ImageTk, Image
import numpy as np
import pyfits
noNewLine = '\x1b[1A\x1b[1M'
def examples():
import glob
#import unicorn.inspect
import pysao
os.chdir(os.getenv('THREEDHST') + '/3DHST_VariableBackgrounds/GS25')
RGB_PATH = os.getenv('RELEASE') + 'v4.0/RGB/All/'
ds9 = pysao.ds9()
#### Objects with fits
x = unicorn.inspect.ImageClassifier(glob.glob('ZFIT/PNG/*zfit.png'), FITS_PATH='2D/FITS/', logfile='inspect_zfit', RGB_PATH=RGB_PATH, ds9=ds9)
#### Raw 2D files, including objects not fit
x = unicorn.inspect.ImageClassifier(glob.glob('2D/PNG/*png'), FITS_PATH='2D/FITS/', logfile='inspect_raw2d', RGB_PATH=RGB_PATH, ds9=ds9)
class TextInput():
"""
Popup for entering a comment
"""
def __init__(self, input='---'):
master = tk.Toplevel()
master.geometry('250x30') # This work fine
self.master = master
self.textbox = tk.Entry(self.master)
self.textbox.delete(0, tk.END)
self.textbox.insert(0, input)
#self.button_quit = tk.Button(self.master, text = 'Quit', command = self.finish)
self.textbox.grid_rowconfigure(0, minsize=240)
self.textbox.grid(row=0, column=0, columnspan=5)
#self.button_quit.grid(row=0, column=5)
self.master.bind("<Return>", self.finish)
self.master.mainloop()
def finish(self, event):
#self.frame.quit()
self.text_value = self.textbox.get()
self.master.destroy()
self.master.quit()
def go_pointing(pointing='goodss-25', RELEASE='/Volumes/Voyager/TEST_SPECTRA_v4.1/GOODS-S/INTERLACE_v4.1/', RGB_PATH='/Volumes/Voyager/TEST_SPECTRA_v4.1/RGB_goodss/',show_ds9=True):
"""
Classify a 3D-HST pointing
"""
import glob
#from unicorn import inspect
field = '-'.join(pointing.split('-')[:-1])
### Example paths for v4.0 and earlier releases
#PNG_PATH = '%s/%s/%s-WFC3_v4.0_SPECTRA/%s/ZFIT/PNG/' %(RELEASE, field, field, pointing)
#FITS_PATH = '%s/%s/%s-WFC3_v4.0_SPECTRA/%s/2D/FITS/' %(RELEASE, field, field, pointing)
#PNG_PATH = '%s/%s/WFC3/%s/ZFIT/PNG/' %(RELEASE, field, pointing)
#FITS_PATH = '%s/%s/WFC3/%s/2D/FITS/' %(RELEASE, field, pointing)
PNG_PATH = FITS_PATH = RELEASE
if show_ds9:
import pysao
ds9=pysao.ds9()
x = ImageClassifier(images=glob.glob(PNG_PATH+pointing+'*new_zfit.png'), RGB_PATH=RGB_PATH, FITS_PATH=FITS_PATH, logfile='%s_inspect.info' %(pointing), ds9=ds9)
else:
x = ImageClassifier(images=glob.glob(PNG_PATH+pointing+'*new_zfit.png'), RGB_PATH=RGB_PATH, FITS_PATH=FITS_PATH, logfile='%s_inspect.info' %(pointing))
return x
class myCheckbutton(object):
"""
Container for CheckButton self + logging variables
"""
def __init__(self, gui, text="(t)ilt", param=[0], hotkey='xxx'):
self.gui = gui
self.param = param
self.hotkey = hotkey
self.var = tk.IntVar()
self.thebutton = tk.Checkbutton(self.gui.frame, text=text, variable=self.var, command=self.set_var)
self.var.set(self.param[0])
def set_var(self):
self.param[self.gui.i] = self.var.get()
#print self.gui.i
class mySlider(object):
"""
Container for Scale slider self + logging variables
"""
def __init__(self, gui, text="(a)bs./break", param=[0], hotkey='xxx', to=3):
self.gui = gui
self.param = param
self.hotkey = hotkey
self.to = to
self.var = tk.IntVar()
self.theslider = tk.Scale(self.gui.frame, to=to, resolution=1, orient=tk.HORIZONTAL, variable=self.var, label=text)
self.var.set(param[0])
def set_var(self):
self.param[self.gui.i] = self.var.get()
class ImageClassifier():
"""
Main classifier tool for 3D-HST fits
"""
def __init__(self, images = ['UDS_54826.zfit.png', 'UDS_55031.zfit.png'], logfile='inspect_3dhst.info', RGB_PATH='./', RGB_EXTENSION='_vJH_6.png', FITS_PATH='./', load_log=True, ds9=None, rgb_lower=True):
"""
GUI tool for inspecting grism redshift fits
x = unicorn.inspect.ImageClassifier(images=glob.glob('Specz/GOODS-S-25*zfit.png'), RGB_PATH=RGB_PATH, FITS_PATH='Specz/')
"""
if len(images) == 0:
print 'No images specified'
return False
if not os.path.exists(images[0]):
print 'First image not found (%s), is path correct?' %(images[0])
return False
self.RGB_PATH = RGB_PATH
self.FITS_PATH = FITS_PATH
self.RGB_EXTENSION = RGB_EXTENSION
self.rgb_lower=rgb_lower
#### Check ds9
self.ds9 = ds9
if ds9 is True:
try:
import pysao
self.ds9 = pysao.ds9()
except:
print 'Couldn\'t import pysao to run DS9'
self.ds9 = None
##### Add .fits to filename and make backup if necessary
self.logfile = logfile
if not self.logfile.lower().endswith('.fits'):
self.logfile += '.fits'
if os.path.exists(self.logfile):
bk = glob.glob(self.logfile+'.backup*')
if len(bk) > 0:
bkup_file = self.logfile + '.backup.%03d' %(len(bk))
else:
bkup_file = self.logfile + '.backup'
shutil.copy(self.logfile, bkup_file)
print 'Made copy of %s -> %s' %(self.logfile, bkup_file)
####### Initialize parameters
self.params = {}
self.images = images
self.marked_lines = None
if os.path.exists(self.logfile) & load_log:
self.read_fits()
self.N = len(self.images)
for key in ['line', 'extended', 'absorption', 'unamb', 'misid', 'contam', 'zp', 'tilt', 'investigate', 'star', 'seen', 'bad2d', 'deblend', 'sed']:
if key not in self.params.keys():
self.params[key] = np.zeros(self.N, dtype=np.int)
if self.marked_lines is None:
self.marked_lines = np.zeros((self.N, 5))
if 'comment' not in self.params.keys():
self.params['comment'] = ['---' for i in range(self.N)]
self.i = 0
####### Initialize GUI
master = tk.Toplevel()
simple_twod = False
if 'zfit.png' in self.images[0]:
master.geometry('1050x630')
elif '2D.png' in self.images[0]:
master.geometry('850x750')
simple_twod = True
else:
master.geometry('1050x630')
self.master = master
self.frame = tk.Frame(master)
self.frame.pack()
#### Image Panels
imageFile = Image.open(self.images[0])
im = ImageTk.PhotoImage(imageFile)
self.panel = tk.Label(self.frame , image=im, cursor='target')
# imageFile2 = Image.open(self.images[0].replace('zfit','zfit.2D')).resize((500,202))
# im2 = ImageTk.PhotoImage(imageFile2)
im2 = ImageTk.PhotoImage(self.get_twod_file(self.images[0]))
self.panel2 = tk.Label(self.frame , image=im2)
# self.panel2.configure(image = im2)
# self.panel2.image = im2
#### RGB Panel
im_rgb = ImageTk.PhotoImage(self.get_rgb_file(self.images[0]))
self.panel_rgb = tk.Label(self.frame , image=im_rgb)
#### Keypress binding
self.master.bind("<Key>", self.keypress_event)
#### Mouse binding - right click
self.master.bind("<Button-2>", self.right_click_event)
### For logging slider movements
#self.sliders = {}
self.sliders = {}
######
### Navigation buttons
###
self.button_quit = tk.Button(self.frame, text = '(q)uit', command = self.finish)
self.button_log = tk.Button(self.frame, text = 'Log to (F)ile', command = self.write_fits)
self.button_prev = tk.Button(self.frame, text = '(p)rev', command = self.img_prev)
self.button_next = tk.Button(self.frame, text = '(n)ext', command = self.img_next)
self.buttons = {}
#### Emission lne?
self.sliders['line'] = mySlider(self, text='Emission (l)ine', param=self.params['line'], hotkey='l')
#### Absorption
self.sliders['absorption'] = mySlider(self, text='(a)bs./break', param=self.params['absorption'], hotkey='a')
#### Extended line
self.sliders['extended'] = mySlider(self, text='(e)xtended', param=self.params['extended'], hotkey='e')
#### Unambiguous redshift
self.buttons['unamb'] = myCheckbutton(self, text='(u)nambiguous', param=self.params['unamb'], hotkey='u')
#### Flag contamination
self.sliders['contam'] = mySlider(self, text='(b)ad contam', param=self.params['contam'], hotkey='b')
#### Line misidentification
self.buttons['misid'] = myCheckbutton(self, text='Line (m)isID', param=self.params['misid'], hotkey='m')
#### Tilt?
self.buttons['tilt'] = myCheckbutton(self, text='(t)ilt', param=self.params['tilt'], hotkey='t')
#### Bad z_phot / z_spec or both
self.buttons['zp'] = myCheckbutton(self, text='Bad (z)spec', param=self.params['zp'], hotkey='z')
#### SED problems
self.sliders['sed'] = mySlider(self, text='Bad SED (k)', param=self.params['sed'], hotkey='k', to=2)
#### Flag for manual investigation
self.sliders['investigate'] = mySlider(self, text='(i)nvestigate', param=self.params['investigate'], hotkey='i', to=4)
### Object seen already?
self.buttons['seen'] = myCheckbutton(self, text='SEEN?', param=self.params['seen'], hotkey='xxx')
#### Star?
self.buttons['star'] = myCheckbutton(self, text='(s)tar', param=self.params['star'], hotkey='s')
#### Spectrum problem
self.buttons['bad2d'] = myCheckbutton(self, text='Bad 1D/2D (x)', param=self.params['bad2d'], hotkey='x')
#### Deblend issue
self.buttons['deblend'] = myCheckbutton(self, text='(d)eblend', param=self.params['deblend'], hotkey='d')
### Comment holder
self.tvar = tk.StringVar()
self.e_comment = tk.Label(self.frame, textvariable=self.tvar)
self.e_comment.configure(relief=tk.RIDGE)
self.tvar.set(self.params['comment'][0])
#####################
##### Set up the grid for the GUI elements
self.button_next.grid(row=1, column=4, columnspan=1)
self.button_prev.grid(row=1, column=5, columnspan=1)
self.button_log.grid(row=2, column=4, columnspan=1)
self.button_quit.grid(row=2, column=5, columnspan=1)
self.buttons['seen'].thebutton.grid(row=3, column=5)
self.buttons['star'].thebutton.grid(row=3, column=4)
self.sliders['line'].theslider.grid(row=4, column=0)
self.sliders['absorption'].theslider.grid(row=5, column=0)
self.sliders['extended'].theslider.grid(row=4, column=1)
self.buttons['unamb'].thebutton.grid(row=5, column=1)
self.sliders['contam'].theslider.grid(row=4, column=2)
self.buttons['misid'].thebutton.grid(row=5, column=2)
self.buttons['tilt'].thebutton.grid(row=4, column=3)
self.buttons['zp'].thebutton.grid(row=5, column=3)
self.sliders['sed'].theslider.grid(row=5, column=4)
self.sliders['investigate'].theslider.grid(row=4, column=5)
self.buttons['bad2d'].thebutton.grid(row=4, column=4)
self.buttons['deblend'].thebutton.grid(row=5, column=5)
self.e_comment.grid(row=6, column=0, columnspan=5)
self.canvas = tk.Canvas(self.frame, background="white", height=5)
self.line_tags = []
self.draw_lines()
if simple_twod:
#self.panel.place(x=0, y=0, relwidth=1, relheight=1)
self.panel.grid(row=0, column=1, rowspan=4, columnspan=3)
#self.canvas.grid(row=0, column=1, rowspan=1, columnspan=3)
self.canvas.place(relx=0, rely=0, relwidth=1)
else:
self.panel.grid(row=0, column=0, columnspan=6)
#self.canvas.grid(row=0, column=0, columnspan=6)
self.canvas.place(relx=0, rely=0, relwidth=1)
self.panel.grid()
#self.canvas.grid(row=0, column=0, columnspan=6)
self.panel2.grid(row=1, column=1, columnspan=3, rowspan=3)
self.panel_rgb.grid(row=1, column=0, columnspan=1, rowspan=3)
self.master.mainloop()
def get_twod_file(self, image_file):
"""
Get the zfit.2D file if appropriate
"""
if ('2D.png' in image_file) | ('_stack.png' in image_file):
imageFile2 = Image.new('RGB', (1,1), "white")
else:
imageFile2 = Image.open(image_file.replace('zfit','zfit.2D')).resize((500,202))
return imageFile2
#im2 = ImageTk.PhotoImage(imageFile2.resize((500,202)))
#self.panel2.configure(image = im2)
#self.panel2.image = im2
def get_rgb_file(self, image_file):
"""
Translate the filename to an RGB thumbnail
"""
rgb_file = image_file
spl = os.path.basename(image_file.replace('new_zfit', 'newzfit'))
if 'SDSS' in image_file:
self.rgb_lower = False
rgb_file = image_file
else:
# if (len(spl) == 2) & ('goods' in spl[0]):
# rgb_file = spl[0]+'_'+spl[1].split('_')[1]
# elif len(spl) > 2:
# rgb_file = ''.join(spl[:2])+'_'+spl[2].split('_')[1]
rgb_file = '%s_%s' %(image_file.split('-')[0], image_file.split('-')[-1].split('_')[1].split('.new')[0])
#rgb_file = image_file
if self.rgb_lower:
rgb_file = rgb_file.lower()
### GOODS-S-25_22461 -> goodss_24461_vJH_6
rgb_file = os.path.join(self.RGB_PATH, rgb_file.split('.new_zfit')[0].split('.zfit')[0].split('_stack')[0].split('.2d.png')[0] + self.RGB_EXTENSION)
if 'stack' in image_file:
rgb_file = image_file.replace('_stack.png', '.thumb.png')
rgb_file = 'RGB/'+image_file.replace('_stack.png', '.rgb.png')
#
rgb_file = 'RGB/'+image_file.replace('.new_zfit.png', '.rgb.png')
rgb_file = image_file.replace('.new_zfit.png', '.thumb.png')
print rgb_file
if not os.path.exists(rgb_file):
im_rgb = Image.new('RGB', (100,100), "white")
else:
if 'thumb' in rgb_file:
im_rgb = Image.open(rgb_file).resize((450,150))
else:
im_rgb = Image.open(rgb_file).resize((150,150))
return im_rgb
def right_click_event(self, event):
"""
Mouse was clicked on a stack spectrum figure
"""
#print "Clicked at %.1f %.1f" %(event.x, event.y)
#oval = self.canvas.create_oval(event.x-5, 10, event.x+5, 0, fill="red", outline="blue", width=1, tags="line tag")
#box = self.canvas.create_rectangle(event.x-5, -10, event.x+5, 10, fill="red", outline="white", width=1, tags="line tag")
#self.canvas.tag_raise(self.panel)
for j in range(5):
if np.abs(self.marked_lines[self.i, j] - event.x) < 8:
self.marked_lines[self.i, j] = 0.
self.draw_lines()
return None
j = 0
while (self.marked_lines[self.i, j] > 0) & (j < 4):
j += 1
self.marked_lines[self.i, j] = event.x*1.
#print j, self.marked_lines[self.i,:]
self.draw_lines()
return None
def draw_lines(self):
self.canvas.delete("line tag")
for j in range(len(self.line_tags)):
p = self.line_tags.pop()
#print 'kill: ', p
self.canvas.delete(p)
for j in range(5):
x = self.marked_lines[self.i, j]
if x > 0:
tag = self.canvas.create_rectangle(x-8, -10, x+8, 10, fill="red", outline="white", width=1, tags="line tag")
#print tag
self.line_tags.append(tag)
def keypress_event(self, event):
key = event.char
#print 'Keyboard: "%s"' %(key)
#self.line_flags[self.i] = (not self.params['line'][self.i])*1
#### Buttons
for id in self.buttons.keys():
if key == self.buttons[id].hotkey:
button = self.buttons[id]
button.param[self.i] = (not button.param[self.i])*1
button.var.set(button.param[self.i])
#print button.param[self.i]
return True
#### Sliders
for id in self.sliders.keys():
if key == self.sliders[id].hotkey:
slider = self.sliders[id]
slider.param[self.i] = ((slider.param[self.i]+1) % (slider.to+1))
slider.var.set(slider.param[self.i])
#print slider.param[self.i]
return True
#### Other keys
if key == 'c':
#comment = raw_input('Comment: ')
ctext = TextInput(self.params['comment'][self.i])
self.tvar.set(ctext.text_value)
self.params['comment'][self.i] = ctext.text_value
#print comment
elif key == '9':
#### Open DS9
#import pysao
#ds9 = pysao.ds9()
if self.ds9 is None:
print 'Need to pass a pysao.ds9 object at initialization'
return False
exts = ['new_zfit.png','_stack.png','2D.png']
twod_file = '%s' %(self.images[self.i])
for ext in exts:
twod_file = twod_file.replace(ext, '2D.fits')
#twod_file = self.images[self.i].replace('zfit.png', '2D.fits')
twod_file = os.path.join(self.FITS_PATH, os.path.basename(twod_file))
im = pyfits.open(twod_file)
self.ds9.frame(1)
self.ds9.view(im['DSCI'].data)
self.ds9.set('scale limits -0.1 1')
self.ds9.set('cmap value 1 0.173403')
self.ds9.frame(2)
self.ds9.view(im['DSEG'].data)
self.ds9.set('scale limits 0 1')
self.ds9.frame(3)
self.ds9.view(im['SCI'].data)
self.ds9.set('scale limits -0.1 1')
self.ds9.set('cmap value 8.06667 0.12255')
self.ds9.frame(4)
self.ds9.view(im['CONTAM'].data)
self.ds9.set('scale limits -0.1 1')
self.ds9.set('cmap value 8.06667 0.12255')
self.ds9.set('tile yes')
elif key == 'n':
### next
self.img_next()
elif key == 'p':
### previous
self.img_prev()
elif key == 'f':
### Force write the output file
self.write_fits()
elif key == 'q':
### quit
self.finish()
elif key == 'N':
### Go to next unseen
self.set_seen()
self.params['comment'][self.i] = self.tvar.get()
while (self.i < self.N-1):
#print self.i
if self.params['seen'][self.i] == 1:
self.i += 1
else:
break
#self.img_next()
self.load_image()
elif key == '0':
### Go to beginning
self.set_seen()
self.params['comment'][self.i] = self.tvar.get()
self.i = 0
self.load_image()
elif key == '!':
### Go until find '!' in a comment
self.set_seen()
self.params['comment'][self.i] = self.tvar.get()
### At end
if self.i > self.N-2:
return False
self.i += 1
while (self.i < self.N-1):
print self.i
if (self.params['seen'][self.i] == 1) & ('!' not in self.params['comment'][self.i]):
self.i += 1
else:
break
self.load_image()
# while (self.params['seen'][self.i] == 1) & ('!' not in self.params['comment'][self.i]):
# self.img_next()
elif key in ['U','L','E','I','A','S','T','B']:
### Go to next non-zero flag for different keys
key_param = {'U':self.params['unamb'], 'L':self.params['line'], 'E':self.params['extended'], 'I':self.params['investigate'], 'A':self.params['absorption'], 'S':self.params['star'], 'T':self.params['tilt'], 'B':self.params['contam']}
self.next_nonzero(key_param[key])
elif key == '?':
print """
Additional keys:
'c': Open comment box, type <tab> to edit and <enter> when done.
'9': Open 2D FITS extensions in DS9, if pysao available.
'N': skip through through all 'seen'
'0': go back to first
'!': go to next object with '!' in the comment
'y': go to next unambigous
'?': this message
"""
else:
print 'Hotkey (%s) not bound.' %(key)
def next_nonzero(self, param=None):
"""
Go to next object where self.param is nonzero
"""
self.set_seen()
self.params['comment'][self.i] = self.tvar.get()
### At end
if self.i > self.N-2:
return False
self.i += 1
while (self.i < self.N-1):
print self.i
if (self.params['seen'][self.i] == 1) & (param[self.i] == 0):
self.i += 1
else:
break
self.load_image()
def set_seen(self):
#print 'Seen flag: %d' %(self.svar.get())
self.params['seen'][self.i] = 1
def img_next(self):
self.set_seen()
self.params['comment'][self.i] = self.tvar.get()
if self.i == self.N-1:
print 'Already at last image'
return False
self.i += 1
self.load_image()
self.draw_lines()
return True
#
def img_prev(self):
self.set_seen()
self.params['comment'][self.i] = self.tvar.get() #self.e_comment.get()
if self.i == 0:
print 'Already at first image'
return False
self.i -= 1
self.load_image()
self.draw_lines()
return True
def load_image(self):
#print '%d %s' %(self.i, self.images[self.i])
#print '%d %d %d' %(self.params['line'][self.i], self.params['unamb'][self.i], self.params['star'][self.i])
print '%s: %d of %d' %(self.images[self.i], self.i+1, self.N)
for key in self.buttons.keys():
button = self.buttons[key]
button.var.set(button.param[self.i])
for key in self.sliders.keys():
slider = self.sliders[key]
slider.var.set(slider.param[self.i])
#self.e_comment.delete(0, tk.END)
#self.e_comment.insert(0, self.params['comment'][self.i])
self.tvar.set(self.params['comment'][self.i])
imageFile = Image.open(self.images[self.i])
im = ImageTk.PhotoImage(imageFile)
self.panel.configure(image = im)
self.panel.image = im
#imageFile2 = Image.open(self.images[self.i].replace('zfit','zfit.2D'))
im2 = ImageTk.PhotoImage(self.get_twod_file(self.images[self.i]))
self.panel2.configure(image = im2)
self.panel2.image = im2
### RGB
im_rgb = ImageTk.PhotoImage(self.get_rgb_file(self.images[self.i]))
self.panel_rgb.configure(image = im_rgb)
self.panel_rgb.image = im_rgb
#self.panel_rgb = tk.Label(self.frame , image=im_rgb)
def write_fits(self):
"""
Write the FITS log
"""
import time
import getpass
formats = {}
formats['bool'] = 'L'
formats['int16'] = 'I'
formats['int32'] = 'J'
formats['int64'] = 'K'
formats['float32'] = 'E'
formats['float64'] = 'D'
formats['>i8'] = 'K'
formats['>f8'] = 'D'
#### Make the table columns, translating numpy data types to "TFORM"
coldefs = []
TFORM = 'A'+str(np.array(self.images).dtype).split('S')[1]
coldefs.append(pyfits.Column(name='images', array=np.array(self.images), format=TFORM))
for column in self.params.keys():
if column == 'comment':
coldata = np.array(self.params['comment'])
else:
coldata = self.params[column]
#
dtype = str(coldata.dtype)
#print column, dtype
if dtype in formats.keys():
TFORM=formats[dtype]
else:
if 'S' not in dtype:
print 'Unrecognized data type in: %s' %(dtype)
return False
#
TFORM = 'A'+dtype.split('S')[1]
#
#data = self.params[column]
if '>' in dtype:
cast_types = {'>i8':np.int64, '>f8':np.float64}
coldata = np.cast[cast_types[dtype]](coldata)
#
coldefs.append(pyfits.Column(name=column, array=coldata, format=TFORM))
#### Done, now make the binary table
if pyfits.__version__ < '3.3':
tbhdu = pyfits.new_table(coldefs)
else:
tbhdu = pyfits.BinTableHDU().from_columns(coldefs)
linehdu = pyfits.ImageHDU(data=self.marked_lines, name='LINELIST')
#### Primary HDU
hdu = pyfits.PrimaryHDU()
thdulist = pyfits.HDUList([hdu, tbhdu, linehdu])
#### Add modification time of "infile" to FITS header
infile_mod_time = time.strftime("%m/%d/%Y %I:%M:%S %p",
time.localtime()) # os.path.getmtime(self.filename)))
if pyfits.__version__ < '3.3':
thdulist[0].header.update('MODTIME', infile_mod_time)
thdulist[0].header.update('USER', getpass.getuser())
else:
thdulist[0].header['MODTIME'] = infile_mod_time
thdulist[0].header['USER'] = getpass.getuser()
thdulist.writeto(self.logfile, clobber=True)
print 'Log to file %s' %(self.logfile)
def read_fits(self):
"""
Read already saved output
"""
im = pyfits.open(self.logfile)
if len(im) == 3:
try:
self.marked_lines = im['LINELIST'].data
except:
self.marked_lines = None
#
tab = im[1].data
print "Read log %s from %s (%s)" %(self.logfile, im[0].header['USER'], im[0].header['MODTIME'])
colnames = tab.columns.names
try:
#### If images in the input list aren't in the file that
#### was read, add them to the table
from astropy.table import Table
tab = Table.read(self.logfile)
colnames = tab.columns
read_images = tab['images']
if self.marked_lines is None:
self.marked_lines = np.zeros((len(tab), 5))
Nadd = 0
for image in self.images:
if image not in read_images:
Nadd += 1
tab.add_row()
tab['images'][-1] = image
tab['comment'][-1] = '---'
if Nadd > 0:
self.marked_lines = np.append(self.marked_lines, np.zeros((Nadd, 5)), axis=0)
except:
#### No astropy?
print 'No astropy found. Forcing image list from %s.' %(self.logfile)
pass
self.images = tab['images']
for c in colnames:
#print c
if c == 'images':
continue
if c == 'comment':
self.params[c] = list(tab[c])
continue
#
self.params[c] = tab[c]
def finish(self):
#self.frame.quit()
#self.write_log()
self.write_fits()
self.master.destroy()
self.master.quit()
if __name__ == "__main__":
files = sys.argv[1:]
widget = ImageClassifier(images=files)