def test_solver_simplified_2(self): lens_model_list = ['SPEP', 'SHEAR_GAMMA_PSI'] lensModel = LensModel(lens_model_list) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.1 sourcePos_y = -0.1 deltapix = 0.05 numPix = 150 gamma = 1.96 e1, e2 = -0.01, -0.01 psi_ext, gamma_ext = param_util.shear_cartesian2polar(e1, e2) kwargs_shear = {'gamma_ext': gamma_ext, 'psi_ext': psi_ext} # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = {'theta_E': 1., 'gamma': gamma, 'center_x': 0, 'center_y': 0, 'e1': -0.2, 'e2': -0.03} kwargs_lens = [kwargs_spemd, kwargs_shear] x_pos, y_pos = lensEquationSolver.findBrightImage(sourcePos_x, sourcePos_y, kwargs_lens, numImages=4, min_distance=deltapix, search_window=numPix * deltapix) kwargs_lens_init = [{'theta_E': 1.3, 'gamma': gamma, 'e1': 0, 'e2': 0, 'center_x': 0., 'center_y': 0}, {'gamma_ext': gamma_ext, 'psi_ext': psi_ext}] solver = Solver4Point(lensModel, solver_type='PROFILE_SHEAR') kwargs_lens_new, accuracy = solver.constraint_lensmodel(x_pos, y_pos, kwargs_lens_init) assert accuracy < 10**(-10) x_source, y_source = lensModel.ray_shooting(x_pos, y_pos, kwargs_lens_new) x_source, y_source = np.mean(x_source), np.mean(y_source) x_pos_new, y_pos_new = lensEquationSolver.findBrightImage(x_source, y_source, kwargs_lens_new, numImages=4, min_distance=deltapix, search_window=numPix * deltapix) print(x_pos, x_pos_new) x_pos = np.sort(x_pos) x_pos_new = np.sort(x_pos_new) y_pos = np.sort(y_pos) y_pos_new = np.sort(y_pos_new) for i in range(len(x_pos)): npt.assert_almost_equal(x_pos[i], x_pos_new[i], decimal=6) npt.assert_almost_equal(y_pos[i], y_pos_new[i], decimal=6) npt.assert_almost_equal(kwargs_lens_new[1]['psi_ext'], kwargs_lens[1]['psi_ext'], decimal=8) npt.assert_almost_equal(kwargs_lens_new[1]['gamma_ext'], kwargs_lens[1]['gamma_ext'], decimal=8)
def assert_differentials(self, lens_model, kwargs): lensModelNum = NumericLens(lens_model) lensModelNum.diff = 0.000001 #x, y = 1., 2. x = np.linspace(start=0.1, stop=8, num=10) y = 0 lensModel = LensModel(lens_model) f_x, f_y = lensModel.lens_model.alpha(x, y, [kwargs]) f_xx, f_xy, f_yx, f_yy = lensModel.hessian(x, y, [kwargs]) f_x_num, f_y_num = lensModelNum.lens_model.alpha(x, y, [kwargs]) f_xx_num, f_xy_num, f_yx_num, f_yy_num = lensModelNum.hessian( x, y, [kwargs]) print(f_xx_num, f_xx) print(f_yy_num, f_yy) print(f_xy_num, f_xy) npt.assert_almost_equal(f_x, f_x_num, decimal=5) npt.assert_almost_equal(f_y, f_y_num, decimal=5) npt.assert_almost_equal(f_xx, f_xx_num, decimal=3) npt.assert_almost_equal(f_yy, f_yy_num, decimal=3) npt.assert_almost_equal(f_xy, f_xy_num, decimal=3) x, y = 1., 0. f_x, f_y = lensModel.lens_model.alpha(x, y, [kwargs]) f_xx, f_xy, f_yx, f_yy = lensModel.hessian(x, y, [kwargs]) f_x_num, f_y_num = lensModelNum.lens_model.alpha(x, y, [kwargs]) f_xx_num, f_xy_num, f_yx_num, f_yy_num = lensModelNum.hessian( x, y, [kwargs]) print(f_xx_num, f_xx) print(f_yy_num, f_yy) print(f_xy_num, f_xy) print(f_xx_num + f_yy_num, f_xx + f_yy) npt.assert_almost_equal(f_x, f_x_num, decimal=5) npt.assert_almost_equal(f_y, f_y_num, decimal=5) npt.assert_almost_equal(f_xx, f_xx_num, decimal=3) npt.assert_almost_equal(f_yy, f_yy_num, decimal=3) npt.assert_almost_equal(f_xy, f_xy_num, decimal=3)
def test_critical_curves(self): lens_model_list = ['SPEP'] phi, q = 1., 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi, q) kwargs_lens = [{ 'theta_E': 1., 'gamma': 2., 'e1': e1, 'e2': e2, 'center_x': 0, 'center_y': 0 }] lens_model = LensModel(lens_model_list) lensModelExtensions = LensModelExtensions(LensModel(lens_model_list)) ra_crit_list, dec_crit_list, ra_caustic_list, dec_caustic_list = lensModelExtensions.critical_curve_caustics( kwargs_lens, compute_window=5, grid_scale=0.005) # here we test whether the caustic points are in fact at high magnifications (close to infinite) # close here means above magnification of 1000000 (with matplotlib method, this limit achieved was 170) for k in range(len(ra_crit_list)): ra_crit = ra_crit_list[k] dec_crit = dec_crit_list[k] mag = lens_model.magnification(ra_crit, dec_crit, kwargs_lens) assert np.all(np.abs(mag) > 100000)
def test_hessian(self): x = 1 y = 2 f_xx, f_yy, f_xy = self.hessian.hessian(x, y, **self.kwargs_lens) npt.assert_almost_equal(f_xx, self.f_xx, decimal=5) npt.assert_almost_equal(f_yy, self.f_yy, decimal=5) npt.assert_almost_equal(f_xy, self.f_xy, decimal=5) lensModel = LensModel(['HESSIAN']) f_xy_true, f_yx_true = 0.3, 0.2 kwargs_lens = { 'f_xx': self.f_xx, 'f_yy': self.f_yy, 'f_xy': f_xy_true, 'f_yx': f_yx_true } f_xx, f_xy, f_yx, f_yy = lensModel.hessian(x, y, [kwargs_lens], diff=0.001) npt.assert_almost_equal(f_xx, self.f_xx, decimal=9) npt.assert_almost_equal(f_yy, self.f_yy, decimal=9) npt.assert_almost_equal(f_xy, f_xy_true, decimal=9) npt.assert_almost_equal(f_yx, f_yx_true, decimal=9)
def setup(self): self.zlens, self.zsource = 0.5, 1.5 epl_kwargs = {'theta_E': 0.8, 'center_x': 0.1, 'center_y': 0., 'e1': -0.2, 'e2': 0.1, 'gamma': 2.05} shear_kwargs = {'gamma1': 0.09, 'gamma2': -0.02} kwargs_macro = [epl_kwargs, shear_kwargs] self.x_image = np.array([0.65043538, -0.31109505, 0.78906059, -0.86222271]) self.y_image = np.array([-0.89067493, 0.94851787, 0.52882605, -0.25403778]) halo_list = ['SIS', 'SIS', 'SIS'] halo_z = [self.zlens - 0.1, self.zlens, self.zlens + 0.4] halo_kwargs = [{'theta_E': 0.05, 'center_x': 0.3, 'center_y': -0.9}, {'theta_E': 0.01, 'center_x': 1.3, 'center_y': -0.5}, {'theta_E': 0.02, 'center_x': -0.4, 'center_y': -0.4}] self.kwargs_epl = kwargs_macro + halo_kwargs self.zlist_epl = [self.zlens, self.zlens] + halo_z self.lens_model_list_epl = ['EPL', 'SHEAR'] + halo_list self.lensModel = LensModel(self.lens_model_list_epl, self.zlens, self.zsource, self.zlist_epl, multi_plane=True) self.param_class = PowerLawFreeShear(self.kwargs_epl)
def test_curved_arc_estimate_tan_diff(self): arc_tan_diff = CurvedArcTanDiff() lens_model_list = ['SIE'] lens = LensModel(lens_model_list=lens_model_list) arc = LensModel(lens_model_list=['CURVED_ARC_TAN_DIFF']) theta_E = 4 # here we model an off-axis ellisoid relative to the x-axis e1, e2 = 0., -0.1 x_0, y_0 = 5, 0 kwargs_lens = [{ 'theta_E': theta_E, 'e1': e1, 'e2': e2, 'center_x': 0, 'center_y': 0 }] ext = LensModelExtensions(lensModel=lens) kwargs_arc = ext.curved_arc_estimate(x_0, y_0, kwargs_lens, tan_diff=True, smoothing_3rd=0.01) theta_E_sie, e1_sie, e2_sie, kappa_ext, center_x_sis, center_y_sis = arc_tan_diff.stretch2sie_mst( **kwargs_arc) print(theta_E_sie, e1_sie, e2_sie, center_x_sis, center_y_sis) npt.assert_almost_equal(e2_sie - e2, 0, decimal=1) npt.assert_almost_equal(e1_sie, e1, decimal=3) # here we model an off-axis ellisoid relative to the y-axis e1, e2 = 0.1, 0. x_0, y_0 = 0, 5 kwargs_lens = [{ 'theta_E': theta_E, 'e1': e1, 'e2': e2, 'center_x': 0, 'center_y': 0 }] ext = LensModelExtensions(lensModel=lens) kwargs_arc = ext.curved_arc_estimate(x_0, y_0, kwargs_lens, tan_diff=True, smoothing_3rd=0.01) theta_E_sie, e1_sie, e2_sie, kappa_ext, center_x_sis, center_y_sis = arc_tan_diff.stretch2sie_mst( **kwargs_arc) print(theta_E_sie, e1_sie, e2_sie, center_x_sis, center_y_sis) npt.assert_almost_equal(e1_sie - e1, 0, decimal=1) npt.assert_almost_equal(e2_sie, e2, decimal=3) x, y = util.make_grid(numPix=100, deltapix=0.1) kappa = lens.kappa(x, y, kwargs_lens) kappa_arc = arc.kappa(x, y, [kwargs_arc])
def test_derivatives(self): Rs = 10. alpha_Rs = 10 x = np.linspace(Rs, Rs, 1000) y = np.linspace(0.2 * Rs, 2 * Rs, 1000) center_x, center_y = -1.2, 0.46 zlist_single = [0.5, 0.5] zlist_multi = [0.5, 0.8] zlist = [zlist_single, zlist_multi] numerical_alpha_class = TestClass() for i, flag in enumerate([False, True]): lensmodel = LensModel(lens_model_list=['NumericalAlpha', 'NFW'], z_source=1.5, z_lens=0.5, lens_redshift_list=zlist[i], multi_plane=flag, numerical_alpha_class=numerical_alpha_class) lensmodel_nfw = LensModel( lens_model_list=['NFW', 'NFW'], z_source=1.5, z_lens=0.5, lens_redshift_list=zlist[i], multi_plane=flag, numerical_alpha_class=numerical_alpha_class) keywords_num = [{ 'norm': alpha_Rs, 'Rs': Rs, 'center_x': center_x, 'center_y': center_y }, { 'alpha_Rs': 0.7 * alpha_Rs, 'Rs': 2 * Rs, 'center_x': center_x, 'center_y': center_y }] keywords_nfw = [{ 'alpha_Rs': alpha_Rs, 'Rs': Rs, 'center_x': center_x, 'center_y': center_y }, { 'alpha_Rs': 0.7 * alpha_Rs, 'Rs': 2 * Rs, 'center_x': center_x, 'center_y': center_y }] dx, dy = lensmodel.alpha(x, y, keywords_num) dxnfw, dynfw = lensmodel_nfw.alpha(x, y, keywords_nfw) npt.assert_almost_equal(dx, dxnfw) npt.assert_almost_equal(dy, dynfw)
def test_arrival_time(self): z_lens = 0.5 z_source = 1.5 x_image, y_image = 1., 0. lensModel = LensModel(lens_model_list=['SIS'], multi_plane=True, lens_redshift_list=[z_lens], z_source=z_source) kwargs = [{'theta_E': 1., 'center_x': 0., 'center_y': 0.}] arrival_time_mp = lensModel.arrival_time(x_image, y_image, kwargs) lensModel_sp = LensModel(lens_model_list=['SIS'], z_source=z_source, z_lens=z_lens) arrival_time_sp = lensModel_sp.arrival_time(x_image, y_image, kwargs) npt.assert_almost_equal(arrival_time_sp, arrival_time_mp, decimal=8)
def test_ray_shooting_partial(self): z_source = 1.5 lens_model_list = ['SIS', 'SIS', 'SIS'] sis1 = {'theta_E': 1., 'center_x': 0, 'center_y': 0} sis2 = {'theta_E': .2, 'center_x': 0.5, 'center_y': 0} sis3 = {'theta_E': .1, 'center_x': 0, 'center_y': 0.5} z1 = 0.1 z2 = 0.5 z3 = 0.7 redshift_list = [z1, z2, z3] kwargs_lens = [sis1, sis2, sis3] lensModel = MultiPlane(z_source=z_source, lens_model_list=lens_model_list, lens_redshift_list=redshift_list) lensModel_2 = LensModel(z_source=z_source, lens_model_list=lens_model_list, lens_redshift_list=redshift_list, multi_plane=True) multiplane_2 = lensModel_2.lens_model intermediate_index = 1 theta_x, theta_y = 1., 1. Tzsrc = lensModel._multi_plane_base._cosmo_bkg.T_xy(0, z_source) z_intermediate = lensModel._multi_plane_base._lens_redshift_list[intermediate_index] for lensmodel_class in [lensModel, multiplane_2]: x_out, y_out, alpha_x_out, alpha_y_out = lensmodel_class.ray_shooting_partial(x=0, y=0, alpha_x=theta_x, alpha_y=theta_y, z_start=0, z_stop=z_intermediate, kwargs_lens=kwargs_lens) x_out_full_0 = x_out y_out_full_0 = y_out x_out, y_out, alpha_x_out, alpha_y_out = lensmodel_class.ray_shooting_partial(x=x_out, y=y_out, alpha_x=alpha_x_out, alpha_y=alpha_y_out, z_start=z_intermediate, z_stop=z_source, kwargs_lens=kwargs_lens) beta_x, beta_y = lensModel.co_moving2angle_source(x_out, y_out) beta_x_true, beta_y_true = lensmodel_class.ray_shooting(theta_x, theta_y, kwargs_lens) npt.assert_almost_equal(beta_x, beta_x_true, decimal=8) npt.assert_almost_equal(beta_y, beta_y_true, decimal=8) T_ij_start = lensModel._multi_plane_base._cosmo_bkg.T_xy(z_observer=0, z_source=0.1) T_ij_end = lensModel._multi_plane_base._cosmo_bkg.T_xy(z_observer=0.7, z_source=1.5) x_out, y_out, alpha_x_out, alpha_y_out = lensmodel_class.ray_shooting_partial(x=0, y=0, alpha_x=theta_x, alpha_y=theta_y, z_start=0, z_stop=z_source, kwargs_lens=kwargs_lens, T_ij_start=T_ij_start, T_ij_end=T_ij_end) beta_x, beta_y = x_out/Tzsrc, y_out/Tzsrc npt.assert_almost_equal(beta_x, beta_x_true, decimal=8) npt.assert_almost_equal(beta_y, beta_y_true, decimal=8)
def test_setup(self): cosmo = None lens_model = LensModel(['EPL']) grid_radius_arcsec = None grid_resolution = None source_fwhm_parsec = 30. source_light_model = 'SINGLE_GAUSSIAN' z_source = 2. source_x, source_y = 0., 0. dx, dy, amp_scale, size_scale = None, None, None, None gridx, gridy, source_model, kwargs_source, grid_resolution, grid_radius_arcsec = setup_mag_finite(cosmo, lens_model, grid_radius_arcsec, grid_resolution, source_fwhm_parsec, source_light_model, z_source, source_x, source_y, dx, dy, amp_scale, size_scale) npt.assert_equal(True, len(source_model.func_list)==1) npt.assert_equal(True, grid_resolution is not None) npt.assert_equal(True, grid_radius_arcsec is not None) grid_resolution = 0.001 grid_radius_arcsec = 0.05 dx, dy, amp_scale, size_scale = 0., 0.1, 1., 1. source_light_model = 'DOUBLE_GAUSSIAN' gridx, gridy, source_model, kwargs_source, grid_resolution, grid_radius_arcsec = setup_mag_finite(cosmo, lens_model, grid_radius_arcsec, grid_resolution, source_fwhm_parsec, source_light_model, z_source, source_x, source_y, dx, dy, amp_scale, size_scale) npt.assert_equal(True, len(source_model.func_list) == 2) npt.assert_equal(kwargs_source[1]['center_y'], kwargs_source[0]['center_y'] + dy) npt.assert_equal(kwargs_source[1]['center_x'], kwargs_source[0]['center_x'] + dx) npt.assert_equal(True, grid_resolution == 0.001) npt.assert_equal(True, grid_radius_arcsec == 0.05) source_light_model = 'trash' npt.assert_raises(Exception, setup_mag_finite, cosmo, lens_model, grid_radius_arcsec, grid_resolution, source_fwhm_parsec, source_light_model, z_source, source_x, source_y, dx, dy, amp_scale, size_scale )
def test__re_order_split(self): lensModel = LensModel(lens_model_list=['SIS', 'SIS'], multi_plane=True, lens_redshift_list=[0.5, 0.4], z_source=3) mapping = Image2SourceMapping( lensModel, LightModel(light_model_list=['SERSIC', 'SHAPELETS'], deflection_scaling_list=None, source_redshift_list=[2, 0.3])) n_list = [1, 2] response = np.zeros((3, 3)) response[1:] = 1 response_reshuffled = mapping._re_order_split(response, n_list) assert response_reshuffled[0, 0] == 1 assert response_reshuffled[1, 0] == 0
def test_zoom_source(self): lens_model_list = ['SIE', 'SHEAR'] lensModel = LensModel(lens_model_list=lens_model_list) lensModelExtensions = LensModelExtensions(lensModel=lensModel) lensEquationSolver = LensEquationSolver(lensModel=lensModel) x_source, y_source = 0.02, 0.01 kwargs_lens = [{'theta_E': 1, 'e1': 0.1, 'e2': 0.1, 'center_x': 0, 'center_y': 0}, {'gamma1': 0.05, 'gamma2': -0.03}] x_img, y_img = lensEquationSolver.image_position_from_source(kwargs_lens=kwargs_lens, sourcePos_x=x_source, sourcePos_y=y_source) image = lensModelExtensions.zoom_source(x_img[0], y_img[0], kwargs_lens, source_sigma=0.003, window_size=0.1, grid_number=100, shape="GAUSSIAN") assert len(image) == 100
def test_mass_fraction_within_radius(self): center_x, center_y = 0.5, -1 theta_E = 1.1 kwargs_lens = [{ 'theta_E': 1.1, 'center_x': center_x, 'center_y': center_y }] lensModel = LensModel(**{'lens_model_list': ['SIS']}) lensAnalysis = LensProfileAnalysis(lens_model=lensModel) kappa_mean_list = lensAnalysis.mass_fraction_within_radius(kwargs_lens, center_x, center_y, theta_E, numPix=100) npt.assert_almost_equal(kappa_mean_list[0], 1, 2)
def test_position_convention(self): lens_model_list = ['SIS', 'SIS','SIS', 'SIS'] redshift_list = [0.5, 0.5, 0.9, 0.6] kwargs_lens = [{'theta_E': 1, 'center_x':0, 'center_y': 0}, {'theta_E': 0.4, 'center_x': 0, 'center_y': 0.2}, {'theta_E': 1, 'center_x': 1.8, 'center_y': -0.4}, {'theta_E': 0.41, 'center_x': 1., 'center_y': 0.7}] index_list = [[2,3], [3,2]] # compute the physical position given lensed position, and check that lensing computations # using the two different conventions and sets of kwargs agree for index in index_list: lensModel_observed = LensModel(lens_model_list=lens_model_list, multi_plane=True, observed_convention_index=index, z_source=1.5, lens_redshift_list=redshift_list) lensModel_physical = LensModel(lens_model_list=lens_model_list, multi_plane=True, z_source=1.5, lens_redshift_list=redshift_list) multi = lensModel_observed.lens_model._multi_plane_base lensed, phys = LensedLocation(multi, index), PhysicalLocation() kwargs_lens_physical = lensModel_observed.lens_model._convention(kwargs_lens) kwargs_phys, kwargs_lensed = phys(kwargs_lens), lensed(kwargs_lens) for j, lensed_kwargs in enumerate(kwargs_lensed): for ki in lensed_kwargs.keys(): assert lensed_kwargs[ki] == kwargs_lens_physical[j][ki] assert kwargs_phys[j][ki] == kwargs_lens[j][ki] fxx, fyy, fxy, fyx = lensModel_observed.hessian(0.5, 0.5, kwargs_lens) fxx2, fyy2, fxy2, fyx2 = lensModel_physical.hessian(0.5, 0.5, kwargs_lens_physical) npt.assert_almost_equal(fxx, fxx2) npt.assert_almost_equal(fxy, fxy2) betax1, betay1 = lensModel_observed.ray_shooting(0.5, 0.5, kwargs_lens) betax2, betay2 = lensModel_physical.ray_shooting(0.5, 0.5, kwargs_lens_physical) npt.assert_almost_equal(betax1, betax2) npt.assert_almost_equal(betay1, betay2)
def test_elliptical_ray_trace(self): lens_model_list = ['SPEMD','SHEAR'] kwargs_lens = [{'theta_E': 1., 'gamma': 2., 'e1': 0.02, 'e2': -0.09, 'center_x': 0, 'center_y': 0},{'e1':0.01,'e2':0.03}] extension = LensModelExtensions(LensModel(lens_model_list)) x_image, y_image = [ 0.56153533,-0.78067875,-0.72551184,0.75664112],[-0.74722528,0.52491177,-0.72799235,0.78503659] mag_square_grid = extension.magnification_finite(x_image,y_image,kwargs_lens,source_sigma=0.001, grid_number=200,window_size=0.1) mag_polar_grid = extension.magnification_finite(x_image,y_image,kwargs_lens,source_sigma=0.001, grid_number=200,window_size=0.1,polar_grid=True) npt.assert_almost_equal(mag_polar_grid,mag_square_grid,decimal=5)
def test__logL(self): lensModel = LensModel(lens_model_list=[]) flux_ratios_init = np.array([1., 1., 1.]) flux_ratio_errors = np.array([1., 1., 1.]) flux_likelihood = FluxRatioLikelihood(lens_model_class=lensModel, flux_ratios=flux_ratios_init, flux_ratio_errors=flux_ratio_errors) flux_ratios = np.array([0, 1, np.nan]) logL = flux_likelihood._logL(flux_ratios) assert logL == -10 ** 15 flux_likelihood = FluxRatioLikelihood(lens_model_class=lensModel, flux_ratios=flux_ratios_init, flux_ratio_errors=np.array([0., 1., 1.])) flux_ratios = np.array([1., 1., 1.]) logL = flux_likelihood._logL(flux_ratios) assert logL == -10 ** 15
def __init__(self, lens_model_list=[], z_lens=None, z_source=None, lens_redshift_list=None, multi_plane=False, source_light_model_list=[], lens_light_model_list=[], point_source_model_list=[], source_redshift_list=None, cosmo=None): """ :param lens_model_list: list of strings with lens model names :param z_lens: redshift of the deflector (only considered when operating in single plane mode). Is only needed for specific functions that require a cosmology. :param z_source: redshift of the source: Needed in multi_plane option only, not required for the core functionalities in the single plane mode. :param lens_redshift_list: list of deflector redshift (corresponding to the lens model list), only applicable in multi_plane mode. :param source_light_model_list: list of strings with source light model names (lensed light profiles) :param lens_light_model_list: list of strings with lens light model names (not lensed light profiles) :param point_source_model_list: list of strings with point source model names :param source_redshift_list: list of redshifts of the source profiles (optional) :param cosmo: instance of the astropy cosmology class. If not specified, uses the default cosmology. """ self._lens_model_class = LensModel( lens_model_list=lens_model_list, z_source=z_source, z_lens=z_lens, lens_redshift_list=lens_redshift_list, multi_plane=multi_plane, cosmo=cosmo) self._source_model_class = LightModel( light_model_list=source_light_model_list, source_redshift_list=source_redshift_list) self._lens_light_model_class = LightModel( light_model_list=lens_light_model_list) fixed_magnification = [False] * len(point_source_model_list) for i, ps_type in enumerate(point_source_model_list): if ps_type == 'SOURCE_POSITION': fixed_magnification[i] = True self._point_source_model_class = PointSource( point_source_type_list=point_source_model_list, lensModel=self._lens_model_class, fixed_magnification_list=fixed_magnification)
def test_hessian(self): lens = LensModel(lens_model_list=['CURVED_ARC']) center_x, center_y = 0, 0 tangential_stretch = 10 radial_stretch = 1 kwargs_lens = [ {'tangential_stretch': tangential_stretch, 'radial_stretch': radial_stretch, 'r_curvature': 10.5, 'direction': 0., 'center_x': center_x, 'center_y': center_y}] mag = lens.magnification(center_x, center_y, kwargs=kwargs_lens) npt.assert_almost_equal(mag, tangential_stretch * radial_stretch, decimal=8) center_x, center_y = 2, 3 tangential_stretch = 10 radial_stretch = 1 kwargs_lens = [ {'tangential_stretch': tangential_stretch, 'radial_stretch': radial_stretch, 'r_curvature': 10.5, 'direction': 0., 'center_x': center_x, 'center_y': center_y}] mag = lens.magnification(center_x, center_y, kwargs=kwargs_lens) npt.assert_almost_equal(mag, tangential_stretch * radial_stretch, decimal=8) center_x, center_y = 0, 0 tangential_stretch = 3 radial_stretch = -1 kwargs_lens = [ {'tangential_stretch': tangential_stretch, 'radial_stretch': radial_stretch, 'r_curvature': 10.5, 'direction': 0., 'center_x': center_x, 'center_y': center_y}] mag = lens.magnification(center_x, center_y, kwargs=kwargs_lens) npt.assert_almost_equal(mag, tangential_stretch * radial_stretch, decimal=8) center_x, center_y = 0, 0 tangential_stretch = -3 radial_stretch = -1 kwargs_lens = [ {'tangential_stretch': tangential_stretch, 'radial_stretch': radial_stretch, 'r_curvature': 10.5, 'direction': 0., 'center_x': center_x, 'center_y': center_y}] mag = lens.magnification(center_x, center_y, kwargs=kwargs_lens) npt.assert_almost_equal(mag, tangential_stretch * radial_stretch, decimal=8) center_x, center_y = 0, 0 tangential_stretch = 10.4 radial_stretch = 0.6 kwargs_lens = [ {'tangential_stretch': tangential_stretch, 'radial_stretch': radial_stretch, 'r_curvature': 10.5, 'direction': 0., 'center_x': center_x, 'center_y': center_y}] mag = lens.magnification(center_x, center_y, kwargs=kwargs_lens) npt.assert_almost_equal(mag, tangential_stretch * radial_stretch, decimal=8)
def test_check_additional_images(self): point_source_class = PointSource( point_source_type_list=['LENSED_POSITION'], additional_images_list=[True], lensModel=LensModel(lens_model_list=['SIE'])) likelihood = PositionLikelihood(point_source_class) kwargs_ps = [{'ra_image': self._x_pos, 'dec_image': self._y_pos}] bool = likelihood.check_additional_images(kwargs_ps, self._kwargs_lens) assert bool is False kwargs_ps = [{ 'ra_image': self._x_pos[1:], 'dec_image': self._y_pos[1:] }] bool = likelihood.check_additional_images(kwargs_ps, self._kwargs_lens) assert bool is True
def test_solver_shapelets(self): lens_model_list = ['SHAPELETS_CART', 'SPEP'] lensModel = LensModel(lens_model_list) solver = Solver4Point(lensModel) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.1 sourcePos_y = -0. deltapix = 0.05 numPix = 150 coeffs = np.array([0, 0.1, 0.1, 0, 0, -0.1]) kwargs_lens = [{ 'beta': 1., 'coeffs': coeffs, 'center_x': 0., 'center_y': 0. }, { 'theta_E': 1., 'gamma': 2, 'q': 0.8, 'phi_G': 0.5, 'center_x': 0, 'center_y': 0 }] x_pos, y_pos = lensEquationSolver.findBrightImage( sourcePos_x, sourcePos_y, kwargs_lens, numImages=4, min_distance=deltapix, search_window=numPix * deltapix) print(x_pos, y_pos) kwargs_lens_init = [{ 'beta': 1, 'coeffs': np.zeros_like(coeffs), 'center_x': 0., 'center_y': 0 }, kwargs_lens[1]] kwargs_lens_new, accuracy = solver.constraint_lensmodel( x_pos, y_pos, kwargs_lens_init) npt.assert_almost_equal(kwargs_lens_new[0]['beta'], kwargs_lens[0]['beta'], decimal=3) coeffs_new = kwargs_lens_new[0]['coeffs'] for i in range(len(coeffs)): npt.assert_almost_equal(coeffs_new[i], coeffs[i], decimal=3)
def test_source_position_plot(self): from lenstronomy.PointSource.point_source import PointSource from lenstronomy.LensModel.lens_model import LensModel lensModel = LensModel(lens_model_list=['SIS']) ps = PointSource(point_source_type_list=['UNLENSED', 'LENSED_POSITION', 'SOURCE_POSITION'], lensModel=lensModel) kwargs_lens = [{'theta_E': 1., 'center_x': 0, 'center_y': 0}] kwargs_ps = [{'ra_image': [1., 1.], 'dec_image': [0, 1], 'point_amp': [1, 1]}, {'ra_image': [1.], 'dec_image': [1.], 'point_amp': [10]}, {'ra_source': 0.1, 'dec_source': 0, 'point_amp': 1.}] ra_source, dec_source = ps.source_position(kwargs_ps, kwargs_lens) from lenstronomy.Data.coord_transforms import Coordinates coords_source = Coordinates(transform_pix2angle=np.array([[1, 0], [0, 1]])* 0.1, ra_at_xy_0=-2, dec_at_xy_0=-2) f, ax = plt.subplots(1, 1, figsize=(4, 4)) plot_util.source_position_plot(ax, coords_source, ra_source, dec_source) plt.close()
def test_tangential_average(self): lens_model_list = ['SIS'] lensModel = LensModel(lens_model_list=lens_model_list) lensModelExtensions = LensModelExtensions(lensModel=lensModel) tang_stretch_ave = lensModelExtensions.tangential_average( x=1.1, y=0, kwargs_lens=[{ 'theta_E': 1, 'center_x': 0, 'center_y': 0 }], dr=1, smoothing=None, num_average=9) npt.assert_almost_equal(tang_stretch_ave, -2.525501464097973, decimal=6)
def cone_instance(self, z_source, cosmo, multi_plane=True, kwargs_interp=None): """ :param z_source: redshift to where lensing quantities are computed :param cosmo: astropy.cosmology class :param multi_plane: boolean, if True, computes multi-plane ray-tracing :param kwargs_interp: interpolation keyword arguments specifying the numerics. See description in the Interpolate() class. Only applicable for 'INTERPOL' and 'INTERPOL_SCALED' models. :return: LensModel instance, keyword argument list of lens model """ lens_model = LensModel(lens_model_list=['INTERPOL'] * len(self._mass_map_list), lens_redshift_list=self._redshift_list, multi_plane=multi_plane, z_source_convention=z_source, cosmo=cosmo, z_source=z_source, kwargs_interp=kwargs_interp) kwargs_lens = [] for mass_slice in self._mass_slice_list: kwargs_lens.append(mass_slice.interpol_instance(z_source, cosmo)) return lens_model, kwargs_lens
def test_curved_arc_estimate(self): lens_model_list = ['SPP'] lens = LensModel(lens_model_list=lens_model_list) arc = LensModel(lens_model_list=['CURVED_ARC_SPP']) theta_E = 4 gamma = 2. kwargs_lens = [{ 'theta_E': theta_E, 'gamma': gamma, 'center_x': 0, 'center_y': 0 }] ext = LensModelExtensions(lensModel=lens) x_0, y_0 = 5, 0 kwargs_arc = ext.curved_arc_estimate(x_0, y_0, kwargs_lens) theta_E_arc, gamma_arc, center_x_spp_arc, center_y_spp_arc = CurvedArcSPP.stretch2spp( **kwargs_arc) npt.assert_almost_equal(theta_E_arc, theta_E, decimal=4) npt.assert_almost_equal(gamma_arc, gamma, decimal=3) npt.assert_almost_equal(center_x_spp_arc, 0, decimal=3) npt.assert_almost_equal(center_y_spp_arc, 0, decimal=3) x, y = util.make_grid(numPix=10, deltapix=1) alpha_x, alpha_y = lens.alpha(x, y, kwargs_lens) alpha0_x, alpha0_y = lens.alpha(x_0, y_0, kwargs_lens) alpha_x_arc, alpha_y_arc = arc.alpha(x, y, [kwargs_arc]) npt.assert_almost_equal(alpha_x_arc, alpha_x - alpha0_x, decimal=3) npt.assert_almost_equal(alpha_y_arc, alpha_y - alpha0_y, decimal=3) x_0, y_0 = 0., 3 kwargs_arc = ext.curved_arc_estimate(x_0, y_0, kwargs_lens) theta_E_arc, gamma_arc, center_x_spp_arc, center_y_spp_arc = CurvedArcSPP.stretch2spp( **kwargs_arc) print(kwargs_arc) print(theta_E_arc, gamma_arc, center_x_spp_arc, center_y_spp_arc) npt.assert_almost_equal(theta_E_arc, theta_E, decimal=4) npt.assert_almost_equal(gamma_arc, gamma, decimal=3) npt.assert_almost_equal(center_x_spp_arc, 0, decimal=3) npt.assert_almost_equal(center_y_spp_arc, 0, decimal=3) x_0, y_0 = -2, -3 kwargs_arc = ext.curved_arc_estimate(x_0, y_0, kwargs_lens) theta_E_arc, gamma_arc, center_x_spp_arc, center_y_spp_arc = CurvedArcSPP.stretch2spp( **kwargs_arc) npt.assert_almost_equal(theta_E_arc, theta_E, decimal=4) npt.assert_almost_equal(gamma_arc, gamma, decimal=3) npt.assert_almost_equal(center_x_spp_arc, 0, decimal=3) npt.assert_almost_equal(center_y_spp_arc, 0, decimal=3)
def test_critical_curves_tiling(self): lens_model_list = ['SPEP'] phi, q = 1., 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi, q) kwargs_lens = [{ 'theta_E': 1., 'gamma': 2., 'e1': e1, 'e2': e2, 'center_x': 0, 'center_y': 0 }] lensModel = LensModelExtensions(LensModel(lens_model_list)) ra_crit, dec_crit = lensModel.critical_curve_tiling(kwargs_lens, compute_window=5, start_scale=0.01, max_order=10) npt.assert_almost_equal(ra_crit[0], -0.5355208333333333, decimal=5)
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)
def test_point_source_rendering(self): # initialize data numPix = 100 deltaPix = 0.05 kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exposure_time=1, background_rms=1) data_class = ImageData(**kwargs_data) kernel = np.zeros((5, 5)) kernel[2, 2] = 1 kwargs_psf = {'kernel_point_source': kernel, 'psf_type': 'PIXEL', 'psf_error_map': np.ones_like(kernel) * 0.001} psf_class = PSF(**kwargs_psf) lens_model_class = LensModel(['SPEP']) source_model_class = LightModel([]) lens_light_model_class = LightModel([]) kwargs_numerics = {'supersampling_factor': 2, 'supersampling_convolution': True, 'point_source_supersampling_factor': 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)}] image = makeImage.image(kwargs_lens_init, kwargs_source={}, kwargs_lens_light={}, kwargs_ps=kwargs_else) #print(np.shape(model), 'test') #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)}] image = 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)
def test_point_source(self): pointSource = PointSource(point_source_type_list=['SOURCE_POSITION'], fixed_magnification_list=[True]) kwargs_ps = [{'source_amp': 1000, 'ra_source': 0.1, 'dec_source': 0.1}] lensModel = LensModel(lens_model_list=['SIS']) kwargs_lens = [{'theta_E': 1, 'center_x': 0, 'center_y': 0}] numPix = 64 deltaPix = 0.13 kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exposure_time=1, background_rms=1) data_class = ImageData(**kwargs_data) psf_type = "GAUSSIAN" fwhm = 0.9 kwargs_psf = {'psf_type': psf_type, 'fwhm': fwhm} psf_class = PSF(**kwargs_psf) imageModel = ImageModel(data_class=data_class, psf_class=psf_class, lens_model_class=lensModel, point_source_class=pointSource) image = imageModel.image(kwargs_lens=kwargs_lens, kwargs_ps=kwargs_ps) assert np.sum(image) > 0
def setup(self): self.num_pix = 25 # cutout pixel size self.subgrid_res_source = 2 delta_pix = 0.32 _, _, ra_at_xy_0, dec_at_xy_0, _, _, Mpix2coord, _ \ = l_util.make_grid_with_coordtransform(numPix=self.num_pix, deltapix=delta_pix, subgrid_res=1, inverse=False, left_lower=False) kwargs_data = { 'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0, 'transform_pix2angle': Mpix2coord, 'image_data': np.zeros((self.num_pix, self.num_pix)) } data_class = ImageData(**kwargs_data) numerics_image = NumericsSubFrame(data_class, PSF('NONE')) numerics_source = NumericsSubFrame( data_class, PSF('NONE'), supersampling_factor=self.subgrid_res_source) self.source_plane = SizeablePlaneGrid(numerics_source.grid_class, verbose=True) # create a mask mimicking the real case of lensing operation lens_model_class = LensModel(['SIE']) kwargs_lens = [{ 'theta_E': 1.5, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 }] lensing_op = LensingOperator(lens_model_class, numerics_image.grid_class, numerics_source.grid_class, self.num_pix, self.subgrid_res_source) lensing_op.update_mapping(kwargs_lens) unit_image = np.ones((self.num_pix, self.num_pix)) mask_image = np.zeros((self.num_pix, self.num_pix)) mask_image[2:-2, 2:-2] = 1 # some binary image that mask out borders self.unit_image_mapped = lensing_op.image2source_2d(unit_image, no_flux_norm=False) self.mask_mapped = lensing_op.image2source_2d(mask_image)
def cone_instance(self, z_source, cosmo, multi_plane=True): """ :param z_source: redshift to where lensing quantities are computed :param cosmo: astropy.cosmology class :param multi_plane: boolean, if True, computes multi-plane ray-tracing :return: LensModel instance, keyword argument list of lens model """ lens_model = LensModel(lens_model_list=['INTERPOL'] * len(self._mass_map_list), lens_redshift_list=self._redshift_list, multi_plane=multi_plane, z_source_convention=z_source, cosmo=cosmo, z_source=z_source) kwargs_lens = [] for mass_slice in self._mass_slice_list: kwargs_lens.append(mass_slice.interpol_instance(z_source, cosmo)) return lens_model, kwargs_lens