class TestSPEMD(object): """ tests the Gaussian methods """ def setup(self): from lenstronomy.LensModel.Profiles.pemd import PEMD from lenstronomy.LensModel.Profiles.spep import SPEP self.PEMD = PEMD(suppress_fastell=True) self.SPEP = SPEP() def test_function(self): phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) x = np.array([1.]) y = np.array([2]) a = np.zeros_like(x) values = self.PEMD.function(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(values[0], 2.1571106351401803, decimal=5) else: assert values == 0 a += values x = np.array(1.) y = np.array(2.) a = np.zeros_like(x) values = self.PEMD.function(x, y, phi_E, gamma, e1, e2) print(x, values) a += values if fastell4py_bool: npt.assert_almost_equal(values, 2.1571106351401803, decimal=5) else: assert values == 0 assert type(x) == type(values) x = np.array([2, 3, 4]) y = np.array([1, 1, 1]) values = self.PEMD.function(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(values[0], 2.180188584782964, decimal=7) npt.assert_almost_equal(values[1], 3.2097137160951874, decimal=7) npt.assert_almost_equal(values[2], 4.3109976673748, decimal=7) else: npt.assert_almost_equal(values[0], 0, decimal=7) npt.assert_almost_equal(values[1], 0, decimal=7) npt.assert_almost_equal(values[2], 0, decimal=7) def test_derivatives(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.PEMD.derivatives(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_x[0], 0.46664118422711387, decimal=7) npt.assert_almost_equal(f_y[0], 0.9530892465981603, decimal=7) else: npt.assert_almost_equal(f_x[0], 0, decimal=7) npt.assert_almost_equal(f_y[0], 0, decimal=7) x = np.array([1., 3, 4]) y = np.array([2., 1, 1]) a = np.zeros_like(x) values = self.PEMD.derivatives(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(values[0][0], 0.46664118422711387, decimal=7) npt.assert_almost_equal(values[1][0], 0.9530892465981603, decimal=7) npt.assert_almost_equal(values[0][1], 1.0722265330847958, decimal=7) npt.assert_almost_equal(values[1][1], 0.3140067377020791, decimal=7) else: npt.assert_almost_equal(values[0][0], 0, decimal=7) npt.assert_almost_equal(values[1][0], 0, decimal=7) npt.assert_almost_equal(values[0][1], 0, decimal=7) npt.assert_almost_equal(values[1][1], 0, decimal=7) a += values[0] x = 1. y = 2. phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.PEMD.derivatives(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_x, 0.46664118422711387, decimal=7) npt.assert_almost_equal(f_y, 0.9530892465981603, decimal=7) else: npt.assert_almost_equal(f_x, 0, decimal=7) npt.assert_almost_equal(f_y, 0, decimal=7) x = 0. y = 0. f_x, f_y = self.PEMD.derivatives(x, y, phi_E, gamma, e1, e2) assert f_x == 0. assert f_y == 0. def test_hessian(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_xy, f_yx, f_yy = self.PEMD.hessian(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_xx, 0.4179041, decimal=7) npt.assert_almost_equal(f_yy, 0.1404714, decimal=7) npt.assert_almost_equal(f_xy, -0.1856134, decimal=7) else: npt.assert_almost_equal(f_xx, 0, decimal=7) npt.assert_almost_equal(f_yy, 0, decimal=7) npt.assert_almost_equal(f_xy, 0, decimal=7) npt.assert_almost_equal(f_xy, f_yx, decimal=8) x = 1. y = 2. phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) a = np.zeros_like(x) f_xx, f_xy, f_yx, f_yy = self.PEMD.hessian(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_xx, 0.41790408341142493, decimal=7) npt.assert_almost_equal(f_yy, 0.14047143086334482, decimal=7) npt.assert_almost_equal(f_xy, -0.1856133848300859, decimal=7) else: npt.assert_almost_equal(f_xx, 0, decimal=7) npt.assert_almost_equal(f_yy, 0, decimal=7) npt.assert_almost_equal(f_xy, 0, decimal=7) a += f_xx x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) values = self.PEMD.hessian(x, y, phi_E, gamma, e1, e2) print(values, 'values') if fastell4py_bool: npt.assert_almost_equal(values[0][0], 0.41789957732890953, decimal=5) npt.assert_almost_equal(values[3][0], 0.14047593655054141, decimal=5) npt.assert_almost_equal(values[1][0], -0.18560737698052343, decimal=5) npt.assert_almost_equal(values[0][1], 0.068359818958208918, decimal=5) npt.assert_almost_equal(values[3][1], 0.32494089371516482, decimal=5) npt.assert_almost_equal(values[1][1], -0.097845438684594374, decimal=5) else: npt.assert_almost_equal(values[0][0], 0, decimal=7) def test_spep_spemd(self): x = np.array([1]) y = np.array([0]) phi_E = 1. gamma = 2. q = 1. phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.PEMD.derivatives(x, y, phi_E, gamma, e1, e2) f_x_spep, f_y_spep = self.SPEP.derivatives(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_x[0], f_x_spep[0], decimal=2) else: pass theta_E = 2. gamma = 2. q = 1. phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.PEMD.derivatives(x, y, theta_E, gamma, e1, e2) f_x_spep, f_y_spep = self.SPEP.derivatives(x, y, theta_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_x[0], f_x_spep[0], decimal=2) else: pass theta_E = 2. gamma = 1.7 q = 1. phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.PEMD.derivatives(x, y, theta_E, gamma, e1, e2) f_x_spep, f_y_spep = self.SPEP.derivatives(x, y, theta_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_x[0], f_x_spep[0], decimal=4) def test_bounds(self): from lenstronomy.LensModel.Profiles.spemd import SPEMD profile = SPEMD(suppress_fastell=True) compute_bool = profile._parameter_constraints(q_fastell=-1, gam=-1, s2=-1, q=-1) assert compute_bool is False def test_is_not_empty(self): func = self.PEMD.spemd_smooth.is_not_empty assert func(0.1, 0.2) assert func([0.1], [0.2]) assert func((0.1, 0.3), (0.2, 0.4)) assert func(np.array([0.1]), np.array([0.2])) assert not func([], []) assert not func(np.array([]), np.array([])) def test_density_lens(self): r = 1 kwargs = {'theta_E': 1, 'gamma': 2, 'e1': 0, 'e2': 0} rho = self.PEMD.density_lens(r, **kwargs) rho_spep = self.SPEP.density_lens(r, **kwargs) npt.assert_almost_equal(rho, rho_spep, decimal=7)
class TestNIE_POTENTIAL(object): """ tests the NIE_POTENTIAL profile for different rotations """ def setup(self): self.nie_potential = NIE_POTENTIAL() self.spep = SPEP() def test_function(self): y = np.array([1., 2]) x = np.array([0., 0.]) theta_E = 1. theta_c = 0. ############# # no rotation ############# e1, e2 = 0.05, 0.0 eps = np.sqrt(e1**2 + e2**2) phi_G, q = param_util.ellipticity2phi_q(e1, e2) # map the nie_potential input to the spep input gamma_spep = 2. q_spep = np.sqrt(q) e1_spep, e2_spep = param_util.phi_q2_ellipticity(phi_G, q_spep) theta_E_conv = self.nie_potential._theta_q_convert(theta_E, q) theta_E_spep = theta_E_conv * np.sqrt(1 - eps) / ((1 - eps) / (1 + eps))**0.25 # compare the non-rotated output values = self.nie_potential.function(x, y, theta_E, theta_c, e1, e2) delta_pot = values[1] - values[0] values = self.spep.function(x, y, theta_E_spep, gamma_spep, e1_spep, e2_spep) delta_pot_spep = values[1] - values[0] npt.assert_almost_equal(delta_pot, delta_pot_spep, decimal=4) ############ # rotation 1 ############ e1, e2 = 0.05, 0.1 eps = np.sqrt(e1**2 + e2**2) phi_G, q = param_util.ellipticity2phi_q(e1, e2) # map the nie_potential input to the spep input gamma_spep = 2. q_spep = np.sqrt(q) e1_spep, e2_spep = param_util.phi_q2_ellipticity(phi_G, q_spep) theta_E_conv = self.nie_potential._theta_q_convert(theta_E, q) theta_E_spep = theta_E_conv * np.sqrt(1 - eps) / ((1 - eps) / (1 + eps))**0.25 # compare the rotated output values = self.nie_potential.function(x, y, theta_E, theta_c, e1, e2) delta_pot = values[1] - values[0] values = self.spep.function(x, y, theta_E_spep, gamma_spep, e1_spep, e2_spep) delta_pot_spep = values[1] - values[0] npt.assert_almost_equal(delta_pot, delta_pot_spep, decimal=4) ############ # rotation 2 ############ e1, e2 = 0.15, 0.13 eps = np.sqrt(e1**2 + e2**2) phi_G, q = param_util.ellipticity2phi_q(e1, e2) # map the nie_potential input to the spep input gamma_spep = 2. q_spep = np.sqrt(q) e1_spep, e2_spep = param_util.phi_q2_ellipticity(phi_G, q_spep) theta_E_conv = self.nie_potential._theta_q_convert(theta_E, q) theta_E_spep = theta_E_conv * np.sqrt(1 - eps) / ((1 - eps) / (1 + eps))**0.25 # compare the rotated output values = self.nie_potential.function(x, y, theta_E, theta_c, e1, e2) delta_pot = values[1] - values[0] values = self.spep.function(x, y, theta_E_spep, gamma_spep, e1_spep, e2_spep) delta_pot_spep = values[1] - values[0] npt.assert_almost_equal(delta_pot, delta_pot_spep, decimal=4) def test_derivatives(self): x = np.array([1]) y = np.array([2]) theta_E = 1. theta_c = 0. ############# # no rotation ############# e1, e2 = 0.05, 0.0 eps = np.sqrt(e1**2 + e2**2) phi_G, q = param_util.ellipticity2phi_q(e1, e2) # map the nie_potential input to the spep input gamma_spep = 2. q_spep = np.sqrt(q) e1_spep, e2_spep = param_util.phi_q2_ellipticity(phi_G, q_spep) theta_E_conv = self.nie_potential._theta_q_convert(theta_E, q) theta_E_spep = theta_E_conv * np.sqrt(1 - eps) / ((1 - eps) / (1 + eps))**0.25 # compare the non-rotated output f_x, f_y = self.nie_potential.derivatives(x, y, theta_E, theta_c, e1, e2) f_x_nie, f_y_nie = self.spep.derivatives(x, y, theta_E_spep, gamma_spep, e1_spep, e2_spep) npt.assert_almost_equal(f_x, f_x_nie, decimal=4) npt.assert_almost_equal(f_y, f_y_nie, decimal=4) ############ # rotation 1 ############ e1, e2 = 0.05, 0.1 eps = np.sqrt(e1**2 + e2**2) phi_G, q = param_util.ellipticity2phi_q(e1, e2) # map the nie_potential input to the spep input gamma_spep = 2. q_spep = np.sqrt(q) e1_spep, e2_spep = param_util.phi_q2_ellipticity(phi_G, q_spep) theta_E_conv = self.nie_potential._theta_q_convert(theta_E, q) theta_E_spep = theta_E_conv * np.sqrt(1 - eps) / ((1 - eps) / (1 + eps))**0.25 # compare the rotated output f_x, f_y = self.nie_potential.derivatives(x, y, theta_E, theta_c, e1, e2) f_x_nie, f_y_nie = self.spep.derivatives(x, y, theta_E_spep, gamma_spep, e1_spep, e2_spep) npt.assert_almost_equal(f_x, f_x_nie, decimal=4) npt.assert_almost_equal(f_y, f_y_nie, decimal=4) ############ # rotation 2 ############ e1, e2 = 0.15, 0.13 eps = np.sqrt(e1**2 + e2**2) phi_G, q = param_util.ellipticity2phi_q(e1, e2) # map the nie_potential input to the spep input gamma_spep = 2. q_spep = np.sqrt(q) e1_spep, e2_spep = param_util.phi_q2_ellipticity(phi_G, q_spep) theta_E_conv = self.nie_potential._theta_q_convert(theta_E, q) theta_E_spep = theta_E_conv * np.sqrt(1 - eps) / ((1 - eps) / (1 + eps))**0.25 # compare the rotated output f_x, f_y = self.nie_potential.derivatives(x, y, theta_E, theta_c, e1, e2) f_x_nie, f_y_nie = self.spep.derivatives(x, y, theta_E_spep, gamma_spep, e1_spep, e2_spep) npt.assert_almost_equal(f_x, f_x_nie, decimal=4) npt.assert_almost_equal(f_y, f_y_nie, decimal=4) def test_hessian(self): x = np.array([1]) y = np.array([2]) theta_E = 1. theta_c = 0. ############# # no rotation ############# e1, e2 = 0.05, 0.0 eps = np.sqrt(e1**2 + e2**2) phi_G, q = param_util.ellipticity2phi_q(e1, e2) # map the nie_potential input to the spep input gamma_spep = 2. q_spep = np.sqrt(q) e1_spep, e2_spep = param_util.phi_q2_ellipticity(phi_G, q_spep) theta_E_conv = self.nie_potential._theta_q_convert(theta_E, q) theta_E_spep = theta_E_conv * np.sqrt(1 - eps) / ((1 - eps) / (1 + eps))**0.25 # compare the non-rotated output f_xx, f_xy, f_yx, f_yy = self.nie_potential.hessian( x, y, theta_E, theta_c, e1, e2) f_xx_nie, f_xy_nie, f_yx_nie, f_yy_nie = self.spep.hessian( x, y, theta_E_spep, gamma_spep, e1_spep, e2_spep) npt.assert_almost_equal(f_xx, f_xx_nie, decimal=4) npt.assert_almost_equal(f_yy, f_yy_nie, decimal=4) npt.assert_almost_equal(f_xy, f_xy_nie, decimal=4) npt.assert_almost_equal(f_yx, f_yx_nie, decimal=4) ############ # rotation 1 ############ e1, e2 = 0.05, 0.1 eps = np.sqrt(e1**2 + e2**2) phi_G, q = param_util.ellipticity2phi_q(e1, e2) # map the nie_potential input to the spep input gamma_spep = 2. q_spep = np.sqrt(q) e1_spep, e2_spep = param_util.phi_q2_ellipticity(phi_G, q_spep) theta_E_conv = self.nie_potential._theta_q_convert(theta_E, q) theta_E_spep = theta_E_conv * np.sqrt(1 - eps) / ((1 - eps) / (1 + eps))**0.25 # compare the rotated output f_xx, f_xy, f_yx, f_yy = self.nie_potential.hessian( x, y, theta_E, theta_c, e1, e2) f_xx_nie, f_xy_nie, f_yx_nie, f_yy_nie = self.spep.hessian( x, y, theta_E_spep, gamma_spep, e1_spep, e2_spep) npt.assert_almost_equal(f_xx, f_xx_nie, decimal=4) npt.assert_almost_equal(f_yy, f_yy_nie, decimal=4) npt.assert_almost_equal(f_xy, f_xy_nie, decimal=4) npt.assert_almost_equal(f_yx, f_yx_nie, decimal=4) ############ # rotation 2 ############ e1, e2 = 0.15, 0.13 eps = np.sqrt(e1**2 + e2**2) phi_G, q = param_util.ellipticity2phi_q(e1, e2) # map the nie_potential input to the spep input gamma_spep = 2. q_spep = np.sqrt(q) e1_spep, e2_spep = param_util.phi_q2_ellipticity(phi_G, q_spep) theta_E_conv = self.nie_potential._theta_q_convert(theta_E, q) theta_E_spep = theta_E_conv * np.sqrt(1 - eps) / ((1 - eps) / (1 + eps))**0.25 # compare the rotated output f_xx, f_xy, f_yx, f_yy = self.nie_potential.hessian( x, y, theta_E, theta_c, e1, e2) f_xx_nie, f_xy_nie, f_yx_nie, f_yy_nie = self.spep.hessian( x, y, theta_E_spep, gamma_spep, e1_spep, e2_spep) npt.assert_almost_equal(f_xx, f_xx_nie, decimal=4) npt.assert_almost_equal(f_yy, f_yy_nie, decimal=4) npt.assert_almost_equal(f_xy, f_xy_nie, decimal=4) npt.assert_almost_equal(f_yx, f_yx_nie, decimal=4) def test_static(self): x, y = 1., 1. phi_G, q = 0.3, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_lens = {'theta_E': 1., 'theta_c': .1, 'e1': e1, 'e2': e2} f_ = self.nie_potential.function(x, y, **kwargs_lens) self.nie_potential.set_static(**kwargs_lens) f_static = self.nie_potential.function(x, y, **kwargs_lens) npt.assert_almost_equal(f_, f_static, decimal=8) self.nie_potential.set_dynamic() kwargs_lens = {'theta_E': 2., 'theta_c': .1, 'e1': e1, 'e2': e2} f_dyn = self.nie_potential.function(x, y, **kwargs_lens) assert f_dyn != f_static
class TestSPEP(object): """ tests the Gaussian methods """ def setup(self): self.SPEP = SPEP() self.SIE = SIE() def test_function(self): x = 1 y = 2 phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.SPEP.function(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(values, 2.104213947346917, decimal=7) x = np.array([0]) y = np.array([0]) values = self.SPEP.function(x, y, phi_E, gamma, e1, e2) assert values[0] == 0 x = np.array([2, 3, 4]) y = np.array([1, 1, 1]) values = self.SPEP.function(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(values[0], 2.1709510681181285, decimal=7) npt.assert_almost_equal(values[1], 3.2293397784259108, decimal=7) npt.assert_almost_equal(values[2], 4.3624056004556948, decimal=7) def test_derivatives(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.SPEP.derivatives(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(f_x[0], 0.43989645846696634, decimal=7) npt.assert_almost_equal(f_y[0], 0.93736944180732129, decimal=7) x = np.array([0]) y = np.array([0]) f_x, f_y = self.SPEP.derivatives(x, y, phi_E, gamma, e1, e2) assert f_x[0] == 0 assert f_y[0] == 0 x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) values = self.SPEP.derivatives(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(values[0][0], 0.43989645846696634, decimal=7) npt.assert_almost_equal(values[1][0], 0.93736944180732129, decimal=7) npt.assert_almost_equal(values[0][1], 1.1029501948308649, decimal=7) npt.assert_almost_equal(values[1][1], 0.24342317177590794, decimal=7) x = 1 y = 2 phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.SPEP.derivatives(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(f_x, 0.43989645846696634, decimal=7) npt.assert_almost_equal(f_y, 0.93736944180732129, decimal=7) x = 0 y = 0 f_x, f_y = self.SPEP.derivatives(x, y, phi_E, gamma, e1, e2) assert f_x == 0 assert f_y == 0 def test_hessian(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_yy,f_xy = self.SPEP.hessian(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(f_xx[0], 0.46312881977317422, decimal=7) npt.assert_almost_equal(f_yy[0], 0.15165326557198552, decimal=7) npt.assert_almost_equal(f_xy[0], -0.20956958696323871, decimal=7) x = np.array([1,3,4]) y = np.array([2,1,1]) values = self.SPEP.hessian(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(values[0][0], 0.46312881977317422, decimal=7) npt.assert_almost_equal(values[1][0], 0.15165326557198552, decimal=7) npt.assert_almost_equal(values[2][0], -0.20956958696323871, decimal=7) npt.assert_almost_equal(values[0][1], 0.070999592014527796, decimal=7) npt.assert_almost_equal(values[1][1], 0.33245358685908111, decimal=7) npt.assert_almost_equal(values[2][1], -0.10270375656049677, decimal=7) def test_spep_sie_conventions(self): x = np.array([1., 2., 0.]) y = np.array([2, 1., 1.]) phi_E = 1. gamma = 2 q = 0.9999 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_yy, f_xy = self.SPEP.hessian(x, y, phi_E, gamma, e1, e2) f_xx_sie, f_yy_sie, f_xy_sie = self.SIE.hessian(x, y, phi_E, e1, e2) npt.assert_almost_equal(f_xx, f_xx_sie, decimal=4) npt.assert_almost_equal(f_yy, f_yy_sie, decimal=4) npt.assert_almost_equal(f_xy, f_xy_sie, decimal=4)
class TestSPEP(object): """ tests the Gaussian methods """ def setup(self): self.SPEP = SPEP() self.SPP = SPP() self.SIS = SIS() def test_function(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 1 phi_G = 0. E = phi_E / (((3 - gamma) / 2.)**(1. / (1 - gamma)) * np.sqrt(q)) values_spep = self.SPEP.function(x, y, E, gamma, q, phi_G) values_spp = self.SPP.function(x, y, E, gamma) assert values_spep[0] == values_spp[0] x = np.array([0]) y = np.array([0]) values_spep = self.SPEP.function(x, y, E, gamma, q, phi_G) values_spp = self.SPP.function(x, y, E, gamma) assert values_spep[0] == values_spp[0] x = np.array([2, 3, 4]) y = np.array([1, 1, 1]) values_spep = self.SPEP.function(x, y, E, gamma, q, phi_G) values_spp = self.SPP.function(x, y, E, gamma) assert values_spep[0] == values_spp[0] assert values_spep[1] == values_spp[1] assert values_spep[2] == values_spp[2] def test_derivatives(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 1 phi_G = 0. E = phi_E / (((3 - gamma) / 2.)**(1. / (1 - gamma)) * np.sqrt(q)) f_x_spep, f_y_spep = self.SPEP.derivatives(x, y, E, gamma, q, phi_G) f_x_spp, f_y_spp = self.SPP.derivatives(x, y, E, gamma) assert f_x_spep[0] == f_x_spp[0] assert f_y_spep[0] == f_y_spp[0] x = np.array([0]) y = np.array([0]) f_x_spep, f_y_spep = self.SPEP.derivatives(x, y, E, gamma, q, phi_G) f_x_spp, f_y_spp = self.SPP.derivatives(x, y, E, gamma) assert f_x_spep[0] == f_x_spp[0] assert f_y_spep[0] == f_y_spp[0] x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) f_x_spep, f_y_spep = self.SPEP.derivatives(x, y, E, gamma, q, phi_G) f_x_spp, f_y_spp = self.SPP.derivatives(x, y, E, gamma) assert f_x_spep[0] == f_x_spp[0] assert f_y_spep[0] == f_y_spp[0] assert f_x_spep[1] == f_x_spp[1] assert f_y_spep[1] == f_y_spp[1] assert f_x_spep[2] == f_x_spp[2] assert f_y_spep[2] == f_y_spp[2] def test_hessian(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 1. phi_G = 0. E = phi_E / (((3 - gamma) / 2.)**(1. / (1 - gamma)) * np.sqrt(q)) f_xx, f_yy, f_xy = self.SPEP.hessian(x, y, E, gamma, q, phi_G) f_xx_spep, f_yy_spep, f_xy_spep = self.SPEP.hessian( x, y, E, gamma, q, phi_G) f_xx_spp, f_yy_spp, f_xy_spp = self.SPP.hessian(x, y, E, gamma) assert f_xx_spep[0] == f_xx_spp[0] assert f_yy_spep[0] == f_yy_spp[0] assert f_xy_spep[0] == f_xy_spp[0] x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) f_xx_spep, f_yy_spep, f_xy_spep = self.SPEP.hessian( x, y, E, gamma, q, phi_G) f_xx_spp, f_yy_spp, f_xy_spp = self.SPP.hessian(x, y, E, gamma) assert f_xx_spep[0] == f_xx_spp[0] assert f_yy_spep[0] == f_yy_spp[0] assert f_xy_spep[0] == f_xy_spp[0] assert f_xx_spep[1] == f_xx_spp[1] assert f_yy_spep[1] == f_yy_spp[1] assert f_xy_spep[1] == f_xy_spp[1] assert f_xx_spep[2] == f_xx_spp[2] assert f_yy_spep[2] == f_yy_spp[2] assert f_xy_spep[2] == f_xy_spp[2] def test_compare_sis(self): x = np.array([1]) y = np.array([2]) theta_E = 1. gamma = 2. f_sis = self.SIS.function(x, y, theta_E) f_spp = self.SPP.function(x, y, theta_E, gamma) f_x_sis, f_y_sis = self.SIS.derivatives(x, y, theta_E) f_x_spp, f_y_spp = self.SPP.derivatives(x, y, theta_E, gamma) f_xx_sis, f_yy_sis, f_xy_sis = self.SIS.hessian(x, y, theta_E) f_xx_spp, f_yy_spp, f_xy_spp = self.SPP.hessian(x, y, theta_E, gamma) npt.assert_almost_equal(f_sis[0], f_spp[0], decimal=7) npt.assert_almost_equal(f_x_sis[0], f_x_spp[0], decimal=7) npt.assert_almost_equal(f_y_sis[0], f_y_spp[0], decimal=7) npt.assert_almost_equal(f_xx_sis[0], f_xx_spp[0], decimal=7) npt.assert_almost_equal(f_yy_sis[0], f_yy_spp[0], decimal=7) npt.assert_almost_equal(f_xy_sis[0], f_xy_spp[0], decimal=7) def test_unit_conversion(self): theta_E = 2. gamma = 2.2 rho0 = self.SPP.theta2rho(theta_E, gamma) theta_E_out = self.SPP.rho2theta(rho0, gamma) assert theta_E == theta_E_out def test_mass_2d_lens(self): r = 1 theta_E = 1 gamma = 2 m_2d = self.SPP.mass_2d_lens(r, theta_E, gamma) npt.assert_almost_equal(m_2d, 3.1415926535897931, decimal=8) def test_grav_pot(self): x, y = 1, 0 rho0 = 1 gamma = 2 grav_pot = self.SPP.grav_pot(x, y, rho0, gamma, center_x=0, center_y=0) npt.assert_almost_equal(grav_pot, 12.566370614359172, decimal=8)
class TestSPEMD(object): """ tests the Gaussian methods """ def setup(self): from lenstronomy.LensModel.Profiles.spemd import SPEMD from lenstronomy.LensModel.Profiles.spep import SPEP self.SPEMD = SPEMD() self.SPEP = SPEP() def test_function(self): phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) x = np.array([1.]) y = np.array([2]) a = np.zeros_like(x) values = self.SPEMD.function(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: assert values == 2.1567297115381039 else: assert values == 0 a += values x = np.array(1.) y = np.array(2.) a = np.zeros_like(x) values = self.SPEMD.function(x, y, phi_E, gamma, e1, e2) print(x, values) a += values if fastell4py_bool: assert values == 2.1567297115381039 else: assert values == 0 assert type(x) == type(values) x = np.array([2, 3, 4]) y = np.array([1, 1, 1]) values = self.SPEMD.function(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(values[0], 2.1798076611034141, decimal=7) npt.assert_almost_equal(values[1], 3.209319798597186, decimal=7) npt.assert_almost_equal(values[2], 4.3105937398856398, decimal=7) else: npt.assert_almost_equal(values[0], 0, decimal=7) npt.assert_almost_equal(values[1], 0, decimal=7) npt.assert_almost_equal(values[2], 0, decimal=7) def test_derivatives(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.SPEMD.derivatives(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_x[0], 0.46663367437984204, decimal=7) npt.assert_almost_equal(f_y[0], 0.95307422686028065, decimal=7) else: npt.assert_almost_equal(f_x[0], 0, decimal=7) npt.assert_almost_equal(f_y[0], 0, decimal=7) x = np.array([1., 3, 4]) y = np.array([2., 1, 1]) a = np.zeros_like(x) values = self.SPEMD.derivatives(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(values[0][0], 0.46663367437984204, decimal=7) npt.assert_almost_equal(values[1][0], 0.95307422686028065, decimal=7) npt.assert_almost_equal(values[0][1], 1.0722152681324291, decimal=7) npt.assert_almost_equal(values[1][1], 0.31400298272329669, decimal=7) else: npt.assert_almost_equal(values[0][0], 0, decimal=7) npt.assert_almost_equal(values[1][0], 0, decimal=7) npt.assert_almost_equal(values[0][1], 0, decimal=7) npt.assert_almost_equal(values[1][1], 0, decimal=7) a += values[0] x = 1. y = 2. phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.SPEMD.derivatives(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_x, 0.46663367437984204, decimal=7) npt.assert_almost_equal(f_y, 0.95307422686028065, decimal=7) else: npt.assert_almost_equal(f_x, 0, decimal=7) npt.assert_almost_equal(f_y, 0, decimal=7) x = 0. y = 0. f_x, f_y = self.SPEMD.derivatives(x, y, phi_E, gamma, e1, e2) assert f_x == 0. assert f_y == 0. def test_hessian(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_yy, f_xy = self.SPEMD.hessian(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_xx, 0.41789957732890953, decimal=7) npt.assert_almost_equal(f_yy, 0.14047593655054141, decimal=7) npt.assert_almost_equal(f_xy, -0.18560737698052343, decimal=7) else: npt.assert_almost_equal(f_xx, 0, decimal=7) npt.assert_almost_equal(f_yy, 0, decimal=7) npt.assert_almost_equal(f_xy, 0, decimal=7) x = 1. y = 2. phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) a = np.zeros_like(x) f_xx, f_yy, f_xy = self.SPEMD.hessian(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_xx, 0.41789957732890953, decimal=7) npt.assert_almost_equal(f_yy, 0.14047593655054141, decimal=7) npt.assert_almost_equal(f_xy, -0.18560737698052343, decimal=7) else: npt.assert_almost_equal(f_xx, 0, decimal=7) npt.assert_almost_equal(f_yy, 0, decimal=7) npt.assert_almost_equal(f_xy, 0, decimal=7) a += f_xx x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) values = self.SPEMD.hessian(x, y, phi_E, gamma, e1, e2) print(values, 'values') if fastell4py_bool: npt.assert_almost_equal(values[0][0], 0.41789957732890953, decimal=7) npt.assert_almost_equal(values[1][0], 0.14047593655054141, decimal=7) npt.assert_almost_equal(values[2][0], -0.18560737698052343, decimal=7) npt.assert_almost_equal(values[0][1], 0.068359818958208918, decimal=7) npt.assert_almost_equal(values[1][1], 0.32494089371516482, decimal=7) npt.assert_almost_equal(values[2][1], -0.097845438684594374, decimal=7) else: npt.assert_almost_equal(values[0][0], 0, decimal=7) def test_spep_spemd(self): x = np.array([1]) y = np.array([0]) phi_E = 1. gamma = 2. q = 1. phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.SPEMD.derivatives(x, y, phi_E, gamma, e1, e2) f_x_spep, f_y_spep = self.SPEP.derivatives(x, y, phi_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_x[0], f_x_spep[0], decimal=2) else: pass theta_E = 2. gamma = 2. q = 1. phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.SPEMD.derivatives(x, y, theta_E, gamma, e1, e2) f_x_spep, f_y_spep = self.SPEP.derivatives(x, y, theta_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_x[0], f_x_spep[0], decimal=2) else: pass theta_E = 2. gamma = 1.7 q = 1. phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.SPEMD.derivatives(x, y, theta_E, gamma, e1, e2) f_x_spep, f_y_spep = self.SPEP.derivatives(x, y, theta_E, gamma, e1, e2) if fastell4py_bool: npt.assert_almost_equal(f_x[0], f_x_spep[0], decimal=4) def test_bounds(self): from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH profile = SPEMD_SMOOTH() theta_E, gamma, q, phi_G, s_scale = profile._parameter_constraints( theta_E=-1, s_scale=0, gamma=3, q=2, phi_G=0) assert theta_E == 0 def test_is_not_empty(self): func = self.SPEMD.spemd_smooth.is_not_empty assert func(0.1, 0.2) assert func([0.1], [0.2]) assert func((0.1, 0.3), (0.2, 0.4)) assert func(np.array([0.1]), np.array([0.2])) assert not func([], []) assert not func(np.array([]), np.array([])) def test_density_lens(self): r = 1 kwargs = {'theta_E': 1, 'gamma': 2, 'e1': 0, 'e2': 0} rho = self.SPEMD.density_lens(r, **kwargs) rho_spep = self.SPEP.density_lens(r, **kwargs) npt.assert_almost_equal(rho, rho_spep, decimal=7)
class TestSPEMD(object): """ tests the Gaussian methods """ def setup(self): from lenstronomy.LensModel.Profiles.spemd import SPEMD from lenstronomy.LensModel.Profiles.spep import SPEP self.SPEMD = SPEMD() self.SPEP = SPEP() def test_function(self): phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. x = np.array([1.]) y = np.array([2]) a = np.zeros_like(x) values = self.SPEMD.function(x, y, phi_E, gamma, q, phi_G) if fastell4py_bool: assert values == 2.1567297115381039 else: assert values == 0 a += values x = np.array(1.) y = np.array(2.) a = np.zeros_like(x) values = self.SPEMD.function(x, y, phi_E, gamma, q, phi_G) print(x, values) a += values if fastell4py_bool: assert values == 2.1567297115381039 else: assert values == 0 assert type(x) == type(values) x = np.array([2, 3, 4]) y = np.array([1, 1, 1]) values = self.SPEMD.function(x, y, phi_E, gamma, q, phi_G) if fastell4py_bool: npt.assert_almost_equal(values[0], 2.1798076611034141, decimal=7) npt.assert_almost_equal(values[1], 3.209319798597186, decimal=7) npt.assert_almost_equal(values[2], 4.3105937398856398, decimal=7) else: npt.assert_almost_equal(values[0], 0, decimal=7) npt.assert_almost_equal(values[1], 0, decimal=7) npt.assert_almost_equal(values[2], 0, decimal=7) def test_derivatives(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. f_x, f_y = self.SPEMD.derivatives(x, y, phi_E, gamma, q, phi_G) if fastell4py_bool: npt.assert_almost_equal(f_x[0], 0.46663367437984204, decimal=7) npt.assert_almost_equal(f_y[0], 0.95307422686028065, decimal=7) else: npt.assert_almost_equal(f_x[0], 0, decimal=7) npt.assert_almost_equal(f_y[0], 0, decimal=7) x = np.array([1., 3, 4]) y = np.array([2., 1, 1]) a = np.zeros_like(x) values = self.SPEMD.derivatives(x, y, phi_E, gamma, q, phi_G) if fastell4py_bool: npt.assert_almost_equal(values[0][0], 0.46663367437984204, decimal=7) npt.assert_almost_equal(values[1][0], 0.95307422686028065, decimal=7) npt.assert_almost_equal(values[0][1], 1.0722152681324291, decimal=7) npt.assert_almost_equal(values[1][1], 0.31400298272329669, decimal=7) else: npt.assert_almost_equal(values[0][0], 0, decimal=7) npt.assert_almost_equal(values[1][0], 0, decimal=7) npt.assert_almost_equal(values[0][1], 0, decimal=7) npt.assert_almost_equal(values[1][1], 0, decimal=7) a += values[0] x = 1. y = 2. phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. f_x, f_y = self.SPEMD.derivatives(x, y, phi_E, gamma, q, phi_G) if fastell4py_bool: npt.assert_almost_equal(f_x, 0.46663367437984204, decimal=7) npt.assert_almost_equal(f_y, 0.95307422686028065, decimal=7) else: npt.assert_almost_equal(f_x, 0, decimal=7) npt.assert_almost_equal(f_y, 0, decimal=7) x = 0. y = 0. f_x, f_y = self.SPEMD.derivatives(x, y, phi_E, gamma, q, phi_G) assert f_x == 0. assert f_y == 0. def test_hessian(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. f_xx, f_yy,f_xy = self.SPEMD.hessian(x, y, phi_E, gamma, q, phi_G) if fastell4py_bool: npt.assert_almost_equal(f_xx, 0.41789957732890953, decimal=7) npt.assert_almost_equal(f_yy, 0.14047593655054141, decimal=7) npt.assert_almost_equal(f_xy, -0.18560737698052343, decimal=7) else: npt.assert_almost_equal(f_xx, 0, decimal=7) npt.assert_almost_equal(f_yy, 0, decimal=7) npt.assert_almost_equal(f_xy, 0, decimal=7) x = 1. y = 2. phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. a = np.zeros_like(x) f_xx, f_yy,f_xy = self.SPEMD.hessian(x, y, phi_E, gamma, q, phi_G) if fastell4py_bool: npt.assert_almost_equal(f_xx, 0.41789957732890953, decimal=7) npt.assert_almost_equal(f_yy, 0.14047593655054141, decimal=7) npt.assert_almost_equal(f_xy, -0.18560737698052343, decimal=7) else: npt.assert_almost_equal(f_xx, 0, decimal=7) npt.assert_almost_equal(f_yy, 0, decimal=7) npt.assert_almost_equal(f_xy, 0, decimal=7) a += f_xx x = np.array([1,3,4]) y = np.array([2,1,1]) values = self.SPEMD.hessian(x, y, phi_E, gamma, q, phi_G) print(values, 'values') if fastell4py_bool: npt.assert_almost_equal(values[0][0], 0.41789957732890953, decimal=7) npt.assert_almost_equal(values[1][0], 0.14047593655054141, decimal=7) npt.assert_almost_equal(values[2][0], -0.18560737698052343, decimal=7) npt.assert_almost_equal(values[0][1], 0.068359818958208918, decimal=7) npt.assert_almost_equal(values[1][1], 0.32494089371516482, decimal=7) npt.assert_almost_equal(values[2][1], -0.097845438684594374, decimal=7) else: npt.assert_almost_equal(values[0][0], 0, decimal=7) def test_spep_spemd(self): x = np.array([1]) y = np.array([0]) phi_E = 1. gamma = 2. q = 1. phi_G = 1. f_x, f_y = self.SPEMD.derivatives(x, y, phi_E, gamma, q, phi_G) f_x_spep, f_y_spep = self.SPEP.derivatives(x, y, phi_E, gamma, q, phi_G) if fastell4py_bool: npt.assert_almost_equal(f_x[0], f_x_spep[0], decimal=2) else: pass theta_E = 2. gamma = 2. q = 1. phi_G = 1. f_x, f_y = self.SPEMD.derivatives(x, y, theta_E, gamma, q, phi_G) f_x_spep, f_y_spep = self.SPEP.derivatives(x, y, theta_E, gamma, q, phi_G) if fastell4py_bool: npt.assert_almost_equal(f_x[0], f_x_spep[0], decimal=2) else: pass theta_E = 2. gamma = 1.7 q = 1. phi_G = 1. f_x, f_y = self.SPEMD.derivatives(x, y, theta_E, gamma, q, phi_G) f_x_spep, f_y_spep = self.SPEP.derivatives(x, y, theta_E, gamma, q, phi_G) if fastell4py_bool: npt.assert_almost_equal(f_x[0], f_x_spep[0], decimal=4)