class TestGaussianKappaEllipse(object):
    """
    test the Gaussian with Gaussian kappa
    """
    def setup(self):
        self.multi = MultiGaussianKappaEllipse()
        self.single = GaussianEllipsePotential()

    def test_function(self):
        x, y = 1, 2
        amp = 1
        sigma = 1
        e1, e2 = 0.1, -0.1
        center_x, center_y = 1, 0
        f_ = self.multi.function(x, y, amp=[amp], sigma=[sigma], e1=e1, e2=e2, center_x=center_x, center_y=center_y)
        f_single = self.single.function(x, y, amp=amp, sigma=sigma, e1=e1, e2=e2, center_x=center_x, center_y=center_y)
        npt.assert_almost_equal(f_, f_single, decimal=8)

    def test_derivatives(self):
        x, y = 1, 2
        amp = 1
        sigma = 1
        e1, e2 = 0.1, -0.1
        center_x, center_y = 1, 0
        f_x, f_y = self.multi.derivatives(x, y, amp=[amp], sigma=[sigma], e1=e1, e2=e2, center_x=center_x, center_y=center_y)
        f_x_s, f_y_s = self.single.derivatives(x, y, amp=amp, sigma=sigma, e1=e1, e2=e2, center_x=center_x, center_y=center_y)
        npt.assert_almost_equal(f_x, f_x_s, decimal=8)
        npt.assert_almost_equal(f_y, f_y_s, decimal=8)

    def test_hessian(self):
        x, y = 1, 2
        amp = 1
        sigma = 1
        e1, e2 = 0.1, -0.1
        center_x, center_y = 1, 0
        f_xx, f_yy, f_xy = self.multi.hessian(x, y, amp=[amp], sigma=[sigma], e1=e1, e2=e2, center_x=center_x, center_y=center_y)
        f_xx_s, f_yy_s, f_xy_s = self.single.hessian(x, y, amp=amp, sigma=sigma, e1=e1, e2=e2, center_x=center_x, center_y=center_y)
        npt.assert_almost_equal(f_xx, f_xx_s, decimal=8)
        npt.assert_almost_equal(f_yy, f_yy_s, decimal=8)
        npt.assert_almost_equal(f_xy, f_xy_s, decimal=8)

    def test_density_2d(self):
        x, y = 1, 2
        amp = 1
        sigma = 1
        e1, e2 = 0.1, -0.1
        center_x, center_y = 1, 0
        f_ = self.multi.density_2d(x, y, amp=[amp], sigma=[sigma], e1=e1, e2=e2, center_x=center_x, center_y=center_y)
        f_single = self.single.density_2d(x, y, amp=amp, sigma=sigma, e1=e1, e2=e2, center_x=center_x, center_y=center_y)
        npt.assert_almost_equal(f_, f_single, decimal=8)

    def test_density(self):
        r = 1
        amp = 1
        sigma = 1
        e1, e2 = 0.1, -0.1
        f_ = self.multi.density(r, amp=[amp], sigma=[sigma], e1=e1, e2=e2)
        f_single = self.single.density(r, amp=amp, sigma=sigma, e1=e1, e2=e2)
        npt.assert_almost_equal(f_, f_single, decimal=8)
class MultiGaussianKappaEllipse(object):
    """

    """
    param_names = ['amp', 'sigma', 'e1', 'e2', 'center_x', 'center_y']
    lower_limit_default = {
        'amp': 0,
        'sigma': 0,
        'e1': -0.5,
        'e2': -0.5,
        'center_x': -100,
        'center_y': -100
    }
    upper_limit_default = {
        'amp': 100,
        'sigma': 100,
        'e1': 0.5,
        'e2': 0.5,
        'center_x': 100,
        'center_y': 100
    }

    def __init__(self):
        self.gaussian_kappa = GaussianEllipsePotential()

    def function(self,
                 x,
                 y,
                 amp,
                 sigma,
                 e1,
                 e2,
                 center_x=0,
                 center_y=0,
                 scale_factor=1):
        """

        :param x:
        :param y:
        :param amp:
        :param sigma:
        :param center_x:
        :param center_y:
        :return:
        """
        f_ = np.zeros_like(x, dtype=float)
        for i in range(len(amp)):
            f_ += self.gaussian_kappa.function(x,
                                               y,
                                               amp=scale_factor * amp[i],
                                               sigma=sigma[i],
                                               e1=e1,
                                               e2=e2,
                                               center_x=center_x,
                                               center_y=center_y)
        return f_

    def derivatives(self,
                    x,
                    y,
                    amp,
                    sigma,
                    e1,
                    e2,
                    center_x=0,
                    center_y=0,
                    scale_factor=1):
        """

        :param x:
        :param y:
        :param amp:
        :param sigma:
        :param center_x:
        :param center_y:
        :return:
        """
        f_x, f_y = np.zeros_like(x, dtype=float), np.zeros_like(x, dtype=float)
        for i in range(len(amp)):
            f_x_i, f_y_i = self.gaussian_kappa.derivatives(x,
                                                           y,
                                                           amp=scale_factor *
                                                           amp[i],
                                                           sigma=sigma[i],
                                                           e1=e1,
                                                           e2=e2,
                                                           center_x=center_x,
                                                           center_y=center_y)
            f_x += f_x_i
            f_y += f_y_i
        return f_x, f_y

    def hessian(self,
                x,
                y,
                amp,
                sigma,
                e1,
                e2,
                center_x=0,
                center_y=0,
                scale_factor=1):
        """

        :param x:
        :param y:
        :param amp:
        :param sigma:
        :param center_x:
        :param center_y:
        :return:
        """
        f_xx, f_yy, f_xy = np.zeros_like(x, dtype=float), np.zeros_like(
            x, dtype=float), np.zeros_like(x, dtype=float)
        for i in range(len(amp)):
            f_xx_i, f_yy_i, f_xy_i = self.gaussian_kappa.hessian(
                x,
                y,
                amp=scale_factor * amp[i],
                sigma=sigma[i],
                e1=e1,
                e2=e2,
                center_x=center_x,
                center_y=center_y)
            f_xx += f_xx_i
            f_yy += f_yy_i
            f_xy += f_xy_i
        return f_xx, f_yy, f_xy

    def density(self, r, amp, sigma, e1, e2, scale_factor=1):
        """

        :param r:
        :param amp:
        :param sigma:
        :return:
        """
        d_ = np.zeros_like(r, dtype=float)
        for i in range(len(amp)):
            d_ += self.gaussian_kappa.density(r, scale_factor * amp[i],
                                              sigma[i], e1, e2)
        return d_

    def density_2d(self,
                   x,
                   y,
                   amp,
                   sigma,
                   e1,
                   e2,
                   center_x=0,
                   center_y=0,
                   scale_factor=1):
        """

        :param R:
        :param am:
        :param sigma_x:
        :param sigma_y:
        :return:
        """
        d_3d = np.zeros_like(x, dtype=float)
        for i in range(len(amp)):
            d_3d += self.gaussian_kappa.density_2d(x, y, scale_factor * amp[i],
                                                   sigma[i], e1, e2, center_x,
                                                   center_y)
        return d_3d

    def mass_3d_lens(self, R, amp, sigma, e1, e2, scale_factor=1):
        """

        :param R:
        :param amp:
        :param sigma:
        :return:
        """
        mass_3d = np.zeros_like(R, dtype=float)
        for i in range(len(amp)):
            mass_3d += self.gaussian_kappa.mass_3d_lens(
                R, scale_factor * amp[i], sigma[i], e1, e2)
        return mass_3d
Example #3
0
class TestGaussianKappaPot(object):
    """
    test the Gaussian with Gaussian kappa
    """
    def setup(self):
        self.gaussian_kappa = GaussianKappa()
        self.ellipse = GaussianEllipsePotential()

    def test_function(self):
        x = 1
        y = 1
        e1, e2 = 0, 0
        sigma = 1
        amp = 1
        f_ = self.ellipse.function(x, y, amp, sigma, e1, e2)
        f_sphere = self.gaussian_kappa.function(x, y, amp=amp, sigma=sigma)
        npt.assert_almost_equal(f_, f_sphere, decimal=8)

    def test_derivatives(self):
        x = 1
        y = 1
        e1, e2 = 0, 0
        sigma = 1
        amp = 1
        f_x, f_y = self.ellipse.derivatives(x, y, amp, sigma, e1, e2)
        f_x_sphere, f_y_sphere = self.gaussian_kappa.derivatives(x, y, amp=amp, sigma=sigma)
        npt.assert_almost_equal(f_x, f_x_sphere, decimal=8)
        npt.assert_almost_equal(f_y, f_y_sphere, decimal=8)

    def test_hessian(self):
        x = 1
        y = 1
        e1, e2 = 0, 0
        sigma = 1
        amp = 1
        f_xx, f_xy, f_yx, f_yy = self.ellipse.hessian(x, y, amp, sigma, e1, e2)
        f_xx_sphere, f_xy_sphere, f_yx_sphere, f_yy_sphere = self.gaussian_kappa.hessian(x, y, amp=amp, sigma=sigma)
        npt.assert_almost_equal(f_xx, f_xx_sphere, decimal=5)
        npt.assert_almost_equal(f_yy, f_yy_sphere, decimal=5)
        npt.assert_almost_equal(f_xy, f_xy_sphere, decimal=5)
        npt.assert_almost_equal(f_xy, f_yx, decimal=8)

    def test_density_2d(self):
        x = 1
        y = 1
        e1, e2 = 0, 0
        sigma = 1
        amp = 1
        f_ = self.ellipse.density_2d(x, y, amp, sigma, e1, e2)
        f_sphere = self.gaussian_kappa.density_2d(x, y, amp=amp, sigma=sigma)
        npt.assert_almost_equal(f_, f_sphere, decimal=8)

    def test_mass_2d(self):
        r = 1
        e1, e2 = 0, 0
        sigma = 1
        amp = 1
        f_ = self.ellipse.mass_2d(r, amp, sigma, e1, e2)
        f_sphere = self.gaussian_kappa.mass_2d(r, amp=amp, sigma=sigma)
        npt.assert_almost_equal(f_, f_sphere, decimal=8)

    def test_mass_2d_lens(self):
        r = 1
        e1, e2 = 0, 0
        sigma = 1
        amp = 1
        f_ = self.ellipse.mass_2d_lens(r, amp, sigma, e1, e2)
        f_sphere = self.gaussian_kappa.mass_2d_lens(r, amp=amp, sigma=sigma)
        npt.assert_almost_equal(f_, f_sphere, decimal=8)