/
maskwrapper.py
1206 lines (1009 loc) · 50.1 KB
/
maskwrapper.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
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
'''
PURPOSE:
The goal of the program is to create a mask for a galaxy image to mask
out other objects within the cutout area.
USAGE:
you just need to run this on R-band images.
PROCEDURE:
REQUIRED MODULES:
os
astropy
numpy
argsparse
matplotlib
scipy
USAGE:
* if running on wise images, try:
python ~/github/halphagui/maskwrapper.py --image AGC006015-unwise-1640p454-w1-img-m.fits --ngrow 1 --sesnr 2 --minarea 5 --auto
NOTES:
- rewrote using a class
# TODO: 2023-02-09: this program relies on source extractor. I should rewrite to use photutils instead.
TESTING
/home/rfinn/research/Virgo-dev/maskwrapper-test/VFID0610-rectangle//VFID0610-NGC5985-INT-20190530-p040
objparams = [self.defcat.cat['RA'][self.igal],self.defcat.cat['DEC'][self.igal],mask_scalefactor*self.radius_arcsec[self.igal],self.BA[self.igal],self.PA[self.igal]+90]
python ~/github/halphagui/maskwrapper.py --image VFID0610-NGC5985-INT-20190530-p040-R.fits --haimage VFID0610-NGC5985-INT-20190530-p040-CS.fits --sepath ~/github/halphagui/astromatic/ --gaiapath /home/rfinn/research/legacy/gaia-mask-dr9.virgo.fits --objra 234.90448 --objdec 59.33198 --objsma 139.25 --objBA .496 --objPA 104.646
'''
import os
import sys
import numpy as np
import warnings
from astropy.io import fits
from astropy.wcs import WCS
from astropy.convolution import Tophat2DKernel, convolve
from astropy.convolution.kernels import CustomKernel
from astropy.table import Table
from astropy.coordinates import SkyCoord
import numpy as np
#import argparse
#import pyds9
from scipy.stats import scoreatpercentile
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib import patches
#from maskGui import Ui_maskWindow
from maskWidget import Ui_Form as Ui_maskWindow
from halphaCommon import cutout_image, circle_pixels
# import gaia function to get stars within region
from get_gaia_stars import gaia_stars_in_rectangle
import imutils
try:
from photutils.segmentation import detect_threshold, detect_sources
#from photutils import source_properties
from photutils.segmentation import SourceCatalog
from photutils.segmentation import deblend_sources
except ModuleNotFoundError:
warnings.warn("Warning - photutils not found")
except ImportError:
print("got an import error with photutils - check your version number")
from PyQt5 import QtCore, QtGui, QtWidgets
try:
from ginga.qtw.ImageViewQt import CanvasView, ScrolledView
from ginga.mplw.ImageViewMpl import ImageView
from ginga import colors
from ginga.canvas.CanvasObject import get_canvas_types
from ginga.misc import log
from ginga.util.loader import load_data
gingaflag = True
except ModuleNotFoundError:
print("Warning - ginga was not found. this will be a problem if running interactively")
gingaflag = False
import timeit
defaultcat='default.sex.HDI.mask'
#####################################
### FUNCTIONS
#####################################
def remove_central_objects(mask, sma=20, BA=1, PA=0, xc=None,yc=None):
"""
find any pixels within central ellipse and set their values to zero
PARAMS:
mask = 2D array containing masked pixels, like from SE segmentation image
sma = semi-major axis in pixels
BA = ratio of semi-minor to semi-major axes
PA = position angle, measured in degree counter clockwise from +x axis
OPTIONAL ARGS:
xc = center of ellipse in pixels; assumed to be center of image if xc is not specified
yc = center of ellipse in pixels; assumed to be center of image if yc is not specified
RETURNS:
newmask = copy of input mask, with pixels within ellipse set equal to zero
"""
# changing the xmax and ymax - if the ellipse looks wrong, then swap back
ymax,xmax = mask.shape
# set center of ellipse as the center of the image
if (xc is None) and (yc is None):
xc,yc = xmax//2,ymax//2
a = sma
b = BA*sma
phirad = np.radians(PA)
X,Y = np.meshgrid(np.arange(xmax),np.arange(ymax))
p1 = ((X-xc)*np.cos(phirad)+(Y-yc)*np.sin(phirad))**2/a**2
p2 = ((X-xc)*np.sin(phirad)-(Y-yc)*np.cos(phirad))**2/b**2
flag2 = p1+p2 < 1
newmask = np.copy(mask)
newmask[flag2] = 0
# we could also get all the unique values associated with flag2, and then remove them
ellipse_params = [xc,yc,sma,BA,phirad]
return newmask,ellipse_params
def mask_radius_for_mag(mag):
"""
function is from legacy pipeline
https://github.com/legacysurvey/legacypipe/blob/6d1a92f8462f4db9360fb1a68ef7d6c252781027/py/legacypipe/reference.py#L314-L319
"""
# Returns a masking radius in degrees for a star of the given magnitude.
# Used for Tycho-2 and Gaia stars.
# This is in degrees, and is from Rongpu in the thread [decam-chatter 12099].
return 1630./3600. * 1.396**(-mag)
class buildmask():
def link_files(self):
# TODO: replace sextractor with photutils
# these are the sextractor files that we need
# set up symbolic links from sextractor directory to the current working directory
sextractor_files=['default.sex.HDI.mask','default.param','default.conv','default.nnw']
for file in sextractor_files:
if os.path.exists(file):
os.remove(file)
os.system('ln -sf '+self.sepath+'/'+file+' .')
#os.copy(self.sepath+'/'+file, file)
def clean_links(self):
# clean up symbolic links to sextractor files
# sextractor_files=['default.sex.sdss','default.param','default.conv','default.nnw']
sextractor_files=['default.sex.HDI.mask','default.param','default.conv','default.nnw']
for file in sextractor_files:
os.system('unlink '+file)
def read_se_cat(self):
sexout=fits.getdata(self.catname)
self.xsex=sexout['XWIN_IMAGE']
self.ysex=sexout['YWIN_IMAGE']
self.fwhm = sexout['FWHM_IMAGE']
dist=np.sqrt((self.yc-self.ysex)**2+(self.xc-self.xsex)**2)
# find object ID
# some objects are rturning an empty sequence - how to handle this?
# I guess this means that the object itself wasn't detected?
# or nothing was detected?
if len(dist) < 1:
# set objnumb to nan
objnumb = np.nan
else:
objIndex=np.where(dist == min(dist))
objNumber=sexout['NUMBER'][objIndex]
objnumb = objNumber[0] # not sure why above line returns a list
return objnumb
def runse(self,galaxy_id = None,weightim=None,weight_threshold=1):
# TODO update this to implement running SE with two diff thresholds
# TODO make an alternate function that creates segmentation image from photutils
# this is already done in ell
print('using a deblending threshold = ',self.threshold)
print("image = ",self.image_name)
self.catname = self.image_name.replace('.fits','.cat')
self.segmentation = self.image_name.replace('.fits','-segmentation.fits')
print("segmentation image = ",self.segmentation)
sestring = f"sex {self.image_name} -c {self.config} -CATALOG_NAME {self.catname} -CATALOG_TYPE FITS_1.0 -DEBLEND_MINCONT {self.threshold} -DETECT_THRESH {self.snr} -ANALYSIS_THRESH {self.snr_analysis} -CHECKIMAGE_NAME {self.segmentation} -DETECT_MINAREA {self.minarea}"
if weightim is not None:
sestring += r" -WEIGHT_TYPE MAP_WEIGHT -WEIGHT_IMAGE {weightim} -WEIGHT_THRESH {weight_threshold}"
print(sestring)
os.system(sestring)
self.maskdat = fits.getdata(self.segmentation)
# grow masked areas
bool_array = np.array(self.maskdat.shape,'bool')
#for i in range(len(self.xsex)):
# check to see if the object is not centered in the cutout
def get_photutils_mask(self,galaxy_id = None):
# TODO make an alternate function that creates segmentation image from photutils
from astropy.stats import sigma_clipped_stats
from photutils import make_source_mask
# create mask to cut low SNR pixels based on SNR in SFR image
mask = make_source_mask(imdat,nsigma=self.snr,npixels=self.minarea,dilate_size=5)
masked_data = np.ma.array(imdat,mask=mask)
self.catname = self.image_name.replace('.fits','.cat')
self.segmentation = self.image_name.replace('.fits','-segmentation.fits')
self.maskdat = fits.getdata(self.segmentation)
# grow masked areas
bool_array = np.array(self.maskdat.shape,'bool')
#for i in range(len(self.xsex)):
# check to see if the object is not centered in the cutout
def remove_center_object(self):
""" this removes the object in the center of the mask, which presumably is the galaxy """
# need to replace this with a function that will remove any objects within the specificed central ellipse
if self.remove_center_object_flag:
if self.off_center_flag:
print('setting center object to objid ',self.galaxy_id)
self.center_object = self.galaxy_id
else:
self.center_object = self.read_se_cat()
if self.center_object is not np.nan:
self.maskdat[self.maskdat == self.center_object] = 0
if self.objsma is not None:
if hasattr(self.objsma, "__len__"):
# loop over objects in fov
self.ellipseparams = []
for i in range(len(self.objsma)):
#print(f"sma={self.objsma_pixels[i]},BA={self.objBA[i]}, PA={self.objPA[i]},xc={self.xpixel[i]},yc={self.ypixel[i]}")
self.maskdat,eparams = remove_central_objects(self.maskdat, sma=self.objsma_pixels[i],\
BA=self.objBA[i], PA=self.objPA[i], \
xc=self.xpixel[i],yc=self.ypixel[i])
self.ellipseparams.append(eparams)
pass
else:
# remove central objects within elliptical aperture
print("ellipse params in remove_central_object :",self.xpixel,self.ypixel,self.objsma_pixels,self.objBA,self.objPA)
self.maskdat,self.ellipseparams = remove_central_objects(self.maskdat, sma=self.objsma_pixels, \
BA=self.objBA, PA=self.objPA, \
xc=self.xpixel,yc=self.ypixel)
else:
print("no ellipse params")
self.ellipseparams = None
self.update_mask()
def update_mask(self):
self.add_user_masks()
self.add_gaia_masks()
self.write_mask()
def add_user_masks(self):
""" this adds back in the objects that the user has masked out """
# add back the square masked areas that the user added
self.maskdat = self.maskdat + self.usr_mask
# remove objects that have already been deleted by user
if len(self.deleted_objects) > 0:
for objID in self.deleted_objects:
self.maskdat[self.maskdat == objID] = 0.
def write_mask(self):
""" write out mask image """
# add ellipse params to imheader
if self.ellipseparams is not None:
#print("HEY!!!")
#print()
#print("Writing central ellipse parameters to header")
#print(self.ellipseparams)
#print()
if hasattr(self.objsma,"__len__"):
xc,yc,r,BA,PA = self.ellipseparams[0]
else:
xc,yc,r,BA,PA = self.ellipseparams
self.imheader.set('ELLIP_XC',float(xc),comment='XC of mask ellipse')
self.imheader.set('ELLIP_YC',float(yc),comment='YC of mask ellipse')
self.imheader.set('ELLIP_A',r,comment='SMA of mask ellipse')
self.imheader.set('ELLIP_BA',BA,comment='BA of mask ellipse')
self.imheader.set('ELLIP_PA',np.degrees(PA),comment='PA (deg) of mask ellipse')
else:
print("HEY!!! writing mask, but no parameters for central ellipse!")
fits.writeto(self.mask_image,self.maskdat,header = self.imheader,overwrite=True)
invmask = self.maskdat > 0.
invmask = np.array(~invmask,'i')
fits.writeto(self.mask_inv_image,invmask,header = self.imheader,overwrite=True)
if not self.auto:
self.mask_saved.emit(self.mask_image)
self.display_mask()
def add_gaia_masks(self):
# check to see if gaia stars were already masked
if self.add_gaia_stars:
if self.gaia_mask is None :
self.get_gaia_stars()
self.make_gaia_mask()
else:
self.maskdat += self.gaia_mask
def get_gaia_stars(self, useastroquery=True):
"""
get gaia stars within FOV
"""
# check to see if gaia table already exists
outfile = self.image_name.replace('.fits','_gaia_stars.csv')
if os.path.exists(outfile):
self.brightstar = Table.read(outfile)
#print(self.brightstar.colnames)
self.xgaia = self.brightstar['xpixel']
self.ygaia = self.brightstar['ypixel']
else:
brightstar = gaia_stars_in_rectangle(self.racenter,self.deccenter,self.dydeg+.01,self.dxdeg+.01)
try:
# get gaia stars within FOV
# adding buffer to the search dimensions for bright stars that might be just off FOV
brightstar = gaia_stars_in_rectangle(self.racenter,self.deccenter,self.dydeg+.01,self.dxdeg+.01)
# Check to see if any stars are returned
if len(brightstar) > 0:
print("found gaia stars in FOV!")
# get radius from mag-radius relation
mask_radius = mask_radius_for_mag(brightstar['phot_g_mean_mag'])
brightstar.add_column(mask_radius,name='radius')
starcoord = SkyCoord(brightstar['ra'],brightstar['dec'],frame='icrs',unit='deg')
self.xgaia,self.ygaia = self.image_wcs.world_to_pixel(starcoord)
brightstar.add_column(self.xgaia,name='xpixel')
brightstar.add_column(self.ygaia,name='ypixel')
self.brightstar = brightstar
else:
self.brightstar = None
self.xgaia = None
self.ygaia = None
except:
print()
print('WARNING: error using astroquery to get gaia stars')
print()
# read in gaia catalog
try:
brightstar = Table.read(self.gaiapath)
# Convert ra,dec to x,y
starcoord = SkyCoord(brightstar['ra'],brightstar['dec'],frame='icrs',unit='deg')
x,y = self.image_wcs.world_to_pixel(starcoord)
# add buffer to catch bright stars off FOV
buffer = 0.1*self.xmax
flag = (x > -buffer) & (x < self.xmax+buffer) & (y>-buffer) & (y < self.ymax+buffer)
# add criteria for proper motion cut
# Hopefully this fix should resolve cases where center of galaxy is masked out as a star...
# changing to make this a SNR > 5 detection, rather than 5 mas min proper motion
pmflag = np.sqrt(brightstar['pmra']**2*brightstar['pmra_ivar'] + brightstar['pmdec']**2*brightstar['pmdec_ivar']) > 5
flag = flag & pmflag
if np.sum(flag) > 0:
self.brightstar = brightstar[flag]
self.xgaia = x
self.ygaia = y
brightstar.add_column(self.xgaia,name='xpixel')
brightstar.add_column(self.ygaia,name='ypixel')
else:
self.brightstar = None
self.xgaia = None
self.ygaia = None
except FileNotFoundError:
warnings.warn(f"Can't find the catalog for gaia stars({self.gaiapath}) - running without bright star masks!")
self.add_gaia_stars = False
return
# Write out resulting file for future use
if self.brightstar is not None:
outfile = self.image_name.replace('.fits','_gaia_stars.csv')
self.brightstar.write(outfile,format='csv')
def make_gaia_mask(self):
"""
mask out bright gaia stars using the legacy dr9 catalog and magnitude-radius relation:
https://github.com/legacysurvey/legacypipe/blob/6d1a92f8462f4db9360fb1a68ef7d6c252781027/py/legacypipe/reference.py#L314-L319
"""
self.get_gaia_stars()
# set up blank
self.gaia_mask = np.zeros_like(self.maskdat)
if self.brightstar is not None:
# add stars to mask according to the magnitude-radius relation
mag = self.brightstar['phot_g_mean_mag']
xstar = self.xgaia
ystar = self.ygaia
rad = self.brightstar['radius'] # in degrees
# Convert radius to pixels
radpixels = rad/self.pscalex.value
# use the same value for all gaia stars. set this above max value in mask
mask_value = np.max(self.maskdat) + 100
print('mask value = ',mask_value)
for i in range(len(mag)):
# mask stars
print(f"star {i}: {xstar[i]:.1f},{ystar[i]:.1f},{radpixels[i]:.1f}")
pixel_mask = circle_pixels(float(xstar[i]),float(ystar[i]),float(radpixels[i]),self.xmax,self.ymax)
#print(f"number of pixels masked for star {i} = {np.sum(pixel_mask)}")
#print('xcursor, ycursor = ',self.xcursor, self.ycursor)
#print("\nshape of pixel_mask = ",pixel_mask.shape)
#print("\nshape of gaia_mask = ",self.gaia_mask.shape)
self.gaia_mask[pixel_mask] = mask_value*np.ones_like(self.gaia_mask)[pixel_mask]
# add gaia stars to main mask
self.maskdat = self.maskdat + self.gaia_mask
else:
print("No bright stars on image - woo hoo!")
def run_photutil(self, snrcut=1.5,npixels=10):
'''
run photutils detect_sources to find objects in fov.
you can specify the snrcut, and only pixels above this value will be counted.
this also measures the sky noise as the mean of the threshold image
'''
self.threshold = detect_threshold(self.image, nsigma=snrcut)
segment_map = detect_sources(self.image, self.threshold, npixels=npixels)
# deblind sources a la source extractor
# tried this, and the deblending is REALLY slow
# going back to source extractor
self.segmentation = deblend_sources(self.image, segment_map,
npixels=10, nlevels=32, contrast=0.001)
self.maskdat = self.segmentation.data
#self.cat = source_properties(self.image, self.segmentation)
self.cat = SourceCatalog(self.image, self.segmentation)
# get average sky noise per pixel
# threshold is the sky noise at the snrcut level, so need to divide by this
self.sky_noise = np.mean(self.threshold)/snrcut
#self.tbl = self.cat.to_table()
if self.off_center_flag:
print('setting center object to objid ',self.galaxy_id)
self.center_object = self.galaxy_id
else:
distance = np.sqrt((self.cat.xcentroid - self.xc)**2 + (self.cat.ycentroid - self.yc)**2)
# save object ID as the row in table with source that is closest to center
objIndex = np.arange(len(distance))[(distance == min(distance))][0]
# the value in shown in the segmentation image is called 'label'
self.center_object = self.cat.label[objIndex]
self.maskdat[self.maskdat == self.center_object] = 0
self.update_mask()
def grow_mask(self, size=7):
"""
Convolution: one way to grow the mask is to convolve the image with a kernel
however, this does not preserve the pixels value of the original
object, which come from the sextractor segmentation image.
if the user wants to remove an object, it's much easier to do this
by segmentation number rather than by pixels (as in the reverse of how we add objects
to mask).
Alternative: is to loop over just the masked pixels, and replace all pixels
within a square region with the masked value at the central pixel.
This will preserve the numbering from the segmentation image.
Disadvantage: everything assumes a square shape after repeated calls.
Alternative is currently implemented.
"""
# convolve mask with top hat kernel
# kernel = Tophat2DKernel(5)
#mykernel = np.ones([5,5])
#kernel = CustomKernel(mykernel)
#self.maskdat = convolve(self.maskdat, kernel)
#self.maskdat = np.ceil(self.maskdat)
# we don't want to grow the size of the gaia stars, do we???
self.maskdat -= self.gaia_mask
nx,ny = self.maskdat.shape
masked_pixels = np.where(self.maskdat > 0.)
for i,j in zip(masked_pixels[0], masked_pixels[1]):
rowmin = max(0,i-int(size/2))
rowmax = min(nx,i+int(size/2))
colmin = max(0,j-int(size/2))
colmax = min(ny,j+int(size/2))
if rowmax <= rowmin:
# something is wrong, return without editing mask
continue
if colmax <= colmin:
# something is wrong, return without editing mask
continue
#print(i,j,rowmin, rowmax, colmin, colmax)
self.maskdat[rowmin:rowmax,colmin:colmax] = self.maskdat[i,j]*np.ones([rowmax-rowmin,colmax-colmin])
# add back in the gaia star masks
self.maskdat += self.gaia_mask
if not self.auto:
self.display_mask()
# save convolved mask as new mask
self.write_mask()
def show_mask_mpl(self):
# plot mpl figure
# this was for debugging purposes
print("plotting mask and central ellipse")
self.fig = plt.figure(1,figsize=self.figure_size)
plt.clf()
plt.subplots_adjust(hspace=0,wspace=0)
plt.subplot(1,2,1)
plt.imshow(self.image,cmap='gray_r',vmin=self.v1,vmax=self.v2,origin='lower')
plt.title('image')
plt.subplot(1,2,2)
#plt.imshow(maskdat,cmap='gray_r',origin='lower')
plt.imshow(self.maskdat,cmap=self.cmap,origin='lower',vmin=np.min(self.maskdat),vmax=np.max(self.maskdat))
plt.title('mask')
plt.gca().set_yticks(())
#plt.draw()
#plt.show(block=False)
#print("in show_mask_mpl: objsma = ",self.objsma)
try:
if hasattr(self.objsma, "__len__"):
#print("working with multiple galaxies")
# add ellipse for each galaxy if there is more than one
for e in self.ellipseparams:
xc,yc,r,BA,PA = e
PAdeg = np.degrees(PA)
#print(f"BA={BA},PA={PAdeg} deg")
#print("just checking - adding ellipse drawing ",self.ellipseparams)
ellip = patches.Ellipse((xc,yc),2*r,2*r*BA,angle=PAdeg,alpha=.2)
plt.gca().add_patch(ellip)
else:
xc,yc,r,BA,PA = self.ellipseparams
PAdeg = np.degrees(PA)
#print(f"BA={BA},PA={PAdeg} deg")
#print("just checking - adding ellipse drawing ",self.ellipseparams)
ellip = patches.Ellipse((xc,yc),r,r*BA,angle=PAdeg,alpha=.2)
plt.gca().add_patch(ellip)
except:
print("problem plotting ellipse with mask")
# outfile
outfile = self.mask_image.replace('.fits','.png')
plt.savefig(outfile)
#plt.show()
class my_cutout_image(QtCore.QObject):#QtCore.QObject):
#mouse_clicked = QtCore.pyqtSignal(str)
key_pressed = QtCore.pyqtSignal(str)
def __init__(self,panel_name,ui, logger, row, col, drow, dcol,autocut_params='stddev',auto=False):
#super(image_panel, self).__init__(panel_name,ui, logger, row, col, drow, dcol)
# enable some user interaction
#fi.get_bindings.enable_all(True)
#super(my_cutout_image, self).__init__(panel_name,ui, logger, row, col, drow, dcol)
#super(my_cutout_image,self).__init__(self)
QtCore.QObject.__init__(self)
self.logger = logger
self.ui = ui
fi = CanvasView(self.logger, render='widget')
fi.enable_autocuts('on')
fi.set_autocut_params(autocut_params)
fi.enable_autozoom('on')
#fi.set_callback('drag-drop', self.drop_file)
#fi.set_callback('none-move',self.cursor_cb)
fi.set_bg(0.2, 0.2, 0.2)
fi.ui_set_active(True)
self.fitsimage = fi
fi.show_focus_indicator(True)
bd = fi.get_bindings()
bd.enable_all(True)
self.cutout = fi.get_widget()
self.ui.cutoutsLayout.addWidget(self.cutout, row, col, drow, dcol)
self.fitsimage = fi
self.fitsimage.set_callback('none-move',self.cursor_cb)
#self.fitsimage.set_callback('cursor-down',self.cursor_down)
self.readout = QtWidgets.QLabel('')
ui.readoutGridLayout.addWidget(self.readout, 1, col, 1, 1)
#self.ui.readoutLabel.setText('this is another test')
self.fitsimage.set_callback('key-press',self.key_press_cb)
# adding lines to allow drawing on canvas?
self.dc = get_canvas_types()
canvas = self.dc.DrawingCanvas()
canvas.enable_draw(True)
canvas.enable_edit(True)
canvas.set_drawtype('rectangle', color='lightblue')
canvas.set_surface(fi)
#canvas.rectangle(.5,.5,10,0)
self.canvas = canvas
# add canvas to view
#fi.add(canvas)
private_canvas = fi.get_canvas()
private_canvas.add(canvas)
canvas.register_for_cursor_drawing(fi)
#canvas.add_callback('draw-event', self.draw_cb)
canvas.set_draw_mode('draw')
canvas.ui_set_active(True)
self.canvas = canvas
self.drawtypes = canvas.get_drawtypes()
self.drawtypes.sort()
def load_image(self, imagearray):
#self.fitsimage.set_image(imagearray)
self.fitsimage.set_data(imagearray)
def load_file(self, filepath):
image = load_data(filepath, logger=self.logger)
self.fitsimage.set_image(image)
#self.setWindowTitle(filepath)
# adding cursor readout so we can identify the pixel values to change
def cursor_cb(self, viewer, button, data_x, data_y):
"""This gets called when the data position relative to the cursor
changes.
"""
# Get the value under the data coordinates
try:
# We report the value across the pixel, even though the coords
# change halfway across the pixel
value = viewer.get_data(int(data_x + viewer.data_off),
int(data_y + viewer.data_off))
except Exception:
value = None
fits_x, fits_y = data_x + 1, data_y + 1
try:
text = "X: %.1f Y: %.1f Value: %.2f" % (fits_x, fits_y, float(value))
#self.readout.setText('this is another test')
self.readout.setText(text)
except:
pass
'''
def cursor_down(self, fitsimage, event, data_x, data_y):
print('clicked at ',data_x, data_y)
#self.mouse_clicked.emit(str(data_x)+','+str(data_y))
'''
def key_press_cb(self, canvas, keyname):
print('key pressed! ',keyname)
data_x, data_y = self.fitsimage.get_last_data_xy()
self.key_pressed.emit(keyname+','+str(data_x)+','+str(data_y))
#return self.imexam_cmd(self.canvas, keyname, data_x, data_y, func)
class maskwindow(Ui_maskWindow, QtCore.QObject,buildmask):
mask_saved = QtCore.pyqtSignal(str)
def __init__(self, MainWindow, logger, image=None, haimage=None, sepath=None, gaiapath=None, config=None, threshold=0.005,snr=10,cmap='gist_heat_r',objparams=None,auto=False,unmaskellipse=False,minarea=10,ngrow=3,weightim=None,weight_threshold=None):
"""
ngrow : number of times to run grow when running in auto mode
"""
self.auto = auto
if MainWindow is None:
self.auto = True
if not self.auto:
super(maskwindow, self).__init__()
self.ui = Ui_maskWindow()
self.ui.setupUi(MainWindow)
MainWindow.setWindowTitle('makin a mask...')
self.MainWindow = MainWindow
self.logger = logger
# define the position of the target galaxy, as well as the shape and size of elliptical region to unmask around galaxy.
#print("inside maskwrapper.init, objparams = ",objparams)
if objparams is not None:
self.objra = objparams[0]
self.objdec = objparams[1]
self.objsma = objparams[2]
self.objBA = objparams[3]
self.objPA = objparams[4]
else:
self.objra = None
self.objdec = None
self.objsma = None
self.objBA = None
self.objPA = None
if (self.objsma is not None): # unmask central elliptical region around object
# get wcs from mask image
wcs = WCS(fits.getheader(image))
# get x and y coord of galaxy from (RA,DEC) using mask wcs
#print(f"\nobject RA={self.objra:.4f}, DEC={self.objdec:.4f}\n")
self.xpixel,self.ypixel = wcs.wcs_world2pix(self.objra,self.objdec,0)
# convert sma to pixels using pixel scale from mask wcs
self.pixel_scale = wcs.pixel_scale_matrix[1][1]
self.objsma_pixels = self.objsma/(self.pixel_scale*3600)
self.weightim = weightim
self.weight_threshold = weight_threshold
### The lines below are for testing purposes
### and should be removed before release.
#if image is None:
# image='MKW8-18216-R.fits'
#if haimage == None:
# haimage='MKW8-18216-CS.fits'
if sepath is None:
sepath=os.getenv('HOME')+'/github/halphagui/astromatic/'
if gaiapath is None:
gaiapath = os.getenv("HOME")+'/research/legacy/gaia-mask-dr9.virgo.fits'
if config is None:
config='default.sex.HDI.mask'
self.image_name = image
self.haimage_name = haimage
print(self.image_name)
print(self.haimage_name)
print(sepath)
self.sepath = sepath
self.gaiapath = gaiapath
self.gaia_mask = None
self.add_gaia_stars = True
self.config = config
self.threshold = threshold
self.snr = snr
self.snr_analysis = snr
self.minarea = minarea
self.cmap = cmap
self.xcursor_old = -99
self.xcursor = -99
self.mask_size = 20.
# create name for output mask file
t = self.image_name.split('.fit')
self.mask_image=t[0]+'-mask.fits'
self.mask_inv_image=t[0]+'-inv-mask.fits'
#print('saving mask image as: ',self.mask_image)
self.remove_center_object_flag = True
# read in image and define center coords
self.image, self.imheader = fits.getdata(self.image_name,header = True)
self.ymax,self.xmax = self.image.shape
self.xc = self.xmax/2.
self.yc = self.ymax/2.
self.image_wcs = WCS(self.imheader)
self.pscalex,self.pscaley = self.image_wcs.proj_plane_pixel_scales() # appears to be degrees/pixel
# get image dimensions in deg,deg
self.dxdeg,self.dydeg = imutils.get_image_size_deg(self.image_name)
# Get coord of image center. will use when getting gaia stars
self.racenter,self.deccenter = imutils.get_image_center_deg(self.image_name)
self.v1,self.v2=scoreatpercentile(self.image,[5.,99.5])
self.adjust_mask = True
self.figure_size = (10,5)
self.mask_size = 20. # side of square to mask out when user clicks on a pixel
# set up array to store the user-created object masks
self.usr_mask = np.zeros_like(self.image)
print(self.image.shape, self.usr_mask.shape)
# set off center flag as false by default
self.off_center_flag = False
# keep track of extra objects that the user deletes from mask
self.deleted_objects = []
if not self.auto:
self.add_cutout_frames()
# time how long it takes to run SE
self.runse_flag = True
runphot = False
if self.runse_flag:
self.link_files()
t_0 = timeit.default_timer()
self.runse(weightim=self.weightim,weight_threshold=self.weight_threshold)
self.remove_center_object()
#self.remove_central_objects(xc=self.xpixels,yc=self.ypixels)
t_1 = timeit.default_timer()
#print("HELLO!!!")
print(f"\ntime to run se: {round((t_1-t_0),3)} sec\n")
if runphot:
self.usephot = True
t_1 = timeit.default_timer()
self.run_photutil()
t_2 = timeit.default_timer()
print(f"\ntime to run photutils: {round((t_2-t_1),3)} sec\n")
#self.update_mask()
if self.auto:
for i in range(int(ngrow)):
# grow mask 7x when running in auto mode
self.grow_mask()
try:
self.show_mask_mpl()
except TypeError:
print("WARNING: could not display mask")
if not self.auto:
self.display_cutouts()
self.connect_buttons()
def connect_buttons(self):
#self.ui.msaveButton.clicked.connect(self.write_mask)
self.ui.mquitButton.clicked.connect(self.quit_program)
self.ui.mhelpButton.clicked.connect(self.print_help_menu)
self.ui.mrunSEButton.clicked.connect(self.runse)
#self.ui.msaveButton.clicked.connect(self.save_mask)
#self.ui.mremoveButton.clicked.connect(self.remove_object)
self.ui.boxSizeLineEdit.textChanged.connect(self.set_box_size)
self.ui.seThresholdLineEdit.textChanged.connect(self.set_threshold)
self.ui.seSNRLineEdit.textChanged.connect(self.set_sesnr)
self.ui.seSNRAnalysisLineEdit.textChanged.connect(self.set_sesnr_analysis)
def close_window(self):
print('click red x to close window')
#sys.exit()
def add_cutout_frames(self):
# r-band cutout
a = QtWidgets.QLabel('r-band')
self.ui.cutoutsLayout.addWidget(a, 0, 0, 1, 1)
a = QtWidgets.QLabel('CS Halpha')
self.ui.cutoutsLayout.addWidget(a, 0, 1, 1, 1)
a = QtWidgets.QLabel('Mask')
self.ui.cutoutsLayout.addWidget(a, 0, 2, 1, 1)
#self.ui.cutoutsLayout.addWidget(self.cutout, row, col, drow, dcol)
self.rcutout = my_cutout_image(self.ui.cutoutsLayout,self.ui, self.logger, 1, 0, 4, 1)
self.hacutout = my_cutout_image(self.ui.cutoutsLayout,self.ui, self.logger, 1, 1, 4, 1)
self.maskcutout = my_cutout_image(self.ui.cutoutsLayout,self.ui, self.logger,1, 2, 4, 1)
#self.maskcutout.mouse_clicked.connect(self.add_object)
# this allows the user to press editing keys in any of the 3 image panels
# not just in the mask panel
self.maskcutout.key_pressed.connect(self.key_press_func)
self.rcutout.key_pressed.connect(self.key_press_func)
self.hacutout.key_pressed.connect(self.key_press_func)
def display_cutouts(self):
self.rcutout.load_file(self.image_name)
self.rcutout.fitsimage.set_autocut_params('stddev')
if self.haimage_name is not None:
self.hacutout.load_file(self.haimage_name)
self.display_mask()
def display_mask(self):
self.maskcutout.load_file(self.mask_image)
self.draw_central_ellipse()
def show_mask(self):
if self.nods9 & (not self.auto):
plt.close('all')
self.fig = plt.figure(1,figsize=self.figure_size)
plt.clf()
plt.subplots_adjust(hspace=0,wspace=0)
plt.subplot(1,2,1)
plt.imshow(self.image,cmap='gray_r',vmin=self.v1,vmax=self.v2,origin='lower')
plt.title('image')
plt.subplot(1,2,2)
#plt.imshow(maskdat,cmap='gray_r',origin='lower')
plt.imshow(self.maskdat,cmap=self.cmap,origin='lower')
plt.title('mask')
plt.gca().set_yticks(())
#plt.draw()
#plt.show(block=False)
self.draw_central_ellipse()
def draw_central_ellipse(self, color='cyan'): # MVC - view
# mark r24
markcolor=color#, 'yellow', 'cyan']
markwidth=1
#print('inside draw_ellipse_results')
image_frames = [self.rcutout,self.hacutout,self.maskcutout]
if self.ellipseparams is None:
print("")
print("no parameters found for central ellipse")
print()
return
xc,yc,r,BA,PA = self.ellipseparams
#print("just checking - adding ellipse drawing ",self.ellipseparams)
objlist = []
for i,im in enumerate(image_frames):
obj =im.dc.Ellipse(xc,yc,r,r*BA, rot_deg = np.degrees(PA), color=markcolor,linewidth=markwidth)
objlist.append(obj)
self.markhltag = im.canvas.add(im.dc.CompoundObject(*objlist))
im.fitsimage.redraw()
#print("did you see anything???")
# mark R17 in halpha image
def key_press_func(self,text):
key, x, y = text.split(',')
self.xcursor = float(x)
self.ycursor = float(y)
try:
self.cursor_value = self.maskdat[int(self.ycursor),int(self.xcursor)]
except IndexError:
print('out of bounds, try again')
#print('cursor value = ',self.cursor_value, key)
if key == 'c':
self.add_circ_object()
elif key == 'b':
self.add_box_object()
elif key == 'r':
print('removing object')
self.remove_object(int(self.cursor_value))
elif key == 'o':
self.off_center()
elif key == 'g':
self.grow_mask()
#elif key == 't':
# self.set_threshold()
#elif key == 'n':
# self.set_sesnr()
elif key == 'h':
self.print_help_menu()
elif key == 'w':
self.save_mask()
elif key == 'q':
self.quit_program()
else:
print('did not understand that. \n Try again!')
def print_help_menu(self):
print('Click on mask or r/ha image, then enter:\n \t r = remove object in mask at the cursor position;'
'\n \t c = add CIRCULAR mask at cursor position;'
'\n \t b = add BOX mask at cursor position;'
'\n \t g = grow the size of the current masks;'
'\n \t o = if target is off center (and program is removing the wrong object);'
#'\n \t s to change the size of the mask box;'
#'\n \t t to adjust SE threshold (0=lots, 1=no deblend );'
#'\n \t n to adjust SE SNR; '
'\n \t h = print this menu; '
'\n \t w = write the mask image;'
#'\n \t q to quit \n \n'
'\n\n'
'Display shortcuts (click on image to adjust):'
'\n \t scroll = zoom'
'\n \t ` = zoom to fit'
'\n \t space+s = enable contrast adjustment, click+drag, scroll wheel'
'\n \t space = exit contrast adjustment'
'\n \t a = automatically set contrast'
#'\n \t ALT-right_click = adjust contrast \n \n'
'\n\nClick Red X to close window')
def add_box_object(self):
'''