data_process.noise_map = err_data try: data_process.generate_target_materials(radius=None, create_mask=False, nsigma=1.5, exp_sz=1.2, npixels=15, if_plot=False) except: continue data_process.PSF_list = [PSF] data_process.checkout() #Check if all the materials is known. #Start to produce the class and params for lens fitting. fit_sepc = FittingSpeficy(data_process) fit_sepc.prepare_fitting_seq( point_source_num=1 ) #, fix_n_list= [[0,4]], fix_center_list = [[0,0]]) fit_sepc.plot_fitting_sets() fit_sepc.build_fitting_seq() #Setting the fitting method and run. fit_run = FittingProcess(fit_sepc, savename='model_result/' + ids_file[i].split('/')[-1][:-5]) fit_run.run(algorithm_list=['PSO'], setting_list=[None]) fit_run.plot_final_qso_fit(save_plot=True, target_ID=ids_file[i].split('/')[-1][:-5]) fit_run.dump_result() # print(fit_run.final_result_galaxy[0])
file_header['CRPIX1'] = file_header['CRPIX1'] - qso_center[0] + int( len(data_process_list[k].target_stamp) / 2) file_header['CRPIX2'] = file_header['CRPIX2'] - qso_center[1] + int( len(data_process_list[k].target_stamp) / 2) wcs = WCS(file_header) write_result = open( fit_folder + 'fit_result_{0}-band.txt'.format(band_seq[k]), 'w') write_result.write("#The fitting information for {0}:\n".format( filename_list[k])) #============================================================================== # fitting the QSO as a BH (PS) + Sersic #============================================================================== print("fitting the QSO as one BH + Sersic ") tag = fit_folder + 'fit_{0}-band_fit0_PS+Sersic'.format(band_seq[k]) _fit_sepc_0 = FittingSpeficy(data_process_list[k]) _fit_sepc_0.prepare_fitting_seq(point_source_num=1, fix_n_list=fix_n_list_0, fix_Re_list=fix_Re_list_0, ps_params=ps_param0, apertures_center_focus=True) _fit_sepc_0.build_fitting_seq() if k == run_list[0]: _fit_sepc_0.plot_fitting_sets( savename=fit_folder + 'fitting0_used_aper.pdf'.format(image_ID), show_plot=show_plot) _fit_run_0 = FittingProcess(_fit_sepc_0, savename=tag) _fit_run_0.run(algorithm_list=['PSO'], setting_list=[pso_setting]) _fit_run_0.translate_result()
# write_result.write("\nmodel_PS_result: "+repr(ps_result_1)) # write_result.write("\n=======================================================\n") # tag_name = tag + "_qso_final_plot" # print(call("mv {0} {1}".format(tag_name+'.pdf', tag+"_chisq_"+repr(round(reduced_Chisq_1,1)))+'.pdf', shell=True)) # if band_seq[k] == 'I': #Get the parameters of I band to fix to the other band. # fix_Re_list_1 = [[i, source_result_1[i]['R_sersic']] for i in range(len(source_result_1))] # fix_n_list_1 = [[i, source_result_1[i]['n_sersic']] for i in range(len(source_result_1))] # ps_param1 = _fit_sepc_1.ps_params #============================================================================== # fitting the QSO as a BHBH (PS+PS) + Sersic #============================================================================== print("fitting the QSO as {0} point sources + Sersic".format(len(arr_x))) num_BHBH = max(len(arr_x), 2) fit_time = 1 tag = 'fit_result_detect/{0}/fit_{2}-band_image2_PSPS+Sersic_fittime-{1}'.format(qsoid,fit_time+1, band_seq[k]) _fit_sepc_2 = FittingSpeficy(data_process_list[k]) _fit_sepc_2.prepare_fitting_seq(point_source_num = num_BHBH, fix_n_list= fix_n_list_2, fix_Re_list= fix_Re_list_2, ps_params = ps_param2, neighborhood_size = neighborhood_size, threshold = threshold) _fit_sepc_2.build_fitting_seq() if k == run_list[0]: _fit_sepc_2.plot_fitting_sets(savename = 'fit_result_detect/{0}/fitting_used_aper.pdf'.format(qsoid), show_plot=show_plot) _fit_run_2 = FittingProcess(_fit_sepc_2, savename = tag) _fit_run_2.run(algorithm_list = ['PSO'], setting_list= [pso_setting]) _fit_run_2.translate_result() _fit_run_2.plot_final_qso_fit(target_ID = qsoid, save_plot = True, show_plot = show_plot) source_result_2, ps_result_2 = _fit_run_2.final_result_galaxy, _fit_run_2.final_result_ps host_mag = source_result_2[0]['magnitude'] AGN_mags = [ps_result_2[i]['magnitude'] for i in range(len(ps_result_2))] if len(ps_result_2) == 2: c_miss = np.sqrt((ps_result_2[0]['ra_image']-ps_result_2[1]['ra_image'])**2+(ps_result_2[0]['dec_image']-ps_result_2[1]['dec_image'])**2) elif len(ps_result_2) > 2:
data_process_0.PSF_list = [psf_data] data_process_0.checkout() #Check if all the materials is known. #%%Start to produce the class and params for lens fitting. # #Manually input another component: # apertures_0 = copy.deepcopy(data_process_0.apertures) # add_aperture1 = copy.deepcopy(apertures_0[0]) # add_pos = [60, 60] #The position of the component. # if isinstance(add_aperture1.positions[0],float): # add_aperture1.positions = np.array(add_pos) # elif isinstance(add_aperture1.positions[0],np.ndarray): # add_aperture1.positions = np.array([add_pos]) # add_aperture1.a, add_aperture1.b = 2, 2 #define the a, b value of this component, i.e., Reff = sqrt(a^2 +b^2) # apertures_0 = apertures_0 + [add_aperture1] #attach the added aperture into the group. # data_process_0.apertures = apertures_0 #Pass apertures to the data fit_sepc_0 = FittingSpeficy(data_process_0) fit_sepc_0.prepare_fitting_seq( point_source_num=1) #, fix_n_list= [[0,4]], fix_center_list = [[0,0]]) # fit_sepc_0.plot_fitting_sets() fit_sepc_0.build_fitting_seq() #Setting the fitting method and run. fit_run_0 = FittingProcess(fit_sepc_0, savename=save_name + 'single_Sersic') fit_run_0.run(algorithm_list=['PSO'], setting_list=[None]) fit_run_0.plot_final_qso_fit() bic_0 = fit_run_0.fitting_seq.bic fit_run_0.dump_result() #%%Fitting as disk + bulge: data_process_1 = copy.deepcopy(data_process_0) apertures = copy.deepcopy(data_process_1.apertures)
data_process_0.checkout() #Check if all the materials is known. #%%Start to produce the class and params for lens fitting. # #Manually input another component: # apertures_0 = copy.deepcopy(data_process_0.apertures) # add_aperture1 = copy.deepcopy(apertures_0[0]) # add_pos = [60, 60] #The position of the component. # if isinstance(add_aperture1.positions[0],float): # add_aperture1.positions = np.array(add_pos) # elif isinstance(add_aperture1.positions[0],np.ndarray): # add_aperture1.positions = np.array([add_pos]) # add_aperture1.a, add_aperture1.b = 2, 2 #define the a, b value of this component, i.e., Reff = sqrt(a^2 +b^2) # apertures_0 = apertures_0 + [add_aperture1] #attach the added aperture into the group. # data_process_0.apertures = apertures_0 #Pass apertures to the data fit_sepc_0 = FittingSpeficy(data_process_0) fit_sepc_0.prepare_fitting_seq( point_source_num=1) #, fix_n_list= [[0,4]], fix_center_list = [[0,0]]) fit_sepc_0.plot_fitting_sets() fit_sepc_0.build_fitting_seq() #Setting the fitting method and run. fit_run_0 = FittingProcess(fit_sepc_0, savename=save_name + 'single_Sersic') fit_run_0.run(algorithm_list=['PSO'], setting_list=[None]) fit_run_0.plot_final_qso_fit() bic_0 = fit_run_0.fitting_seq.bic fit_run_0.dump_result() #%%Fitting as disk + bulge: data_process_1 = copy.deepcopy(data_process_0) apertures = copy.deepcopy(data_process_1.apertures)
qsoid)) == True and fit_data == True: print("Fiting the: " + filename_list[k]) print(claim) print("Comparing the fitting Chisq:") write_result = open( 'fit_result_detect/{0}/fit_result.txt'.format(qsoid), 'w') write_result.write("#The fitting information:\n") #============================================================================== # fitting the QSO as a BH (PS) + Sersic #============================================================================== for ft in range(1): #The fitting rounds for each band print("fitting the QSO as one BH + Sersic ") fit_time = ft tag = 'fit_result_detect/{0}/fit_image0_PS+Sersic_fittime-{1}'.format( qsoid, fit_time + 1) _fit_sepc = FittingSpeficy(data_process_list[k]) _fit_sepc.prepare_fitting_seq(point_source_num=1) _fit_sepc.build_fitting_seq() _fit_run = FittingProcess(_fit_sepc, savename=tag) _fit_run.run(algorithm_list=['PSO'], setting_list=[None]) _fit_run.translate_result() _fit_run.plot_final_qso_fit(target_ID=qsoid, save_plot=True, show_plot=show_plot) source_result_0, ps_result_0 = _fit_run.final_result_galaxy, _fit_run.final_result_ps host_mag, AGN_mag = source_result_0[0]['magnitude'], ps_result_0[ 0]['magnitude'] c_miss = np.sqrt((source_result_0[0]['center_x'] - ps_result_0[0]['ra_image'])**2 + (source_result_0[0]['center_y'] - ps_result_0[0]['dec_image'])**2)
data_process.generate_target_materials( radius=None, create_mask=False, nsigma=2.8, #radius=None, the target size would be automated set. exp_sz=1.2, npixels=15, if_plot=False) data_process.PSF_list = [PSF] data_process.checkout() #Check if all the materials is known. #%%Start to produce the class and params for lens fitting. from decomprofile.fitting_specify import FittingSpeficy fit_sepc = FittingSpeficy(data_process) fit_sepc.prepare_fitting_seq(point_source_num=0) fit_sepc.build_fitting_seq() #%%Setting the fitting method and run. from decomprofile.fitting_process import FittingProcess savename = image_id + '-' + band fit_run = FittingProcess(fit_sepc, savename=savename) fit_run.run(algorithm_list=['PSO'], setting_list=[None]) #Only PSO, not MCMC # fit_run.plot_all() fit_run.plot_final_galaxy_fit(target_ID=savename, show_plot=False) fit_run.translate_result() # fit_run.dump_result() #To save result as pickle file # print(fit_run.final_result_galaxy[0]) #%%