def simulate_simple(image_model_class, kwargs_lens=None, kwargs_source=None, kwargs_lens_light=None, kwargs_ps=None, no_noise=False, source_add=True, lens_light_add=True, point_source_add=True): """ :param image_model_class: :param kwargs_lens: :param kwargs_source: :param kwargs_lens_light: :param kwargs_ps: :param no_noise: :param source_add: :param lens_light_add: :param point_source_add: :return: """ image = image_model_class.image(kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps, source_add=source_add, lens_light_add=lens_light_add, point_source_add=point_source_add) # add noise if no_noise: return image else: poisson = image_util.add_poisson( image, exp_time=image_model_class.Data.exposure_map) bkg = image_util.add_background( image, sigma_bkd=image_model_class.Data.background_rms) return image + bkg + poisson
def simulate(self, image_model_class, kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps, no_noise=False, source_add=True, lens_light_add=True, point_source_add=True): """ simulate image :param kwargs_options: :param kwargs_data: :param kwargs_psf: :param kwargs_lens: :param kwargs_source: :param kwargs_lens_light: :param kwargs_ps: :param no_noise: :return: """ image = image_model_class.image(kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps, source_add=source_add, lens_light_add=lens_light_add, point_source_add=point_source_add) #image = makeImage.ImageNumerics.array2image(image) # add noise if no_noise: return image else: poisson = image_util.add_poisson(image, exp_time=image_model_class.Data.exposure_map) bkg = image_util.add_background(image, sigma_bkd=image_model_class.Data.background_rms) return image + bkg + poisson
def generate_lens(sigma_bkg=sigma_bkg, exp_time=exp_time, numPix=numPix, deltaPix=deltaPix, fwhm=fwhm, psf_type=psf_type, kernel_size=kernel_size, z_source=z_source, z_lens=z_lens, phi_ext=phi_ext, gamma_ext=gamma_ext, theta_E=theta_E, gamma_lens=gamma_lens, e1_lens=e1_lens, e2_lens=e2_lens, center_x_lens_light=center_x_lens_light, center_y_lens_light=center_y_lens_light, source_x=source_y, source_y=source_y, q_source=q_source, phi_source=phi_source, center_x=center_x, center_y=center_y, amp_source=amp_source, R_sersic_source=R_sersic_source, n_sersic_source=n_sersic_source, phi_lens_light=phi_lens_light, q_lens_light=q_lens_light, amp_lens=amp_lens, R_sersic_lens=R_sersic_lens, n_sersic_lens=n_sersic_lens, amp_ps=amp_ps, supersampling_factor=supersampling_factor, v_min=v_min, v_max=v_max, cosmo=cosmo, cosmo2=cosmo2, lens_pos_eq_lens_light_pos=True, same_cosmology=True): kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg) data_class = ImageData(**kwargs_data) kwargs_psf = {'psf_type': psf_type, 'pixel_size': deltaPix, 'fwhm': fwhm} psf_class = PSF(**kwargs_psf) cosmo = FlatLambdaCDM(H0=75, Om0=0.3, Ob0=0.) cosmo = FlatLambdaCDM(H0=65, Om0=0.3, Ob0=0.) gamma1, gamma2 = param_util.shear_polar2cartesian(phi=phi_ext, gamma=gamma_ext) kwargs_shear = {'gamma1': gamma1, 'gamma2': gamma2} if lens_pos_eq_lens_light_pos: center_x = center_x_lens_light center_y = center_y_lens_light if same_cosmology: cosmo2 = cosmo kwargs_spemd = { 'theta_E': theta_E, 'gamma': gamma_lens, 'center_x': center_x, 'center_y': center_y, 'e1': e1_lens, 'e2': e2_lens } lens_model_list = ['SPEP', 'SHEAR'] kwargs_lens = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list, z_lens=z_lens, z_source=z_source, cosmo=cosmo) lens_model_class2 = LensModel(lens_model_list=lens_model_list, z_lens=z_lens, z_source=z_source, cosmo=cosmo2) e1_source, e2_source = param_util.phi_q2_ellipticity(phi_source, q_source) kwargs_sersic_source = { 'amp': amp_source, 'R_sersic': R_sersic_source, 'n_sersic': n_sersic_source, 'e1': e1_source, 'e2': e2_source, 'center_x': source_x, 'center_y': source_y } source_model_list = ['SERSIC_ELLIPSE'] kwargs_source = [kwargs_sersic_source] source_model_class = LightModel(light_model_list=source_model_list) ## e1_lens_light, e2_lens_light = param_util.phi_q2_ellipticity( phi_lens_light, q_lens_light) kwargs_sersic_lens = { 'amp': amp_lens, 'R_sersic': R_sersic_lens, 'n_sersic': n_sersic_lens, 'e1': e1_lens_light, 'e2': e2_lens_light, 'center_x': center_x_lens_light, 'center_y': center_y_lens_light } lens_light_model_list = ['SERSIC_ELLIPSE'] kwargs_lens_light = [kwargs_sersic_lens] lens_light_model_class = LightModel(light_model_list=lens_light_model_list) ## lensEquationSolver = LensEquationSolver(lens_model_class) x_image, y_image = lensEquationSolver.findBrightImage( source_x, source_y, #position of ps kwargs_lens, #lens proporties numImages=4, #expected number of images min_distance=deltaPix, #'resolution' search_window=numPix * deltaPix) #search window limits mag = lens_model_class.magnification( x_image, y_image, #for found above ps positions kwargs=kwargs_lens) # and same lens properties kwargs_ps = [{ 'ra_image': x_image, 'dec_image': y_image, 'point_amp': np.abs(mag) * amp_ps }] point_source_list = ['LENSED_POSITION'] point_source_class = PointSource(point_source_type_list=point_source_list, fixed_magnification_list=[False]) kwargs_numerics = {'supersampling_factor': supersampling_factor} imageModel = ImageModel( data_class, # take generated above data specs psf_class, # same for psf lens_model_class, # lens model (gal+ext) source_model_class, # sourse light model lens_light_model_class, # lens light model point_source_class, # add generated ps images kwargs_numerics=kwargs_numerics) image_sim = imageModel.image(kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps) poisson = image_util.add_poisson(image_sim, exp_time=exp_time) bkg = image_util.add_background(image_sim, sigma_bkd=sigma_bkg) image_sim = image_sim + bkg + poisson data_class.update_data(image_sim) kwargs_data['image_data'] = image_sim cmap_string = 'gray' cmap = plt.get_cmap(cmap_string) cmap.set_bad(color='b', alpha=1.) cmap.set_under('k') f, axes = plt.subplots(1, 1, figsize=(6, 6), sharex=False, sharey=False) ax = axes im = ax.matshow(np.log10(image_sim), origin='lower', vmin=v_min, vmax=v_max, cmap=cmap, extent=[0, 1, 0, 1]) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax.autoscale(False) plt.show() kwargs_model = { 'lens_model_list': lens_model_list, 'lens_light_model_list': lens_light_model_list, 'source_light_model_list': source_model_list, 'point_source_model_list': point_source_list } from lenstronomy.Analysis.td_cosmography import TDCosmography td_cosmo = TDCosmography(z_lens, z_source, kwargs_model, cosmo_fiducial=cosmo) # time delays, the unit [days] is matched when the lensing angles are in arcsec t_days = td_cosmo.time_delays(kwargs_lens, kwargs_ps, kappa_ext=0) dt_days = t_days[1:] - t_days[0] dt_sigma = [3, 5, 10] # Gaussian errors dt_measured = np.random.normal(dt_days, dt_sigma) print("the measured relative delays are: ", dt_measured) return f, source_model_list, kwargs_data, kwargs_psf, kwargs_numerics, dt_measured, dt_sigma, kwargs_ps, lens_model_list, lens_light_model_list, source_model_list, point_source_list, lens_model_class2
def setup(self): # data specifics sigma_bkg = .05 # background noise per pixel (Gaussian) exp_time = 100. # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) numPix = 100 # cutout pixel size deltaPix = 0.05 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.1 # full width half max of PSF (only valid when psf_type='gaussian') psf_type = 'GAUSSIAN' # 'GAUSSIAN', 'PIXEL', 'NONE' # generate the coordinate grid and image properties kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg) kwargs_data['exposure_time'] = exp_time * np.ones_like(kwargs_data['image_data']) data_class = ImageData(**kwargs_data) # generate the psf variables kwargs_psf = {'psf_type': psf_type, 'pixel_size': deltaPix, 'fwhm': fwhm} # kwargs_psf = sim_util.psf_configure_simple(psf_type=psf_type, fwhm=fwhm, kernelsize=kernel_size, deltaPix=deltaPix, kernel=kernel) psf_class = PSF(**kwargs_psf) # lensing quantities kwargs_shear = {'gamma1': 0.02, 'gamma2': -0.04} # shear values to the source plane kwargs_spemd = {'theta_E': 1.26, 'gamma': 2., 'center_x': 0.0, 'center_y': 0.0, 'e1': -0.1, 'e2': 0.05} # parameters of the deflector lens model # the lens model is a supperposition of an elliptical lens model with external shear lens_model_list = ['EPL', 'SHEAR'] kwargs_lens_true = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) # choice of source type source_type = 'SERSIC' # 'SERSIC' or 'SHAPELETS' source_x = 0. source_y = 0.05 # Sersic parameters in the initial simulation phi_G, q = 0.5, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_sersic_source = {'amp': 1000, 'R_sersic': 0.05, 'n_sersic': 1, 'e1': e1, 'e2': e2, 'center_x': source_x, 'center_y': source_y} # kwargs_else = {'sourcePos_x': source_x, 'sourcePos_y': source_y, 'quasar_amp': 400., 'gamma1_foreground': 0.0, 'gamma2_foreground':-0.0} source_model_list = ['SERSIC_ELLIPSE'] kwargs_source_true = [kwargs_sersic_source] source_model_class = LightModel(light_model_list=source_model_list) lensEquationSolver = LensEquationSolver(lens_model_class) x_image, y_image = lensEquationSolver.findBrightImage(source_x, source_y, kwargs_lens_true, numImages=4, min_distance=deltaPix, search_window=numPix * deltaPix) mag = lens_model_class.magnification(x_image, y_image, kwargs=kwargs_lens_true) kwargs_numerics = {'supersampling_factor': 1} imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, kwargs_numerics=kwargs_numerics) # generate image model = imageModel.image(kwargs_lens_true, kwargs_source_true) poisson = image_util.add_poisson(model, exp_time=exp_time) bkg = image_util.add_background(model, sigma_bkd=sigma_bkg) image_sim = model + bkg + poisson data_class.update_data(image_sim) kwargs_data['image_data'] = image_sim kwargs_model = {'lens_model_list': lens_model_list, 'source_light_model_list': source_model_list, } # make cutous and data instances of them x_pos, y_pos = data_class.map_coord2pix(x_image, y_image) ra_grid, dec_grid = data_class.pixel_coordinates multi_band_list = [] for i in range(len(x_pos)): n_cut = 12 x_c = int(x_pos[i]) y_c = int(y_pos[i]) image_cut = image_sim[int(y_c - n_cut):int(y_c + n_cut), int(x_c - n_cut):int(x_c + n_cut)] exposure_map_cut = data_class.exposure_map[int(y_c - n_cut):int(y_c + n_cut), int(x_c - n_cut):int(x_c + n_cut)] kwargs_data_i = { 'background_rms': data_class.background_rms, 'exposure_time': exposure_map_cut, 'ra_at_xy_0': ra_grid[y_c - n_cut, x_c - n_cut], 'dec_at_xy_0': dec_grid[y_c - n_cut, x_c - n_cut], 'transform_pix2angle': data_class.transform_pix2angle , 'image_data': image_cut } multi_band_list.append([kwargs_data_i, kwargs_psf, kwargs_numerics]) kwargs_params = {'kwargs_lens': kwargs_lens_true, 'kwargs_source': kwargs_source_true} self.multiPatch = MultiPatchPlot(multi_band_list, kwargs_model, kwargs_params, multi_band_type='joint-linear', kwargs_likelihood=None, verbose=True, cmap_string="gist_heat") self.data_class = data_class self.model = model self.lens_model_class = lens_model_class self.kwargs_lens = kwargs_lens_true
# here we super-sample the resolution of some of the pixels where the surface brightness profile has a high gradient supersampled_indexes = np.zeros((numPix, numPix), dtype=bool) supersampled_indexes[23:27, 23:27] = True kwargs_numerics = { 'supersampling_factor': 4, 'compute_mode': 'adaptive', 'supersampled_indexes': supersampled_indexes } from lenstronomy.ImSim.image_model import ImageModel imageModel = ImageModel(data_class, psf_class, lens_light_model_class=lightModel, kwargs_numerics=kwargs_numerics) image_sim = imageModel.image(kwargs_lens_light=kwargs_light) poisson = image_util.add_poisson(image_sim, exp_time=exp_time) bkg = image_util.add_background(image_sim, sigma_bkd=background_rms) image_noisy = image_sim + bkg + poisson plt.imshow(image_noisy, origin='lower') plt.show() #%% from galight.data_process import DataProcess from galight.fitting_specify import FittingSpecify from galight.fitting_process import FittingProcess data_process = DataProcess(fov_image=image_noisy, target_pos=[25, 25], pos_type='pixel', exptime=100, rm_bkglight=False, if_plot=False, zp=25)
source_lensed = shapeletSet.function(beta_x_high_res, beta_y_high_res, coeff_ngc, n_max, beta=.05, center_x=0.1, center_y=0) # and turn the 1d vector back into a 2d array source_lensed_HR = util.array2image(source_lensed) # map 1d data vector in 2d image source_lensed = image_util.re_size(source_lensed_HR, high_res_factor) bkg_cut=cat[i][0:20,0:20] bkgmed=np.median(bkg_cut) sigma=0.985 source_lensed_conv = scipy.ndimage.filters.gaussian_filter(source_lensed, sigma, mode='nearest', truncate=6) exp_time = 90 # exposure time to quantify the Poisson noise level background_rms = 0.9# np.sqrt(bkgmed) # background rms value poisson = image_util.add_poisson(source_lensed_conv, exp_time=exp_time) bkg = image_util.add_background(source_lensed_conv, sigma_bkd=background_rms) noisy_source_lensed = source_lensed_conv + bkg + poisson f, axes = plt.subplots(1, 4, figsize=(24, 6), sharex=False, sharey=False) axes[0].imshow(np.log10(source_ALL), cmap='gist_heat', origin="lower") axes[0].set_title('Original source') axes[1].imshow(np.log10(noisy_source_lensed), cmap='gist_heat', origin="lower") axes[1].set_title('Lensed source') axes[2].imshow(np.log10(cat[i]), cmap='gist_heat', origin="lower") axes[2].set_title('DES Lens galaxy') axes[3].imshow(np.log10(noisy_source_lensed+cat[i]),cmap='gist_heat',origin='lower') axes[3].set_title('DES Lens galaxy + DES lensed source') f.tight_layout() plt.show()
from lenstronomy.LightModel.light_model import LightModel lightModel = LightModel(source_model_list) imageModel = ImageModel(data_class=data_real, psf_class=psf_deconv, kwargs_numerics=kwargs_numerics, lens_model_class=None, lens_light_model_class=lightModel) image_no_noise = imageModel.image(kwargs_lens=None, kwargs_source=None, kwargs_lens_light=kwargs_light, kwargs_ps=None) poisson = image_util.add_poisson(image_no_noise, exp_time=exp_time) bkg = image_util.add_background(image_no_noise, sigma_bkd=background_rms) image_real = image_no_noise + poisson + bkg imageModel_high_res = ImageModel(data_class=data_high_res, psf_class=psf_deconv_high_res, kwargs_numerics={}, lens_model_class=None, lens_light_model_class=lightModel) image_high_res_conv = imageModel_high_res.image(kwargs_lens=None, kwargs_source=None, kwargs_lens_light=kwargs_light, kwargs_ps=None) imageModel_high_res = ImageModel(data_class=data_high_res, psf_class=psf_no, kwargs_numerics={},
def test_add_background(): image = np.ones((10, 10)) sigma_bkgd = 1. image_noisy = image_util.add_background(image, sigma_bkgd) assert abs(np.sum(image_noisy)) < np.sqrt(np.sum(image) * sigma_bkgd) * 3
def sim_lens(data, numPix=101, sigma_bkg=8.0, exp_time=100.0, deltaPix=0.263, psf_type='GAUSSIAN', kernel_size=91): flux_g = mag_to_flux(data['mag_g'], 27.5) flux_r = mag_to_flux(data['mag_r'], 27.5) flux_i = mag_to_flux(data['mag_i'], 27.5) flux_z = mag_to_flux(data['mag_z'], 27.5) flux_source = mag_to_flux(data['source_mag'], 27.5) flux_lens = mag_to_flux(data['lens_mag'], 27.5) color_idx = {'g': 0, 'r': 1, 'i': 2, 'z': 3} cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Ob0=0.) full_band_images = np.zeros((numPix, numPix, 4)) ### Set kwargs based on input data file #shear kwargs_shear = { 'gamma_ext': data['lens_shear_gamma_ext'], 'psi_ext': data['lens_shear_psi_ext'] } #lens potential kwargs_spemd = { 'theta_E': data['lens_theta_E'], 'gamma': data['lens_gamma'], 'center_x': data['lens_center_x'], 'center_y': data['lens_center_y'], 'e1': data['lens_e1'], 'e2': data['lens_e2'] } #lens light kwargs_sersic_lens = { 'amp': flux_lens, 'R_sersic': data['lens_R_sersic'], 'n_sersic': data['lens_n_sersic'], 'e1': data['lens_e1'], 'e2': data['lens_e2'], 'center_x': data['lens_center_x'], 'center_y': data['lens_center_y'] } #source kwargs_sersic_source = { 'amp': flux_source, 'R_sersic': data['source_R_sersic'], 'n_sersic': data['source_n_sersic'], 'e1': data['source_e1'], 'e2': data['source_e2'], 'center_x': data['source_center_x'], 'center_y': data['source_center_y'] } ###set model parameters based on kwargs #lens potential lens_model_list = ['SPEP', 'SHEAR_GAMMA_PSI'] kwargs_lens = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) #lens light lens_light_model_list = ['SERSIC_ELLIPSE'] kwargs_lens_light = [kwargs_sersic_lens] lens_light_model_class = LightModel(light_model_list=lens_light_model_list) #source source_model_list = ['SERSIC_ELLIPSE'] kwargs_source = [kwargs_sersic_source] source_model_class = LightModel(light_model_list=source_model_list) ###configure image based on data properties kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg) data_class = ImageData(**kwargs_data) ###solve lens equation lensEquationSolver = LensEquationSolver(lens_model_class) x_image, y_image = lensEquationSolver.findBrightImage( kwargs_sersic_source['center_x'], kwargs_sersic_source['center_y'], kwargs_lens, numImages=4, min_distance=deltaPix, search_window=numPix * deltaPix) magnification = lens_model_class.magnification(x_image, y_image, kwargs=kwargs_lens) ###iterate through bands to simulate images for band in ['g', 'r', 'i', 'z']: #psf info kwargs_psf = { 'psf_type': psf_type, 'fwhm': data['psf_%s' % band], 'pixel_size': deltaPix, 'truncation': 3 } psf_class = PSF(**kwargs_psf) #quasar info kwargs_ps = [{ 'ra_image': x_image, 'dec_image': y_image, 'point_amp': np.abs(magnification) * eval('flux_%s' % band) }] point_source_list = ['LENSED_POSITION'] point_source_class = PointSource( point_source_type_list=point_source_list, fixed_magnification_list=[False]) #build image model kwargs_numerics = {'supersampling_factor': 1} imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) #generate image image_sim = imageModel.image(kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps) poisson = image_util.add_poisson(image_sim, exp_time=exp_time) bkg = image_util.add_background(image_sim, sigma_bkd=sigma_bkg) image_sim = image_sim + bkg + poisson data_class.update_data(image_sim) kwargs_data['image_data'] = image_sim kwargs_model = { 'lens_model_list': lens_model_list, 'lens_light_model_list': lens_light_model_list, 'source_light_model_list': source_model_list, 'point_source_model_list': point_source_list } #build up an array with one slice for each band full_band_images[:, :, color_idx[band]] += image_sim return full_band_images
def sim_non_lens_gal(data, numPix=101, sigma_bkg=8.0, exp_time=100.0, deltaPix=0.263, psf_type='GAUSSIAN', kernel_size=91): full_band_images = np.zeros((numPix, numPix, 4)) center_x_1 = np.random.uniform(-0.03, 0.03) center_y_1 = np.random.uniform(-0.03, 0.03) flux_1 = mag_to_flux(data['source_mag'], 27.5) flux_2 = mag_to_flux(data['lens_mag'], 27.5) light_model_list = ['SERSIC_ELLIPSE', 'SERSIC'] lightModel = LightModel(light_model_list=light_model_list) kwargs_disk = { 'amp': flux_1, 'R_sersic': data['source_R_sersic'], 'n_sersic': data['source_n_sersic'], 'e1': data['source_e1'], 'e2': data['source_e2'], 'center_x': center_x_1, 'center_y': center_y_1 } kwargs_bulge = { 'amp': flux_2, 'R_sersic': data['lens_R_sersic'], 'n_sersic': data['lens_n_sersic'], 'center_x': center_x_1, 'center_y': center_y_1 } kwargs_host = [kwargs_disk, kwargs_bulge] color_idx = {'g': 0, 'r': 1, 'i': 2, 'z': 3} for band in ['g', 'r', 'i', 'z']: kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg) data_class = ImageData(**kwargs_data) kwargs_psf = { 'psf_type': psf_type, 'fwhm': data['psf_%s' % band], 'pixel_size': deltaPix, 'truncation': 3 } psf_class = PSF(**kwargs_psf) kwargs_numerics = { 'supersampling_factor': 1, 'supersampling_convolution': False } imageModel = ImageModel(data_class, psf_class, lens_light_model_class=lightModel, kwargs_numerics=kwargs_numerics) image_sim = imageModel.image(kwargs_lens_light=kwargs_host) poisson = image_util.add_poisson(image_sim, exp_time=exp_time) bkg = image_util.add_background(image_sim, sigma_bkd=sigma_bkg) image_sim = image_sim + bkg + poisson full_band_images[:, :, color_idx[band]] += image_sim return full_band_images
def sim_non_lens_agn(data, center_x_1, center_y_1, center_x_2, center_y_2, numPix=101, sigma_bkg=8.0, exp_time=100.0, deltaPix=0.263, psf_type='GAUSSIAN', kernel_size=91): full_band_images = np.zeros((numPix, numPix, 4)) flux_1 = mag_to_flux(data['source_mag'], 27.5) flux_g_1 = mag_to_flux(data['mag_g'], 27.5) flux_r_1 = mag_to_flux(data['mag_r'], 27.5) flux_i_1 = mag_to_flux(data['mag_i'], 27.5) flux_z_1 = mag_to_flux(data['mag_z'], 27.5) flux_2 = mag_to_flux(data['lens_mag'], 27.5) flux_g_2 = flux_g_1 * flux_2 / flux_1 flux_r_2 = flux_r_1 * flux_2 / flux_1 flux_i_2 = flux_i_1 * flux_2 / flux_1 flux_z_2 = flux_z_1 * flux_2 / flux_1 center_x_list = [center_x_1, center_x_2] center_y_list = [center_y_1, center_y_2] kwargs_gal1 = { 'amp': flux_1, 'R_sersic': data['source_R_sersic'], 'n_sersic': data['source_n_sersic'], 'e1': data['source_e1'], 'e2': data['source_e2'], 'center_x': center_x_1, 'center_y': center_y_1 } kwargs_gal2 = { 'amp': flux_2, 'R_sersic': data['lens_R_sersic'], 'n_sersic': data['lens_n_sersic'], 'center_x': center_x_2, 'center_y': center_y_2 } kwargs_gals = [kwargs_gal1, kwargs_gal2] color_idx = {'g': 0, 'r': 1, 'i': 2, 'z': 3} cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Ob0=0.) light_model_list = ['SERSIC_ELLIPSE', 'SERSIC'] lightModel = LightModel(light_model_list=light_model_list) ###iterate through bands to simulate images for band in ['g', 'r', 'i', 'z']: #psf info kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg) data_class = ImageData(**kwargs_data) kwargs_psf = { 'psf_type': psf_type, 'fwhm': data['psf_%s' % band], 'pixel_size': deltaPix, 'truncation': 3 } psf_class = PSF(**kwargs_psf) point_source_list = ['UNLENSED'] pointSource = PointSource(point_source_type_list=point_source_list) point_amp_list = [eval('flux_%s_1' % band), eval('flux_%s_2' % band)] kwargs_ps = [{ 'ra_image': center_x_list, 'dec_image': center_y_list, 'point_amp': point_amp_list }] kwargs_numerics = { 'supersampling_factor': 1, 'supersampling_convolution': False } imageModel = ImageModel(data_class, psf_class, lens_light_model_class=lightModel, point_source_class=pointSource, kwargs_numerics=kwargs_numerics) # generate image image_sim = imageModel.image(kwargs_lens_light=kwargs_gals, kwargs_ps=kwargs_ps) poisson = image_util.add_poisson(image_sim, exp_time=exp_time) bkg = image_util.add_background(image_sim, sigma_bkd=sigma_bkg) image_sim = image_sim + bkg + poisson full_band_images[:, :, color_idx[band]] += image_sim return full_band_images
def sim_image(self, info_dict): """ Simulate an image based on specifications in sim_dict Args: info_dict (dict): A single element from the list produced interanlly by input_reader.Organizer.breakup(). Contains all the properties of a single image to generate. """ output_image = [] if self.return_planes: output_source, output_lens, output_point_source, output_noise = [], [], [], [] output_metadata = [] #set the cosmology cosmology_info = ['H0', 'Om0', 'Tcmb0', 'Neff', 'm_nu', 'Ob0'] cosmo = FlatLambdaCDM( **dict_select_choose(list(info_dict.values())[0], cosmology_info)) for band, sim_dict in info_dict.items(): # Parse the info dict params = self.parse_single_band_info_dict(sim_dict, cosmo, band=band) kwargs_single_band = params[0] kwargs_model = params[1] kwargs_numerics = params[2] kwargs_lens_light_list = params[3] kwargs_source_list = params[4] kwargs_point_source_list = params[5] kwargs_lens_model_list = params[6] output_metadata += params[7] # Make image # data properties kwargs_data = sim_util.data_configure_simple( sim_dict['numPix'], kwargs_single_band['pixel_scale'], kwargs_single_band['exposure_time']) data_class = ImageData(**kwargs_data) # psf properties kwargs_psf = { 'psf_type': kwargs_single_band['psf_type'], 'pixel_size': kwargs_single_band['pixel_scale'], 'fwhm': kwargs_single_band['seeing'] } psf_class = PSF(**kwargs_psf) # SimAPI instance for conversion to observed quantities sim = SimAPI(numpix=sim_dict['numPix'], kwargs_single_band=kwargs_single_band, kwargs_model=kwargs_model) kwargs_lens_model_list = sim.physical2lensing_conversion( kwargs_mass=kwargs_lens_model_list) kwargs_lens_light_list, kwargs_source_list, _ = sim.magnitude2amplitude( kwargs_lens_light_mag=kwargs_lens_light_list, kwargs_source_mag=kwargs_source_list) # lens model properties lens_model_class = LensModel( lens_model_list=kwargs_model['lens_model_list'], z_lens=kwargs_model['lens_redshift_list'][0], z_source=kwargs_model['z_source'], cosmo=cosmo) # source properties source_model_class = LightModel( light_model_list=kwargs_model['source_light_model_list']) # lens light properties lens_light_model_class = LightModel( light_model_list=kwargs_model['lens_light_model_list']) # solve for PS positions to incorporate time delays lensEquationSolver = LensEquationSolver(lens_model_class) kwargs_ps = [] for ps_idx, ps_mag in enumerate(kwargs_point_source_list): # modify the SimAPI instance to do one point source at a time temp_kwargs_model = {k: v for k, v in kwargs_model.items()} temp_kwargs_model['point_source_model_list'] = [ kwargs_model['point_source_model_list'][ps_idx] ] sim = SimAPI(numpix=sim_dict['numPix'], kwargs_single_band=kwargs_single_band, kwargs_model=temp_kwargs_model) if kwargs_model['point_source_model_list'][ ps_idx] == 'SOURCE_POSITION': # convert each image to an amplitude amplitudes = [] for mag in ps_mag['magnitude']: ps_dict = {k: v for k, v in ps_mag.items()} ps_dict['magnitude'] = mag _, _2, ps = sim.magnitude2amplitude( kwargs_ps_mag=[ps_dict]) amplitudes.append(ps[0]['source_amp']) x_image, y_image = lensEquationSolver.findBrightImage( ps[0]['ra_source'], ps[0]['dec_source'], kwargs_lens_model_list, numImages=4, # max number of images min_distance=kwargs_single_band['pixel_scale'], search_window=sim_dict['numPix'] * kwargs_single_band['pixel_scale']) magnification = lens_model_class.magnification( x_image, y_image, kwargs=kwargs_lens_model_list) #amplitudes = np.array(amplitudes) * np.abs(magnification) amplitudes = np.array( [a * m for a, m in zip(amplitudes, magnification)]) kwargs_ps.append({ 'ra_image': x_image, 'dec_image': y_image, 'point_amp': amplitudes }) else: _, _2, ps = sim.magnitude2amplitude(kwargs_ps_mag=[ps_mag]) kwargs_ps.append(ps[0]) # point source properties point_source_class = PointSource(point_source_type_list=[ x if x != 'SOURCE_POSITION' else 'LENSED_POSITION' for x in kwargs_model['point_source_model_list'] ], fixed_magnification_list=[False] * len(kwargs_ps)) # create an image model image_model = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) # generate image image_sim = image_model.image(kwargs_lens_model_list, kwargs_source_list, kwargs_lens_light_list, kwargs_ps) poisson = image_util.add_poisson( image_sim, exp_time=kwargs_single_band['exposure_time']) sigma_bkg = data_util.bkg_noise( kwargs_single_band['read_noise'], kwargs_single_band['exposure_time'], kwargs_single_band['sky_brightness'], kwargs_single_band['pixel_scale'], num_exposures=kwargs_single_band['num_exposures']) bkg = image_util.add_background(image_sim, sigma_bkd=sigma_bkg) image = image_sim + bkg + poisson # Save theta_E (and sigma_v if used) for ii in range(len(output_metadata)): output_metadata.append({ 'PARAM_NAME': output_metadata[ii]['PARAM_NAME'].replace( 'sigma_v', 'theta_E'), 'PARAM_VALUE': kwargs_lens_model_list[output_metadata[ii] ['LENS_MODEL_IDX']]['theta_E'], 'LENS_MODEL_IDX': output_metadata[ii]['LENS_MODEL_IDX'] }) # Solve lens equation if desired if self.solve_lens_equation: #solver = lens_equation_solver.LensEquationSolver(imSim.LensModel) #x_mins, y_mins = solver.image_position_from_source(sourcePos_x=kwargs_source_list[0]['center_x'], # sourcePos_y=kwargs_source_list[0]['center_y'], # kwargs_lens=kwargs_lens_model_list) x_mins, y_mins = x_image, y_image num_source_images = len(x_mins) # Add noise image_noise = np.zeros(np.shape(image)) for noise_source_num in range( 1, sim_dict['NUMBER_OF_NOISE_SOURCES'] + 1): image_noise += self._generate_noise( sim_dict['NOISE_SOURCE_{0}-NAME'.format(noise_source_num)], np.shape(image), select_params( sim_dict, 'NOISE_SOURCE_{0}-'.format(noise_source_num))) image += image_noise # Combine with other bands output_image.append(image) # Store plane-separated info if requested if self.return_planes: output_lens.append( image_model.lens_surface_brightness( kwargs_lens_light_list)) output_source.append( image_model.source_surface_brightness( kwargs_source_list, kwargs_lens_model_list)) output_point_source.append( image_model.point_source(kwargs_ps, kwargs_lens_model_list)) output_noise.append(image_noise) # Return the desired information in a dictionary return_dict = { 'output_image': np.array(output_image), 'output_lens_plane': None, 'output_source_plane': None, 'output_point_source_plane': None, 'output_noise_plane': None, 'x_mins': None, 'y_mins': None, 'num_source_images': None, 'additional_metadata': output_metadata } if self.return_planes: return_dict['output_lens_plane'] = np.array(output_lens) return_dict['output_source_plane'] = np.array(output_source) return_dict['output_point_source_plane'] = np.array( output_point_source) return_dict['output_noise_plane'] = np.array(output_noise) if self.solve_lens_equation: return_dict['x_mins'] = x_mins return_dict['y_mins'] = y_mins return_dict['num_source_images'] = num_source_images return return_dict