def estimate_params(img): img_green = img[..., 1] # supposedly using the green channel is a good idea. alternatively we could use luma. # this seems to work well enough. out = pysex.run(img_green, params=[ 'X_IMAGE', 'Y_IMAGE', # barycenter # 'XMIN_IMAGE', 'XMAX_IMAGE', 'YMIN_IMAGE', 'YMAX_IMAGE', # enclosing rectangle # 'XPEAK_IMAGE', 'YPEAK_IMAGE', # location of maximal intensity 'A_IMAGE', 'B_IMAGE', 'THETA_IMAGE', # ellipse parameters 'PETRO_RADIUS', # 'KRON_RADIUS', 'PETRO_RADIUS', 'FLUX_RADIUS', 'FWHM_IMAGE', # various radii ], conf_args={ 'DETECT_THRESH': DETECT_THRESH }) # x and y are flipped for some reason. # theta should be 90 - theta. # we convert these here so we can plot stuff with matplotlib easily. try: ys = out['X_IMAGE'].tonumpy() xs = out['Y_IMAGE'].tonumpy() as_ = out['A_IMAGE'].tonumpy() bs = out['B_IMAGE'].tonumpy() thetas = 90 - out['THETA_IMAGE'].tonumpy() # kron_radii = out['KRON_RADIUS'].tonumpy() petro_radii = out['PETRO_RADIUS'].tonumpy() # flux_radii = out['FLUX_RADIUS'].tonumpy() # fwhms = out['FWHM_IMAGE'].tonumpy() # detect the most salient galaxy # take in account size and centrality surface_areas = np.pi * (as_ * bs) centralities = np.exp(-((xs - 211.5)**2 + (ys - 211.5)**2)/SIGMA2) # 211.5, 211.5 is the center of the image # salience is proportional to surface area, with a gaussian prior on the distance to the center. saliences = surface_areas * centralities most_salient_idx = np.argmax(saliences) x = xs[most_salient_idx] y = ys[most_salient_idx] a = as_[most_salient_idx] b = bs[most_salient_idx] theta = thetas[most_salient_idx] # kron_radius = kron_radii[most_salient_idx] petro_radius = petro_radii[most_salient_idx] # flux_radius = flux_radii[most_salient_idx] # fwhm = fwhms[most_salient_idx] except TypeError: # sometimes these are empty (no objects found), use defaults in that case x = 211.5 y = 211.5 a = np.nan # dunno what this has to be, deal with it later b = np.nan # same theta = np.nan # same # kron_radius = np.nan petro_radius = np.nan # flux_radius = np.nan # fwhm = np.nan # return (x, y, a, b, theta, flux_radius, kron_radius, petro_radius, fwhm) return (x, y, a, b, theta, petro_radius)
def tweakreg_sextract(files_to_sextract, catfile_name, sexfile_dir=None): ''' Systematically loop through each file and sextract the sources and write them in a catalogue It has to output the catalogue in pixels using the files tweak.sex and tweak.param INPUT : files_to_sextract : a python list of file names to source extract catfile_name : name of the file containing the image and catalogue names OUTPUT : A source catalogue for each ''' if sexfile_dir is None: sexfile_dir = '/'.join(os.path.abspath(__file__).\ split('/')[:-1])+'/tweak_sex/' catfile = open(catfile_name, "wb") for iFile in files_to_sextract: outcat_name = iFile[:-5] + '_sex.cat' conf_file = sexfile_dir + 'tweak.sex' filter_name = sexfile_dir + 'gauss_5.0_9x9.conv' starnnw_name = sexfile_dir + 'default.nnw' param_file = sexfile_dir + 'tweak.param' sexCat = sex.run( iFile, conf_file=conf_file, param_file=param_file, \ conf_args={'CATALOG_NAME':outcat_name, 'FILTER_NAME':filter_name, 'STARNNW_NAME':starnnw_name}) catfile.write("%s %s\n" % (iFile, outcat_name))
def clusterSizeSex( image, clusterSource): ''' Get cluster size in arcseconds ''' try: sources = sex.run(image) returnSource = sources[ np.argmin(raSep(sources['X_WORLD'],sources['Y_WORLD'],clusterSource['RA'],clusterSource['DEC'],abs=True))] radius = returnSource['FLUX_RADIUS1']*fits.open(image)[0].header['CDELT2']*3600/206265*ang_distance(0.2)*1e3 except: radius =100 return radius
def makecat(self, rerun=True, keepcat=False, verbose=True): self.cat = pysex.run( self.filepath, conf_args={ "DETECT_THRESH": 3.0, "ANALYSIS_THRESH": 3.0, "DETECT_MINAREA": 10, "PIXEL_SCALE": 1.0, "SEEING_FWHM": 2.0, "FILTER": "Y", "VERBOSE_TYPE": "NORMAL" if verbose else "QUIET", }, params=["X_IMAGE", "Y_IMAGE", "FLUX_AUTO", "FWHM_IMAGE", "FLAGS", "ELONGATION", "NUMBER", "EXT_NUMBER"], rerun=rerun, keepcat=keepcat, catdir="alipy_cats", )
def component_extractor( image_file, smoothing=20, \ pixel_size_kpc=5, redshift=0.25,\ filtername='gauss_5.0_9x9.conv'): ''' This is a general script that should extract a given mass component from the simulations. ''' image = fits.open(image_file)[0].data if smoothing is not None: smoothed_image = gauss_filter(image, smoothing) else: smoothed_image = image pixel_size_degrees = pixel_size_kpc / (lensing.ang_distance(redshift) * 1e3) * 180. / np.pi #Create a random string to component find file_name = 'component_finding_' + str(np.random.random_integers( 0, 100000)) + '.fits' fits.writeto(file_name, smoothed_image, clobber=True) conf_path = '/Users/DavidHarvey/Library/Code/IDL/rrg/code/shape/sex_files/' hot_conf = conf_path + 'wavdetectpy.sex' filtername = conf_path + '/' + filtername conf_args = { 'WEIGHT_TYPE': 'NONE', 'FILTER_NAME': filtername, 'FILTER': 'YES' } sources = sex.run(file_name, conf_file=hot_conf, conf_args=conf_args) os.system('rm -fr ' + file_name) ra = sources['X_IMAGE'] * pixel_size_degrees dec = sources['Y_IMAGE'] * pixel_size_degrees sources = append_rec(sources, 'RA', ra, usemask=False, asrecarray=True) sources = append_rec(sources, 'DEC', dec) return sources
def makecat(self, rerun=True, keepcat=False, verbose=True): self.cat = pysex.run(self.filepath, conf_args={ 'DETECT_THRESH': 3.0, 'ANALYSIS_THRESH': 3.0, 'DETECT_MINAREA': 10, 'PIXEL_SCALE': 1.0, 'SEEING_FWHM': 2.0, "FILTER": "Y", 'VERBOSE_TYPE': 'NORMAL' if verbose else 'QUIET' }, params=[ 'X_IMAGE', 'Y_IMAGE', 'FLUX_AUTO', 'FWHM_IMAGE', 'FLAGS', 'ELONGATION', 'NUMBER', "EXT_NUMBER" ], rerun=rerun, keepcat=keepcat, catdir="alipy_cats")
def ref_sex(refimage, sexfile_dir=None): ''' PURPOSE : TO SEXTRACT THE COORDINATES OF THE OBJECTS IN FOR THE REFERENCE CATALOGUE ''' if sexfile_dir is None: sexfile_dir = '/'.join(os.path.abspath(__file__).\ split('/')[:-1])+'/tweak_sex/' conf_file = sexfile_dir + 'tweak_wcs.sex' filter_name = sexfile_dir + 'gauss_5.0_9x9.conv' starnnw_name = sexfile_dir + 'default.nnw' param_file = sexfile_dir + 'tweak_wcs.param' sexCat = sex.run( refimage, conf_file=conf_file, param_file=param_file, \ conf_args={'CATALOG_NAME':'reference.cat', 'FILTER_NAME':filter_name, 'STARNNW_NAME':starnnw_name})
def result(fits): cat = pysex.run(fits, params=['X_IMAGE', 'Y_IMAGE', 'FLUX_APER'], conf_args={'PHOT_APERTURES':5}) print cat['FLUX_APER'] image = afwImage.MaskedImageF(fits) binsize = 128 nx = int(image.getWidth()/binsize) + 1 ny = int(image.getHeight()/binsize) + 1 bctrl = afwMath.BackgroundControl(nx, ny) bkgd = afwMath.makeBackground(image, bctrl) statsImage = afwMath.cast_BackgroundMI(bkgd).getStatsImage() image -= bkgd.getImageF(afwMath.Interpolate.NATURAL_SPLINE) return bkgd return
def makecat(self, rerun=True, keepcat=False, verbose=True): self.cat = pysex.run(self.filepath, conf_args={'DETECT_THRESH':3.0, 'ANALYSIS_THRESH':3.0, 'DETECT_MINAREA':10, 'PIXEL_SCALE':1.0, 'SEEING_FWHM':2.0, "FILTER":"Y", 'VERBOSE_TYPE':'NORMAL' if verbose else 'QUIET'}, params=['X_IMAGE', 'Y_IMAGE', 'FLUX_AUTO', 'FWHM_IMAGE', 'FLAGS', 'ELONGATION', 'NUMBER', "EXT_NUMBER"], rerun=rerun, keepcat=keepcat, catdir="alipy_cats")