def __init__(self, collector_area, numPix, deltaPix, readout_noise, sky_brightness, extinction, exposure_time, psf_type, fwhm, *args, **kwargs): """ :param collector_area: area of collector in m^2 :param numPix: number of pixels :param deltaPix: FoV per pixel in units of arcsec :param readout_noise: rms value of readout per pixel in units of photons :param sky_brightness: number of photons of sky per area (arcsec) per time (second) for a collector area (1 m^2) :param extinction: exctinction (galactic and atmosphere combined). Only use this if magnitude calibration is done without it. :param exposure_time: exposure time (seconds) :param psf_type: :param fwhm: :param args: :param kwargs: """ self.simulation = Simulation() sky_per_pixel = sky_brightness * collector_area * deltaPix**2 # time independent noise term per pixel per second sigma_bkg = np.sqrt( readout_noise**2 + exposure_time * sky_per_pixel**2 ) / exposure_time # total Gaussian noise term per pixel in full exposure (in units of counts per second) kwargs_data = self.simulation.data_configure(numPix, deltaPix, exposure_time, sigma_bkg) self._data = Data(kwargs_data) kwargs_psf = self.simulation.psf_configure(psf_type, fwhm) self._psf = PSF(kwargs_psf) self._flux_calibration_factor = collector_area / extinction * deltaPix**2 # transforms intrinsic surface brightness per angular area into the flux normalizations per pixel
def setup(self): self.SimAPI = Simulation() # data specifics sigma_bkg = .05 # background noise per pixel 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.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=31, deltaPix=deltaPix, truncate=3, kernel=None) psf_class = PSF(kwargs_psf) psf_class._psf_error_map = np.zeros_like(psf_class.kernel_point_source) # 'EXERNAL_SHEAR': external shear kwargs_shear = {'e1': 0.01, 'e2': 0.01} # gamma_ext: shear strength, psi_ext: shear angel (in radian) phi, q = 0.2, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi, q) kwargs_spemd = {'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': e1, 'e2': e2} lens_model_list = ['SPEP', 'SHEAR'] self.kwargs_lens = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile kwargs_sersic = {'amp': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0} # 'SERSIC_ELLIPSE': elliptical Sersic profile phi, q = 0.2, 0.9 e1, e2 = param_util.phi_q2_ellipticity(phi, q) kwargs_sersic_ellipse = {'amp': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'e1': e1, 'e2': e2} 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.01, 'dec_source': 0.0, 'source_amp': 1.}] # quasar point source position in the source plane and intrinsic brightness point_source_class = PointSource(point_source_type_list=['SOURCE_POSITION'], fixed_magnification_list=[True]) kwargs_numerics = {'subgrid_res': 2, 'psf_subgrid': True} 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 = self.SimAPI.simulate(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps) data_class.update_data(image_sim) 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.solver = LensEquationSolver(lensModel=self.imageModel.LensModel)
def lens_model_plot(ax, lensModel, kwargs_lens, numPix=500, deltaPix=0.01, sourcePos_x=0, sourcePos_y=0, point_source=False, with_caustics=False): """ plots a lens model (convergence) and the critical curves and caustics :param ax: :param kwargs_lens: :param numPix: :param deltaPix: :return: """ from lenstronomy.SimulationAPI.simulations import Simulation simAPI = Simulation() kwargs_data = simAPI.data_configure(numPix, deltaPix) data = Data(kwargs_data) _frame_size = numPix * deltaPix _coords = data._coords x_grid, y_grid = data.coordinates lensModelExt = LensModelExtensions(lensModel) #ra_crit_list, dec_crit_list, ra_caustic_list, dec_caustic_list = lensModelExt.critical_curve_caustics( # kwargs_lens, compute_window=_frame_size, grid_scale=deltaPix/2.) x_grid1d = util.image2array(x_grid) y_grid1d = util.image2array(y_grid) kappa_result = lensModel.kappa(x_grid1d, y_grid1d, kwargs_lens) kappa_result = util.array2image(kappa_result) im = ax.matshow(np.log10(kappa_result), origin='lower', extent=[0, _frame_size, 0, _frame_size], cmap='Greys', vmin=-1, vmax=1) #, cmap=self._cmap, vmin=v_min, vmax=v_max) if with_caustics is True: ra_crit_list, dec_crit_list = lensModelExt.critical_curve_tiling(kwargs_lens, compute_window=_frame_size, start_scale=deltaPix, max_order=10) ra_caustic_list, dec_caustic_list = lensModel.ray_shooting(ra_crit_list, dec_crit_list, kwargs_lens) plot_line_set(ax, _coords, ra_caustic_list, dec_caustic_list, color='g') plot_line_set(ax, _coords, ra_crit_list, dec_crit_list, color='r') if point_source: from lenstronomy.LensModel.Solver.lens_equation_solver import LensEquationSolver solver = LensEquationSolver(lensModel) theta_x, theta_y = solver.image_position_from_source(sourcePos_x, sourcePos_y, kwargs_lens) mag_images = lensModel.magnification(theta_x, theta_y, kwargs_lens) x_image, y_image = _coords.map_coord2pix(theta_x, theta_y) abc_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'] for i in range(len(x_image)): x_ = (x_image[i] + 0.5) * deltaPix y_ = (y_image[i] + 0.5) * deltaPix ax.plot(x_, y_, 'dk', markersize=4*(1 + np.log(np.abs(mag_images[i]))), alpha=0.5) ax.text(x_, y_, abc_list[i], fontsize=20, color='k') x_source, y_source = _coords.map_coord2pix(sourcePos_x, sourcePos_y) ax.plot((x_source + 0.5) * deltaPix, (y_source + 0.5) * deltaPix, '*k', markersize=10) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax.autoscale(False) #image_position_plot(ax, _coords, self._kwargs_else) #source_position_plot(ax, self._coords, self._kwargs_source) return ax
def test_point_source_rendering(self): # initialize data from lenstronomy.SimulationAPI.simulations import Simulation SimAPI = Simulation() numPix = 100 deltaPix = 0.05 kwargs_data = SimAPI.data_configure(numPix, deltaPix, exposure_time=1, sigma_bkg=1) data_class = Data(kwargs_data) kernel = np.zeros((5, 5)) kernel[2, 2] = 1 kwargs_psf = {'kernel_point_source': kernel, 'kernel_pixel': kernel, 'psf_type': 'PIXEL'} psf_class = PSF(kwargs_psf) lens_model_class = LensModel(['SPEP']) source_model_class = LightModel([]) lens_light_model_class = LightModel([]) kwargs_numerics = {'subgrid_res': 2, 'point_source_subgrid': 1} point_source_class = PointSource(point_source_type_list=['LENSED_POSITION'], fixed_magnification_list=[False]) makeImage = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) # chose point source positions x_pix = np.array([10, 5, 10, 90]) y_pix = np.array([40, 50, 60, 50]) ra_pos, dec_pos = makeImage.Data.map_pix2coord(x_pix, y_pix) e1, e2 = param_util.phi_q2_ellipticity(0, 0.8) kwargs_lens_init = [{'theta_E': 1, 'gamma': 2, 'e1': e1, 'e2': e2, 'center_x': 0, 'center_y': 0}] kwargs_else = [{'ra_image': ra_pos, 'dec_image': dec_pos, 'point_amp': np.ones_like(ra_pos)}] model = makeImage.image(kwargs_lens_init, kwargs_source={}, kwargs_lens_light={}, kwargs_ps=kwargs_else) image = makeImage.ImageNumerics.array2image(model) for i in range(len(x_pix)): npt.assert_almost_equal(image[y_pix[i], x_pix[i]], 1, decimal=2) x_pix = np.array([10.5, 5.5, 10.5, 90.5]) y_pix = np.array([40, 50, 60, 50]) ra_pos, dec_pos = makeImage.Data.map_pix2coord(x_pix, y_pix) phi, q = 0., 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi, q) kwargs_lens_init = [{'theta_E': 1, 'gamma': 2, 'e1': e1, 'e2': e2, 'center_x': 0, 'center_y': 0}] kwargs_else = [{'ra_image': ra_pos, 'dec_image': dec_pos, 'point_amp': np.ones_like(ra_pos)}] model = makeImage.image(kwargs_lens_init, kwargs_source={}, kwargs_lens_light={}, kwargs_ps=kwargs_else) image = makeImage.ImageNumerics.array2image(model) for i in range(len(x_pix)): print(int(y_pix[i]), int(x_pix[i]+0.5)) npt.assert_almost_equal(image[int(y_pix[i]), int(x_pix[i])], 0.5, decimal=1) npt.assert_almost_equal(image[int(y_pix[i]), int(x_pix[i]+0.5)], 0.5, decimal=1)
#general imports import numpy as np import matplotlib.pyplot as plt import random as rand from astropy import visualization as aviz import scipy import os #lenstronomy imports from lenstronomy.Data.imaging_data import Data from lenstronomy.Data.psf import PSF from lenstronomy.SimulationAPI.simulations import Simulation SimAPI = Simulation() from lenstronomy.LightModel.light_model import LightModel from lenstronomy.LensModel.lens_model import LensModel import lenstronomy.Plots.output_plots as lens_plot from lenstronomy.LensModel.Solver.lens_equation_solver import LensEquationSolver import lenstronomy.Util.param_util as param_util from lenstronomy.PointSource.point_source import PointSource from lenstronomy.ImSim.image_model import ImageModel import lenstronomy.Util.image_util as image_util from lenstronomy.Plots.output_plots import LensModelPlot from lenstronomy.Workflow.fitting_sequence import FittingSequence import lenstronomy.Plots.output_plots as out_plot import lenstronomy.Util.util as util from lenstronomy.LightModel.Profiles.shapelets import ShapeletSet shapeletSet = ShapeletSet() #********************************************************************************# # DES CATALOGUE # #********************************************************************************#
class SingleBand(object): """ class to operate on single exposure """ def __init__(self, collector_area, numPix, deltaPix, readout_noise, sky_brightness, extinction, exposure_time, psf_type, fwhm, *args, **kwargs): """ :param collector_area: area of collector in m^2 :param numPix: number of pixels :param deltaPix: FoV per pixel in units of arcsec :param readout_noise: rms value of readout per pixel in units of photons :param sky_brightness: number of photons of sky per area (arcsec) per time (second) for a collector area (1 m^2) :param extinction: exctinction (galactic and atmosphere combined). Only use this if magnitude calibration is done without it. :param exposure_time: exposure time (seconds) :param psf_type: :param fwhm: :param args: :param kwargs: """ self.simulation = Simulation() sky_per_pixel = sky_brightness * collector_area * deltaPix**2 # time independent noise term per pixel per second sigma_bkg = np.sqrt( readout_noise**2 + exposure_time * sky_per_pixel**2 ) / exposure_time # total Gaussian noise term per pixel in full exposure (in units of counts per second) kwargs_data = self.simulation.data_configure(numPix, deltaPix, exposure_time, sigma_bkg) self._data = Data(kwargs_data) kwargs_psf = self.simulation.psf_configure(psf_type, fwhm) self._psf = PSF(kwargs_psf) self._flux_calibration_factor = collector_area / extinction * deltaPix**2 # transforms intrinsic surface brightness per angular area into the flux normalizations per pixel def simulate(self, kwargs_options, kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_else, lens_colour, source_colour, quasar_colour, no_noise=False, source_add=True, lens_light_add=True, point_source_add=True): """ :param kwargs_options: :param kwargs_lens: :param kwargs_source: :param kwargs_lens_light: :param kwargs_else: :param no_noise: :return: """ lensLightModel = LightModel( kwargs_options.get('lens_light_model_list', [])) sourceModel = LightModel( kwargs_options.get('source_light_model_list', [])) lensModel = LensModel( lens_model_list=kwargs_options.get('lens_model_list', [])) pointSource = PointSource( point_source_type_list=kwargs_options.get('point_source_list', []), lensModel=lensModel, fixed_magnification_list=kwargs_options.get( 'fixed_magnification_list', None), additional_images_list=kwargs_options.get('additional_images', None)) norm_factor_source = self._flux_calibration_factor * source_colour norm_factor_lens_light = self._flux_calibration_factor * lens_colour norm_factor_point_source = self._flux_calibration_factor * quasar_colour kwargs_source_updated, kwargs_lens_light_updated, kwargs_else_updated = self.simulation.normalize_flux( kwargs_options, kwargs_source, kwargs_lens_light, kwargs_else, norm_factor_source, norm_factor_lens_light, norm_factor_point_source) imageModel = ImageModel(self._data, self._psf, lensModel, sourceModel, lensLightModel, pointSource, kwargs_numerics={}) image = self.simulation.simulate(imageModel, kwargs_lens, kwargs_source_updated, kwargs_lens_light_updated, kwargs_else_updated, no_noise=no_noise, source_add=source_add, lens_light_add=lens_light_add, point_source_add=point_source_add) return image def source_plane(self, kwargs_options, kwargs_source, source_colour, numPix=100, deltaPix=0.01): """ :param kwargs_options: :param kwargs_source: :param kwargs_else: :param source_colour: :param numPix: :param deltaPix: :return: """ norm_factor_source = self._flux_calibration_factor * source_colour kwargs_source_updated = self.simulation.normalize_flux_source( kwargs_options, kwargs_source, norm_factor_source) image = self.simulation.source_plane(kwargs_options, kwargs_source_updated, numPix, deltaPix) return image
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 = 100 # cutout pixel size deltaPix = 0.05 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.5 # full width half max of PSF # PSF specification data_class = self.SimAPI.data_configure(numPix, deltaPix, exp_time, sigma_bkg) psf_class = self.SimAPI.psf_configure(psf_type='GAUSSIAN', fwhm=fwhm, kernelsize=31, deltaPix=deltaPix, truncate=3, kernel=None) psf_class = self.SimAPI.psf_configure( psf_type='PIXEL', fwhm=fwhm, kernelsize=31, deltaPix=deltaPix, truncate=6, kernel=psf_class.kernel_point_source) # 'EXERNAL_SHEAR': external shear kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'q': 0.8, 'phi_G': 0.2 } lens_model_list = ['SPEP', 'SHEAR'] self.kwargs_lens = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile kwargs_sersic = { 'I0_sersic': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'I0_sersic': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'phi_G': 0.2, 'q': 0.9 } 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.0, 'dec_source': 0.0, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness 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 = self.SimAPI.simulate(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps) data_class.update_data(image_sim) self.kwargs_data = data_class.constructor_kwargs() self.kwargs_psf = psf_class.constructor_kwargs() 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, 'fixed_magnification_list': [False], } self.kwargs_numerics = {'subgrid_res': 2, 'psf_subgrid': True} num_source_model = len(source_model_list) self.kwargs_constraints = { 'joint_center_lens_light': False, 'joint_center_source_light': False, 'num_point_source_list': [4], 'additional_images_list': [False], 'fix_to_point_source_list': [False] * num_source_model, 'image_plane_source_list': [False] * num_source_model, 'solver': False, 'solver_type': 'PROFILE_SHEAR', # 'PROFILE', 'PROFILE_SHEAR', 'ELLIPSE', 'CENTER' } self.kwargs_likelihood = { 'check_bounds': True, 'force_no_add_image': True, 'source_marg': True, 'point_source_likelihood': False, 'position_uncertainty': 0.004, 'check_solver': True, 'solver_tolerance': 0.001 } kwargs_fixed = [[{}, {}], [{}], [{}], [{}]] image_band = [self.kwargs_data, self.kwargs_psf, self.kwargs_numerics] multi_band_list = [image_band] kwargs_init = [ self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps ] self.likelihoodModule = LikelihoodModule( multi_band_list, self.kwargs_model, self.kwargs_constraints, self.kwargs_likelihood, kwargs_fixed, kwargs_lower=kwargs_init, kwargs_upper=kwargs_init, kwargs_lens_init=self.kwargs_lens, compute_bool=None) kwargs_fixed_lens, kwargs_fixed_source, kwargs_fixed_lens_light, kwargs_fixed_ps = kwargs_fixed self.param = Param(self.kwargs_model, self.kwargs_constraints, kwargs_fixed_lens, kwargs_fixed_source, kwargs_fixed_lens_light, kwargs_fixed_ps, kwargs_lens_init=self.kwargs_lens)
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)
class TestFittingSequence(object): """ test the fitting sequences """ 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 test_pso(self): n_particles = 2 n_iterations = 2 result, chain = self.sampler.pso(n_particles, n_iterations, lower_start=None, upper_start=None, threadCount=1, init_pos=None, mpi=False, print_key='PSO') assert len(result) == 16 def test_mcmc_emcee(self): n_walkers = 36 n_run = 2 n_burn = 2 mean_start = self.param_class.setParams( kwargs_lens=self.kwargs_lens, kwargs_source=self.kwargs_source, kwargs_lens_light=self.kwargs_lens_light) sigma_start = np.ones_like(mean_start) * 0.1 samples = self.sampler.mcmc_emcee(n_walkers, n_run, n_burn, mean_start, sigma_start, mpi=False) assert len(samples) == n_walkers * n_run def test_mcmc_CH(self): walkerRatio = 2 n_run = 2 n_burn = 2 mean_start = self.param_class.setParams( kwargs_lens=self.kwargs_lens, kwargs_source=self.kwargs_source, kwargs_lens_light=self.kwargs_lens_light) sigma_start = np.ones_like(mean_start) * 0.1 self.sampler.mcmc_CH(walkerRatio, n_run, n_burn, mean_start, sigma_start, threadCount=1, init_pos=None, mpi=False)
def setup(self): self.SimAPI = Simulation() # data specifics sigma_bkg = 0.01 # background noise per pixel 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.5 # full width half max of PSF # PSF specification data_class = self.SimAPI.data_configure(numPix, deltaPix, exp_time, sigma_bkg) sigma = util.fwhm2sigma(fwhm) x_grid, y_grid = util.make_grid(numPix=31, deltapix=0.05) from lenstronomy.LightModel.Profiles.gaussian import Gaussian gaussian = Gaussian() kernel_point_source = gaussian.function(x_grid, y_grid, amp=1., sigma_x=sigma, sigma_y=sigma, center_x=0, center_y=0) kernel_point_source /= np.sum(kernel_point_source) kernel_point_source = util.array2image(kernel_point_source) self.kwargs_psf = { 'psf_type': 'PIXEL', 'kernel_point_source': kernel_point_source } psf_class = PSF(kwargs_psf=self.kwargs_psf) # 'EXERNAL_SHEAR': external shear kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'q': 0.8, 'phi_G': 0.2 } lens_model_list = ['SPEP', 'SHEAR'] self.kwargs_lens = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile kwargs_sersic = { 'I0_sersic': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'I0_sersic': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'phi_G': 0.2, 'q': 0.9 } 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.0, 'dec_source': 0.0, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness point_source_class = PointSource( point_source_type_list=['SOURCE_POSITION'], fixed_magnification_list=[True]) kwargs_numerics = {'subgrid_res': 3, 'psf_subgrid': True} 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 = self.SimAPI.simulate(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps) data_class.update_data(image_sim) 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.psf_fitting = PsfFitting(self.imageModel)
class TestImageModel(object): """ tests the source model routines """ def setup(self): self.SimAPI = Simulation() # data specifics sigma_bkg = 0.01 # background noise per pixel 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.5 # full width half max of PSF # PSF specification data_class = self.SimAPI.data_configure(numPix, deltaPix, exp_time, sigma_bkg) sigma = util.fwhm2sigma(fwhm) x_grid, y_grid = util.make_grid(numPix=31, deltapix=0.05) from lenstronomy.LightModel.Profiles.gaussian import Gaussian gaussian = Gaussian() kernel_point_source = gaussian.function(x_grid, y_grid, amp=1., sigma_x=sigma, sigma_y=sigma, center_x=0, center_y=0) kernel_point_source /= np.sum(kernel_point_source) kernel_point_source = util.array2image(kernel_point_source) self.kwargs_psf = { 'psf_type': 'PIXEL', 'kernel_point_source': kernel_point_source } psf_class = PSF(kwargs_psf=self.kwargs_psf) # 'EXERNAL_SHEAR': external shear kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'q': 0.8, 'phi_G': 0.2 } lens_model_list = ['SPEP', 'SHEAR'] self.kwargs_lens = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile kwargs_sersic = { 'I0_sersic': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'I0_sersic': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'phi_G': 0.2, 'q': 0.9 } 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.0, 'dec_source': 0.0, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness point_source_class = PointSource( point_source_type_list=['SOURCE_POSITION'], fixed_magnification_list=[True]) kwargs_numerics = {'subgrid_res': 3, 'psf_subgrid': True} 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 = self.SimAPI.simulate(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps) data_class.update_data(image_sim) 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.psf_fitting = PsfFitting(self.imageModel) def test_update_psf(self): fwhm = 0.3 sigma = util.fwhm2sigma(fwhm) x_grid, y_grid = util.make_grid(numPix=31, deltapix=0.05) from lenstronomy.LightModel.Profiles.gaussian import Gaussian gaussian = Gaussian() kernel_point_source = gaussian.function(x_grid, y_grid, amp=1., sigma_x=sigma, sigma_y=sigma, center_x=0, center_y=0) kernel_point_source /= np.sum(kernel_point_source) kernel_point_source = util.array2image(kernel_point_source) kwargs_psf = { 'psf_type': 'PIXEL', 'kernel_point_source': kernel_point_source } kwargs_psf_return, improved_bool = self.psf_fitting.update_psf( kwargs_psf, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, factor=0.1, symmetry=1) assert improved_bool kernel_new = kwargs_psf_return['kernel_point_source'] kernel_true = self.kwargs_psf['kernel_point_source'] kernel_old = kwargs_psf['kernel_point_source'] diff_old = np.sum((kernel_old - kernel_true)**2) diff_new = np.sum((kernel_new - kernel_true)**2) assert diff_old > diff_new def test_update_iterative(self): fwhm = 0.3 sigma = util.fwhm2sigma(fwhm) x_grid, y_grid = util.make_grid(numPix=31, deltapix=0.05) from lenstronomy.LightModel.Profiles.gaussian import Gaussian gaussian = Gaussian() kernel_point_source = gaussian.function(x_grid, y_grid, amp=1., sigma_x=sigma, sigma_y=sigma, center_x=0, center_y=0) kernel_point_source /= np.sum(kernel_point_source) kernel_point_source = util.array2image(kernel_point_source) kwargs_psf = { 'psf_type': 'PIXEL', 'kernel_point_source': kernel_point_source } kwargs_psf_new = self.psf_fitting.update_iterative( kwargs_psf, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, factor=0.2, num_iter=3, symmetry=1) kernel_new = kwargs_psf_new['kernel_point_source'] kernel_true = self.kwargs_psf['kernel_point_source'] kernel_old = kwargs_psf['kernel_point_source'] diff_old = np.sum((kernel_old - kernel_true)**2) diff_new = np.sum((kernel_new - kernel_true)**2) assert diff_old > diff_new assert diff_new < 0.01 kwargs_psf_new = self.psf_fitting.update_iterative( kwargs_psf, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, factor=0.2, num_iter=3, symmetry=1, no_break=True) kernel_new = kwargs_psf_new['kernel_point_source'] kernel_true = self.kwargs_psf['kernel_point_source'] kernel_old = kwargs_psf['kernel_point_source'] diff_old = np.sum((kernel_old - kernel_true)**2) diff_new = np.sum((kernel_new - kernel_true)**2) assert diff_old > diff_new assert diff_new < 0.01 def test_mask_point_sources(self): ra_image, dec_image, amp = self.imageModel.PointSource.point_source_list( self.kwargs_ps, self.kwargs_lens) print(ra_image, dec_image, amp) x_grid, y_grid = self.imageModel.Data.coordinates radius = 0.5 mask_point_source_list = self.psf_fitting.mask_point_sources( ra_image, dec_image, x_grid, y_grid, radius) assert mask_point_source_list[0][10, 10] == 1
class TestSimulation(object): def setup(self): self.SimAPI = Simulation() # data specifics sigma_bkg = 1. # background noise per pixel exp_time = 10 # 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.5 # full width half max of PSF # PSF specification data_class = self.SimAPI.data_configure(numPix, deltaPix, exp_time, sigma_bkg) self.kwargs_data = data_class.constructor_kwargs() psf_class = self.SimAPI.psf_configure(psf_type='GAUSSIAN', fwhm=fwhm, kernelsize=31, deltaPix=deltaPix, truncate=5) self.kwargs_psf = psf_class.constructor_kwargs() # 'EXERNAL_SHEAR': external shear kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'q': 0.8, 'phi_G': 0.2 } lens_model_list = ['SPEP', 'SHEAR'] self.kwargs_lens = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile kwargs_sersic = { 'I0_sersic': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'I0_sersic': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'phi_G': 0.2, 'q': 0.9 } 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.0, 'dec_source': 0.0, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness point_source_class = PointSource( point_source_type_list=['SOURCE_POSITION'], fixed_magnification_list=[True]) kwargs_numerics = {'subgrid_res': 2, 'psf_subgrid': True} self.imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) def test_im_sim(self): # model specifics # list of lens models, supports: # 'EXERNAL_SHEAR': external shear kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'q': 0.8, 'phi_G': 0.2 } kwargs_lens_list = [kwargs_spemd, kwargs_shear] # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile kwargs_sersic = { 'I0_sersic': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'I0_sersic': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'phi_G': 0.2, 'q': 0.9 } kwargs_lens_light_list = [kwargs_sersic] kwargs_source_list = [kwargs_sersic_ellipse] kwargs_ps = [ { 'ra_source': 0.0, 'dec_source': 0.0, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness image_sim = self.SimAPI.simulate(self.imageModel, kwargs_lens_list, kwargs_source_list, kwargs_lens_light_list, kwargs_ps) assert len(image_sim) == 100 npt.assert_almost_equal(np.sum(image_sim), 24476.280571230454, decimal=-3) def test_normalize_flux(self): kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'q': 0.8, 'phi_G': 0.2 } lens_model_list = ['SPEP', 'SHEAR'] kwargs_lens_list = [kwargs_spemd, kwargs_shear] # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile kwargs_sersic = { 'I0_sersic': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'I0_sersic': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'phi_G': 0.2, 'q': 0.9 } lens_light_model_list = ['SERSIC'] kwargs_lens_light_list = [kwargs_sersic] source_model_list = ['SERSIC_ELLIPSE'] kwargs_source_list = [kwargs_sersic_ellipse] kwargs_ps = [ { 'ra_source': 0.0, 'dec_source': 0.0, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness kwargs_options = { 'lens_model_list': lens_model_list, 'lens_light_model_list': lens_light_model_list, 'source_light_model_list': source_model_list, 'psf_type': 'PIXEL', 'point_source_list': ['SOURCE_POSITION'], 'fixed_magnification': True # if True, simulates point source at source position of 'sourcePos_xy' in kwargs_ps } #image_sim = self.SimAPI.simulate(self.imageModel, kwargs_lens_list, kwargs_source_list, # kwargs_lens_light_list, kwargs_ps) kwargs_source_updated, kwargs_lens_light_updated, kwargs_else_updated = self.SimAPI.normalize_flux( kwargs_options, kwargs_source_list, kwargs_lens_light_list, kwargs_ps, norm_factor_source=3, norm_factor_lens_light=2, norm_factor_point_source=0.) print(kwargs_else_updated, 'test') assert kwargs_else_updated[0]['source_amp'] == 0 def test_normalize_flux_source(self): # 'EXERNAL_SHEAR': external shear kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'q': 0.8, 'phi_G': 0.2 } lens_model_list = ['SPEP', 'SHEAR'] kwargs_lens_list = [kwargs_spemd, kwargs_shear] # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile kwargs_sersic = { 'I0_sersic': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'I0_sersic': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'phi_G': 0.2, 'q': 0.9 } lens_light_model_list = ['SERSIC'] kwargs_lens_light_list = [kwargs_sersic] source_model_list = ['SERSIC_ELLIPSE'] kwargs_source_list = [kwargs_sersic_ellipse] kwargs_options = { 'lens_model_list': lens_model_list, 'lens_light_model_list': lens_light_model_list, 'source_light_model_list': source_model_list, 'psf_type': 'PIXEL', 'point_source': True # if True, simulates point source at source position of 'sourcePos_xy' in kwargs_else } kwargs_source_updated = self.SimAPI.normalize_flux_source( kwargs_options, kwargs_source_list, norm_factor_source=10) assert kwargs_source_updated[0][ 'I0_sersic'] == kwargs_source_list[0]['I0_sersic'] * 10 def test_source_plane(self): numPix = 10 deltaPix = 0.1 kwargs_sersic_ellipse = { 'I0_sersic': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'phi_G': 0.2, 'q': 0.9 } lens_light_model_list = ['SERSIC'] source_model_list = ['SERSIC_ELLIPSE'] kwargs_source = [kwargs_sersic_ellipse] kwargs_options = { 'lens_light_model_list': lens_light_model_list, 'source_light_model_list': source_model_list, 'psf_type': 'pixel', 'point_source': True # if True, simulates point source at source position of 'sourcePos_xy' in kwargs_else } source = self.SimAPI.source_plane(kwargs_options, kwargs_source, numPix, deltaPix) assert len(source) == numPix def test_shift_coordinate_grid(self): x_shift = 0.05 y_shift = 0 kwargs_data_shifted = self.SimAPI.shift_coordinate_grid( self.kwargs_data, x_shift, y_shift, pixel_units=False) kwargs_data_new = copy.deepcopy(self.kwargs_data) kwargs_data_new['ra_shift'] = x_shift kwargs_data_new['dec_shift'] = y_shift data = Data(kwargs_data=kwargs_data_shifted) data_new = Data(kwargs_data=kwargs_data_new) ra, dec = 0, 0 x, y = data.map_coord2pix(ra, dec) x_new, y_new = data_new.map_coord2pix(ra, dec) npt.assert_almost_equal(x, x_new, decimal=10) npt.assert_almost_equal(y, y_new, decimal=10) ra, dec = data.map_pix2coord(x, y) ra_new, dec_new = data_new.map_pix2coord(x, y) npt.assert_almost_equal(ra, ra_new, decimal=10) npt.assert_almost_equal(dec, dec_new, decimal=10) x_coords, y_coords = data.coordinates x_coords_new, y_coords_new = data_new.coordinates npt.assert_almost_equal(x_coords[0], x_coords_new[0], decimal=10) npt.assert_almost_equal(y_coords[0], y_coords_new[0], decimal=10)
class TestImageModel(object): """ tests the source model routines """ def setup(self): self.SimAPI = Simulation() # data specifics sigma_bkg = .05 # background noise per pixel 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.5 # full width half max of PSF psf_type = 'PIXEL' # 'gaussian', 'pixel', 'NONE' # PSF specification data_class = self.SimAPI.data_configure(numPix, deltaPix, exp_time, sigma_bkg) psf_class = self.SimAPI.psf_configure(psf_type='GAUSSIAN', fwhm=fwhm, kernelsize=31, deltaPix=deltaPix, truncate=5) # 'EXERNAL_SHEAR': external shear kwargs_shear = {'e1': 0.01, 'e2': 0.01} # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = {'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'q': 0.8, 'phi_G': 0.2} lens_model_list = ['SPEP', 'SHEAR'] self.kwargs_lens = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile kwargs_sersic = {'I0_sersic': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0} # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = {'I0_sersic': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'phi_G': 0.2, 'q': 0.9} 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.0, 'dec_source': 0.0, 'source_amp': 1.}] # quasar point source position in the source plane and intrinsic brightness point_source_class = PointSource(point_source_type_list=['SOURCE_POSITION'], fixed_magnification_list=[True]) kwargs_numerics = {'subgrid_res': 2, 'psf_subgrid': True} 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 = self.SimAPI.simulate(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps) data_class.update_data(image_sim) kwargs_data = data_class.constructor_kwargs() kwargs_psf = psf_class.constructor_kwargs() self.solver = LensEquationSolver(lensModel=lens_model_class) multi_band_list = [[kwargs_data, kwargs_psf, kwargs_numerics]] self.imageModel = Multiband(multi_band_list, lens_model_class, source_model_class, lens_light_model_class, point_source_class) def test_source_surface_brightness(self): source_model = self.imageModel.source_surface_brightness(self.kwargs_source, self.kwargs_lens, unconvolved=False, de_lensed=False) assert len(source_model[0]) == 100 npt.assert_almost_equal(source_model[0][10, 10], 0.1370014246240874, decimal=8) source_model = self.imageModel.source_surface_brightness(self.kwargs_source, self.kwargs_lens, unconvolved=True, de_lensed=False) assert len(source_model[0]) == 100 npt.assert_almost_equal(source_model[0][10, 10], 0.13164547458291054, decimal=8) def test_lens_surface_brightness(self): lens_flux = self.imageModel.lens_surface_brightness(self.kwargs_lens_light, unconvolved=False) npt.assert_almost_equal(lens_flux[0][50, 50], 0.4415194068014886, decimal=8) lens_flux = self.imageModel.lens_surface_brightness(self.kwargs_lens_light, unconvolved=True) npt.assert_almost_equal(lens_flux[0][50, 50], 4.7310552067454452, decimal=8) def test_image_linear_solve(self): model, error_map, cov_param, param = self.imageModel.image_linear_solve(self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, inv_bool=False) chi2_reduced = self.imageModel._imageModel_list[0].reduced_chi2(model[0], error_map[0]) npt.assert_almost_equal(chi2_reduced, 1, decimal=1) def test_image(self): model = self.imageModel.image(self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, unconvolved=False, source_add=True, lens_light_add=True, point_source_add=True) error_map = self.imageModel.error_map(self.kwargs_lens, self.kwargs_ps) chi2_reduced = self.imageModel._imageModel_list[0].reduced_chi2(model[0], error_map[0]) npt.assert_almost_equal(chi2_reduced, 1, decimal=1) def test_image_positions(self): x_im, y_im = self.imageModel.image_positions(self.kwargs_ps, self.kwargs_lens) ra_pos, dec_pos = self.solver.image_position_from_source(sourcePos_x=self.kwargs_ps[0]['ra_source'], sourcePos_y=self.kwargs_ps[0]['dec_source'], kwargs_lens=self.kwargs_lens) ra_pos_new = x_im[0] npt.assert_almost_equal(ra_pos_new[0], ra_pos[0], decimal=8) npt.assert_almost_equal(ra_pos_new[1], ra_pos[1], decimal=8) npt.assert_almost_equal(ra_pos_new[2], ra_pos[2], decimal=8) npt.assert_almost_equal(ra_pos_new[3], ra_pos[3], decimal=8) def test_likelihood_data_given_model(self): logL = self.imageModel.likelihood_data_given_model(self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, source_marg=False) npt.assert_almost_equal(logL, -5100, decimal=-3) logLmarg = self.imageModel.likelihood_data_given_model(self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, source_marg=True) npt.assert_almost_equal(logL - logLmarg, 0, decimal=-2) def test_numData_evaluate(self): numData = self.imageModel.numData_evaluate() assert numData == 10000 def test_fermat_potential(self): phi_fermat = self.imageModel.fermat_potential(self.kwargs_lens, self.kwargs_ps) phi_fermat = phi_fermat[0] npt.assert_almost_equal(phi_fermat[0], -0.2719737, decimal=3) npt.assert_almost_equal(phi_fermat[1], -0.2719737, decimal=3) npt.assert_almost_equal(phi_fermat[2], -0.51082354, decimal=3) npt.assert_almost_equal(phi_fermat[3], -0.51082354, decimal=3)
class TestSimulation(object): def setup(self): self.SimAPI = Simulation() # data specifics sigma_bkg = 1. # background noise per pixel exp_time = 10 # 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.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=31, deltaPix=deltaPix, truncate=5) psf_class = PSF(kwargs_psf) # 'EXERNAL_SHEAR': external shear kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) e1, e2 = param_util.phi_q2_ellipticity(0.2, 0.8) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': e1, 'e2': e2 } lens_model_list = ['SPEP', 'SHEAR'] self.kwargs_lens = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile 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': 7, 'center_x': 0, 'center_y': 0, 'e1': 0.2, 'e2': 0.3 } 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.0, 'dec_source': 0.0, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness point_source_class = PointSource( point_source_type_list=['SOURCE_POSITION'], fixed_magnification_list=[True]) kwargs_numerics = {'subgrid_res': 2, 'psf_subgrid': True} self.imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) def test_im_sim(self): # model specifics # list of lens models, supports: # 'EXERNAL_SHEAR': external shear kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) e1, e2 = param_util.phi_q2_ellipticity(0.2, 0.8) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': e1, 'e2': e2 } kwargs_lens_list = [kwargs_spemd, kwargs_shear] # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile 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': 7, 'center_x': 0, 'center_y': 0, 'e1': 0.2, 'e2': 0.3 } kwargs_lens_light_list = [kwargs_sersic] kwargs_source_list = [kwargs_sersic_ellipse] kwargs_ps = [ { 'ra_source': 0.0, 'dec_source': 0.0, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness image_sim = self.SimAPI.simulate(self.imageModel, kwargs_lens_list, kwargs_source_list, kwargs_lens_light_list, kwargs_ps) assert len(image_sim) == 100 npt.assert_almost_equal(np.sum(image_sim), 14894.805448596271, decimal=-3) def test_normalize_flux(self): kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'q': 0.8, 'phi_G': 0.2 } lens_model_list = ['SPEP', 'SHEAR'] kwargs_lens_list = [kwargs_spemd, kwargs_shear] # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile 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': 7, 'center_x': 0, 'center_y': 0, 'e1': 0.2, 'e2': 0.3 } lens_light_model_list = ['SERSIC'] kwargs_lens_light_list = [kwargs_sersic] source_model_list = ['SERSIC_ELLIPSE'] kwargs_source_list = [kwargs_sersic_ellipse] kwargs_ps = [ { 'ra_source': 0.0, 'dec_source': 0.0, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness kwargs_options = { 'lens_model_list': lens_model_list, 'lens_light_model_list': lens_light_model_list, 'source_light_model_list': source_model_list, 'psf_type': 'PIXEL', 'point_source_list': ['SOURCE_POSITION'], 'fixed_magnification': True # if True, simulates point source at source position of 'sourcePos_xy' in kwargs_ps } #image_sim = self.SimAPI.simulate(self.imageModel, kwargs_lens_list, kwargs_source_list, # kwargs_lens_light_list, kwargs_ps) kwargs_source_updated, kwargs_lens_light_updated, kwargs_else_updated = self.SimAPI.normalize_flux( kwargs_options, kwargs_source_list, kwargs_lens_light_list, kwargs_ps, norm_factor_source=3, norm_factor_lens_light=2, norm_factor_point_source=0.) print(kwargs_else_updated, 'test') assert kwargs_else_updated[0]['source_amp'] == 0 def test_normalize_flux_source(self): # 'EXERNAL_SHEAR': external shear kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'q': 0.8, 'phi_G': 0.2 } lens_model_list = ['SPEP', 'SHEAR'] kwargs_lens_list = [kwargs_spemd, kwargs_shear] # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile 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': 7, 'center_x': 0, 'center_y': 0, 'e1': 0.2, 'e2': 0.3 } lens_light_model_list = ['SERSIC'] kwargs_lens_light_list = [kwargs_sersic] source_model_list = ['SERSIC_ELLIPSE'] kwargs_source_list = [kwargs_sersic_ellipse] kwargs_options = { 'lens_model_list': lens_model_list, 'lens_light_model_list': lens_light_model_list, 'source_light_model_list': source_model_list, 'psf_type': 'PIXEL', 'point_source': True # if True, simulates point source at source position of 'sourcePos_xy' in kwargs_else } kwargs_source_updated = self.SimAPI.normalize_flux_source( kwargs_options, kwargs_source_list, norm_factor_source=10) assert kwargs_source_updated[0][ 'amp'] == kwargs_source_list[0]['amp'] * 10 def test_source_plane(self): numPix = 10 deltaPix = 0.1 kwargs_sersic_ellipse = { 'amp': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'e1': 0.2, 'e2': 0.3 } lens_light_model_list = ['SERSIC'] source_model_list = ['SERSIC_ELLIPSE'] kwargs_source = [kwargs_sersic_ellipse] kwargs_options = { 'lens_light_model_list': lens_light_model_list, 'source_light_model_list': source_model_list, 'psf_type': 'pixel', 'point_source': True # if True, simulates point source at source position of 'sourcePos_xy' in kwargs_else } source = self.SimAPI.source_plane(kwargs_options, kwargs_source, numPix, deltaPix) assert len(source) == numPix
class TestFittingSequence(object): """ test the fitting sequences """ 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 = 100 # cutout pixel size deltaPix = 0.05 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.5 # full width half max of PSF # PSF specification data_class = self.SimAPI.data_configure(numPix, deltaPix, exp_time, sigma_bkg) psf_class = self.SimAPI.psf_configure(psf_type='GAUSSIAN', fwhm=fwhm, kernelsize=31, deltaPix=deltaPix, truncate=3, kernel=None) psf_class = self.SimAPI.psf_configure( psf_type='PIXEL', fwhm=fwhm, kernelsize=31, deltaPix=deltaPix, truncate=6, kernel=psf_class.kernel_point_source) # 'EXERNAL_SHEAR': external shear kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'q': 0.8, 'phi_G': 0.2 } lens_model_list = ['SPEP', 'SHEAR'] self.kwargs_lens = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile kwargs_sersic = { 'I0_sersic': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'I0_sersic': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'phi_G': 0.2, 'q': 0.9 } 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.0, 'dec_source': 0.0, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness 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 = self.SimAPI.simulate(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_data = data_class.constructor_kwargs() self.kwargs_psf = psf_class.constructor_kwargs() 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, 'fixed_magnification_list': [False], } self.kwargs_numerics = {'subgrid_res': 2, 'psf_subgrid': True} num_source_model = len(source_model_list) self.kwargs_constraints = { 'joint_center_lens_light': False, 'joint_center_source_light': False, 'num_point_source_list': [4], 'additional_images_list': [False], 'fix_to_point_source_list': [False] * num_source_model, 'image_plane_source_list': [False] * num_source_model, 'solver': False, 'solver_type': 'PROFILE_SHEAR', # 'PROFILE', 'PROFILE_SHEAR', 'ELLIPSE', 'CENTER' } self.kwargs_likelihood = { 'force_no_add_image': True, 'source_marg': True, 'point_source_likelihood': False, 'position_uncertainty': 0.004, 'check_solver': True, 'solver_tolerance': 0.001 } def test_simulationAPI_image(self): npt.assert_almost_equal(self.data_class.data[4, 4], 0.1, decimal=0) def test_simulationAPI_psf(self): npt.assert_almost_equal(self.psf_class.kernel_pixel[1, 1], 1.681921511056146e-07, decimal=8) def test_fitting_sequence(self): kwargs_init = [ self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps ] lens_sigma = [{ 'theta_E_sigma': 0.1, 'gamma_sigma': 0.1, 'ellipse_sigma': 0.1, 'center_x_sigma': 0.1, 'center_y_sigma': 0.1 }, { 'shear_sigma': 0.1 }] source_sigma = [{ 'R_sersic_sigma': 0.05, 'n_sersic_sigma': 0.5, 'center_x_sigma': 0.1, 'center_y_sigma': 0.1, 'ellipse_sigma': 0.1 }] lens_light_sigma = [{ 'R_sersic_sigma': 0.05, 'n_sersic_sigma': 0.5, 'center_x_sigma': 0.1, 'center_y_sigma': 0.1 }] ps_sigma = [{'pos_sigma': 1, 'point_amp_sigma': 1}] kwargs_sigma = [lens_sigma, source_sigma, lens_light_sigma, ps_sigma] kwargs_fixed = [[{}, {}], [{}], [{}], [{}]] kwargs_params = [ kwargs_init, kwargs_sigma, kwargs_fixed, kwargs_init, kwargs_init ] image_band = [self.kwargs_data, self.kwargs_psf, self.kwargs_numerics] multi_band_list = [image_band] fittingSequence = FittingSequence(multi_band_list, self.kwargs_model, self.kwargs_constraints, self.kwargs_likelihood, kwargs_params) lens_temp, source_temp, lens_light_temp, else_temp, chain_list, param_list, samples_mcmc, param_mcmc, dist_mcmc = fittingSequence.fit_sequence( fitting_kwargs_list=[]) npt.assert_almost_equal(lens_temp[0]['theta_E'], self.kwargs_lens[0]['theta_E'], decimal=2) n_p = 2 n_i = 2 """
#image_ps = imageModel_ps.image(kwargs_ps=kwargs_ps) #plt.matshow(np.log10(image_ps), origin='lower') #plt.show() # import numpy as np import os import time import copy import corner import astropy.io.fits as pyfits import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import make_axes_locatable from lenstronomy.SimulationAPI.simulations import Simulation SimAPI = Simulation() # import PSF file (here as a relative path in the lenstronomy_extension repository) # the psf_example.fits file can be found here: # https://github.com/sibirrer/lenstronomy_extensions/tree/master/Data/PSF_TinyTim # and imported from a local file path as well # data specifics background_rms = 0.1 # background noise per pixel (Gaussian) exp_time = 100. # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) numPix = 21 # cutout pixel size deltaPix = 1 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 2.5 # full width half max of PSF (only valid when psf_type='gaussian') psf_type = 'GAUSSIAN' # 'gaussian', 'pixel', 'NONE' kernel_size = 21 # initial input simulation
class TestImageModel(object): """ tests the source model routines """ def setup(self): self.SimAPI = Simulation() # data specifics sigma_bkg = .05 # background noise per pixel 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.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=31, deltaPix=deltaPix, truncate=3, kernel=None) psf_class = PSF(kwargs_psf) psf_class._psf_error_map = np.zeros_like(psf_class.kernel_point_source) # 'EXERNAL_SHEAR': external shear kwargs_shear = {'e1': 0.01, 'e2': 0.01} # gamma_ext: shear strength, psi_ext: shear angel (in radian) phi, q = 0.2, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi, q) kwargs_spemd = {'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': e1, 'e2': e2} lens_model_list = ['SPEP', 'SHEAR'] self.kwargs_lens = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile kwargs_sersic = {'amp': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0} # 'SERSIC_ELLIPSE': elliptical Sersic profile phi, q = 0.2, 0.9 e1, e2 = param_util.phi_q2_ellipticity(phi, q) kwargs_sersic_ellipse = {'amp': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'e1': e1, 'e2': e2} 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.01, 'dec_source': 0.0, 'source_amp': 1.}] # quasar point source position in the source plane and intrinsic brightness point_source_class = PointSource(point_source_type_list=['SOURCE_POSITION'], fixed_magnification_list=[True]) kwargs_numerics = {'subgrid_res': 2, 'psf_subgrid': True} 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 = self.SimAPI.simulate(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps) data_class.update_data(image_sim) 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.solver = LensEquationSolver(lensModel=self.imageModel.LensModel) def test_source_surface_brightness(self): source_model = self.imageModel.source_surface_brightness(self.kwargs_source, self.kwargs_lens, unconvolved=False, de_lensed=False) assert len(source_model) == 100 npt.assert_almost_equal(source_model[10, 10], 0.13939841209844345, decimal=4) source_model = self.imageModel.source_surface_brightness(self.kwargs_source, self.kwargs_lens, unconvolved=True, de_lensed=False) assert len(source_model) == 100 npt.assert_almost_equal(source_model[10, 10], 0.13536114618182182, decimal=4) def test_lens_surface_brightness(self): lens_flux = self.imageModel.lens_surface_brightness(self.kwargs_lens_light, unconvolved=False) npt.assert_almost_equal(lens_flux[50, 50], 0.54214440654021534, decimal=4) lens_flux = self.imageModel.lens_surface_brightness(self.kwargs_lens_light, unconvolved=True) npt.assert_almost_equal(lens_flux[50, 50], 4.7310552067454452, decimal=4) def test_image_linear_solve(self): model, error_map, cov_param, param = self.imageModel.image_linear_solve(self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, inv_bool=False) chi2_reduced = self.imageModel.reduced_chi2(model, error_map) npt.assert_almost_equal(chi2_reduced, 1, decimal=1) def test_image_with_params(self): model = self.imageModel.image(self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, unconvolved=False, source_add=True, lens_light_add=True, point_source_add=True) error_map = self.imageModel.error_map(self.kwargs_lens, self.kwargs_ps) chi2_reduced = self.imageModel.reduced_chi2(model, error_map) npt.assert_almost_equal(chi2_reduced, 1, decimal=1) def test_point_sources_list(self): point_source_list = self.imageModel.point_sources_list(self.kwargs_ps, self.kwargs_lens) assert len(point_source_list) == 4 def test_image_positions(self): x_im, y_im = self.imageModel.image_positions(self.kwargs_ps, self.kwargs_lens) ra_pos, dec_pos = self.solver.image_position_from_source(sourcePos_x=self.kwargs_ps[0]['ra_source'], sourcePos_y=self.kwargs_ps[0]['dec_source'], kwargs_lens=self.kwargs_lens) ra_pos_new = x_im[0] print(ra_pos_new, ra_pos) npt.assert_almost_equal(ra_pos_new[0], ra_pos[0], decimal=8) npt.assert_almost_equal(ra_pos_new[1], ra_pos[1], decimal=8) npt.assert_almost_equal(ra_pos_new[2], ra_pos[2], decimal=8) npt.assert_almost_equal(ra_pos_new[3], ra_pos[3], decimal=8) def test_likelihood_data_given_model(self): logL = self.imageModel.likelihood_data_given_model(self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, source_marg=False) npt.assert_almost_equal(logL, -5000, decimal=-3) logLmarg = self.imageModel.likelihood_data_given_model(self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, source_marg=True) npt.assert_almost_equal(logL - logLmarg, 0, decimal=-3) def test_reduced_residuals(self): model = self.SimAPI.simulate(self.imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, no_noise=True) residuals = self.imageModel.reduced_residuals(model, error_map=0) npt.assert_almost_equal(np.std(residuals), 1.01, decimal=1) chi2 = self.imageModel.reduced_chi2(model, error_map=0) npt.assert_almost_equal(chi2, 1, decimal=1) def test_numData_evaluate(self): numData = self.imageModel.numData_evaluate() assert numData == 10000 def test_fermat_potential(self): phi_fermat = self.imageModel.fermat_potential(self.kwargs_lens, self.kwargs_ps) print(phi_fermat) npt.assert_almost_equal(phi_fermat[0][0], -0.2630531731871062, decimal=3) npt.assert_almost_equal(phi_fermat[0][1], -0.2809100018126987, decimal=3) npt.assert_almost_equal(phi_fermat[0][2], -0.5086643370512096, decimal=3) npt.assert_almost_equal(phi_fermat[0][3], -0.5131716608238992, decimal=3) def test_add_mask(self): mask = np.array([[0, 1],[1, 0]]) A = np.ones((10, 4)) A_masked = self.imageModel._add_mask(A, mask) assert A[0, 1] == A_masked[0, 1] assert A_masked[0, 3] == 0 def test_point_source_rendering(self): # initialize data from lenstronomy.SimulationAPI.simulations import Simulation SimAPI = Simulation() numPix = 100 deltaPix = 0.05 kwargs_data = SimAPI.data_configure(numPix, deltaPix, exposure_time=1, sigma_bkg=1) data_class = Data(kwargs_data) kernel = np.zeros((5, 5)) kernel[2, 2] = 1 kwargs_psf = {'kernel_point_source': kernel, 'kernel_pixel': kernel, 'psf_type': 'PIXEL'} psf_class = PSF(kwargs_psf) lens_model_class = LensModel(['SPEP']) source_model_class = LightModel([]) lens_light_model_class = LightModel([]) kwargs_numerics = {'subgrid_res': 2, 'point_source_subgrid': 1} point_source_class = PointSource(point_source_type_list=['LENSED_POSITION'], fixed_magnification_list=[False]) makeImage = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class, kwargs_numerics=kwargs_numerics) # chose point source positions x_pix = np.array([10, 5, 10, 90]) y_pix = np.array([40, 50, 60, 50]) ra_pos, dec_pos = makeImage.Data.map_pix2coord(x_pix, y_pix) e1, e2 = param_util.phi_q2_ellipticity(0, 0.8) kwargs_lens_init = [{'theta_E': 1, 'gamma': 2, 'e1': e1, 'e2': e2, 'center_x': 0, 'center_y': 0}] kwargs_else = [{'ra_image': ra_pos, 'dec_image': dec_pos, 'point_amp': np.ones_like(ra_pos)}] model = makeImage.image(kwargs_lens_init, kwargs_source={}, kwargs_lens_light={}, kwargs_ps=kwargs_else) image = makeImage.ImageNumerics.array2image(model) for i in range(len(x_pix)): npt.assert_almost_equal(image[y_pix[i], x_pix[i]], 1, decimal=2) x_pix = np.array([10.5, 5.5, 10.5, 90.5]) y_pix = np.array([40, 50, 60, 50]) ra_pos, dec_pos = makeImage.Data.map_pix2coord(x_pix, y_pix) phi, q = 0., 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi, q) kwargs_lens_init = [{'theta_E': 1, 'gamma': 2, 'e1': e1, 'e2': e2, 'center_x': 0, 'center_y': 0}] kwargs_else = [{'ra_image': ra_pos, 'dec_image': dec_pos, 'point_amp': np.ones_like(ra_pos)}] model = makeImage.image(kwargs_lens_init, kwargs_source={}, kwargs_lens_light={}, kwargs_ps=kwargs_else) image = makeImage.ImageNumerics.array2image(model) for i in range(len(x_pix)): print(int(y_pix[i]), int(x_pix[i]+0.5)) npt.assert_almost_equal(image[int(y_pix[i]), int(x_pix[i])], 0.5, decimal=1) npt.assert_almost_equal(image[int(y_pix[i]), int(x_pix[i]+0.5)], 0.5, decimal=1)
class TestFittingSequence(object): """ test the fitting sequences """ 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 = 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 self.kwargs_data = self.SimAPI.data_configure(numPix, deltaPix, exp_time, sigma_bkg) data_class = Data(self.kwargs_data) kwargs_psf = self.SimAPI.psf_configure(psf_type='GAUSSIAN', fwhm=fwhm, kernelsize=11, deltaPix=deltaPix, truncate=3, kernel=None) self.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(self.kwargs_psf) # 'EXERNAL_SHEAR': external shear kwargs_shear = {'e1': 0.01, 'e2': 0.01} # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = {'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1} lens_model_list = ['SPEP', 'SHEAR'] self.kwargs_lens = [kwargs_spemd, kwargs_shear] 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) self.kwargs_ps = [{'ra_source': 0.0, 'dec_source': 0.0, 'source_amp': 1.}] # quasar point source position in the source plane and intrinsic brightness 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 = self.SimAPI.simulate(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_data['image_data'] = image_sim 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, 'fixed_magnification_list': [False], } self.kwargs_numerics = { 'subgrid_res': 1, 'psf_subgrid': False} num_source_model = len(source_model_list) self.kwargs_constraints = {'joint_center_lens_light': False, 'joint_center_source_light': False, 'num_point_source_list': [4], 'additional_images_list': [False], 'fix_to_point_source_list': [False] * num_source_model, 'image_plane_source_list': [False] * num_source_model, 'solver': False, 'solver_type': 'PROFILE_SHEAR', # 'PROFILE', 'PROFILE_SHEAR', 'ELLIPSE', 'CENTER' } self.kwargs_likelihood = {'force_no_add_image': True, 'source_marg': True, 'point_source_likelihood': False, 'position_uncertainty': 0.004, 'check_solver': False, 'solver_tolerance': 0.001, 'check_positive_flux': True, } def test_simulationAPI_image(self): npt.assert_almost_equal(self.data_class.data[4, 4], 0.1, decimal=0) def test_simulationAPI_psf(self): npt.assert_almost_equal(self.psf_class.kernel_pixel[1, 1], 0.0010335243878451812, decimal=6) def test_fitting_sequence(self): #kwargs_init = [self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps] lens_sigma = [{'theta_E': 0.1, 'gamma': 0.1, 'e1': 0.1, 'e2': 0.1, 'center_x': 0.1, 'center_y': 0.1}, {'e1': 0.1, 'e2': 0.1}] lens_lower = [{'theta_E': 0., 'gamma': 1.5, 'center_x': -2, 'center_y': -2, 'e1': -0.4, 'e2': -0.4}, {'e1': -0.3, 'e2': -0.3}] lens_upper = [{'theta_E': 10., 'gamma': 2.5, 'center_x': 2, 'center_y': 2, 'e1': 0.4, 'e2': 0.4}, {'e1': 0.3, 'e2': 0.3}] source_sigma = [{'R_sersic': 0.05, 'n_sersic': 0.5, 'center_x': 0.1, 'center_y': 0.1, 'e1': 0.1, 'e2': 0.1}] source_lower = [{'R_sersic': 0.01, 'n_sersic': 0.5, 'center_x': -2, 'center_y': -2, 'e1': -0.4, 'e2': -0.4}] source_upper = [{'R_sersic': 10, 'n_sersic': 5.5, 'center_x': 2, 'center_y': 2, 'e1': 0.4, 'e2': 0.4}] lens_light_sigma = [{'R_sersic': 0.05, 'n_sersic': 0.5, 'center_x': 0.1, 'center_y': 0.1}] lens_light_lower = [{'R_sersic': 0.01, 'n_sersic': 0.5, 'center_x': -2, 'center_y': -2}] lens_light_upper = [{'R_sersic': 10, 'n_sersic': 5.5, 'center_x': 2, 'center_y': 2}] ps_sigma = [{'ra_source': 1, 'dec_source': 1, 'point_amp': 1}] lens_param = self.kwargs_lens, lens_sigma, [{}, {}], lens_lower, lens_upper source_param = self.kwargs_source, source_sigma, [{}], source_lower, source_upper lens_light_param = self.kwargs_lens_light, lens_light_sigma, [{}], lens_light_lower, lens_light_upper ps_param = self.kwargs_ps, ps_sigma, [{}], self.kwargs_ps, self.kwargs_ps kwargs_params = {'lens_model': lens_param, 'source_model': source_param, 'lens_light_model': lens_light_param, 'point_source_model': ps_param, #'cosmography': cosmo_param } #kwargs_params = [kwargs_init, kwargs_sigma, kwargs_fixed, kwargs_init, kwargs_init] image_band = [self.kwargs_data, self.kwargs_psf, self.kwargs_numerics] multi_band_list = [image_band] fittingSequence = FittingSequence(multi_band_list, self.kwargs_model, self.kwargs_constraints, self.kwargs_likelihood, kwargs_params) lens_temp, source_temp, lens_light_temp, else_temp, cosmo_temp, chain_list, param_list, samples_mcmc, param_mcmc, dist_mcmc = fittingSequence.fit_sequence(fitting_kwargs_list=[]) npt.assert_almost_equal(lens_temp[0]['theta_E'], self.kwargs_lens[0]['theta_E'], decimal=2) n_p = 2 n_i = 2 fitting_kwargs_list = [ {'fitting_routine': 'PSO', 'sigma_scale': 1, 'n_particles': n_p, 'n_iterations': n_i}, {'fitting_routine': 'MCMC', 'sigma_scale': 0.1, 'n_burn': 1, 'n_run': 1, 'walkerRatio': 2}, {'fitting_routine': 'align_images', 'lower_limit_shift': -0.1, 'upper_limit_shift': 0.1, 'n_particles': 2, 'n_iterations': 2}, {'fitting_routine': 'psf_iteration', 'psf_iter_num': 2, 'psf_iter_factor': 0.5, 'kwargs_psf_iter': {'stacking_option': 'mean'}} ] lens_temp, source_temp, lens_light_temp, else_temp, cosmo_temp, chain_list, param_list, samples_mcmc, param_mcmc, dist_mcmc = fittingSequence.fit_sequence(fitting_kwargs_list=fitting_kwargs_list) npt.assert_almost_equal(lens_temp[0]['theta_E'], self.kwargs_lens[0]['theta_E'], decimal=1)
class TestOutputPlots(object): """ test the fitting sequences """ 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 = 100 # cutout pixel size deltaPix = 0.05 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.5 # full width half max of PSF # PSF specification data_class = self.SimAPI.data_configure(numPix, deltaPix, exp_time, sigma_bkg) psf_class = self.SimAPI.psf_configure(psf_type='GAUSSIAN', fwhm=fwhm, kernelsize=31, deltaPix=deltaPix, truncate=3, kernel=None) psf_class = self.SimAPI.psf_configure( psf_type='PIXEL', fwhm=fwhm, kernelsize=31, deltaPix=deltaPix, truncate=6, kernel=psf_class.kernel_point_source) # 'EXERNAL_SHEAR': external shear kwargs_shear = { 'e1': 0.01, 'e2': 0.01 } # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'q': 0.8, 'phi_G': 0.2 } lens_model_list = ['SPEP', 'SHEAR'] self.kwargs_lens = [kwargs_spemd, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) self.LensModel = lens_model_class # list of light profiles (for lens and source) # 'SERSIC': spherical Sersic profile kwargs_sersic = { 'I0_sersic': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'I0_sersic': 1., 'R_sersic': .6, 'n_sersic': 7, 'center_x': 0, 'center_y': 0, 'phi_G': 0.2, 'q': 0.9 } 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.0, 'dec_source': 0.0, 'source_amp': 1. } ] # quasar point source position in the source plane and intrinsic brightness 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 = self.SimAPI.simulate(imageModel, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps) data_class.update_data(image_sim) self.kwargs_data = data_class.constructor_kwargs() self.kwargs_psf = psf_class.constructor_kwargs() 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, 'fixed_magnification_list': [False], } self.kwargs_numerics = kwargs_numerics self.data_class = Data(self.kwargs_data) def test_lensModelPlot(self): lensPlot = LensModelPlot(self.kwargs_data, self.kwargs_psf, self.kwargs_numerics, self.kwargs_model, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, arrow_size=0.02, cmap_string="gist_heat", high_res=5) f, axes = plt.subplots(2, 3, figsize=(16, 8), sharex=False, sharey=False) lensPlot.data_plot(ax=axes[0, 0]) lensPlot.model_plot(ax=axes[0, 1]) lensPlot.normalized_residual_plot(ax=axes[0, 2], v_min=-6, v_max=6) lensPlot.source_plot(ax=axes[1, 0], convolution=False, deltaPix_source=0.01, numPix=100) lensPlot.convergence_plot(ax=axes[1, 1], v_max=1) lensPlot.magnification_plot(ax=axes[1, 2]) plt.close() f, axes = plt.subplots(2, 3, figsize=(16, 8), sharex=False, sharey=False) lensPlot.decomposition_plot(ax=axes[0, 0], text='Lens light', lens_light_add=True, unconvolved=True) lensPlot.decomposition_plot(ax=axes[1, 0], text='Lens light convolved', lens_light_add=True) lensPlot.decomposition_plot(ax=axes[0, 1], text='Source light', source_add=True, unconvolved=True) lensPlot.decomposition_plot(ax=axes[1, 1], text='Source light convolved', source_add=True) lensPlot.decomposition_plot(ax=axes[0, 2], text='All components', source_add=True, lens_light_add=True, unconvolved=True) lensPlot.decomposition_plot(ax=axes[1, 2], text='All components convolved', source_add=True, lens_light_add=True, point_source_add=True) plt.close() f, axes = plt.subplots(2, 3, figsize=(16, 8), sharex=False, sharey=False) lensPlot.subtract_from_data_plot(ax=axes[0, 0], text='Data') lensPlot.subtract_from_data_plot(ax=axes[0, 1], text='Data - Point Source', point_source_add=True) lensPlot.subtract_from_data_plot(ax=axes[0, 2], text='Data - Lens Light', lens_light_add=True) lensPlot.subtract_from_data_plot(ax=axes[1, 0], text='Data - Source Light', source_add=True) lensPlot.subtract_from_data_plot( ax=axes[1, 1], text='Data - Source Light - Point Source', source_add=True, point_source_add=True) lensPlot.subtract_from_data_plot( ax=axes[1, 2], text='Data - Lens Light - Point Source', lens_light_add=True, point_source_add=True) plt.close() f, ax = plt.subplots(1, 1, figsize=(4, 4)) lensPlot.deflection_plot(ax=ax) plt.close() numPix = 100 deltaPix_source = 0.01 f, ax = plt.subplots(1, 1, figsize=(4, 4)) lensPlot.error_map_source_plot(ax, numPix, deltaPix_source) plt.close() f, ax = plt.subplots(1, 1, figsize=(4, 4)) lensPlot.absolute_residual_plot(ax=ax) plt.close() def test_lens_model_plot(self): f, ax = plt.subplots(1, 1, figsize=(4, 4)) numPix = 100 deltaPix = 0.05 ax = output_plots.lens_model_plot(ax, self.LensModel, self.kwargs_lens, numPix, deltaPix, sourcePos_x=0, sourcePos_y=0, point_source=True) plt.close() def test_psf_iteration_compare(self): kwargs_psf = self.kwargs_psf kwargs_psf['kernel_point_source_init'] = kwargs_psf[ 'kernel_point_source'] f, ax = output_plots.psf_iteration_compare(kwargs_psf=kwargs_psf) plt.close() def test_external_shear_direction(self): f, ax = output_plots.ext_shear_direction( data_class=self.data_class, lens_model_class=self.LensModel, kwargs_lens=self.kwargs_lens, strength_multiply=10) plt.close() def test_plot_chain(self): X2_list = [1, 1, 2] pos_list = [[1, 0], [2, 0], [3, 0]] vel_list = [[-1, 0], [0, 0], [1, 0]] param_list = ['test1', 'test2'] chain = X2_list, pos_list, vel_list, None output_plots.plot_chain(chain=chain, param_list=param_list) plt.close()
def setup(self): np.random.seed(42) 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 = 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 = 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) 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., '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 = self.SimAPI.simulate(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 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, 'cosmo_type': 'D_dt' } self.kwargs_numerics = {'subgrid_res': 1, 'psf_subgrid': False} kwargs_constraints = { 'num_point_source_list': [4], 'additional_images_list': [True], 'solver': False, 'solver_type': 'PROFILE_SHEAR', # 'PROFILE', 'PROFILE_SHEAR', 'ELLIPSE', 'CENTER' } 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(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=kwargs_likelihood)