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 _test_curved_arc_recovery(self, kwargs_arc_init): ext = LensModelExtensions( LensModel(lens_model_list=['CURVED_ARC_TAN_DIFF'])) center_x, center_y = kwargs_arc_init['center_x'], kwargs_arc_init[ 'center_y'] kwargs_arc = ext.curved_arc_estimate(center_x, center_y, [kwargs_arc_init]) lambda_rad, lambda_tan, orientation_angle, dlambda_tan_dtan, dlambda_tan_drad, dlambda_rad_drad, dlambda_rad_dtan, dphi_tan_dtan, dphi_tan_drad, dphi_rad_drad, dphi_rad_dtan = ext.radial_tangential_differentials( center_x, center_y, [kwargs_arc_init]) print(lambda_tan, dlambda_tan_dtan, kwargs_arc_init['dtan_dtan']) npt.assert_almost_equal(kwargs_arc['tangential_stretch'] / kwargs_arc_init['tangential_stretch'], 1, decimal=3) npt.assert_almost_equal(kwargs_arc['radial_stretch'], kwargs_arc_init['radial_stretch'], decimal=3) npt.assert_almost_equal(kwargs_arc['curvature'], kwargs_arc_init['curvature'], decimal=2) npt.assert_almost_equal(dphi_tan_dtan, kwargs_arc_init['curvature'], decimal=2) npt.assert_almost_equal(kwargs_arc['direction'], kwargs_arc_init['direction'], decimal=3) npt.assert_almost_equal(dlambda_tan_dtan / lambda_tan, kwargs_arc_init['dtan_dtan'], decimal=2)
def mst_invariant_differential(self, kwargs_lens, radius, center_x=None, center_y=None, model_list_bool=None, num_points=10): """ Average of the radial stretch differential in radial direction, divided by the radial stretch factor. .. math:: \\xi = \\frac{\\partial \\lambda_{\\rm rad}}{\\partial r} \\frac{1}{\\lambda_{\\rm rad}} This quantity is invariant under the MST. The specific definition is provided by Birrer 2021. Equivalent (proportional) definitions are provided by e.g. Kochanek 2020, Sonnenfeld 2018. :param kwargs_lens: lens model keyword argument list :param radius: radius from the center where to compute the MST invariant differential :param center_x: center position :param center_y: center position :param model_list_bool: indicate which part of the model to consider :param num_points: number of estimates around the radius :return: xi """ center_x, center_y = analysis_util.profile_center( kwargs_lens, center_x, center_y) x, y = util.points_on_circle(radius, num_points) ext = LensModelExtensions(lensModel=self._lens_model) lambda_rad, lambda_tan, orientation_angle, dlambda_tan_dtan, dlambda_tan_drad, dlambda_rad_drad, dlambda_rad_dtan, dphi_tan_dtan, dphi_tan_drad, dphi_rad_drad, dphi_rad_dtan = ext.radial_tangential_differentials( x, y, kwargs_lens, center_x=center_x, center_y=center_y) xi = np.mean(dlambda_rad_drad / lambda_rad) return xi
def __init__(self, multi_band_list, kwargs_model, model, error_map, cov_param, param, kwargs_params, likelihood_mask_list=None, band_index=0, arrow_size=0.02, cmap_string="gist_heat"): """ :param multi_band_list: list of imaging data configuration [[kwargs_data, kwargs_psf, kwargs_numerics], [...]] :param kwargs_model: model keyword argument list for the full multi-band modeling :param model: 2d numpy array of modeled image for the specified band :param error_map: 2d numpy array of size of the image, additional error in the pixels coming from PSF uncertainties :param cov_param: covariance matrix of the linear inversion :param param: 1d numpy array of the linear coefficients of this imaging band :param kwargs_params: keyword argument of keyword argument lists of the different model components selected for the imaging band, NOT including linear amplitudes (not required as being overwritten by the param list) :param image_likelihood_mask_list: list of 2d numpy arrays of likelihood masks (for all bands) :param band_index: integer of the band to be considered in this class :param arrow_size: size of the scale and orientation arrow :param cmap_string: string of color map (or cmap matplotlib object) """ ModelBand.__init__(self, multi_band_list, kwargs_model, model, error_map, cov_param, param, kwargs_params, image_likelihood_mask_list=likelihood_mask_list, band_index=band_index) self._lensModel = self._bandmodel.LensModel self._lensModelExt = LensModelExtensions(self._lensModel) log_model = np.log10(model) log_model[np.isnan(log_model)] = -5 self._v_min_default = max(np.min(log_model), -5) self._v_max_default = min(np.max(log_model), 10) self._coords = self._bandmodel.Data self._data = self._coords.data self._deltaPix = self._coords.pixel_width self._frame_size = np.max(self._coords.width) x_grid, y_grid = self._coords.pixel_coordinates self._x_grid = util.image2array(x_grid) self._y_grid = util.image2array(y_grid) if isinstance(cmap_string, str): cmap = plt.get_cmap(cmap_string) else: cmap = cmap_string cmap.set_bad(color='k', alpha=1.) cmap.set_under('k') self._cmap = cmap self._arrow_size = arrow_size
def test_curved_arc_finite_area(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) dr = 0.001 kwargs_arc_finite = ext.curved_arc_finite_area(x_0, y_0, kwargs_lens, dr) npt.assert_almost_equal(kwargs_arc['direction'], kwargs_arc_finite['direction'], decimal=3) npt.assert_almost_equal(kwargs_arc['radial_stretch'], kwargs_arc_finite['radial_stretch'], decimal=3) npt.assert_almost_equal(kwargs_arc['tangential_stretch'], kwargs_arc_finite['tangential_stretch'], decimal=3) npt.assert_almost_equal(kwargs_arc['curvature'], kwargs_arc_finite['curvature'], decimal=3)
def __init__(self, lens_model_class, flux_ratios, flux_ratio_errors, source_type='INF', window_size=0.1, grid_number=100, polar_grid=False, aspect_ratio=0.5): """ :param point_source_class: PointSource class instance :param lens_model_class: LensModel class instance :param param_class: Param() class instance :param flux_ratios: ratio of fluxes of the multiple images (relative to the first appearing) :param flux_ratio_errors: errors in the flux ratios (relative to the first appearing :param source_type: string, type of source, 'INF' specifies a point source, while 'GAUSSIAN' specifies a finite-size source modeled as a Gaussian :param window_size: size of window to compute the finite flux :param grid_number: number of grid cells per axis in the window to numerically comute the flux """ self._lens_model_class = lens_model_class self._flux_ratios = np.array(flux_ratios) self._flux_ratio_errors = np.array(flux_ratio_errors) self._lens_model_extensions = LensModelExtensions( lensModel=lens_model_class) self._source_type = source_type self._window_size = window_size self._gird_number = grid_number self._polar_grid = polar_grid self._aspect_ratio = aspect_ratio
def test_lens_center(self): center_x, center_y = 0.43, -0.67 kwargs_lens = [{'theta_E': 1, 'center_x': center_x, 'center_y': center_y}] lensModel = LensModelExtensions(LensModel(lens_model_list=['SIS'])) center_x_out, center_y_out = lensModel.lens_center(kwargs_lens) npt.assert_almost_equal(center_x_out, center_x, 2) npt.assert_almost_equal(center_y_out, center_y, 2)
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 test_radial_tangential_stretch(self): lens_model_list = ['SIS', 'SHEAR'] lens_mp = LensModel(lens_model_list=lens_model_list, lens_redshift_list=[0.5, 0.4], multi_plane=True, z_source=2) lens = LensModel(lens_model_list=lens_model_list) x0, y0 = 1., 1. kwargs_lens = [{ 'theta_E': 1, 'center_x': 0, 'center_y': 0 }, { 'gamma1': 0.0, 'gamma2': 0.00001 }] extensions = LensModelExtensions(lensModel=lens) extensions_mp = LensModelExtensions(lensModel=lens_mp) radial_stretch, tangential_stretch, v_rad1, v_rad2, v_tang1, v_tang2 = extensions.radial_tangential_stretch( x0, y0, kwargs_lens, diff=None) radial_stretch_mp, tangential_stretch_mp, v_rad1_mp, v_rad2_mp, v_tang1_mp, v_tang2_mp = extensions_mp.radial_tangential_stretch( x0, y0, kwargs_lens, diff=None) npt.assert_almost_equal(radial_stretch, radial_stretch_mp, decimal=4) npt.assert_almost_equal(tangential_stretch, tangential_stretch_mp, decimal=4) npt.assert_almost_equal(v_rad1, v_rad1_mp, decimal=4) npt.assert_almost_equal(v_rad2, v_rad2_mp, decimal=4) npt.assert_almost_equal(v_tang1, v_tang1_mp, decimal=4) npt.assert_almost_equal(v_tang2, v_tang2_mp, decimal=4)
def critical_cruves_caustics(self, lens_system=None, main=None, halos=None, multiplane=None, compute_window=1.5, scale=0.5, max_order=10, method=None, grid_scale=0.005): if lens_system is None: lens_system = self.build_system(main=main, realization=halos, multiplane=multiplane) lenstronomy = self.lenstronomy_build() lensmodel, lensmodel_params = lenstronomy.get_lensmodel(lens_system) extension = LensModelExtensions(lensmodel) if method == 'tiling': xcrit, ycrit = extension.critical_curve_tiling( lensmodel_params, compute_window=compute_window, start_scale=scale, max_order=max_order) return xcrit, ycrit else: ra_crit_list, dec_crit_list, ra_caustic_list, dec_caustic_list = \ extension.critical_curve_caustics(lensmodel_params,compute_window=5,grid_scale=grid_scale) return ra_crit_list, dec_crit_list, ra_caustic_list, dec_caustic_list
def test_external_shear(self): lens_model_list = ['SHEAR'] kwargs_lens = [{'e1': 0.1, 'e2': 0.01}] lensModel = LensModelExtensions(LensModel(lens_model_list)) phi, gamma = lensModel.external_shear(kwargs_lens) npt.assert_almost_equal(phi, 0.049834326245581012, decimal=8) npt.assert_almost_equal(gamma, 0.10049875621120891, decimal=8)
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 }] lensModel = LensModelExtensions(LensModel(lens_model_list)) ra_crit_list, dec_crit_list, ra_caustic_list, dec_caustic_list = lensModel.critical_curve_caustics( kwargs_lens, compute_window=5, grid_scale=0.005) print(ra_caustic_list) npt.assert_almost_equal(ra_caustic_list[0][3], -0.25629009803139047, decimal=5) npt.assert_almost_equal(dec_caustic_list[0][3], -0.39153358367275115, decimal=5) npt.assert_almost_equal(ra_crit_list[0][3], -0.53249999999999997, decimal=5) npt.assert_almost_equal(dec_crit_list[0][3], -1.2536936868024853, decimal=5)
def test_radial_tangential_distortions(self): lens_model_list = ['CURVED_ARC_SPP', 'SHEAR', 'FLEXION'] center_x, center_y = 0.01, 0 curvature = 1. / 2 lens = LensModel(lens_model_list=lens_model_list) kwargs_lens = [{ 'tangential_stretch': 10, 'radial_stretch': 1., 'curvature': curvature, 'direction': -10, 'center_x': center_x, 'center_y': center_y }, { 'gamma1': -0., 'gamma2': -0.0 }, { 'g1': 0., 'g2': 0., 'g3': -0., 'g4': 0 }] extensions = LensModelExtensions(lensModel=lens) lambda_rad, lambda_tan, orientation_angle, dlambda_tan_dtan, dlambda_tan_drad, dlambda_rad_drad, dlambda_rad_dtan, dphi_tan_dtan, dphi_tan_drad, dphi_rad_drad, dphi_rad_dtan = extensions.radial_tangential_differentials( x=center_x, y=center_y, kwargs_lens=kwargs_lens, smoothing_3rd=0.0001) print(orientation_angle, 'orientation angle') l = 1. / dphi_tan_dtan npt.assert_almost_equal(l, 1. / curvature, decimal=6)
def test_radial_tangential_distortions_multi_plane(self): lens_model_list = ['SIS', 'SHEAR'] lens_mp = LensModel(lens_model_list=lens_model_list, lens_redshift_list=[0.5, 0.4], multi_plane=True, z_source=2) lens = LensModel(lens_model_list=lens_model_list) x0, y0 = 2., 1. kwargs_lens = [{ 'theta_E': 1, 'center_x': 0, 'center_y': 0 }, { 'gamma1': 0.0, 'gamma2': 0.00001 }] extensions = LensModelExtensions(lensModel=lens) extensions_mp = LensModelExtensions(lensModel=lens_mp) lambda_rad, lambda_tan, orientation_angle, dlambda_tan_dtan, dlambda_tan_drad, dlambda_rad_drad, dlambda_rad_dtan, dphi_tan_dtan, dphi_tan_drad, dphi_rad_drad, dphi_rad_dtan = extensions.radial_tangential_differentials( x=x0, y=y0, kwargs_lens=kwargs_lens, smoothing_3rd=0.0001) lambda_rad_mp, lambda_tan_mp, orientation_angle_mp, dlambda_tan_dtan_mp, dlambda_tan_drad_mp, dlambda_rad_drad_mp, dlambda_rad_dtan_mp, dphi_tan_dtan_mp, dphi_tan_drad_mp, dphi_rad_drad_mp, dphi_rad_dtan_mp = extensions_mp.radial_tangential_differentials( x=x0, y=y0, kwargs_lens=kwargs_lens, smoothing_3rd=0.0001) npt.assert_almost_equal(lambda_rad, lambda_rad_mp, decimal=3) npt.assert_almost_equal(lambda_tan, lambda_tan_mp, decimal=3) npt.assert_almost_equal(dphi_tan_dtan, dphi_rad_dtan_mp, decimal=3)
def test_radial_tangential_differentials(self): from lenstronomy.Util import util x, y = util.make_grid(numPix=10, deltapix=1) lens_model_list = ['SIS'] center_x, center_y = 0, 0 lens = LensModel(lens_model_list=lens_model_list) kwargs_lens = [{ 'theta_E': 1, 'center_x': center_x, 'center_y': center_y }] extensions = LensModelExtensions(lensModel=lens) lambda_rad, lambda_tan, orientation_angle, dlambda_tan_dtan, dlambda_tan_drad, dlambda_rad_drad, dlambda_rad_dtan, dphi_tan_dtan, dphi_tan_drad, dphi_rad_drad, dphi_rad_dtan = extensions.radial_tangential_differentials( 2, 2, kwargs_lens, smoothing_3rd=0.001) npt.assert_almost_equal(lambda_rad, 1, decimal=5) npt.assert_almost_equal(lambda_tan, 1.5469181606780271, decimal=5) lambda_rad, lambda_tan, orientation_angle, dlambda_tan_dtan, dlambda_tan_drad, dlambda_rad_drad, dlambda_rad_dtan, dphi_tan_dtan, dphi_tan_drad, dphi_rad_drad, dphi_rad_dtan = extensions.radial_tangential_differentials( np.array([2]), np.array([2]), kwargs_lens, smoothing_3rd=0.001) npt.assert_almost_equal(lambda_rad, 1, decimal=5) npt.assert_almost_equal(lambda_tan, 1.5469181606780271, decimal=5) mag = lens.magnification(x, y, kwargs_lens) lambda_rad, lambda_tan, orientation_angle, dlambda_tan_dtan, dlambda_tan_drad, dlambda_rad_drad, dlambda_rad_dtan, dphi_tan_dtan, dphi_tan_drad, dphi_rad_drad, dphi_rad_dtan = extensions.radial_tangential_differentials( x, y, kwargs_lens, smoothing_3rd=0.001) mag_tang_rad = lambda_tan * lambda_rad npt.assert_almost_equal(mag_tang_rad, mag, decimal=5)
def test_get_magnification_model(self): self.kwargs_options = { 'lens_model_list': ['GAUSSIAN'], 'source_light_model_list': ['GAUSSIAN'], 'lens_light_model_list': ['SERSIC'], 'subgrid_res': 10, 'numPix': 200, 'psf_type': 'gaussian', 'x2_simple': True } kwargs_lens = [{ 'amp': 1, 'sigma_x': 2, 'sigma_y': 2, 'center_x': 0, 'center_y': 0 }] x_pos = np.array([1., 1., 2.]) y_pos = np.array([-1., 0., 0.]) lens_model = LensModelExtensions( LensModel(lens_model_list=['GAUSSIAN'])) mag = lens_model.magnification_finite(x_pos, y_pos, kwargs_lens, source_sigma=0.003, window_size=0.1, grid_number=100) npt.assert_almost_equal(mag[0], 0.98848384784633392, decimal=5)
def test_magnification_finite(self): lens_model_list = ['SPEP', 'SHEAR'] 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 }] extension = LensModelExtensions(LensModel(lens_model_list)) x_image = [0.56153533, -0.78067875, -0.72551184, 0.75664112] y_image = [-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_critical_curves(self): lens_model_list = ['SPEP'] kwargs_lens = [{ 'theta_E': 1., 'gamma': 2., 'q': 0.8, 'phi_G': 1., 'center_x': 0, 'center_y': 0 }] lensModel = LensModelExtensions(lens_model_list) ra_crit_list, dec_crit_list, ra_caustic_list, dec_caustic_list = lensModel.critical_curve_caustics( kwargs_lens, compute_window=5, grid_scale=0.005) print(ra_caustic_list) npt.assert_almost_equal(ra_caustic_list[0][3], -0.25629009803139047, decimal=5) npt.assert_almost_equal(dec_caustic_list[0][3], -0.39153358367275115, decimal=5) npt.assert_almost_equal(ra_crit_list[0][3], -0.53249999999999997, decimal=5) npt.assert_almost_equal(dec_crit_list[0][3], -1.2536936868024853, decimal=5) """
def __init__(self, lens_model_class, flux_ratios, flux_ratio_errors, source_type='INF', window_size=0.1, grid_number=100, polar_grid=False, aspect_ratio=0.5): """ :param point_source_class: PointSource class instance :param lens_model_class: LensModel class instance :param param_class: Param() class instance :param flux_ratios: ratio of fluxes of the multiple images (relative to the first appearing) :param flux_ratio_errors: errors in the flux ratios (relative to the first appearing. Alternatively a log-normal covariance matrix. Note: in the case of a the covariance matrix, the errors are are assumed to be log-normal, i.e. the logarithms of the flux ratios, ln(F[i]/F[0]) are assumed to have a multivariate Gaussian distribution, with the given covariance matrix. :param source_type: string, type of source, 'INF' specifies a point source, while 'GAUSSIAN' specifies a finite-size source modeled as a Gaussian :param window_size: size of window to compute the finite flux :param grid_number: number of grid cells per axis in the window to numerically comute the flux """ self._lens_model_class = lens_model_class self._flux_ratios = np.array(flux_ratios) self._flux_ratio_errors = np.array(flux_ratio_errors) self._lens_model_extensions = LensModelExtensions( lensModel=lens_model_class) self._source_type = source_type self._window_size = window_size self._gird_number = grid_number self._polar_grid = polar_grid self._aspect_ratio = aspect_ratio
def __init__(self, kwargs_model): self.LensLightModel = LightModel(kwargs_model.get('lens_light_model_list', ['NONE'])) self.SourceModel = LightModel(kwargs_model.get('source_light_model_list', ['NONE'])) self.LensModel = LensModelExtensions(lens_model_list=kwargs_model['lens_model_list']) self.PointSource = PointSource(point_source_type_list=kwargs_model.get('point_source_model_list', ['NONE'])) self.kwargs_model = kwargs_model self.NumLensModel = NumericLens(lens_model_list=kwargs_model['lens_model_list']) self.gaussian = Gaussian()
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 __init__(self, multi_band_list, kwargs_model, model, error_map, cov_param, param, kwargs_params, likelihood_mask_list=None, band_index=0, arrow_size=0.02, cmap_string="gist_heat"): self.bandmodel = SingleBandMultiModel( multi_band_list, kwargs_model, likelihood_mask_list=likelihood_mask_list, band_index=band_index) self._kwargs_special_partial = kwargs_params.get( 'kwargs_special', None) kwarks_lens_partial, kwargs_source_partial, kwargs_lens_light_partial, kwargs_ps_partial, self._kwargs_extinction_partial = self.bandmodel.select_kwargs( **kwargs_params) self._kwargs_lens_partial, self._kwargs_source_partial, self._kwargs_lens_light_partial, self._kwargs_ps_partial = self.bandmodel.update_linear_kwargs( param, kwarks_lens_partial, kwargs_source_partial, kwargs_lens_light_partial, kwargs_ps_partial) self._norm_residuals = self.bandmodel.reduced_residuals( model, error_map=error_map) self._reduced_x2 = self.bandmodel.reduced_chi2(model, error_map=error_map) print("reduced chi^2 of data ", band_index, "= ", self._reduced_x2) self._model = model self._cov_param = cov_param self._param = param self._lensModel = self.bandmodel.LensModel self._lensModelExt = LensModelExtensions(self._lensModel) log_model = np.log10(model) log_model[np.isnan(log_model)] = -5 self._v_min_default = max(np.min(log_model), -5) self._v_max_default = min(np.max(log_model), 10) self._coords = self.bandmodel.Data self._data = self._coords.data self._deltaPix = self._coords.pixel_width self._frame_size = np.max(self._coords.width) x_grid, y_grid = self._coords.pixel_coordinates self._x_grid = util.image2array(x_grid) self._y_grid = util.image2array(y_grid) if isinstance(cmap_string, str): cmap = plt.get_cmap(cmap_string) else: cmap = cmap_string cmap.set_bad(color='k', alpha=1.) cmap.set_under('k') self._cmap = cmap self._arrow_size = arrow_size
def test_external_lensing_effect(self): lens_model_list = ['SHEAR'] kwargs_lens = [{'e1': 0.1, 'e2': 0.01}] lensModel = LensModelExtensions(LensModel(lens_model_list)) alpha0_x, alpha0_y, kappa_ext, shear1, shear2 = lensModel.external_lensing_effect(kwargs_lens, lens_model_internal_bool=[False]) print(alpha0_x, alpha0_y, kappa_ext, shear1, shear2) assert alpha0_x == 0 assert alpha0_y == 0 assert shear1 == 0.1 assert shear2 == 0.01 assert kappa_ext == 0
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 __init__(self, kwargs_data, kwargs_psf, kwargs_numerics, kwargs_model, kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps, arrow_size=0.02, cmap_string="gist_heat"): """ :param kwargs_options: :param kwargs_data: :param arrow_size: :param cmap_string: """ self._kwargs_data = kwargs_data if isinstance(cmap_string, str) or isinstance(cmap_string, unicode): cmap = plt.get_cmap(cmap_string) else: cmap = cmap_string cmap.set_bad(color='k', alpha=1.) cmap.set_under('k') self._cmap = cmap self._arrow_size = arrow_size data = Data(kwargs_data) self._coords = data._coords nx, ny = np.shape(kwargs_data['image_data']) Mpix2coord = kwargs_data['transform_pix2angle'] self._Mpix2coord = Mpix2coord self._deltaPix = self._coords.pixel_size self._frame_size = self._deltaPix * nx x_grid, y_grid = data.coordinates self._x_grid = util.image2array(x_grid) self._y_grid = util.image2array(y_grid) self._imageModel = class_creator.create_image_model(kwargs_data, kwargs_psf, kwargs_numerics, kwargs_model) self._analysis = LensAnalysis(kwargs_model) self._lensModel = LensModel(lens_model_list=kwargs_model.get('lens_model_list', []), z_source=kwargs_model.get('z_source', None), redshift_list=kwargs_model.get('redshift_list', None), multi_plane=kwargs_model.get('multi_plane', False)) self._lensModelExt = LensModelExtensions(self._lensModel) model, error_map, cov_param, param = self._imageModel.image_linear_solve(kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps, inv_bool=True) self._kwargs_lens = kwargs_lens self._kwargs_source = kwargs_source self._kwargs_lens_light = kwargs_lens_light self._kwargs_else = kwargs_ps self._model = model self._data = kwargs_data['image_data'] self._cov_param = cov_param self._norm_residuals = self._imageModel.reduced_residuals(model, error_map=error_map) self._reduced_x2 = self._imageModel.reduced_chi2(model, error_map=error_map) log_model = np.log10(model) log_model[np.isnan(log_model)] = -5 self._v_min_default = max(np.min(log_model), -5) self._v_max_default = min(np.max(log_model), 10) print("reduced chi^2 = ", self._reduced_x2)
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 __init__(self, kwargs_model): self.LensLightModel = LightModel( kwargs_model.get('lens_light_model_list', [])) self.SourceModel = LightModel( kwargs_model.get('source_light_model_list', [])) self.LensModel = LensModel( lens_model_list=kwargs_model.get('lens_model_list', []), z_source=kwargs_model.get('z_source', None), redshift_list=kwargs_model.get('redshift_list', None), multi_plane=kwargs_model.get('multi_plane', False)) self._lensModelExtensions = LensModelExtensions(self.LensModel) self.PointSource = PointSource(point_source_type_list=kwargs_model.get( 'point_source_model_list', [])) self.kwargs_model = kwargs_model self.NumLensModel = NumericLens( lens_model_list=kwargs_model.get('lens_model_list', []))
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 __init__(self, kwargs_model): self.LensModel, self.SourceModel, self.LensLightModel, self.PointSource, extinction_class = class_creator.create_class_instances( all_models=True, **kwargs_model) #self.LensLightModel = LightModel(kwargs_model.get('lens_light_model_list', [])) #self.LensModel = LensModel(lens_model_list=kwargs_model.get('lens_model_list', []), # z_source=kwargs_model.get('z_source', None), # lens_redshift_list=kwargs_model.get('lens_redshift_list', None), # multi_plane=kwargs_model.get('multi_plane', False)) #self.SourceModel = LightModel(kwargs_model.get('source_light_model_list', [])) self._lensModelExtensions = LensModelExtensions(self.LensModel) #self.PointSource = PointSource(point_source_type_list=kwargs_model.get('point_source_model_list', []), lensModel=self.LensModel) self.kwargs_model = kwargs_model self.NumLensModel = NumericLens( lens_model_list=kwargs_model.get('lens_model_list', []))
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)