Iband_inf = source_result # in order to save the I band source_reslt to fix Reff and n for other band. if pltshow == 0: plot_compare = False fits_plot = False else: plot_compare = True fits_plot = True # for the plot propose: _ = 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_list[k], fixcenter=fixcenter, ID=image_ID + band_seq[k], QSO_msk=None, tag=tag, plot_compare=plot_compare) # result = transfer_obj_to_result(source_result[0], image_host[0], zp) result['redu_Chisq'] = reduced_chisq fitsFile = pyfits.open(image_folder + filename_list[k]) file_header = copy.deepcopy(fitsFile[1].header) file_header['CRPIX1'] = file_header['CRPIX1'] - qso_center_list[k][ 0] + len(QSO_img) / 2 file_header['CRPIX2'] = file_header['CRPIX2'] - qso_center_list[k][ 1] + len(QSO_img) / 2
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 '''
corner_plot=False, flux_ratio_plot=True, dump_result=run_MCMC, pso_diag=True) 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[0], image_host=image_host, error_map=error_map, zp=zp, fixcenter=fixcenter, ID='ID' + repr(ID), tag=tag, plot_compare=plot_compare) print("Saving results:") pickle.dump([result, framesize], open(folder + '/{0}.pkl'.format(save_name), 'wb')) filtname = folder + '/{0}.txt'.format(save_name) result_file = open(filtname, 'w') result_file.write(repr(result)) result_file.close() # #%% # print("The truth:")
# QSO_msk = QSO_msk # else: # QSO_msk = QSO_img*0 + 1 # label = ['data', 'QSO', '{0} Sersics'.format(len(image_host)), 'model', 'normalized residual'] # flux_list = [QSO_img, image_ps, host, multi_band_list[0][0]['noise_map']] # fig = total_compare_6(label_list = label, flux_list = flux_list, target_ID = ID+'-'+band, pix_sz=pix_scale, # data_mask_list = [], data_cut = 0, zp = 27.0, # plot_compare = True, msk_image = QSO_msk) # fig.savefig("{0}_SB_profile.pdf".format(ID+'-'+band), bbox_inches = 'tight') _ = 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=multi_band_list[0][0]['noise_map'], zp=27.0, fixcenter=True, ID=ID + '-' + band, QSO_msk=QSO_msk, tag='021930.51-055643.0/fit_image_021930.51-055643.0_HSC-' + band, plot_compare=True) # #%% Recover the plot # from lenstronomy.Plots.model_plot import ModelPlot # modelPlot = ModelPlot(multi_band_list, kwargs_model, kwargs_result, # arrow_size=0.02, cmap_string="gist_heat", likelihood_mask_list=[QSO_msk]) # f, axes = plt.subplots(3, 3, figsize=(16, 16), sharex=False, sharey=False) # modelPlot.data_plot(ax=axes[0,0], text="Data") # modelPlot.model_plot(ax=axes[0,1]) # modelPlot.normalized_residual_plot(ax=axes[0,2], v_min=-6, v_max=6)
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
modelPlot.subtract_from_data_plot(ax=axes[2,0], text='Data - Point Source', point_source_add=True) modelPlot.subtract_from_data_plot(ax=axes[2,1], text='Data - host galaxy', source_add=True) modelPlot.subtract_from_data_plot(ax=axes[2,2], text='Data - host galaxy - Point Source', source_add=True, point_source_add=True) f.tight_layout() plt.show() import sys sys.path.insert(0, '../../../py_tools/') from transfer_to_result import transfer_to_result if len(image_ps) >1: image_ps = np.sum(image_ps, axis=0) else: image_ps = image_ps[0] _ = 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=multi_band_list[0][0]['noise_map'], zp=27.0, fixcenter=True,ID='Data', QSO_msk = QSO_msk, tag=None, plot_compare = True) #%%Recover the translated cornor plot plt.imshow(QSO_img, origin='low', norm=LogNorm()) for i in range(len(ps_result)): obj_x, obj_y = len(QSO_img)/2 - ps_result[i]['ra_image'][0]/pix_scale, len(QSO_img)/2+ps_result[i]['dec_image'][0]/pix_scale # print(obj_x, obj_y) plt.text(obj_x, obj_y, "QSO{0}".format(i), fontsize=10, color='k') plt.show() plt.imshow(QSO_img, origin='low', norm=LogNorm()) for i in range(len(source_result)): obj_x, obj_y = len(QSO_img)/2 - source_result[i]['center_x']/pix_scale, len(QSO_img)/2+source_result[i]['center_y']/pix_scale plt.text(obj_x, obj_y, "obj{0}".format(i), fontsize=15, color='k')