def __init__(self, z_lens, z_source, kwargs_model, cosmo_fiducial=None, lens_model_kinematics_bool=None, light_model_kinematics_bool=None, kwargs_seeing={}, kwargs_aperture={}, anisotropy_model=None): if cosmo_fiducial is None: cosmo_fiducial = default_cosmology.get() self._z_lens = z_lens self._z_source = z_source self._cosmo_fiducial = cosmo_fiducial self._lens_cosmo = LensCosmo(z_lens=z_lens, z_source=z_source, cosmo=self._cosmo_fiducial) self.LensModel, self.SourceModel, self.LensLightModel, self.PointSource, extinction_class = class_creator.create_class_instances( all_models=True, **kwargs_model) super(TDCosmography, self).__init__( z_lens=z_lens, z_source=z_source, kwargs_model=kwargs_model, cosmo=cosmo_fiducial, lens_model_kinematics_bool=lens_model_kinematics_bool, light_model_kinematics_bool=light_model_kinematics_bool, kwargs_seeing=kwargs_seeing, kwargs_aperture=kwargs_aperture, anisotropy_model=anisotropy_model)
def setup(self): self.z_lens, self.z_source = 0.5, 2 from astropy.cosmology import FlatLambdaCDM cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Ob0=0.05) self.nfw = NFW() self.nfwmc = NFWMC(z_source=self.z_source, z_lens=self.z_lens, cosmo=cosmo) self.lensCosmo = LensCosmo(z_lens=self.z_lens, z_source=self.z_source, cosmo=cosmo)
def get_theta_E_SIS(self, vel_disp_iso, z_lens, z_src): """Compute the Einstein radius for a given isotropic velocity dispersion assuming a singular isothermal sphere (SIS) mass profile Parameters ---------- vel_disp_iso : float isotropic velocity dispersion, or an approximation to it, in km/s z_lens : float the lens redshift z_src : float the source redshift Note ---- The computation is purely analytic. .. math:: \theta_E = 4 \pi \frac{\sigma_V^2}{c^2} \frac{D_{ls}}{D_s} Returns ------- float the Einstein radius for an SIS in arcsec """ lens_cosmo = LensCosmo(z_lens, z_src, cosmo=self.cosmo) theta_E_SIS = lens_cosmo.sis_sigma_v2theta_E(vel_disp_iso) return theta_E_SIS
def physical2lensing_conversion(self, kwargs_mass): """ :param kwargs_mass: list of keyword arguments of all the lens models. Einstein radius 'theta_E' are replaced by 'sigma_v', velocity dispersion in km/s, 'alpha_Rs' and 'Rs' of NFW profiles are replaced by 'M200' and 'concentration' :return: kwargs_lens in reduced deflection angles compatible with the lensModel instance of this module """ kwargs_lens = copy.deepcopy(kwargs_mass) for i in range(len(kwargs_mass)): kwargs_mass_i = kwargs_mass[i] if self._lens_redshift_list is None: z_lens = self._z_lens else: z_lens = self._lens_redshift_list[i] lens_cosmo = LensCosmo(z_lens, self._z_source_convention, cosmo=self._cosmo) if 'sigma_v' in kwargs_mass_i: sigma_v = kwargs_mass_i['sigma_v'] theta_E = lens_cosmo.sis_sigma_v2theta_E(sigma_v) kwargs_lens[i]['theta_E'] = theta_E del kwargs_lens[i]['sigma_v'] elif 'M200' in kwargs_mass_i: M200 = kwargs_mass_i['M200'] c = kwargs_mass_i['concentration'] Rs, alpha_RS = lens_cosmo.nfw_physical2angle(M200, c) kwargs_lens[i]['Rs'] = Rs kwargs_lens[i]['alpha_Rs'] = alpha_RS del kwargs_lens[i]['M200'] del kwargs_lens[i]['concentration'] return kwargs_lens
class TestNFWMC(object): """ tests the Gaussian methods """ def setup(self): self.z_lens, self.z_source = 0.5, 2 from astropy.cosmology import FlatLambdaCDM cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Ob0=0.05) self.nfw = NFW() self.nfwmc = NFWMC(z_source=self.z_source, z_lens=self.z_lens, cosmo=cosmo) self.lensCosmo = LensCosmo(z_lens=self.z_lens, z_source=self.z_source, cosmo=cosmo) def test_function(self): x, y = 1., 1. logM = 12 concentration = 10 f_mc = self.nfwmc.function(x, y, logM, concentration, center_x=0, center_y=0) Rs, alpha_Rs = self.lensCosmo.nfw_physical2angle(10**logM, concentration) f_ = self.nfw.function(x, y, Rs, alpha_Rs, center_x=0, center_y=0) npt.assert_almost_equal(f_mc, f_, decimal=8) def test_derivatives(self): x, y = 1., 1. logM = 12 concentration = 10 f_x_mc, f_y_mc = self.nfwmc.derivatives(x, y, logM, concentration, center_x=0, center_y=0) Rs, alpha_Rs = self.lensCosmo.nfw_physical2angle(10 ** logM, concentration) f_x, f_y = self.nfw.derivatives(x, y, Rs, alpha_Rs, center_x=0, center_y=0) npt.assert_almost_equal(f_x_mc, f_x, decimal=8) npt.assert_almost_equal(f_y_mc, f_y, decimal=8) def test_hessian(self): x, y = 1., 1. logM = 12 concentration = 10 f_xx_mc, f_xy_mc, f_yx_mc, f_yy_mc = self.nfwmc.hessian(x, y, logM, concentration, center_x=0, center_y=0) Rs, alpha_Rs = self.lensCosmo.nfw_physical2angle(10 ** logM, concentration) f_xx, f_xy, f_yx, f_yy = self.nfw.hessian(x, y, Rs, alpha_Rs, center_x=0, center_y=0) npt.assert_almost_equal(f_xx_mc, f_xx, decimal=8) npt.assert_almost_equal(f_yy_mc, f_yy, decimal=8) npt.assert_almost_equal(f_xy_mc, f_xy, decimal=8) npt.assert_almost_equal(f_yx_mc, f_yx, decimal=8) def test_static(self): x, y = 1., 1. logM = 12 concentration = 10 f_ = self.nfwmc.function(x, y, logM, concentration, center_x=0, center_y=0) self.nfwmc.set_static(logM, concentration) f_static = self.nfwmc.function(x, y, 0, 0, center_x=0, center_y=0) npt.assert_almost_equal(f_, f_static, decimal=8) self.nfwmc.set_dynamic() f_dyn = self.nfwmc.function(x, y, 11, 20, center_x=0, center_y=0) assert f_dyn != f_static
def __init__(self, z_lens, z_source, kwargs_model, cosmo_fiducial=None, lens_model_kinematics_bool=None, light_model_kinematics_bool=None, kwargs_seeing={}, kwargs_aperture={}, anisotropy_model=None, multi_observations=False, kwargs_lens_eqn_solver={}): """ :param z_lens: redshift of deflector :param z_source: redshift of source :param kwargs_model: model configurations (according to FittingSequence) :param cosmo_fiducial: fiducial cosmology used to compute angular diameter distances where required :param lens_model_kinematics_bool: (optional) bool list, corresponding to lens models being included into the kinematics modeling :param light_model_kinematics_bool: (optional) bool list, corresponding to lens light models being included into the kinematics modeling :param kwargs_seeing: seeing conditions (see observation class in Galkin) :param kwargs_aperture: aperture keyword arguments (see aperture class in Galkin) :param anisotropy_model: string, anisotropy model type :param multi_observations: bool, if True, interprets kwargs_aperture and kwargs_seeing as lists of multiple observations """ if cosmo_fiducial is None: cosmo_fiducial = default_cosmology.get() self._z_lens = z_lens self._z_source = z_source self._cosmo_fiducial = cosmo_fiducial self._lens_cosmo = LensCosmo(z_lens=z_lens, z_source=z_source, cosmo=self._cosmo_fiducial) self.LensModel, self.SourceModel, self.LensLightModel, self.PointSource, extinction_class = class_creator.create_class_instances( all_models=True, **kwargs_model, kwargs_lens_eqn_solver=kwargs_lens_eqn_solver) super(TDCosmography, self).__init__( z_lens=z_lens, z_source=z_source, kwargs_model=kwargs_model, cosmo=cosmo_fiducial, lens_model_kinematics_bool=lens_model_kinematics_bool, light_model_kinematics_bool=light_model_kinematics_bool, kwargs_seeing=kwargs_seeing, kwargs_aperture=kwargs_aperture, anisotropy_model=anisotropy_model, multi_observations=multi_observations, kwargs_lens_eqn_solver=kwargs_lens_eqn_solver)
def __init__(self, z_lens, z_source, cosmo=None): """ :param z_lens: redshift of lens :param z_source: redshift of source :param cosmo: astropy cosmology instance """ if cosmo is None: from astropy.cosmology import FlatLambdaCDM cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Ob0=0.05) self._lens_cosmo = LensCosmo(z_lens=z_lens, z_source=z_source, cosmo=cosmo) super(NFWVirTrunc, self).__init__()
def __init__(self, z_lens, z_source, kwargs_model, cosmo=None): self.z_d = z_lens self.z_s = z_source self.lensCosmo = LensCosmo(z_lens, z_source, cosmo=cosmo) self.lens_analysis = LensAnalysis(kwargs_model) self.lens_model = LensModelExtensions( lens_model_list=kwargs_model['lens_model_list']) self.kwargs_options = kwargs_model kwargs_cosmo = { 'D_d': self.lensCosmo.D_d, 'D_s': self.lensCosmo.D_s, 'D_ds': self.lensCosmo.D_ds } self.dispersion = Velocity_dispersion(kwargs_cosmo=kwargs_cosmo)
def get_nfw_kwargs(halo_mass, stellar_mass, halo_z, z_src, seed): c_200 = get_concentration(halo_mass, stellar_mass, seed=seed) n_halos = len(halo_mass) Rs_angle, alpha_Rs = np.empty(n_halos), np.empty(n_halos) lensing_eff = np.empty(n_halos) for h in range(n_halos): lens_cosmo = LensCosmo(z_lens=halo_z[h], z_source=z_src, cosmo=WMAP7) eff = lens_cosmo.dds / lens_cosmo.ds Rs_angle_h, alpha_Rs_h = lens_cosmo.nfw_physical2angle(M=halo_mass[h], c=c_200[h]) Rs_angle[h] = Rs_angle_h alpha_Rs[h] = alpha_Rs_h lensing_eff[h] = eff return Rs_angle, alpha_Rs, lensing_eff
def __init__(self, z_lens, z_source, cosmo=None, static=False): """ :param z_lens: redshift of lens :param z_source: redshift of source :param cosmo: astropy cosmology instance :param static: boolean, if True, only operates with fixed parameter values """ self._nfw = NFW() if cosmo is None: from astropy.cosmology import FlatLambdaCDM cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Ob0=0.05) self._lens_cosmo = LensCosmo(z_lens, z_source, cosmo=cosmo) self._static = static super(NFWMC, self).__init__()
def __init__(self, seed, fixh0=None, fixz=None, rung3=False): if isinstance(fixh0, int): self.seed = fixh0 np.random.seed(self.seed) self.H0 = np.random.uniform(50, 90) elif fixh0 == None: self.seed = seed np.random.seed(self.seed) self.H0 = np.random.uniform(50, 90) else: raise ValueError("the fixh0 is not give correctly" % fixh0) self.seed = seed + 1000 self.cosmo = FlatLambdaCDM(H0=self.H0, Om0=0.27) if not isinstance(fixz, np.ndarray): np.random.seed(self.seed) self.z_lens = np.random.normal(0.5, 0.2) self.z_source = np.random.normal(2.0, 0.4) elif isinstance(fixz, np.ndarray) and len(fixz) == 2: self.z_lens = fixz[0] self.z_source = fixz[1] else: raise ValueError("the fixz is not give correctly" % fixz) lensunits = LensCosmo(z_lens=self.z_lens, z_source=self.z_source, cosmo=self.cosmo) self.D_l = lensunits.dd self.D_s = lensunits.ds self.D_ls = lensunits.dds
def setup(self): np.random.seed(seed=41) self.z_L = 0.8 self.z_S = 3.0 self.H0_true = 70 self.omega_m_true = 0.3 self.cosmo = FlatLambdaCDM(H0=self.H0_true, Om0=self.omega_m_true, Ob0=0.05) lensCosmo = LensCosmo(self.z_L, self.z_S, cosmo=self.cosmo) self.Dd_true = lensCosmo.dd self.D_dt_true = lensCosmo.ddt self.sigma_Dd = 100 self.sigma_Ddt = 500 num_samples = 10000 self.D_dt_samples = np.random.normal(self.D_dt_true, self.sigma_Ddt, num_samples) self.D_d_samples = np.random.normal(self.Dd_true, self.sigma_Dd, num_samples) self.kwargs_lens = { 'z_lens': self.z_L, 'z_source': self.z_S, 'dd_samples': self.D_d_samples, 'ddt_samples': self.D_dt_samples, 'kde_type': 'scipy_gaussian', 'bandwidth': 1 }
def test_raise(self): self.sigma_Dd = 100 self.sigma_Ddt = 100 num_samples = 100 cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Ob0=0.05) z_L = 0.3 z_S = 2 lensCosmo = LensCosmo(z_L, z_S, cosmo=cosmo) self.Dd_true = lensCosmo.D_d self.D_dt_true = lensCosmo.D_dt D_dt_samples = np.random.normal(self.D_dt_true, self.sigma_Ddt, num_samples) D_d_samples = np.random.normal(self.Dd_true, self.sigma_Dd, num_samples) self.cosmoL = CosmoLikelihood(z_L, z_S, D_d_samples, D_dt_samples, sampling_option="H0_only", omega_m_fixed=0.3, omega_lambda_fixed=0.7, omega_mh2_fixed=0.14157, kde_type='scipy_gaussian', bandwidth=1, flat=True) self.cosmoL.sampling_option = 'WRONG' with self.assertRaises(ValueError): self.cosmoL(a=[]) with self.assertRaises(ValueError): self.cosmoL.likelihood(a=[]) with self.assertRaises(ValueError): self.cosmoL.computeLikelihood(ctx=[]) with self.assertRaises(ValueError): param = CosmoParam(sampling_option='WRONG') param.numParam with self.assertRaises(ValueError): param = CosmoParam(sampling_option='WRONG') param.param_bounds
def setup(self): np.random.seed(seed=41) self.z_L = 0.8 self.z_S = 3.0 self.H0_true = 70 self.omega_m_true = 0.3 self.cosmo = FlatLambdaCDM(H0=self.H0_true, Om0=self.omega_m_true, Ob0=0.0) lensCosmo = LensCosmo(self.z_L, self.z_S, cosmo=self.cosmo) self.Dd_true = lensCosmo.dd self.D_dt_true = lensCosmo.ddt self.sigma_Dd = 0.9 * self.Dd_true self.sigma_Ddt = 0.01 * self.D_dt_true num_samples = 20000 self.ddt_samples = np.random.normal(self.D_dt_true, self.sigma_Ddt, num_samples) self.dd_samples = np.random.normal(self.Dd_true, self.sigma_Dd, num_samples) self.kwargs_likelihood_list = [{'z_lens': self.z_L, 'z_source': self.z_S, 'likelihood_type': 'DdtDdGaussian', 'ddt_mean': self.D_dt_true, 'ddt_sigma': self.sigma_Ddt, 'dd_mean': self.Dd_true, 'dd_sigma': self.sigma_Dd}] kwargs_lower_lens = {'gamma_ppn': 0, 'lambda_mst': 0} kwargs_upper_lens = {'gamma_ppn': 2, 'lambda_mst': 2} kwargs_fixed_lens = {} kwargs_lower_cosmo = {'h0': 10, 'om': 0., 'ok': -0.8, 'w': -2, 'wa': -1, 'w0': -2} kwargs_upper_cosmo = {'h0': 200, 'om': 1, 'ok': 0.8, 'w': 0, 'wa': 1, 'w0': 1} self.cosmology = 'oLCDM' self.kwargs_bounds = {'kwargs_lower_lens': kwargs_lower_lens, 'kwargs_upper_lens': kwargs_upper_lens, 'kwargs_fixed_lens': kwargs_fixed_lens, 'kwargs_lower_cosmo': kwargs_lower_cosmo, 'kwargs_upper_cosmo': kwargs_upper_cosmo}
def setup(self): np.random.seed(seed=41) self.z_L = 0.8 self.z_S = 3.0 self.H0_true = 70 self.omega_m_true = 0.3 self.cosmo = FlatLambdaCDM(H0=self.H0_true, Om0=self.omega_m_true, Ob0=0.05) lensCosmo = LensCosmo(self.z_L, self.z_S, cosmo=self.cosmo) self.Dd_true = lensCosmo.dd self.D_dt_true = lensCosmo.ddt self.sigma_Dd = 100 self.sigma_Ddt = 100 num_samples = 10000 self.D_dt_samples = np.random.normal(self.D_dt_true, self.sigma_Ddt, num_samples) self.D_d_samples = np.random.normal(self.Dd_true, self.sigma_Dd, num_samples) ani_param_array = np.linspace(0, 2, 10) ani_scaling_array = np.ones_like(ani_param_array) self.kwargs_lens_list = [{'z_lens': self.z_L, 'z_source': self.z_S, 'likelihood_type': 'DdtDdKDE', 'dd_samples': self.D_d_samples, 'ddt_samples': self.D_dt_samples, 'kde_type': 'scipy_gaussian', 'bandwidth': 1}, {'z_lens': self.z_L, 'z_source': self.z_S, 'likelihood_type': 'DsDdsGaussian', 'ds_dds_mean': lensCosmo.ds/lensCosmo.dds, 'ds_dds_sigma': 1, 'ani_param_array': ani_param_array, 'ani_scaling_array': ani_scaling_array}] self.likelihood = LensSampleLikelihood(kwargs_lens_list=self.kwargs_lens_list)
def setup(self): self.sigma_Dd = 100 self.sigma_Ddt = 100 num_samples = 100 cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Ob0=0.05) z_L = 0.3 z_S = 2 lensCosmo = LensCosmo(z_L, z_S, cosmo=cosmo) self.Dd_true = lensCosmo.D_d self.D_dt_true = lensCosmo.D_dt D_dt_samples = np.random.normal(self.D_dt_true, self.sigma_Ddt, num_samples) D_d_samples = np.random.normal(self.Dd_true, self.sigma_Dd, num_samples) self.cosmoL = CosmoLikelihood(z_L, z_S, D_d_samples, D_dt_samples, sampling_option="H0_only", omega_m_fixed=0.3, omega_lambda_fixed=0.7, omega_mh2_fixed=0.14157, kde_type='scipy_gaussian', bandwidth=1, flat=True)
def interpol_instance(self, z_source, cosmo): """ scales the mass map integrals (with units of mass not convergence) into a convergence map for the given cosmology and source redshift and returns the keyword arguments of the interpolated reduced deflection and lensing potential. :param z_source: redshift of the source :param cosmo: astropy.cosmology instance :return: keyword arguments of the interpolation instance with numerically computed deflection angles and lensing potential """ lens_cosmo = LensCosmo(z_lens=self._redshift, z_source=z_source, cosmo=cosmo) mpc2arcsec = lens_cosmo.dd * const.arcsec x_axes = self._x_axes_mpc / mpc2arcsec # units of arc seconds in grid spacing y_axes = self._y_axes_mpc / mpc2arcsec # units of arc seconds in grid spacing f_ = self._f_mass / lens_cosmo.sigma_crit_angle / self._grid_spacing**2 f_x = self._f_x_mass / lens_cosmo.sigma_crit_angle / self._grid_spacing**2 * mpc2arcsec f_y = self._f_y_mass / lens_cosmo.sigma_crit_angle / self._grid_spacing**2 * mpc2arcsec kwargs_interp = { 'grid_interp_x': x_axes, 'grid_interp_y': y_axes, 'f_': f_, 'f_x': f_x, 'f_y': f_y } return kwargs_interp
def cal_Ddt(zl, zs, H0_ini=100, om=0.27): cosmo = FlatLambdaCDM(H0=H0_ini, Om0=0.27) lensunits = LensCosmo(z_lens=zl, z_source=zs, cosmo=cosmo) D_l = lensunits.dd D_s = lensunits.ds D_ls = lensunits.dds Ddt_corr = (1 + zl) * D_l * D_s / D_ls return Ddt_corr
def __init__(self, lens_model_list, z_lens=None, z_source=None, lens_redshift_list=None, cosmo=None, multi_plane=False, numerical_alpha_class=None, observed_convention_index=None, z_source_convention=None): """ :param lens_model_list: list of strings with lens model names :param z_lens: redshift of the deflector (only considered when operating in single plane mode). Is only needed for specific functions that require a cosmology. :param z_source: redshift of the source: Needed in multi_plane option only, not required for the core functionalities in the single plane mode. :param lens_redshift_list: list of deflector redshift (corresponding to the lens model list), only applicable in multi_plane mode. :param cosmo: instance of the astropy cosmology class. If not specified, uses the default cosmology. :param multi_plane: bool, if True, uses multi-plane mode. Default is False. :param numerical_alpha_class: an instance of a custom class for use in NumericalAlpha() lens model (see documentation in Profiles/numerical_alpha) :param observed_convention_index: a list of lens indexes that correspond to observed positions on the sky, not physical positions :param z_source_convention: float, redshift of a source to define the reduced deflection angles of the lens models. If None, 'z_source' is used. """ self.lens_model_list = lens_model_list self.z_lens = z_lens if z_source_convention is None: z_source_convention = z_source self.z_source = z_source self._z_source_convention = z_source_convention self.redshift_list = lens_redshift_list if cosmo is None: cosmo = default_cosmology.get() self.cosmo = cosmo self.multi_plane = multi_plane if multi_plane is True: if z_source is None: raise ValueError('z_source needs to be set for multi-plane lens modelling.') self.lens_model = MultiPlane(z_source, lens_model_list, lens_redshift_list, cosmo=cosmo, numerical_alpha_class=numerical_alpha_class, observed_convention_index=observed_convention_index, z_source_convention=z_source_convention) else: self.lens_model = SinglePlane(lens_model_list, numerical_alpha_class=numerical_alpha_class) if z_lens is not None and z_source is not None: self._lensCosmo = LensCosmo(z_lens, z_source, cosmo=cosmo)
def setup(self): z_lens = 0.55 z_source = 2.5 from astropy.cosmology import FlatLambdaCDM cosmo = FlatLambdaCDM(H0=70, Om0=0.3, Ob0=0.05) self.nfw = NFWVirTrunc(z_lens=z_lens, z_source=z_source, cosmo=cosmo) self.lensCosmo = LensCosmo(z_lens=z_lens, z_source=z_source, cosmo=cosmo) NFWVirTrunc(z_lens=z_lens, z_source=z_source, cosmo=None)
def __init__(self, z_lens, z_source, kwargs_model, cosmo=None): """ :param z_lens: redshift of lens :param z_source: redshift of source :param kwargs_model: model keyword arguments :param cosmo: astropy.cosmology instance """ self.z_d = z_lens self.z_s = z_source self.lensCosmo = LensCosmo(z_lens, z_source, cosmo=cosmo) self.lens_analysis = LensAnalysis(kwargs_model) self._lensModelExt = LensModelExtensions(self.lens_analysis.LensModel) self.kwargs_options = kwargs_model self._kwargs_cosmo = { 'D_d': self.lensCosmo.D_d, 'D_s': self.lensCosmo.D_s, 'D_ds': self.lensCosmo.D_ds }
def test_ddt2h0(self): z_lens, z_source = 0.5, 2 omega_m = 0.3 h0_true = 73 cosmo = FlatLambdaCDM(H0=h0_true, Om0=omega_m, Ob0=0.) lensCosmo = LensCosmo(z_lens=z_lens, z_source=z_source, cosmo=cosmo) ddt_true = lensCosmo.ddt cosmo_fiducial = FlatLambdaCDM(H0=60, Om0=omega_m, Ob0=0.) h0_inferred = ddt2h0(ddt_true, z_lens, z_source, cosmo_fiducial) npt.assert_almost_equal(h0_inferred, h0_true, decimal=4)
def get_lens_params(lens_info, z_src, cosmo): """Get SIE lens parameters into a form Lenstronomy understands Parameters ---------- lens_info : dict SIE lens and external shear parameters for a system, where `ellip_lens` is 1 minus the axis ratio z_src : float source redshift, required for Einstein radius approximation cosmo : astropy.cosmology object cosmology to use to get distances """ lens_phie_rad = np.pi * (lens_info['phie_lens'] / 180.0) + 0.5 * np.pi # in rad, origin at y-axis lens_e1, lens_e2 = param_util.phi_q2_ellipticity( lens_phie_rad, 1 - lens_info['ellip_lens']) # Instantiate cosmology-aware models lens_cosmo = LensCosmo(z_lens=lens_info['redshift'], z_source=z_src, cosmo=cosmo) theta_E = lens_cosmo.sis_sigma_v2theta_E(lens_info['vel_disp_lenscat']) lam = get_lambda_factor( lens_info['ellip_lens'] ) # removed because lenstronomy accepts the spherically-averaged Einstein radius as input phi, q = param_util.ellipticity2phi_q(lens_e1, lens_e2) gravlens_to_lenstronomy = np.sqrt( (1.0 + q**2.0) / (2.0 * q) ) # factor converting the grav lens ellipticity convention (square average) to lenstronomy's (product average) sie_mass = dict( center_x=0.0, center_y=0.0, #s_scale=0.0, theta_E=theta_E * gravlens_to_lenstronomy, e1=lens_e1, e2=lens_e2) external_shear = dict(gamma_ext=lens_info['gamma_lenscat'], psi_ext=np.deg2rad(lens_info['phig_lenscat'])) #external_shear = dict( # gamma1=lens_info['shear_1_lenscat'], # gamma2=lens_info['shear_2_lenscat'] # ) return [sie_mass, external_shear]
def test_physical2lensing_conversion(self): lens_redshift_list = [0.5, 1] z_source_convention = 2 api = ModelAPI(lens_model_list=['SIS', 'NFW'], lens_redshift_list=lens_redshift_list, z_source_convention=z_source_convention, cosmo=None, z_source=z_source_convention) kwargs_mass = [{ 'sigma_v': 200, 'center_x': 0, 'center_y': 0 }, { 'M200': 10**13, 'concentration': 5, 'center_x': 1, 'center_y': 1 }] kwargs_lens = api.physical2lensing_conversion(kwargs_mass) theta_E = kwargs_lens[0]['theta_E'] lens_cosmo = LensCosmo(z_lens=lens_redshift_list[0], z_source=z_source_convention) theta_E_test = lens_cosmo.sis_sigma_v2theta_E( kwargs_mass[0]['sigma_v']) npt.assert_almost_equal(theta_E, theta_E_test, decimal=7) alpha_Rs = kwargs_lens[1]['alpha_Rs'] lens_cosmo = LensCosmo(z_lens=lens_redshift_list[1], z_source=z_source_convention) Rs_new, alpha_Rs_new = lens_cosmo.nfw_physical2angle( kwargs_mass[1]['M200'], kwargs_mass[1]['concentration']) npt.assert_almost_equal(alpha_Rs, alpha_Rs_new, decimal=7) api = ModelAPI(lens_model_list=['SIS', 'NFW'], z_lens=0.5, z_source_convention=z_source_convention, cosmo=None, z_source=z_source_convention) kwargs_lens = api.physical2lensing_conversion(kwargs_mass) theta_E = kwargs_lens[0]['theta_E'] lens_cosmo = LensCosmo(z_lens=0.5, z_source=z_source_convention) theta_E_test = lens_cosmo.sis_sigma_v2theta_E( kwargs_mass[0]['sigma_v']) npt.assert_almost_equal(theta_E, theta_E_test, decimal=7)
def __init__(self, lens_model_list, z_lens=None, z_source=None, lens_redshift_list=None, cosmo=None, multi_plane=False, numerical_alpha_class=None): """ :param lens_model_list: list of strings with lens model names :param z_lens: redshift of the deflector (only considered when operating in single plane mode). Is only needed for specific functions that require a cosmology. :param z_source: redshift of the source: Needed in multi_plane option only, not required for the core functionalities in the single plane mode. :param lens_redshift_list: list of deflector redshift (corresponding to the lens model list), only applicable in multi_plane mode. :param cosmo: instance of the astropy cosmology class. If not specified, uses the default cosmology. :param multi_plane: bool, if True, uses multi-plane mode. Default is False. :param numerical_alpha_class: an instance of a custom class for use in NumericalAlpha() lens model (see documentation in Profiles/numerical_alpha) """ self.lens_model_list = lens_model_list self.z_lens = z_lens self.z_source = z_source self.redshift_list = lens_redshift_list self.cosmo = cosmo self.multi_plane = multi_plane if multi_plane is True: self.lens_model = MultiPlane( z_source, lens_model_list, lens_redshift_list, cosmo=cosmo, numerical_alpha_class=numerical_alpha_class) else: self.lens_model = SinglePlane( lens_model_list, numerical_alpha_class=numerical_alpha_class) if z_lens is not None and z_source is not None: self._lensCosmo = LensCosmo(z_lens, z_source, cosmo=self.cosmo)
def cosmo2Dd_Ds_Dds(self, H_0, omega_m): """ :param H_0: Hubble constant :param omega_m: matter density :return: angular diameter distances Dd and Ds/Dds """ cosmo = FlatLambdaCDM(H0=H_0, Om0=omega_m, Ob0=0.) lensCosmo = LensCosmo(z_lens=self.z_d, z_source=self.z_s, cosmo=cosmo) Dd = lensCosmo.D_d Ds = lensCosmo.D_s Dds = lensCosmo.D_ds return Dd, Ds / Dds
def __init__(self, zl, zs, xdeflection=None, ydeflection=None, gamma1=None, gamma2=None, kappa=None, hdul=None): """ input the initial lens model. :param zl: redshift of the lens :param zs: redshift of the source :param xdeflection: the x deflection map :param ydeflection: the y deflection map :param gamma1: gamma1 map of shear :param gamma2: gamma2 map of shear :param kappa: convergence map """ cosmo = LensCosmo(zl, zs) dlsds = cosmo.dds / cosmo.ds self.dlsds = dlsds self.wcs = wcs.WCS(hdul[0].header) #alphax if xdeflection is None: self.alphax = None else: self.alphax = xdeflection * dlsds #alphay if ydeflection is None: self.alphay = None else: self.alphay = ydeflection * dlsds #gamma1 if gamma1 is None: self.gamma1 = None else: self.gamma1 = gamma1 * dlsds #gamma2 if gamma2 is None: self.gamma2 = None else: self.gamma2 = gamma2 * dlsds #kappa if kappa is None: self.kappa = None else: self.kappa = kappa * dlsds
def cosmo2angular_diameter_distances(H_0, omega_m, z_lens, z_source): """ :param H_0: Hubble constant [km/s/Mpc] :param omega_m: dimensionless matter density at z=0 :param z_lens: deflector redshift :param z_source: source redshift :return: angular diameter distances Dd and Ds/Dds """ cosmo = FlatLambdaCDM(H0=H_0, Om0=omega_m, Ob0=0.) lensCosmo = LensCosmo(z_lens=z_lens, z_source=z_source, cosmo=cosmo) Dd = lensCosmo.dd Ds = lensCosmo.ds Dds = lensCosmo.dds return Dd, Ds / Dds
def ddt2h0(ddt, z_lens, z_source, cosmo): """ converts time-delay distance to H0 for a given expansion history :param ddt: time-delay distance in Mpc :param z_lens: deflector redshift :param z_source: source redshift :param cosmo: astropy.cosmology class instance :return: h0 value which matches the cosmology class effectively replacing the h0 value used in the creation of this class """ h0_fiducial = cosmo.H0.value lens_cosmo = LensCosmo(z_lens=z_lens, z_source=z_source, cosmo=cosmo) ddt_fiducial = lens_cosmo.ddt h0 = h0_fiducial * ddt_fiducial / ddt return h0
def _get_cosom(self, H_0, Om0, Ode0=None): """ :param H_0: :param Om0: :param Ode0: :return: """ if self._flat is True: cosmo = FlatLambdaCDM(H0=H_0, Om0=Om0) else: cosmo = LambdaCDM(H0=H_0, Om0=Om0, Ode0=Ode0) lensCosmo = LensCosmo(z_lens=self.z_lens, z_source=self.z_source, cosmo=cosmo) return lensCosmo