class MultiGaussianKappa(object):
    """

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

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

    def function(self,
                 x,
                 y,
                 amp,
                 sigma,
                 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],
                                               center_x=center_x,
                                               center_y=center_y)
        return f_

    def derivatives(self,
                    x,
                    y,
                    amp,
                    sigma,
                    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],
                                                           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,
                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],
                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, 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])
        return d_

    def density_2d(self,
                   x,
                   y,
                   amp,
                   sigma,
                   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], center_x,
                                                   center_y)
        return d_3d

    def mass_3d_lens(self, R, amp, sigma, 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])
        return mass_3d
예제 #2
0
class GaussianEllipsePotential(LensProfileBase):
    """
    this class contains functions to evaluate a Gaussian function and calculates its derivative and hessian matrix
    with ellipticity in the convergence

    the calculation follows Glenn van de Ven et al. 2009

    """
    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.spherical = GaussianKappa()
        self._diff = 0.000001
        super(GaussianEllipsePotential, self).__init__()

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

        phi_G, q = param_util.ellipticity2phi_q(e1, e2)
        x_shift = x - center_x
        y_shift = y - center_y
        cos_phi = np.cos(phi_G)
        sin_phi = np.sin(phi_G)
        e = abs(1 - q)
        x_ = (cos_phi * x_shift + sin_phi * y_shift) * np.sqrt(1 - e)
        y_ = (-sin_phi * x_shift + cos_phi * y_shift) * np.sqrt(1 + e)
        f_ = self.spherical.function(x_, y_, amp=amp, sigma=sigma)
        return f_

    def derivatives(self, x, y, amp, sigma, e1, e2, center_x=0, center_y=0):
        """
        returns df/dx and df/dy of the function
        """
        phi_G, q = param_util.ellipticity2phi_q(e1, e2)
        x_shift = x - center_x
        y_shift = y - center_y
        cos_phi = np.cos(phi_G)
        sin_phi = np.sin(phi_G)
        e = abs(1 - q)
        x_ = (cos_phi * x_shift + sin_phi * y_shift) * np.sqrt(1 - e)
        y_ = (-sin_phi * x_shift + cos_phi * y_shift) * np.sqrt(1 + e)

        f_x_prim, f_y_prim = self.spherical.derivatives(x_,
                                                        y_,
                                                        amp=amp,
                                                        sigma=sigma)
        f_x_prim *= np.sqrt(1 - e)
        f_y_prim *= np.sqrt(1 + e)
        f_x = cos_phi * f_x_prim - sin_phi * f_y_prim
        f_y = sin_phi * f_x_prim + cos_phi * f_y_prim
        return f_x, f_y

    def hessian(self, x, y, amp, sigma, e1, e2, center_x=0, center_y=0):
        """
        returns Hessian matrix of function d^2f/dx^2, d^2/dxdy, d^2/dydx, d^f/dy^2
        """
        alpha_ra, alpha_dec = self.derivatives(x, y, amp, sigma, e1, e2,
                                               center_x, center_y)
        diff = self._diff
        alpha_ra_dx, alpha_dec_dx = self.derivatives(x + diff, y, amp, sigma,
                                                     e1, e2, center_x,
                                                     center_y)
        alpha_ra_dy, alpha_dec_dy = self.derivatives(x, y + diff, amp, sigma,
                                                     e1, e2, center_x,
                                                     center_y)

        f_xx = (alpha_ra_dx - alpha_ra) / diff
        f_xy = (alpha_ra_dy - alpha_ra) / diff
        f_yx = (alpha_dec_dx - alpha_dec) / diff
        f_yy = (alpha_dec_dy - alpha_dec) / diff
        return f_xx, f_xy, f_yx, f_yy

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

        :param r:
        :param amp:
        :param sigma:
        :return:
        """
        return self.spherical.density(r, amp, sigma)

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

        :param R:
        :param am:
        :param sigma_x:
        :param sigma_y:
        :return:
        """
        return self.spherical.density_2d(x, y, amp, sigma, center_x, center_y)

    def mass_2d(self, R, amp, sigma, e1, e2):
        """

        :param R:
        :param amp:
        :param sigma_x:
        :param sigma_y:
        :return:
        """
        return self.spherical.mass_2d(R, amp, sigma)

    def mass_3d(self, R, amp, sigma, e1, e2):
        """

        :param R:
        :param amp:
        :param sigma:
        :param e1:
        :param e2:
        :return:
        """
        return self.spherical.mass_3d(R, amp, sigma)

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

        :param R:
        :param amp:
        :param sigma:
        :param e1:
        :param e2:
        :return:
        """
        return self.spherical.mass_3d_lens(R, amp, sigma)

    def mass_2d_lens(self, R, amp, sigma, e1, e2):
        """

        :param R:
        :param amp:
        :param sigma_x:
        :param sigma_y:
        :return:
        """
        return self.spherical.mass_2d_lens(R, amp, sigma)
예제 #3
0
class TestGaussianKappa(object):
    """
    test the Gaussian with Gaussian kappa
    """
    def setup(self):
        self.gaussian_kappa = GaussianKappa()
        self.gaussian = Gaussian()

    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, sigma, sigma, 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.gaussian_kappa._amp2d_to_3d(amp, sigma, sigma)
        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_3d_2d_convention(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.
        amp_3d = self.gaussian_kappa._amp2d_to_3d(amp, sigma, sigma)
        density_2d_gauss = self.gaussian_kappa.density_2d(
            x, y, amp_3d, sigma, center_x, center_y)
        density_2d = self.gaussian.function(x, y, amp, sigma, sigma, center_x,
                                            center_y)
        npt.assert_almost_equal(density_2d_gauss[1], density_2d[1], decimal=5)
예제 #4
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)
예제 #5
0
class MultiGaussian_kappa(object):
    """

    """
    def __init__(self):
        self.gaussian_kappa = GaussianKappa()

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

        :param x:
        :param y:
        :param amp:
        :param sigma:
        :param center_x:
        :param center_y:
        :return:
        """
        f_ = np.zeros_like(x)
        for i in range(len(amp)):
            f_ += self.gaussian_kappa.function(x,
                                               y,
                                               amp=amp[i],
                                               sigma_x=sigma[i],
                                               sigma_y=sigma[i],
                                               center_x=center_x,
                                               center_y=center_y)
        return f_

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

        :param x:
        :param y:
        :param amp:
        :param sigma:
        :param center_x:
        :param center_y:
        :return:
        """
        f_x, f_y = np.zeros_like(x), np.zeros_like(x)
        for i in range(len(amp)):
            f_x_i, f_y_i = self.gaussian_kappa.derivatives(x,
                                                           y,
                                                           amp=amp[i],
                                                           sigma_x=sigma[i],
                                                           sigma_y=sigma[i],
                                                           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, center_x=0, center_y=0):
        """

        :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), np.zeros_like(x), np.zeros_like(x)
        for i in range(len(amp)):
            f_xx_i, f_yy_i, f_xy_i = self.gaussian_kappa.hessian(
                x,
                y,
                amp=amp[i],
                sigma_x=sigma[i],
                sigma_y=sigma[i],
                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):
        """

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

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

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

    def mass_3d_lens(self, R, amp, sigma):
        """

        :param R:
        :param amp:
        :param sigma:
        :return:
        """
        mass_3d = np.zeros_like(R)
        for i in range(len(amp)):
            mass_3d += self.gaussian_kappa.mass_3d_lens(
                R, amp[i], sigma[i], sigma[i])
        return mass_3d