def test_update_lens_model(self):
     lensModel = LensModel(lens_model_list=['SIS'])
     self.PointSource.update_lens_model(lens_model_class=lensModel)
     kwargs_lens = [{'theta_E': 1, 'center_x': 0, 'center_y': 0}]
     x_image_list, y_image_list = self.PointSource.image_position(kwargs_ps=self.kwargs_ps,
                                                                  kwargs_lens=kwargs_lens)
     npt.assert_almost_equal(x_image_list[0][0], -0.82654997748011705 , decimal=8)
Beispiel #2
0
 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 create_image_model(kwargs_data, kwargs_psf, kwargs_numerics, kwargs_model):
    """

    :param kwargs_data:
    :param kwargs_psf:
    :param kwargs_options:
    :return:
    """
    data_class = Data(kwargs_data)
    psf_class = PSF(kwargs_psf)
    lens_model_class = 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))
    source_model_class = LightModel(
        light_model_list=kwargs_model.get('source_light_model_list', []))
    lens_light_model_class = LightModel(
        light_model_list=kwargs_model.get('lens_light_model_list', []))
    point_source_class = PointSource(
        point_source_type_list=kwargs_model.get('point_source_model_list', []),
        lensModel=lens_model_class,
        fixed_magnification_list=kwargs_model.get('fixed_magnification_list',
                                                  None),
        additional_images_list=kwargs_model.get('additional_images_list',
                                                None),
        min_distance=kwargs_model.get('min_distance', 0.01),
        search_window=kwargs_model.get('search_window', 5),
        precision_limit=kwargs_model.get('precision_limit', 10**(-10)),
        num_iter_max=kwargs_model.get('num_iter_max', 100))
    imageModel = ImageModel(data_class, psf_class, lens_model_class,
                            source_model_class, lens_light_model_class,
                            point_source_class, kwargs_numerics)
    return imageModel
Beispiel #4
0
    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,
                                                     sigma_bkg=1)
        data_class = Data(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
Beispiel #5
0
    def get_cored_image(self, lens_info, src_light_info, z_lens, z_src,
                        bulge_or_disk):
        """Render a lensed cored sersic

        Note
        ----
        This method is only used to test the total magnification computation

        """
        lens_mass_model = LensModel([
            'SIE',
            'SHEAR_GAMMA_PSI',
        ],
                                    cosmo=self.cosmo,
                                    z_lens=z_lens,
                                    z_source=z_src)
        lens_mass_kwargs = get_lens_params(lens_info,
                                           z_src=z_src,
                                           cosmo=self.cosmo)
        src_light_kwargs = get_cored_sersic_params(src_light_info,
                                                   bulge_or_disk=bulge_or_disk)
        img, img_features = generate_image(lens_mass_kwargs, src_light_kwargs,
                                           self.null_psf, self.data_api,
                                           lens_mass_model,
                                           self.cored_sersic_model)
        img /= np.max(img)
        return img, img_features
Beispiel #6
0
 def get_image(self, lens_info, src_light_info, z_lens, z_src,
               bulge_or_disk):
     lens_mass_model = LensModel([
         'SIE',
         'SHEAR_GAMMA_PSI',
     ],
                                 cosmo=self.cosmo,
                                 z_lens=z_lens,
                                 z_source=z_src)
     lens_mass_kwargs = get_lens_params(lens_info,
                                        z_src=z_src,
                                        cosmo=self.cosmo)
     src_light_kwargs = get_src_light_params(src_light_info,
                                             bulge_or_disk=bulge_or_disk)
     img, img_features = generate_image(lens_mass_kwargs, src_light_kwargs,
                                        self.null_psf, self.data_api,
                                        lens_mass_model,
                                        self.src_light_model)
     img /= np.max(img)
     dmag = -2.5 * np.log10(img_features['total_magnification'])
     img_features['magnorms'] = {
         band: src_light_info[f'magnorm_{bulge_or_disk}_{band}'] + dmag
         for band in self.bands
     }
     return img, img_features
Beispiel #7
0
    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)
Beispiel #8
0
 def test_multi_gaussian_lens(self):
     kwargs_options = {'lens_model_list': ['SPEP']}
     lensModel = LensModel(**kwargs_options)
     lensAnalysis = LensProfileAnalysis(lens_model=lensModel)
     e1, e2 = param_util.phi_q2_ellipticity(0, 0.9)
     kwargs_lens = [{
         'gamma': 1.8,
         'theta_E': 0.6,
         'e1': e1,
         'e2': e2,
         'center_x': 0.5,
         'center_y': -0.1
     }]
     amplitudes, sigmas, center_x, center_y = lensAnalysis.multi_gaussian_lens(
         kwargs_lens, n_comp=20)
     model = MultiGaussianKappa()
     x = np.logspace(-2, 0.5, 10) + 0.5
     y = np.zeros_like(x) - 0.1
     f_xx, fxy, fyx, f_yy = model.hessian(x,
                                          y,
                                          amplitudes,
                                          sigmas,
                                          center_x=0.5,
                                          center_y=-0.1)
     kappa_mge = (f_xx + f_yy) / 2
     kappa_true = lensAnalysis._lens_model.kappa(x, y, kwargs_lens)
     print(kappa_true / kappa_mge)
     for i in range(len(x)):
         npt.assert_almost_equal(kappa_mge[i] / kappa_true[i], 1, decimal=1)
Beispiel #9
0
    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)
Beispiel #10
0
    def __init__(self, x_image, y_image, z_lens, z_source, lens_model_list, redshift_list,
                 astropy_instance, param_class, foreground_rays,
                 tol_source=1e-5, numerical_alpha_class=None):

        """

        :param x_image: x_image to fit
        :param y_image: y_image to fit
        :param z_lens: lens redshift
        :param z_source: source redshift
        :param lens_model_list: list of lens models
        :param redshift_list: list of lens redshifts
        :param astropy_instance: instance of astropy to pass to lens model
        :param param_class: an instance of ParamClass (see documentation in QuadOptimmizer.param_manager)
        :param foreground_rays: (optional) pre-computed foreground rays from a previous iteration, if they are not specified
        they will be re-computed
        :param tol_source: source plane chi^2 sigma
        :param numerical_alpha_class: class for computing numerically tabulated deflection angles
        """

        self.lensModel = LensModel(lens_model_list, z_lens, z_source, redshift_list, astropy_instance,
                                   multi_plane=True, numerical_alpha_class=numerical_alpha_class)

        lensmodel_list_to_vary = lens_model_list[0:param_class.to_vary_index]
        redshift_list_to_vary = redshift_list[0:param_class.to_vary_index]
        lensmodel_list_fixed = lens_model_list[param_class.to_vary_index:]
        redshift_list_fixed = redshift_list[param_class.to_vary_index:]

        self.lens_model_to_vary = LensModel(lensmodel_list_to_vary, z_lens, z_source, redshift_list_to_vary,
                                       cosmo=astropy_instance, multi_plane=True,
                                       numerical_alpha_class=numerical_alpha_class)

        self.lens_model_fixed = LensModel(lensmodel_list_fixed, z_lens, z_source, redshift_list_fixed,
                                            cosmo=astropy_instance, multi_plane=True,
                                            numerical_alpha_class=numerical_alpha_class)

        self._z_lens = z_lens

        self._z_source = z_source

        self._x_image = x_image
        self._y_image = y_image
        self._param_class = param_class

        self._tol_source = tol_source

        self._foreground_rays = foreground_rays
Beispiel #11
0
 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)
Beispiel #12
0
    def test_update_lens_model(self):
        self.base.update_lens_model(lens_model_class=None)
        assert self.base._solver is None

        base = PSBase()
        base.update_lens_model(lens_model_class=LensModel(lens_model_list=['SIS']))
        assert base._solver is not None
        PSBase(fixed_magnification=True, additional_image=True)
Beispiel #13
0
 def test_subtract(self):
     lensModel = LensModel(['SPEP'])
     solver_spep_center = Solver2Point(lensModel, solver_type='CENTER')
     x_cat = np.array([0, 0])
     y_cat = np.array([1, 2])
     a = solver_spep_center._subtract_constraint(x_cat, y_cat)
     assert a[0] == 0
     assert a[1] == 1
 def setup(self):
     lens_model = LensModel(lens_model_list=['SIS'])
     self.kwargs_lens = [{'theta_E': 1, 'center_x': 0, 'center_y': 0}]
     self.ps_mag = LensedPositions(lens_model=lens_model, fixed_magnification=True)
     self.ps = LensedPositions(lens_model=lens_model, fixed_magnification=False)
     self.ps_add = LensedPositions(lens_model=lens_model, fixed_magnification=[False], additional_image=True)
     self.kwargs = {'point_amp': [2, 1], 'ra_image': [0, 1.2], 'dec_image': [0, 0]}
     self.kwargs_mag = {'source_amp': 2, 'ra_image': [0, 1.2], 'dec_image': [0, 0]}
Beispiel #15
0
 def test_external_shear_direction(self):
     lensModel = LensModel(lens_model_list=['SHEAR'])
     kwargs_lens = [{'e1': 0.1, 'e2': -0.1}, {'e1': 0.1, 'e2': -0.1}]
     f, ax = output_plots.ext_shear_direction(data_class=self.data_class,
                                              lens_model_class=lensModel,
                                              kwargs_lens=kwargs_lens,
                                              strength_multiply=10)
     plt.close()
Beispiel #16
0
 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 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
        }, {
            'e1': 0.06,
            'e2': -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
Beispiel #18
0
    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)
Beispiel #19
0
    def test_profile_slope(self):
        lens_model = LensModelExtensions(LensModel(lens_model_list=['SPP']))
        gamma_in = 2.
        kwargs_lens = [{
            'theta_E': 1.,
            'gamma': gamma_in,
            'center_x': 0,
            'center_y': 0
        }]
        gamma_out = lens_model.profile_slope(kwargs_lens)
        npt.assert_array_almost_equal(gamma_out, gamma_in, decimal=3)
        gamma_in = 1.7
        kwargs_lens = [{
            'theta_E': 1.,
            'gamma': gamma_in,
            'center_x': 0,
            'center_y': 0
        }]
        gamma_out = lens_model.profile_slope(kwargs_lens)
        npt.assert_array_almost_equal(gamma_out, gamma_in, decimal=3)

        gamma_in = 2.5
        kwargs_lens = [{
            'theta_E': 1.,
            'gamma': gamma_in,
            'center_x': 0,
            'center_y': 0
        }]
        gamma_out = lens_model.profile_slope(kwargs_lens)
        npt.assert_array_almost_equal(gamma_out, gamma_in, decimal=3)

        lens_model = LensModelExtensions(LensModel(lens_model_list=['SPEP']))
        gamma_in = 2.
        phi, q = 0.34403343049704888, 0.89760957136967312
        e1, e2 = param_util.phi_q2_ellipticity(phi, q)
        kwargs_lens = [{
            'theta_E': 1.4516812130749424,
            'e1': e1,
            'e2': e2,
            'center_x': -0.04507598845306314,
            'center_y': 0.054491803177414651,
            'gamma': gamma_in
        }]
        gamma_out = lens_model.profile_slope(kwargs_lens)
        npt.assert_array_almost_equal(gamma_out, gamma_in, decimal=3)
    def test_assertions(self):
        lensModel = LensModel(['SPEP'])
        lensEquationSolver = LensEquationSolver(lensModel)
        kwargs_lens = [{
            'theta_E': 1,
            'gamma': 2,
            'e1': 0.2,
            'e2': -0.03,
            'center_x': 0,
            'center_y': 0
        }]
        with pytest.raises(ValueError):
            lensEquationSolver.image_position_from_source(0.1,
                                                          0.,
                                                          kwargs_lens,
                                                          solver='analytical')

        lensModel = LensModel(['EPL_NUMBA', 'SHEAR'])
        lensEquationSolver = LensEquationSolver(lensModel)
        kwargs_lens = [{
            'theta_E': 1.,
            'gamma': 2.2,
            'center_x': 0.0,
            'center_y': 0.0,
            'e1': 0.01,
            'e2': 0.05
        }, {
            'gamma1': -0.04,
            'gamma2': -0.1,
            'ra_0': 0.0,
            'dec_0': 0.0
        }]

        with pytest.raises(ValueError):
            lensEquationSolver.image_position_from_source(0.1,
                                                          0.,
                                                          kwargs_lens,
                                                          solver='nonexisting')

        with pytest.raises(ValueError):
            kwargs_lens[1]['ra_0'] = 0.1
            lensEquationSolver.image_position_from_source(0.1,
                                                          0.,
                                                          kwargs_lens,
                                                          solver='analytical')
Beispiel #21
0
    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)
Beispiel #22
0
 def setup(self):
     lens_model = LensModel(lens_model_list=['SIS'])
     self.kwargs_lens = [{'theta_E': 1, 'center_x': 0, 'center_y': 0}]
     self.ps_mag = SourcePositions(lens_model=lens_model,
                                   fixed_magnification=True)
     self.ps = SourcePositions(lens_model=lens_model,
                               fixed_magnification=False)
     self.kwargs = {'point_amp': [2, 1], 'ra_source': 0.1, 'dec_source': 0}
     self.kwargs_mag = {'source_amp': 1, 'ra_source': 0.1, 'dec_source': 0}
Beispiel #23
0
    def test_image_position(self):
        x_img, y_img = self.ps.image_position(self.kwargs, self.kwargs_lens)

        lens_model = LensModel(lens_model_list=['SIS'])
        x_src, y_src = lens_model.ray_shooting(x_img,
                                               y_img,
                                               kwargs=self.kwargs_lens)
        npt.assert_almost_equal(x_src, self.kwargs['ra_source'])
        npt.assert_almost_equal(y_src, self.kwargs['dec_source'])
 def test_flexion(self):
     lensModel = LensModel(lens_model_list=['FLEXION'])
     g1, g2, g3, g4 = 0.01, 0.02, 0.03, 0.04
     kwargs = [{'g1': g1, 'g2': g2, 'g3': g3, 'g4': g4}]
     f_xxx, f_xxy, f_xyy, f_yyy = lensModel.flexion(x=1., y=1., kwargs=kwargs)
     npt.assert_almost_equal(f_xxx, g1, decimal=8)
     npt.assert_almost_equal(f_xxy, g2, decimal=8)
     npt.assert_almost_equal(f_xyy, g3, decimal=8)
     npt.assert_almost_equal(f_yyy, g4, decimal=8)
    def test_magnification(self):
        ra_0, dec_0 = 1, -1

        flex = LensModel(['FLEXION'])
        g1, g2, g3, g4 = 0.01, 0.02, 0.03, 0.04

        kwargs = {'g1': g1, 'g2': g2, 'g3': g3, 'g4': g4, 'ra_0': ra_0, 'dec_0': dec_0}
        mag = flex.magnification(ra_0, dec_0, [kwargs])
        npt.assert_almost_equal(mag, 1, decimal=8)
 def test_flexion(self):
     x = np.array(0)
     y = np.array(2)
     flex = LensModel(['FLEXION'])
     f_xxx, f_xxy, f_xyy, f_yyy = flex.flexion(x, y, [self.kwargs_lens])
     npt.assert_almost_equal(f_xxx, self.kwargs_lens['g1'], decimal=9)
     npt.assert_almost_equal(f_xxy, self.kwargs_lens['g2'], decimal=9)
     npt.assert_almost_equal(f_xyy, self.kwargs_lens['g3'], decimal=9)
     npt.assert_almost_equal(f_yyy, self.kwargs_lens['g4'], decimal=9)
Beispiel #27
0
 def setup(self):
     self.lensModel = LensModel(['GAUSSIAN'])
     self.kwargs = [{
         'amp': 1.,
         'sigma_x': 2.,
         'sigma_y': 2.,
         'center_x': 0.,
         'center_y': 0.
     }]
Beispiel #28
0
 def test_init(self):
     lens_model_list = [
         'FLEXION', 'SIS_TRUNCATED', 'SERSIC', 'SERSIC_ELLIPSE',
         'SERSIC_DOUBLE', 'COMPOSITE', 'PJAFFE', 'PJAFFE_ELLIPSE',
         'HERNQUIST_ELLIPSE', 'INTERPOL', 'INTERPOL_SCALED',
         'SHAPELETS_POLAR', 'DIPOLE'
     ]
     lensModel = LensModel(lens_model_list)
     assert len(lensModel.lens_model_list) == len(lens_model_list)
Beispiel #29
0
    def assert_differentials(self, lens_model, kwargs, potential=True):
        #lensModelNum = NumericLens(lens_model)
        diff = 0.000001
        #x, y = 1., 2.

        x = np.linspace(start=0.1, stop=5.5, num=10)
        y = np.zeros_like(x)

        lensModel = LensModel(lens_model)
        f_xx, f_xy, f_yx, f_yy = lensModel.hessian(x, y, [kwargs])
        f_xx_num, f_xy_num, f_yx_num, f_yy_num = lensModel.hessian(x,
                                                                   y, [kwargs],
                                                                   diff=diff)

        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)

        if potential is True:
            f_x, f_y = lensModel.alpha(x, y, [kwargs])
            f_x_num, f_y_num = lensModel.alpha(x, y, [kwargs], diff=diff)

            npt.assert_almost_equal(f_x, f_x_num, decimal=3)
            npt.assert_almost_equal(f_y, f_y_num, decimal=3)

        y = np.linspace(start=0.1, stop=5.5, num=10)
        x = np.zeros_like(y)

        lensModel = LensModel(lens_model)
        f_xx, f_xy, f_yx, f_yy = lensModel.hessian(x, y, [kwargs])
        f_xx_num, f_xy_num, f_yx_num, f_yy_num = lensModel.hessian(x,
                                                                   y, [kwargs],
                                                                   diff=diff)

        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)

        if potential is True:
            f_x, f_y = lensModel.alpha(x, y, [kwargs])
            f_x_num, f_y_num = lensModel.alpha(x, y, [kwargs], diff=diff)

            npt.assert_almost_equal(f_x, f_x_num, decimal=3)
            npt.assert_almost_equal(f_y, f_y_num, decimal=3)
Beispiel #30
0
    def __init__(self,
                 data_class,
                 psf_class,
                 lens_model_class=None,
                 source_model_class=None,
                 lens_light_model_class=None,
                 point_source_class=None,
                 extinction_class=None,
                 kwargs_numerics=None):
        """
        :param data_class: instance of ImageData() or PixelGrid() class
        :param psf_class: instance of PSF() class
        :param lens_model_class: instance of LensModel() class
        :param source_model_class: instance of LightModel() class describing the source parameters
        :param lens_light_model_class: instance of LightModel() class describing the lens light parameters
        :param point_source_class: instance of PointSource() class describing the point sources
        :param kwargs_numerics: keyword argument with various numeric description (see ImageNumerics class for options)
        """
        self.type = 'single-band'
        self.PSF = psf_class
        self.Data = data_class
        self.PSF.set_pixel_size(self.Data.pixel_width)
        if kwargs_numerics is None:
            kwargs_numerics = {}
        self.ImageNumerics = NumericsSubFrame(pixel_grid=self.Data,
                                              psf=self.PSF,
                                              **kwargs_numerics)
        if lens_model_class is None:
            lens_model_class = LensModel(lens_model_list=[])
        self.LensModel = lens_model_class
        if point_source_class is None:
            point_source_class = PointSource(point_source_type_list=[])
        self.PointSource = point_source_class
        self.PointSource.update_lens_model(lens_model_class=lens_model_class)
        x_center, y_center = self.Data.center
        self.PointSource.update_search_window(
            search_window=np.max(self.Data.width),
            x_center=x_center,
            y_center=y_center,
            min_distance=self.Data.pixel_width,
            only_from_unspecified=True)
        self._psf_error_map = self.PSF.psf_error_map_bool

        if source_model_class is None:
            source_model_class = LightModel(light_model_list=[])
        self.SourceModel = source_model_class
        if lens_light_model_class is None:
            lens_light_model_class = LightModel(light_model_list=[])
        self.LensLightModel = lens_light_model_class
        self.source_mapping = Image2SourceMapping(
            lensModel=lens_model_class, sourceModel=source_model_class)
        self.num_bands = 1
        self._kwargs_numerics = kwargs_numerics
        if extinction_class is None:
            extinction_class = DifferentialExtinction(optical_depth_model=[])
        self._extinction = extinction_class