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 setup(self): from lenstronomy.LensModel.Profiles.sie import SIE from lenstronomy.LensModel.Profiles.spemd import SPEMD from lenstronomy.LensModel.Profiles.nie import NIE self.sie = SIE(NIE=False) self.sie_nie = SIE(NIE=True) self.spemd = SPEMD() self.nie = NIE()
def setup(self): from lenstronomy.LensModel.Profiles.sie import SIE from lenstronomy.LensModel.Profiles.pemd import PEMD from lenstronomy.LensModel.Profiles.nie import NIE self.sie = SIE(NIE=False, suppress_fastell=True) self.sie_nie = SIE(NIE=True) self.spemd = PEMD(suppress_fastell=True) self.nie = NIE()
def setup(self): from lenstronomy.LensModel.Profiles.sie import SIE from lenstronomy.LensModel.Profiles.epl import EPL from lenstronomy.LensModel.Profiles.nie import NIE self.sie = SIE(NIE=False) self.sie_nie = SIE(NIE=True) self.epl = EPL() self.nie = NIE()
def test_function(self): """ :return: """ lens = NIE_lens() light = NIE_light() x = np.linspace(0.1, 10, 10) e1, e2 = 0.1, 0 s = 0.2 kwargs_light = {'amp': 1., 'e1': e1, 'e2': e2, 's_scale': s} kwargs_lens = {'theta_E': 1., 'e1': e1, 'e2': e2, 's_scale': s} flux = light.function(x=x, y=1., **kwargs_light) f_xx, f_yy, f_xy = lens.hessian(x=x, y=1., **kwargs_lens) kappa = 1/2. * (f_xx + f_yy) npt.assert_almost_equal(flux/flux[-1], kappa/kappa[-1], decimal=4)
def __init__(self, NIE=False): self._nie = NIE if NIE: from lenstronomy.LensModel.Profiles.nie import NIE self.profile = NIE() else: from lenstronomy.LensModel.Profiles.spemd import SPEMD self.profile = SPEMD() self._s_scale = 0.0000000001 self._gamma = 2
def __init__(self, NIE=True): """ :param NIE: bool, if True, is using the NIE analytic model. Otherwise it uses PEMD with gamma=2 from fastell4py """ self._nie = NIE if NIE: from lenstronomy.LensModel.Profiles.nie import NIE self.profile = NIE() else: from lenstronomy.LensModel.Profiles.epl import EPL self.profile = EPL() self._s_scale = 0.0000000001 self._gamma = 2 super(SIE, self).__init__()
def __init__(self, NIE=True, suppress_fastell=False): """ :param NIE: bool, if True, is using the NIE analytic model. Otherwise it uses PEMD with gamma=2 from fastell4py :param suppress_fastell: bool, if True, does not raise if fastell4py is not installed """ self._nie = NIE if NIE: from lenstronomy.LensModel.Profiles.nie import NIE self.profile = NIE() else: from lenstronomy.LensModel.Profiles.pemd import PEMD self.profile = PEMD(suppress_fastell=suppress_fastell) self._s_scale = 0.0000000001 self._gamma = 2 super(SIE, self).__init__()
class TestChameleon(object): """ class to test the Moffat profile """ def setup(self): self.chameleon = Chameleon() self.nie = NIE() def test_theta_E_convert(self): w_c, w_t = 2, 1 theta_E_convert, w_c, w_t, s_scale_1, s_scale_2 = self.chameleon.param_convert( alpha_1=1, w_c=w_c, w_t=w_t, e1=0, e2=0) assert w_c == 1 assert w_t == 2 assert theta_E_convert == 0 def test_function(self): """ :return: """ x = np.linspace(0.1, 10, 10) w_c, w_t = 0.5, 1. phi_G, q = 0.3, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_light = { 'alpha_1': 1., 'w_c': .5, 'w_t': 1., 'e1': e1, 'e2': e2 } theta_E_convert, w_c, w_t, s_scale_1, s_scale_2 = self.chameleon.param_convert( alpha_1=1, w_c=0.5, w_t=1., e1=e1, e2=e2) kwargs_1 = { 'theta_E': theta_E_convert, 's_scale': s_scale_1, 'e1': e1, 'e2': e2 } kwargs_2 = { 'theta_E': theta_E_convert, 's_scale': s_scale_2, 'e1': e1, 'e2': e2 } f_ = self.chameleon.function(x=x, y=1., **kwargs_light) f_1 = self.nie.function(x=x, y=1., **kwargs_1) f_2 = self.nie.function(x=x, y=1., **kwargs_2) npt.assert_almost_equal(f_, (f_1 - f_2), decimal=5) def test_derivatives(self): """ :return: """ x = np.linspace(0.1, 10, 10) w_c, w_t = 0.5, 1. phi_G, q = 0.3, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_light = { 'alpha_1': 1., 'w_c': .5, 'w_t': 1., 'e1': e1, 'e2': e2 } theta_E_convert, w_c, w_t, s_scale_1, s_scale_2 = self.chameleon.param_convert( alpha_1=1, w_c=0.5, w_t=1., e1=e1, e2=e2) kwargs_1 = { 'theta_E': theta_E_convert, 's_scale': s_scale_1, 'e1': e1, 'e2': e2 } kwargs_2 = { 'theta_E': theta_E_convert, 's_scale': s_scale_2, 'e1': e1, 'e2': e2 } f_x, f_y = self.chameleon.derivatives(x=x, y=1., **kwargs_light) f_x_1, f_y_1 = self.nie.derivatives(x=x, y=1., **kwargs_1) f_x_2, f_y_2 = self.nie.derivatives(x=x, y=1., **kwargs_2) npt.assert_almost_equal(f_x, (f_x_1 - f_x_2), decimal=5) npt.assert_almost_equal(f_y, (f_y_1 - f_y_2), decimal=5) f_x, f_y = self.chameleon.derivatives(x=1, y=0., **kwargs_light) npt.assert_almost_equal(f_x, 1, decimal=1) def test_hessian(self): """ :return: """ x = np.linspace(0.1, 10, 10) w_c, w_t = 0.5, 1. phi_G, q = 0.3, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_light = { 'alpha_1': 1., 'w_c': .5, 'w_t': 1., 'e1': e1, 'e2': e2 } theta_E_convert, w_c, w_t, s_scale_1, s_scale_2 = self.chameleon.param_convert( alpha_1=1, w_c=0.5, w_t=1., e1=e1, e2=e2) kwargs_1 = { 'theta_E': theta_E_convert, 's_scale': s_scale_1, 'e1': e1, 'e2': e2 } kwargs_2 = { 'theta_E': theta_E_convert, 's_scale': s_scale_2, 'e1': e1, 'e2': e2 } f_xx, f_xy, f_yx, f_yy = self.chameleon.hessian(x=x, y=1., **kwargs_light) f_xx_1, f_xy_1, f_yx_1, f_yy_1 = self.nie.hessian(x=x, y=1., **kwargs_1) f_xx_2, f_xy_2, f_yx_2, f_yy_2 = self.nie.hessian(x=x, y=1., **kwargs_2) npt.assert_almost_equal(f_xx, (f_xx_1 - f_xx_2), decimal=5) npt.assert_almost_equal(f_yy, (f_yy_1 - f_yy_2), decimal=5) npt.assert_almost_equal(f_xy, (f_xy_1 - f_xy_2), decimal=5) npt.assert_almost_equal(f_yx, (f_yx_1 - f_yx_2), 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_light = { 'alpha_1': 1., 'w_c': .5, 'w_t': 1., 'e1': e1, 'e2': e2 } f_ = self.chameleon.function(x, y, **kwargs_light) self.chameleon.set_static(**kwargs_light) f_static = self.chameleon.function(x, y, **kwargs_light) npt.assert_almost_equal(f_, f_static, decimal=8) self.chameleon.set_dynamic() kwargs_light = { 'alpha_1': 2., 'w_c': .5, 'w_t': 1., 'e1': e1, 'e2': e2 } f_dyn = self.chameleon.function(x, y, **kwargs_light) assert f_dyn != f_static
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)
def __init__(self): self.nie = NIE()
class TestEPL(object): """ tests the Gaussian methods """ def setup(self): from lenstronomy.LensModel.Profiles.epl import EPL self.EPL = EPL() from lenstronomy.LensModel.Profiles.nie import NIE self.NIE = NIE() def test_function(self): phi_E = 1. t = 1. q = 0.999 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) x = np.array([1., 2]) y = np.array([2, 0]) values = self.EPL.function(x, y, phi_E, e1, e2, t) values_nie = self.NIE.function(x, y, phi_E, e1, e2, 0.) 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) q = 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) x = np.array([1., 2]) y = np.array([2, 0]) values = self.EPL.function(x, y, phi_E, e1, e2, t) values_nie = self.NIE.function(x, y, phi_E, e1, e2, 0.) 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) q = 0.4 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) x = np.array([1., 2]) y = np.array([2, 0]) values = self.EPL.function(x, y, phi_E, e1, e2, t) values_nie = self.NIE.function(x, y, phi_E, e1, e2, 0.) 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) def test_derivatives(self): x = np.array([1]) y = np.array([2]) phi_E = 1. t = 1. q = 1. phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.EPL.derivatives(x, y, phi_E, e1, e2, t) f_x_nie, f_y_nie = self.NIE.derivatives(x, y, phi_E, e1, e2, 0.) npt.assert_almost_equal(f_x, f_x_nie, decimal=4) npt.assert_almost_equal(f_y, f_y_nie, decimal=4) q = 0.7 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.EPL.derivatives(x, y, phi_E, e1, e2, t) f_x_nie, f_y_nie = self.NIE.derivatives(x, y, phi_E, e1, e2, 0.) 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.]) phi_E = 1. t = 1. q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_yy, f_xy = self.EPL.hessian(x, y, phi_E, e1, e2, t) f_xx_nie, f_yy_nie, f_xy_nie = self.NIE.hessian( x, y, phi_E, e1, e2, 0.) 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) 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., 't': 1.5, 'e1': e1, 'e2': e2} f_ = self.EPL.function(x, y, **kwargs_lens) self.EPL.set_static(**kwargs_lens) f_static = self.EPL.function(x, y, **kwargs_lens) npt.assert_almost_equal(f_, f_static, decimal=8) self.EPL.set_dynamic() kwargs_lens = {'theta_E': 2., 't': 0.5, 'e1': e1, 'e2': e2} f_dyn = self.EPL.function(x, y, **kwargs_lens) assert f_dyn != f_static
def setup(self): self.chameleon = Chameleon() self.nie = NIE()
def setup(self): from lenstronomy.LensModel.Profiles.spemd import SPEMD self.SPEMD = SPEMD(suppress_fastell=True) from lenstronomy.LensModel.Profiles.nie import NIE self.NIE = NIE()
def setup(self): self.nie = NIE() self.spemd = SPEMD(suppress_fastell=True) self.sis = SIS()
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
class TestEPLvsNIE(object): """ tests the Gaussian methods """ def setup(self): from lenstronomy.LensModel.Profiles.epl import EPL self.EPL = EPL() 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. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) x = np.array([1., 2]) y = np.array([2, 0]) values = self.EPL.function(x, y, phi_E, gamma, e1, e2) values_nie = self.NIE.function(x, y, phi_E, e1, e2, 0.) 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) q = 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) x = np.array([1., 2]) y = np.array([2, 0]) values = self.EPL.function(x, y, phi_E, gamma, e1, e2) values_nie = self.NIE.function(x, y, phi_E, e1, e2, 0.) 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) q = 0.4 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) x = np.array([1., 2]) y = np.array([2, 0]) values = self.EPL.function(x, y, phi_E, gamma, e1, e2) values_nie = self.NIE.function(x, y, phi_E, e1, e2, 0.) 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) def test_derivatives(self): x = np.array([1]) y = np.array([2]) 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.EPL.derivatives(x, y, phi_E, gamma, e1, e2) f_x_nie, f_y_nie = self.NIE.derivatives(x, y, phi_E, e1, e2, 0.) npt.assert_almost_equal(f_x, f_x_nie, decimal=4) npt.assert_almost_equal(f_y, f_y_nie, decimal=4) q = 0.7 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.EPL.derivatives(x, y, phi_E, gamma, e1, e2) f_x_nie, f_y_nie = self.NIE.derivatives(x, y, phi_E, e1, e2, 0.) 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.]) phi_E = 1. gamma = 2. 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.EPL.hessian(x, y, phi_E, gamma, e1, e2) f_xx_nie, f_xy_nie, f_yx_nie, f_yy_nie = self.NIE.hessian(x, y, phi_E, e1, e2, 0.) 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_xy, f_yx, decimal=8) def test_density_lens(self): r = 1 kwargs = {'theta_E': 1, 'gamma': 2, 'e1': 0, 'e2': 0} rho = self.EPL.density_lens(r, **kwargs) from lenstronomy.LensModel.Profiles.spep import SPEP spep = SPEP() rho_spep = spep.density_lens(r, **kwargs) npt.assert_almost_equal(rho, rho_spep, decimal=7) def test_mass_3d_lens(self): r = 1 kwargs = {'theta_E': 1, 'gamma': 2, 'e1': 0, 'e2': 0} rho = self.EPL.mass_3d_lens(r, **kwargs) from lenstronomy.LensModel.Profiles.spep import SPEP spep = SPEP() rho_spep = spep.mass_3d_lens(r, **kwargs) npt.assert_almost_equal(rho, rho_spep, decimal=7) 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., 'gamma': 1.5, 'e1': e1, 'e2': e2} f_ = self.EPL.function(x, y, **kwargs_lens) self.EPL.set_static(**kwargs_lens) f_static = self.EPL.function(x, y, **kwargs_lens) npt.assert_almost_equal(f_, f_static, decimal=8) self.EPL.set_dynamic() kwargs_lens = {'theta_E': 2., 'gamma': 1.9, 'e1': e1, 'e2': e2} f_dyn = self.EPL.function(x, y, **kwargs_lens) assert f_dyn != f_static def test_regularization(self): phi_E = 1. gamma = 2. q = 1. phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) x = 0. y = 0. f_x, f_y = self.EPL.derivatives(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(f_x, 0.) npt.assert_almost_equal(f_y, 0.) x = 0. y = 0. f_xx, f_xy, f_yx, f_yy = self.EPL.hessian(x, y, phi_E, gamma, e1, e2) assert f_xx > 10 ** 5 assert f_yy > 10 ** 5 #npt.assert_almost_equal(f_xx, 10**10) #npt.assert_almost_equal(f_yy, 10**10) npt.assert_almost_equal(f_xy, 0) npt.assert_almost_equal(f_yx, 0)
def __init__(self, static=False): self._nie_1 = NIE() self._nie_2 = NIE() super(Chameleon, self).__init__() self._static = static
class Chameleon(LensProfileBase): """ class of the Chameleon model (See Suyu+2014) an elliptical truncated double isothermal profile """ param_names = ['alpha_1', 'w_c', 'w_t', 'e1', 'e2', 'center_x', 'center_y'] lower_limit_default = {'alpha_1': 0, 'w_c': 0, 'w_t': 0, 'e1': -0.8, 'e2': -0.8, 'center_x': -100, 'center_y': -100} upper_limit_default = {'alpha_1': 100, 'w_c': 100, 'w_t': 100, 'e1': 0.8, 'e2': 0.8, 'center_x': 100, 'center_y': 100} def __init__(self, static=False): self._nie_1 = NIE() self._nie_2 = NIE() super(Chameleon, self).__init__() self._static = static def function(self, x, y, alpha_1, w_c, w_t, e1, e2, center_x=0, center_y=0): """ :param x: ra-coordinate :param y: dec-coordinate :param alpha_1: deflection angle at 1 (arcseconds) from the center :param w_c: see Suyu+2014 :param w_t: see Suyu+2014 :param e1: ellipticity parameter :param e2: ellipticity parameter :param center_x: ra center :param center_y: dec center :return: lensing potential """ theta_E_conv, w_c, w_t, s_scale_1, s_scale_2 = self.param_convert(alpha_1, w_c, w_t, e1, e2) f_1 = self._nie_1.function(x, y, theta_E_conv, e1, e2, s_scale_1, center_x, center_y) f_2 = self._nie_2.function(x, y, theta_E_conv, e1, e2, s_scale_2, center_x, center_y) f_ = f_1 - f_2 return f_ def derivatives(self, x, y, alpha_1, w_c, w_t, e1, e2, center_x=0, center_y=0): """ :param x: ra-coordinate :param y: dec-coordinate :param alpha_1: deflection angle at 1 (arcseconds) from the center :param w_c: see Suyu+2014 :param w_t: see Suyu+2014 :param e1: ellipticity parameter :param e2: ellipticity parameter :param center_x: ra center :param center_y: dec center :return: deflection angles (RA, DEC) """ theta_E_conv, w_c, w_t, s_scale_1, s_scale_2 = self.param_convert(alpha_1, w_c, w_t, e1, e2) f_x_1, f_y_1 = self._nie_1.derivatives(x, y, theta_E_conv, e1, e2, s_scale_1, center_x, center_y) f_x_2, f_y_2 = self._nie_2.derivatives(x, y, theta_E_conv, e1, e2, s_scale_2, center_x, center_y) f_x = f_x_1 - f_x_2 f_y = f_y_1 - f_y_2 return f_x, f_y def hessian(self, x, y, alpha_1, w_c, w_t, e1, e2, center_x=0, center_y=0): """ :param x: ra-coordinate :param y: dec-coordinate :param alpha_1: deflection angle at 1 (arcseconds) from the center :param w_c: see Suyu+2014 :param w_t: see Suyu+2014 :param e1: ellipticity parameter :param e2: ellipticity parameter :param center_x: ra center :param center_y: dec center :return: second derivatives of the lensing potential (Hessian: f_xx, f_xy, f_yx, f_yy) """ theta_E_conv, w_c, w_t, s_scale_1, s_scale_2 = self.param_convert(alpha_1, w_c, w_t, e1, e2) f_xx_1, f_xy_1, f_yx_1, f_yy_1 = self._nie_1.hessian(x, y, theta_E_conv, e1, e2, s_scale_1, center_x, center_y) f_xx_2, f_xy_2, f_yx_2, f_yy_2 = self._nie_2.hessian(x, y, theta_E_conv, e1, e2, s_scale_2, center_x, center_y) f_xx = f_xx_1 - f_xx_2 f_yy = f_yy_1 - f_yy_2 f_xy = f_xy_1 - f_xy_2 f_yx = f_yx_1 - f_yx_2 return f_xx, f_xy, f_yx, f_yy def param_convert(self, alpha_1, w_c, w_t, e1, e2): """ convert the parameter alpha_1 (deflection angle one arcsecond from the center) into the "Einstein radius" scale parameter of the two NIE profiles :param alpha_1: deflection angle at 1 (arcseconds) from the center :param w_c: see Suyu+2014 :param w_t: see Suyu+2014 :param e1: eccentricity modulus :param ee: eccentricity modulus :return: """ if self._static is True: return self._theta_convert_static, self._w_c_static, self._w_t_stactic, self._s_scale_1_static, self._s_scale_2_static return self._param_convert(alpha_1, w_c, w_t, e1, e2) def _param_convert(self, alpha_1, w_c, w_t, e1, e2): if not w_t >= w_c: return 0, w_t, w_c, 1, 1 s_scale_1 = w_c s_scale_2 = w_t f_x_1, f_y_1 = self._nie_1.derivatives(1, 0, theta_E=1, e1=0, e2=0, s_scale=s_scale_1) f_x_2, f_y_2 = self._nie_2.derivatives(1, 0, theta_E=1, e1=0, e2=0, s_scale=s_scale_2) f_x = f_x_1 - f_x_2 theta_E_convert = alpha_1 / f_x phi_G, q = param_util.ellipticity2phi_q(e1, e2) s_scale_1 = np.sqrt(4 * w_c ** 2 / (1. + q) ** 2) s_scale_2 = np.sqrt(4 * w_t ** 2 / (1. + q) ** 2) return theta_E_convert, w_c, w_t, s_scale_1, s_scale_2 def set_static(self, alpha_1, w_c, w_t, e1, e2, center_x=0, center_y=0): """ :param logM: :param concentration: :param center_x: :param center_y: :return: """ self._static = True self._theta_convert_static, self._w_c_static, self._w_t_stactic, self._s_scale_1_static, self._s_scale_2_static = self._param_convert(alpha_1, w_c, w_t, e1, e2) self._nie_1.set_static(self._theta_convert_static, e1, e2, self._s_scale_1_static, center_x, center_y) self._nie_2.set_static(self._theta_convert_static, e1, e2, self._s_scale_2_static, center_x, center_y) def set_dynamic(self): """ :return: """ self._static = False if hasattr(self, '_theta_convert_static'): del self._theta_convert_static if hasattr(self, '_w_c_static'): del self._w_c_static if hasattr(self, '_w_t_stactic'): del self._w_t_stactic if hasattr(self, '_s_scale_1_static'): del self._s_scale_1_static if hasattr(self, '_s_scale_2_static'): del self._s_scale_2_static self._nie_1.set_dynamic() self._nie_2.set_dynamic()
def setup(self): self.nie = NIE() self.spemd = SPEMD_SMOOTH() self.sis = SIS()
def setup(self): from lenstronomy.LensModel.Profiles.epl import EPL self.EPL = EPL() from lenstronomy.LensModel.Profiles.nie import NIE self.NIE = NIE()
def _import_class(lens_type, custom_class, kwargs_interp, 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 :param kwargs_interp: interpolation keyword arguments specifying the numerics. See description in the Interpolate() class. Only applicable for 'INTERPOL' and 'INTERPOL_SCALED' models. :return: class instance of the lens model type """ if lens_type == 'SHIFT': from lenstronomy.LensModel.Profiles.constant_shift import Shift return Shift() elif lens_type == 'NIE_POTENTIAL': from lenstronomy.LensModel.Profiles.nie_potential import NIE_POTENTIAL return NIE_POTENTIAL() elif lens_type == 'CONST_MAG': from lenstronomy.LensModel.Profiles.const_mag import ConstMag return ConstMag() 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 == 'SHEAR_REDUCED': from lenstronomy.LensModel.Profiles.shear import ShearReduced return ShearReduced() elif lens_type == 'CONVERGENCE': from lenstronomy.LensModel.Profiles.convergence import Convergence return Convergence() elif lens_type == 'HESSIAN': from lenstronomy.LensModel.Profiles.hessian import Hessian return Hessian() 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 == 'PEMD': from lenstronomy.LensModel.Profiles.pemd import PEMD return PEMD() elif lens_type == 'SPEMD': from lenstronomy.LensModel.Profiles.spemd import SPEMD return SPEMD() elif lens_type == 'EPL': from lenstronomy.LensModel.Profiles.epl import EPL return EPL() elif lens_type == 'EPL_NUMBA': from lenstronomy.LensModel.Profiles.epl_numba import EPL_numba return EPL_numba() elif lens_type == 'SPL_CORE': from lenstronomy.LensModel.Profiles.splcore import SPLCORE return SPLCORE() 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 == 'NFW_ELLIPSE_CSE': from lenstronomy.LensModel.Profiles.nfw_ellipse_cse import NFW_ELLIPSE_CSE return NFW_ELLIPSE_CSE() elif lens_type == 'TNFW': from lenstronomy.LensModel.Profiles.tnfw import TNFW return TNFW() elif lens_type == 'TNFW_ELLIPSE': from lenstronomy.LensModel.Profiles.tnfw_ellipse import TNFW_ELLIPSE return TNFW_ELLIPSE() 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 == 'HERNQUIST_ELLIPSE_CSE': from lenstronomy.LensModel.Profiles.hernquist_ellipse_cse import HernquistEllipseCSE return HernquistEllipseCSE() 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(**kwargs_interp) elif lens_type == 'INTERPOL_SCALED': from lenstronomy.LensModel.Profiles.interpol import InterpolScaled return InterpolScaled(**kwargs_interp) 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_CONST': from lenstronomy.LensModel.Profiles.curved_arc_const import CurvedArcConst return CurvedArcConst() elif lens_type == 'CURVED_ARC_CONST_MST': from lenstronomy.LensModel.Profiles.curved_arc_const import CurvedArcConstMST return CurvedArcConstMST() elif lens_type == 'CURVED_ARC_SPP': from lenstronomy.LensModel.Profiles.curved_arc_spp import CurvedArcSPP return CurvedArcSPP() elif lens_type == 'CURVED_ARC_SIS_MST': from lenstronomy.LensModel.Profiles.curved_arc_sis_mst import CurvedArcSISMST return CurvedArcSISMST() elif lens_type == 'CURVED_ARC_SPT': from lenstronomy.LensModel.Profiles.curved_arc_spt import CurvedArcSPT return CurvedArcSPT() elif lens_type == 'CURVED_ARC_TAN_DIFF': from lenstronomy.LensModel.Profiles.curved_arc_tan_diff import CurvedArcTanDiff return CurvedArcTanDiff() 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_EXP': from lenstronomy.LensModel.Profiles.cored_density_exp import CoredDensityExp return CoredDensityExp() 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 == 'CORED_DENSITY_EXP_MST': from lenstronomy.LensModel.Profiles.cored_density_mst import CoredDensityMST return CoredDensityMST(profile_type='CORED_DENSITY_EXP') elif lens_type == 'NumericalAlpha': from lenstronomy.LensModel.Profiles.numerical_deflections import NumericalAlpha return NumericalAlpha(custom_class) elif lens_type == 'MULTIPOLE': from lenstronomy.LensModel.Profiles.multipole import Multipole return Multipole() elif lens_type == 'CSE': from lenstronomy.LensModel.Profiles.cored_steep_ellipsoid import CSE return CSE() elif lens_type == 'ElliSLICE': from lenstronomy.LensModel.Profiles.elliptical_density_slice import ElliSLICE return ElliSLICE() elif lens_type == 'ULDM': from lenstronomy.LensModel.Profiles.uldm import Uldm return Uldm() elif lens_type == 'CORED_DENSITY_ULDM_MST': from lenstronomy.LensModel.Profiles.cored_density_mst import CoredDensityMST return CoredDensityMST(profile_type='CORED_DENSITY_ULDM') else: raise ValueError( '%s is not a valid lens model. Supported are: %s.' % (lens_type, _SUPPORTED_MODELS))
class TestNIE(object): """ tests the Gaussian methods """ def setup(self): self.nie = NIE() self.spemd = SPEMD(suppress_fastell=True) 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 TestSIE(object): """ tests the Gaussian methods """ def setup(self): from lenstronomy.LensModel.Profiles.sie import SIE from lenstronomy.LensModel.Profiles.spemd import SPEMD from lenstronomy.LensModel.Profiles.nie import NIE self.sie = SIE(NIE=False) self.sie_nie = SIE(NIE=True) self.spemd = SPEMD() self.nie = NIE() def test_function(self): x = np.array([1]) y = np.array([2]) theta_E = 1. q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.sie.function(x, y, theta_E, e1, e2) gamma = 2 values_spemd = self.spemd.function(x, y, theta_E, gamma, e1, e2) assert values == values_spemd values = self.sie_nie.function(x, y, theta_E, e1, e2) s_scale = 0.0000001 values_spemd = self.nie.function(x, y, theta_E, e1, e2, s_scale) npt.assert_almost_equal(values, values_spemd, decimal=6) def test_derivatives(self): x = np.array([1]) y = np.array([2]) theta_E = 1. q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.sie.derivatives(x, y, theta_E, e1, e2) gamma = 2 values_spemd = self.spemd.derivatives(x, y, theta_E, gamma, e1, e2) assert values == values_spemd values = self.sie_nie.derivatives(x, y, theta_E, e1, e2) s_scale = 0.0000001 values_spemd = self.nie.derivatives(x, y, theta_E, e1, e2, s_scale) npt.assert_almost_equal(values, values_spemd, decimal=6) def test_hessian(self): x = np.array([1]) y = np.array([2]) theta_E = 1. q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.sie.hessian(x, y, theta_E, e1, e2) gamma = 2 values_spemd = self.spemd.hessian(x, y, theta_E, gamma, e1, e2) assert values[0] == values_spemd[0] values = self.sie_nie.hessian(x, y, theta_E, e1, e2) s_scale = 0.0000001 values_spemd = self.nie.hessian(x, y, theta_E, e1, e2, s_scale) npt.assert_almost_equal(values, values_spemd, decimal=5)
class TestSPEMD(object): """ tests the Gaussian methods """ def setup(self): from lenstronomy.LensModel.Profiles.spemd import SPEMD self.SPEMD = SPEMD(suppress_fastell=True) 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.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.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.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_xy, f_yx, f_yy = self.SPEMD.hessian(x, y, phi_E, gamma, e1, e2, s_scale) if fastell4py_bool: f_xx_nie, f_xy_nie, f_yx_nie, f_yy_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) npt.assert_almost_equal(f_yx, f_yx_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) npt.assert_almost_equal(f_xy, f_yx, decimal=8) def test_bounds(self): compute_bool = self.SPEMD._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.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 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 _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 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)
class TestChameleon(object): """ class to test the Moffat profile """ def setup(self): self.chameleon = Chameleon() self.nie = NIE() def test_theta_E_convert(self): w_c, w_t = 2, 1 theta_E_convert, w_c, w_t = self.chameleon._theta_E_convert(theta_E=1, w_c=w_c, w_t=w_t) assert w_c == 1 assert w_t == 2 def test_function(self): """ :return: """ x = np.linspace(0.1, 10, 10) w_c, w_t = 0.5, 1. phi_G, q = 0.3, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_light = { 'theta_E': 1., 'w_c': .5, 'w_t': 1., 'e1': e1, 'e2': e2 } theta_E_convert, w_c, w_t = self.chameleon._theta_E_convert(theta_E=1, w_c=0.5, w_t=1.) s_scale_1 = np.sqrt(4 * w_c**2 / (1. + q)**2) s_scale_2 = np.sqrt(4 * w_t**2 / (1. + q)**2) kwargs_1 = { 'theta_E': theta_E_convert, 's_scale': s_scale_1, 'e1': e1, 'e2': e2 } kwargs_2 = { 'theta_E': theta_E_convert, 's_scale': s_scale_2, 'e1': e1, 'e2': e2 } f_ = self.chameleon.function(x=x, y=1., **kwargs_light) f_1 = self.nie.function(x=x, y=1., **kwargs_1) f_2 = self.nie.function(x=x, y=1., **kwargs_2) npt.assert_almost_equal(f_, (f_1 - f_2), decimal=5) def test_derivatives(self): """ :return: """ x = np.linspace(0.1, 10, 10) w_c, w_t = 0.5, 1. phi_G, q = 0.3, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_light = { 'theta_E': 1., 'w_c': .5, 'w_t': 1., 'e1': e1, 'e2': e2 } theta_E_convert, w_c, w_t = self.chameleon._theta_E_convert(theta_E=1, w_c=0.5, w_t=1.) s_scale_1 = np.sqrt(4 * w_c**2 / (1. + q)**2) s_scale_2 = np.sqrt(4 * w_t**2 / (1. + q)**2) kwargs_1 = { 'theta_E': theta_E_convert, 's_scale': s_scale_1, 'e1': e1, 'e2': e2 } kwargs_2 = { 'theta_E': theta_E_convert, 's_scale': s_scale_2, 'e1': e1, 'e2': e2 } f_x, f_y = self.chameleon.derivatives(x=x, y=1., **kwargs_light) f_x_1, f_y_1 = self.nie.derivatives(x=x, y=1., **kwargs_1) f_x_2, f_y_2 = self.nie.derivatives(x=x, y=1., **kwargs_2) npt.assert_almost_equal(f_x, (f_x_1 - f_x_2), decimal=5) npt.assert_almost_equal(f_y, (f_y_1 - f_y_2), decimal=5) f_x, f_y = self.chameleon.derivatives(x=1, y=0., **kwargs_light) npt.assert_almost_equal(f_x, 1, decimal=1) def test_hessian(self): """ :return: """ x = np.linspace(0.1, 10, 10) w_c, w_t = 0.5, 1. phi_G, q = 0.3, 0.8 e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) kwargs_light = { 'theta_E': 1., 'w_c': .5, 'w_t': 1., 'e1': e1, 'e2': e2 } theta_E_convert, w_c, w_t = self.chameleon._theta_E_convert(theta_E=1, w_c=0.5, w_t=1.) s_scale_1 = np.sqrt(4 * w_c**2 / (1. + q)**2) s_scale_2 = np.sqrt(4 * w_t**2 / (1. + q)**2) kwargs_1 = { 'theta_E': theta_E_convert, 's_scale': s_scale_1, 'e1': e1, 'e2': e2 } kwargs_2 = { 'theta_E': theta_E_convert, 's_scale': s_scale_2, 'e1': e1, 'e2': e2 } f_xx, f_yy, f_xy = self.chameleon.hessian(x=x, y=1., **kwargs_light) f_xx_1, f_yy_1, f_xy_1 = self.nie.hessian(x=x, y=1., **kwargs_1) f_xx_2, f_yy_2, f_xy_2 = self.nie.hessian(x=x, y=1., **kwargs_2) npt.assert_almost_equal(f_xx, (f_xx_1 - f_xx_2), decimal=5) npt.assert_almost_equal(f_yy, (f_yy_1 - f_yy_2), decimal=5) npt.assert_almost_equal(f_xy, (f_xy_1 - f_xy_2), decimal=5)
class Chameleon(object): """ class of the Chameleon model (See Suyu+2014) an elliptical truncated double isothermal profile """ param_names = ['theta_E', 'w_c', 'w_t', 'e1', 'e2', 'center_x', 'center_y'] lower_limit_default = { 'theta_E': 0, 'w_c': 0, 'w_t': 0, 'e1': -0.8, 'e2': -0.8, 'center_x': -100, 'center_y': -100 } upper_limit_default = { 'theta_E': 100, 'w_c': 100, 'w_t': 100, 'e1': 0.8, 'e2': 0.8, 'center_x': 100, 'center_y': 100 } def __init__(self): self.nie = NIE() def function(self, x, y, theta_E, w_c, w_t, e1, e2, center_x=0, center_y=0): """ :param x: ra-coordinate :param y: dec-coordinate :param amp: amplitude of first power-law flux :param flux_ratio: ratio of amplitudes of first to second power-law profile :param gamma1: power-law slope :param gamma2: power-law slope :param e1: ellipticity parameter :param e2: ellipticity parameter :param center_x: center :param center_y: center :return: flux of chameleon profile """ theta_E_conv, w_c, w_t = self._theta_E_convert(theta_E, w_c, w_t) #if not w_t > w_c: # w_t, w_c = w_c, w_t phi_G, q = param_util.ellipticity2phi_q(e1, e2) s_scale_1 = np.sqrt(4 * w_c**2 / (1. + q)**2) s_scale_2 = np.sqrt(4 * w_t**2 / (1. + q)**2) f_1 = self.nie.function(x, y, theta_E_conv, e1, e2, s_scale_1, center_x, center_y) f_2 = self.nie.function(x, y, theta_E_conv, e1, e2, s_scale_2, center_x, center_y) f_ = f_1 - f_2 return f_ def derivatives(self, x, y, theta_E, w_c, w_t, e1, e2, center_x=0, center_y=0): """ :param x: ra-coordinate :param y: dec-coordinate :param amp: amplitude of first power-law flux :param flux_ratio: ratio of amplitudes of first to second power-law profile :param gamma1: power-law slope :param gamma2: power-law slope :param e1: ellipticity parameter :param e2: ellipticity parameter :param center_x: center :param center_y: center :return: flux of chameleon profile """ theta_E_conv, w_c, w_t = self._theta_E_convert(theta_E, w_c, w_t) #if not w_t > w_c: # w_t, w_c = w_c, w_t phi_G, q = param_util.ellipticity2phi_q(e1, e2) s_scale_1 = np.sqrt(4 * w_c**2 / (1. + q)**2) s_scale_2 = np.sqrt(4 * w_t**2 / (1. + q)**2) f_x_1, f_y_1 = self.nie.derivatives(x, y, theta_E_conv, e1, e2, s_scale_1, center_x, center_y) f_x_2, f_y_2 = self.nie.derivatives(x, y, theta_E_conv, e1, e2, s_scale_2, center_x, center_y) f_x = f_x_1 - f_x_2 f_y = f_y_1 - f_y_2 return f_x, f_y def hessian(self, x, y, theta_E, w_c, w_t, e1, e2, center_x=0, center_y=0): """ :param x: ra-coordinate :param y: dec-coordinate :param theta_E: amplitude of first power-law flux :param flux_ratio: ratio of amplitudes of first to second power-law profile :param gamma1: power-law slope :param gamma2: power-law slope :param e1: ellipticity parameter :param e2: ellipticity parameter :param center_x: center :param center_y: center :return: flux of chameleon profile """ theta_E_conv, w_c, w_t = self._theta_E_convert(theta_E, w_c, w_t) #if not w_t > w_c: # w_t, w_c = w_c, w_t phi_G, q = param_util.ellipticity2phi_q(e1, e2) s_scale_1 = np.sqrt(4 * w_c**2 / (1. + q)**2) s_scale_2 = np.sqrt(4 * w_t**2 / (1. + q)**2) f_xx_1, f_yy_1, f_xy_1 = self.nie.hessian(x, y, theta_E_conv, e1, e2, s_scale_1, center_x, center_y) f_xx_2, f_yy_2, f_xy_2 = self.nie.hessian(x, y, theta_E_conv, e1, e2, s_scale_2, center_x, center_y) f_xx = f_xx_1 - f_xx_2 f_yy = f_yy_1 - f_yy_2 f_xy = f_xy_1 - f_xy_2 return f_xx, f_yy, f_xy def _theta_E_convert(self, theta_E, w_c, w_t): """ convert the parameter theta_E (deflection angle one arcsecond from the center) into the "Einstein radius" scale parameter of the two NIE profiles :param theta_E: :param w_c: :param w_t: :return: """ if not w_t > w_c: w_t, w_c = w_c, w_t s_scale_1 = w_c s_scale_2 = w_t f_x_1, f_y_1 = self.nie.derivatives(1, 0, theta_E=1, e1=0, e2=0, s_scale=s_scale_1) f_x_2, f_y_2 = self.nie.derivatives(1, 0, theta_E=1, e1=0, e2=0, s_scale=s_scale_2) f_x = f_x_1 - f_x_2 theta_E_convert = theta_E / f_x return theta_E_convert, w_c, w_t