dist_psfs = (psfs_pos-center_QSO)[:,0]**2+(psfs_pos-center_QSO)[:,1]**2 #The distance of the PSF close to the QSO. psfs_pos = psfs_pos[dist_psfs.argsort()] # Sort the PSF order based on their distance. count = 0 #The following lines are to check if the PSF's Gaussian center is consistent or not with the brighest pixel PSF_gauss_centers, PSF_bright_centers=[],[] PSFs_ = [] for i in range(len(psfs_pos)): print 'PSF',count PSF, PSF_center = cut_center_bright(image=img_sub, center=psfs_pos[i], radius=75, return_center=True, plot=False, center_float=True) PSF_gauss_centers.append(PSF_center) _, PSF_br_center = cut_center_bright(image=img_sub, center=psfs_pos[i], radius=75, kernel = 'center_bright', return_center=True, plot=False) PSF_bright_centers.append(PSF_br_center) #Creat the PSFs mask that block the objects nearby _, PSF_mask = mask_obj(img=PSF, exp_sz=1.4, pltshow = 1) if len(PSF_mask) > 1: PSF_mask = np.sum(np.asarray(PSF_mask),axis=0) elif len(PSF_mask) == 1: PSF_mask = PSF_mask[0] PSF_mask = (1 - (PSF_mask != 0)*1.) PSFs_.append([PSF,PSF_center,PSF_mask]) count += 1 center_match = (np.sum(abs(np.asarray(PSF_gauss_centers)-np.asarray(PSF_bright_centers)<0.7),axis = 1) == 2) psf_pos_list = [PSF_gauss_centers[i] for i in range(len(PSFs_)) if center_match[i]==True] PSFs = [PSFs_[i] for i in range(len(PSFs_)) if center_match[i]==True] save_loc_png(img_sub,center_QSO,psf_pos_list, ID=ID, label='PSF' ,reg_ty = None) # Compare the Kron slope of the PSFs: PSF_list = [PSFs[i][0] for i in range(len(PSFs))]
def decomp(fitsfile, psffile, deepseed, fix_center, runMCMC, name, iresults, band): #Setting the fitting condition: deep_seed = deepseed #Set as True to put more seed and steps to fit. # pltshow = 1 #Note that setting plt.ion() in line27, the plot won't show anymore if running in terminal. pltshow = 0 pix_scale = 0.168 fixcenter = fix_center run_MCMC = runMCMC zp = 27.0 fitfile = fits.open(fitsfile) psf1 = pyfits.getdata(psffile) #was 3 before, changed it to 1 QSO_img1 = fitfile[1].data #print(QSO_img1) #print(QSO_img1.shape) #I CHANGED IT TO 1 IT SHOUDL ABSOLUTELY BE 3 QSO_std1 = fitfile[3].data**0.5 frame_size = len(QSO_img1) cut_to = 38 #cut_to = 30 ct = (frame_size - cut_to) / 2 ''' QSO_img = QSO_img1[ct:-ct, ct:-ct] QSO_std = QSO_std1[ct:-ct, ct:-ct] ''' #print('before for image: ', len(QSO_img1), len(QSO_img1[1])) #print('before for std: ', len(QSO_std1), len(QSO_std1[1])) #goes [y,x] if (len(QSO_img1) - len(QSO_img1[1]) == 0): cut_to = len(QSO_img1) - 2 ct = int((frame_size - cut_to) / 2) #print(ct) #print('square: ', len(QSO_img1),len(QSO_img1[1])) QSO_img = QSO_img1[ct:-ct, ct:-ct] if (len(QSO_img1) - len(QSO_img1[1]) > 0): cut_to = len(QSO_img1[1]) - 1 ct = int((frame_size - cut_to) / 2) #print('left is bigger', len(QSO_img1),len(QSO_img1[1])) diff = len(QSO_img1) - len(QSO_img1[1]) QSO_img = QSO_img1[ct + diff:-ct, ct:-ct] #QSO_img = QSO_img1[20:-ct, ct:-ct] if (len(QSO_img1) - len(QSO_img1[1]) < 0): cut_to = len(QSO_img1) - 2 ct = int((frame_size - cut_to) / 2) diff = len(QSO_img1) - len(QSO_img1[1]) QSO_img = QSO_img1[ct + diff:-ct, ct:-ct] #print('right is bigger', len(QSO_img1),len(QSO_img1[1])) #print(len(QSO_img1), len(QSO_img1[1])) #print('after for img: ', len(QSO_img), len(QSO_img[1])) #print('ok now QSO_std time') if (len(QSO_std1) == len(QSO_std1[1])): ct = int((frame_size - cut_to) / 2) QSO_std = QSO_std1[ct:-ct, ct:-ct] if (len(QSO_std1) - len(QSO_std1[1]) > 0): ct = int((frame_size - cut_to) / 2) diff = len(QSO_std1) - len(QSO_std1[1]) QSO_std = QSO_std1[ct + diff:-ct, ct:-ct] if (len(QSO_std1) - len(QSO_std1[1]) < 0): ct = int((frame_size - cut_to) / 2) diff = len(QSO_std1) - len(QSO_std1[1]) QSO_std = QSO_std1[ct + diff:-ct, ct:-ct] #print('fix this!!!') #print('after for std: ', len(QSO_std), len(QSO_std[1])) #print('psf size before: ', psf1.shape) #now do ct for psf frame_size = len(psf1) cut_to = 39 ct = int((frame_size - cut_to) / 2) if (len(psf1) == len(psf1[1])): cut_to = len(psf1) - 2 ct = int((frame_size - cut_to) / 2) psf = psf1[ct:-ct, ct:-ct] if (len(psf1) - len(psf1[1]) > 0): cut_to = len(psf1[1]) - 1 ct = int((frame_size - cut_to) / 2) #print('weird psf') diff = len(psf1) - len(psf1[1]) psf = psf1[ct + diff:-ct, ct:-ct] if (len(psf1) - len(psf1[1]) < 0): cut_to = len(psf1) - 2 ct = int((frame_size - cut_to) / 2) #print('weird psf') diff = len(psf1) - len(psf1[1]) psf = psf1[ct:-ct, ct:-ct + diff] #print('psf size after: ', psf.shape) #if(len(psf1) - len(psf1[1]) == 1): # psf = psf1[ct + 1:-ct, ct:-ct] #if(len(psf1) - len(psf1[1]) == -1): # psf = psf1[ct:-ct, ct+1:-ct] #print('psf size after: ', psf.shape) #print(QSO_img) #print(QSO_img.shape) #psf = psf1 #%% #============================================================================== # input the objects components and parameteres #============================================================================== ins = iresults[0]['n_sersic'] iRs = iresults[0]['R_sersic'] ie1 = iresults[0]['e1'] ie2 = iresults[0]['e2'] ixc = iresults[0]['center_x'] iyc = iresults[0]['center_y'] objs, Q_index = detect_obj(QSO_img, pltshow=pltshow) qso_info = objs[Q_index] obj = [objs[i] for i in range(len(objs)) if i != Q_index] fixed_source = [] kwargs_source_init = [] kwargs_source_sigma = [] kwargs_lower_source = [] kwargs_upper_source = [] fixed_source.append({ 'R_sersic': iRs, 'n_sersic': ins, 'e1': ie1, 'e2': ie2 }) kwargs_source_init.append({ 'R_sersic': 0.3, 'n_sersic': 2., 'e1': 0., 'e2': 0., 'center_x': 0., 'center_y': 0. }) kwargs_source_sigma.append({ 'n_sersic': 0.5, 'R_sersic': 0.5, 'e1': 0.1, 'e2': 0.1, 'center_x': 0.1, 'center_y': 0.1 }) kwargs_lower_source.append({ 'e1': -0.5, 'e2': -0.5, 'R_sersic': 0.1, 'n_sersic': 0.3, 'center_x': -0.5, 'center_y': -0.5 }) kwargs_upper_source.append({ 'e1': 0.5, 'e2': 0.5, 'R_sersic': 3., 'n_sersic': 7., 'center_x': 0.5, 'center_y': 0.5 }) if len(obj) >= 1: for i in range(len(obj)): if len(iresults) <= i + 1: fixed_source.append({}) kwargs_source_init.append({ 'R_sersic': obj[i][1] * pix_scale, 'n_sersic': 2., 'e1': 0., 'e2': 0., 'center_x': -obj[i][0][0] * pix_scale, 'center_y': obj[i][0][1] * pix_scale }) kwargs_source_sigma.append({ 'n_sersic': 0.5, 'R_sersic': 0.5, 'e1': 0.1, 'e2': 0.1, 'center_x': 0.1, 'center_y': 0.1 }) kwargs_lower_source.append({ 'e1': -0.5, 'e2': -0.5, 'R_sersic': obj[i][1] * pix_scale / 5, 'n_sersic': 0.3, 'center_x': -obj[i][0][0] * pix_scale - 10, 'center_y': obj[i][0][1] * pix_scale - 10 }) kwargs_upper_source.append({ 'e1': 0.5, 'e2': 0.5, 'R_sersic': 3., 'n_sersic': 7., 'center_x': -obj[i][0][0] * pix_scale + 10, 'center_y': obj[i][0][1] * pix_scale + 10 }) else: ins = iresults[i + 1]['n_sersic'] iRs = iresults[i + 1]['R_sersic'] ie1 = iresults[i + 1]['e1'] ie2 = iresults[i + 1]['e2'] ixc = iresults[i + 1]['center_x'] iyc = iresults[i + 1]['center_y'] fixed_source.append({ 'R_sersic': iRs, 'n_sersic': ins, 'e1': ie1, 'e2': ie2 }) kwargs_source_init.append({ 'R_sersic': obj[i][1] * pix_scale, 'n_sersic': 2., 'e1': 0., 'e2': 0., 'center_x': -obj[i][0][0] * pix_scale, 'center_y': obj[i][0][1] * pix_scale }) kwargs_source_sigma.append({ 'n_sersic': 0.5, 'R_sersic': 0.5, 'e1': 0.1, 'e2': 0.1, 'center_x': 0.1, 'center_y': 0.1 }) kwargs_lower_source.append({ 'e1': -0.5, 'e2': -0.5, 'R_sersic': obj[i][1] * pix_scale / 5, 'n_sersic': 0.3, 'center_x': -obj[i][0][0] * pix_scale - 10, 'center_y': obj[i][0][1] * pix_scale - 10 }) kwargs_upper_source.append({ 'e1': 0.5, 'e2': 0.5, 'R_sersic': 3., 'n_sersic': 7., 'center_x': -obj[i][0][0] * pix_scale + 10, 'center_y': obj[i][0][1] * pix_scale + 10 }) source_params = [ kwargs_source_init, kwargs_source_sigma, fixed_source, kwargs_lower_source, kwargs_upper_source ] #%% #%% # ============================================================================= # Creat the QSO mask # ============================================================================= from mask_objects import mask_obj _, _, deblend_sources = mask_obj(QSO_img, snr=1.2, npixels=50, return_deblend=True) print("deblend image to find the ID for the Objects for the mask:") plt.imshow(deblend_sources, origin='lower', cmap=deblend_sources.cmap(random_state=12345)) plt.colorbar() if pltshow == 0: plt.close() #else: #plt.show() QSO_msk = None import os if not os.path.exists( '/Users/jasminwashington/Google Drive File Stream/My Drive/usrp/git_repo/hostgal/py_tools/targets/shortlist/' + name + '/'): os.makedirs( '/Users/jasminwashington/Google Drive File Stream/My Drive/usrp/git_repo/hostgal/py_tools/targets/shortlist/' + name + '/') tag = '/Users/jasminwashington/Google Drive File Stream/My Drive/usrp/git_repo/hostgal/py_tools/targets/shortlist/' + name + '/' + name #tag = 'home/michelle/Desktop/' + name # for if you want to save the image to your computer source_result, ps_result, image_ps, image_host, error_map = fit_qso( QSO_img, psf_ave=psf, psf_std=None, source_params=source_params, QSO_msk=QSO_msk, fixcenter=fixcenter, pix_sz=pix_scale, no_MCMC=(run_MCMC == False), QSO_std=QSO_std, tag=tag, deep_seed=deep_seed, pltshow=pltshow, corner_plot=False, flux_ratio_plot=True, dump_result=run_MCMC, band=band) if pltshow == 0: plot_compare = False fits_plot = False else: plot_compare = True fits_plot = True # print("imps:") # print(image_ps) worm = np.array(image_ps[0]) # print(np.unique(image_ps)) image_ps = worm print("Source") print(source_result) result, flux_list = transfer_to_result(data=QSO_img, pix_sz=pix_scale, source_result=source_result, ps_result=ps_result, image_ps=image_ps, image_host=image_host, error_map=error_map, zp=zp, fixcenter=fixcenter, ID='Example', QSO_msk=QSO_msk, tag=tag, plot_compare=plot_compare, band=band) if QSO_msk is None: QSO_mask = QSO_img * 0 + 1 dmps = (flux_list[0] - flux_list[1]) * QSO_mask hdu = fits.PrimaryHDU(dmps) hdu.writeto( '/Users/jasminwashington/Google Drive File Stream/My Drive/usrp/git_repo/hostgal/py_tools/targets/shortlist/' + name + '/' + name + '_' + band + '.fits', overwrite=True) hdu1 = fits.PrimaryHDU(flux_list[0]) hdu1.writeto( '/Users/jasminwashington/Google Drive File Stream/My Drive/usrp/git_repo/hostgal/py_tools/targets/shortlist/' + name + '/' + name + '_' + band + '_all.fits', overwrite=True) return result '''
for j in range(len(obj)): dis = np.sqrt(np.sum((np.asarray(obj[j][0])-np.asarray(obj_temp[i][0]))**2)) if dis < (obj[j][1]+obj_temp[i][1])/2: count += 1 if count == 0: obj.append(obj_temp[i]) print("the number of nearby objs:", len(obj)) data_host_list = [] Iband_inf = [] # from regions import PixCoord, EllipsePixelRegion # from photutils import EllipticalAperture # aperture = EllipticalAperture(qso_info[0], a, b, theta=theta) # reg = EllipsePixelRegion(center=center, width=aperture.a*2, height=aperture.b*2, angle=theta) masks_0 = mask_obj(QSO_img_l[1], snr=1., exp_sz= 1.4, pltshow = 0, npixels=20, return_deblend = False) masks_1 = mask_obj(QSO_img_l[1], snr=1.4, exp_sz= 1.6, pltshow = 0, npixels=20, return_deblend = False) QSO_msk = masks_0[0] * (1-masks_1[1][0]) # obj.append(((8, 20), # 1.0812992677234345, # 0.8441567009970872)) plt.imshow(QSO_img*QSO_msk, origin='low', norm=LogNorm()) # for i in range(len(obj)): # obj_x, obj_y = len(QSO_img)/2 + obj[i][0][0] , len(QSO_img)/2+ obj[i][0][1] # plt.text(obj_x, obj_y, "obj{0}".format(i), fontsize=15, color='k') plt.show() obj = [] #%% for k in run_list:#len(band_seq)):
# -*- coding: utf-8 -*- """ Created on Fri Sep 7 14:39:05 2018 @author: dartoon Test create mask auto """ import numpy as np import astropy.io.fits as pyfits import matplotlib.pyplot as plt from matplotlib.colors import LogNorm import sys from mask_objects import mask_obj import os path = os.getcwd() ID = path.split('/')[-2] QSO_img = pyfits.getdata('test.fits') masks = mask_obj(img=QSO_img, exp_sz=1.2) obj_mask = masks[0] + masks[1] + masks[2] obj_mask = [obj_mask == 0][0] + 0 plt.imshow(obj_mask) print "the yellow area(==1) is the area to fit" #kwargs_numerics['mask'] = obj_mask
plt.show() print("The adopted PSF:") psf = kernel_util.cut_psf(psf, psf_size=kernel_size) plt.imshow(psf, norm=LogNorm(), origin='lower') plt.savefig('./zoutput/{0}_psf.png'.format(ID)) if pltshow == 0: plt.close() else: plt.show() from mask_objects import mask_obj _, _, deblend_sources = mask_obj(agn_image, snr=snratio, npixels=numberpixels, return_deblend=True) print("deblend image to find the ID for the Objects for the mask:") plt.imshow(deblend_sources, origin='lower', cmap=deblend_sources.cmap(random_state=12345)) plt.colorbar() plt.savefig('./zoutput/{0}_mask.png'.format(ID)) if pltshow == 0: plt.close() else: plt.show() if block_id == []: QSO_msk = np.ones_like(agn_image)
psf_size) / 2 # If want to cut to 81, agn_image[ct:-ct,ct:-ct] psf = psf[pct:-pct, pct:-pct] psf /= psf.sum() print "The adopted PSF:" plt.imshow(psf, norm=LogNorm(), origin='lower') if pltshow == 0: plt.close() else: plt.show() # ============================================================================= # Creat the QSO mask # ============================================================================= from mask_objects import mask_obj _, _, deblend_sources = mask_obj(agn_image, snr=1., npixels=200, return_deblend=True) print "deblend image to find the ID for the Objects for the mask:" plt.imshow(deblend_sources, origin='lower', cmap=deblend_sources.cmap(random_state=12345)) plt.colorbar() if pltshow == 0: plt.close() else: plt.show() # Based on the deblend_sources, it is the ID 1 and 2 to block: block_id = [] if block_id == []:
def itsfits(fitsfile, psffile, deepseed, fix_center, runMCMC): #Setting the fitting condition: deep_seed = deepseed #Set as True to put more seed and steps to fit. pltshow = 1 #Note that setting plt.ion() in line27, the plot won't show anymore if running in terminal. pix_scale = 0.168 fixcenter = fix_center run_MCMC = runMCMC zp = 27.0 fitfile = fits.open(fitsfile) psf = pyfits.getdata(psffile) QSO_img1 = fitfile[1].data #print(QSO_img1) #print(QSO_img1.shape) QSO_std1 = fitfile[3].data**0.5 frame_size = len(QSO_img1) cut_to = 70 ct = (frame_size - cut_to) / 2 #print(frame_size, ct) #QSO_img = QSO_img.data[ct:-ct, ct:-ct] #QSO_std = QSO_std.data[ct:-ct, ct:-ct] QSO_img = QSO_img1[ct:-ct, ct:-ct] QSO_std = QSO_std1[ct:-ct, ct:-ct] #print(QSO_img) #print(QSO_img.shape) #%% #============================================================================== # input the objects components and parameteres #============================================================================== objs, Q_index = detect_obj(QSO_img, pltshow=pltshow) qso_info = objs[Q_index] obj = [objs[i] for i in range(len(objs)) if i != Q_index] fixed_source = [] kwargs_source_init = [] kwargs_source_sigma = [] kwargs_lower_source = [] kwargs_upper_source = [] fixed_source.append({}) kwargs_source_init.append({ 'R_sersic': 0.3, 'n_sersic': 2., 'e1': 0., 'e2': 0., 'center_x': 0., 'center_y': 0. }) kwargs_source_sigma.append({ 'n_sersic': 0.5, 'R_sersic': 0.5, 'e1': 0.1, 'e2': 0.1, 'center_x': 0.1, 'center_y': 0.1 }) kwargs_lower_source.append({ 'e1': -0.5, 'e2': -0.5, 'R_sersic': 0.1, 'n_sersic': 0.3, 'center_x': -0.5, 'center_y': -0.5 }) kwargs_upper_source.append({ 'e1': 0.5, 'e2': 0.5, 'R_sersic': 3., 'n_sersic': 7., 'center_x': 0.5, 'center_y': 0.5 }) if len(obj) >= 1: for i in range(len(obj)): fixed_source.append({}) kwargs_source_init.append({ 'R_sersic': obj[i][1] * pix_scale, 'n_sersic': 2., 'e1': 0., 'e2': 0., 'center_x': -obj[i][0][0] * pix_scale, 'center_y': obj[i][0][1] * pix_scale }) kwargs_source_sigma.append({ 'n_sersic': 0.5, 'R_sersic': 0.5, 'e1': 0.1, 'e2': 0.1, 'center_x': 0.1, 'center_y': 0.1 }) kwargs_lower_source.append({ 'e1': -0.5, 'e2': -0.5, 'R_sersic': obj[i][1] * pix_scale / 5, 'n_sersic': 0.3, 'center_x': -obj[i][0][0] * pix_scale - 10, 'center_y': obj[i][0][1] * pix_scale - 10 }) kwargs_upper_source.append({ 'e1': 0.5, 'e2': 0.5, 'R_sersic': 3., 'n_sersic': 7., 'center_x': -obj[i][0][0] * pix_scale + 10, 'center_y': obj[i][0][1] * pix_scale + 10 }) source_params = [ kwargs_source_init, kwargs_source_sigma, fixed_source, kwargs_lower_source, kwargs_upper_source ] #%% # ============================================================================= # Creat the QSO mask # ============================================================================= from mask_objects import mask_obj _, _, deblend_sources = mask_obj(QSO_img, snr=1.2, npixels=50, return_deblend=True) print "deblend image to find the ID for the Objects for the mask:" plt.imshow(deblend_sources, origin='lower', cmap=deblend_sources.cmap(random_state=12345)) plt.colorbar() if pltshow == 0: plt.close() else: plt.show() QSO_msk = None tag = 'example' source_result, ps_result, image_ps, image_host, error_map = fit_qso( QSO_img, psf_ave=psf, psf_std=None, source_params=source_params, QSO_msk=QSO_msk, fixcenter=fixcenter, pix_sz=pix_scale, no_MCMC=(run_MCMC == False), QSO_std=QSO_std, tag=tag, deep_seed=deep_seed, pltshow=pltshow, corner_plot=False, flux_ratio_plot=True, dump_result=run_MCMC) if pltshow == 0: plot_compare = False fits_plot = False else: plot_compare = True fits_plot = True result = transfer_to_result(data=QSO_img, pix_sz=pix_scale, source_result=source_result, ps_result=ps_result, image_ps=image_ps, image_host=image_host, error_map=error_map, zp=zp, fixcenter=fixcenter, ID='Example', QSO_msk=QSO_msk, tag=tag, plot_compare=plot_compare) return result