def test_force_positive_source_surface_brightness(self): kwargs_likelihood = {'force_minimum_source_surface_brightness': True} kwargs_model = {'source_light_model_list': ['SERSIC']} kwargs_constraints = {} param_class = Param(kwargs_model, **kwargs_constraints) kwargs_data = sim_util.data_configure_simple(numPix=10, deltaPix=0.1, exposure_time=1, sigma_bkg=0.1) data_class = ImageData(**kwargs_data) kwargs_psf = {'psf_type': 'NONE'} psf_class = PSF(**kwargs_psf) kwargs_sersic = {'amp': -1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0} source_model_list = ['SERSIC'] kwargs_source = [kwargs_sersic] source_model_class = LightModel(light_model_list=source_model_list) imageModel = ImageModel(data_class, psf_class, lens_model_class=None, source_model_class=source_model_class) image_sim = sim_util.simulate_simple(imageModel, [], kwargs_source) kwargs_data['image_data'] = image_sim kwargs_data_joint = {'multi_band_list': [[kwargs_data, kwargs_psf, {}]], 'multi_band_type': 'single-band'} likelihood = LikelihoodModule(kwargs_data_joint=kwargs_data_joint, kwargs_model=kwargs_model, param_class=param_class, **kwargs_likelihood) logL, _ = likelihood.logL(args=param_class.kwargs2args(kwargs_source=kwargs_source), verbose=True) assert logL <= -10**10
def test_time_delay_likelihood(self): kwargs_likelihood = {'time_delay_likelihood': True, } likelihood = LikelihoodModule(kwargs_data_joint=self.kwargs_data, kwargs_model=self.kwargs_model, param_class=self.param_class, **kwargs_likelihood) args = self.param_class.kwargs2args(kwargs_lens=self.kwargs_lens, kwargs_source=self.kwargs_source, kwargs_lens_light=self.kwargs_lens_light, kwargs_ps=self.kwargs_ps, kwargs_special=self.kwargs_cosmo) logL = likelihood.logL(args, verbose=True) npt.assert_almost_equal(logL, -3097.189103539873, decimal=-1)
def test_pixelbased_modelling(self): ss_source = 2 numPix_source = self.numPix*ss_source n_scales = 3 kwargs_pixelbased = { 'source_interpolation': 'nearest', 'supersampling_factor_source': ss_source, # supersampling of pixelated source grid # following choices are to minimize pixel solver runtime (not to get accurate reconstruction!) 'threshold_decrease_type': 'none', 'num_iter_source': 2, 'num_iter_lens': 2, 'num_iter_global': 2, 'num_iter_weights': 2, } kwargs_likelihood = { 'image_likelihood': True, 'kwargs_pixelbased': kwargs_pixelbased, 'check_positive_flux': True, # effectively not applied, activated for code coverage purposes } kernel = PSF(**self.kwargs_psf).kernel_point_source kwargs_psf = {'psf_type': 'PIXEL', 'kernel_point_source': kernel} kwargs_numerics = {'supersampling_factor': 1} kwargs_data = {'multi_band_list': [[self.kwargs_band, kwargs_psf, kwargs_numerics]]} kwargs_model = { 'lens_model_list': ['SPEP'], 'lens_light_model_list': ['SLIT_STARLETS'], 'source_light_model_list': ['SLIT_STARLETS'], } kwargs_fixed_source = [{'n_scales': n_scales, 'n_pixels': numPix_source**2, 'scale': 1, 'center_x': 0, 'center_y': 0}] kwargs_fixed_lens_light = [{'n_scales': n_scales, 'n_pixels': self.numPix**2, 'scale': 1, 'center_x': 0, 'center_y': 0}] kwargs_constraints = {'source_grid_offset': True} param_class = Param(kwargs_model, kwargs_fixed_source=kwargs_fixed_source, kwargs_fixed_lens_light=kwargs_fixed_lens_light, **kwargs_constraints) likelihood = LikelihoodModule(kwargs_data_joint=kwargs_data, kwargs_model=kwargs_model, param_class=param_class, **kwargs_likelihood) kwargs_source = [{'amp': np.ones(n_scales*numPix_source**2)}] kwargs_lens_light = [{'amp': np.ones(n_scales*self.numPix**2)}] kwargs_special = {'delta_x_source_grid': 0, 'delta_y_source_grid': 0} args = param_class.kwargs2args(kwargs_lens=self.kwargs_lens, kwargs_source=kwargs_source, kwargs_lens_light=kwargs_lens_light, kwargs_special=kwargs_special) logL = likelihood.logL(args, verbose=True) num_data_evaluate = likelihood.num_data npt.assert_almost_equal(logL/num_data_evaluate, -1/2., decimal=1)
def test_time_delay_likelihood(self): kwargs_likelihood = { 'time_delay_likelihood': True, 'time_delays_measured': np.ones(4), 'time_delays_uncertainties': np.ones(4) } likelihood = LikelihoodModule(imSim_class=self.imageModel, param_class=self.param_class, **kwargs_likelihood) args = self.param_class.kwargs2args( kwargs_lens=self.kwargs_lens, kwargs_source=self.kwargs_source, kwargs_lens_light=self.kwargs_lens_light, kwargs_ps=self.kwargs_ps, kwargs_cosmo=self.kwargs_cosmo) logL, _ = likelihood.logL(args) npt.assert_almost_equal(logL, -3340.79, decimal=-1)
def likelihoodModule(self): """ :return: Likelihood() class instance reflecting the current state of Fittingsequence """ kwargs_model = self._updateManager.kwargs_model kwargs_likelihood = self._updateManager.kwargs_likelihood likelihoodModule = LikelihoodModule(self.kwargs_data_joint, kwargs_model, self.param_class, **kwargs_likelihood) return likelihoodModule
def test_force_positive_source_surface_brightness(self): kwargs_likelihood = { 'force_positive_source_surface_brightness': True, 'numPix_source': 10, 'deltaPix_source': 0.1 } kwargs_model = {'source_light_model_list': ['SERSIC']} kwargs_constraints = {} param_class = Param(kwargs_model, **kwargs_constraints) kwargs_data = sim_util.data_configure_simple(numPix=10, deltaPix=0.1, exposure_time=1, sigma_bkg=0.1) data_class = Data(kwargs_data) kwargs_psf = {'psf_type': 'NONE'} psf_class = PSF(kwargs_psf) kwargs_sersic = { 'amp': -1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } source_model_list = ['SERSIC'] kwargs_source = [kwargs_sersic] source_model_class = LightModel(light_model_list=source_model_list) imageModel = ImageModel(data_class, psf_class, lens_model_class=None, source_model_class=source_model_class) image_sim = sim_util.simulate_simple(imageModel, [], kwargs_source) data_class.update_data(image_sim) likelihood = LikelihoodModule(imSim_class=imageModel, param_class=param_class, **kwargs_likelihood) logL, _ = likelihood.logL(args=param_class.kwargs2args( kwargs_source=kwargs_source)) assert logL <= -10**10
def likelihoodModule(self): """ :return: Likelihood() class instance reflecting the current state of Fittingsequence """ kwargs_model = self._updateManager.kwargs_model kwargs_likelihood = self._updateManager.kwargs_likelihood param_class = self._updateManager.param_class(self._lens_temp) imSim_class = class_creator.create_multiband(self.multi_band_list, **kwargs_model) likelihoodModule = LikelihoodModule(imSim_class=imSim_class, param_class=param_class, **kwargs_likelihood) return likelihoodModule
def setup(self): # data specifics sigma_bkg = 0.05 # background noise per pixel exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) numPix = 10 # cutout pixel size deltaPix = 0.1 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.5 # full width half max of PSF # PSF specification kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg) data_class = ImageData(**kwargs_data) kwargs_psf_gaussian = { 'psf_type': 'GAUSSIAN', 'fwhm': fwhm, 'pixel_size': deltaPix } psf = PSF(**kwargs_psf_gaussian) kwargs_psf = { 'psf_type': 'PIXEL', 'kernel_point_source': psf.kernel_point_source } psf_class = PSF(**kwargs_psf) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } lens_model_list = ['SPEP'] self.kwargs_lens = [kwargs_spemd] lens_model_class = LensModel(lens_model_list=lens_model_list) kwargs_sersic = { 'amp': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } lens_light_model_list = ['SERSIC'] self.kwargs_lens_light = [kwargs_sersic] lens_light_model_class = LightModel( light_model_list=lens_light_model_list) source_model_list = ['SERSIC_ELLIPSE'] self.kwargs_source = [kwargs_sersic_ellipse] source_model_class = LightModel(light_model_list=source_model_list) kwargs_numerics = { 'supersampling_factor': 1, 'supersampling_convolution': False, 'compute_mode': 'regular' } imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, kwargs_numerics=kwargs_numerics) image_sim = sim_util.simulate_simple(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light) data_class.update_data(image_sim) kwargs_data['image_data'] = image_sim kwargs_data_joint = { 'multi_band_list': [[kwargs_data, kwargs_psf, kwargs_numerics]], 'multi_band_type': 'single-band' } self.data_class = data_class self.psf_class = psf_class kwargs_model = { 'lens_model_list': lens_model_list, 'source_light_model_list': source_model_list, 'lens_light_model_list': lens_light_model_list, 'fixed_magnification_list': [False], } self.kwargs_numerics = {'subgrid_res': 1, 'psf_subgrid': False} kwargs_constraints = { 'image_plane_source_list': [False] * len(source_model_list) } kwargs_likelihood = { 'source_marg': False, 'position_uncertainty': 0.004, 'check_solver': False, 'solver_tolerance': 0.001, } self.param_class = Param(kwargs_model, **kwargs_constraints) self.Likelihood = LikelihoodModule(kwargs_data_joint=kwargs_data_joint, kwargs_model=kwargs_model, param_class=self.param_class, **kwargs_likelihood) prior_means = self.param_class.kwargs2args( kwargs_lens=self.kwargs_lens, kwargs_source=self.kwargs_source, kwargs_lens_light=self.kwargs_lens_light) prior_sigmas = np.ones_like(prior_means) * 0.1 self.output_dir = 'test_nested_out' self.sampler = MultiNestSampler(self.Likelihood, prior_type='uniform', prior_means=prior_means, prior_sigmas=prior_sigmas, output_dir=self.output_dir, remove_output_dir=True)
def _run_pso(self, n_particles, n_iterations, kwargs_fixed_lens, kwargs_mean_lens, kwargs_sigma_lens, kwargs_fixed_source, kwargs_mean_source, kwargs_sigma_source, kwargs_fixed_lens_light, kwargs_mean_lens_light, kwargs_sigma_lens_light, kwargs_fixed_ps, kwargs_mean_ps, kwargs_sigma_ps, kwargs_fixed_cosmo, kwargs_mean_cosmo, kwargs_sigma_cosmo, threadCount=1, mpi=False, print_key='PSO', sigma_factor=1, compute_bool=None, fix_solver=False): # initialise mcmc classes param_class = Param(self.kwargs_model, self.kwargs_constraints, kwargs_fixed_lens, kwargs_fixed_source, kwargs_fixed_lens_light, kwargs_fixed_ps, kwargs_fixed_cosmo, self._lens_lower, self._source_lower, self._lens_light_lower, self._ps_lower, self._cosmo_lower, self._lens_upper, self._source_upper, self._lens_light_upper, self._ps_upper, self._cosmo_upper, kwargs_lens_init=kwargs_mean_lens, fix_lens_solver=fix_solver) init_pos = param_class.setParams(kwargs_mean_lens, kwargs_mean_source, kwargs_mean_lens_light, kwargs_mean_ps, kwargs_mean_cosmo) sigma_start = param_class.setParams(kwargs_sigma_lens, kwargs_sigma_source, kwargs_sigma_lens_light, kwargs_sigma_ps, kwargs_sigma_cosmo) lowerLimit = np.array(init_pos) - np.array(sigma_start) * sigma_factor upperLimit = np.array(init_pos) + np.array(sigma_start) * sigma_factor num_param, param_list = param_class.num_param() # initialize ImSim() class kwargs_likelihood = copy.deepcopy(self.kwargs_likelihood) if compute_bool is not None: kwargs_likelihood['bands_compute'] = compute_bool imSim_class = class_creator.create_multiband(self.multi_band_list, self.kwargs_model) likelihoodModule = LikelihoodModule( imSim_class=imSim_class, param_class=param_class, kwargs_likelihood=kwargs_likelihood) # run PSO mcmc_class = Sampler(likelihoodModule=likelihoodModule) result, chain = mcmc_class.pso(n_particles, n_iterations, lowerLimit, upperLimit, init_pos=init_pos, threadCount=threadCount, mpi=mpi, print_key=print_key) lens_result, source_result, lens_light_result, ps_result, cosmo_result = param_class.getParams( result, bijective=True) return lens_result, source_result, lens_light_result, ps_result, cosmo_result, chain, param_list
def _mcmc_run(self, n_burn, n_run, walkerRatio, kwargs_fixed_lens, kwargs_mean_lens, kwargs_sigma_lens, kwargs_fixed_source, kwargs_mean_source, kwargs_sigma_source, kwargs_fixed_lens_light, kwargs_mean_lens_light, kwargs_sigma_lens_light, kwargs_fixed_ps, kwargs_mean_ps, kwargs_sigma_ps, kwargs_fixed_cosmo, kwargs_mean_cosmo, kwargs_sigma_cosmo, threadCount=1, mpi=False, init_samples=None, sigma_factor=1, compute_bool=None, fix_solver=False): param_class = Param(self.kwargs_model, self.kwargs_constraints, kwargs_fixed_lens, kwargs_fixed_source, kwargs_fixed_lens_light, kwargs_fixed_ps, kwargs_fixed_cosmo, self._lens_lower, self._source_lower, self._lens_light_lower, self._ps_lower, self._cosmo_lower, self._lens_upper, self._source_upper, self._lens_light_upper, self._ps_upper, self._cosmo_upper, kwargs_lens_init=kwargs_mean_lens, fix_lens_solver=fix_solver) # initialize ImSim() class kwargs_likelihood = copy.deepcopy(self.kwargs_likelihood) if compute_bool is not None: kwargs_likelihood['bands_compute'] = compute_bool imSim_class = class_creator.create_multiband(self.multi_band_list, self.kwargs_model) likelihoodModule = LikelihoodModule( imSim_class=imSim_class, param_class=param_class, kwargs_likelihood=kwargs_likelihood) # run PSO mcmc_class = Sampler(likelihoodModule=likelihoodModule) mean_start = param_class.setParams(kwargs_mean_lens, kwargs_mean_source, kwargs_mean_lens_light, kwargs_mean_ps, kwargs_mean_cosmo) sigma_start = param_class.setParams(kwargs_sigma_lens, kwargs_sigma_source, kwargs_sigma_lens_light, kwargs_sigma_ps, kwargs_sigma_cosmo) num_param, param_list = param_class.num_param() # run MCMC if not init_samples is None: initpos = ReusePositionGenerator(init_samples) else: initpos = None samples, dist = mcmc_class.mcmc_CH(walkerRatio, n_run, n_burn, mean_start, np.array(sigma_start) * sigma_factor, threadCount=threadCount, mpi=mpi, init_pos=initpos) return samples, param_list, dist
def setup(self): self.SimAPI = Simulation() # data specifics sigma_bkg = 0.05 # background noise per pixel exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) numPix = 10 # cutout pixel size deltaPix = 0.1 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.5 # full width half max of PSF # PSF specification kwargs_data = self.SimAPI.data_configure(numPix, deltaPix, exp_time, sigma_bkg) data_class = Data(kwargs_data) kwargs_psf = self.SimAPI.psf_configure(psf_type='GAUSSIAN', fwhm=fwhm, kernelsize=11, deltaPix=deltaPix, truncate=3, kernel=None) kwargs_psf = self.SimAPI.psf_configure( psf_type='PIXEL', fwhm=fwhm, kernelsize=11, deltaPix=deltaPix, truncate=6, kernel=kwargs_psf['kernel_point_source']) psf_class = PSF(kwargs_psf) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } lens_model_list = ['SPEP'] self.kwargs_lens = [kwargs_spemd] lens_model_class = LensModel(lens_model_list=lens_model_list) kwargs_sersic = { 'amp': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } lens_light_model_list = ['SERSIC'] self.kwargs_lens_light = [kwargs_sersic] lens_light_model_class = LightModel( light_model_list=lens_light_model_list) source_model_list = ['SERSIC_ELLIPSE'] self.kwargs_source = [kwargs_sersic_ellipse] source_model_class = LightModel(light_model_list=source_model_list) kwargs_numerics = {'subgrid_res': 1, 'psf_subgrid': False} imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, kwargs_numerics=kwargs_numerics) image_sim = self.SimAPI.simulate(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light) data_class.update_data(image_sim) self.data_class = data_class self.psf_class = psf_class kwargs_model = { 'lens_model_list': lens_model_list, 'source_light_model_list': source_model_list, 'lens_light_model_list': lens_light_model_list, 'fixed_magnification_list': [False], } self.kwargs_numerics = {'subgrid_res': 1, 'psf_subgrid': False} num_source_model = len(source_model_list) kwargs_constraints = { 'joint_center_lens_light': False, 'joint_center_source_light': False, 'additional_images_list': [False], 'fix_to_point_source_list': [False] * num_source_model, 'image_plane_source_list': [False] * num_source_model, 'solver': False, } kwargs_likelihood = { 'source_marg': True, 'point_source_likelihood': False, 'position_uncertainty': 0.004, 'check_solver': False, 'solver_tolerance': 0.001, } self.param_class = Param(kwargs_model, kwargs_constraints) self.Likelihood = LikelihoodModule(imSim_class=imageModel, param_class=self.param_class, kwargs_likelihood=kwargs_likelihood) self.sampler = Sampler(likelihoodModule=self.Likelihood)
def setup(self): np.random.seed(42) # data specifics sigma_bkg = 0.05 # background noise per pixel exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) numPix = 50 # cutout pixel size deltaPix = 0.1 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.5 # full width half max of PSF kwargs_model = {'lens_model_list': ['SPEP'], 'lens_light_model_list': ['SERSIC'], 'source_light_model_list': ['SERSIC'], 'point_source_model_list': ['SOURCE_POSITION'], 'fixed_magnification_list': [True]} # PSF specification kwargs_band = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg) data_class = ImageData(**kwargs_band) kwargs_psf = {'psf_type': 'GAUSSIAN', 'fwhm': fwhm, 'pixel_size': deltaPix} psf_class = PSF(**kwargs_psf) print(np.shape(psf_class.kernel_point_source), 'test kernel shape -') kwargs_spep = {'theta_E': 1., 'gamma': 1.95, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1} self.kwargs_lens = [kwargs_spep] kwargs_sersic = {'amp': 1/0.05**2., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0} # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = {'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0} self.kwargs_lens_light = [kwargs_sersic] self.kwargs_source = [kwargs_sersic_ellipse] self.kwargs_ps = [{'ra_source': 0.55, 'dec_source': 0.02, 'source_amp': 1.}] # quasar point source position in the source plane and intrinsic brightness self.kwargs_cosmo = {'D_dt': 1000} kwargs_numerics = {'supersampling_factor': 1, 'supersampling_convolution': False} lens_model_class, source_model_class, lens_light_model_class, point_source_class, extinction_class = class_creator.create_class_instances(**kwargs_model) imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, extinction_class, kwargs_numerics=kwargs_numerics) image_sim = sim_util.simulate_simple(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps) ra_pos, dec_pos = imageModel.PointSource.image_position(kwargs_ps=self.kwargs_ps, kwargs_lens=self.kwargs_lens) data_class.update_data(image_sim) kwargs_band['image_data'] = image_sim self.data_class = data_class self.psf_class = psf_class self.kwargs_model = kwargs_model self.kwargs_numerics = { 'supersampling_factor': 1, 'supersampling_convolution': False} kwargs_constraints = { 'num_point_source_list': [4], 'solver_type': 'NONE', # 'PROFILE', 'PROFILE_SHEAR', 'ELLIPSE', 'CENTER' 'Ddt_sampling': True } def condition_definition(kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps=None, kwargs_special=None, kwargs_extinction=None): logL = 0 if kwargs_lens_light[0]['R_sersic'] > kwargs_source[0]['R_sersic']: logL -= 10**15 return logL kwargs_likelihood = {'force_no_add_image': True, 'source_marg': True, 'astrometric_likelihood': True, 'image_position_uncertainty': 0.004, 'check_matched_source_position': False, 'source_position_tolerance': 0.001, 'source_position_sigma': 0.001, 'check_positive_flux': True, 'flux_ratio_likelihood': True, 'prior_lens': [[0, 'theta_E', 1, 0.1]], 'custom_logL_addition': condition_definition, 'image_position_likelihood': True } self.kwargs_data = {'multi_band_list': [[kwargs_band, kwargs_psf, kwargs_numerics]], 'multi_band_type': 'single-band', 'time_delays_measured': np.ones(4), 'time_delays_uncertainties': np.ones(4), 'flux_ratios': np.ones(4), 'flux_ratio_errors': np.ones(4), 'ra_image_list': ra_pos, 'dec_image_list': dec_pos } self.param_class = Param(self.kwargs_model, **kwargs_constraints) self.imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) self.Likelihood = LikelihoodModule(kwargs_data_joint=self.kwargs_data, kwargs_model=kwargs_model, param_class=self.param_class, **kwargs_likelihood) self.kwargs_band = kwargs_band self.kwargs_psf = kwargs_psf self.numPix = numPix
class TestLikelihoodModule(object): """ test the fitting sequences """ def setup(self): np.random.seed(42) # data specifics sigma_bkg = 0.05 # background noise per pixel exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) numPix = 50 # cutout pixel size deltaPix = 0.1 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.5 # full width half max of PSF kwargs_model = {'lens_model_list': ['SPEP'], 'lens_light_model_list': ['SERSIC'], 'source_light_model_list': ['SERSIC'], 'point_source_model_list': ['SOURCE_POSITION'], 'fixed_magnification_list': [True]} # PSF specification kwargs_band = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg) data_class = ImageData(**kwargs_band) kwargs_psf = {'psf_type': 'GAUSSIAN', 'fwhm': fwhm, 'pixel_size': deltaPix} psf_class = PSF(**kwargs_psf) print(np.shape(psf_class.kernel_point_source), 'test kernel shape -') kwargs_spep = {'theta_E': 1., 'gamma': 1.95, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1} self.kwargs_lens = [kwargs_spep] kwargs_sersic = {'amp': 1/0.05**2., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0} # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = {'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0} self.kwargs_lens_light = [kwargs_sersic] self.kwargs_source = [kwargs_sersic_ellipse] self.kwargs_ps = [{'ra_source': 0.55, 'dec_source': 0.02, 'source_amp': 1.}] # quasar point source position in the source plane and intrinsic brightness self.kwargs_cosmo = {'D_dt': 1000} kwargs_numerics = {'supersampling_factor': 1, 'supersampling_convolution': False} lens_model_class, source_model_class, lens_light_model_class, point_source_class, extinction_class = class_creator.create_class_instances(**kwargs_model) imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, extinction_class, kwargs_numerics=kwargs_numerics) image_sim = sim_util.simulate_simple(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps) ra_pos, dec_pos = imageModel.PointSource.image_position(kwargs_ps=self.kwargs_ps, kwargs_lens=self.kwargs_lens) data_class.update_data(image_sim) kwargs_band['image_data'] = image_sim self.data_class = data_class self.psf_class = psf_class self.kwargs_model = kwargs_model self.kwargs_numerics = { 'supersampling_factor': 1, 'supersampling_convolution': False} kwargs_constraints = { 'num_point_source_list': [4], 'solver_type': 'NONE', # 'PROFILE', 'PROFILE_SHEAR', 'ELLIPSE', 'CENTER' 'Ddt_sampling': True } def condition_definition(kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps=None, kwargs_special=None, kwargs_extinction=None): logL = 0 if kwargs_lens_light[0]['R_sersic'] > kwargs_source[0]['R_sersic']: logL -= 10**15 return logL kwargs_likelihood = {'force_no_add_image': True, 'source_marg': True, 'astrometric_likelihood': True, 'image_position_uncertainty': 0.004, 'check_matched_source_position': False, 'source_position_tolerance': 0.001, 'source_position_sigma': 0.001, 'check_positive_flux': True, 'flux_ratio_likelihood': True, 'prior_lens': [[0, 'theta_E', 1, 0.1]], 'custom_logL_addition': condition_definition, 'image_position_likelihood': True } self.kwargs_data = {'multi_band_list': [[kwargs_band, kwargs_psf, kwargs_numerics]], 'multi_band_type': 'single-band', 'time_delays_measured': np.ones(4), 'time_delays_uncertainties': np.ones(4), 'flux_ratios': np.ones(4), 'flux_ratio_errors': np.ones(4), 'ra_image_list': ra_pos, 'dec_image_list': dec_pos } self.param_class = Param(self.kwargs_model, **kwargs_constraints) self.imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) self.Likelihood = LikelihoodModule(kwargs_data_joint=self.kwargs_data, kwargs_model=kwargs_model, param_class=self.param_class, **kwargs_likelihood) self.kwargs_band = kwargs_band self.kwargs_psf = kwargs_psf self.numPix = numPix def test_logL(self): args = self.param_class.kwargs2args(kwargs_lens=self.kwargs_lens, kwargs_source=self.kwargs_source, kwargs_lens_light=self.kwargs_lens_light, kwargs_ps=self.kwargs_ps, kwargs_special=self.kwargs_cosmo) logL = self.Likelihood.logL(args, verbose=True) num_data_evaluate = self.Likelihood.num_data npt.assert_almost_equal(logL/num_data_evaluate, -1/2., decimal=1) def test_time_delay_likelihood(self): kwargs_likelihood = {'time_delay_likelihood': True, } likelihood = LikelihoodModule(kwargs_data_joint=self.kwargs_data, kwargs_model=self.kwargs_model, param_class=self.param_class, **kwargs_likelihood) args = self.param_class.kwargs2args(kwargs_lens=self.kwargs_lens, kwargs_source=self.kwargs_source, kwargs_lens_light=self.kwargs_lens_light, kwargs_ps=self.kwargs_ps, kwargs_special=self.kwargs_cosmo) logL = likelihood.logL(args, verbose=True) npt.assert_almost_equal(logL, -3097.189103539873, decimal=-1) def test_check_bounds(self): penalty, bound_hit = self.Likelihood.check_bounds(args=[0, 1], lowerLimit=[1, 0], upperLimit=[2, 2], verbose=True) assert bound_hit def test_pixelbased_modelling(self): ss_source = 2 numPix_source = self.numPix*ss_source n_scales = 3 kwargs_pixelbased = { 'source_interpolation': 'nearest', 'supersampling_factor_source': ss_source, # supersampling of pixelated source grid # following choices are to minimize pixel solver runtime (not to get accurate reconstruction!) 'threshold_decrease_type': 'none', 'num_iter_source': 2, 'num_iter_lens': 2, 'num_iter_global': 2, 'num_iter_weights': 2, } kwargs_likelihood = { 'image_likelihood': True, 'kwargs_pixelbased': kwargs_pixelbased, 'check_positive_flux': True, # effectively not applied, activated for code coverage purposes } kernel = PSF(**self.kwargs_psf).kernel_point_source kwargs_psf = {'psf_type': 'PIXEL', 'kernel_point_source': kernel} kwargs_numerics = {'supersampling_factor': 1} kwargs_data = {'multi_band_list': [[self.kwargs_band, kwargs_psf, kwargs_numerics]]} kwargs_model = { 'lens_model_list': ['SPEP'], 'lens_light_model_list': ['SLIT_STARLETS'], 'source_light_model_list': ['SLIT_STARLETS'], } kwargs_fixed_source = [{'n_scales': n_scales, 'n_pixels': numPix_source**2, 'scale': 1, 'center_x': 0, 'center_y': 0}] kwargs_fixed_lens_light = [{'n_scales': n_scales, 'n_pixels': self.numPix**2, 'scale': 1, 'center_x': 0, 'center_y': 0}] kwargs_constraints = {'source_grid_offset': True} param_class = Param(kwargs_model, kwargs_fixed_source=kwargs_fixed_source, kwargs_fixed_lens_light=kwargs_fixed_lens_light, **kwargs_constraints) likelihood = LikelihoodModule(kwargs_data_joint=kwargs_data, kwargs_model=kwargs_model, param_class=param_class, **kwargs_likelihood) kwargs_source = [{'amp': np.ones(n_scales*numPix_source**2)}] kwargs_lens_light = [{'amp': np.ones(n_scales*self.numPix**2)}] kwargs_special = {'delta_x_source_grid': 0, 'delta_y_source_grid': 0} args = param_class.kwargs2args(kwargs_lens=self.kwargs_lens, kwargs_source=kwargs_source, kwargs_lens_light=kwargs_lens_light, kwargs_special=kwargs_special) logL = likelihood.logL(args, verbose=True) num_data_evaluate = likelihood.num_data npt.assert_almost_equal(logL/num_data_evaluate, -1/2., decimal=1)
class TestFittingSequence(object): """ test the fitting sequences """ def setup(self): np.random.seed(42) # data specifics sigma_bkg = 0.05 # background noise per pixel exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) numPix = 50 # cutout pixel size deltaPix = 0.1 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.5 # full width half max of PSF # PSF specification kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg) data_class = Data(kwargs_data) kwargs_psf = sim_util.psf_configure_simple(psf_type='GAUSSIAN', fwhm=fwhm, kernelsize=11, deltaPix=deltaPix, truncate=3, kernel=None) psf_class = PSF(kwargs_psf) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.95, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } lens_model_list = ['SPEP'] self.kwargs_lens = [kwargs_spemd] lens_model_class = LensModel(lens_model_list=lens_model_list) kwargs_sersic = { 'amp': 1 / 0.05**2., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } lens_light_model_list = ['SERSIC'] self.kwargs_lens_light = [kwargs_sersic] lens_light_model_class = LightModel( light_model_list=lens_light_model_list) source_model_list = ['SERSIC_ELLIPSE'] self.kwargs_source = [kwargs_sersic_ellipse] source_model_class = LightModel(light_model_list=source_model_list) self.kwargs_ps = [ { 'ra_source': 0.55, 'dec_source': 0.02, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness self.kwargs_cosmo = {'D_dt': 1000} point_source_list = ['SOURCE_POSITION'] point_source_class = PointSource( point_source_type_list=point_source_list, fixed_magnification_list=[True]) kwargs_numerics = {'subgrid_res': 1, 'psf_subgrid': False} imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) image_sim = sim_util.simulate_simple(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps) data_class.update_data(image_sim) self.data_class = data_class self.psf_class = psf_class self.kwargs_model = { 'lens_model_list': lens_model_list, 'source_light_model_list': source_model_list, 'lens_light_model_list': lens_light_model_list, 'point_source_model_list': point_source_list, } self.kwargs_numerics = {'subgrid_res': 1, 'psf_subgrid': False} kwargs_constraints = { 'num_point_source_list': [4], 'solver_type': 'NONE', # 'PROFILE', 'PROFILE_SHEAR', 'ELLIPSE', 'CENTER' 'cosmo_type': 'D_dt' } kwargs_likelihood = { 'force_no_add_image': True, 'source_marg': True, 'point_source_likelihood': True, 'position_uncertainty': 0.004, 'check_solver': True, 'solver_tolerance': 0.001, 'check_positive_flux': True, } self.param_class = Param(self.kwargs_model, **kwargs_constraints) self.imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) self.Likelihood = LikelihoodModule(imSim_class=self.imageModel, param_class=self.param_class, **kwargs_likelihood) def test_logL(self): args = self.param_class.kwargs2args( kwargs_lens=self.kwargs_lens, kwargs_source=self.kwargs_source, kwargs_lens_light=self.kwargs_lens_light, kwargs_ps=self.kwargs_ps, kwargs_cosmo=self.kwargs_cosmo) logL, _ = self.Likelihood.logL(args) num_data_evaluate = self.Likelihood.imSim.numData_evaluate() npt.assert_almost_equal(logL / num_data_evaluate, -1 / 2., decimal=1) def test_time_delay_likelihood(self): kwargs_likelihood = { 'time_delay_likelihood': True, 'time_delays_measured': np.ones(4), 'time_delays_uncertainties': np.ones(4) } likelihood = LikelihoodModule(imSim_class=self.imageModel, param_class=self.param_class, **kwargs_likelihood) args = self.param_class.kwargs2args( kwargs_lens=self.kwargs_lens, kwargs_source=self.kwargs_source, kwargs_lens_light=self.kwargs_lens_light, kwargs_ps=self.kwargs_ps, kwargs_cosmo=self.kwargs_cosmo) logL, _ = likelihood.logL(args) npt.assert_almost_equal(logL, -3340.79, decimal=-1) def test_solver(self): # make simulation with point source positions in image plane x_pos, y_pos = self.imageModel.PointSource.image_position( self.kwargs_ps, self.kwargs_lens) kwargs_ps = [{'ra_image': x_pos[0], 'dec_image': y_pos[0]}] kwargs_likelihood = { 'source_marg': True, 'point_source_likelihood': True, 'position_uncertainty': 0.004, 'check_solver': True, 'solver_tolerance': 0.001, 'check_positive_flux': True, 'solver': True } #imageModel = ImageModel(self.data_class, self.psf_class, self.lens_model_class, self.source_model_class, # self.lens_light_model_class, # point_source_class, kwargs_numerics=kwargs_numerics) def test_force_positive_source_surface_brightness(self): kwargs_likelihood = { 'force_positive_source_surface_brightness': True, 'numPix_source': 10, 'deltaPix_source': 0.1 } kwargs_model = {'source_light_model_list': ['SERSIC']} kwargs_constraints = {} param_class = Param(kwargs_model, **kwargs_constraints) kwargs_data = sim_util.data_configure_simple(numPix=10, deltaPix=0.1, exposure_time=1, sigma_bkg=0.1) data_class = Data(kwargs_data) kwargs_psf = {'psf_type': 'NONE'} psf_class = PSF(kwargs_psf) kwargs_sersic = { 'amp': -1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } source_model_list = ['SERSIC'] kwargs_source = [kwargs_sersic] source_model_class = LightModel(light_model_list=source_model_list) imageModel = ImageModel(data_class, psf_class, lens_model_class=None, source_model_class=source_model_class) image_sim = sim_util.simulate_simple(imageModel, [], kwargs_source) data_class.update_data(image_sim) likelihood = LikelihoodModule(imSim_class=imageModel, param_class=param_class, **kwargs_likelihood) logL, _ = likelihood.logL(args=param_class.kwargs2args( kwargs_source=kwargs_source)) assert logL <= -10**10
class TestLikelihoodModule(object): """ test the fitting sequences """ def setup(self): np.random.seed(42) # data specifics sigma_bkg = 0.05 # background noise per pixel exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) numPix = 50 # cutout pixel size deltaPix = 0.1 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.5 # full width half max of PSF kwargs_model = { 'lens_model_list': ['SPEP'], 'lens_light_model_list': ['SERSIC'], 'source_light_model_list': ['SERSIC_ELLIPSE'], 'point_source_model_list': ['SOURCE_POSITION'], 'fixed_magnification_list': [True] } # PSF specification kwargs_band = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg) data_class = ImageData(**kwargs_band) kwargs_psf = { 'psf_type': 'GAUSSIAN', 'fwhm': fwhm, 'pixel_size': deltaPix } psf_class = PSF(**kwargs_psf) print(np.shape(psf_class.kernel_point_source), 'test kernel shape -') kwargs_spemd = { 'theta_E': 1., 'gamma': 1.95, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } self.kwargs_lens = [kwargs_spemd] kwargs_sersic = { 'amp': 1 / 0.05**2., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } self.kwargs_lens_light = [kwargs_sersic] self.kwargs_source = [kwargs_sersic_ellipse] self.kwargs_ps = [ { 'ra_source': 0.55, 'dec_source': 0.02, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness self.kwargs_cosmo = {'D_dt': 1000} kwargs_numerics = { 'supersampling_factor': 1, 'supersampling_convolution': False, 'compute_mode': 'gaussian' } lens_model_class, source_model_class, lens_light_model_class, point_source_class, extinction_class = class_creator.create_class_instances( **kwargs_model) imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, extinction_class, kwargs_numerics=kwargs_numerics) image_sim = sim_util.simulate_simple(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps) ra_pos, dec_pos = imageModel.PointSource.image_position( kwargs_ps=self.kwargs_ps, kwargs_lens=self.kwargs_lens) data_class.update_data(image_sim) kwargs_band['image_data'] = image_sim self.data_class = data_class self.psf_class = psf_class self.kwargs_model = kwargs_model self.kwargs_numerics = { 'supersampling_factor': 1, 'supersampling_convolution': False } kwargs_constraints = { 'num_point_source_list': [4], 'solver_type': 'NONE', # 'PROFILE', 'PROFILE_SHEAR', 'ELLIPSE', 'CENTER' 'Ddt_sampling': True } def condition_definition(kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps=None, kwargs_special=None, kwargs_extinction=None): logL = 0 if kwargs_lens_light[0]['R_sersic'] > kwargs_source[0]['R_sersic']: logL -= 10**15 return logL kwargs_likelihood = { 'force_no_add_image': True, 'source_marg': True, 'astrometric_likelihood': True, 'image_position_uncertainty': 0.004, 'check_matched_source_position': False, 'source_position_tolerance': 0.001, 'source_position_sigma': 0.001, 'check_positive_flux': True, 'flux_ratio_likelihood': True, 'prior_lens': [[0, 'theta_E', 1, 0.1]], 'custom_logL_addition': condition_definition, 'image_position_likelihood': True } self.kwargs_data = { 'multi_band_list': [[kwargs_band, kwargs_psf, kwargs_numerics]], 'multi_band_type': 'single-band', 'time_delays_measured': np.ones(4), 'time_delays_uncertainties': np.ones(4), 'flux_ratios': np.ones(4), 'flux_ratio_errors': np.ones(4), 'ra_image_list': ra_pos, 'dec_image_list': dec_pos } self.param_class = Param(self.kwargs_model, **kwargs_constraints) self.imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) self.Likelihood = LikelihoodModule(kwargs_data_joint=self.kwargs_data, kwargs_model=kwargs_model, param_class=self.param_class, **kwargs_likelihood) def test_logL(self): args = self.param_class.kwargs2args( kwargs_lens=self.kwargs_lens, kwargs_source=self.kwargs_source, kwargs_lens_light=self.kwargs_lens_light, kwargs_ps=self.kwargs_ps, kwargs_special=self.kwargs_cosmo) logL = self.Likelihood.logL(args, verbose=True) num_data_evaluate = self.Likelihood.num_data npt.assert_almost_equal(logL / num_data_evaluate, -1 / 2., decimal=1) def test_time_delay_likelihood(self): kwargs_likelihood = { 'time_delay_likelihood': True, } likelihood = LikelihoodModule(kwargs_data_joint=self.kwargs_data, kwargs_model=self.kwargs_model, param_class=self.param_class, **kwargs_likelihood) args = self.param_class.kwargs2args( kwargs_lens=self.kwargs_lens, kwargs_source=self.kwargs_source, kwargs_lens_light=self.kwargs_lens_light, kwargs_ps=self.kwargs_ps, kwargs_special=self.kwargs_cosmo) logL = likelihood.logL(args, verbose=True) npt.assert_almost_equal(logL, -3080.29, decimal=-1) #def test_solver(self): # make simulation with point source positions in image plane # x_pos, y_pos = self.imageModel.PointSource.image_position(self.kwargs_ps, self.kwargs_lens) # kwargs_ps = [{'ra_image': x_pos[0], 'dec_image': y_pos[0]}] # kwargs_likelihood = { # 'source_marg': True, # 'astrometric_likelihood': True, # 'position_uncertainty': 0.004, # 'check_solver': True, # 'solver_tolerance': 0.001, # 'check_positive_flux': True, # 'solver': True # } #imageModel = ImageModel(self.data_class, self.psf_class, self.lens_model_class, self.source_model_class, # self.lens_light_model_class, # point_source_class, kwargs_numerics=kwargs_numerics) def test_force_positive_source_surface_brightness(self): kwargs_likelihood = {'force_minimum_source_surface_brightness': True} kwargs_model = {'source_light_model_list': ['SERSIC']} kwargs_constraints = {} param_class = Param(kwargs_model, **kwargs_constraints) kwargs_data = sim_util.data_configure_simple(numPix=10, deltaPix=0.1, exposure_time=1, background_rms=0.1) data_class = ImageData(**kwargs_data) kwargs_psf = {'psf_type': 'NONE'} psf_class = PSF(**kwargs_psf) kwargs_sersic = { 'amp': -1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } source_model_list = ['SERSIC'] kwargs_source = [kwargs_sersic] source_model_class = LightModel(light_model_list=source_model_list) imageModel = ImageModel(data_class, psf_class, lens_model_class=None, source_model_class=source_model_class) image_sim = sim_util.simulate_simple(imageModel, [], kwargs_source) kwargs_data['image_data'] = image_sim kwargs_data_joint = { 'multi_band_list': [[kwargs_data, kwargs_psf, {}]], 'multi_band_type': 'single-band' } likelihood = LikelihoodModule(kwargs_data_joint=kwargs_data_joint, kwargs_model=kwargs_model, param_class=param_class, **kwargs_likelihood) logL = likelihood.logL( args=param_class.kwargs2args(kwargs_source=kwargs_source), verbose=True) assert logL <= -10**10
def simple_einstein_ring_likelihood_2d(): # data specifics sigma_bkg = 0.05 # background noise per pixel exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) numPix = 10 # cutout pixel size deltaPix = 0.1 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.5 # full width half max of PSF # PSF specification kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg) data_class = ImageData(**kwargs_data) kwargs_psf_gaussian = { 'psf_type': 'GAUSSIAN', 'fwhm': fwhm, 'pixel_size': deltaPix } psf = PSF(**kwargs_psf_gaussian) kwargs_psf = { 'psf_type': 'PIXEL', 'kernel_point_source': psf.kernel_point_source } psf_class = PSF(**kwargs_psf) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } lens_model_list = ['SPEP'] kwargs_lens = [kwargs_spemd] lens_model_class = LensModel(lens_model_list=lens_model_list) kwargs_sersic = { 'amp': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } lens_light_model_list = ['SERSIC'] kwargs_lens_light = [kwargs_sersic] lens_light_model_class = LightModel(light_model_list=lens_light_model_list) source_model_list = ['SERSIC_ELLIPSE'] kwargs_source = [kwargs_sersic_ellipse] source_model_class = LightModel(light_model_list=source_model_list) kwargs_numerics = { 'supersampling_factor': 1, 'supersampling_convolution': False, 'compute_mode': 'regular' } imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, kwargs_numerics=kwargs_numerics) image_sim = sim_util.simulate_simple(imageModel, kwargs_lens, kwargs_source, kwargs_lens_light) data_class.update_data(image_sim) kwargs_data['image_data'] = image_sim kwargs_data_joint = { 'multi_band_list': [[kwargs_data, kwargs_psf, kwargs_numerics]], 'multi_band_type': 'single-band' } kwargs_model = { 'lens_model_list': lens_model_list, 'source_light_model_list': source_model_list, 'lens_light_model_list': lens_light_model_list, 'fixed_magnification_list': [False], } kwargs_constraints = { 'image_plane_source_list': [False] * len(source_model_list) } kwargs_likelihood = { 'source_marg': False, 'image_position_uncertainty': 0.004, 'check_matched_source_position': False, 'source_position_tolerance': 0.001, 'source_position_sigma': 0.001, } # reduce number of param to sample (for runtime) kwargs_fixed_lens = [{'gamma': 1.8, 'center_y': 0, 'e1': 0.1, 'e2': 0.1}] kwargs_lower_lens = [{'theta_E': 0.8, 'center_x': -0.1}] kwargs_upper_lens = [{'theta_E': 1.2, 'center_x': 0.1}] kwargs_fixed_source = [{ 'R_sersic': 0.6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 }] kwargs_fixed_lens_light = [{ 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 }] param_class = Param(kwargs_model, kwargs_fixed_lens=kwargs_fixed_lens, kwargs_fixed_source=kwargs_fixed_source, kwargs_fixed_lens_light=kwargs_fixed_lens_light, kwargs_lower_lens=kwargs_lower_lens, kwargs_upper_lens=kwargs_upper_lens, **kwargs_constraints) likelihood = LikelihoodModule(kwargs_data_joint=kwargs_data_joint, kwargs_model=kwargs_model, param_class=param_class, **kwargs_likelihood) kwargs_truths = { 'kwargs_lens': kwargs_lens, 'kwargs_source': kwargs_source, 'kwargs_lens_light': kwargs_lens_light } return likelihood, kwargs_truths
def setup(self): np.random.seed(42) # data specifics sigma_bkg = 0.05 # background noise per pixel exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) numPix = 50 # cutout pixel size deltaPix = 0.1 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.5 # full width half max of PSF # PSF specification kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg) data_class = Data(kwargs_data) kwargs_psf = sim_util.psf_configure_simple(psf_type='GAUSSIAN', fwhm=fwhm, kernelsize=11, deltaPix=deltaPix, truncate=3, kernel=None) psf_class = PSF(kwargs_psf) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.95, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } lens_model_list = ['SPEP'] self.kwargs_lens = [kwargs_spemd] lens_model_class = LensModel(lens_model_list=lens_model_list) kwargs_sersic = { 'amp': 1 / 0.05**2., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } lens_light_model_list = ['SERSIC'] self.kwargs_lens_light = [kwargs_sersic] lens_light_model_class = LightModel( light_model_list=lens_light_model_list) source_model_list = ['SERSIC_ELLIPSE'] self.kwargs_source = [kwargs_sersic_ellipse] source_model_class = LightModel(light_model_list=source_model_list) self.kwargs_ps = [ { 'ra_source': 0.55, 'dec_source': 0.02, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness self.kwargs_cosmo = {'D_dt': 1000} point_source_list = ['SOURCE_POSITION'] point_source_class = PointSource( point_source_type_list=point_source_list, fixed_magnification_list=[True]) kwargs_numerics = {'subgrid_res': 1, 'psf_subgrid': False} imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) image_sim = sim_util.simulate_simple(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps) data_class.update_data(image_sim) self.data_class = data_class self.psf_class = psf_class self.kwargs_model = { 'lens_model_list': lens_model_list, 'source_light_model_list': source_model_list, 'lens_light_model_list': lens_light_model_list, 'point_source_model_list': point_source_list, } self.kwargs_numerics = {'subgrid_res': 1, 'psf_subgrid': False} kwargs_constraints = { 'num_point_source_list': [4], 'solver_type': 'NONE', # 'PROFILE', 'PROFILE_SHEAR', 'ELLIPSE', 'CENTER' 'cosmo_type': 'D_dt' } kwargs_likelihood = { 'force_no_add_image': True, 'source_marg': True, 'point_source_likelihood': True, 'position_uncertainty': 0.004, 'check_solver': True, 'solver_tolerance': 0.001, 'check_positive_flux': True, } self.param_class = Param(self.kwargs_model, **kwargs_constraints) self.imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) self.Likelihood = LikelihoodModule(imSim_class=self.imageModel, param_class=self.param_class, **kwargs_likelihood)