-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdmag.py
More file actions
executable file
·1186 lines (866 loc) · 36.5 KB
/
cmdmag.py
File metadata and controls
executable file
·1186 lines (866 loc) · 36.5 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
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
'''
Toy program to demo a query of the Data Lab TAP service and visualize
the object density and associated CMD. The user moves the cursor in the
left-hand plot, the smaller plots track to show the catalog points zoomed
in around the cursor, and the g-r vs g CMD for the points around the cursor,
and a histogram of the differential values.
A reticle in the zoomed position plot shows the size of the sampling cursor
being used. A mouseover in the CMD plot will show the corresponding point
in the density plot with a highlight marker. Holding the Shift key down
will suspend tracking to allow you to move the cursor to another window
without changing the zoom/CMD display.
A box may be dragged out in the CMD plot to select points corresponding to
specific color/magnitude region, the density plot will automatically be
updated. Similarly, the contrast of the density plot can be adjusted by
selecting a horizontal region of the histogram plot, e.g. to show only the
values 3-sigma above the mean drag the cursor between zero (the difference
from the mean) and 3 (3-sigma above the mean).
Command-line options:
-h, --help # Help
-d, --debug # Debug
-v, --verbose # Verbose
-c, --catalog # Catalog to query
-l, --load # Load the FITS bintable
-p, --ra # Set RA position
-r, --dec # Set DEC position
-s, --sz # Set field size
-S, --small_k # Set small kernel size
-B, --big_k # Set big kernel size
-F, --floor # Set clipping floor (sigma)
-C, --ceiling # Set clipping ceiling (sigma)
-R, --reticle # Set cursor size
-Z, --zoomsz # Set size of zoom box
Density-plot Commands:
+ # zoom in on field
- # zoom out on field
h / left-arrow # pan left (get new data)
j / down-arrow # pan down (get new data)
k / up-arrow # pan up (get new data)
l / right-arrow # pan right (get new data)
Shift # freeze motion tracking in density plot
Shift-release # enable motion tracking in density plot
Zoom-plot Commands:
< / > # change sample cursor by a factor of 2
, / . # change sample cursor by a factor of 1.5
+ # increase zoom factor
- # decrease zoom factor
Additional work to pre-fetch/cache data, or use of different density tools
will help speed and precision but is TBD.
MJF (2/15/16)
'''
import matplotlib.pyplot as plt
import numpy as np
from gavo import votable
from lxml import etree
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText
from matplotlib.widgets import Button, RadioButtons, CheckButtons
from matplotlib.widgets import Slider
from matplotlib.widgets import Cursor
from matplotlib.widgets import RectangleSelector
from matplotlib.widgets import SpanSelector
from astropy import convolution
from astropy.io import fits
from astropy import convolution
from astropy.table import Table
import matplotlib.patches as patches
import mpld3
import sys, time, math, getopt
################################################
# Some interesting initial positions
################################################
xpos = 202.75655
ypos = -8.09553
xpos = 34.81666667 # Segue-2
ypos = 20.17527777
xpos = 168.3716667 # Leo - 2
ypos = 22.15472222
xpos = 263.5 # DECaLS DR2 Field
ypos = 26.9
xpos = 229.015 # Pal-5
ypos = -0.093
xpos = 260.31 # DECaLS DR2 Field, possible horiz. branch?
ypos = 25.46
xpos = 247.758333 # Hercules dwarf
ypos = 12.775
xpos = 185.4254 # Hydra-II
ypos = -31.98523
xsize = 0.5 # initial field size in degrees
ysize = 0.5
xsize = 0.75 # initial field size in degrees
ysize = 0.75
################################################
# Globals
################################################
ra0 = None # Entire dataset
dec0 = None
data0 = None
g0 = None
gr0 = None
ra_idx = None
dec_idx = None
fname = None # file name to load
small_k = 2.0 # Differential kernel parameters
big_k = 20.0
floor = 0.0
ceiling = 2.5
dmap = None # resultant density map
cursz = 0.1 # mouseover cursor size (degrees)
zoomsz = 0.15 # initi size of zoom box (degrees)
# Data Lab TAP Service endpoint
accessURL = "http://dldb1.sdm.noao.edu:8080/ivoa-dal/tap"
accessURL = "http://zeus1.sdm.noao.edu/tap"
# Default to use the LS DR2 catalog
catalog = 'lsdr2.star'
catalog = 'smash.blue_stars'
# GETDATA -- Retrieve the data via TAP and put into a numpy array
def getData (catalog='lsdr2.star'):
print "catalog is " + catalog
# Template query string based on (xpos,ypos) position.
if catalog == 'lsdr2.star':
query = ('select ra_j2000,dec_j2000,g,g_r,c30star from '+ catalog + \
' where ' + \
' (ra_j2000 between %g and %g) and ' + \
' (dec_j2000 between %g and %g) and ' + \
' (g is not null) and ' + \
#' (c30star < 40) and ' + \
#' (g between 20.5 and 23.5) and (g_r between 0.25 and 0.5)') % \
' (g between 1 and 23) and (g_r between -0.5 and 2.0)') % \
(xpos-xsize, xpos+xsize, ypos-ysize, ypos+ysize)
else:
query = ('select ra_j2000,dec_j2000,gmag,(gmag-rmag) as g_r,rmag,sharp '+
' from '+ catalog + \
' where ' + \
' (ra_j2000 between %g and %g) and ' + \
' (dec_j2000 between %g and %g) and ' + \
' (gmag is not null) and (rmag is not null) and ' + \
' (gmag between 10 and 23) and ' + \
' ((gmag-rmag) between -0.5 and 1.0)') % \
(xpos-xsize, xpos+xsize, ypos-ysize, ypos+ysize)
print "query is " + query
print "Getting data ....",
sys.stdout.flush()
start_time = time.time()
raw = votable.ADQLSyncJob (accessURL, query).run().openResult()
# We now need to parse the VOTable XML
xml = etree.parse (raw)
data = []
for row in xml.findall('//TR'):
data.append ([td.text for td in row.findall('TD')])
data = np.array (data).T
ra0 = data[0].astype('float64')
dec0 = data[1].astype('float64')
g0 = data[2].astype('float')
gr0 = data[3].astype('float')
print 'Got', data.shape[1], 'objects in %s seconds' % \
(time.time()-start_time)
return ra0, dec0, g0, gr0, data
# READDATA -- Read the data from a static FITS BINTABLE (testing/demo mode)
def readData (fname):
global xpos, ypos, xsize, ysize
global ra0, dec0, g0, gr0
start_time = time.time()
print "Getting data in file '%s' ...." % fname,
sys.stdout.flush()
data = fits.getdata (fname)
t = Table (data)
ra0 = t['ra_j2000']
dec0 = t['dec_j2000']
#g0 = t['g'] # FIXME -- Need to account for null values
#gr0 = t['g_r']
g0 = t['gmag'] # FIXME -- Need to account for null values
gr0 = t['gmag'] - t['rmag']
print type(ra0)
print 'c/m (%g,%g) (%g,%g)' % (g0.min(),g0.max(),gr0.min(),gr0.max())
xpos, ypos = np.mean (ra0), np.mean (dec0)
xsize = (ra0.max() - ra0.min()) / 2.0
ysize = (dec0.max() - dec0.min()) / 2.0
print 'Got', len(ra0), 'objects in %s seconds' % (time.time()-start_time)
return ra0, dec0, g0, gr0, data
# DWARF_FILTER -- Ken Mighell's differential convolution filter.
def dwarf_filter (ra, dec, ra_index, dec_index, fwhm_small=2.0, fwhm_big=20):
if (ra is None or dec is None):
return
x = (ra if ra_index is None else ra[ra_index])
y = (dec if dec_index is None else dec[dec_index])
start_time = time.time()
print "Computing differential convolution .... ",
sys.stdout.flush()
# Information about declination (y) [degrees]
ymean = (y.min() + y.max()) / 2.0
ydiff_arcmin = (y.max() - y.min()) * 60.0 # convert from degrees to arcmin
# Information about right ascension (x) [degrees in time]:
xdiff = x.max() - x.min() # angular separation [degrees (time)]
xmean = (x.min() + x.max())/2.0
# convert from degrees in time to separation in angular degrees:
xdiff_angular = (x.max() - x.min()) * np.cos (ymean*(np.pi/180.0))
# convert from degress to arcmin
xdiff_angular_arcmin = xdiff_angular * 60.0
# Get the number of one-arcmin pixels in the X and Y directions:
nx = np.rint (xdiff_angular_arcmin).astype('int')
ny = np.rint (ydiff_arcmin).astype('int')
# Create a two-dimensional histogram of the raw counts:
Counts, xedges, yedges = np.histogram2d (x, y, (nx,ny) )
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
raw_hist = np.rot90 (Counts).copy() # hack around Pythonic weirdness
# Make the small and big Gaussian kernels with a standard deviation
# of the given FWHM in arcmin^2 pixels.
kernel_small = convolution.Gaussian2DKernel (
stddev = (fwhm_small / 2.35), factor=1)
kernel_big = convolution.Gaussian2DKernel (
stddev = (fwhm_big / 2.35), factor=1)
# Compute the differential convolution kernels.
conv_big = convolution.convolve (raw_hist, kernel_big)
conv_small = convolution.convolve (raw_hist, kernel_small)
conv_delta = conv_small - conv_big
delta = conv_delta.copy()
# Compute statistics and the floor
mean = np.mean (delta, dtype='float64')
sigma = np.std (delta, dtype='float64')
median = np.median (delta) # not used
floor = mean
#print 'dwarf_filter: mean = %g sigma = %g' % (mean, sigma)
# Clip to specified limits.
clipped = delta.copy()
if dmap is not None:
if dmap.lower == None:
dmap.lower = mean
else:
floor = dmap.lower
clipped[ delta < dmap.lower ] = dmap.lower
if dmap.upper != None:
clipped[ delta > dmap.upper ] = dmap.upper
else:
dmap.upper = 999.0
print 'clip limits = (%g,%g)' % (dmap.lower, dmap.upper)
else:
clipped[ delta < floor ] = floor
print " done in %s seconds\n" % (time.time() - start_time)
# Return the computed fields.
return raw_hist, extent, delta, clipped
pass
# DENSMAP -- A Class to handle the density convolution map..
class densMap (object):
"""
DENSPLOT -- A class to ...
"""
def __init__ (self, ra, dec, small_k, big_k):
self.ra = ra
self.dec = dec
self.small_k = small_k
self.big_k = big_k
self.raw = None
self.extent = None
self.delta = None
self.clipped = None
self.lower = None
self.upper = None
def setData (self, ra, dec):
self.ra = ra
self.dec = dec
def clipRefresh (self, lower, upper):
self.clipped = dmap.delta.copy()
sigma = np.std (self.clipped, dtype='float64')
floor = (self.clipped.mean() if lower is None else lower)
self.lower = lower * sigma
self.clipped[ self.delta < self.lower ] = self.lower
if upper is not None:
self.upper = upper * sigma
self.clipped[ self.delta > self.upper ] = self.upper
print 'clipRefresh: sigma = %g floor = %g upper = %g' % \
(sigma,self.lower,self.upper)
densWindow.redraw() # Redraw the sub-plots
def setHist (self, raw, extent, delta, clipped):
self.raw = raw # raw counts histogram
self.extent = extent # edges of the field
self.delta = delta # difference in convolved field
self.clipped = clipped # clipped map of difference
# DENSPLOT -- A Class to handle the density window.
class densPlot (object):
"""
DENSPLOT -- A class to ...
"""
def __init__ (self, ax):
self.ax = ax
self.canvas = ax.figure.canvas
self.xdata = None
self.ydata = None
self.showContours = True
self.showImage = True
self.use_raw = False
self.track_cursor = True
# Create a text box to display the density stats.
at = AnchoredText ("Max=%8.5g Min=%8.5g" % (0.0, 0.0),
prop=dict(size=8), frameon=True, loc=2)
self.ax.add_artist (at)
# Create a marker to map a point in the CMD to it's place on the
# plot.
self.marker = patches.Ellipse ((xpos, ypos), width=0.05, height=0.05,
angle=0, alpha=0.9, facecolor="red")
self.ax.add_patch (self.marker)
# Attach the event handlers.
self.canvas.mpl_connect ('motion_notify_event', self.motionCallback)
self.canvas.mpl_connect ('key_press_event', self.onpressCallback)
self.canvas.mpl_connect ('key_release_event', self.onreleaseCallback)
self.canvas.mpl_connect ('axes_leave_event', self.onleaveCallback)
def onpressCallback (self, event):
global xpos, ypos, xsize, ysize, cursz, zoomsz
global ra0, dec0, g0, gr0, data0, ra_idx, dec_idx
global small_k, big_k, ra_idx, dec_idx
if (event.inaxes is not self.ax):
return
key = event.key
if (key == 'up' or key == 'k'): # Pan up
ypos += 1.5 * ysize
elif (key == 'down' or key == 'j'): # Pan down
ypos -= 1.5 * ysize
elif (key == 'left' or key == 'h'): # Pan left
xpos -= 1.5 * xsize
elif (key == 'right' or key == 'l'): # Pan right
xpos += 1.5 * xsize
elif (key == 'c'): # Center on current pos
xpos, ypos = event.xdata, event.ydata
elif (key == '('): # Decrease fwhm_big
big_k = max (5.0*small_k, big_k - 5)
elif (key == ')'): # Increase fwhm_big
big_k += 5
elif (key == '['): # Decrease fwhm_small
small_k = max (1.0, small_k - 2)
elif (key == ']'): # Increase fwhm_small
small_k += 1
elif (key == '<'): # Decrease cursor size * 2
zoomWindow.setCursorSize (zoomWindow.cursz / 2.0)
elif (key == '>'): # Increase cursor size * 2
zoomWindow.setCursorSize (zoomWindow.cursz * 2.0)
elif (key == ','): # Decrease cursor size * 1.5
zoomWindow.setCursorSize (zoomWindow.cursz / 1.5)
elif (key == '.'): # Increase cursor size * 1.5
zoomWindow.setCursorSize (zoomWindow.cursz * 1.5)
elif (key == '+'): # Decrease cursor size * 1.5
zoomWindow.setZoomSize (zoomWindow.zoomsz * 1.5)
elif (key == '-'): # Increase cursor size * 1.5
zoomWindow.setZoomSize (max (zoomWindow.zoomsz / 1.5, 0.15))
elif (key == 'shift'): # Suspend cursor tracking
self.track_cursor = False
return
else:
return
print 'Pos (%g,%g) : Size (%g,%g) : Cursor = %g : Zoom = %g' % \
(xpos,ypos,xsize,ysize,cursz,zoomsz)
if (key in ('h','j','k','l','left','down','up','right','-','+')):
# Get the data for the new field.
ra0, dec0, g0, gr0, data0 = getData ( catalog )
ra_idx = ra0 < 361 # reset working dataset
dec_idx = dec0 < 91
self.update ()
# Refresh the main density plot.
raw, extent, delta, clipped = dwarf_filter (ra0, dec0,
ra_idx, dec_idx, fwhm_small=small_k, fwhm_big=big_k)
dmap.setData (ra0[ra_idx], dec0[dec_idx])
dmap.setHist (raw, extent, delta, clipped)
# Update the zoomed data.
zoomWindow.setData (ra0[ra_idx], dec0[dec_idx])
zoomWindow.draw ()
fig.canvas.draw () # Redraw the sub-plots
def onreleaseCallback (self, event):
if (event.inaxes is not self.ax):
return
if (event.key == 'shift'): # resume motion tracking
self.track_cursor = True
def onleaveCallback (self, event):
if (event.inaxes is not self.ax):
return
self.track_cursor = True # resume motion tracking
def motionCallback (self, event):
if (event.inaxes is not self.ax):
return
if (self.track_cursor == False):
return
try:
# Redraw the CMD and zoom plots
cmdWindow.update (event.xdata, event.ydata)
zoomWindow.setPos (event.xdata, event.ydata)
except:
# Just ignore any exceptions for now
pass
def setData (self, xdata, ydata):
self.xdata = xdata
self.ydata = ydata
def setLabels (self, xlab, ylab):
self.ax.set_xlabel (xlab)
self.ax.set_ylabel (ylab)
def drawContour (self, hist, extent):
x = np.linspace (extent[0], extent[1], num=len(hist[0]))
y = np.linspace (extent[3], extent[2], num=len(hist))
CS = self.ax.contour (x, y, hist, 8, alpha=0.25)
self.ax.clabel (CS, inline=1, fontsize=10)
self.contours = CS
self.canvas.draw() # redraw the plot
def update (self):
#global raw, extent, delta, clipped
# Create the main density display.
raw, extent, delta, clipped = dwarf_filter (ra0, dec0,
ra_idx, dec_idx, fwhm_small=small_k, fwhm_big=big_k)
dmap.setHist (raw, extent, delta, clipped)
self.redraw()
def redraw (self):
self.ax.clear()
self.ax.set_xlim (xpos + xsize, xpos - xsize)
self.ax.set_ylim (ypos - ysize, ypos + ysize)
at = AnchoredText ("Max=%8.5g Min=%8.5g Npts=%d" % \
(dmap.clipped.max(), dmap.clipped.min(),len(ra0[ra_idx])),
prop=dict(size=8), frameon=True, loc=2)
self.ax.add_artist (at)
if self.showImage:
self.ax.imshow (dmap.clipped, extent=dmap.extent,
interpolation='nearest', cmap='Greys')
if self.showContours:
if self.use_raw:
self.drawContour (dmap.raw, dmap.extent)
else:
self.drawContour (dmap.clipped, dmap.extent)
self.canvas.draw () # Redraw the sub-plots
# CMDPLOT -- A Class to handle the CMD window.
class cmdPlot (object):
"""
CMDPLOT -- A class to ...
"""
def __init__ (self, ax):
global gr0, g0
self.ax = ax
self.canvas = ax.figure.canvas
self.color = None
self.mag = None
self.marker = None
self.ax.set_xlim (-0.5, 2.0)
#self.ax.set_ylim (g0.max()+0.25, g0.min()-0.25)
self.ax.set_ylim (max(25.0,g0.max()+0.25), 10.0)
self.cmd_select = RectangleSelector (self.ax,
self.cmd_select_callback,
drawtype='box', useblit=True,
button=[1, 3], # don't use middle
minspanx=5, minspany=5,
spancoords='pixels')
# Attach the event handlers.
self.canvas.mpl_connect ('motion_notify_event', self.motionCallback)
def cmd_select_callback (self, eclick, erelease):
global ra0, dec0, ra_idx, dec_idx
# eclick and erelease are the press and release events
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
# Add a marker to indicate the selected region
if (self.marker is not None):
self.marker.set_visible (False)
self.marker = patches.Rectangle ((x1, y1),
abs(x2-x1), abs(y2-y1),
color='red', alpha=0.25)
self.ax.add_patch (self.marker)
self.canvas.draw ()
idx_x = np.logical_and (gr0 > x1, gr0 < x2)
idx_y = np.logical_and (g0 > y1, g0 < y2)
data_idx = np.logical_and (idx_x, idx_y)
ra_idx = data_idx
dec_idx = data_idx
raw, extent, delta, clipped = dwarf_filter (ra0, dec0,
ra_idx, dec_idx, fwhm_small=small_k, fwhm_big=big_k)
dmap.setData (ra0[ra_idx], dec0[dec_idx])
dmap.setHist (raw, extent, delta, clipped)
densWindow.redraw()
def keypressCallback (self, event):
if (event.inaxes is not self.ax):
return
# Not yet implemented
pass
def motionCallback (self, event):
global ra0, dec0, marker
if (event.inaxes is not self.ax):
return
try:
# Find all points within some specified radius
x, y = event.xdata, event.ydata
dist = np.hypot (x - self.color, y - self.mag)
indx = dist.argmin()
dx, dy = ra[indx], dec[indx]
# Redraw the density marker at the location corresponding to
# the nearest point in the CMD.
densWindow.marker.center = dx, dy
self.canvas.draw ()
fig.canvas.draw ()
except:
# Just ignore any exceptions for now
pass
def setData (self, color, mag):
self.color = color
self.mag = mag
self.ax.set_xlim (-0.5, 2.0)
self.ax.set_ylim (max(25.0,mag.max()+0.25), 10.0)
def setLabels (self, xlabel, ylabel):
self.ax.set_xlabel (xlabel)
self.ax.set_ylabel (ylabel)
def update (self, x, y):
global ra, dec, cursz
if (densWindow.track_cursor == False):
return
try:
# Find all points within some specified radius
dist = np.hypot (x - ra0, y - dec0)
idx = dist < cursz
gr, g = gr0[idx], g0[idx]
# Redraw the CMD plot
self.ax.clear()
self.ax.scatter (gr, g, s=1, marker='.', cmap='jet')
at = AnchoredText ("Npts = %d" % len(gr), prop=dict(size=8),
frameon=True, loc=2)
self.ax.add_artist (at)
self.ax.set_xlim (-0.5, 2.0)
self.ax.set_ylim (max(25.0,self.mag.max()+0.25), 10.0)
densWindow.marker.center = x, y
self.canvas.draw () # Redraw the sub-plots
except:
# Just ignore any exceptions for now
pass
# ZOOMPLOT -- A Class to handle the position zoom window.
class zoomPlot (object):
"""
ZOOMPLOT -- A class to display a magnifier window zoomed in on a
region around the cursor in another plot window.
Key Bindings:
< Decrease cursor size by factor of 2
> Increase cursor size by factor of 2
, Decrease cursor size by factor of 1.5
. Increase cursor size by factor of 1.5
+ Increase zoom area
- Decrease zoom area
"""
def __init__ (self, ax):
global xpos, ypos, cursz, zoomsz
self.ax = ax
self.canvas = ax.figure.canvas
self.cursz = cursz
self.zoomsz = zoomsz
self.xdata = None
self.ydata = None
self.cur_x = xpos
self.cur_y = ypos
# Create a reticle overlay to show the sampling cursor size.
self.reticle = patches.Ellipse ((xpos, ypos),
width=(2 * cursz), height=(2.66 * cursz),
angle=0, alpha=0.1, facecolor="cyan")
# Attach the event handlers.
self.canvas.mpl_connect ('key_press_event', self.keypressCallback)
def keypressCallback (self, event):
global cursz
if (event.inaxes is not self.ax):
return
key = event.key
if (key == '<'): # Decrease cursor size * 2
self.setCursorSize (self.cursz / 2.0)
elif (key == '>'): # Increase cursor size * 2
self.setCursorSize (self.cursz * 2.0)
elif (key == ','): # Decrease cursor size * 1.5
self.setCursorSize (self.cursz / 1.5)
elif (key == '.'): # Increase cursor size * 1.5
self.setCursorSize (self.cursz * 1.5)
elif (key == '+'): # Zoom In
self.setZoomSize (self.zoomsz * 1.5)
elif (key == '-'): # Zoom Out
self.setZoomSize (max (self.zoomsz / 1.5, 0.15))
elif (key == 'shift'):
return
def setData (self, x, y):
self.xdata = x
self.ydata = y
def setCursorSize (self, size):
global cursz, zoomsz
size = max (size, 0.01) # enforce min cursor size
cursz = size
self.cursz = size
# Note: The reticle size is adjusted for the aspect ratio of the plot.
self.reticle.width = (2 * size)
self.reticle.height = (2.66 * size)
self.reticle.center = self.cur_x, self.cur_y # update reticle position
zoomsz = max (0.15, cursz * 1.5)
self.zoomsz = max (0.15, cursz * 1.5)
self.ax.set_xlim (self.cur_x + zoomsz, self.cur_x - zoomsz)
self.ax.set_ylim (self.cur_y - zoomsz, self.cur_y + zoomsz)
cmdWindow.update(self.cur_x, self.cur_y)
self.canvas.draw() # redraw the plot
def setZoomSize (self, size):
global xpos, ypos, cursz, zoomsz
zoomsz = size
self.zoomsz = size
# Flip RA for std orientation
self.ax.set_xlim (self.cur_x + zoomsz, self.cur_x - zoomsz)
self.ax.set_ylim (self.cur_y - zoomsz, self.cur_y + zoomsz)
self.canvas.draw() # redraw the plot
def setPos (self, x, y):
self.cur_x = x
self.cur_y = y
# Redraw the zoom plot (flip RA for std orientation)
self.ax.set_xlim (self.cur_x + zoomsz, self.cur_x - zoomsz)
self.ax.set_ylim (self.cur_y - zoomsz, self.cur_y + zoomsz)
self.reticle.center = x, y # update reticle position
self.canvas.draw() # redraw the plot
def setLabels (self, xlab, ylab):
self.ax.set_xlabel (xlab)
self.ax.set_ylabel (ylab)
def draw (self):
global xpos, ypos, cursz
if (self.xdata is None) or (self.ydata is None):
return
# Create the zoomed positional plot. Note the reticle is adjusted for
# the aspect ratio of the subplot.
self.ax.scatter (self.xdata, self.ydata, s=9, alpha=0.6,
marker='.', cmap='Greys')
print "pos=(%g,%g) cursz=%g zoom=%g" % (xpos, ypos, cursz, zoomsz)
self.ax.set_xlim (self.cur_x + self.zoomsz, self.cur_x - self.zoomsz)
self.ax.set_ylim (self.cur_y - self.zoomsz, self.cur_y + self.zoomsz)
self.reticle = patches.Ellipse ((self.cur_x, self.cur_y),
width=(2 * cursz), height=(2.66 * cursz),
angle=0, alpha=0.1, facecolor="cyan")
self.ax.add_patch (self.reticle)
atz = AnchoredText ("Zoom=%g Cursor=%g" % (zoomsz, cursz),
prop=dict(size=8), frameon=True, loc=2)
self.ax.add_artist (atz)
def drawContour (self, hist, extent):
x = np.linspace (extent[0], extent[1], num=len(hist[0]))
y = np.linspace (extent[3], extent[2], num=len(hist))
CS = self.ax.contour (x, y, hist)
self.ax.clabel (CS, inline=1, fontsize=10)
self.canvas.draw() # redraw the plot
# HISTPLOT -- A Class to handle the position zoom window.
class histPlot (object):
"""
HISTPLOT -- A class to display a histogram window.
Key Bindings:
"""
def __init__ (self, ax, data):
self.ax = ax
self.canvas = ax.figure.canvas
self.ax.get_yaxis().set_visible (False)
self.marker = None
self.lower = None
self.upper = None
self.data = data
dv = np.ndarray.flatten (data)
self.mean = np.mean (dv,dtype='float64')
self.sigma = np.std (dv,dtype='float64')
self.median = np.median (dv)
self.lower = self.mean
self.upper = self.mean + (2 * self.sigma)
# Draw the histogram
sighist = (dv-self.mean) / self.sigma
self.ax.hist (sighist, 20, log=True, color='lightgrey')
# FIXME
self.ax.set_xlabel ('Sigma')
self.ax.set_xlim (max(-6.0,data.min()), min(10.0,data.max()))
self.ax.axvline (x=self.mean, color='red')
self.ax.axvline (x=-3.0, color='cyan')
self.ax.axvline (x=3.0, color='cyan')
# set useblit True on gtkagg for enhanced performance
self.span = SpanSelector (self.ax, self.onselect,
'horizontal', useblit=True,
rectprops=dict(alpha=0.5, facecolor='yellow'))
self.canvas.draw()
def onselect (self, xmin, xmax):
self.lower = max(self.mean,xmin)
self.lower = xmin
self.upper = xmax
# Add a marker to indicate the selected region
if (self.marker is not None):
self.marker.set_visible (False)
self.marker = patches.Rectangle ((self.lower, 0.0),
abs(xmax-self.lower), 99999.0,
color='red', alpha=0.25)
self.ax.add_patch (self.marker)
dmap.clipRefresh (xmin, xmax)
# CONTROLFRAME -- A class to manage the control widgets.
class controlFrame (object):
"""
CONTROLFRAME -- A class to manage the control widgets.
"""
def __init__ (self):
self.smallS = Slider (plt.axes ([0.1, 0.11, 0.325, 0.03]),
'Small-K', 1, 40, valinit=small_k)
self.bigS = Slider (plt.axes ([0.1, 0.075, 0.325, 0.03]),
'Big-K', 1, 99, valinit=big_k)
self.countS = Slider (plt.axes ([0.1, 0.04, 0.16, 0.03]),
'C30 Stars', 10, 99, valinit=40)
self.reset_button = Button (plt.axes([0.315, 0.04, 0.05, 0.03]),
'Reset', color='white', hovercolor='yellow')
self.apply_button = Button (plt.axes([0.375, 0.04, 0.05, 0.03]),
'Apply', color='white', hovercolor='yellow')
self.up_button = Button (plt.axes([0.25, 0.925, 0.05, 0.03]),
'N', color='white', hovercolor='yellow')
self.down_button = Button (plt.axes([0.25, 0.855, 0.05, 0.03]),
'S', color='white', hovercolor='yellow')
self.left_button = Button (plt.axes([0.22, 0.89, 0.05, 0.03]),
'E', color='white', hovercolor='yellow')
self.right_button = Button (plt.axes([0.28, 0.89, 0.05, 0.03]),
'W', color='white', hovercolor='yellow')
self.zoom_in = Button (plt.axes([0.375, 0.925, 0.095, 0.03]),
'Zoom In', color='white', hovercolor='yellow')
self.zoom_out = Button (plt.axes([0.375, 0.89, 0.095, 0.03]),
'Zoom Out', color='white', hovercolor='yellow')
self.center = Button (plt.axes([0.375, 0.855, 0.095, 0.03]),
'Center', color='white', hovercolor='yellow')
self.do_contour = CheckButtons (plt.axes([0.055, 0.855, 0.115, 0.095]),
('Contours','Image','Raw Bkg'), (True,True,False))
# Bind the slider control methods.
self.smallS.on_changed (self.smallk_update)
self.bigS.on_changed (self.bigk_update)
self.countS.on_changed (self.counts_update)
# Bind the Pan/Zoom control methods.
self.zoom_in.on_clicked (self.zoomIn)
self.zoom_out.on_clicked (self.zoomOut)
self.center.on_clicked (self.center)
self.apply_button.on_clicked (self.apply)
self.reset_button.on_clicked (self.reset)
self.up_button.on_clicked (self.panUp)
self.down_button.on_clicked (self.panDown)
self.left_button.on_clicked (self.panLeft)
self.right_button.on_clicked (self.panRight)
# Bind the density plot options methods.