def setup(self): from lenstronomy.LensModel.Profiles.nie import NIE from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH from lenstronomy.LensModel.Profiles.sis import SIS self.nie = NIE() self.spemd = SPEMD_SMOOTH() self.sis = SIS()
class SPEMD(object): """ class for smooth power law ellipse mass density profile """ param_names = ['theta_E', 'gamma', 'e1', 'e2', 'center_x', 'center_y'] lower_limit_default = {'theta_E': 0, 'gamma': 0, 'e1': -0.5, 'e2': -0.5, 'center_x': -100, 'center_y': -100} upper_limit_default = {'theta_E': 100, 'gamma': 100, 'e1': 0.5, 'e2': 0.5, 'center_x': 100, 'center_y': 100} def __init__(self): self.s2 = 0.00000001 self.spp = SPP() self.spemd_smooth = SPEMD_SMOOTH() def function(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): return self.spemd_smooth.function(x, y, theta_E, gamma, e1, e2, self.s2, center_x, center_y) def derivatives(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): return self.spemd_smooth.derivatives(x, y, theta_E, gamma, e1, e2, self.s2, center_x, center_y) def hessian(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): return self.spemd_smooth.hessian(x, y, theta_E, gamma, e1, e2, self.s2, center_x, center_y) def mass_3d_lens(self, r, theta_E, gamma, e1, e2): """ computes the spherical power-law mass enclosed (with SPP routiune) :param r: :param theta_E: :param gamma: :param q: :param phi_G: :return: """ return self.spp.mass_3d_lens(r, theta_E, gamma)
def test_bounds(self): from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH profile = SPEMD_SMOOTH() compute_bool = profile._parameter_constraints(q_fastell=-1, gam=-1, s2=-1, q=-1) assert compute_bool is False
class SPEMD(object): """ class for smooth power law ellipse mass density profile """ def __init__(self): self.s2 = 0.00000001 self.spp = SPP() self.spemd_smooth = SPEMD_SMOOTH() def function(self, x, y, theta_E, gamma, q, phi_G, center_x=0, center_y=0): return self.spemd_smooth.function(x, y, theta_E, gamma, q, phi_G, self.s2, center_x, center_y) def derivatives(self, x, y, theta_E, gamma, q, phi_G, center_x=0, center_y=0): return self.spemd_smooth.derivatives(x, y, theta_E, gamma, q, phi_G, self.s2, center_x, center_y) def hessian(self, x, y, theta_E, gamma, q, phi_G, center_x=0, center_y=0): return self.spemd_smooth.hessian(x, y, theta_E, gamma, q, phi_G, self.s2, center_x, center_y) def mass_3d_lens(self, r, theta_E, gamma, q, phi_G): """ computes the spherical power-law mass enclosed (with SPP routiune) :param r: :param theta_E: :param gamma: :param q: :param phi_G: :return: """ return self.spp.mass_3d_lens(r, theta_E, gamma) def convert_params(self, theta_E, gamma, q): """ :param theta_E: Einstein radius :param gamma: power law slope :param q: axis ratio :return: prefactor to SPEMP profile for FASTELL """ return self.spemd_smooth.convert_params(theta_E, gamma, q)
def _import_class(self, lens_type, i, custom_class): if lens_type == 'SHIFT': from lenstronomy.LensModel.Profiles.alpha_shift import Shift return Shift() elif lens_type == 'SHEAR': from lenstronomy.LensModel.Profiles.shear import Shear return Shear() elif lens_type == 'CONVERGENCE': from lenstronomy.LensModel.Profiles.convergence import Convergence return Convergence() elif lens_type == 'FLEXION': from lenstronomy.LensModel.Profiles.flexion import Flexion return Flexion() elif lens_type == 'POINT_MASS': from lenstronomy.LensModel.Profiles.point_mass import PointMass return PointMass() elif lens_type == 'SIS': from lenstronomy.LensModel.Profiles.sis import SIS return SIS() elif lens_type == 'SIS_TRUNCATED': from lenstronomy.LensModel.Profiles.sis_truncate import SIS_truncate return SIS_truncate() elif lens_type == 'SIE': from lenstronomy.LensModel.Profiles.sie import SIE return SIE() elif lens_type == 'SPP': from lenstronomy.LensModel.Profiles.spp import SPP return SPP() elif lens_type == 'NIE': from lenstronomy.LensModel.Profiles.nie import NIE return NIE() elif lens_type == 'NIE_SIMPLE': from lenstronomy.LensModel.Profiles.nie import NIE_simple return NIE_simple() elif lens_type == 'CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import Chameleon return Chameleon() elif lens_type == 'DOUBLE_CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import DoubleChameleon return DoubleChameleon() elif lens_type == 'SPEP': from lenstronomy.LensModel.Profiles.spep import SPEP return SPEP() elif lens_type == 'SPEMD': from lenstronomy.LensModel.Profiles.spemd import SPEMD return SPEMD() elif lens_type == 'SPEMD_SMOOTH': from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH return SPEMD_SMOOTH() elif lens_type == 'NFW': from lenstronomy.LensModel.Profiles.nfw import NFW return NFW() elif lens_type == 'NFW_ELLIPSE': from lenstronomy.LensModel.Profiles.nfw_ellipse import NFW_ELLIPSE return NFW_ELLIPSE() elif lens_type == 'TNFW': from lenstronomy.LensModel.Profiles.tnfw import TNFW return TNFW() elif lens_type == 'CNFW': from lenstronomy.LensModel.Profiles.cnfw import CNFW return CNFW() elif lens_type == 'SERSIC': from lenstronomy.LensModel.Profiles.sersic import Sersic return Sersic() elif lens_type == 'SERSIC_ELLIPSE': from lenstronomy.LensModel.Profiles.sersic_ellipse import SersicEllipse return SersicEllipse() elif lens_type == 'PJAFFE': from lenstronomy.LensModel.Profiles.p_jaffe import PJaffe return PJaffe() elif lens_type == 'PJAFFE_ELLIPSE': from lenstronomy.LensModel.Profiles.p_jaffe_ellipse import PJaffe_Ellipse return PJaffe_Ellipse() elif lens_type == 'HERNQUIST': from lenstronomy.LensModel.Profiles.hernquist import Hernquist return Hernquist() elif lens_type == 'HERNQUIST_ELLIPSE': from lenstronomy.LensModel.Profiles.hernquist_ellipse import Hernquist_Ellipse return Hernquist_Ellipse() elif lens_type == 'GAUSSIAN': from lenstronomy.LensModel.Profiles.gaussian_potential import Gaussian return Gaussian() elif lens_type == 'GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.gaussian_kappa import GaussianKappa return GaussianKappa() elif lens_type == 'GAUSSIAN_KAPPA_ELLIPSE': from lenstronomy.LensModel.Profiles.gaussian_kappa_ellipse import GaussianKappaEllipse return GaussianKappaEllipse() elif lens_type == 'MULTI_GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappa return MultiGaussianKappa() elif lens_type == 'MULTI_GAUSSIAN_KAPPA_ELLIPSE': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappaEllipse return MultiGaussianKappaEllipse() elif lens_type == 'INTERPOL': from lenstronomy.LensModel.Profiles.interpol import Interpol return Interpol(grid=False, min_grid_number=100) elif lens_type == 'INTERPOL_SCALED': from lenstronomy.LensModel.Profiles.interpol import InterpolScaled return InterpolScaled() elif lens_type == 'SHAPELETS_POLAR': from lenstronomy.LensModel.Profiles.shapelet_pot_polar import PolarShapelets return PolarShapelets() elif lens_type == 'SHAPELETS_CART': from lenstronomy.LensModel.Profiles.shapelet_pot_cartesian import CartShapelets return CartShapelets() elif lens_type == 'DIPOLE': from lenstronomy.LensModel.Profiles.dipole import Dipole return Dipole() elif lens_type == 'FOREGROUND_SHEAR': from lenstronomy.LensModel.Profiles.shear import Shear self._foreground_shear = True self._foreground_shear_idex = i return Shear() elif lens_type == 'coreBURKERT': from lenstronomy.LensModel.Profiles.coreBurkert import coreBurkert return coreBurkert() elif lens_type == 'NumericalAlpha': from lenstronomy.LensModel.Profiles.numerical_deflections import NumericalAlpha return NumericalAlpha(custom_class[i]) else: raise ValueError('%s is not a valid lens model' % lens_type)
class TestNIE(object): """ tests the Gaussian methods """ def setup(self): from lenstronomy.LensModel.Profiles.nie import NIE from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH from lenstronomy.LensModel.Profiles.sis import SIS self.nie = NIE() self.spemd = SPEMD_SMOOTH() self.sis = SIS() def test_function(self): y = np.array([1., 2]) x = np.array([0., 0.]) theta_E = 1. q = 0.9999 s = 0.00001 phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.nie.function(x, y, theta_E, e1, e2, s_scale=s) delta_pot = values[1] - values[0] values_spemd = self.sis.function(x, y, theta_E) delta_pot_spemd = values_spemd[1] - values_spemd[0] npt.assert_almost_equal(delta_pot, delta_pot_spemd, decimal=4) if bool_test is True: q = 0.99 s = 0.000001 phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.nie.function(x, y, theta_E, e1, e2, s_scale=s) delta_pot = values[1] - values[0] gamma = 2. values_spemd = self.spemd.function(x, y, theta_E, gamma, e1, e2, s_scale=s) delta_pot_spemd = values_spemd[1] - values_spemd[0] npt.assert_almost_equal(delta_pot, delta_pot_spemd, decimal=2) def test_derivatives(self): x = np.array([1]) y = np.array([2]) theta_E = 1. q = 0.99999 phi_G = 0 s = 0.0000001 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.nie.derivatives(x, y, theta_E, e1, e2, s_scale=s) f_x_spemd, f_y_spemd = self.sis.derivatives(x, y, theta_E) npt.assert_almost_equal(f_x, f_x_spemd, decimal=4) npt.assert_almost_equal(f_y, f_y_spemd, decimal=4) if bool_test is True: q = 0.99 s = 0.000001 phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.nie.derivatives(x, y, theta_E, e1, e2, s_scale=s) gamma = 2. f_x_spemd, f_y_spemd = self.spemd.derivatives(x, y, theta_E, gamma, e1, e2, s_scale=s) print(f_x / f_x_spemd, 'ratio deflections') print(1 + (1 - q) / 2) npt.assert_almost_equal(f_x, f_x_spemd, decimal=2) npt.assert_almost_equal(f_y, f_y_spemd, decimal=2) def test_hessian(self): x = np.array([1]) y = np.array([2]) theta_E = 1. q = 0.999999 phi_G = 0 s = 0.0000001 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_yy, f_xy = self.nie.hessian(x, y, theta_E, e1, e2, s_scale=s) f_xx_spemd, f_yy_spemd, f_xy_spemd = self.sis.hessian(x, y, theta_E) npt.assert_almost_equal(f_xx, f_xx_spemd, decimal=4) npt.assert_almost_equal(f_yy, f_yy_spemd, decimal=4) npt.assert_almost_equal(f_xy, f_xy_spemd, decimal=4)
def __init__(self): self.s2 = 0.00000001 self.spp = SPP() self.spemd_smooth = SPEMD_SMOOTH()
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
class TestSPEMD(object): """ tests the Gaussian methods """ def setup(self): from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH self.SPEMD_SMOOT = SPEMD_SMOOTH() from lenstronomy.LensModel.Profiles.nie import NIE self.NIE = NIE() def test_function(self): phi_E = 1. gamma = 2. q = 0.999 phi_G = 1. s_scale = 0.1 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) x = np.array([1., 2]) y = np.array([2, 0]) values = self.SPEMD_SMOOT.function(x, y, phi_E, gamma, e1, e2, s_scale) if fastell4py_bool: values_nie = self.NIE.function(x, y, phi_E, e1, e2, s_scale) delta_f = values[0] - values[1] delta_f_nie = values_nie[0] - values_nie[1] npt.assert_almost_equal(delta_f, delta_f_nie, decimal=5) else: npt.assert_almost_equal(values, 0, decimal=5) def test_derivatives(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 2. q = 0.7 phi_G = 1. s_scale = 0.1 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.SPEMD_SMOOT.derivatives(x, y, phi_E, gamma, e1, e2, s_scale) if fastell4py_bool: f_x_nie, f_y_nie = self.NIE.derivatives(x, y, phi_E, e1, e2, s_scale) #TODO test with higher precision, convention of theta_E might be slightly different from NIE with SPEMD npt.assert_almost_equal(f_x, f_x_nie, decimal=2) npt.assert_almost_equal(f_y, f_y_nie, decimal=2) else: npt.assert_almost_equal(f_x, 0, decimal=7) npt.assert_almost_equal(f_y, 0, decimal=7) def test_hessian(self): x = np.array([1.]) y = np.array([2.]) phi_E = 1. gamma = 2. q = 0.9 phi_G = 1. s_scale = 0.1 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_yy, f_xy = self.SPEMD_SMOOT.hessian(x, y, phi_E, gamma, e1, e2, s_scale) if fastell4py_bool: f_xx_nie, f_yy_nie, f_xy_nie = self.NIE.hessian( x, y, phi_E, e1, e2, s_scale) npt.assert_almost_equal(f_xx, f_xx_nie, decimal=3) npt.assert_almost_equal(f_yy, f_yy_nie, decimal=3) npt.assert_almost_equal(f_xy, f_xy_nie, decimal=3) 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) def test_bounds(self): theta_E, gamma, q, phi_G, s_scale = self.SPEMD_SMOOT._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_SMOOT.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([]))
class TestNIE(object): """ tests the Gaussian methods """ def setup(self): self.nie = NIE() self.spemd = SPEMD_SMOOTH() self.sis = SIS() def test_function(self): y = np.array([1., 2]) x = np.array([0., 0.]) theta_E = 1. q = 0.9999 s = 0.00001 phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.nie.function(x, y, theta_E, e1, e2, s_scale=s) delta_pot = values[1] - values[0] values_spemd = self.sis.function(x, y, theta_E) delta_pot_spemd = values_spemd[1] - values_spemd[0] npt.assert_almost_equal(delta_pot, delta_pot_spemd, decimal=4) if bool_test is True: q = 0.99 s = 0.000001 phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.nie.function(x, y, theta_E, e1, e2, s_scale=s) delta_pot = values[1] - values[0] gamma = 2. values_spemd = self.spemd.function(x, y, theta_E, gamma, e1, e2, s_scale=s) delta_pot_spemd = values_spemd[1] - values_spemd[0] npt.assert_almost_equal(delta_pot, delta_pot_spemd, decimal=2) def test_derivatives(self): x = np.array([1]) y = np.array([2]) theta_E = 1. q = 0.99999 phi_G = 0 s = 0.0000001 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.nie.derivatives(x, y, theta_E, e1, e2, s_scale=s) f_x_spemd, f_y_spemd = self.sis.derivatives(x, y, theta_E) npt.assert_almost_equal(f_x, f_x_spemd, decimal=4) npt.assert_almost_equal(f_y, f_y_spemd, decimal=4) if bool_test is True: q = 0.99 s = 0.000001 phi_G = 0 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.nie.derivatives(x, y, theta_E, e1, e2, s_scale=s) gamma = 2. f_x_spemd, f_y_spemd = self.spemd.derivatives(x, y, theta_E, gamma, e1, e2, s_scale=s) print(f_x / f_x_spemd, 'ratio deflections') print(1 + (1 - q) / 2) npt.assert_almost_equal(f_x, f_x_spemd, decimal=2) npt.assert_almost_equal(f_y, f_y_spemd, decimal=2) def test_hessian(self): x = np.array([1]) y = np.array([2]) theta_E = 1. q = 0.999999 phi_G = 0 s = 0.0000001 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_yy, f_xy = self.nie.hessian(x, y, theta_E, e1, e2, s_scale=s) f_xx_spemd, f_yy_spemd, f_xy_spemd = self.sis.hessian(x, y, theta_E) npt.assert_almost_equal(f_xx, f_xx_spemd, decimal=4) npt.assert_almost_equal(f_yy, f_yy_spemd, decimal=4) npt.assert_almost_equal(f_xy, f_xy_spemd, decimal=4) def test_convergence2surface_brightness(self): from lenstronomy.LightModel.Profiles.nie import NIE as NIE_Light nie_light = NIE_Light() kwargs = {'e1': 0.3, 'e2': -0.05, 's_scale': 0.5} x, y = util.make_grid(numPix=10, deltapix=0.1) f_xx, f_yy, f_xy = self.nie.hessian(x, y, theta_E=1, **kwargs) kappa = 1 / 2. * (f_xx + f_yy) flux = nie_light.function(x, y, amp=1, **kwargs) npt.assert_almost_equal(kappa / np.sum(kappa), flux / np.sum(flux), decimal=5) 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., 's_scale': .1, 'e1': e1, 'e2': e2} f_ = self.nie.function(x, y, **kwargs_lens) self.nie.set_static(**kwargs_lens) f_static = self.nie.function(x, y, **kwargs_lens) npt.assert_almost_equal(f_, f_static, decimal=8) self.nie.set_dynamic() kwargs_lens = {'theta_E': 2., 's_scale': .1, 'e1': e1, 'e2': e2} f_dyn = self.nie.function(x, y, **kwargs_lens) assert f_dyn != f_static
class SPEMD(LensProfileBase): """ class for power law ellipse mass density profile. This class effectively calls the class SPEMD_SMOOTH with a fixed and very small central smoothing scale to perform the numerical integral using the FASTELL code by Renan Barkana. The Einstein ring parameter converts to the definition used by GRAVLENS as follow: (theta_E / theta_E_gravlens) = sqrt[ (1+q^2) / (2 q) ] """ param_names = ['theta_E', 'gamma', 'e1', 'e2', 'center_x', 'center_y'] lower_limit_default = {'theta_E': 0, 'gamma': 1.5, 'e1': -0.5, 'e2': -0.5, 'center_x': -100, 'center_y': -100} upper_limit_default = {'theta_E': 100, 'gamma': 2.5, 'e1': 0.5, 'e2': 0.5, 'center_x': 100, 'center_y': 100} def __init__(self): self.s2 = 0.00000001 # smoothing scale as used to numerically compute a power-law profile self.spp = SPP() self.spemd_smooth = SPEMD_SMOOTH() super(SPEMD, self).__init__() def function(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: x-coordinate (angle) :param y: y-coordinate (angle) :param theta_E: Einstein radius (angle), pay attention to specific definition! :param gamma: logarithmic slope of the power-law profile. gamma=2 corresponds to isothermal :param e1: eccentricity component :param e2: eccentricity component :param center_x: x-position of lens center :param center_y: y-position of lens center :return: lensing potential """ return self.spemd_smooth.function(x, y, theta_E, gamma, e1, e2, self.s2, center_x, center_y) def derivatives(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: x-coordinate (angle) :param y: y-coordinate (angle) :param theta_E: Einstein radius (angle), pay attention to specific definition! :param gamma: logarithmic slope of the power-law profile. gamma=2 corresponds to isothermal :param e1: eccentricity component :param e2: eccentricity component :param center_x: x-position of lens center :param center_y: y-position of lens center :return: deflection angles alpha_x, alpha_y """ return self.spemd_smooth.derivatives(x, y, theta_E, gamma, e1, e2, self.s2, center_x, center_y) def hessian(self, x, y, theta_E, gamma, e1, e2, center_x=0, center_y=0): """ :param x: x-coordinate (angle) :param y: y-coordinate (angle) :param theta_E: Einstein radius (angle), pay attention to specific definition! :param gamma: logarithmic slope of the power-law profile. gamma=2 corresponds to isothermal :param e1: eccentricity component :param e2: eccentricity component :param center_x: x-position of lens center :param center_y: y-position of lens center :return: Hessian components f_xx, f_yy, f_xy """ return self.spemd_smooth.hessian(x, y, theta_E, gamma, e1, e2, self.s2, center_x, center_y) def mass_3d_lens(self, r, theta_E, gamma, e1=None, e2=None): """ computes the spherical power-law mass enclosed (with SPP routine) :param r: radius within the mass is computed :param theta_E: Einstein radius :param gamma: power-law slope :param e1: eccentricity component (not used) :param e2: eccentricity component (not used) :return: mass enclosed a 3D radius r """ return self.spp.mass_3d_lens(r, theta_E, gamma)
def __init__(self): self.s2 = 0.00000001 # smoothing scale as used to numerically compute a power-law profile self.spp = SPP() self.spemd_smooth = SPEMD_SMOOTH() super(SPEMD, self).__init__()
def setup(self): from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH self.SPEMD_SMOOT = SPEMD_SMOOTH() from lenstronomy.LensModel.Profiles.nie import NIE self.NIE = NIE()
def __init__(self, lens_model_list, **kwargs): """ :param lens_model_list: list of strings with lens model names :param foreground_shear: bool, when True, models a foreground non-linear shear distortion """ self.func_list = [] self._foreground_shear = False for i, lens_type in enumerate(lens_model_list): if lens_type == 'SHEAR': from lenstronomy.LensModel.Profiles.external_shear import ExternalShear self.func_list.append(ExternalShear()) elif lens_type == 'CONVERGENCE': from lenstronomy.LensModel.Profiles.mass_sheet import MassSheet self.func_list.append(MassSheet()) elif lens_type == 'FLEXION': from lenstronomy.LensModel.Profiles.flexion import Flexion self.func_list.append(Flexion()) elif lens_type == 'POINT_MASS': from lenstronomy.LensModel.Profiles.point_mass import PointMass self.func_list.append(PointMass()) elif lens_type == 'SIS': from lenstronomy.LensModel.Profiles.sis import SIS self.func_list.append(SIS()) elif lens_type == 'SIS_TRUNCATED': from lenstronomy.LensModel.Profiles.sis_truncate import SIS_truncate self.func_list.append(SIS_truncate()) elif lens_type == 'SIE': from lenstronomy.LensModel.Profiles.sie import SIE self.func_list.append(SIE()) elif lens_type == 'SPP': from lenstronomy.LensModel.Profiles.spp import SPP self.func_list.append(SPP()) elif lens_type == 'NIE': from lenstronomy.LensModel.Profiles.nie import NIE self.func_list.append(NIE()) elif lens_type == 'NIE_SIMPLE': from lenstronomy.LensModel.Profiles.nie import NIE_simple self.func_list.append(NIE_simple()) elif lens_type == 'CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import Chameleon self.func_list.append(Chameleon()) elif lens_type == 'DOUBLE_CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import DoubleChameleon self.func_list.append(DoubleChameleon()) elif lens_type == 'SPEP': from lenstronomy.LensModel.Profiles.spep import SPEP self.func_list.append(SPEP()) elif lens_type == 'SPEMD': from lenstronomy.LensModel.Profiles.spemd import SPEMD self.func_list.append(SPEMD()) elif lens_type == 'SPEMD_SMOOTH': from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH self.func_list.append(SPEMD_SMOOTH()) elif lens_type == 'NFW': from lenstronomy.LensModel.Profiles.nfw import NFW self.func_list.append(NFW(**kwargs)) elif lens_type == 'NFW_ELLIPSE': from lenstronomy.LensModel.Profiles.nfw_ellipse import NFW_ELLIPSE self.func_list.append( NFW_ELLIPSE(interpol=False, num_interp_X=1000, max_interp_X=100)) elif lens_type == 'TNFW': from lenstronomy.LensModel.Profiles.tnfw import TNFW self.func_list.append(TNFW()) elif lens_type == 'SERSIC': from lenstronomy.LensModel.Profiles.sersic import Sersic self.func_list.append(Sersic()) elif lens_type == 'SERSIC_ELLIPSE': from lenstronomy.LensModel.Profiles.sersic_ellipse import SersicEllipse self.func_list.append(SersicEllipse()) elif lens_type == 'PJAFFE': from lenstronomy.LensModel.Profiles.p_jaffe import PJaffe self.func_list.append(PJaffe()) elif lens_type == 'PJAFFE_ELLIPSE': from lenstronomy.LensModel.Profiles.p_jaffe_ellipse import PJaffe_Ellipse self.func_list.append(PJaffe_Ellipse()) elif lens_type == 'HERNQUIST': from lenstronomy.LensModel.Profiles.hernquist import Hernquist self.func_list.append(Hernquist()) elif lens_type == 'HERNQUIST_ELLIPSE': from lenstronomy.LensModel.Profiles.hernquist_ellipse import Hernquist_Ellipse self.func_list.append(Hernquist_Ellipse()) elif lens_type == 'GAUSSIAN': from lenstronomy.LensModel.Profiles.gaussian_potential import Gaussian self.func_list.append(Gaussian()) elif lens_type == 'GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.gaussian_kappa import GaussianKappa self.func_list.append(GaussianKappa()) elif lens_type == 'GAUSSIAN_KAPPA_ELLIPSE': from lenstronomy.LensModel.Profiles.gaussian_kappa_ellipse import GaussianKappaEllipse self.func_list.append(GaussianKappaEllipse()) elif lens_type == 'MULTI_GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappa self.func_list.append(MultiGaussianKappa()) elif lens_type == 'MULTI_GAUSSIAN_KAPPA_ELLIPSE': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappaEllipse self.func_list.append(MultiGaussianKappaEllipse()) elif lens_type == 'INTERPOL': from lenstronomy.LensModel.Profiles.interpol import Interpol_func self.func_list.append( Interpol_func(grid=False, min_grid_number=100)) elif lens_type == 'INTERPOL_SCALED': from lenstronomy.LensModel.Profiles.interpol import Interpol_func_scaled self.func_list.append( Interpol_func_scaled(grid=False, min_grid_number=100)) elif lens_type == 'SHAPELETS_POLAR': from lenstronomy.LensModel.Profiles.shapelet_pot_polar import PolarShapelets self.func_list.append(PolarShapelets()) elif lens_type == 'SHAPELETS_CART': from lenstronomy.LensModel.Profiles.shapelet_pot_cartesian import CartShapelets self.func_list.append(CartShapelets()) elif lens_type == 'DIPOLE': from lenstronomy.LensModel.Profiles.dipole import Dipole self.func_list.append(Dipole()) elif lens_type == 'FOREGROUND_SHEAR': from lenstronomy.LensModel.Profiles.external_shear import ExternalShear self.func_list.append(ExternalShear()) self._foreground_shear = True self._foreground_shear_idex = i else: raise ValueError('%s is not a valid lens model' % lens_type) self._model_list = lens_model_list
def setup(self): self.nie = NIE() self.spemd = SPEMD_SMOOTH() self.sis = SIS()
def _import_class(lens_type, custom_class, z_lens=None, z_source=None): """ :param lens_type: string, lens model type :param custom_class: custom class :param z_lens: lens redshift # currently only used in NFW_MC model as this is redshift dependent :param z_source: source redshift # currently only used in NFW_MC model as this is redshift dependent :return: class instance of the lens model type """ if lens_type == 'SHIFT': from lenstronomy.LensModel.Profiles.alpha_shift import Shift return Shift() elif lens_type == 'SHEAR': from lenstronomy.LensModel.Profiles.shear import Shear return Shear() elif lens_type == 'SHEAR_GAMMA_PSI': from lenstronomy.LensModel.Profiles.shear import ShearGammaPsi return ShearGammaPsi() elif lens_type == 'CONVERGENCE': from lenstronomy.LensModel.Profiles.convergence import Convergence return Convergence() elif lens_type == 'FLEXION': from lenstronomy.LensModel.Profiles.flexion import Flexion return Flexion() elif lens_type == 'FLEXIONFG': from lenstronomy.LensModel.Profiles.flexionfg import Flexionfg return Flexionfg() elif lens_type == 'POINT_MASS': from lenstronomy.LensModel.Profiles.point_mass import PointMass return PointMass() elif lens_type == 'SIS': from lenstronomy.LensModel.Profiles.sis import SIS return SIS() elif lens_type == 'SIS_TRUNCATED': from lenstronomy.LensModel.Profiles.sis_truncate import SIS_truncate return SIS_truncate() elif lens_type == 'SIE': from lenstronomy.LensModel.Profiles.sie import SIE return SIE() elif lens_type == 'SPP': from lenstronomy.LensModel.Profiles.spp import SPP return SPP() elif lens_type == 'NIE': from lenstronomy.LensModel.Profiles.nie import NIE return NIE() elif lens_type == 'NIE_SIMPLE': from lenstronomy.LensModel.Profiles.nie import NIEMajorAxis return NIEMajorAxis() elif lens_type == 'CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import Chameleon return Chameleon() elif lens_type == 'DOUBLE_CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import DoubleChameleon return DoubleChameleon() elif lens_type == 'TRIPLE_CHAMELEON': from lenstronomy.LensModel.Profiles.chameleon import TripleChameleon return TripleChameleon() elif lens_type == 'SPEP': from lenstronomy.LensModel.Profiles.spep import SPEP return SPEP() elif lens_type == 'SPEMD': from lenstronomy.LensModel.Profiles.spemd import SPEMD return SPEMD() elif lens_type == 'SPEMD_SMOOTH': from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH return SPEMD_SMOOTH() elif lens_type == 'NFW': from lenstronomy.LensModel.Profiles.nfw import NFW return NFW() elif lens_type == 'NFW_ELLIPSE': from lenstronomy.LensModel.Profiles.nfw_ellipse import NFW_ELLIPSE return NFW_ELLIPSE() elif lens_type == 'NFW_ELLIPSE_GAUSS_DEC': from lenstronomy.LensModel.Profiles.gauss_decomposition import NFWEllipseGaussDec return NFWEllipseGaussDec() elif lens_type == 'TNFW': from lenstronomy.LensModel.Profiles.tnfw import TNFW return TNFW() elif lens_type == 'CNFW': from lenstronomy.LensModel.Profiles.cnfw import CNFW return CNFW() elif lens_type == 'CNFW_ELLIPSE': from lenstronomy.LensModel.Profiles.cnfw_ellipse import CNFW_ELLIPSE return CNFW_ELLIPSE() elif lens_type == 'CTNFW_GAUSS_DEC': from lenstronomy.LensModel.Profiles.gauss_decomposition import CTNFWGaussDec return CTNFWGaussDec() elif lens_type == 'NFW_MC': from lenstronomy.LensModel.Profiles.nfw_mass_concentration import NFWMC return NFWMC(z_lens=z_lens, z_source=z_source) elif lens_type == 'SERSIC': from lenstronomy.LensModel.Profiles.sersic import Sersic return Sersic() elif lens_type == 'SERSIC_ELLIPSE_POTENTIAL': from lenstronomy.LensModel.Profiles.sersic_ellipse_potential import SersicEllipse return SersicEllipse() elif lens_type == 'SERSIC_ELLIPSE_KAPPA': from lenstronomy.LensModel.Profiles.sersic_ellipse_kappa import SersicEllipseKappa return SersicEllipseKappa() elif lens_type == 'SERSIC_ELLIPSE_GAUSS_DEC': from lenstronomy.LensModel.Profiles.gauss_decomposition \ import SersicEllipseGaussDec return SersicEllipseGaussDec() elif lens_type == 'PJAFFE': from lenstronomy.LensModel.Profiles.p_jaffe import PJaffe return PJaffe() elif lens_type == 'PJAFFE_ELLIPSE': from lenstronomy.LensModel.Profiles.p_jaffe_ellipse import PJaffe_Ellipse return PJaffe_Ellipse() elif lens_type == 'HERNQUIST': from lenstronomy.LensModel.Profiles.hernquist import Hernquist return Hernquist() elif lens_type == 'HERNQUIST_ELLIPSE': from lenstronomy.LensModel.Profiles.hernquist_ellipse import Hernquist_Ellipse return Hernquist_Ellipse() elif lens_type == 'GAUSSIAN': from lenstronomy.LensModel.Profiles.gaussian_potential import Gaussian return Gaussian() elif lens_type == 'GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.gaussian_kappa import GaussianKappa return GaussianKappa() elif lens_type == 'GAUSSIAN_ELLIPSE_KAPPA': from lenstronomy.LensModel.Profiles.gaussian_ellipse_kappa import GaussianEllipseKappa return GaussianEllipseKappa() elif lens_type == 'GAUSSIAN_ELLIPSE_POTENTIAL': from lenstronomy.LensModel.Profiles.gaussian_ellipse_potential import GaussianEllipsePotential return GaussianEllipsePotential() elif lens_type == 'MULTI_GAUSSIAN_KAPPA': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappa return MultiGaussianKappa() elif lens_type == 'MULTI_GAUSSIAN_KAPPA_ELLIPSE': from lenstronomy.LensModel.Profiles.multi_gaussian_kappa import MultiGaussianKappaEllipse return MultiGaussianKappaEllipse() elif lens_type == 'INTERPOL': from lenstronomy.LensModel.Profiles.interpol import Interpol return Interpol() elif lens_type == 'INTERPOL_SCALED': from lenstronomy.LensModel.Profiles.interpol import InterpolScaled return InterpolScaled() elif lens_type == 'SHAPELETS_POLAR': from lenstronomy.LensModel.Profiles.shapelet_pot_polar import PolarShapelets return PolarShapelets() elif lens_type == 'SHAPELETS_CART': from lenstronomy.LensModel.Profiles.shapelet_pot_cartesian import CartShapelets return CartShapelets() elif lens_type == 'DIPOLE': from lenstronomy.LensModel.Profiles.dipole import Dipole return Dipole() elif lens_type == 'CURVED_ARC': from lenstronomy.LensModel.Profiles.curved_arc import CurvedArc return CurvedArc() elif lens_type == 'ARC_PERT': from lenstronomy.LensModel.Profiles.arc_perturbations import ArcPerturbations return ArcPerturbations() elif lens_type == 'coreBURKERT': from lenstronomy.LensModel.Profiles.coreBurkert import CoreBurkert return CoreBurkert() elif lens_type == 'CORED_DENSITY': from lenstronomy.LensModel.Profiles.cored_density import CoredDensity return CoredDensity() elif lens_type == 'CORED_DENSITY_2': from lenstronomy.LensModel.Profiles.cored_density_2 import CoredDensity2 return CoredDensity2() elif lens_type == 'CORED_DENSITY_MST': from lenstronomy.LensModel.Profiles.cored_density_mst import CoredDensityMST return CoredDensityMST(profile_type='CORED_DENSITY') elif lens_type == 'CORED_DENSITY_2_MST': from lenstronomy.LensModel.Profiles.cored_density_mst import CoredDensityMST return CoredDensityMST(profile_type='CORED_DENSITY_2') elif lens_type == 'NumericalAlpha': from lenstronomy.LensModel.Profiles.numerical_deflections import NumericalAlpha return NumericalAlpha(custom_class) else: raise ValueError('%s is not a valid lens model' % lens_type)
class TestSPEMD(object): """ tests the Gaussian methods """ def setup(self): from lenstronomy.LensModel.Profiles.spemd_smooth import SPEMD_SMOOTH self.SPEMD_SMOOT = SPEMD_SMOOTH() from lenstronomy.LensModel.Profiles.nie import NIE self.NIE = NIE() def test_function(self): phi_E = 1. gamma = 2. q = 0.999 phi_G = 1. s_scale = 0.1 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) x = np.array([1., 2]) y = np.array([2, 0]) values = self.SPEMD_SMOOT.function(x, y, phi_E, gamma, e1, e2, s_scale) if fastell4py_bool: values_nie = self.NIE.function(x, y, phi_E, e1, e2, s_scale) delta_f = values[0] - values[1] delta_f_nie = values_nie[0] - values_nie[1] npt.assert_almost_equal(delta_f, delta_f_nie, decimal=5) else: npt.assert_almost_equal(values, 0, decimal=5) def test_derivatives(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 2. q = 1. phi_G = 1. s_scale = 0.1 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.SPEMD_SMOOT.derivatives(x, y, phi_E, gamma, e1, e2, s_scale) if fastell4py_bool: f_x_nie, f_y_nie = self.NIE.derivatives(x, y, phi_E, e1, e2, s_scale) npt.assert_almost_equal(f_x, f_x_nie, decimal=4) npt.assert_almost_equal(f_y, f_y_nie, decimal=4) else: npt.assert_almost_equal(f_x, 0, decimal=7) npt.assert_almost_equal(f_y, 0, decimal=7) q = 0.7 phi_G = 1. s_scale = 0.001 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.SPEMD_SMOOT.derivatives(x, y, phi_E, gamma, e1, e2, s_scale) if fastell4py_bool: f_x_nie, f_y_nie = self.NIE.derivatives(x, y, phi_E, e1, e2, s_scale) npt.assert_almost_equal(f_x, f_x_nie, decimal=4) npt.assert_almost_equal(f_y, f_y_nie, decimal=4) else: npt.assert_almost_equal(f_x, 0, decimal=7) npt.assert_almost_equal(f_y, 0, decimal=7) def test_hessian(self): x = np.array([1.]) y = np.array([2.]) phi_E = 1. gamma = 2. q = 0.9 phi_G = 1. s_scale = 0.001 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_yy, f_xy = self.SPEMD_SMOOT.hessian(x, y, phi_E, gamma, e1, e2, s_scale) if fastell4py_bool: f_xx_nie, f_yy_nie, f_xy_nie = self.NIE.hessian( x, y, phi_E, e1, e2, s_scale) 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) 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) def test_bounds(self): compute_bool = self.SPEMD_SMOOT._parameter_constraints(q_fastell=-1, gam=-1, s2=-1, q=-1) assert compute_bool is False def test_is_not_empty(self): func = self.SPEMD_SMOOT.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([]))