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)
Beispiel #2
0
class TestSersic(object):
    """
    tests the Gaussian methods
    """
    def setup(self):
        self.sersic = Sersic(smoothing=0.02)
        self.sersic_elliptic = Sersic_elliptic(smoothing=0.02)
        self.double_sersic = DoubleSersic(smoothing=0.02)
        self.core_sersic = CoreSersic(smoothing=0.02)
        self.double_core_sersic = DoubleCoreSersic(smoothing=0.02)
        self.buldge_disk = BuldgeDisk(smoothing=0.02)

    def test_sersic(self):
        x = np.array([1])
        y = np.array([2])
        I0_sersic = 1
        R_sersic = 1
        n_sersic = 1
        center_x = 0
        center_y = 0
        values = self.sersic.function(x, y, I0_sersic, R_sersic, n_sersic, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.12658651833626802, decimal=6)
        x = np.array([0])
        y = np.array([0])
        values = self.sersic.function( x, y, I0_sersic, R_sersic, n_sersic, center_x, center_y)
        npt.assert_almost_equal(values[0],  5.1482559148107292, decimal=2)

        x = np.array([2,3,4])
        y = np.array([1,1,1])
        values = self.sersic.function( x, y, I0_sersic, R_sersic, n_sersic, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.12658651833626802, decimal=6)
        npt.assert_almost_equal(values[1], 0.026902273598180083, decimal=6)
        npt.assert_almost_equal(values[2], 0.0053957432862338055, decimal=6)

    def test_symmetry_r_sersic(self):
        x = np.array([2,3,4])
        y = np.array([1,1,1])
        I0_sersic = 1
        R_sersic1 = 1
        R_sersic2 = 0.1
        n_sersic = 1
        center_x = 0
        center_y = 0
        values1 = self.sersic.function(x*R_sersic1, y*R_sersic1, I0_sersic, R_sersic1, n_sersic, center_x, center_y)
        values2 = self.sersic.function(x*R_sersic2, y*R_sersic2, I0_sersic, R_sersic2, n_sersic, center_x, center_y)
        npt.assert_almost_equal(values1[0], values2[0], decimal=6)
        npt.assert_almost_equal(values1[1], values2[1], decimal=6)
        npt.assert_almost_equal(values1[2], values2[2], decimal=6)

    def test_sersic_center(self):
        x = 0.01
        y = 0.
        I0_sersic = 1
        R_sersic = 0.1
        n_sersic = 4.
        center_x = 0
        center_y = 0
        values = self.sersic.function(x, y, I0_sersic, R_sersic, n_sersic, center_x, center_y)
        npt.assert_almost_equal(values, 12.688073819377406, decimal=6)

    def test_sersic_elliptic(self):
        x = np.array([1])
        y = np.array([2])
        I0_sersic = 1
        R_sersic = 1
        n_sersic = 1
        phi_G = 1
        q = 0.9
        center_x = 0
        center_y = 0
        values = self.sersic_elliptic.function(x, y, I0_sersic, R_sersic, n_sersic, phi_G, q, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.12595366113005077, decimal=6)
        x = np.array([0])
        y = np.array([0])
        values = self.sersic_elliptic.function(x, y, I0_sersic, R_sersic, n_sersic, phi_G, q, center_x, center_y)
        npt.assert_almost_equal(values[0], 5.1482553482055664, decimal=2)

        x = np.array([2,3,4])
        y = np.array([1,1,1])
        values = self.sersic_elliptic.function(x, y, I0_sersic, R_sersic, n_sersic, phi_G, q, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.11308277793465012, decimal=6)
        npt.assert_almost_equal(values[1], 0.021188620675507107, decimal=6)
        npt.assert_almost_equal(values[2], 0.0037276744362724477, decimal=6)

    def test_core_sersic(self):
        x = np.array([1])
        y = np.array([2])
        I0 = 1
        Rb = 1
        Re = 2
        gamma = 3
        n = 1
        phi_G = 1
        q = 0.9
        center_x = 0
        center_y = 0
        values = self.core_sersic.function(x, y, I0, Rb, Re, n, gamma, phi_G, q, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.84489101, decimal=8)
        x = np.array([0])
        y = np.array([0])
        values = self.core_sersic.function(x, y, I0, Rb, Re, n, gamma, phi_G, q, center_x, center_y)
        npt.assert_almost_equal(values[0], 288406.09, decimal=0)

        x = np.array([2,3,4])
        y = np.array([1,1,1])
        values = self.core_sersic.function(x, y, I0, Rb, Re, n, gamma, phi_G, q, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.79749529635325933, decimal=6)
        npt.assert_almost_equal(values[1], 0.33653478121594838, decimal=6)
        npt.assert_almost_equal(values[2], 0.14050402887681532, decimal=6)

    def test_double_sersic(self):
        x = np.array([1])
        y = np.array([2])
        I0_sersic = 1
        R_sersic = 1
        n_sersic = 1
        phi_G = 1
        q = 0.9
        I0_2 = 0.1
        R_2 = 2
        n_2 = 2
        phi_G_2 = 1
        q_2 = 1
        center_x = 0
        center_y = 0
        values = self.double_sersic.function(x, y, I0_sersic, R_sersic, n_sersic, phi_G, q, I0_2, R_2, n_2, phi_G_2, q_2, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.20696126663199443, decimal=8)
        x = np.array([0])
        y = np.array([0])
        values = self.double_sersic.function(x, y, I0_sersic, R_sersic, n_sersic, phi_G, q, I0_2, R_2, n_2, phi_G_2, q_2, center_x, center_y)
        npt.assert_almost_equal(values[0], 7.8708484172821045, decimal=8)

        x = np.array([2,3,4])
        y = np.array([1,1,1])
        values = self.double_sersic.function(x, y, I0_sersic, R_sersic, n_sersic, phi_G, q, I0_2, R_2, n_2, phi_G_2, q_2, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.19409037374964733, decimal=8)
        npt.assert_almost_equal(values[1], 0.060052096255106595, decimal=8)
        npt.assert_almost_equal(values[2], 0.023917479151437715, decimal=8)

    def test_double_sersic_function_split(self):
        x = np.array([1])
        y = np.array([2])
        I0_sersic = 1
        R_sersic = 1
        n_sersic = 1
        phi_G = 1
        q = 0.9
        I0_2 = 0.1
        R_2 = 2
        n_2 = 2
        phi_G_2 = 1
        q_2 = 1
        center_x = 0
        center_y = 0
        func_1, func_2 = self.double_sersic.function_split(x, y, I0_sersic, R_sersic, n_sersic, phi_G, q, I0_2, R_2, n_2, phi_G_2, q_2, center_x, center_y)
        npt.assert_almost_equal(func_1[0], 0.12595365941524506)
        npt.assert_almost_equal(func_2[0], 0.081007614731788635)

    def test_double_core_sersic(self):
        x = np.array([1])
        y = np.array([2])
        I0_sersic = 1
        R_sersic = 1
        n_sersic = 1
        phi_G = 1
        q = 0.9
        I0_2 = 0.1
        R_2 = 2
        n_2 = 2
        Re = 0.1
        gamma = 2
        values = self.double_core_sersic.function(x, y, I0_sersic, Re, R_sersic, n_sersic, gamma, phi_G, q, I0_2, R_2, n_2, phi_G_2=0, q_2=1, center_x=0, center_y=0)
        npt.assert_almost_equal(values[0], 0.20695476233959198, decimal=5)
        x = np.array([0])
        y = np.array([0])
        values = self.double_core_sersic.function(x, y, I0_sersic, Re, R_sersic, n_sersic, gamma, phi_G, q, I0_2, R_2, n_2, phi_G_2=0, q_2=1, center_x=0, center_y=0)
        npt.assert_almost_equal(values[0], 115.86341203723811, decimal=5)

        x = np.array([2,3,4])
        y = np.array([1,1,1])
        values = self.double_core_sersic.function(x, y, I0_sersic, Re, R_sersic, n_sersic, gamma, phi_G, q, I0_2, R_2, n_2, phi_G_2=0, q_2=1, center_x=0, center_y=0)
        npt.assert_almost_equal(values[0], 0.19408468241596671, decimal=5)
        npt.assert_almost_equal(values[1], 0.060051398351788521, decimal=5)
        npt.assert_almost_equal(values[2], 0.023917404236271977, decimal=5)

    def test_double_core_sersic_function_split(self):
        x = np.array([1])
        y = np.array([2])
        I0_sersic = 1
        R_sersic = 1
        n_sersic = 1
        phi_G = 1
        q = 0.9
        I0_2 = 0.1
        R_2 = 2
        n_2 = 2
        Re = 0.1
        gamma = 2
        func_1, func_2 = self.double_core_sersic.function_split(x, y, I0_sersic, Re, R_sersic, n_sersic, gamma, phi_G, q, I0_2, R_2, n_2, phi_G_2=0, q_2=1, center_x=0, center_y=0)
        npt.assert_almost_equal(func_1[0], 0.12594715)
        npt.assert_almost_equal(func_2[0], 0.081007614731788635)

    def test_buldge_disk(self):
        x = np.array([1])
        y = np.array([2])
        I0_b = 1
        R_b = 1
        phi_G_b = 0
        q_b = 0.9
        I0_d = 1
        R_d = 2
        phi_G_d = 0
        q_d = 0.7

        values = self.buldge_disk.function(x, y, I0_b, R_b, phi_G_b, q_b, I0_d, R_d, phi_G_d, q_d, center_x=0, center_y=0)
        npt.assert_almost_equal(values[0], 0.57135696709156036, decimal=8)
        x = np.array([0])
        y = np.array([0])
        values = self.buldge_disk.function(x, y, I0_b, R_b, phi_G_b, q_b, I0_d, R_d, phi_G_d, q_d, center_x=0, center_y=0)
        npt.assert_almost_equal(values[0], 124.98796224594116, decimal=8)

        x = np.array([2,3,4])
        y = np.array([1,1,1])
        values = self.buldge_disk.function(x, y, I0_b, R_b, phi_G_b, q_b, I0_d, R_d, phi_G_d, q_d, center_x=0, center_y=0)
        npt.assert_almost_equal(values[0], 0.85350380837917328, decimal=8)
        npt.assert_almost_equal(values[1], 0.40610484033823013, decimal=8)
        npt.assert_almost_equal(values[2], 0.19044427201151848, decimal=8)

    def test_buldge_disk_function_split(self):
        x = np.array([1])
        y = np.array([2])
        I0_b = 1
        R_b = 1
        phi_G_b = 0
        q_b = 0.9
        I0_d = 1
        R_d = 2
        phi_G_d = 0
        q_d = 0.7
        func_1, func_2 = self.buldge_disk.function_split(x, y, I0_b, R_b, phi_G_b, q_b, I0_d, R_d, phi_G_d, q_d, center_x=0, center_y=0)
        npt.assert_almost_equal(func_1[0], 0.1476433128118515)
        npt.assert_almost_equal(func_2[0], 0.42371365427970886)
Beispiel #3
0
class TestMGE(object):
    """
    tests the Gaussian methods
    """
    def setup(self):
        self.sersic = Sersic()
        self.multiGaussian = MultiGaussian()

    def test_mge_1d_sersic(self):
        n_comp = 30
        r_sersic = 1.
        n_sersic = 3.7
        I0_sersic = 1.
        rs = np.logspace(-2., 1., 50) * r_sersic
        ss = self.sersic.function(rs,
                                  np.zeros_like(rs),
                                  amp=I0_sersic,
                                  n_sersic=n_sersic,
                                  R_sersic=r_sersic)

        amplitudes, sigmas, norm = mge.mge_1d(rs, ss, N=n_comp)
        ss_mge = self.multiGaussian.function(rs,
                                             np.zeros_like(rs),
                                             amp=amplitudes,
                                             sigma=sigmas)
        #print((ss - ss_mge)/ss)
        for i in range(10, len(ss) - 10):
            #print(rs[i])
            npt.assert_almost_equal((ss_mge[i] - ss[i]) / ss[i], 0, decimal=1)

        amplitudes, sigmas, norm = mge.mge_1d(rs, np.zeros_like(rs), N=n_comp)
        assert amplitudes[0] == 0

        amplitudes, sigmas, norm = mge.mge_1d(rs, np.zeros_like(rs), N=0)
        assert amplitudes[0] == 0

    def test_mge_sersic_radius(self):
        n_comp = 30
        r_sersic = .5
        n_sersic = 3.7
        I0_sersic = 1.
        rs = np.logspace(-2., 1., 50) * r_sersic
        ss = self.sersic.function(rs,
                                  np.zeros_like(rs),
                                  amp=I0_sersic,
                                  n_sersic=n_sersic,
                                  R_sersic=r_sersic)

        amplitudes, sigmas, norm = mge.mge_1d(rs, ss, N=n_comp)
        ss_mge = self.multiGaussian.function(rs,
                                             np.zeros_like(rs),
                                             amp=amplitudes,
                                             sigma=sigmas)
        print((ss - ss_mge) / (ss + ss_mge))
        for i in range(10, len(ss) - 10):
            #print(rs[i])
            npt.assert_almost_equal((ss_mge[i] - ss[i]) / (ss[i]),
                                    0,
                                    decimal=1)

    def test_mge_sersic_n_sersic(self):
        n_comp = 20
        r_sersic = 1.5
        n_sersic = .5
        I0_sersic = 1.
        rs = np.logspace(-2., 1., 50) * r_sersic
        ss = self.sersic.function(rs,
                                  np.zeros_like(rs),
                                  amp=I0_sersic,
                                  n_sersic=n_sersic,
                                  R_sersic=r_sersic)

        amplitudes, sigmas, norm = mge.mge_1d(rs, ss, N=n_comp)
        ss_mge = self.multiGaussian.function(rs,
                                             np.zeros_like(rs),
                                             amp=amplitudes,
                                             sigma=sigmas)
        for i in range(10, len(ss) - 10):
            npt.assert_almost_equal((ss_mge[i] - ss[i]) / (ss[i] + ss_mge[i]),
                                    0,
                                    decimal=1)

        n_comp = 20
        r_sersic = 1.5
        n_sersic = 3.5
        I0_sersic = 1.
        rs = np.logspace(-2., 1., 50) * r_sersic
        ss = self.sersic.function(rs,
                                  np.zeros_like(rs),
                                  amp=I0_sersic,
                                  n_sersic=n_sersic,
                                  R_sersic=r_sersic)

        amplitudes, sigmas, norm = mge.mge_1d(rs, ss, N=n_comp)
        ss_mge = self.multiGaussian.function(rs,
                                             np.zeros_like(rs),
                                             amp=amplitudes,
                                             sigma=sigmas)
        for i in range(10, len(ss) - 10):
            npt.assert_almost_equal((ss_mge[i] - ss[i]) / (ss[i] + ss_mge[i]),
                                    0,
                                    decimal=1)

    def test_hernquist(self):
        hernquist = Hernquist()
        n_comp = 20
        sigma0 = 1
        r_eff = 1.5
        rs = np.logspace(-2., 1., 50) * r_eff * 0.5
        ss = hernquist.function(rs, np.zeros_like(rs), sigma0, Rs=r_eff)
        amplitudes, sigmas, norm = mge.mge_1d(rs, ss, N=n_comp)
        ss_mge = self.multiGaussian.function(rs,
                                             np.zeros_like(rs),
                                             amp=amplitudes,
                                             sigma=sigmas)
        for i in range(10, len(ss) - 10):
            npt.assert_almost_equal((ss_mge[i] - ss[i]) / (ss[i] + ss_mge[i]),
                                    0,
                                    decimal=2)

    def test_hernquist_deprojection(self):
        hernquist = Hernquist()
        n_comp = 20
        sigma0 = 1
        r_eff = 1.5
        rs = np.logspace(-2., 1., 50) * r_eff * 0.5
        ss = hernquist.function(rs, np.zeros_like(rs), sigma0, Rs=r_eff)
        amplitudes, sigmas, norm = mge.mge_1d(rs, ss, N=n_comp)
        amplitudes_3d, sigmas_3d = mge.de_projection_3d(amplitudes, sigmas)
        ss_3d_mge = self.multiGaussian.function(rs,
                                                np.zeros_like(rs),
                                                amp=amplitudes_3d,
                                                sigma=sigmas_3d)
        ss_3d_mulit = self.multiGaussian.light_3d(rs,
                                                  amp=amplitudes,
                                                  sigma=sigmas)
        for i in range(10, len(ss_3d_mge)):
            npt.assert_almost_equal((ss_3d_mge[i] - ss_3d_mulit[i]) /
                                    (ss_3d_mulit[i] + ss_3d_mge[i]),
                                    0,
                                    decimal=1)

        ss_3d = hernquist.light_3d(rs, sigma0, Rs=r_eff)
        for i in range(10, len(ss_3d) - 10):
            npt.assert_almost_equal(
                (ss_3d_mge[i] - ss_3d[i]) / (ss_3d[i] + ss_3d_mge[i]),
                0,
                decimal=1)

    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_example(self):
        n_comp = 10
        rs = np.array([
            0.01589126, 0.01703967, 0.01827108, 0.01959148, 0.0210073,
            0.02252544, 0.02415329, 0.02589879, 0.02777042, 0.02977731,
            0.03192923, 0.03423667, 0.03671086, 0.03936385, 0.04220857,
            0.04525886, 0.0485296, 0.0520367, 0.05579724, 0.05982956,
            0.06415327, 0.06878945, 0.07376067, 0.07909115, 0.08480685,
            0.09093561, 0.09750727, 0.10455385, 0.11210966, 0.12021152,
            0.12889887, 0.13821403, 0.14820238, 0.15891255, 0.17039672,
            0.18271082, 0.19591482, 0.21007304, 0.22525444, 0.24153295,
            0.25898787, 0.2777042, 0.29777311, 0.31929235, 0.34236672,
            0.36710861, 0.39363853, 0.42208569, 0.45258865, 0.48529597,
            0.52036697, 0.55797244, 0.59829556, 0.64153272, 0.6878945,
            0.73760673, 0.79091152, 0.8480685, 0.90935605, 0.97507269,
            1.04553848, 1.12109664, 1.20211518, 1.28898871, 1.38214034,
            1.48202378, 1.58912553, 1.70396721, 1.82710819, 1.95914822,
            2.10073042, 2.25254437, 2.4153295, 2.58987865, 2.77704199,
            2.9777311, 3.19292345, 3.42366716, 3.67108607, 3.93638527,
            4.22085689, 4.5258865, 4.85295974, 5.20366966, 5.57972441,
            5.98295559, 6.41532717, 6.87894505, 7.37606729, 7.90911519,
            8.48068497, 9.09356051, 9.75072687, 10.45538481, 11.21096643,
            12.02115183, 12.88988708, 13.82140341, 14.82023784, 15.89125526
        ])
        kappa = np.array([
            12.13776067, 11.60484966, 11.09533396, 10.60818686, 10.14242668,
            9.69711473, 9.27135349, 8.86428482, 8.47508818, 8.10297905,
            7.7472073, 7.40705574, 7.08183863, 6.77090034, 6.47361399,
            6.18938022, 5.917626, 5.65780342, 5.40938864, 5.1718808,
            4.94480104, 4.72769151, 4.52011448, 4.3216514, 4.13190214,
            3.9504841, 3.77703149, 3.61119459, 3.45263901, 3.30104507,
            3.1561071, 3.01753287, 2.88504297, 2.75837025, 2.63725931,
            2.52146595, 2.41075668, 2.30490829, 2.20370736, 2.10694982,
            2.01444058, 1.92599312, 1.84142909, 1.76057799, 1.6832768,
            1.60936965, 1.53870751, 1.47114792, 1.40655465, 1.34479745,
            1.28575181, 1.22929867, 1.17532421, 1.12371958, 1.07438074,
            1.02720821, 0.98210687, 0.93898578, 0.897758, 0.85834039,
            0.82065349, 0.78462129, 0.75017114, 0.71723359, 0.68574222,
            0.65563353, 0.62684681, 0.59932403, 0.57300967, 0.5478507,
            0.52379638, 0.5007982, 0.47880979, 0.45778683, 0.43768691,
            0.41846951, 0.40009589, 0.38252899, 0.3657334, 0.34967525,
            0.33432216, 0.31964317, 0.30560868, 0.29219041, 0.27936129,
            0.26709545, 0.25536817, 0.24415579, 0.23343571, 0.22318631,
            0.21338694, 0.20401782, 0.19506006, 0.18649562, 0.17830721,
            0.17047832, 0.16299318, 0.15583668, 0.14899441, 0.14245255
        ])
        amplitudes, sigmas, norm = mge.mge_1d(rs, kappa, N=n_comp)

    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_yy_nfw, f_xy_nfw = nfw.hessian(rs, 0, **kwargs_lens_nfw)
        f_xx_s, f_yy_s, f_xy_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_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_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)
class TestSersic(object):
    """
    tests the Gaussian methods
    """
    def setup(self):

        self.sersic_2 = SersicEllipseKappa()
        self.sersic = Sersic()
        self.sersic_light = Sersic_light()

    def test_function(self):

        x = 1
        y = 2
        n_sersic = 2.
        R_sersic = 1.
        k_eff = 0.2
        values = self.sersic.function(x, y, n_sersic, R_sersic, k_eff)
        npt.assert_almost_equal(values, 1.0272982586319199, decimal=10)

        x = np.array([0])
        y = np.array([0])
        values = self.sersic.function(x, y, n_sersic, R_sersic, k_eff)
        npt.assert_almost_equal(values[0], 0., decimal=9)

        x = np.array([2, 3, 4])
        y = np.array([1, 1, 1])
        values = self.sersic.function(x, y, n_sersic, R_sersic, k_eff)

        npt.assert_almost_equal(values[0], 1.0272982586319199, decimal=10)
        npt.assert_almost_equal(values[1], 1.3318743892966658, decimal=10)
        npt.assert_almost_equal(values[2], 1.584299393114988, decimal=10)

    def test_derivatives(self):
        x = np.array([1])
        y = np.array([2])
        n_sersic = 2.
        R_sersic = 1.
        k_eff = 0.2
        f_x, f_y = self.sersic.derivatives(x, y, n_sersic, R_sersic, k_eff)
        f_x2, f_y2 = self.sersic_2.derivatives(x, y, n_sersic, R_sersic, k_eff,
                                               0, 0.00000001)

        assert f_x[0] == 0.16556078301997193
        assert f_y[0] == 0.33112156603994386
        npt.assert_almost_equal(f_x2[0], f_x[0])
        npt.assert_almost_equal(f_y2[0], f_y[0])

        x = np.array([0])
        y = np.array([0])
        f_x, f_y = self.sersic.derivatives(x, y, n_sersic, R_sersic, k_eff)
        f_x2, f_y2 = self.sersic_2.derivatives(x, y, n_sersic, R_sersic, k_eff,
                                               0, 0.00000001)
        assert f_x[0] == 0
        assert f_y[0] == 0
        npt.assert_almost_equal(f_x2[0], f_x[0])
        npt.assert_almost_equal(f_y2[0], f_y[0])

        x = np.array([1, 3, 4])
        y = np.array([2, 1, 1])
        values = self.sersic.derivatives(x, y, n_sersic, R_sersic, k_eff)
        values2 = self.sersic_2.derivatives(x, y, n_sersic, R_sersic, k_eff, 0,
                                            0.00000001)
        assert values[0][0] == 0.16556078301997193
        assert values[1][0] == 0.33112156603994386
        assert values[0][1] == 0.2772992378623737
        assert values[1][1] == 0.092433079287457892
        npt.assert_almost_equal(values2[0][0], values[0][0])
        npt.assert_almost_equal(values2[1][0], values[1][0])
        npt.assert_almost_equal(values2[0][1], values[0][1])
        npt.assert_almost_equal(values2[1][1], values[1][1])

        values2 = self.sersic_2.derivatives(0.3, -0.2, n_sersic, R_sersic,
                                            k_eff, 0, 0.00000001)
        values = self.sersic.derivatives(0.3, -0.2, n_sersic, R_sersic, k_eff,
                                         0, 0.00000001)
        npt.assert_almost_equal(values2[0], values[0])
        npt.assert_almost_equal(values2[1], values[1])

    def test_differentails(self):
        x_, y_ = 1., 1
        n_sersic = 2.
        R_sersic = 1.
        k_eff = 0.2
        r = np.sqrt(x_**2 + y_**2)

        d_alpha_dr = self.sersic.d_alpha_dr(x_, y_, n_sersic, R_sersic, k_eff)
        alpha = self.sersic.alpha_abs(x_, y_, n_sersic, R_sersic, k_eff)

        f_xx_ = d_alpha_dr * calc_util.d_r_dx(
            x_, y_) * x_ / r + alpha * calc_util.d_x_diffr_dx(x_, y_)
        f_yy_ = d_alpha_dr * calc_util.d_r_dy(
            x_, y_) * y_ / r + alpha * calc_util.d_y_diffr_dy(x_, y_)
        f_xy_ = d_alpha_dr * calc_util.d_r_dy(
            x_, y_) * x_ / r + alpha * calc_util.d_x_diffr_dy(x_, y_)

        f_xx = (d_alpha_dr / r - alpha / r**2) * y_**2 / r + alpha / r
        f_yy = (d_alpha_dr / r - alpha / r**2) * x_**2 / r + alpha / r
        f_xy = (d_alpha_dr / r - alpha / r**2) * x_ * y_ / r
        npt.assert_almost_equal(f_xx, f_xx_, decimal=10)
        npt.assert_almost_equal(f_yy, f_yy_, decimal=10)
        npt.assert_almost_equal(f_xy, f_xy_, decimal=10)

    def test_hessian(self):
        x = np.array([1])
        y = np.array([2])
        n_sersic = 2.
        R_sersic = 1.
        k_eff = 0.2
        f_xx, f_xy, f_yx, f_yy = self.sersic.hessian(x, y, n_sersic, R_sersic,
                                                     k_eff)
        assert f_xx[0] == 0.1123170666045793
        npt.assert_almost_equal(f_yy[0], -0.047414082641598576, decimal=10)
        npt.assert_almost_equal(f_xy[0], -0.10648743283078525, decimal=10)
        npt.assert_almost_equal(f_xy, f_yx, decimal=5)
        x = np.array([1, 3, 4])
        y = np.array([2, 1, 1])
        values = self.sersic.hessian(x, y, n_sersic, R_sersic, k_eff)
        assert values[0][0] == 0.1123170666045793
        npt.assert_almost_equal(values[3][0],
                                -0.047414082641598576,
                                decimal=10)
        npt.assert_almost_equal(values[1][0], -0.10648743283078525, decimal=10)
        npt.assert_almost_equal(values[0][1],
                                -0.053273787681591328,
                                decimal=10)
        npt.assert_almost_equal(values[3][1], 0.076243427402007985, decimal=10)
        npt.assert_almost_equal(values[1][1],
                                -0.048568955656349749,
                                decimal=10)

        f_xx2, f_xy2, f_yx2, f_yy2 = self.sersic_2.hessian(
            x, y, n_sersic, R_sersic, k_eff, 0.0000001, 0)
        npt.assert_almost_equal(f_xx2, values[0])
        npt.assert_almost_equal(f_yy2, values[3], decimal=6)
        npt.assert_almost_equal(f_xy2, values[1], decimal=6)
        npt.assert_almost_equal(f_yx2, values[2], decimal=6)

    def test_alpha_abs(self):
        x = 1.
        dr = 0.0000001
        n_sersic = 2.5
        R_sersic = .5
        k_eff = 0.2
        alpha_abs = self.sersic.alpha_abs(x, 0, n_sersic, R_sersic, k_eff)
        f_dr = self.sersic.function(x + dr, 0, n_sersic, R_sersic, k_eff)
        f_ = self.sersic.function(x, 0, n_sersic, R_sersic, k_eff)
        alpha_abs_num = -(f_dr - f_) / dr
        npt.assert_almost_equal(alpha_abs_num, alpha_abs, decimal=3)

    def test_dalpha_dr(self):
        x = 1.
        dr = 0.0000001
        n_sersic = 1.
        R_sersic = .5
        k_eff = 0.2
        d_alpha_dr = self.sersic.d_alpha_dr(x, 0, n_sersic, R_sersic, k_eff)
        alpha_dr = self.sersic.alpha_abs(x + dr, 0, n_sersic, R_sersic, k_eff)
        alpha = self.sersic.alpha_abs(x, 0, n_sersic, R_sersic, k_eff)
        d_alpha_dr_num = (alpha_dr - alpha) / dr
        npt.assert_almost_equal(d_alpha_dr, d_alpha_dr_num, decimal=3)

    def test_mag_sym(self):
        """

        :return:
        """
        r = 2.
        angle1 = 0.
        angle2 = 1.5
        x1 = r * np.cos(angle1)
        y1 = r * np.sin(angle1)

        x2 = r * np.cos(angle2)
        y2 = r * np.sin(angle2)
        n_sersic = 4.5
        R_sersic = 2.5
        k_eff = 0.8
        f_xx1, f_xy1, f_yx1, f_yy1 = self.sersic.hessian(
            x1, y1, n_sersic, R_sersic, k_eff)
        f_xx2, f_xy2, f_yx2, f_yy2 = self.sersic.hessian(
            x2, y2, n_sersic, R_sersic, k_eff)
        kappa_1 = (f_xx1 + f_yy1) / 2
        kappa_2 = (f_xx2 + f_yy2) / 2
        npt.assert_almost_equal(kappa_1, kappa_2, decimal=10)
        A_1 = (1 - f_xx1) * (1 - f_yy1) - f_xy1 * f_yx1
        A_2 = (1 - f_xx2) * (1 - f_yy2) - f_xy2 * f_yx2
        npt.assert_almost_equal(A_1, A_2, decimal=10)

    def test_convergernce(self):
        """
        test the convergence and compares it with the original Sersic profile
        :return:
        """
        x = np.array([0, 0, 0, 0, 0])
        y = np.array([0.5, 1, 1.5, 2, 2.5])
        n_sersic = 4.5
        R_sersic = 2.5
        k_eff = 0.2
        f_xx, f_xy, f_yx, f_yy = self.sersic.hessian(x, y, n_sersic, R_sersic,
                                                     k_eff)
        kappa = (f_xx + f_yy) / 2.
        assert kappa[0] > 0
        flux = self.sersic_light.function(x,
                                          y,
                                          amp=1.,
                                          R_sersic=R_sersic,
                                          n_sersic=n_sersic)
        flux /= flux[0]
        kappa /= kappa[0]
        npt.assert_almost_equal(flux[1], kappa[1], decimal=5)

        xvalues = np.linspace(0.5, 3., 100)

        e1, e2 = 0.4, 0.
        q = ellipticity2phi_q(e1, e2)[1]
        kappa_ellipse = self.sersic_2.projected_mass(xvalues, 0, q, n_sersic,
                                                     R_sersic, k_eff)
        fxx, _, _, fyy = self.sersic_2.hessian(xvalues, 0, n_sersic, R_sersic,
                                               k_eff, e1, e2)

        npt.assert_almost_equal(kappa_ellipse, 0.5 * (fxx + fyy), decimal=5)

    def test_sersic_util(self):
        n = 1.
        Re = 2.
        k, bn = self.sersic.k_bn(n, Re)
        Re_new = self.sersic.k_Re(n, k)
        assert Re == Re_new
Beispiel #5
0
class TestSersic(object):
    """
    tests the Gaussian methods
    """
    def setup(self):
        self.sersic = Sersic(smoothing=0.02)
        self.sersic_elliptic = SersicElliptic(smoothing=0.02)
        self.core_sersic = CoreSersic(smoothing=0.02)

    def test_sersic(self):
        x = np.array([1])
        y = np.array([2])
        I0_sersic = 1
        R_sersic = 1
        n_sersic = 1
        center_x = 0
        center_y = 0
        values = self.sersic.function(x, y, I0_sersic, R_sersic, n_sersic,
                                      center_x, center_y)
        npt.assert_almost_equal(values[0], 0.12658651833626802, decimal=6)
        x = np.array([0])
        y = np.array([0])
        values = self.sersic.function(x, y, I0_sersic, R_sersic, n_sersic,
                                      center_x, center_y)
        npt.assert_almost_equal(values[0], 5.1482559148107292, decimal=2)

        x = np.array([2, 3, 4])
        y = np.array([1, 1, 1])
        values = self.sersic.function(x, y, I0_sersic, R_sersic, n_sersic,
                                      center_x, center_y)
        npt.assert_almost_equal(values[0], 0.12658651833626802, decimal=6)
        npt.assert_almost_equal(values[1], 0.026902273598180083, decimal=6)
        npt.assert_almost_equal(values[2], 0.0053957432862338055, decimal=6)

        value = self.sersic.function(1000, 0, I0_sersic, R_sersic, n_sersic,
                                     center_x, center_y)
        npt.assert_almost_equal(value, 0, decimal=8)

    def test_symmetry_r_sersic(self):
        x = np.array([2, 3, 4])
        y = np.array([1, 1, 1])
        I0_sersic = 1
        R_sersic1 = 1
        R_sersic2 = 0.1
        n_sersic = 1
        center_x = 0
        center_y = 0
        values1 = self.sersic.function(x * R_sersic1, y * R_sersic1, I0_sersic,
                                       R_sersic1, n_sersic, center_x, center_y)
        values2 = self.sersic.function(x * R_sersic2, y * R_sersic2, I0_sersic,
                                       R_sersic2, n_sersic, center_x, center_y)
        npt.assert_almost_equal(values1[0], values2[0], decimal=6)
        npt.assert_almost_equal(values1[1], values2[1], decimal=6)
        npt.assert_almost_equal(values1[2], values2[2], decimal=6)

    def test_sersic_center(self):
        x = 0.01
        y = 0.
        I0_sersic = 1
        R_sersic = 0.1
        n_sersic = 4.
        center_x = 0
        center_y = 0
        values = self.sersic.function(x, y, I0_sersic, R_sersic, n_sersic,
                                      center_x, center_y)
        npt.assert_almost_equal(values, 12.688073819377406, decimal=6)

    def test_sersic_elliptic(self):
        x = np.array([1])
        y = np.array([2])
        I0_sersic = 1
        R_sersic = 1
        n_sersic = 1
        phi_G = 1
        q = 0.9
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        center_x = 0
        center_y = 0
        values = self.sersic_elliptic.function(x, y, I0_sersic, R_sersic,
                                               n_sersic, e1, e2, center_x,
                                               center_y)
        npt.assert_almost_equal(values[0], 0.12595366113005077, decimal=6)
        x = np.array([0])
        y = np.array([0])
        values = self.sersic_elliptic.function(x, y, I0_sersic, R_sersic,
                                               n_sersic, e1, e2, center_x,
                                               center_y)
        npt.assert_almost_equal(values[0], 5.1482553482055664, decimal=2)

        x = np.array([2, 3, 4])
        y = np.array([1, 1, 1])
        values = self.sersic_elliptic.function(x, y, I0_sersic, R_sersic,
                                               n_sersic, e1, e2, center_x,
                                               center_y)
        npt.assert_almost_equal(values[0], 0.11308277793465012, decimal=6)
        npt.assert_almost_equal(values[1], 0.021188620675507107, decimal=6)
        npt.assert_almost_equal(values[2], 0.0037276744362724477, decimal=6)

    def test_core_sersic(self):
        x = np.array([1])
        y = np.array([2])
        I0 = 1
        Rb = 1
        Re = 2
        gamma = 3
        n = 1
        phi_G = 1
        q = 0.9
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        center_x = 0
        center_y = 0
        values = self.core_sersic.function(x, y, I0, Rb, Re, n, gamma, e1, e2,
                                           center_x, center_y)
        npt.assert_almost_equal(values[0], 0.84489101, decimal=8)
        x = np.array([0])
        y = np.array([0])
        values = self.core_sersic.function(x, y, I0, Rb, Re, n, gamma, e1, e2,
                                           center_x, center_y)
        npt.assert_almost_equal(values[0], 288406.09, decimal=0)

        x = np.array([2, 3, 4])
        y = np.array([1, 1, 1])
        values = self.core_sersic.function(x, y, I0, Rb, Re, n, gamma, e1, e2,
                                           center_x, center_y)
        npt.assert_almost_equal(values[0], 0.79749529635325933, decimal=6)
        npt.assert_almost_equal(values[1], 0.33653478121594838, decimal=6)
        npt.assert_almost_equal(values[2], 0.14050402887681532, decimal=6)

    def test_total_flux(self):
        r_eff = 0.2
        I_eff = 1.
        n_sersic = 4
        flux = self.sersic._total_flux(r_eff, I_eff, n_sersic)
        npt.assert_almost_equal(flux, 0.9065917451904356, decimal=5)
Beispiel #6
0
class TestSersic(object):
    """
    tests the Gaussian methods
    """
    def setup(self):
        self.sersic = Sersic(smoothing=0.02)
        self.sersic_elliptic = SersicElliptic(smoothing=0.02)
        self.core_sersic = CoreSersic(smoothing=0.02)

    def test_sersic(self):
        x = np.array([1])
        y = np.array([2])
        I0_sersic = 1
        R_sersic = 1
        n_sersic = 1
        center_x = 0
        center_y = 0
        values = self.sersic.function(x, y, I0_sersic, R_sersic, n_sersic, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.12658651833626802, decimal=6)
        x = np.array([0])
        y = np.array([0])
        values = self.sersic.function( x, y, I0_sersic, R_sersic, n_sersic, center_x, center_y)
        npt.assert_almost_equal(values[0],  5.1482559148107292, decimal=2)

        x = np.array([2,3,4])
        y = np.array([1,1,1])
        values = self.sersic.function( x, y, I0_sersic, R_sersic, n_sersic, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.12658651833626802, decimal=6)
        npt.assert_almost_equal(values[1], 0.026902273598180083, decimal=6)
        npt.assert_almost_equal(values[2], 0.0053957432862338055, decimal=6)

        value = self.sersic.function(1000, 0, I0_sersic, R_sersic, n_sersic, center_x, center_y)
        npt.assert_almost_equal(value, 0, decimal=8)

    def test_symmetry_r_sersic(self):
        x = np.array([2,3,4])
        y = np.array([1,1,1])
        I0_sersic = 1
        R_sersic1 = 1
        R_sersic2 = 0.1
        n_sersic = 1
        center_x = 0
        center_y = 0
        values1 = self.sersic.function(x*R_sersic1, y*R_sersic1, I0_sersic, R_sersic1, n_sersic, center_x, center_y)
        values2 = self.sersic.function(x*R_sersic2, y*R_sersic2, I0_sersic, R_sersic2, n_sersic, center_x, center_y)
        npt.assert_almost_equal(values1[0], values2[0], decimal=6)
        npt.assert_almost_equal(values1[1], values2[1], decimal=6)
        npt.assert_almost_equal(values1[2], values2[2], decimal=6)

    def test_sersic_center(self):
        x = 0.01
        y = 0.
        I0_sersic = 1
        R_sersic = 0.1
        n_sersic = 4.
        center_x = 0
        center_y = 0
        values = self.sersic.function(x, y, I0_sersic, R_sersic, n_sersic, center_x, center_y)
        npt.assert_almost_equal(values, 12.688073819377406, decimal=6)

    def test_sersic_elliptic(self):
        x = np.array([1])
        y = np.array([2])
        I0_sersic = 1
        R_sersic = 1
        n_sersic = 1
        phi_G = 1
        q = 0.9
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        center_x = 0
        center_y = 0
        values = self.sersic_elliptic.function(x, y, I0_sersic, R_sersic, n_sersic, e1, e2, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.12595366113005077, decimal=6)
        x = np.array([0])
        y = np.array([0])
        values = self.sersic_elliptic.function(x, y, I0_sersic, R_sersic, n_sersic, e1, e2, center_x, center_y)
        npt.assert_almost_equal(values[0], 5.1482553482055664, decimal=2)

        x = np.array([2,3,4])
        y = np.array([1,1,1])
        values = self.sersic_elliptic.function(x, y, I0_sersic, R_sersic, n_sersic, e1, e2, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.11308277793465012, decimal=6)
        npt.assert_almost_equal(values[1], 0.021188620675507107, decimal=6)
        npt.assert_almost_equal(values[2], 0.0037276744362724477, decimal=6)

    def test_core_sersic(self):
        x = np.array([1])
        y = np.array([2])
        I0 = 1
        Rb = 1
        Re = 2
        gamma = 3
        n = 1
        phi_G = 1
        q = 0.9
        e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
        center_x = 0
        center_y = 0
        values = self.core_sersic.function(x, y, I0, Rb, Re, n, gamma, e1, e2, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.10338957116342086, decimal=8)
        x = np.array([0])
        y = np.array([0])
        values = self.core_sersic.function(x, y, I0, Rb, Re, n, gamma, e1, e2, center_x, center_y)
        npt.assert_almost_equal(values[0], 187852.14004235074, decimal=0)

        x = np.array([2,3,4])
        y = np.array([1,1,1])
        values = self.core_sersic.function(x, y, I0, Rb, Re, n, gamma, e1, e2, center_x, center_y)
        npt.assert_almost_equal(values[0], 0.09255079955772508, decimal=6)
        npt.assert_almost_equal(values[1], 0.01767817014938002, decimal=6)
        npt.assert_almost_equal(values[2], 0.0032541063777438853, decimal=6)

    def test_total_flux(self):
        deltapix = 0.1
        x_grid, y_grid = util.make_grid(numPix=400, deltapix=deltapix)
        r_eff = 1
        I_eff = 1.
        n_sersic = 2
        flux_analytic = self.sersic.total_flux(amp=I_eff, R_sersic=r_eff, n_sersic=n_sersic, e1=0, e2=0)
        flux_grid = self.sersic.function(x_grid, y_grid, R_sersic=r_eff, n_sersic=n_sersic, amp=I_eff)
        flux_numeric = np.sum(flux_grid) * deltapix**2
        npt.assert_almost_equal(flux_numeric/flux_analytic, 1, decimal=2)

        # and here we check with ellipticity
        e1, e2 = 0.1, 0
        flux_analytic_ell = self.sersic.total_flux(amp=I_eff, R_sersic=r_eff, n_sersic=n_sersic, e1=e1, e2=e2)
        flux_grid = self.sersic_elliptic.function(x_grid, y_grid, R_sersic=r_eff, n_sersic=n_sersic, amp=I_eff, e1=e1, e2=e2)
        flux_numeric_ell = np.sum(flux_grid) * deltapix ** 2
        print(flux_analytic, flux_analytic_ell, flux_numeric_ell)
        npt.assert_almost_equal(flux_numeric_ell / flux_analytic_ell, 1, decimal=2)