def assert_differentials(self, lens_model, kwargs):
        lensModelNum = NumericLens(lens_model)
        x, y = 1., 2.
        lensModel = LensModel(lens_model)
        f_x, f_y = lensModel.lens_model.alpha(x, y, [kwargs])
        f_xx, f_xy, f_yy = lensModel.hessian(x, y, [kwargs])
        f_x_num, f_y_num = lensModelNum.lens_model.alpha(x, y, [kwargs])
        f_xx_num, f_xy_num, f_yx_num, f_yy_num = lensModelNum.hessian(
            x, y, [kwargs])
        print(f_xx_num, f_xx)
        print(f_yy_num, f_yy)
        print(f_xy_num, f_xy)
        print((f_xx - f_yy)**2 / 4 + f_xy**2,
              (f_xx_num - f_yy_num)**2 / 4 + f_xy_num**2)
        npt.assert_almost_equal(f_x, f_x_num, decimal=5)
        npt.assert_almost_equal(f_y, f_y_num, decimal=5)
        npt.assert_almost_equal(f_xx, f_xx_num, decimal=3)
        npt.assert_almost_equal(f_yy, f_yy_num, decimal=3)
        npt.assert_almost_equal(f_xy, f_xy_num, decimal=3)

        x, y = 1., 0.
        f_x, f_y = lensModel.lens_model.alpha(x, y, [kwargs])
        f_xx, f_xy, f_yy = lensModel.hessian(x, y, [kwargs])
        f_x_num, f_y_num = lensModelNum.lens_model.alpha(x, y, [kwargs])
        f_xx_num, f_xy_num, f_yx_num, f_yy_num = lensModelNum.hessian(
            x, y, [kwargs])
        print(f_xx_num, f_xx)
        print(f_yy_num, f_yy)
        print(f_xy_num, f_xy)
        print((f_xx - f_yy)**2 / 4 + f_xy**2,
              (f_xx_num - f_yy_num)**2 / 4 + f_xy_num**2)
        npt.assert_almost_equal(f_x, f_x_num, decimal=5)
        npt.assert_almost_equal(f_y, f_y_num, decimal=5)
        npt.assert_almost_equal(f_xx, f_xx_num, decimal=3)
        npt.assert_almost_equal(f_yy, f_yy_num, decimal=3)
        npt.assert_almost_equal(f_xy, f_xy_num, decimal=3)
    def assert_differentials(self, lens_model, kwargs):
        lensModelNum = NumericLens(lens_model)
        lensModelNum.diff = 0.000001
        #x, y = 1., 2.
        x = np.linspace(start=0.1, stop=8, num=10)
        y = np.zeros_like(x)
        lensModel = LensModel(lens_model)
        f_x, f_y = lensModel.lens_model.alpha(x, y, [kwargs])
        f_xx, f_xy, f_yx, f_yy = lensModel.hessian(x, y, [kwargs])
        f_x_num, f_y_num = lensModelNum.lens_model.alpha(x, y, [kwargs])
        f_xx_num, f_xy_num, f_yx_num, f_yy_num = lensModelNum.hessian(
            x, y, [kwargs])
        print(f_xx_num, f_xx)
        print(f_yy_num, f_yy)
        print(f_xy_num, f_xy)
        npt.assert_almost_equal(f_x, f_x_num, decimal=5)
        npt.assert_almost_equal(f_y, f_y_num, decimal=5)
        npt.assert_almost_equal(f_xx, f_xx_num, decimal=3)
        npt.assert_almost_equal(f_yy, f_yy_num, decimal=3)
        npt.assert_almost_equal(f_xy, f_xy_num, decimal=3)

        x, y = 1., 0.
        f_x, f_y = lensModel.lens_model.alpha(x, y, [kwargs])
        f_xx, f_xy, f_yx, f_yy = lensModel.hessian(x, y, [kwargs])
        f_x_num, f_y_num = lensModelNum.lens_model.alpha(x, y, [kwargs])
        f_xx_num, f_xy_num, f_yx_num, f_yy_num = lensModelNum.hessian(
            x, y, [kwargs])
        print(f_xx_num, f_xx)
        print(f_yy_num, f_yy)
        print(f_xy_num, f_xy)
        print(f_xx_num + f_yy_num, f_xx + f_yy)
        npt.assert_almost_equal(f_x, f_x_num, decimal=5)
        npt.assert_almost_equal(f_y, f_y_num, decimal=5)
        npt.assert_almost_equal(f_xx, f_xx_num, decimal=3)
        npt.assert_almost_equal(f_yy, f_yy_num, decimal=3)
        npt.assert_almost_equal(f_xy, f_xy_num, decimal=3)
class TestNumerics(object):
    """
    tests the source model routines
    """
    def setup(self):
        self.lensModel = LensModel(['GAUSSIAN'])
        self.lensModelNum = NumericLens(['GAUSSIAN'])
        self.kwargs = [{
            'amp': 1. / 4.,
            'sigma_x': 2.,
            'sigma_y': 2.,
            'center_x': 0.,
            'center_y': 0.
        }]

    def test_kappa(self):
        x, y = 1., 1.
        output = self.lensModel.kappa(x, y, self.kwargs)
        output_num = self.lensModelNum.kappa(x, y, self.kwargs)
        npt.assert_almost_equal(output_num, output, decimal=5)

    def test_gamma(self):
        x, y = 1., 2.
        output1, output2 = self.lensModel.gamma(x, y, self.kwargs)
        output1_num, output2_num = self.lensModelNum.gamma(x, y, self.kwargs)
        npt.assert_almost_equal(output1_num, output1, decimal=5)
        npt.assert_almost_equal(output2_num, output2, decimal=5)

    def test_magnification(self):
        x, y = 1., 1.
        output = self.lensModel.magnification(x, y, self.kwargs)
        output_num = self.lensModelNum.magnification(x, y, self.kwargs)
        npt.assert_almost_equal(output_num, output, decimal=5)

    def test_differentials(self):
        x, y = 1., 1.
        f_xx, f_xy, f_yx, f_yy = self.lensModel.hessian(x, y, self.kwargs)
        f_xx_num, f_xy_num, f_yx_num, f_yy_num = self.lensModelNum.hessian(
            x, y, self.kwargs)
        assert f_xy_num == f_yx_num
        npt.assert_almost_equal(f_xx_num, f_xx, decimal=5)
        npt.assert_almost_equal(f_xy_num, f_xy, decimal=5)
        npt.assert_almost_equal(f_yx_num, f_yx, decimal=5)
        npt.assert_almost_equal(f_yy_num, f_yy, decimal=5)
Exemplo n.º 4
0
    def light2mass_interpol(lens_light_model_list,
                            kwargs_lens_light,
                            numPix=100,
                            deltaPix=0.05,
                            subgrid_res=5,
                            center_x=0,
                            center_y=0):
        """
        takes a lens light model and turns it numerically in a lens model
        (with all lensmodel quantities computed on a grid). Then provides an interpolated grid for the quantities.

        :param kwargs_lens_light: lens light keyword argument list
        :param numPix: number of pixels per axis for the return interpolation
        :param deltaPix: interpolation/pixel size
        :param center_x: center of the grid
        :param center_y: center of the grid
        :param subgrid: subgrid for the numerical integrals
        :return:
        """
        # make sugrid
        x_grid_sub, y_grid_sub = util.make_grid(numPix=numPix * 5,
                                                deltapix=deltaPix,
                                                subgrid_res=subgrid_res)
        import lenstronomy.Util.mask as mask_util
        mask = mask_util.mask_sphere(x_grid_sub,
                                     y_grid_sub,
                                     center_x,
                                     center_y,
                                     r=1)
        x_grid, y_grid = util.make_grid(numPix=numPix, deltapix=deltaPix)
        # compute light on the subgrid
        lightModel = LightModel(light_model_list=lens_light_model_list)
        flux = lightModel.surface_brightness(x_grid_sub, y_grid_sub,
                                             kwargs_lens_light)
        flux_norm = np.sum(flux[mask == 1]) / np.sum(mask)
        flux /= flux_norm
        from lenstronomy.LensModel.numerical_profile_integrals import ConvergenceIntegrals
        integral = ConvergenceIntegrals()

        # compute lensing quantities with subgrid
        convergence_sub = flux
        f_x_sub, f_y_sub = integral.deflection_from_kappa(convergence_sub,
                                                          x_grid_sub,
                                                          y_grid_sub,
                                                          deltaPix=deltaPix /
                                                          float(subgrid_res))
        f_sub = integral.potential_from_kappa(convergence_sub,
                                              x_grid_sub,
                                              y_grid_sub,
                                              deltaPix=deltaPix /
                                              float(subgrid_res))
        # interpolation function on lensing quantities
        x_axes_sub, y_axes_sub = util.get_axes(x_grid_sub, y_grid_sub)
        from lenstronomy.LensModel.Profiles.interpol import Interpol_func
        interp_func = Interpol_func()
        interp_func.do_interp(x_axes_sub, y_axes_sub, f_sub, f_x_sub, f_y_sub)
        # compute lensing quantities on sparser grid
        x_axes, y_axes = util.get_axes(x_grid, y_grid)
        f_ = interp_func.function(x_grid, y_grid)
        f_x, f_y = interp_func.derivatives(x_grid, y_grid)
        # numerical differentials for second order differentials
        from lenstronomy.LensModel.numeric_lens_differentials import NumericLens
        lens_differential = NumericLens(lens_model_list=['INTERPOL'])
        kwargs = [{
            'grid_interp_x': x_axes_sub,
            'grid_interp_y': y_axes_sub,
            'f_': f_sub,
            'f_x': f_x_sub,
            'f_y': f_y_sub
        }]
        f_xx, f_xy, f_yx, f_yy = lens_differential.hessian(
            x_grid, y_grid, kwargs)
        kwargs_interpol = {
            'grid_interp_x': x_axes,
            'grid_interp_y': y_axes,
            'f_': util.array2image(f_),
            'f_x': util.array2image(f_x),
            'f_y': util.array2image(f_y),
            'f_xx': util.array2image(f_xx),
            'f_xy': util.array2image(f_xy),
            'f_yy': util.array2image(f_yy)
        }
        return kwargs_interpol