class TestNFWELLIPSE(object): """ tests the Gaussian methods """ def setup(self): self.nfw = NFW() self.nfw_e = NFW_ELLIPSE() def test_function(self): x = np.array([1]) y = np.array([2]) Rs = 1. theta_Rs = 1. q = 1. phi_G = 0 values = self.nfw.function(x, y, Rs, theta_Rs) values_e = self.nfw_e.function(x, y, Rs, theta_Rs, q, phi_G) npt.assert_almost_equal(values[0], values_e[0], decimal=5) x = np.array([0]) y = np.array([0]) q = .8 phi_G = 0 values = self.nfw_e.function(x, y, Rs, theta_Rs, q, phi_G) npt.assert_almost_equal(values[0], 0, decimal=4) x = np.array([2, 3, 4]) y = np.array([1, 1, 1]) values = self.nfw_e.function(x, y, Rs, theta_Rs, q, phi_G) npt.assert_almost_equal(values[0], 1.8827504143588476, decimal=5) npt.assert_almost_equal(values[1], 2.6436373117941852, decimal=5) npt.assert_almost_equal(values[2], 3.394127018818891, decimal=5) def test_derivatives(self): x = np.array([1]) y = np.array([2]) Rs = 1. theta_Rs = 1. q = 1. phi_G = 0 f_x, f_y = self.nfw.derivatives(x, y, Rs, theta_Rs) f_x_e, f_y_e = self.nfw_e.derivatives(x, y, Rs, theta_Rs, q, phi_G) npt.assert_almost_equal(f_x[0], f_x_e[0], decimal=5) npt.assert_almost_equal(f_y[0], f_y_e[0], decimal=5) x = np.array([0]) y = np.array([0]) theta_Rs = 0 f_x, f_y = self.nfw_e.derivatives(x, y, Rs, theta_Rs, q, phi_G) npt.assert_almost_equal(f_x[0], 0, decimal=5) npt.assert_almost_equal(f_y[0], 0, decimal=5) x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) theta_Rs = 1. q = .8 phi_G = 0 values = self.nfw_e.derivatives(x, y, Rs, theta_Rs, q, phi_G) npt.assert_almost_equal(values[0][0], 0.32458737284934414, decimal=5) npt.assert_almost_equal(values[1][0], 0.9737621185480323, decimal=5) npt.assert_almost_equal(values[0][1], 0.76249351329615234, decimal=5) npt.assert_almost_equal(values[1][1], 0.38124675664807617, decimal=5) def test_hessian(self): x = np.array([1]) y = np.array([2]) Rs = 1. theta_Rs = 1. q = 1. phi_G = 0 f_xx, f_yy, f_xy = self.nfw.hessian(x, y, Rs, theta_Rs) f_xx_e, f_yy_e, f_xy_e = self.nfw_e.hessian(x, y, Rs, theta_Rs, q, phi_G) npt.assert_almost_equal(f_xx[0], f_xx_e[0], decimal=5) npt.assert_almost_equal(f_yy[0], f_yy_e[0], decimal=5) npt.assert_almost_equal(f_xy[0], f_xy_e[0], decimal=5) x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) q = .8 phi_G = 0 values = self.nfw_e.hessian(x, y, Rs, theta_Rs, q, phi_G) npt.assert_almost_equal(values[0][0], 0.26998576668768592, decimal=5) npt.assert_almost_equal(values[1][0], -0.0045328224507201753, decimal=5) npt.assert_almost_equal(values[2][0], -0.16380454531672584, decimal=5) npt.assert_almost_equal(values[0][1], -0.014833136829928151, decimal=5) npt.assert_almost_equal(values[1][1], 0.31399726446723619, decimal=5) npt.assert_almost_equal(values[2][1], -0.13449884961325154, decimal=5)
class TestNFWELLIPSE(object): """ tests the Gaussian methods """ def setup(self): self.nfw = NFW() self.nfw_e = NFW_ELLIPSE() def test_function(self): x = np.array([1]) y = np.array([2]) Rs = 1. alpha_Rs = 1. q = 1. phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.nfw.function(x, y, Rs, alpha_Rs) values_e = self.nfw_e.function(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(values[0], values_e[0], decimal=5) x = np.array([0]) y = np.array([0]) q = .8 phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.nfw_e.function(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(values[0], 0, decimal=4) x = np.array([2, 3, 4]) y = np.array([1, 1, 1]) values = self.nfw_e.function(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(values[0], 1.8690403434928538, decimal=5) npt.assert_almost_equal(values[1], 2.6186971904371217, decimal=5) npt.assert_almost_equal(values[2], 3.360273255326431, decimal=5) def test_derivatives(self): x = np.array([1]) y = np.array([2]) Rs = 1. alpha_Rs = 1. q = 1. phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.nfw.derivatives(x, y, Rs, alpha_Rs) f_x_e, f_y_e = self.nfw_e.derivatives(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(f_x[0], f_x_e[0], decimal=5) npt.assert_almost_equal(f_y[0], f_y_e[0], decimal=5) x = np.array([0]) y = np.array([0]) alpha_Rs = 0 f_x, f_y = self.nfw_e.derivatives(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(f_x[0], 0, decimal=5) npt.assert_almost_equal(f_y[0], 0, decimal=5) x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) alpha_Rs = 1. q = .8 phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.nfw_e.derivatives(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(values[0][0], 0.31473652125391116, decimal=5) npt.assert_almost_equal(values[1][0], 0.9835516289184723, decimal=5) npt.assert_almost_equal(values[0][1], 0.7525519008422061, decimal=5) npt.assert_almost_equal(values[1][1], 0.39195411502198224, decimal=5) def test_hessian(self): x = np.array([1]) y = np.array([2]) Rs = 1. alpha_Rs = 1. q = 1. phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_xy, f_yx, f_yy = self.nfw.hessian(x, y, Rs, alpha_Rs) f_xx_e, f_xy_e, f_yx_e, f_yy_e = self.nfw_e.hessian( x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(f_xx[0], f_xx_e[0], decimal=5) npt.assert_almost_equal(f_yy[0], f_yy_e[0], decimal=5) npt.assert_almost_equal(f_xy[0], f_xy_e[0], decimal=5) npt.assert_almost_equal(f_yx[0], f_yx_e[0], decimal=5) x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) q = .8 phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.nfw_e.hessian(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(values[0][0], 0.26355306825820435, decimal=5) npt.assert_almost_equal(values[3][0], -0.008064660050877137, decimal=5) npt.assert_almost_equal(values[1][0], -0.159949276046234, decimal=5) npt.assert_almost_equal(values[0][1], -0.01251554415659939, decimal=5) npt.assert_almost_equal(values[3][1], 0.32051139520206107, decimal=5) npt.assert_almost_equal(values[1][1], -0.13717027513848734, decimal=5) def test_mass_3d_lens(self): R = 1 Rs = 3 alpha_Rs = 1 m_3d = self.nfw_e.mass_3d_lens(R, Rs, alpha_Rs) npt.assert_almost_equal(m_3d, 1.1573795105019022, decimal=8)
class TestNFWELLIPSE(object): """ tests the Gaussian methods """ def setup(self): self.tnfw = TNFW() self.nfw_e = NFW_ELLIPSE() self.tnfw_e = TNFW_ELLIPSE() def test_function(self): x = np.linspace(start=0.1, stop=10, num=10) y = np.linspace(start=0.1, stop=10, num=10) # test round case against TNFW kwargs_tnfw_e_round = { 'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 5, 'e1': 0., 'e2': 0 } kwargs_tnfw_round = {'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 5} f_e = self.tnfw_e.function(x, y, **kwargs_tnfw_e_round) f_r = self.tnfw.function(x, y, **kwargs_tnfw_round) npt.assert_almost_equal(f_e, f_r, decimal=5) # test elliptical case with r_trunc -> infinity against NFW_ELLIPSE kwargs_tnfw_e = { 'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 500, 'e1': 0.2, 'e2': -0.01 } kwargs_nfw_e = {'Rs': 1, 'alpha_Rs': 0.1, 'e1': 0.2, 'e2': -0.01} f_te = self.tnfw_e.function(x, y, **kwargs_tnfw_e) f_e = self.nfw_e.function(x, y, **kwargs_nfw_e) npt.assert_almost_equal(f_te, f_e, decimal=3) def test_derivatives(self): x = np.linspace(start=0.1, stop=10, num=10) y = np.linspace(start=0.1, stop=10, num=10) # test round case against TNFW kwargs_tnfw_e_round = { 'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 5, 'e1': 0., 'e2': 0 } kwargs_tnfw_round = {'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 5} f_xe, f_ye = self.tnfw_e.derivatives(x, y, **kwargs_tnfw_e_round) f_xr, f_yr = self.tnfw.derivatives(x, y, **kwargs_tnfw_round) npt.assert_almost_equal(f_xe, f_xr, decimal=5) npt.assert_almost_equal(f_ye, f_yr, decimal=5) # test elliptical case with r_trunc -> infinity against NFW_ELLIPSE kwargs_tnfw_e = { 'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 500, 'e1': 0.2, 'e2': -0.01 } kwargs_nfw_e = {'Rs': 1, 'alpha_Rs': 0.1, 'e1': 0.2, 'e2': -0.01} out_te = self.tnfw_e.derivatives(x, y, **kwargs_tnfw_e) out_e = self.nfw_e.derivatives(x, y, **kwargs_nfw_e) npt.assert_almost_equal(out_te, out_e, decimal=3) def test_hessian(self): x = np.linspace(start=0.1, stop=10, num=10) y = np.linspace(start=0.1, stop=10, num=10) # test round case against TNFW kwargs_tnfw_e_round = { 'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 5, 'e1': 0., 'e2': 0 } kwargs_tnfw_round = {'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 5} out_e = self.tnfw_e.hessian(x, y, **kwargs_tnfw_e_round) out_r = self.tnfw.hessian(x, y, **kwargs_tnfw_round) npt.assert_almost_equal(out_e, out_r, decimal=4) # test elliptical case with r_trunc -> infinity against NFW_ELLIPSE kwargs_tnfw_e = { 'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 500, 'e1': 0.2, 'e2': -0.01 } kwargs_nfw_e = {'Rs': 1, 'alpha_Rs': 0.1, 'e1': 0.2, 'e2': -0.01} out_te = self.tnfw_e.hessian(x, y, **kwargs_tnfw_e) out_e = self.nfw_e.hessian(x, y, **kwargs_nfw_e) npt.assert_almost_equal(out_te, out_e, decimal=3) def test_mass_3d_lens(self): with npt.assert_raises(ValueError): kwargs_tnfw_e = { 'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 5, 'e1': 0.1, 'e2': -0.02 } self.tnfw_e.mass_3d_lens(1, **kwargs_tnfw_e) def test_density_lens(self): with npt.assert_raises(ValueError): kwargs_tnfw_e = { 'Rs': 1, 'alpha_Rs': 0.1, 'r_trunc': 5, 'e1': 0.1, 'e2': -0.02 } self.tnfw_e.density_lens(1, **kwargs_tnfw_e)
class TestNFWELLIPSE(object): """ tests the Gaussian methods """ def setup(self): self.nfw = NFW() self.nfw_e = NFW_ELLIPSE() def test_function(self): x = np.array([1]) y = np.array([2]) Rs = 1. alpha_Rs = 1. q = 1. phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.nfw.function(x, y, Rs, alpha_Rs) values_e = self.nfw_e.function(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(values[0], values_e[0], decimal=5) x = np.array([0]) y = np.array([0]) q = .8 phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.nfw_e.function(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(values[0], 0, decimal=4) x = np.array([2, 3, 4]) y = np.array([1, 1, 1]) values = self.nfw_e.function(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(values[0], 1.8827504143588476, decimal=5) npt.assert_almost_equal(values[1], 2.6436373117941852, decimal=5) npt.assert_almost_equal(values[2], 3.394127018818891, decimal=5) def test_derivatives(self): x = np.array([1]) y = np.array([2]) Rs = 1. alpha_Rs = 1. q = 1. phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.nfw.derivatives(x, y, Rs, alpha_Rs) f_x_e, f_y_e = self.nfw_e.derivatives(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(f_x[0], f_x_e[0], decimal=5) npt.assert_almost_equal(f_y[0], f_y_e[0], decimal=5) x = np.array([0]) y = np.array([0]) alpha_Rs = 0 f_x, f_y = self.nfw_e.derivatives(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(f_x[0], 0, decimal=5) npt.assert_almost_equal(f_y[0], 0, decimal=5) x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) alpha_Rs = 1. q = .8 phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.nfw_e.derivatives(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(values[0][0], 0.32458737284934414, decimal=5) npt.assert_almost_equal(values[1][0], 0.9737621185480323, decimal=5) npt.assert_almost_equal(values[0][1], 0.76249351329615234, decimal=5) npt.assert_almost_equal(values[1][1], 0.38124675664807617, decimal=5) def test_hessian(self): x = np.array([1]) y = np.array([2]) Rs = 1. alpha_Rs = 1. q = 1. phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_xy, f_yx, f_yy = self.nfw.hessian(x, y, Rs, alpha_Rs) f_xx_e, f_xy_e, f_yx_e, f_yy_e = self.nfw_e.hessian( x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(f_xx[0], f_xx_e[0], decimal=5) npt.assert_almost_equal(f_yy[0], f_yy_e[0], decimal=5) npt.assert_almost_equal(f_xy[0], f_xy_e[0], decimal=5) npt.assert_almost_equal(f_yx[0], f_yx_e[0], decimal=5) x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) q = .8 phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.nfw_e.hessian(x, y, Rs, alpha_Rs, e1, e2) npt.assert_almost_equal(values[0][0], 0.26998576668768592, decimal=5) npt.assert_almost_equal(values[3][0], -0.0045328224507201753, decimal=5) npt.assert_almost_equal(values[1][0], -0.16380454531672584, decimal=5) npt.assert_almost_equal(values[0][1], -0.014833136829928151, decimal=5) npt.assert_almost_equal(values[3][1], 0.31399726446723619, decimal=5) npt.assert_almost_equal(values[1][1], -0.13449884961325154, decimal=5) def test_mass_3d_lens(self): R = 1 Rs = 3 alpha_Rs = 1 m_3d = self.nfw_e.mass_3d_lens(R, Rs, alpha_Rs) npt.assert_almost_equal(m_3d, 1.1573795105019022, decimal=8)
class CompositeSersicNFW(object): """ class for a composite model (Sersic and NFW profile combined) with joint center and parameterization of Einstein radius """ def __init__(self): self.sersic = SersicEllipse() self.nfw = NFW_ELLIPSE() def function(self, x, y, theta_E, mass_light, Rs, q, phi_G, n_sersic, r_eff, q_s, phi_G_s, center_x=0, center_y=0): """ :param theta_E: :param mass_light: :param Rs: :param q: :param phi_G: :param n_sersic: :param r_eff: :param center_x: :param center_y: :return: """ theta_Rs, k_eff = self.convert_mass(theta_E, mass_light, Rs, n_sersic, r_eff) f_s = self.sersic.function(x, y, n_sersic, r_eff, k_eff, q_s, phi_G_s, center_x, center_y) f_nfw = self.nfw.function(x, y, Rs, theta_Rs, q, phi_G, center_x, center_y) return f_s + f_nfw def derivatives(self, x, y, theta_E, mass_light, Rs, q, phi_G, n_sersic, r_eff, q_s, phi_G_s, center_x=0, center_y=0): """ :param theta_E: :param mass_light: :param Rs: :param q: :param phi_G: :param n_sersic: :param r_eff: :param center_x: :param center_y: :return: """ theta_Rs, k_eff = self.convert_mass(theta_E, mass_light, Rs, n_sersic, r_eff) f_x_s, f_y_s = self.sersic.derivatives(x, y, n_sersic, r_eff, k_eff, q_s, phi_G_s, center_x, center_y) f_x_nfw, f_y_nfw = self.nfw.derivatives(x, y, Rs, theta_Rs, q, phi_G, center_x, center_y) return f_x_s + f_x_nfw, f_y_s + f_y_nfw def hessian(self, x, y, theta_E, mass_light, Rs, q, phi_G, n_sersic, r_eff, q_s, phi_G_s, center_x=0, center_y=0): """ :param theta_E: :param mass_light: :param Rs: :param q: :param phi_G: :param n_sersic: :param r_eff: :param center_x: :param center_y: :return: """ theta_Rs, k_eff = self.convert_mass(theta_E, mass_light, Rs, n_sersic, r_eff) f_xx_s, f_yy_s, f_xy_s = self.sersic.hessian(x, y, n_sersic, r_eff, k_eff, q_s, phi_G_s, center_x, center_y) f_xx_nfw, f_yy_nfw, f_xy_nfw = self.nfw.hessian( x, y, Rs, theta_Rs, q, phi_G, center_x, center_y) return f_xx_s + f_xx_nfw, f_yy_s + f_yy_nfw, f_xy_s + f_xy_nfw def convert_mass(self, theta_E, mass_light, Rs, n_sersic, r_eff): """ convert global parameters theta_E and mass_light to specific ones theta_Rs and k_eff :param theta_E: :param mass_light: :param Rs: :param n_sersic: :param r_eff: :return: """ if theta_E < 0.0000001: return 0, 0 alpha_E_sersic, _ = self.sersic.derivatives(theta_E, 0, n_sersic, r_eff, k_eff=1, q=1, phi_G=0) alpha_E_nfw, _ = self.nfw.derivatives(theta_E, 0, Rs, theta_Rs=1, q=1, phi_G=0) #f_xx_s, f_yy_s, _ = self.sersic.hessian(r_eff, 0, n_sersic, r_eff, k_eff=1) #f_xx_n, f_yy_n, _ = self.nfw.hessian(r_eff, 0, Rs, theta_Rs=1, q=1, phi_G=0) #kappa_eff_sersic = (f_xx_s + f_yy_s) / 2. #kappa_eff_nfw = (f_xx_n + f_yy_n) / 2. # equations must satisfy: # theta_Rs * alpha_E_nfw + k_eff * alpha_E_sersic = theta_E # theta_Rs * kappa_eff_nfw / (k_eff * kappa_eff_sersic) = mass_light #k_eff = theta_E * kappa_eff_nfw / (alpha_E_sersic*kappa_eff_nfw+mass_light*alpha_E_nfw*kappa_eff_sersic) #theta_Rs = (theta_E - k_eff*alpha_E_sersic)/alpha_E_nfw theta_Rs = theta_E / alpha_E_nfw / (1 + 1. / mass_light) k_eff = (theta_E - theta_Rs * alpha_E_nfw) / alpha_E_sersic return theta_Rs, k_eff