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.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 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()
class TestSIE(object): """ tests the Gaussian methods """ def setup(self): from lenstronomy.LensModel.Profiles.sie import SIE from lenstronomy.LensModel.Profiles.spemd import SPEMD self.sie = SIE() self.spemd = SPEMD() def test_function(self): x = np.array([1]) y = np.array([2]) theta_E = 1. q = 0.9 phi_G = 1. values = self.sie.function(x, y, theta_E, q, phi_G) gamma = 2 values_spemd = self.spemd.function(x, y, theta_E, gamma, q, phi_G) assert values == values_spemd def test_derivatives(self): x = np.array([1]) y = np.array([2]) theta_E = 1. q = 0.9 phi_G = 1. values = self.sie.derivatives(x, y, theta_E, q, phi_G) gamma = 2 values_spemd = self.spemd.derivatives(x, y, theta_E, gamma, q, phi_G) assert values == values_spemd def test_hessian(self): x = np.array([1]) y = np.array([2]) theta_E = 1. q = 0.9 phi_G = 1. values = self.sie.hessian(x, y, theta_E, q, phi_G) gamma = 2 values_spemd = self.spemd.hessian(x, y, theta_E, gamma, q, phi_G) assert values[0] == values_spemd[0]
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)
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.SPEP = SPEP() self.SIE = SIE()
class TestSPEP(object): """ tests the Gaussian methods """ def setup(self): self.SPEP = SPEP() self.SIE = SIE() def test_function(self): x = 1 y = 2 phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) values = self.SPEP.function(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(values, 2.104213947346917, decimal=7) x = np.array([0]) y = np.array([0]) values = self.SPEP.function(x, y, phi_E, gamma, e1, e2) assert values[0] == 0 x = np.array([2, 3, 4]) y = np.array([1, 1, 1]) values = self.SPEP.function(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(values[0], 2.1709510681181285, decimal=7) npt.assert_almost_equal(values[1], 3.2293397784259108, decimal=7) npt.assert_almost_equal(values[2], 4.3624056004556948, decimal=7) def test_derivatives(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.SPEP.derivatives(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(f_x[0], 0.43989645846696634, decimal=7) npt.assert_almost_equal(f_y[0], 0.93736944180732129, decimal=7) x = np.array([0]) y = np.array([0]) f_x, f_y = self.SPEP.derivatives(x, y, phi_E, gamma, e1, e2) assert f_x[0] == 0 assert f_y[0] == 0 x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) values = self.SPEP.derivatives(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(values[0][0], 0.43989645846696634, decimal=7) npt.assert_almost_equal(values[1][0], 0.93736944180732129, decimal=7) npt.assert_almost_equal(values[0][1], 1.1029501948308649, decimal=7) npt.assert_almost_equal(values[1][1], 0.24342317177590794, decimal=7) x = 1 y = 2 phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_x, f_y = self.SPEP.derivatives(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(f_x, 0.43989645846696634, decimal=7) npt.assert_almost_equal(f_y, 0.93736944180732129, decimal=7) x = 0 y = 0 f_x, f_y = self.SPEP.derivatives(x, y, phi_E, gamma, e1, e2) assert f_x == 0 assert f_y == 0 def test_hessian(self): x = np.array([1]) y = np.array([2]) phi_E = 1. gamma = 1.9 q = 0.9 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_yy,f_xy = self.SPEP.hessian(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(f_xx[0], 0.46312881977317422, decimal=7) npt.assert_almost_equal(f_yy[0], 0.15165326557198552, decimal=7) npt.assert_almost_equal(f_xy[0], -0.20956958696323871, decimal=7) x = np.array([1,3,4]) y = np.array([2,1,1]) values = self.SPEP.hessian(x, y, phi_E, gamma, e1, e2) npt.assert_almost_equal(values[0][0], 0.46312881977317422, decimal=7) npt.assert_almost_equal(values[1][0], 0.15165326557198552, decimal=7) npt.assert_almost_equal(values[2][0], -0.20956958696323871, decimal=7) npt.assert_almost_equal(values[0][1], 0.070999592014527796, decimal=7) npt.assert_almost_equal(values[1][1], 0.33245358685908111, decimal=7) npt.assert_almost_equal(values[2][1], -0.10270375656049677, decimal=7) def test_spep_sie_conventions(self): x = np.array([1., 2., 0.]) y = np.array([2, 1., 1.]) phi_E = 1. gamma = 2 q = 0.9999 phi_G = 1. e1, e2 = param_util.phi_q2_ellipticity(phi_G, q) f_xx, f_yy, f_xy = self.SPEP.hessian(x, y, phi_E, gamma, e1, e2) f_xx_sie, f_yy_sie, f_xy_sie = self.SIE.hessian(x, y, phi_E, e1, e2) npt.assert_almost_equal(f_xx, f_xx_sie, decimal=4) npt.assert_almost_equal(f_yy, f_yy_sie, decimal=4) npt.assert_almost_equal(f_xy, f_xy_sie, decimal=4)
class CurvedArcTanDiff(LensProfileBase): """ Curved arc model with an additional non-zero tangential stretch differential in tangential direction component Observables are: - curvature radius (basically bending relative to the center of the profile) - radial stretch (plus sign) thickness of arc with parity (more generalized than the power-law slope) - tangential stretch (plus sign). Infinity means at critical curve - direction of curvature - position of arc Requirements: - Should work with other perturbative models without breaking its meaning (say when adding additional shear terms) - Must best reflect the observables in lensing - minimal covariances between the parameters, intuitive parameterization. """ param_names = [ 'tangential_stretch', 'radial_stretch', 'curvature', 'dtan_dtan', 'direction', 'center_x', 'center_y' ] lower_limit_default = { 'tangential_stretch': -100, 'radial_stretch': -5, 'curvature': 0.000001, 'dtan_dtan': -10, 'direction': -np.pi, 'center_x': -100, 'center_y': -100 } upper_limit_default = { 'tangential_stretch': 100, 'radial_stretch': 5, 'curvature': 100, 'dtan_dtan': 10, 'direction': np.pi, 'center_x': 100, 'center_y': 100 } def __init__(self): self._sie = SIE(NIE=True) self._mst = Convergence() super(CurvedArcTanDiff, self).__init__() @staticmethod def stretch2sie_mst(tangential_stretch, radial_stretch, curvature, dtan_dtan, direction, center_x, center_y): """ :param tangential_stretch: float, stretch of intrinsic source in tangential direction :param radial_stretch: float, stretch of intrinsic source in radial direction :param curvature: 1/curvature radius :param dtan_dtan: d(tangential_stretch) / d(tangential direction) / tangential stretch :param direction: float, angle in radian :param center_x: center of source in image plane :param center_y: center of source in image plane :return: parameters in terms of a spherical SIS + MST resulting in the same observables """ center_x_sis, center_y_sis = center_deflector(curvature, direction, center_x, center_y) r_curvature = 1. / curvature lambda_mst = 1. / radial_stretch kappa_ext = 1 - lambda_mst theta_E = r_curvature * (1. - radial_stretch / tangential_stretch) # analytic relation (see Birrer 2021) dlambda_tan_dr = tangential_stretch / r_curvature * ( 1 - tangential_stretch / radial_stretch) # translate tangential eigenvalue gradient in lens ellipticity dtan_dtan_ = dtan_dtan * tangential_stretch epsilon = np.abs(dtan_dtan_ / dlambda_tan_dr) # bound epsilon by (-1, 1) epsilon = np.minimum(epsilon, 0.99999) q = np.sqrt((1 - epsilon) / (1 + epsilon)) if dtan_dtan_ < 0: phi = direction - np.pi / 4 else: phi = direction + np.pi / 4 e1_sie, e2_sie = param_util.phi_q2_ellipticity(phi, q) # ellipticity adopted Einstein radius to match local tangential and radial stretch factor = np.sqrt(1 + q**2) / np.sqrt(2 * q) theta_E_sie = theta_E * factor return theta_E_sie, e1_sie, e2_sie, kappa_ext, center_x_sis, center_y_sis def function(self, x, y, tangential_stretch, radial_stretch, curvature, dtan_dtan, direction, center_x, center_y): """ ATTENTION: there may not be a global lensing potential! :param x: :param y: :param tangential_stretch: float, stretch of intrinsic source in tangential direction :param radial_stretch: float, stretch of intrinsic source in radial direction :param curvature: 1/curvature radius :param direction: float, angle in radian :param dtan_dtan: d(tangential_stretch) / d(tangential direction) / tangential stretch :param center_x: center of source in image plane :param center_y: center of source in image plane :return: """ lambda_mst = 1. / radial_stretch theta_E_sie, e1_sie, e2_sie, kappa_ext, center_x_sis, center_y_sis = self.stretch2sie_mst( tangential_stretch, radial_stretch, curvature, dtan_dtan, direction, center_x, center_y) f_sis = self._sie.function( x, y, theta_E_sie, e1_sie, e2_sie, center_x_sis, center_y_sis ) # - self._sis.function(center_x, center_y, theta_E, center_x_sis, center_y_sis) alpha_x, alpha_y = self._sie.derivatives(center_x, center_y, theta_E_sie, e1_sie, e2_sie, center_x_sis, center_y_sis) f_sis_0 = alpha_x * (x - center_x) + alpha_y * (y - center_y) f_mst = self._mst.function(x, y, kappa_ext, ra_0=center_x, dec_0=center_y) return lambda_mst * (f_sis - f_sis_0) + f_mst def derivatives(self, x, y, tangential_stretch, radial_stretch, curvature, dtan_dtan, direction, center_x, center_y): """ :param x: :param y: :param tangential_stretch: float, stretch of intrinsic source in tangential direction :param radial_stretch: float, stretch of intrinsic source in radial direction :param curvature: 1/curvature radius :param direction: float, angle in radian :param dtan_dtan: d(tangential_stretch) / d(tangential direction) / tangential stretch :param center_x: center of source in image plane :param center_y: center of source in image plane :return: """ lambda_mst = 1. / radial_stretch theta_E_sie, e1_sie, e2_sie, kappa_ext, center_x_sis, center_y_sis = self.stretch2sie_mst( tangential_stretch, radial_stretch, curvature, dtan_dtan, direction, center_x, center_y) f_x_sis, f_y_sis = self._sie.derivatives(x, y, theta_E_sie, e1_sie, e2_sie, center_x_sis, center_y_sis) f_x0, f_y0 = self._sie.derivatives(center_x, center_y, theta_E_sie, e1_sie, e2_sie, center_x_sis, center_y_sis) f_x_mst, f_y_mst = self._mst.derivatives(x, y, kappa_ext, ra_0=center_x, dec_0=center_y) f_x = lambda_mst * (f_x_sis - f_x0) + f_x_mst f_y = lambda_mst * (f_y_sis - f_y0) + f_y_mst return f_x, f_y def hessian(self, x, y, tangential_stretch, radial_stretch, curvature, dtan_dtan, direction, center_x, center_y): """ :param x: :param y: :param tangential_stretch: float, stretch of intrinsic source in tangential direction :param radial_stretch: float, stretch of intrinsic source in radial direction :param curvature: 1/curvature radius :param direction: float, angle in radian :param dtan_dtan: d(tangential_stretch) / d(tangential direction) / tangential stretch :param center_x: center of source in image plane :param center_y: center of source in image plane :return: """ lambda_mst = 1. / radial_stretch theta_E_sie, e1_sie, e2_sie, kappa_ext, center_x_sis, center_y_sis = self.stretch2sie_mst( tangential_stretch, radial_stretch, curvature, dtan_dtan, direction, center_x, center_y) f_xx_sis, f_xy_sis, f_yx_sis, f_yy_sis = self._sie.hessian( x, y, theta_E_sie, e1_sie, e2_sie, center_x_sis, center_y_sis) f_xx_mst, f_xy_mst, f_yx_mst, f_yy_mst = self._mst.hessian( x, y, kappa_ext, ra_0=center_x, dec_0=center_y) return lambda_mst * f_xx_sis + f_xx_mst, lambda_mst * f_xy_sis + f_xy_mst, lambda_mst * f_yx_sis + f_yx_mst, lambda_mst * f_yy_sis + f_yy_mst
def __init__(self): self._sie = SIE(NIE=True) self._mst = Convergence() super(CurvedArcTanDiff, self).__init__()
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 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)
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))
def setup(self): self.model = CurvedArcTanDiff() self.sie = SIE() self.mst = Convergence()
def setup(self): from lenstronomy.LensModel.Profiles.sie import SIE from lenstronomy.LensModel.Profiles.spemd import SPEMD self.sie = SIE() self.spemd = SPEMD()