class TestGaussianKappa(object):
    """
    test the Gaussian with Gaussian kappa
    """
    def setup(self):
        self.gaussian_kappa = MultiGaussianKappa()
        self.gaussian = Gaussian()
        self.g_kappa = GaussianKappa()

    def test_derivatives(self):
        x = np.linspace(0, 5, 10)
        y = np.linspace(0, 5, 10)
        amp = [1. * 2 * np.pi]
        center_x = 0.
        center_y = 0.
        sigma = [1.]
        f_x, f_y = self.gaussian_kappa.derivatives(x, y, amp, sigma, center_x,
                                                   center_y)
        npt.assert_almost_equal(f_x[2], 0.63813558702212059, decimal=8)
        npt.assert_almost_equal(f_y[2], 0.63813558702212059, decimal=8)

    def test_hessian(self):
        x = np.linspace(0, 5, 10)
        y = np.linspace(0, 5, 10)
        amp = [1. * 2 * np.pi]
        center_x = 0.
        center_y = 0.
        sigma = [1.]
        f_xx, f_yy, f_xy = self.gaussian_kappa.hessian(x, y, amp, sigma,
                                                       center_x, center_y)
        kappa = 1. / 2 * (f_xx + f_yy)
        kappa_true = self.gaussian.function(x, y, amp[0], sigma[0], sigma[0],
                                            center_x, center_y)
        print(kappa_true)
        print(kappa)
        npt.assert_almost_equal(kappa[0], kappa_true[0], decimal=5)
        npt.assert_almost_equal(kappa[1], kappa_true[1], decimal=5)

    def test_density_2d(self):
        x = np.linspace(0, 5, 10)
        y = np.linspace(0, 5, 10)
        amp = [1. * 2 * np.pi]
        center_x = 0.
        center_y = 0.
        sigma = [1.]
        f_xx, f_yy, f_xy = self.gaussian_kappa.hessian(x, y, amp, sigma,
                                                       center_x, center_y)
        kappa = 1. / 2 * (f_xx + f_yy)
        amp_3d = self.g_kappa._amp2d_to_3d(amp, sigma[0], sigma[0])
        density_2d = self.gaussian_kappa.density_2d(x, y, amp_3d, sigma,
                                                    center_x, center_y)
        npt.assert_almost_equal(kappa[1], density_2d[1], decimal=5)
        npt.assert_almost_equal(kappa[2], density_2d[2], decimal=5)

    def test_density(self):
        amp = [1. * 2 * np.pi]

        sigma = [1.]
        density = self.gaussian_kappa.density(1., amp, sigma)
        npt.assert_almost_equal(density, 0.6065306597126334, decimal=8)
Exemplo n.º 2
0
 def test_multi_gaussian_lens(self):
     kwargs_options = {'lens_model_list': ['SPEP']}
     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
     }]
     lensAnalysis = LensAnalysis(kwargs_options)
     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, f_yy, fxy = model.hessian(x,
                                     y,
                                     amplitudes,
                                     sigmas,
                                     center_x=0.5,
                                     center_y=-0.1)
     kappa_mge = (f_xx + f_yy) / 2
     kappa_true = lensAnalysis.LensModel.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)
Exemplo n.º 3
0
    def test_spemd(self):
        from lenstronomy.LensModel.Profiles.spep import SPEP
        from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappa
        spep = SPEP()
        mge_kappa = MultiGaussianKappa()
        n_comp = 8
        theta_E = 1.41
        kwargs = {'theta_E': theta_E, 'e1': 0, 'e2': 0, 'gamma': 1.61}
        rs = np.logspace(-2., 1., 100) * theta_E
        f_xx, f_yy, f_xy = spep.hessian(rs, 0, **kwargs)
        kappa = 1 / 2. * (f_xx + f_yy)
        amplitudes, sigmas, norm = mge.mge_1d(rs, kappa, N=n_comp)
        kappa_mge = self.multiGaussian.function(rs,
                                                np.zeros_like(rs),
                                                amp=amplitudes,
                                                sigma=sigmas)
        f_xx_mge, f_yy_mge, f_xy_mge = mge_kappa.hessian(rs,
                                                         np.zeros_like(rs),
                                                         amp=amplitudes,
                                                         sigma=sigmas)
        for i in range(0, 80):
            npt.assert_almost_equal(kappa_mge[i],
                                    1. / 2 * (f_xx_mge[i] + f_yy_mge[i]),
                                    decimal=1)
            npt.assert_almost_equal((kappa[i] - kappa_mge[i]) / kappa[i],
                                    0,
                                    decimal=1)

        f_ = spep.function(theta_E, 0, **kwargs)
        f_mge = mge_kappa.function(theta_E, 0, sigma=sigmas, amp=amplitudes)
        npt.assert_almost_equal(f_mge / f_, 1, decimal=2)
    def test_nfw_sersic(self):
        kwargs_lens_nfw = {'alpha_Rs': 1.4129647849966354, 'Rs': 7.0991113634274736}
        kwargs_lens_sersic = {'k_eff': 0.24100561407593576, 'n_sersic': 1.8058507329346063, 'R_sersic': 1.0371803141813705}
        from lenstronomy.LensModel.Profiles.nfw import NFW
        from lenstronomy.LensModel.Profiles.sersic import Sersic
        nfw = NFW()
        sersic = Sersic()
        theta_E = 1.5
        n_comp = 10
        rs = np.logspace(-2., 1., 100) * theta_E
        f_xx_nfw, f_xy_nfw, f_yx_nfw, f_yy_nfw = nfw.hessian(rs, 0, **kwargs_lens_nfw)
        f_xx_s, f_xy_s, f_yx_s, f_yy_s = sersic.hessian(rs, 0, **kwargs_lens_sersic)
        kappa = 1 / 2. * (f_xx_nfw + f_xx_s + f_yy_nfw + f_yy_s)
        amplitudes, sigmas, norm = mge.mge_1d(rs, kappa, N=n_comp)
        kappa_mge = self.multiGaussian.function(rs, np.zeros_like(rs), amp=amplitudes, sigma=sigmas)
        from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappa
        mge_kappa = MultiGaussianKappa()
        f_xx_mge, f_xy_mge, f_yx_mge, f_yy_mge = mge_kappa.hessian(rs, np.zeros_like(rs), amp=amplitudes, sigma=sigmas)
        for i in range(0, 80):
            npt.assert_almost_equal(kappa_mge[i], 1. / 2 * (f_xx_mge[i] + f_yy_mge[i]), decimal=1)
            npt.assert_almost_equal((kappa[i] - kappa_mge[i]) / kappa[i], 0, decimal=1)

        f_nfw = nfw.function(theta_E, 0, **kwargs_lens_nfw)
        f_s = sersic.function(theta_E, 0, **kwargs_lens_sersic)
        f_mge = mge_kappa.function(theta_E, 0, sigma=sigmas, amp=amplitudes)
        npt.assert_almost_equal(f_mge / (f_nfw + f_s), 1, decimal=2)