def emask(cutimage, xcntr, ycntr, NXPTS, NYPTS, line_s, galflag): imagefile = c.imagefile sex_cata = c.sex_cata clus_cata = c.out_cata threshold = c.threshold thresh_area = c.thresh_area mask_reg = c.mask_reg values = line_s.split() mask_file = 'EM_' + c.fstring + '.fits' xcntr_o = xcntr * 1.0 #x center of the object ycntr_o = ycntr * 1.0 #y center of the object mag = float(values[7]) #Magnitude radius = float(values[9]) #Half light radius mag_zero = c.mag_zero #magnitude zero point sky = float(values[10]) #sky pos_ang = float(values[11]) #position angle axis_rat = 1.0 / float(values[12]) #axis ration b/a area_o = float(values[13]) # object area major_axis = float(values[14]) #major axis of the object eg = 1.0 - axis_rat f_tmp_mask = pyfits.open('seg.fits') tmp_mask = f_tmp_mask[0].data f_tmp_mask.close() for line_j in open('SegCat.cat', 'r'): try: values = line_j.split() id_n = float(values[0]) xcntr_n = float(values[1]) #x center of the neighbour ycntr_n = float(values[2]) #y center of the neighbour mag = float(values[7]) #Magnitude radius = float(values[9]) #Half light radius sky = float(values[10]) #sky pos_ang = float(values[11]) #position angle axis_rat = 1.0 / float(values[12]) #axis ration b/a si = np.sin(pos_ang * np.pi / 180.0) co = np.cos(pos_ang * np.pi / 180.0) area = float(values[13]) maj_axis = float(values[14]) #major axis of neighbour eg = 1.0 - axis_rat one_minus_eg_sq = (1.0 - eg)**2.0 if np.abs(xcntr_n - xcntr_o) < 5.0 and np.abs( ycntr_n - ycntr_o) < 5.0 and galflag == 1: tmp_mask[np.where(tmp_mask == id_n)] = 0 except: pass boxcar = np.reshape(np.ones(3 * 3), (3, 3)) tmp_mask = pymconvolve.Convolve(tmp_mask, boxcar) tmp_mask[tmp_mask > 1e-5] = 1 tmp_mask[tmp_mask != 1] = 0 if (galflag): hdu = pyfits.PrimaryHDU(tmp_mask.astype(np.float32)) hdu.writeto(mask_file) else: try: os.remove("BMask.fits") except: pass hdu = pyfits.PrimaryHDU(tmp_mask.astype(np.float32)) hdu.writeto("BMask.fits")
def CLUMPNESS(z, xcntr, ycntr, pa, eg, sky, ext_rad, sigma, flag_image): CutImDa, cut_xcntr, cut_ycntr, SizeY, SizeX, ymin, ymax, xmin, \ xmax, flag_out = ImSec(z, xcntr, ycntr, ext_rad) co = np.cos(pa * np.pi / 180.0) si = np.sin(pa * np.pi / 180.0) one_minus_eg_sq = (1.0 - eg)**2.0 x = np.reshape(np.arange(SizeX * SizeY), (SizeY, SizeX)) % SizeX x = x.astype(np.float32) y = np.reshape(np.arange(SizeX * SizeY), (SizeY, SizeX)) / SizeX y = y.astype(np.float32) tx = (x - cut_xcntr) * co + (y - cut_ycntr) * si ty = (cut_xcntr - x) * si + (y - cut_ycntr) * co R = np.sqrt(tx**2.0 + ty**2.0 / one_minus_eg_sq) boxcar = np.reshape(np.ones(sigma * sigma), (sigma, sigma)) I_sigma = pymconvolve.Convolve(CutImDa, boxcar) res = CutImDa - I_sigma #the residual image if (flag_image): # the below will find the image portion which is an anulus of inner # radius 0.3 * eta(.2) and outer radius 1.5 * eta(.2) # making the residual value equal to zero inside the # ext_rad/20) res[R <= ext_rad * (1 / 20.0)] = 0 res[R >= ext_rad] = 0 # sum of positive values of residue res_inside_anulus_sum = res[res > 0].sum() # Average inside ext_rad. res_inside_anulus_sum will be divided by # z_inside_R_sum in casgm module z_inside_R_sum = CutImDa[R <= ext_rad].sum() / (3.14 * \ ext_rad * ext_rad * np.sqrt(1 - eg**2.0)) # FIX I dont know why 1/6 instead of 1/20. area = 3.14 * (ext_rad * ext_rad * np.sqrt(1 - eg**2.0)) - \ 3.14 * (ext_rad * ext_rad * (1 / 6.0) * (1 / 6.0) * \ np.sqrt(1 - eg**2.0)) # END S = res_inside_anulus_sum / area e1sq = CutImDa[res > 0].sum() + I_sigma[res > 0].sum() + \ 4. * CutImDa[res > 0].size * sky else: res[R >= ext_rad] = 0 res_inside_anulus_sum = res[np.where(res > 0)].sum() area = 3.14 * ext_rad**2.0 S = res_inside_anulus_sum / area z_inside_R_sum = 0 # just to return the value in the end e1sq = CutImDa[res > 0].sum() + I_sigma[res > 0].sum() + \ 2. * CutImDa[res > 0].size * sky e2sq = res_inside_anulus_sum**2.0 e3sq = CutImDa[R <= ext_rad].sum() + 2 * CutImDa[R <= ext_rad].size * sky e4sq = (CutImDa[R <= ext_rad].sum())**2.0 if (e2sq != 0): error = e1sq / e2sq else: print "Could not find error" error = 0.0 return S, error, z_inside_R_sum, e3sq, e4sq
def FindYetSky(gimg, X0, Y0): RunSegSex(gimg) f = pyfits.open(gimg) z = f[0].data f.close() f = pyfits.open('seg.fits') zm = f[0].data f.close() SexSky, SkyYet, SkyMed, SkyMin, SkyQua, SkySig = 9999, 9999, 9999, \ 9999, 9999, 9999 for l_s in open('SegCat.cat'): v_s = l_s.split() try: SexId = n.float(v_s[0]) xcntr = n.float(v_s[1]) ycntr = n.float(v_s[2]) pa = n.float(v_s[11]) bbya = 1.0 / n.float(v_s[12]) a = n.float(v_s[14]) * 3.0 b = a * bbya hr = n.float(v_s[9]) sky = n.float(v_s[10]) if n.abs(X0 - xcntr) < 5.0 and n.abs(Y0 - ycntr) < 5.0: boxcar = np.reshape(np.ones(3 * 3), (3, 3)) zm = pymconvolve.Convolve(zm, boxcar) zm[n.where(zm > 0)] = 1 SkyQua = [] for ii in n.arange(1, 5): SkyQua.append(QuarterMask(z, zm, xcntr-1.0, ycntr-1.0, \ bbya, pa, ii)) SkyQua = n.array(SkyQua) SexSky = sky tmpstd = n.std(ma.masked_array(z, zm).compressed()) tmpmed = n.median(ma.masked_array(z, zm).compressed()) zm[n.where((z - tmpmed) > 1.3 * tmpstd)] = 1 SkyYet = n.median(ma.masked_array(z, zm).compressed()) SkyMed = n.median(SkyQua) SkyMin = n.min(SkyQua) SkySig = n.std(ma.masked_array(z, zm).compressed()) # os.system('rm -f SegCat.cat default_seg.sex seg.fits') break except: pass return SexSky, SkyYet, SkyMed, SkyMin, SkyQua, SkySig
def gmask(cutimage, xcntr, ycntr, NXPTS, NYPTS, line_s): imagefile = c.imagefile sex_cata = c.sex_cata threshold = c.threshold thresh_area = c.thresh_area mask_reg = c.mask_reg avoidme = c.avoidme target = SEx_obj(NXPTS, NYPTS, line_s) mask_file = 'M_' + c.fstring + '.fits' mag_zero = c.mag_zero #magnitude zero point f_tmp_mask = pyfits.open('seg.fits') tmp_mask = f_tmp_mask[0].data f_tmp_mask.close() for line_j in open('SegCat.cat', 'r'): if line_j[0] != '#': #line is not a comment #try: neighbor = SEx_obj(NXPTS, NYPTS, line_j) if target.mask_or_fit(neighbor, threshold, thresh_area, avoidme) != 1: tmp_mask[np.where(tmp_mask == neighbor.sex_num)] = 0 #except: # pass if c.NoMask: tmp_mask[np.where(tmp_mask > 0)] = 0 elif c.NormMask: pass boxcar = np.reshape(np.ones(3 * 3), (3, 3)) tmp_mask = pymconvolve.Convolve(tmp_mask, boxcar) tmp_mask[tmp_mask > 1e-5] = 1 tmp_mask[tmp_mask != 1] = 0 hdu = pyfits.PrimaryHDU(tmp_mask.astype(np.float32)) hdu.writeto(mask_file)
def segmentation(zextract, xcntr, ycntr, pa, eg, sky, r20, r50, r80, \ ext_rad, sigma, skysig, lower): """This function find the segmentation map""" def gini_coef(I): """This will calculate Gini Coefficient""" oneD_I = np.reshape(I, (1,-1)) sorted_I = np.sort(oneD_I) sorted_I = sorted_I[sorted_I > 0] nn = sorted_I.size average = np.abs(sorted_I).mean() sumI = 0.0 for i in range(nn): sumI += (2 * (i + 1) - nn - 1) * abs(sorted_I[i]) G = sumI / (average * nn * (nn - 1)) return G sigma = int(sigma) co= np.cos(pa*np.pi/180.0) si= np.sin(pa*np.pi/180.0) one_minus_eg_sq = (1.0 - eg)**2.0 SizeY = zextract.shape[0] # Y size SizeX = zextract.shape[1] # X size x = np.reshape(np.arange(SizeX * SizeY), (SizeY, SizeX)) % SizeX x = x.astype(np.float32) y = np.reshape(np.arange(SizeX * SizeY), (SizeY, SizeX)) / SizeX y = y.astype(np.float32) rx = (x - xcntr)* co + (y - ycntr) * si ry = (xcntr - x) * si + (y - ycntr) * co R = np.sqrt(rx**2.0 + ry**2.0 / one_minus_eg_sq) boxcar = np.reshape(np.ones(sigma * sigma), (sigma, sigma)) I_sigma = pymconvolve.Convolve(zextract, boxcar) lower_thre = (I_sigma[R <= ext_rad / 1.5].sum() - \ I_sigma[R <= ext_rad / 1.5 - 1].sum()) / \ (3.14 * ((ext_rad / 1.5)**2. - (ext_rad / 1.5 - 1)**2.)) upper_thre = 10.0 * skysig # the upper threshold #The below will finds the segmentation image I0 = np.where(R > (2.5 / 1.5) * ext_rad, 0, zextract) I1 = np.where(I_sigma < lower_thre, 0, I0) I2 = np.where(R > (ext_rad / 1.5 - (0.02 * ext_rad)), 0, I1) I3 = np.where(R <= (ext_rad / 1.5 - (0.02 * ext_rad)), 0, I1) I4 = np.where(I_sigma > upper_thre, 0, I3) I = I2 + I4 G = gini_coef(I) mo = moment(I, xcntr, ycntr) M = mo.moment_of_light[0] # FIX the following will calculate the Gini coef. & M20 of the residual # image of asymmetry. Now it is turning off. In the future it can be # replaced with the Gini coef. of residual galfit image # f = pyfits.open(c.outdir +'AResidual.fits') # res = f[0].data # f.close() # if os.access(c.outdir +'AResidual.fits', os.F_OK): # os.remove(c.outdir +'AResidual.fits') # res1=np.where(I == 0, 0, res) # G_res = gini_coef(res1) # mo1=moment(res, xcntr, ycntr) # M_res = mo1.moment_of_light[0] G_res = 0.0 M_res = 0.0 # END I80 = np.where(R > r80, 0, zextract) G80 = gini_coef(I80) mo2 = moment(I80, xcntr, ycntr) M80 = mo2.moment_of_light[0] I50 = np.where(R > r50, 0, zextract) G50 = gini_coef(I50) mo3 = moment(I50, xcntr, ycntr) M_50 = mo3.moment_of_light[0] I20 = np.where(R > r20, 0, zextract) G20 = gini_coef(I20) mo4 = moment(I20, xcntr, ycntr) M_20 = mo4.moment_of_light[0] return G, G_res, G80, G50, G20, M, M_res, M80, M_50, M_20