def test_solver_spep(self): lens_model_list = ['SPEP'] lensModel = LensModel(lens_model_list) solver = Solver4Point(lensModel) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.1 sourcePos_y = -0.1 deltapix = 0.05 numPix = 150 gamma = 1.9 phi_G, q = 0.5, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_lens = [{'theta_E': 1., 'gamma': gamma, 'e1': e1, 'e2': e2, 'center_x': 0.1, 'center_y': -0.1}] x_pos, y_pos = lensEquationSolver.findBrightImage(sourcePos_x, sourcePos_y, kwargs_lens, numImages=4, min_distance=deltapix, search_window=numPix*deltapix) phi_G, q = 1.5, 0.9 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_lens_init = [{'theta_E': 1.3, 'gamma': gamma, 'e1': e1, 'e2': e2, 'center_x': 0., 'center_y': 0}] kwargs_lens_new, accuracy = solver.constraint_lensmodel(x_pos, y_pos, kwargs_lens_init) npt.assert_almost_equal(kwargs_lens_new[0]['theta_E'], kwargs_lens[0]['theta_E'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['e1'], kwargs_lens[0]['e1'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['e2'], kwargs_lens[0]['e2'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['center_x'], kwargs_lens[0]['center_x'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['center_y'], kwargs_lens[0]['center_y'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['theta_E'], 1., decimal=3) lensModel = LensModel(lens_model_list=lens_model_list) x_source_new, y_source_new = lensModel.ray_shooting(x_pos, y_pos, kwargs_lens_new) dist = np.sqrt((x_source_new - x_source_new[0]) ** 2 + (y_source_new - y_source_new[0]) ** 2) print(dist) assert np.max(dist) < 0.000001
def test_foreground_shear(self): lens_model_list = ['SPEP', 'FOREGROUND_SHEAR'] lensModel = LensModel(lens_model_list) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.1 sourcePos_y = -0.1 min_distance = 0.05 search_window = 10 gamma = 1.9 kwargs_lens = [{ 'theta_E': 1., 'gamma': gamma, 'e1': 0.2, 'e2': -0.03, 'center_x': 0.1, 'center_y': -0.1 }, { 'e1': 0.01, 'e2': -0.05 }] x_pos, y_pos = lensEquationSolver.image_position_from_source( sourcePos_x, sourcePos_y, kwargs_lens, min_distance=min_distance, search_window=search_window, precision_limit=10**(-10), num_iter_max=10) source_x, source_y = lensModel.ray_shooting(x_pos, y_pos, kwargs_lens) npt.assert_almost_equal(sourcePos_x, source_x, decimal=10)
def test_plot_quasar_images(self): lens_model_list = ['EPL', 'SHEAR'] z_source = 1.5 kwargs_lens = [{ 'theta_E': 1., 'gamma': 2., 'e1': 0.02, 'e2': -0.09, 'center_x': 0, 'center_y': 0 }, { 'gamma1': 0.01, 'gamma2': 0.03 }] lensmodel = LensModel(lens_model_list) solver = LensEquationSolver(lensmodel) source_x, source_y = 0.07, 0.03 x_image, y_image = solver.findBrightImage(source_x, source_y, kwargs_lens) source_fwhm_parsec = 40. plot_quasar_images(lensmodel, x_image, y_image, source_x, source_y, kwargs_lens, source_fwhm_parsec, z_source) plt.close()
def test_constraint_lensmodel(self): lens_model_list = ['SPEP', 'SIS'] lensModel = LensModel(lens_model_list) solver = Solver(solver_type='PROFILE', lensModel=lensModel, num_images=4) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.1 sourcePos_y = -0.1 deltapix = 0.05 numPix = 150 gamma = 1.9 kwargs_lens = [{'theta_E': 1., 'gamma': gamma,'q': 0.8, 'phi_G': 0.5, 'center_x': 0.1, 'center_y': -0.1}, {'theta_E': 0.1, 'center_x': 0.5, 'center_y': 0}] 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,'q': 0.9, 'phi_G': 1.5, 'center_x': 0., 'center_y': 0}, {'theta_E': 0.1, 'center_x': 0.5, 'center_y': 0}] kwargs_lens_new, accuracy = solver.constraint_lensmodel(x_pos, y_pos, kwargs_lens_init) npt.assert_almost_equal(kwargs_lens_new[0]['theta_E'], kwargs_lens[0]['theta_E'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['q'], kwargs_lens[0]['q'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['phi_G'], kwargs_lens[0]['phi_G'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['center_x'], kwargs_lens[0]['center_x'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['center_y'], kwargs_lens[0]['center_y'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['theta_E'], 1., decimal=3) lensModel = LensModel(lens_model_list=lens_model_list) x_source_new, y_source_new = lensModel.ray_shooting(x_pos, y_pos, kwargs_lens_new) dist = np.sqrt((x_source_new - x_source_new[0]) ** 2 + (y_source_new - y_source_new[0]) ** 2) assert np.max(dist) < 0.000001 kwargs_ps4 = [{'ra_image': x_pos, 'dec_image': y_pos}] kwargs_lens_new = solver.update_solver(kwargs_lens_init, kwargs_ps4) npt.assert_almost_equal(kwargs_lens_new[0]['theta_E'], kwargs_lens[0]['theta_E'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['q'], kwargs_lens[0]['q'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['phi_G'], kwargs_lens[0]['phi_G'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['center_x'], kwargs_lens[0]['center_x'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['center_y'], kwargs_lens[0]['center_y'], decimal=3)
def test_analytical_sie(self): sourcePos_x = 0.03 sourcePos_y = 0.0 lensModel = LensModel(['SIE']) lensEquationSolver = LensEquationSolver(lensModel) kwargs_lens = [ { 'theta_E': 1., 'center_x': 0.0, 'center_y': 0.0, 'e1': 0.5, 'e2': 0.05 }, ] x_pos, y_pos = lensEquationSolver.image_position_from_source( sourcePos_x, sourcePos_y, kwargs_lens, solver='analytical', magnification_limit=1e-3) source_x, source_y = lensModel.ray_shooting(x_pos, y_pos, kwargs_lens) assert len(source_x) == len(source_y) == 4 npt.assert_almost_equal(sourcePos_x, source_x, decimal=10) npt.assert_almost_equal(sourcePos_y, source_y, decimal=10)
def test_example(self): lens_model_list = ['SPEP', 'SHEAR'] lensModel = LensModel(lens_model_list) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.03 sourcePos_y = 0.0 min_distance = 0.05 search_window = 10 gamma = 2. e1, e2 = -0.04, -0.1 kwargs_shear = {'e1': e1, 'e2': e2} # shear values to the source plane kwargs_spemd = {'theta_E': 1., 'gamma': gamma, 'center_x': 0.0, 'center_y': 0.0, 'e1': 0.01, 'e2': 0.05} # parameters of the deflector lens model kwargs_lens = [kwargs_spemd, kwargs_shear] x_pos, y_pos = lensEquationSolver.image_position_from_source(sourcePos_x, sourcePos_y, kwargs_lens, min_distance=min_distance, search_window=search_window, precision_limit=10 ** (-10), num_iter_max=10, arrival_time_sort=True) x_pos_stoch, y_pos_stoch = lensEquationSolver.image_position_stochastic(sourcePos_x, sourcePos_y, kwargs_lens, search_window=search_window, precision_limit=10 ** (-10), arrival_time_sort=True, x_center=0, y_center=0, num_random=100, verbose=False ) assert len(x_pos) == 4 assert len(x_pos_stoch) == 4 npt.assert_almost_equal(x_pos, x_pos_stoch, decimal=5)
def test_central_image(self): lens_model_list = ['SPEP', 'SIS', 'SHEAR'] kwargs_spep = { 'theta_E': 1, 'gamma': 2, 'e1': 0.2, 'e2': -0.03, 'center_x': 0, 'center_y': 0 } kwargs_sis = {'theta_E': 1, 'center_x': 1.5, 'center_y': 0} kwargs_shear = {'e1': 0.01, 'e2': 0} kwargs_lens = [kwargs_spep, kwargs_sis, kwargs_shear] lensModel = LensModel(lens_model_list) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.1 sourcePos_y = -0.1 min_distance = 0.05 search_window = 10 x_pos, y_pos = lensEquationSolver.image_position_from_source( sourcePos_x, sourcePos_y, kwargs_lens, min_distance=min_distance, search_window=search_window, precision_limit=10**(-10), num_iter_max=10) source_x, source_y = lensModel.ray_shooting(x_pos, y_pos, kwargs_lens) npt.assert_almost_equal(sourcePos_x, source_x, decimal=10) print(x_pos, y_pos) assert len(x_pos) == 4
def test_nfw(self): lens_model_list = ['NFW_ELLIPSE', 'SIS'] lensModel = LensModel(lens_model_list) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.1 sourcePos_y = -0.1 min_distance = 0.05 search_window = 10 Rs = 4. kwargs_lens = [{ 'alpha_Rs': 1., 'Rs': Rs, 'e1': 0.2, 'e2': -0.03, 'center_x': 0.1, 'center_y': -0.1 }, { 'theta_E': 1, 'center_x': 0, 'center_y': 0 }] x_pos, y_pos = lensEquationSolver.image_position_from_source( sourcePos_x, sourcePos_y, kwargs_lens, min_distance=min_distance, search_window=search_window, precision_limit=10**(-10), num_iter_max=10, verbose=True, magnification_limit=1) source_x, source_y = lensModel.ray_shooting(x_pos, y_pos, kwargs_lens) npt.assert_almost_equal(sourcePos_x, source_x, decimal=10)
def solve_leq(self, xsrc, ysrc, lensmodel, lens_model_params, brightimg, precision_lim=10**(-10), nitermax=10): lensEquationSolver = LensEquationSolver(lensModel=lensmodel) if brightimg: x_image, y_image = lensEquationSolver.findBrightImage( xsrc, ysrc, lens_model_params, arrival_time_sort=False, min_distance=0.02, search_window=3.5, precision_limit=precision_lim, num_iter_max=nitermax) else: x_image, y_image = lensEquationSolver.image_position_from_source( kwargs_lens=lens_model_params, sourcePos_x=xsrc, sourcePos_y=ysrc, min_distance=self.min_distance, search_window=self.search_window, precision_limit=self.precision_limit, num_iter_max=self.num_iter_max, arrival_time_sort=False) return x_image, y_image
def test_multiplane(self): lens_model_list = ['SPEP', 'SIS'] lensModel = LensModel(lens_model_list, z_source=1., lens_redshift_list=[0.5, 0.3], multi_plane=True) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.1 sourcePos_y = -0.1 min_distance = 0.05 search_window = 10 gamma = 1.9 kwargs_lens = [{ 'theta_E': 1., 'gamma': gamma, 'e1': 0.2, 'e2': -0.03, 'center_x': 0.1, 'center_y': -0.1 }, { 'theta_E': 0.1, 'center_x': 0.5, 'center_y': 0 }] x_pos, y_pos = lensEquationSolver.image_position_from_source( sourcePos_x, sourcePos_y, kwargs_lens, min_distance=min_distance, search_window=search_window, precision_limit=10**(-10), num_iter_max=10) source_x, source_y = lensModel.ray_shooting(x_pos, y_pos, kwargs_lens) npt.assert_almost_equal(sourcePos_x, source_x, decimal=10)
def test_solver_simplified(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.9 gamma_ext = 0.05 psi_ext = 0.4 #e1, e2 = param_util.phi_gamma_ellipticity(phi=psi_ext, gamma=gamma_ext) kwargs_lens = [{'theta_E': 1., 'gamma': gamma, 'e1': 0.1, 'e2': -0.1, 'center_x': 0.1, 'center_y': -0.1}, {'gamma_ext': gamma_ext, 'psi_ext': psi_ext}] x_pos, y_pos = lensEquationSolver.findBrightImage(sourcePos_x, sourcePos_y, kwargs_lens, numImages=4, min_distance=deltapix, search_window=numPix * deltapix) e1_new, e2_new = param_util.phi_gamma_ellipticity(phi=0., gamma=gamma_ext+0.1) kwargs_lens_init = [{'theta_E': 1.3, 'gamma': gamma, 'e1': 0., 'e2': 0., 'center_x': 0., 'center_y': 0}, {'gamma_ext': gamma_ext + 0.1, 'psi_ext': 0}] 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)
def test_spep_sis(self): lens_model_list = ['SPEP', 'SIS'] lensModel = LensModel(lens_model_list) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.1 sourcePos_y = -0.1 min_distance = 0.05 search_window = 10 gamma = 1.9 kwargs_lens = [{ 'theta_E': 1., 'gamma': gamma, 'q': 0.8, 'phi_G': 0.5, 'center_x': 0.1, 'center_y': -0.1 }, { 'theta_E': 0.1, 'center_x': 0.5, 'center_y': 0 }] x_pos, y_pos = lensEquationSolver.image_position_from_source( sourcePos_x, sourcePos_y, kwargs_lens, min_distance=min_distance, search_window=search_window, precision_limit=10**(-10), num_iter_max=10) source_x, source_y = lensModel.ray_shooting(x_pos, y_pos, kwargs_lens) npt.assert_almost_equal(sourcePos_x, source_x, decimal=10)
def setup(self): lens_model_list = ['SPEP', '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, 'gamma': 2., 'center_x': 0, 'center_y': 0}, {'gamma1': 0.06, 'gamma2': -0.03}] x_img, y_img = lensEquationSolver.image_position_from_source(kwargs_lens=kwargs_lens, sourcePos_x=x_source, sourcePos_y=y_source) print('image positions are: ', x_img, y_img) mag_inf = lensModel.magnification(x_img, y_img, kwargs_lens) print('point source magnification: ', mag_inf) source_size_arcsec = 0.001 window_size = 0.1 grid_number = 100 print('source size in arcsec: ', source_size_arcsec) mag_finite = lensModelExtensions.magnification_finite(x_pos=x_img, y_pos=y_img, kwargs_lens=kwargs_lens, source_sigma=source_size_arcsec, window_size=window_size, grid_number=grid_number) flux_ratios = mag_finite[1:] / mag_finite[0] flux_ratio_errors = [0.1, 0.1, 0.1] self.flux_likelihood = FluxRatioLikelihood(lens_model_class=lensModel, flux_ratios=flux_ratios, flux_ratio_errors=flux_ratio_errors, source_type='GAUSSIAN', window_size=window_size, grid_number=grid_number) self.flux_likelihood_inf = FluxRatioLikelihood(lens_model_class=lensModel, flux_ratios=flux_ratios, flux_ratio_errors=flux_ratio_errors, source_type='INF', window_size=window_size, grid_number=grid_number) self.kwargs_cosmo = {'source_size': source_size_arcsec} self.x_img, self.y_img = x_img, y_img self.kwargs_lens = kwargs_lens
def setup(self): # compute image positions lensModel = LensModel(lens_model_list=['SIE']) solver = LensEquationSolver(lensModel=lensModel) self._kwargs_lens = [{ 'theta_E': 1, 'e1': 0.1, 'e2': -0.03, 'center_x': 0, 'center_y': 0 }] x_pos, y_pos = solver.image_position_from_source( sourcePos_x=0.01, sourcePos_y=-0.01, kwargs_lens=self._kwargs_lens) point_source_class = PointSource( point_source_type_list=['LENSED_POSITION'], lensModel=lensModel) self.likelihood = PositionLikelihood(point_source_class, position_uncertainty=0.005, astrometric_likelihood=True, image_position_likelihood=True, ra_image_list=[x_pos], dec_image_list=[y_pos], source_position_likelihood=True, check_solver=False, solver_tolerance=0.001, force_no_add_image=False, restrict_image_number=False, max_num_images=None) self._x_pos, self._y_pos = x_pos, y_pos
def test_analytical_lens_equation_solver(self): lensModel = LensModel(['EPL_NUMBA', 'SHEAR']) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.03 sourcePos_y = 0.0 kwargs_lens = [{ 'theta_E': 1., 'gamma': 2.2, 'center_x': 0.01, 'center_y': 0.02, 'e1': 0.01, 'e2': 0.05 }, { 'gamma1': -0.04, 'gamma2': -0.1, 'ra_0': 0.01, 'dec_0': 0.02 }] x_pos, y_pos = lensEquationSolver.image_position_from_source( sourcePos_x, sourcePos_y, kwargs_lens, solver='analytical') source_x, source_y = lensModel.ray_shooting(x_pos, y_pos, kwargs_lens) assert len(source_x) == len(source_y) >= 4 npt.assert_almost_equal(sourcePos_x, source_x, decimal=10) npt.assert_almost_equal(sourcePos_y, source_y, decimal=10) x_pos_ls, y_pos_ls = lensEquationSolver.image_position_from_source( sourcePos_x, sourcePos_y, kwargs_lens, solver='analytical') for x, y in zip( x_pos_ls, y_pos_ls): # Check if it found all solutions lenstronomy found assert np.sqrt((x - x_pos)**2 + (y - y_pos)**2).min() < 1e-8
def test_zoom_source(self): lens_model_list = ['SPEMD', '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, 'gamma': 2, '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 __init__(self, lensModel, fixed_magnification=False, additional_image=False): self._lensModel = lensModel self._solver = LensEquationSolver(lensModel) self._fixed_magnification = fixed_magnification self._additional_image = additional_image
def test_shapelet_cart(self): lens_model_list = ['SHAPELETS_CART', 'SIS'] lens = LensModel(lens_model_list) solver = Solver2Point(lens, solver_type='SHAPELETS') image_position = LensEquationSolver(lens) sourcePos_x = 0.1 sourcePos_y = 0.03 deltapix = 0.05 numPix = 100 kwargs_lens = [{ 'coeffs': [1., 0., 0.1, 1.], 'beta': 1. }, { 'theta_E': 1., 'center_x': -0.1, 'center_y': 0.1 }] x_pos, y_pos = image_position.findBrightImage( sourcePos_x, sourcePos_y, kwargs_lens, numImages=2, min_distance=deltapix, search_window=numPix * deltapix, precision_limit=10**(-10)) print(len(x_pos), 'number of images') x_pos = x_pos[:2] y_pos = y_pos[:2] kwargs_init = [{ 'coeffs': [1., 0., 0.1, 1.], 'beta': 1. }, { 'theta_E': 1., 'center_x': -0.1, 'center_y': 0.1 }] kwargs_out, precision = solver.constraint_lensmodel( x_pos, y_pos, kwargs_init) print(kwargs_out, 'output') source_x, source_y = lens.ray_shooting(x_pos[0], y_pos[0], kwargs_out) x_pos_new, y_pos_new = image_position.findBrightImage( source_x, source_y, kwargs_out, numImages=2, min_distance=deltapix, search_window=numPix * deltapix) npt.assert_almost_equal(x_pos_new[0], x_pos[0], decimal=3) npt.assert_almost_equal(y_pos_new[0], y_pos[0], decimal=3) npt.assert_almost_equal(kwargs_out[0]['coeffs'][1], kwargs_lens[0]['coeffs'][1], decimal=3) npt.assert_almost_equal(kwargs_out[0]['coeffs'][2], kwargs_lens[0]['coeffs'][2], decimal=3)
def point_source_plot(ax, pixel_grid, lens_model, kwargs_lens, source_x, source_y, **kwargs): """ plots and illustrates images of a point source The plotting routine orders the image labels according to the arrival time and illustrates a diamond shape of the size of the magnification. The coordinates are chosen in pixel coordinates :param ax: matplotlib axis instance :param pixel_grid: lenstronomy PixelGrid() instance (or class with inheritance of PixelGrid() :param lens_model: LensModel() class instance :param kwargs_lens: lens model keyword argument list :param source_x: x-position of source :param source_y: y-position of source :param kwargs: additional plotting keyword arguments :return: matplotlib axis instance with figure """ from lenstronomy.LensModel.Solver.lens_equation_solver import LensEquationSolver solver = LensEquationSolver(lens_model) x_center, y_center = pixel_grid.center delta_pix = pixel_grid.pixel_width ra0, dec0 = pixel_grid.radec_at_xy_0 tranform = pixel_grid.transform_angle2pix if np.linalg.det( tranform ) < 0: # if coordiate transform has negative parity (#TODO temporary fix) delta_pix_x = -delta_pix else: delta_pix_x = delta_pix origin = [ra0, dec0] theta_x, theta_y = solver.image_position_from_source( source_x, source_y, kwargs_lens, search_window=np.max(pixel_grid.width), x_center=x_center, y_center=y_center, min_distance=pixel_grid.pixel_width) mag_images = lens_model.magnification(theta_x, theta_y, kwargs_lens) #ax = plot_util.image_position_plot(ax=ax, coords=pixel_grid, ra_image=theta_x, dec_image=theta_y, color='w', image_name_list=None) x_image, y_image = pixel_grid.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) * delta_pix_x + origin[0] y_ = (y_image[i] + 0.5) * delta_pix + origin[1] 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 = pixel_grid.map_coord2pix(source_x, source_y) ax.plot((x_source + 0.5) * delta_pix_x + origin[0], (y_source + 0.5) * delta_pix + origin[1], '*k', markersize=10) return ax
def test_solver_nfw(self): lens_model_list = ['NFW_ELLIPSE', 'SIS'] lensModel = LensModel(lens_model_list) solver = Solver4Point(lensModel) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.1 sourcePos_y = -0.1 deltapix = 0.05 numPix = 150 Rs = 4. phi_G, q = 0.5, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_lens = [{ 'theta_Rs': 1., 'Rs': Rs, 'e1': e1, 'e2': e2, 'center_x': 0.1, 'center_y': -0.1 }, { 'theta_E': 1, '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) phi_G, q = 1.5, 0.9 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_lens_init = [{ 'theta_Rs': 0.5, 'Rs': Rs, 'e1': e1, 'e2': e2, '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]['theta_Rs'], kwargs_lens[0]['theta_Rs'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['e1'], kwargs_lens[0]['e1'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['e2'], kwargs_lens[0]['e2'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['center_x'], kwargs_lens[0]['center_x'], decimal=3) npt.assert_almost_equal(kwargs_lens_new[0]['center_y'], kwargs_lens[0]['center_y'], decimal=3)
def update_lens_model(self, lens_model_class): """ update LensModel() and LensEquationSolver() instance :param lens_model_class: LensModel() class instance :return: internal lensModel class updated """ self._lensModel = lens_model_class self._solver = LensEquationSolver(lens_model_class)
def setup(self): kwargs_model = { 'lens_light_model_list': ['HERNQUIST'], 'lens_model_list': ['SIE'], 'point_source_model_list': ['LENSED_POSITION'] } z_lens = 0.5 z_source = 2.5 R_slit = 3.8 dR_slit = 1. aperture_type = 'slit' kwargs_aperture = { 'aperture_type': aperture_type, 'center_ra': 0, 'width': dR_slit, 'length': R_slit, 'angle': 0, 'center_dec': 0 } psf_fwhm = 0.7 kwargs_seeing = {'psf_type': 'GAUSSIAN', 'fwhm': psf_fwhm} TDCosmography(z_lens, z_source, kwargs_model) from astropy.cosmology import FlatLambdaCDM cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Ob0=0.05) self.td_cosmo = TDCosmography(z_lens, z_source, kwargs_model, cosmo_fiducial=cosmo, lens_model_kinematics_bool=None, kwargs_aperture=kwargs_aperture, kwargs_seeing=kwargs_seeing, light_model_kinematics_bool=None) self.lens = LensModel(lens_model_list=['SIE'], cosmo=cosmo, z_lens=z_lens, z_source=z_source) self.solver = LensEquationSolver(lensModel=self.lens) self.kwargs_lens = [{ 'theta_E': 1, 'e1': 0.1, 'e2': -0.2, 'center_x': 0, 'center_y': 0 }] source_x, source_y = 0, 0.05 image_x, image_y = self.solver.image_position_from_source( source_x, source_y, self.kwargs_lens, min_distance=0.1, search_window=10) self.kwargs_ps = [{'ra_image': image_x, 'dec_image': image_y}] self.image_x, self.image_y = image_x, image_y
def __init__(self, lensModel, fixed_magnification=True): """ :param lensModel: instance of the LensModel() class :param fixed_magnification: bool. If True, magnification ratio of point sources is fixed to the one given by the lens model """ self._lensModel = lensModel self._solver = LensEquationSolver(lensModel) self._fixed_magnification = fixed_magnification
def test_point_source(self): kwargs_model = { 'lens_model_list': ['SPEMD', 'SHEAR_GAMMA_PSI'], 'point_source_model_list': ['SOURCE_POSITION'] } lensAnalysis = LensAnalysis(kwargs_model=kwargs_model) source_x, source_y = 0.02, 0.1 kwargs_ps = [{ 'dec_source': source_y, 'ra_source': source_x, 'point_amp': 75.155 }] kwargs_lens = [{ 'e2': 0.1, 'center_x': 0, 'theta_E': 1.133, 'e1': 0.1, 'gamma': 2.063, 'center_y': 0 }, { 'gamma_ext': 0.026, 'psi_ext': 1.793 }] x_image, y_image = lensAnalysis.PointSource.image_position( kwargs_ps=kwargs_ps, kwargs_lens=kwargs_lens) from lenstronomy.LensModel.Solver.lens_equation_solver import LensEquationSolver from lenstronomy.LensModel.lens_model import LensModel lensModel = LensModel(lens_model_list=['SPEMD', 'SHEAR_GAMMA_PSI']) from lenstronomy.PointSource.point_source import PointSource ps = PointSource(point_source_type_list=['SOURCE_POSITION'], lensModel=lensModel) x_image_new, y_image_new = ps.image_position(kwargs_ps, kwargs_lens) npt.assert_almost_equal(x_image_new[0], x_image[0], decimal=7) solver = LensEquationSolver(lensModel=lensModel) x_image_true, y_image_true = solver.image_position_from_source( source_x, source_y, kwargs_lens, min_distance=0.01, search_window=5, precision_limit=10**(-10), num_iter_max=100, arrival_time_sort=True, initial_guess_cut=False, verbose=False, x_center=0, y_center=0, num_random=0, non_linear=False, magnification_limit=None) print(x_image[0], y_image[0], x_image_true, y_image_true) npt.assert_almost_equal(x_image_true, x_image[0], decimal=7)
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_arcs_at_image_position(self): # lensing quantities kwargs_spp = { 'theta_E': 1.26, 'gamma': 2., 'e1': 0.1, 'e2': -0.1, 'center_x': 0.0, 'center_y': 0.0 } # parameters of the deflector lens model # the lens model is a supperposition of an elliptical lens model with external shear lens_model_list = ['SPEP'] #, 'SHEAR'] kwargs_lens = [kwargs_spp] #, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) lensEquationSolver = LensEquationSolver(lens_model_class) source_x = 0. source_y = 0.05 x_image, y_image = lensEquationSolver.findBrightImage( source_x, source_y, kwargs_lens, numImages=4, min_distance=0.05, search_window=5) arc_model = LensModel(lens_model_list=['CURVED_ARC_SPP', 'SHIFT']) for i in range(len(x_image)): x0, y0 = x_image[i], y_image[i] print(x0, y0, i) ext = LensModelExtensions(lensModel=lens_model_class) kwargs_arc_i = ext.curved_arc_estimate(x0, y0, kwargs_lens) alpha_x, alpha_y = lens_model_class.alpha(x0, y0, kwargs_lens) kwargs_arc = [ kwargs_arc_i, { 'alpha_x': alpha_x, 'alpha_y': alpha_y } ] print(kwargs_arc_i) direction = kwargs_arc_i['direction'] print(np.cos(direction), np.sin(direction)) x, y = util.make_grid(numPix=5, deltapix=0.01) x = x0 y = y0 gamma1_arc, gamma2_arc = arc_model.gamma(x, y, kwargs_arc) gamma1, gamma2 = lens_model_class.gamma(x, y, kwargs_lens) print(gamma1, gamma2) npt.assert_almost_equal(gamma1_arc, gamma1, decimal=3) npt.assert_almost_equal(gamma2_arc, gamma2, decimal=3) theta_E_arc, gamma_arc, center_x_spp_arc, center_y_spp_arc = CurvedArcSPP.stretch2spp( **kwargs_arc_i) print(theta_E_arc, gamma_arc, center_x_spp_arc, center_y_spp_arc) npt.assert_almost_equal(center_x_spp_arc, 0, decimal=3) npt.assert_almost_equal(center_y_spp_arc, 0, decimal=3)
def test_caustics(self): lm = LensModel(['EPL_NUMBA', 'SHEAR']) leqs = LensEquationSolver(lm) kwargs = [{ 'theta_E': 1., 'e1': 0.5, 'e2': 0.1, 'center_x': 0.0, 'center_y': 0.0, 'gamma': 1.9 }, { 'gamma1': 0.03, 'gamma2': 0.01, 'ra_0': 0.0, 'dec_0': 0.0 }] # Calculate the caustics and a few critical curves. caus = caustics_epl_shear(kwargs, return_which='caustic') lensplane_caus = caustics_epl_shear(kwargs, return_which='caustic', sourceplane=False) cut = caustics_epl_shear(kwargs, return_which='cut') lensplane_cut = caustics_epl_shear(kwargs, return_which='cut', sourceplane=False) twoimg = caustics_epl_shear(kwargs, return_which='double') fourimg = caustics_epl_shear(kwargs, return_which='quad') assert np.abs(lm.magnification(*lensplane_caus, kwargs)).min() > 1e12 assert np.abs(lm.magnification(*lensplane_cut, kwargs)).min() > 1e12 # Test whether the caustics indeed the number of images they say N = 20 xpl, ypl = np.linspace(-1, 1, N), np.linspace(-1, 1, N) xgr, ygr = np.meshgrid(xpl, ypl, indexing='ij') xf, yf = xgr.flatten(), ygr.flatten() sols = [ leqs.image_position_from_source(x, y, kwargs, solver='analytical') for x, y in zip(xf, yf) ] numsols = np.array([len(p[0]) for p in sols]) from matplotlib.path import Path points = np.vstack((xf, yf)).T p = Path(twoimg.T) # make a polygon grid2img = p.contains_points(points) assert np.all(numsols[grid2img] >= 2) p = Path(fourimg.T) # make a polygon grid4img = p.contains_points(points) assert np.all(numsols[grid4img] >= 4)
def test_with_solver(self): kwargs_model = { 'lens_model_list': ['SPEP'], 'source_light_model_list': ['SERSIC'], 'point_source_model_list': ['LENSED_POSITION'] } i_lens_light, k_ps = 0, 0 kwargs_constraints = { 'solver_type': 'PROFILE', 'num_point_source_list': [4] } kwargs_lens = [{ 'theta_E': 1, 'gamma': 2, 'e1': 0.1, 'e2': 0.1, 'center_x': 0, 'center_y': 0 }] kwargs_source = [{ 'amp': 1, 'n_sersic': 2, 'R_sersic': 0.3, 'center_x': 1, 'center_y': 1 }] kwargs_lens_light = [{ 'amp': 1, 'n_sersic': 2, 'R_sersic': 0.3, 'center_x': 0.2, 'center_y': 0.2 }] lensModel = LensModel(lens_model_list=['SPEP']) lensEquationSlover = LensEquationSolver(lensModel=lensModel) x_image, y_image = lensEquationSlover.image_position_from_source( sourcePos_x=0.0, sourcePos_y=0.01, kwargs_lens=kwargs_lens) print(x_image, y_image, 'test') kwargs_ps = [{'ra_image': x_image, 'dec_image': y_image}] param = Param(kwargs_model=kwargs_model, kwargs_lens_init=kwargs_lens, **kwargs_constraints) args = param.kwargs2args(kwargs_lens=kwargs_lens, kwargs_source=kwargs_source, kwargs_lens_light=kwargs_lens_light, kwargs_ps=kwargs_ps) #kwargs_lens_out, kwargs_source_out, kwargs_lens_light_out, kwargs_ps_out, _ = param.args2kwargs(args) kwargs_return = param.args2kwargs(args) kwargs_lens_out = kwargs_return['kwargs_lens'] kwargs_ps_out = kwargs_return['kwargs_ps'] dist = param.check_solver(kwargs_lens=kwargs_lens_out, kwargs_ps=kwargs_ps_out) npt.assert_almost_equal(dist, 0, decimal=10)
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 setup(self): lensModel = LensModel(lens_model_list=['SPEP']) solver = LensEquationSolver(lensModel=lensModel) self.kwargs_lens = [{'theta_E': 1., 'center_x': 0, 'center_y': 0, 'q': 0.7, 'phi_G': 0, 'gamma': 2}] self.sourcePos_x, self.sourcePos_y = 0.01, -0.01 self.x_pos, self.y_pos = solver.image_position_from_source(sourcePos_x=self.sourcePos_x, sourcePos_y=self.sourcePos_y, kwargs_lens=self.kwargs_lens) self.PointSource = PointSource(point_source_type_list=['LENSED_POSITION', 'UNLENSED', 'SOURCE_POSITION', 'NONE'], lensModel=lensModel, fixed_magnification_list=[True]*4, additional_images_list=[False]*4) self.kwargs_ps = [{'ra_image': self.x_pos, 'dec_image': self.y_pos, 'source_amp': 1}, {'ra_image': [1.], 'dec_image': [1.], 'point_amp': [10]}, {'ra_source': self.sourcePos_x, 'dec_source': self.sourcePos_y, 'source_amp': 1.}, {}]