Exemple #1
0
                           exptime=np.ones_like(data) * exptime,
                           if_plot=False,
                           zp=zp)  #Gain value assuming as 1
data_process.generate_target_materials(radius=65,
                                       create_mask=False,
                                       nsigma=2.8,
                                       if_select_obj=False,
                                       exp_sz=1.2,
                                       npixels=15,
                                       if_plot=True)
data_process.find_PSF(radius=30, user_option=True)
data_process.plot_overview(label='Example', target_label=None)

#Start to produce the class and params for lens fitting.
from galight.fitting_specify import FittingSpecify

# data_process.apertures = []
fit_sepc = FittingSpecify(data_process)
fit_sepc.prepare_fitting_seq(point_source_num=1)  #, fix_n_list= [[0,4],[1,1]])
fit_sepc.build_fitting_seq()

#Plot the initial settings for fittings.
fit_sepc.plot_fitting_sets()

#Setting the fitting method and run.
from galight.fitting_process import FittingProcess
fit_run = FittingProcess(fit_sepc, savename='savename', fitting_level='norm')
fit_run.run(algorithm_list=['PSO', 'PSO'])
fit_run.plot_final_galaxy_fit()
print(fit_run.final_result_galaxy[0])
print(fit_run.final_result_ps[0])
Exemple #2
0
    for i in run_list:  
        band = bands[i]
        print("Staring fitting band-"+band+"... ... ...")
        data_process_list[i].apertures = apertures #Pass apertures to the data
        fit_sepc_l[i] = FittingSpecify(data_process_list[i])
        fix_n_list, fix_Re_list = None, None
        if i != l_idx:
            if fix_n == True:
                fix_n_list = [[0,fit_run_l[l_idx].final_result_galaxy[0]['n_sersic'] ]]
            if fix_re == True:
                fix_Re_list = [[0,fit_run_l[l_idx].final_result_galaxy[0]['R_sersic'] ]]
        fit_sepc_l[i].prepare_fitting_seq(point_source_num = point_source_num, supersampling_factor=3, 
                                          fix_n_list= fix_n_list, fix_Re_list=fix_Re_list)
        fit_sepc_l[i].plot_fitting_sets(object_id+'/fitconfig-band-{0}.png'.format(band))
        fit_sepc_l[i].build_fitting_seq()
        fit_run_l[i] = FittingProcess(fit_sepc_l[i], savename = object_id+'/result-band-{0}'.format(band), fitting_level=fitting_level)
        fit_run_l[i].run(algorithm_list = ['PSO'], setting_list=[None])
        # fit_run.plot_all(target_ID = object_id)
        if fit_run_l[i].image_ps_list != []:
            fit_run_l[i].plot_final_qso_fit(save_plot=True, target_ID= object_id +'-'+ band )
        else:
            fit_run_l[i].plot_final_galaxy_fit(save_plot=True, target_ID= object_id +'-'+ band )
            
        fit_run_l[i].cal_astrometry()
        fit_run_l[i].dump_result()
        
        residual = fit_run_l[i].flux_2d_out['data'] - fit_run_l[i].flux_2d_out['model']
        pyfits.PrimaryHDU(residual).writeto(object_id+'/residual(data-model).fits',overwrite=True)
        
    
Exemple #3
0
                           zp=25)

data_process.PSF_list = [psf_class.kernel_pixel]

data_process.generate_target_materials(radius=25,
                                       create_mask=False,
                                       nsigma=2.8,
                                       exp_sz=1,
                                       npixels=10,
                                       if_plot=True,
                                       bkg_std=background_rms)
data_process.deltaPix = 1
data_process.checkout()  #Check if all the materials is known.
fit_sepc = FittingSpecify(data_process)
fit_sepc.prepare_fitting_seq(
    point_source_num=0, sersic_major_axis=False
)  #, 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)
fit_run.run(algorithm_list=['PSO', 'PSO'],
            setting_list=[{
                'sigma_scale': 1.,
                'n_particles': 50,
                'n_iterations': 50
            }] * 2)
fit_run.plot_final_galaxy_fit()
# fit_run.dump_result()
print(phi / np.pi * 180, fit_run.final_result_galaxy[0]['phi_G'] / np.pi * 180)
Exemple #4
0
                                             detect_tool='sep',
                                             create_mask=False,
                                             nsigma=2.8,
                                             exp_sz=1.2,
                                             npixels=15,
                                             if_plot=True)
    data_process_0.PSF_list = [PSF]
    data_process_0.checkout()  #Check if all the materials is known.

    fit_sepc_0 = FittingSpecify(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 + ID + '_single_Sersic')
    fit_run_0.run(algorithm_list=['PSO', 'PSO'],
                  setting_list=[{
                      'sigma_scale': 1.,
                      'n_particles': 50,
                      'n_iterations': 50
                  }] * 2)
    fit_run_0.plot_final_qso_fit()
    bic_0 = fit_run_0.fitting_seq.bic
    fit_run_0.dump_result()

#%%
fit_run_0.plot_final_qso_fit()
# fit_run_0.fitting_specify_class.plot_fitting_sets()
# obj_id = int(input('Which obj to measure using statmorph?\n'))
# morph = fit_run_0.cal_statmorph(obj_id=obj_id, if_plot=True)
Exemple #5
0
                                           nsigma=2.8,
                                           exp_sz=1.2,
                                           npixels=25,
                                           if_plot=False)
    import copy
    apr0 = copy.deepcopy(data_process.apertures[0])
    apr1 = copy.deepcopy(data_process.apertures[0])
    apr0.positions = np.array([48., 49.])
    apr1.positions = np.array([56., 48.])
    data_process.apertures = [apr0, apr1]
    # if rerun != 's21a':
    #     picklename = 'fit_result/'+'dual_result-band-{0}-s21a.pkl'.format(band)
    #     s21_res = pickle.load(open(picklename,'rb'))
    #     data_process.target_stamp -= s21_res.image_host_list[0]
    fit_sepc = FittingSpecify(data_process)
    fit_sepc.prepare_fitting_seq(point_source_num=point_source_num,
                                 supersampling_factor=3)
    fit_sepc.plot_fitting_sets(
        object_id + '_fitconfig-band-{0}-{1}.png'.format(band, rerun))
    fit_sepc.build_fitting_seq()
    # if rerun != 's19a':
    #     fit_sepc.kwargs_params['point_source_model'] = s21_res.fitting_specify_class.kwargs_params['point_source_model']
    fit_run = FittingProcess(fit_sepc,
                             savename=object_id +
                             '_result-band-{0}-{1}'.format(band, rerun),
                             fitting_level=fitting_level)
    fit_run.run(algorithm_list=['PSO', 'PSO'], setting_list=[None, None])
    fit_run.plot_final_qso_fit(save_plot=True,
                               target_ID=object_id + '-' + band)
    fit_run.dump_result()
Exemple #6
0
data_process.apertures = apertures  #Pass apertures to the data

from galight.tools.measure_tools import plot_data_apertures

plot_data_apertures(data_process.target_stamp, apertures)

#%%Start to produce the class and params for lens fitting.
from galight.fitting_specify import FittingSpecify

fit_sepc = FittingSpecify(data_process)

fit_sepc.prepare_fitting_seq(
    point_source_num=1,
    fix_n_list=[
        [0, 4], [1, 1]
    ],  #fix_Re_list= [[0,0.2], [1,1.14]],  #First component fix n = 4 (bluge), second one fix to 1 (disk).
    fix_center_list=[[0, 0]])

fit_sepc.plot_fitting_sets()

fit_sepc.build_fitting_seq()

# #%%Setting the fitting method and run.
from galight.fitting_process import FittingProcess

fit_run = FittingProcess(fit_sepc, savename='test_bulge_disk.pkl')
fit_run.run()
fit_run.plot_all()
fit_run.dump_result()
print(fit_run.final_result_galaxy[:2])
Exemple #7
0
     if fix_re == True:
         fix_Re_list = [[
             0, fit_run_l[l_idx].final_result_galaxy[0]['R_sersic']
         ]]
 fit_sepc_l[i].prepare_fitting_seq(
     point_source_num=point_source_num,
     supersampling_factor=3,
     ps_pix_center_list=ps_pix_center_list,
     fix_n_list=fix_n_list,
     fix_Re_list=fix_Re_list)
 fit_sepc_l[i].plot_fitting_sets(out_dir +
                                 '/fitconfig-band-{0}.png'.format(band),
                                 show_plot=show_plot)
 fit_sepc_l[i].build_fitting_seq()
 fit_run_l[i] = FittingProcess(fit_sepc_l[i],
                               savename=out_dir +
                               '/result-band-{0}'.format(band),
                               fitting_level=fitting_level)
 fit_run_l[i].run(algorithm_list=['PSO'], setting_list=[None])
 for j in range(5):  #!!!Will be removed after Lenstrnomy debug.
     if np.sum(fit_run_l[i].image_host_list[0]) != 0:
         continue
     else:
         cut_radius = cut_radius - 1
         data_process_list[i].generate_target_materials(
             radius=cut_radius)
         data_process_list[i].checkout()
         data_process_list[
             i].apertures = apertures  #Pass apertures to the data
         fit_sepc_ = FittingSpecify(data_process_list[i])
         fit_sepc_.prepare_fitting_seq(
             point_source_num=point_source_num,
Exemple #8
0
 data_process.apertures = [data_process.apertures[0]]
 info = {}
 for psf, name in [[psf_true, 'same_psf'], [psf_model, 'diff_psf']]:
     # for psf, name in [[psf_model,'diff_psf']]:
     plot_fit_name = filename + name + '_fit'
     data_process.PSF_list = [psf]
     #Start to produce the class and params for lens fitting.
     fit_sepc = FittingSpecify(data_process)
     fit_sepc.prepare_fitting_seq(
         point_source_num=1)  #, fix_n_list= [[0,4],[1,1]])
     fit_sepc.build_fitting_seq()
     #Plot the initial settings for fittings.
     # fit_sepc.plot_fitting_sets()
     #
     fit_run = FittingProcess(fit_sepc,
                              savename=plot_fit_name,
                              fitting_level='norm')
     fit_run.run(algorithm_list=['PSO', 'PSO'])
     if fit_run.reduced_Chisq > 1.3:
         fit_run = FittingProcess(fit_sepc,
                                  savename=plot_fit_name,
                                  fitting_level='deep')
         fit_run.run(algorithm_list=['PSO', 'PSO'])
     info['inferred_host_flux_' +
          name] = fit_run.final_result_galaxy[0]['flux_within_frame']
     info['inferred_magnitude_' +
          name] = fit_run.final_result_galaxy[0]['magnitude']
     info['inferred_R_sersic_' +
          name] = fit_run.final_result_galaxy[0]['R_sersic']
     info['inferred_n_sersic_' +
          name] = fit_run.final_result_galaxy[0]['n_sersic']
Exemple #9
0
# data_process.apertures = []

fit_sepc = FittingSpecify(data_process)
fit_sepc.prepare_fitting_seq(point_source_num = 1) #, fix_n_list= [[0,4],[1,1]])
# psf_error_map = np.ones_like(data_process.PSF_list[data_process.psf_id_for_fitting]) *0.01 # It is in the variance unit (std^2).
# fit_sepc.prepare_fitting_seq(point_source_num = 1, psf_error_map = psf_error_map)

fit_sepc.build_fitting_seq()

#Plot the initial settings for fittings. 
fit_sepc.plot_fitting_sets()

#%%Setting the fitting method and run.
from galight.fitting_process import FittingProcess
fit_run = FittingProcess(fit_sepc, savename = 'savename', fitting_level='norm')
fit_run.run(algorithm_list = ['PSO', 'MCMC','MCMC'], setting_list=[None,{'n_burn': 100, 'n_run': 100, 'walkerRatio': 10,'sigma_scale': .1},
                                                                   {'n_burn': 200, 'n_run': 200, 'walkerRatio': 10,'sigma_scale': .1}])
            # setting_list = [{'sigma_scale': 1., 'n_particles': 100, 'n_iterations': 100}, {'n_burn': 100, 'n_run': 100, 'walkerRatio': 10,'sigma_scale': .1}])
fit_run.plot_all()

fit_run.dump_result()
print(fit_run.final_result_galaxy[0])

#%%
# Test load pkl
import pickle
picklename = 'savename.pkl'
fitting_run_class = pickle.load(open(picklename,'rb'))
fitting_run_class.run_diag()
                           if_plot=False,
                           zp=zp)

data_process.generate_target_materials(radius=None,
                                       create_mask=False,
                                       nsigma=2.8,
                                       exp_sz=1.2,
                                       npixels=15,
                                       if_plot=True)

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 galight.fitting_specify import FittingSpecify
fit_sepc = FittingSpecify(data_process)
fit_sepc.prepare_fitting_seq(
    point_source_num=1,
    supersampling_factor=3)  #, 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.
from galight.fitting_process import FittingProcess
fit_run = FittingProcess(fit_sepc, savename='HSC_QSO', fitting_level='deep')
fit_run.run(algorithm_list=['PSO'], setting_list=[None])
# fit_run.plot_all()
# fit_run.dump_result()
print(fit_run.final_result_galaxy[0])
Exemple #11
0
# plt.imshow(data_process.segm_deblend, origin='lower')
# plt.colorbar()
# plt.show()

#%%Start to produce the class and params for lens fitting.
from galight.fitting_specify import FittingSpecify
fit_sepc = FittingSpecify(data_process)
fit_sepc.prepare_fitting_seq(
    point_source_num=1,
    supersampling_factor=3)  #, 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.
from galight.fitting_process import FittingProcess
fit_run = FittingProcess(fit_sepc, savename='HSC_QSO', fitting_level='deep')
# fit_run.run(algorithm_list = ['PSO'], setting_list=[None])
# # fit_run.plot_all()
# fit_run.dump_result()
# print(fit_run.final_result_galaxy[0])

#%%
import pickle
from galight.tools.asymmetry_tools import Measure_asy
from galight.tools.astro_tools import plt_fits

fit_run_pkl = fit_run
obj_id = 0
asy_class = Measure_asy(
    fit_run_pkl,
    seg_cal_reg='or',
    add_aperture0.a, add_aperture0.b = add_aperture0.a / 6, add_aperture0.a / 4
    apertures = apertures[:comp_id + 1] + [add_aperture0
                                           ] + apertures[comp_id + 1:]
    data_process_1.apertures = apertures  #Pass apertures to the data
    fit_sepc_1 = FittingSpecify(data_process_1)
    fit_sepc_1.prepare_fitting_seq(
        point_source_num=
        0,  #fix_n_list= [[0,1]],  #First component fix n = 4 (bluge), second one fix to 1 (disk).
        condition=condition_diskbulge)

    fit_sepc_1.plot_fitting_sets()

    fit_sepc_1.build_fitting_seq()
    # fit_sepc_1.kwargs_params['lens_light_model'][0][0] = fit_run_0.final_result_galaxy[0] #Input to bulge

    fit_run_1 = FittingProcess(fit_sepc_1, savename=save_name + 'bulge+disk')

    fit_run_1.run(algorithm_list=['PSO', 'PSO'],
                  setting_list=[{
                      'sigma_scale': 1.,
                      'n_particles': 150,
                      'n_iterations': 150
                  }] * 2)
    bic_1 = fit_run_1.fitting_seq.bic
    fit_run_1.dump_result()
    print(fit_run_1.final_result_galaxy[0]['q'],
          fit_run_1.final_result_galaxy[1]['q'])
    fit_run_1.plot_final_galaxy_fit(target_ID=ID)
    bulge1 = fit_run_1.image_host_list[1]
    disk1 = fit_run_1.image_host_list[0]
    B2T = np.sum(bulge1) / np.sum(bulge1 + disk1)
Exemple #13
0
    # 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 = FittingSpecify(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)
    comp_id = 0  #Change the component (galaxy) id = 0 into to components (i.e., bulge + disk)
    add_aperture0 = copy.deepcopy(apertures[comp_id])
    add_aperture0.a, add_aperture0.b = add_aperture0.a / 3, add_aperture0.b / 3
    apertures = apertures[:comp_id] + [add_aperture0] + apertures[comp_id:]
    data_process_1.apertures = apertures  #Pass apertures to the data
    fit_sepc_1 = FittingSpecify(data_process_1)
    fit_sepc_1.prepare_fitting_seq(
Exemple #14
0
#Setting the fitting method and run.
import glob, pickle
if glob.glob(savename + '*pkl') == []:
    # apertures = copy.deepcopy(data_process.apertures)
    # apertures = [copy.deepcopy(apertures[0])] + apertures
    # apertures[0].a, apertures[0].b = apertures[0].a*0.2,  apertures[0].b*0.2
    # data_process.apertures = apertures
    from galight.fitting_specify import FittingSpecify
    fit_sepc = FittingSpecify(data_process)
    fit_sepc.prepare_fitting_seq(
        point_source_num=0)  #, fix_n_list= [[0,4],[1,1]])
    fit_sepc.build_fitting_seq()
    fit_sepc.plot_fitting_sets(
    )  #The apertures shows how the images will be modelled.
    from galight.fitting_process import FittingProcess
    fit_run = FittingProcess(fit_sepc, savename=savename, fitting_level='deep')
    fit_run.run(algorithm_list=['PSO'], setting_list=[None])
    fit_run.dump_result()
else:
    fit_run = pickle.load(open(glob.glob(savename + '*.pkl')[0], 'rb'))
    fit_run.fitting_specify_class.plot_fitting_sets()

file_header = copy.deepcopy(fitsFile[1].header)
file_header['CRPIX1'] = file_header['CRPIX1'] - data_process.target_pos[
    0] + len(data_process.target_stamp) / 2
file_header['CRPIX2'] = file_header['CRPIX2'] - data_process.target_pos[
    1] + len(data_process.target_stamp) / 2

fit_run.plot_all()
# fit_run.plot_final_galaxy_fit()
res = (fit_run.flux_2d_out['data'] - fit_run.flux_2d_out['model'])  #* masks
Exemple #15
0
                                               if_plot=False)

        plt_fits(data_process.PSF_list[0])
        # data_process.apertures = [] #Assuming there is no host (i.e., as constant.) #!!!

        # Manually input the PSF:
        # data_process.PSF_list = [PSF]

        # Check if all the materials is given, if so to pass to the next step.
        data_process.checkout()  #Check if all the materials is known.
        fit_sepc = FittingSpecify(data_process)
        # fit_sepc.prepare_fitting_seq(point_source_num = 2)
        fit_sepc.prepare_fitting_seq(point_source_num=2,
                                     ps_pix_center_list=[[-2.0, -1.0],
                                                         [6.0, -1.0]])
        # if fit_sepc.kwargs_params['point_source_model'][0][0] == fit_sepc.kwargs_params['point_source_model'][0][1]:
        #     fit_sepc.prepare_fitting_seq(point_source_num = 2, ps_pix_center_list= [[-2.0, -1.0], [6.0, -1.0]])
        #     print(file)
        fit_sepc.build_fitting_seq()
        fit_sepc.plot_fitting_sets()
        # if rerun != 's19a':
        #     fit_sepc.kwargs_params['point_source_model'] = s21_res.fitting_specify_class.kwargs_params['point_source_model']
        fit_run = FittingProcess(fit_sepc,
                                 savename=name +
                                 '-{0}-band{1}'.format(folder, band),
                                 fitting_level='norm')
        fit_run.run(algorithm_list=['PSO', 'PSO'], setting_list=[None, None])
        fit_run.plot_final_qso_fit(save_plot=True,
                                   target_ID=folder + '-' + band)
        fit_run.dump_result()