From d3cfe8fc4f61921f9a83ea00b8a8d19684d61f2d Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Thu, 21 Nov 2024 22:08:40 -0500 Subject: [PATCH 01/13] updated velocity dispersion calculation for EPL models --- slsim/Deflectors/DeflectorTypes/epl_sersic.py | 31 +++++- slsim/Deflectors/velocity_dispersion.py | 96 ++++++++++++++++++- slsim/lens.py | 43 +++++---- .../test_velocity_dispersion.py | 41 ++++++++ tests/test_lens.py | 4 +- 5 files changed, 189 insertions(+), 26 deletions(-) diff --git a/slsim/Deflectors/DeflectorTypes/epl_sersic.py b/slsim/Deflectors/DeflectorTypes/epl_sersic.py index c95d42133..a71450c61 100644 --- a/slsim/Deflectors/DeflectorTypes/epl_sersic.py +++ b/slsim/Deflectors/DeflectorTypes/epl_sersic.py @@ -1,5 +1,6 @@ from slsim.Deflectors.DeflectorTypes.deflector_base import DeflectorBase from slsim.Util.param_util import ellipticity_slsim_to_lenstronomy +from slsim.Deflectors.velocity_dispersion import theta_E_from_vel_disp_epl class EPLSersic(DeflectorBase): @@ -15,6 +16,17 @@ class EPLSersic(DeflectorBase): - 'e2_light': eccentricity of light - 'z': redshift of deflector """ + def __init__(self, deflector_dict): + """ + + :param deflector_dict: dictionary of deflector quantities + """ + super().__init__(deflector_dict=deflector_dict) + try: + sis_convention = deflector_dict["sis_convention"] + except KeyError: + sis_convention = True + self._sis_convention = sis_convention def velocity_dispersion(self, cosmo=None): """Velocity dispersion of deflector. @@ -44,13 +56,18 @@ def mass_model_lenstronomy(self, lens_cosmo): :type lens_cosmo: ~lenstronomy.Cosmo.LensCosmo instance :return: lens_mass_model_list, kwargs_lens_mass """ + gamma = self.halo_properties if lens_cosmo.z_lens >= lens_cosmo.z_source: theta_E = 0.0 else: - theta_E = lens_cosmo.sis_sigma_v2theta_E( - float(self.velocity_dispersion(cosmo=lens_cosmo.background.cosmo)) - ) - gamma = self.halo_properties + lens_light_model_list, kwargs_lens_light = self.light_model_lenstronomy() + theta_E = theta_E_from_vel_disp_epl(vel_disp=float(self.velocity_dispersion(cosmo=lens_cosmo.background.cosmo)), + gamma=gamma, + r_half=self.angular_size_light, + kwargs_light=kwargs_lens_light, light_model_list=lens_light_model_list, + lens_cosmo=lens_cosmo, + kappa_ext=0, sis_convention=self._sis_convention) + e1_mass, e2_mass = self.mass_ellipticity e1_mass_lenstronomy, e2_mass_lenstronomy = ellipticity_slsim_to_lenstronomy( e1_slsim=e1_mass, e2_slsim=e2_mass @@ -111,7 +128,11 @@ def halo_properties(self): :return: gamma (with =2 is isothermal) """ - + #if hasattr(self._deflector_dict, "gamma_pl"): + # return float(self._deflector_dict["gamma_pl"]) + #else: + # # TODO: this can (optionally) be made a function of stellar mass, velocity dispersion etc + # return 2 try: return float(self._deflector_dict["gamma_pl"]) except KeyError: diff --git a/slsim/Deflectors/velocity_dispersion.py b/slsim/Deflectors/velocity_dispersion.py index b53592ea8..5fe9973e2 100644 --- a/slsim/Deflectors/velocity_dispersion.py +++ b/slsim/Deflectors/velocity_dispersion.py @@ -3,6 +3,10 @@ from scipy import interpolate import copy +from lenstronomy.Cosmo.lens_cosmo import LensCosmo +from lenstronomy.Analysis.light_profile import LightProfileAnalysis +from lenstronomy.LightModel.light_model import LightModel + """ This module provides functions to compute velocity dispersion using schechter function. """ @@ -33,8 +37,6 @@ def vel_disp_composite_model(r, m_star, rs_star, m_halo, c_halo, cosmo, z_lens): } # turn physical masses to lenstronomy units - from lenstronomy.Cosmo.lens_cosmo import LensCosmo - lens_cosmo = LensCosmo(z_lens=z_lens, z_source=10, cosmo=cosmo) # Hernquist profile sigma0, rs_angle_hernquist = lens_cosmo.hernquist_phys2angular( @@ -69,6 +71,93 @@ def vel_disp_composite_model(r, m_star, rs_star, m_halo, c_halo, cosmo, z_lens): return vel_disp +def vel_disp_power_law(theta_E, gamma, r_half, kwargs_light, light_model_list, lens_cosmo): + """ + velocity dispersion for a power-law mass density profile + + :param theta_E: Einstein radius [arc seconds] + :param gamma: power-law slope of deflector + :param r_half: half light radius of deflector + :param kwargs_light: list of dict for light model parameters + :param light_model_list: list of light models + :param lens_cosmo: ~LensCosmo instance + :return: half light radius averaged velocity dispersion + """ + # turn physical masses to lenstronomy units + + kwargs_mass = [ + {"theta_E": theta_E, "gamma": gamma, "e1": 0, "e2": 0, "center_x": 0, "center_y": 0}, + ] + kwargs_anisotropy = {"beta": 0} + light_model = LightModel(light_model_list=light_model_list) + lensLightProfile = LightProfileAnalysis(light_model=light_model) + + ( + amps, + sigmas, + center_x, + center_y, + ) = lensLightProfile.multi_gaussian_decomposition( + kwargs_light, + r_h=r_half, + n_comp=20, + ) + light_profile_list = ["MULTI_GAUSSIAN"] + kwargs_model = { + "mass_profile_list": ["EPL"], + "light_profile_list": light_profile_list, + "anisotropy_model": "const", + } + + kwargs_light_mge = [{"amp": amps, "sigma": sigmas}] + + from lenstronomy.GalKin.numeric_kinematics import NumericKinematics + + kwargs_numerics = { + "interpol_grid_num": 1000, + "log_integration": True, + "max_integrate": 1000, + "min_integrate": 0.0001, + "max_light_draw": None, + "lum_weight_int_method": True, + } + + kwargs_cosmo = {"d_d": lens_cosmo.dd, "d_s": lens_cosmo.ds, "d_ds": lens_cosmo.dds} + + num_kin = NumericKinematics(kwargs_model, kwargs_cosmo, **kwargs_numerics) + vel_disp = num_kin.lum_weighted_vel_disp( + r_half, kwargs_mass, kwargs_light_mge, kwargs_anisotropy + ) + return vel_disp + + +def theta_E_from_vel_disp_epl(vel_disp, gamma, r_half, kwargs_light, light_model_list, lens_cosmo, + kappa_ext=0, sis_convention=True): + """ + calculates Einstein radius given measured aperture averaged velocity dispersion and given power-law slope + + :param vel_disp: velocity dispersion measured within an aperture radius [km/s] + :param gamma: power-law slope + :param r_half: half light radius (aperture radius) [arc seconds] + :param kwargs_light: list of dict for light model parameters + :param light_model_list: list of light models + :param lens_cosmo: ~LensCosmo instance + :param kappa_ext: external convergence + :param sis_convention: it True, uses velocity dispersion not as measured one but as the SIS equivalent velocity + dispersion + :return: Einstein radius matching the velocity dispersion and external convergence + """ + if gamma == 2 or sis_convention is True: + theta_E = lens_cosmo.sis_sigma_v2theta_E(vel_disp) + else: + theta_E_0 = 1 + vel_disp_0 = vel_disp_power_law(theta_E_0, gamma, r_half, kwargs_light, light_model_list, lens_cosmo) + # transform theta_E from vel_disp_0 prediction + theta_E = theta_E_0 * (vel_disp / vel_disp_0) ** (2 / (gamma - 1)) + theta_E /= (1 - kappa_ext) ** (1.0 / (gamma - 1)) + return theta_E + + def vel_disp_nfw_3d(r, m_halo, c_halo, cosmo, z_lens): """Computes the unweighted velocity dispersion at 3D radius r for a deflector with a NFW halo profile, assuming isotropic anisotropy (beta = 0). @@ -185,7 +274,6 @@ def vel_disp_nfw(m_halo, c_halo, cosmo, z_lens): """Computes vel_disp_nfw_aperture using the characteristic radius rs of the NFW as aperture (which is independent of the source redshift). - :param r: radius of the aperture for the velocity dispersion [arcsec] :param m_halo: Halo mass [physical M_sun] :param c_halo: halo concentration :param cosmo: cosmology @@ -397,7 +485,7 @@ def schechter_vel_disp_redshift( bounds. sky_area : `~astropy.units.Quantity` Sky area over which galaxies are sampled. Must be in units of solid angle. - cosmology : `~astropy.cosmology` + cosmo : `~astropy.cosmology` `astropy.cosmology` object to calculate comoving densities. noise : bool, optional Poisson-sample the number of galaxies. Default is `True`. diff --git a/slsim/lens.py b/slsim/lens.py index 5740eaec4..b2ede778a 100644 --- a/slsim/lens.py +++ b/slsim/lens.py @@ -13,6 +13,7 @@ from lenstronomy.Util import util from slsim.lensed_system_base import LensedSystemBase +from slsim.Deflectors.velocity_dispersion import theta_E_from_vel_disp_epl class Lens(LensedSystemBase): @@ -399,25 +400,31 @@ def _einstein_radius(self, source): """ if self.deflector.redshift >= source.redshift: theta_E = 0 - elif self.deflector.deflector_type in ["EPL"]: - _lens_cosmo = LensCosmo( - z_lens=float(self.deflector.redshift), - z_source=float(source.redshift), - cosmo=self.cosmo, - ) - kappa_ext = self.los_class.convergence + return theta_E + lens_model_list, kwargs_lens = self.deflector_mass_model_lenstronomy() + lens_model = LensModel(lens_model_list=lens_model_list, + z_lens=self.deflector_redshift, + z_source_convention=self.max_redshift_source_class.redshift, + multi_plane=False, + z_source=source.redshift) + if self.deflector.deflector_type in ["EPL"]: + kappa_ext_convention = self.los_class.convergence gamma_pl = self.deflector.halo_properties - theta_E = _lens_cosmo.sis_sigma_v2theta_E( - float(self.deflector.velocity_dispersion(cosmo=self.cosmo)) - ) / (1 - kappa_ext) ** (1.0 / (gamma_pl - 1)) + theta_E_convention = kwargs_lens[0]["theta_E"] + if source.redshift == self.max_redshift_source_class.redshift: + theta_E = theta_E_convention + kappa_ext = kappa_ext_convention + else: + # TODO: translate Einstein radius to different source redshift with power-law slope difference + beta = self._lens_cosmo.beta_double_source_plane(z_lens=self.deflector_redshift, + z_source_1=self.max_redshift_source_class.redshift, + z_source_2=source.redshift) + theta_E = theta_E_convention * beta ** (1.0 / (gamma_pl - 1)) + kappa_ext = kappa_ext_convention * beta + + theta_E /= (1 - kappa_ext) ** (1.0 / (gamma_pl - 1)) else: # numerical solution for the Einstein radius - lens_model_list, kwargs_lens = self.deflector_mass_model_lenstronomy() - lens_model = LensModel(lens_model_list=lens_model_list, - z_lens=self.deflector_redshift, - z_source_convention=self.max_redshift_source_class.redshift, - multi_plane=False, - z_source=source.redshift) lens_analysis = LensProfileAnalysis(lens_model=lens_model) theta_E = lens_analysis.effective_einstein_radius( kwargs_lens, r_min=1e-3, r_max=5e1, num_points=50 @@ -781,6 +788,8 @@ def deflector_mass_model_lenstronomy(self): :return: lens_model_list, kwargs_lens """ + if hasattr(self, "_lens_mass_model_list") and hasattr(self, "_kwargs_lens"): + return self._lens_mass_model_list, self._kwargs_lens if self.deflector.deflector_type in ["EPL", "NFW_HERNQUIST", "NFW_CLUSTER"]: lens_mass_model_list, kwargs_lens = self.deflector.mass_model_lenstronomy( lens_cosmo=self._lens_cosmo @@ -806,6 +815,8 @@ def deflector_mass_model_lenstronomy(self): kwargs_lens.append({"kappa": kappa_ext, "ra_0": 0, "dec_0": 0}) lens_mass_model_list.append("SHEAR") lens_mass_model_list.append("CONVERGENCE") + self._kwargs_lens = kwargs_lens + self._lens_mass_model_list = lens_mass_model_list return lens_mass_model_list, kwargs_lens diff --git a/tests/test_Deflectors/test_velocity_dispersion.py b/tests/test_Deflectors/test_velocity_dispersion.py index c6a5cf638..e895179a8 100644 --- a/tests/test_Deflectors/test_velocity_dispersion.py +++ b/tests/test_Deflectors/test_velocity_dispersion.py @@ -4,7 +4,10 @@ vel_disp_composite_model, vel_disp_nfw_aperture, redshifts_from_comoving_density, + vel_disp_power_law, + theta_E_from_vel_disp_epl ) +from lenstronomy.Cosmo.lens_cosmo import LensCosmo import numpy as np import numpy.testing as npt from astropy.cosmology import FlatLambdaCDM @@ -43,6 +46,44 @@ def test_vel_disp_nfw_aperture(): npt.assert_almost_equal(vel_disp, 1000, decimal=-1) +def test_vel_disp_power_law(): + cosmo = FlatLambdaCDM(H0=70, Om0=0.3) + z_lens = 0.5 + z_source = 1.5 + theta_E = 1.5 + gamma = 2 + r_half = 1 + kwargs_light = [{"amp": 1, "R_sersic": 2, "n_sersic": 1, "e1": 0, "e2": 0, "center_x": 0, "center_y": 0}] + light_model_list = ["SERSIC_ELLIPSE"] + + # kwargs_light = [{"amp": 1, "gamma": 2, "e1": 0, "e2": 0, "center_x": 0, "center_y": 0}] + # light_model_list = ["POWER_LAW"] + lens_cosmo = LensCosmo(z_lens=z_lens, z_source=z_source, cosmo=cosmo) + + vel_disp_pl = vel_disp_power_law(theta_E, gamma, r_half, kwargs_light, light_model_list, lens_cosmo) + + vel_disp_sis = lens_cosmo.sis_theta_E2sigma_v(theta_E) + npt.assert_almost_equal(vel_disp_pl / vel_disp_sis, 1, decimal=1) + + +def test_theta_E_from_vel_disp_epl(): + cosmo = FlatLambdaCDM(H0=70, Om0=0.3) + z_lens = 0.5 + z_source = 1.5 + lens_cosmo = LensCosmo(z_lens=z_lens, z_source=z_source, cosmo=cosmo) + theta_E = 1.5 + gamma = 2.5 + r_half = 1 + kwargs_light = [{"amp": 1, "R_sersic": 2, "n_sersic": 1, "e1": 0, "e2": 0, "center_x": 0, "center_y": 0}] + light_model_list = ["SERSIC_ELLIPSE"] + + vel_disp_pl = vel_disp_power_law(theta_E, gamma, r_half, kwargs_light, light_model_list, lens_cosmo) + + theta_E_out = theta_E_from_vel_disp_epl(vel_disp_pl, gamma, r_half, kwargs_light, + light_model_list, lens_cosmo, kappa_ext=0, sis_convention=False) + npt.assert_almost_equal(theta_E_out/theta_E, 1, decimal=3) + + def test_schechter_vdf(): """SDSS velocity dispersion function for galaxies brighter than Mr >= -16.8.""" diff --git a/tests/test_lens.py b/tests/test_lens.py index c704c1929..60a9343c8 100644 --- a/tests/test_lens.py +++ b/tests/test_lens.py @@ -583,6 +583,8 @@ def setup_method(self): deflector_type="EPL", deflector_dict=deflector_dict, ) + + def test_multi_source(self): lens_class1 = Lens( deflector_class=self.deflector, source_class=self.source1, @@ -634,8 +636,8 @@ def setup_method(self): #Test multisource image observation time assert image_observation_time1[0] == image_observation_time3[0] assert image_observation_time2[0] == image_observation_time3[1] - assert lens_class1.einstein_radius_deflector[0] == lens_class3.einstein_radius_deflector[0] assert lens_class1.einstein_radius[0] == lens_class3.einstein_radius[0] + assert lens_class2.einstein_radius[0] == lens_class3.einstein_radius[1] assert len(lens_class3.image_observer_times(t_obs=10)) == 2 From 7c77f6a96125b9d6f901010e9ec86e4bb30e55ea Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Fri, 22 Nov 2024 08:06:13 -0500 Subject: [PATCH 02/13] better ellipticity definitions --- slsim/lens.py | 2 +- tests/test_lens.py | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/slsim/lens.py b/slsim/lens.py index 6c43a4839..abeb03d47 100644 --- a/slsim/lens.py +++ b/slsim/lens.py @@ -834,7 +834,7 @@ def deflector_mass_model_lenstronomy(self): % self.deflector.deflector_type ) # adding line-of-sight structure - gamma1, gamma2, kappa_ext = self.los_linear_distortions + kappa_ext, gamma1, gamma2 = self.los_linear_distortions gamma1_lenstronomy, gamma2_lenstronomy = ellipticity_slsim_to_lenstronomy( e1_slsim=gamma1, e2_slsim=gamma2 ) diff --git a/tests/test_lens.py b/tests/test_lens.py index 5da2288cc..b67e9b130 100644 --- a/tests/test_lens.py +++ b/tests/test_lens.py @@ -1,3 +1,5 @@ +import copy + import pytest import numpy as np from numpy import testing as npt @@ -34,6 +36,7 @@ def setup_method(self): cosmo = FlatLambdaCDM(H0=70, Om0=0.3) self.source_dict = blue_one self.deflector_dict = red_one + self.los_individual = LOSIndividual(kappa=0.1, gamma=[-0.1, -0.2]) print(blue_one) blue_one["gamma_pl"] = 2.1 @@ -52,6 +55,7 @@ def setup_method(self): gg_lens = Lens( source_class=self.source, deflector_class=self.deflector, + los_class=self.los_individual, lens_equation_solver="lenstronomy_analytical", # kwargs_variability={"MJD", "ps_mag_i"}, # This line will not be used in # the testing but at least code go through this warning message. @@ -113,8 +117,11 @@ def test_deflector_velocity_dispersion(self): assert vdp >= 10 def test_los_linear_distortions(self): - losd = self.gg_lens.los_linear_distortions - assert losd != 0 + kappa, gamma1, gamma2 = self.gg_lens.los_linear_distortions + assert kappa == self.los_individual.convergence + g1, g2 = self.los_individual.shear + assert gamma1 == g1 + assert gamma2 == g2 def test_point_source_arrival_times(self): dt_days = self.gg_lens.point_source_arrival_times() @@ -526,8 +533,10 @@ def setup_method(self): deflector_dict = Table.read( os.path.join(path, "TestData/deflector_supernovae_new.fits"), format="fits" ) + source_dict2 = copy.deepcopy(source_dict1) + source_dict2["z"] += 0.5 self.source1 = Source( - source_dict=source_dict1, + source_dict=source_dict2, cosmo=self.cosmo, source_type="point_plus_extended", light_profile="double_sersic", @@ -607,6 +616,8 @@ def test_einstein_radius_multi(self): # Test multisource einstein radius. assert einstein_radius1[0] == einstein_radius3[0] assert einstein_radius2[0] == einstein_radius3[1] + print(einstein_radius3, einstein_radius1) + assert 1 == 0 def test_image_observer_time_multi(self): observation_time = 50 From 6071ad283000fcb0e668b100322dd0f6faa333a9 Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Fri, 22 Nov 2024 10:22:40 -0500 Subject: [PATCH 03/13] updated to lenstronomy 1.12.2, explicitly changed some outputs to floats --- requirements.txt | 2 +- slsim/Deflectors/deflector.py | 2 +- slsim/Sources/source.py | 6 +++--- slsim/lens.py | 9 +++++---- tests/test_Source/test_source.py | 6 +++--- tests/test_lens.py | 24 ++++++++++++++++-------- 6 files changed, 29 insertions(+), 20 deletions(-) diff --git a/requirements.txt b/requirements.txt index 9802fc7d8..b8f60894c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ skypy -lenstronomy>=1.12.1 +lenstronomy>=1.12.2 astropy>=5.2 numpy scipy<=1.13.1 diff --git a/slsim/Deflectors/deflector.py b/slsim/Deflectors/deflector.py index bd35fb925..a5b5082fc 100644 --- a/slsim/Deflectors/deflector.py +++ b/slsim/Deflectors/deflector.py @@ -36,7 +36,7 @@ def redshift(self): :return: redshift """ - return self._deflector.redshift + return float(self._deflector.redshift) def velocity_dispersion(self, cosmo=None): """Velocity dispersion of deflector. diff --git a/slsim/Sources/source.py b/slsim/Sources/source.py index a3ab09ba5..397556dc3 100644 --- a/slsim/Sources/source.py +++ b/slsim/Sources/source.py @@ -343,19 +343,19 @@ def kwargs_variability_extracted(self): def redshift(self): """Returns source redshift.""" - return self.source_dict["z"] + return float(self.source_dict["z"]) @property def n_sersic(self): """Returns sersic index of the source.""" - return self.source_dict["n_sersic"] + return float(self.source_dict["n_sersic"]) @property def angular_size(self): """Returns angular size of the source.""" - return self.source_dict["angular_size"] + return float(self.source_dict["angular_size"]) @property def ellipticity(self): diff --git a/slsim/lens.py b/slsim/lens.py index abeb03d47..038171ae9 100644 --- a/slsim/lens.py +++ b/slsim/lens.py @@ -100,8 +100,8 @@ def __init__( self._source_type = self.max_redshift_source_class.source_type # we conventionally use highest source redshift in the lens cosmo. self._lens_cosmo = LensCosmo( - z_lens=float(self.deflector.redshift), - z_source=float(self.max_redshift_source_class.redshift), + z_lens=self.deflector.redshift, + z_source=self.max_redshift_source_class.redshift, cosmo=self.cosmo, ) @@ -211,6 +211,7 @@ def _point_source_image_positions(self, source): point_source_pos_x, point_source_pos_y = source.point_source_position( center_lens=self.deflector_position, draw_area=self.test_area ) + # uses analytical lens equation solver in case it is supported by lenstronomy for speed-up if ( self._lens_equation_solver == "lenstronomy_analytical" @@ -435,8 +436,8 @@ def _einstein_radius(self, source): else: # TODO: translate Einstein radius to different source redshift with power-law slope difference beta = self._lens_cosmo.beta_double_source_plane(z_lens=self.deflector_redshift, - z_source_1=self.max_redshift_source_class.redshift, - z_source_2=source.redshift) + z_source_2=self.max_redshift_source_class.redshift, + z_source_1=source.redshift) theta_E = theta_E_convention * beta ** (1.0 / (gamma_pl - 1)) kappa_ext = kappa_ext_convention * beta diff --git a/tests/test_Source/test_source.py b/tests/test_Source/test_source.py index 6c37dbc65..b0b6f1f6f 100644 --- a/tests/test_Source/test_source.py +++ b/tests/test_Source/test_source.py @@ -536,14 +536,14 @@ def setup_method(self): ) def test_redshift(self): - assert self.source.redshift == [0.5] + assert self.source.redshift == 0.5 assert self.source11.redshift == 3.123 def test_n_sersic(self): - assert self.source.n_sersic == [4] + assert self.source.n_sersic == 4 def test_angular_size(self): - assert self.source.angular_size == [0.35] + assert self.source.angular_size == 0.35 def test_ellipticity(self): assert self.source.ellipticity[0] == 0.8 diff --git a/tests/test_lens.py b/tests/test_lens.py index b67e9b130..bc3ecfd42 100644 --- a/tests/test_lens.py +++ b/tests/test_lens.py @@ -579,6 +579,14 @@ def setup_method(self): deflector_class=self.deflector, source_class=[self.source1, self.source2], cosmo=self.cosmo, + lens_equation_solver="lenstronomy_general" + ) + + self.lens_class3_analytical = Lens( + deflector_class=self.deflector, + source_class=[self.source1, self.source2], + cosmo=self.cosmo, + lens_equation_solver="lenstronomy_analytical" ) def test_point_source_arrival_time_multi(self): @@ -606,18 +614,16 @@ def test_es_magnification_multi(self): es_magnification2 = self.lens_class2.extended_source_magnification() es_magnification3 = self.lens_class3.extended_source_magnification() # Test multisource extended source magnifications. - assert es_magnification1[0] == es_magnification3[0] - assert es_magnification2[0] == es_magnification3[1] + npt.assert_almost_equal(es_magnification1[0], es_magnification3[0], decimal=3) + npt.assert_almost_equal(es_magnification2[0], es_magnification3[1], decimal=3) def test_einstein_radius_multi(self): einstein_radius1 = self.lens_class1.einstein_radius einstein_radius2 = self.lens_class2.einstein_radius einstein_radius3 = self.lens_class3.einstein_radius # Test multisource einstein radius. - assert einstein_radius1[0] == einstein_radius3[0] - assert einstein_radius2[0] == einstein_radius3[1] - print(einstein_radius3, einstein_radius1) - assert 1 == 0 + npt.assert_almost_equal(einstein_radius1[0], einstein_radius3[0], decimal=5) + npt.assert_almost_equal(einstein_radius2[0], einstein_radius3[1], decimal=5) def test_image_observer_time_multi(self): observation_time = 50 @@ -631,8 +637,10 @@ def test_image_observer_time_multi(self): observation_time ) # Test multisource image observation time - assert image_observation_time1[0] == image_observation_time3[0][0] - assert np.all(image_observation_time2 == image_observation_time3[1]) + npt.assert_almost_equal(image_observation_time1[0], image_observation_time3[0][0], decimal=5) + # assert image_observation_time1[0] == image_observation_time3[0][0] + npt.assert_almost_equal(image_observation_time2, image_observation_time3[1], decimal=5) + # assert np.all(image_observation_time2 == image_observation_time3[1]) assert len(self.lens_class3.image_observer_times(t_obs=10)) == 2 From 897f3f834f27de4ff5ea3251a2526b2631da9676 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:25:37 +0000 Subject: [PATCH 04/13] Autofix formatting from pre-commit.com hooks --- slsim/Deflectors/DeflectorTypes/epl_sersic.py | 23 +++++---- slsim/Deflectors/velocity_dispersion.py | 46 +++++++++++++----- slsim/lens.py | 20 ++++---- .../test_velocity_dispersion.py | 48 +++++++++++++++---- tests/test_lens.py | 12 +++-- 5 files changed, 108 insertions(+), 41 deletions(-) diff --git a/slsim/Deflectors/DeflectorTypes/epl_sersic.py b/slsim/Deflectors/DeflectorTypes/epl_sersic.py index 40e5aa7cd..b2937fa12 100644 --- a/slsim/Deflectors/DeflectorTypes/epl_sersic.py +++ b/slsim/Deflectors/DeflectorTypes/epl_sersic.py @@ -16,6 +16,7 @@ class EPLSersic(DeflectorBase): - 'e2_light': eccentricity of light - 'z': redshift of deflector """ + def __init__(self, deflector_dict): """ @@ -62,12 +63,18 @@ def mass_model_lenstronomy(self, lens_cosmo): theta_E = 0.0 else: lens_light_model_list, kwargs_lens_light = self.light_model_lenstronomy() - theta_E = theta_E_from_vel_disp_epl(vel_disp=float(self.velocity_dispersion(cosmo=lens_cosmo.background.cosmo)), - gamma=gamma, - r_half=self.angular_size_light, - kwargs_light=kwargs_lens_light, light_model_list=lens_light_model_list, - lens_cosmo=lens_cosmo, - kappa_ext=0, sis_convention=self._sis_convention) + theta_E = theta_E_from_vel_disp_epl( + vel_disp=float( + self.velocity_dispersion(cosmo=lens_cosmo.background.cosmo) + ), + gamma=gamma, + r_half=self.angular_size_light, + kwargs_light=kwargs_lens_light, + light_model_list=lens_light_model_list, + lens_cosmo=lens_cosmo, + kappa_ext=0, + sis_convention=self._sis_convention, + ) e1_mass, e2_mass = self.mass_ellipticity e1_mass_lenstronomy, e2_mass_lenstronomy = ellipticity_slsim_to_lenstronomy( @@ -130,9 +137,9 @@ def halo_properties(self): :return: gamma (with =2 is isothermal) """ - #if hasattr(self._deflector_dict, "gamma_pl"): + # if hasattr(self._deflector_dict, "gamma_pl"): # return float(self._deflector_dict["gamma_pl"]) - #else: + # else: # # TODO: this can (optionally) be made a function of stellar mass, velocity dispersion etc # return 2 try: diff --git a/slsim/Deflectors/velocity_dispersion.py b/slsim/Deflectors/velocity_dispersion.py index b6ba9aac1..8a998018b 100644 --- a/slsim/Deflectors/velocity_dispersion.py +++ b/slsim/Deflectors/velocity_dispersion.py @@ -73,9 +73,10 @@ def vel_disp_composite_model(r, m_star, rs_star, m_halo, c_halo, cosmo, z_lens): return vel_disp -def vel_disp_power_law(theta_E, gamma, r_half, kwargs_light, light_model_list, lens_cosmo): - """ - velocity dispersion for a power-law mass density profile +def vel_disp_power_law( + theta_E, gamma, r_half, kwargs_light, light_model_list, lens_cosmo +): + """Velocity dispersion for a power-law mass density profile. :param theta_E: Einstein radius [arc seconds] :param gamma: power-law slope of deflector @@ -88,7 +89,14 @@ def vel_disp_power_law(theta_E, gamma, r_half, kwargs_light, light_model_list, l # turn physical masses to lenstronomy units kwargs_mass = [ - {"theta_E": theta_E, "gamma": gamma, "e1": 0, "e2": 0, "center_x": 0, "center_y": 0}, + { + "theta_E": theta_E, + "gamma": gamma, + "e1": 0, + "e2": 0, + "center_x": 0, + "center_y": 0, + }, ] kwargs_anisotropy = {"beta": 0} light_model = LightModel(light_model_list=light_model_list) @@ -133,27 +141,39 @@ def vel_disp_power_law(theta_E, gamma, r_half, kwargs_light, light_model_list, l return vel_disp -def theta_E_from_vel_disp_epl(vel_disp, gamma, r_half, kwargs_light, light_model_list, lens_cosmo, - kappa_ext=0, sis_convention=True): - """ - calculates Einstein radius given measured aperture averaged velocity dispersion and given power-law slope +def theta_E_from_vel_disp_epl( + vel_disp, + gamma, + r_half, + kwargs_light, + light_model_list, + lens_cosmo, + kappa_ext=0, + sis_convention=True, +): + """Calculates Einstein radius given measured aperture averaged velocity + dispersion and given power-law slope. - :param vel_disp: velocity dispersion measured within an aperture radius [km/s] + :param vel_disp: velocity dispersion measured within an aperture + radius [km/s] :param gamma: power-law slope :param r_half: half light radius (aperture radius) [arc seconds] :param kwargs_light: list of dict for light model parameters :param light_model_list: list of light models :param lens_cosmo: ~LensCosmo instance :param kappa_ext: external convergence - :param sis_convention: it True, uses velocity dispersion not as measured one but as the SIS equivalent velocity - dispersion - :return: Einstein radius matching the velocity dispersion and external convergence + :param sis_convention: it True, uses velocity dispersion not as + measured one but as the SIS equivalent velocity dispersion + :return: Einstein radius matching the velocity dispersion and + external convergence """ if gamma == 2 or sis_convention is True: theta_E = lens_cosmo.sis_sigma_v2theta_E(vel_disp) else: theta_E_0 = 1 - vel_disp_0 = vel_disp_power_law(theta_E_0, gamma, r_half, kwargs_light, light_model_list, lens_cosmo) + vel_disp_0 = vel_disp_power_law( + theta_E_0, gamma, r_half, kwargs_light, light_model_list, lens_cosmo + ) # transform theta_E from vel_disp_0 prediction theta_E = theta_E_0 * (vel_disp / vel_disp_0) ** (2 / (gamma - 1)) theta_E /= (1 - kappa_ext) ** (1.0 / (gamma - 1)) diff --git a/slsim/lens.py b/slsim/lens.py index 038171ae9..ebeb7c5b1 100644 --- a/slsim/lens.py +++ b/slsim/lens.py @@ -421,11 +421,13 @@ def _einstein_radius(self, source): theta_E = 0 return theta_E lens_model_list, kwargs_lens = self.deflector_mass_model_lenstronomy() - lens_model = LensModel(lens_model_list=lens_model_list, - z_lens=self.deflector_redshift, - z_source_convention=self.max_redshift_source_class.redshift, - multi_plane=False, - z_source=source.redshift) + lens_model = LensModel( + lens_model_list=lens_model_list, + z_lens=self.deflector_redshift, + z_source_convention=self.max_redshift_source_class.redshift, + multi_plane=False, + z_source=source.redshift, + ) if self.deflector.deflector_type in ["EPL"]: kappa_ext_convention = self.los_class.convergence gamma_pl = self.deflector.halo_properties @@ -435,9 +437,11 @@ def _einstein_radius(self, source): kappa_ext = kappa_ext_convention else: # TODO: translate Einstein radius to different source redshift with power-law slope difference - beta = self._lens_cosmo.beta_double_source_plane(z_lens=self.deflector_redshift, - z_source_2=self.max_redshift_source_class.redshift, - z_source_1=source.redshift) + beta = self._lens_cosmo.beta_double_source_plane( + z_lens=self.deflector_redshift, + z_source_2=self.max_redshift_source_class.redshift, + z_source_1=source.redshift, + ) theta_E = theta_E_convention * beta ** (1.0 / (gamma_pl - 1)) kappa_ext = kappa_ext_convention * beta diff --git a/tests/test_Deflectors/test_velocity_dispersion.py b/tests/test_Deflectors/test_velocity_dispersion.py index b00a02975..a262865a6 100644 --- a/tests/test_Deflectors/test_velocity_dispersion.py +++ b/tests/test_Deflectors/test_velocity_dispersion.py @@ -5,7 +5,7 @@ vel_disp_nfw_aperture, redshifts_from_comoving_density, vel_disp_power_law, - theta_E_from_vel_disp_epl + theta_E_from_vel_disp_epl, ) from lenstronomy.Cosmo.lens_cosmo import LensCosmo import numpy as np @@ -53,14 +53,26 @@ def test_vel_disp_power_law(): theta_E = 1.5 gamma = 2 r_half = 1 - kwargs_light = [{"amp": 1, "R_sersic": 2, "n_sersic": 1, "e1": 0, "e2": 0, "center_x": 0, "center_y": 0}] + kwargs_light = [ + { + "amp": 1, + "R_sersic": 2, + "n_sersic": 1, + "e1": 0, + "e2": 0, + "center_x": 0, + "center_y": 0, + } + ] light_model_list = ["SERSIC_ELLIPSE"] # kwargs_light = [{"amp": 1, "gamma": 2, "e1": 0, "e2": 0, "center_x": 0, "center_y": 0}] # light_model_list = ["POWER_LAW"] lens_cosmo = LensCosmo(z_lens=z_lens, z_source=z_source, cosmo=cosmo) - vel_disp_pl = vel_disp_power_law(theta_E, gamma, r_half, kwargs_light, light_model_list, lens_cosmo) + vel_disp_pl = vel_disp_power_law( + theta_E, gamma, r_half, kwargs_light, light_model_list, lens_cosmo + ) vel_disp_sis = lens_cosmo.sis_theta_E2sigma_v(theta_E) npt.assert_almost_equal(vel_disp_pl / vel_disp_sis, 1, decimal=1) @@ -74,14 +86,34 @@ def test_theta_E_from_vel_disp_epl(): theta_E = 1.5 gamma = 2.5 r_half = 1 - kwargs_light = [{"amp": 1, "R_sersic": 2, "n_sersic": 1, "e1": 0, "e2": 0, "center_x": 0, "center_y": 0}] + kwargs_light = [ + { + "amp": 1, + "R_sersic": 2, + "n_sersic": 1, + "e1": 0, + "e2": 0, + "center_x": 0, + "center_y": 0, + } + ] light_model_list = ["SERSIC_ELLIPSE"] - vel_disp_pl = vel_disp_power_law(theta_E, gamma, r_half, kwargs_light, light_model_list, lens_cosmo) + vel_disp_pl = vel_disp_power_law( + theta_E, gamma, r_half, kwargs_light, light_model_list, lens_cosmo + ) - theta_E_out = theta_E_from_vel_disp_epl(vel_disp_pl, gamma, r_half, kwargs_light, - light_model_list, lens_cosmo, kappa_ext=0, sis_convention=False) - npt.assert_almost_equal(theta_E_out/theta_E, 1, decimal=3) + theta_E_out = theta_E_from_vel_disp_epl( + vel_disp_pl, + gamma, + r_half, + kwargs_light, + light_model_list, + lens_cosmo, + kappa_ext=0, + sis_convention=False, + ) + npt.assert_almost_equal(theta_E_out / theta_E, 1, decimal=3) def test_schechter_vdf(): diff --git a/tests/test_lens.py b/tests/test_lens.py index bc3ecfd42..5a4a93ffc 100644 --- a/tests/test_lens.py +++ b/tests/test_lens.py @@ -579,14 +579,14 @@ def setup_method(self): deflector_class=self.deflector, source_class=[self.source1, self.source2], cosmo=self.cosmo, - lens_equation_solver="lenstronomy_general" + lens_equation_solver="lenstronomy_general", ) self.lens_class3_analytical = Lens( deflector_class=self.deflector, source_class=[self.source1, self.source2], cosmo=self.cosmo, - lens_equation_solver="lenstronomy_analytical" + lens_equation_solver="lenstronomy_analytical", ) def test_point_source_arrival_time_multi(self): @@ -637,9 +637,13 @@ def test_image_observer_time_multi(self): observation_time ) # Test multisource image observation time - npt.assert_almost_equal(image_observation_time1[0], image_observation_time3[0][0], decimal=5) + npt.assert_almost_equal( + image_observation_time1[0], image_observation_time3[0][0], decimal=5 + ) # assert image_observation_time1[0] == image_observation_time3[0][0] - npt.assert_almost_equal(image_observation_time2, image_observation_time3[1], decimal=5) + npt.assert_almost_equal( + image_observation_time2, image_observation_time3[1], decimal=5 + ) # assert np.all(image_observation_time2 == image_observation_time3[1]) assert len(self.lens_class3.image_observer_times(t_obs=10)) == 2 From e8f5b992a39524119b814e83c01cbeb4bd44e199 Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Fri, 22 Nov 2024 10:28:54 -0500 Subject: [PATCH 05/13] pre-commit fix --- slsim/lens.py | 1 - 1 file changed, 1 deletion(-) diff --git a/slsim/lens.py b/slsim/lens.py index 038171ae9..b5066abc4 100644 --- a/slsim/lens.py +++ b/slsim/lens.py @@ -13,7 +13,6 @@ from lenstronomy.Util import util from slsim.lensed_system_base import LensedSystemBase -from slsim.Deflectors.velocity_dispersion import theta_E_from_vel_disp_epl class Lens(LensedSystemBase): From 8e2f9e5c38fd6032276dd1c3f1fc9e86ae29edf4 Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Fri, 22 Nov 2024 10:35:57 -0500 Subject: [PATCH 06/13] added additional tests explicitly testing the analytical and numerical lens equation solver option --- tests/test_lens.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/test_lens.py b/tests/test_lens.py index 5a4a93ffc..fb18a0675 100644 --- a/tests/test_lens.py +++ b/tests/test_lens.py @@ -593,6 +593,15 @@ def test_point_source_arrival_time_multi(self): point_source_arival_time1 = self.lens_class1.point_source_arrival_times() point_source_arival_time2 = self.lens_class2.point_source_arrival_times() point_source_arival_time3 = self.lens_class3.point_source_arrival_times() + # Test multisource point source arrival time. + assert np.all(point_source_arival_time1[0]) == np.all( + point_source_arival_time3[0] + ) + assert np.all(point_source_arival_time2[0]) == np.all( + point_source_arival_time3[1] + ) + + point_source_arival_time3 = self.lens_class3_analytical.point_source_arrival_times() # Test multisource point source arival time. assert np.all(point_source_arival_time1[0]) == np.all( point_source_arival_time3[0] @@ -609,6 +618,10 @@ def test_ps_magnification_multi(self): assert np.all(ps_magnification1[0]) == np.all(ps_magnification3[0]) assert np.all(ps_magnification2[0]) == np.all(ps_magnification3[1]) + ps_magnification3 = self.lens_class3_analytical.point_source_magnification() + assert np.all(ps_magnification1[0]) == np.all(ps_magnification3[0]) + assert np.all(ps_magnification2[0]) == np.all(ps_magnification3[1]) + def test_es_magnification_multi(self): es_magnification1 = self.lens_class1.extended_source_magnification() es_magnification2 = self.lens_class2.extended_source_magnification() @@ -617,6 +630,10 @@ def test_es_magnification_multi(self): npt.assert_almost_equal(es_magnification1[0], es_magnification3[0], decimal=3) npt.assert_almost_equal(es_magnification2[0], es_magnification3[1], decimal=3) + es_magnification3 = self.lens_class3_analytical.extended_source_magnification() + npt.assert_almost_equal(es_magnification1[0], es_magnification3[0], decimal=3) + npt.assert_almost_equal(es_magnification2[0], es_magnification3[1], decimal=3) + def test_einstein_radius_multi(self): einstein_radius1 = self.lens_class1.einstein_radius einstein_radius2 = self.lens_class2.einstein_radius @@ -625,6 +642,10 @@ def test_einstein_radius_multi(self): npt.assert_almost_equal(einstein_radius1[0], einstein_radius3[0], decimal=5) npt.assert_almost_equal(einstein_radius2[0], einstein_radius3[1], decimal=5) + einstein_radius3 = self.lens_class3_analytical.einstein_radius + npt.assert_almost_equal(einstein_radius1[0], einstein_radius3[0], decimal=5) + npt.assert_almost_equal(einstein_radius2[0], einstein_radius3[1], decimal=5) + def test_image_observer_time_multi(self): observation_time = 50 image_observation_time1 = self.lens_class1.image_observer_times( @@ -647,6 +668,18 @@ def test_image_observer_time_multi(self): # assert np.all(image_observation_time2 == image_observation_time3[1]) assert len(self.lens_class3.image_observer_times(t_obs=10)) == 2 + image_observation_time3 = self.lens_class3_analytical.image_observer_times( + observation_time + ) + # Test multisource image observation time + npt.assert_almost_equal( + image_observation_time1[0], image_observation_time3[0][0], decimal=5 + ) + # assert image_observation_time1[0] == image_observation_time3[0][0] + npt.assert_almost_equal( + image_observation_time2, image_observation_time3[1], decimal=5 + ) + if __name__ == "__main__": pytest.main() From 45ce68415069d804b2d01ce3cca5b4c8953c5d4b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:36:24 +0000 Subject: [PATCH 07/13] Autofix formatting from pre-commit.com hooks --- tests/test_lens.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_lens.py b/tests/test_lens.py index fb18a0675..3a0dffadc 100644 --- a/tests/test_lens.py +++ b/tests/test_lens.py @@ -601,7 +601,9 @@ def test_point_source_arrival_time_multi(self): point_source_arival_time3[1] ) - point_source_arival_time3 = self.lens_class3_analytical.point_source_arrival_times() + point_source_arival_time3 = ( + self.lens_class3_analytical.point_source_arrival_times() + ) # Test multisource point source arival time. assert np.all(point_source_arival_time1[0]) == np.all( point_source_arival_time3[0] From 66be058375cb8fb9de59e531d2ecf2712b016022 Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Fri, 22 Nov 2024 10:39:58 -0500 Subject: [PATCH 08/13] delete outcommented lines --- slsim/Deflectors/DeflectorTypes/epl_sersic.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/slsim/Deflectors/DeflectorTypes/epl_sersic.py b/slsim/Deflectors/DeflectorTypes/epl_sersic.py index b2937fa12..205ea1d1a 100644 --- a/slsim/Deflectors/DeflectorTypes/epl_sersic.py +++ b/slsim/Deflectors/DeflectorTypes/epl_sersic.py @@ -137,12 +137,8 @@ def halo_properties(self): :return: gamma (with =2 is isothermal) """ - # if hasattr(self._deflector_dict, "gamma_pl"): - # return float(self._deflector_dict["gamma_pl"]) - # else: - # # TODO: this can (optionally) be made a function of stellar mass, velocity dispersion etc - # return 2 try: return float(self._deflector_dict["gamma_pl"]) except KeyError: + # TODO: this can (optionally) be made a function of stellar mass, velocity dispersion etc return 2 From 6ecb3e0d856eea7c022e480d532ad02d8d1d3c88 Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Fri, 22 Nov 2024 10:41:49 -0500 Subject: [PATCH 09/13] remove a TODO as done --- slsim/lens.py | 1 - 1 file changed, 1 deletion(-) diff --git a/slsim/lens.py b/slsim/lens.py index 2426edc44..385e2ec0a 100644 --- a/slsim/lens.py +++ b/slsim/lens.py @@ -435,7 +435,6 @@ def _einstein_radius(self, source): theta_E = theta_E_convention kappa_ext = kappa_ext_convention else: - # TODO: translate Einstein radius to different source redshift with power-law slope difference beta = self._lens_cosmo.beta_double_source_plane( z_lens=self.deflector_redshift, z_source_2=self.max_redshift_source_class.redshift, From 8a83210bb47631022ff4d35d2f049c5dbc70675b Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Fri, 22 Nov 2024 10:50:09 -0500 Subject: [PATCH 10/13] make test requirements in coolest explicit --- tests/test_coolest_slsim_interface.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_coolest_slsim_interface.py b/tests/test_coolest_slsim_interface.py index cc754fd52..6c2cc2338 100644 --- a/tests/test_coolest_slsim_interface.py +++ b/tests/test_coolest_slsim_interface.py @@ -112,19 +112,19 @@ def test_update_coolest_from_slsim_and_create_slsim_from_coolest( ) npt.assert_almost_equal( slsim_from_updated_coolest[1]["kwargs_lens"][2]["kappa"], - expected_result[1]["kwargs_lens"][2]["kappa"], + expected_result[1]["kwargs_lens"][2]["kappa"], decimal=6 ) npt.assert_almost_equal( slsim_from_updated_coolest[1]["kwargs_source"][0]["magnitude"], - expected_result[1]["kwargs_source"][0]["magnitude"], + expected_result[1]["kwargs_source"][0]["magnitude"], decimal=6 ) npt.assert_almost_equal( slsim_from_updated_coolest[1]["kwargs_source"][1]["magnitude"], - expected_result[1]["kwargs_source"][1]["magnitude"], + expected_result[1]["kwargs_source"][1]["magnitude"], decimal=6 ) npt.assert_almost_equal( slsim_from_updated_coolest[1]["kwargs_ps"][0]["magnitude"], - [expected_result[1]["kwargs_ps"][0]["magnitude"]], + [expected_result[1]["kwargs_ps"][0]["magnitude"]], decimal=4 ) os.remove(test_path + "coolest_template_update.json") From 69db6012dd2b6addee8ed1737e464ce50bebdd8e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:51:05 +0000 Subject: [PATCH 11/13] Autofix formatting from pre-commit.com hooks --- tests/test_coolest_slsim_interface.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/test_coolest_slsim_interface.py b/tests/test_coolest_slsim_interface.py index 6c2cc2338..6b757500b 100644 --- a/tests/test_coolest_slsim_interface.py +++ b/tests/test_coolest_slsim_interface.py @@ -112,19 +112,23 @@ def test_update_coolest_from_slsim_and_create_slsim_from_coolest( ) npt.assert_almost_equal( slsim_from_updated_coolest[1]["kwargs_lens"][2]["kappa"], - expected_result[1]["kwargs_lens"][2]["kappa"], decimal=6 + expected_result[1]["kwargs_lens"][2]["kappa"], + decimal=6, ) npt.assert_almost_equal( slsim_from_updated_coolest[1]["kwargs_source"][0]["magnitude"], - expected_result[1]["kwargs_source"][0]["magnitude"], decimal=6 + expected_result[1]["kwargs_source"][0]["magnitude"], + decimal=6, ) npt.assert_almost_equal( slsim_from_updated_coolest[1]["kwargs_source"][1]["magnitude"], - expected_result[1]["kwargs_source"][1]["magnitude"], decimal=6 + expected_result[1]["kwargs_source"][1]["magnitude"], + decimal=6, ) npt.assert_almost_equal( slsim_from_updated_coolest[1]["kwargs_ps"][0]["magnitude"], - [expected_result[1]["kwargs_ps"][0]["magnitude"]], decimal=4 + [expected_result[1]["kwargs_ps"][0]["magnitude"]], + decimal=4, ) os.remove(test_path + "coolest_template_update.json") From 118af3a2c9971392229592acfae7c3ad67be3d9e Mon Sep 17 00:00:00 2001 From: Simon Birrer Date: Fri, 22 Nov 2024 11:15:07 -0500 Subject: [PATCH 12/13] better ellipticity definitions --- tests/test_coolest_slsim_interface.py | 2 +- tests/test_lens.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_coolest_slsim_interface.py b/tests/test_coolest_slsim_interface.py index 6c2cc2338..1fcbc85ba 100644 --- a/tests/test_coolest_slsim_interface.py +++ b/tests/test_coolest_slsim_interface.py @@ -108,7 +108,7 @@ def test_update_coolest_from_slsim_and_create_slsim_from_coolest( ) npt.assert_almost_equal( slsim_from_updated_coolest[1]["kwargs_lens"][1]["gamma1"], - expected_result[1]["kwargs_lens"][1]["gamma1"], + expected_result[1]["kwargs_lens"][1]["gamma1"], decimal=6, ) npt.assert_almost_equal( slsim_from_updated_coolest[1]["kwargs_lens"][2]["kappa"], diff --git a/tests/test_lens.py b/tests/test_lens.py index 3a0dffadc..4206bd56a 100644 --- a/tests/test_lens.py +++ b/tests/test_lens.py @@ -533,6 +533,8 @@ def setup_method(self): deflector_dict = Table.read( os.path.join(path, "TestData/deflector_supernovae_new.fits"), format="fits" ) + self.gamma_pl = 2.0 + deflector_dict["gamma_pl"] = self.gamma_pl source_dict2 = copy.deepcopy(source_dict1) source_dict2["z"] += 0.5 self.source1 = Source( @@ -561,10 +563,12 @@ def setup_method(self): sn_absolute_mag_band="bessellb", sn_absolute_zpsys="ab", ) + self.deflector = Deflector( deflector_type="EPL", deflector_dict=deflector_dict, ) + self.lens_class1 = Lens( deflector_class=self.deflector, source_class=self.source1, @@ -590,6 +594,9 @@ def setup_method(self): ) def test_point_source_arrival_time_multi(self): + gamma_pl_out = self.deflector.halo_properties + assert gamma_pl_out == self.gamma_pl + point_source_arival_time1 = self.lens_class1.point_source_arrival_times() point_source_arival_time2 = self.lens_class2.point_source_arrival_times() point_source_arival_time3 = self.lens_class3.point_source_arrival_times() From 9cd36e11a83c9686c8d63212098676e229004b92 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:15:44 +0000 Subject: [PATCH 13/13] Autofix formatting from pre-commit.com hooks --- tests/test_coolest_slsim_interface.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_coolest_slsim_interface.py b/tests/test_coolest_slsim_interface.py index de777d331..a833a1dc6 100644 --- a/tests/test_coolest_slsim_interface.py +++ b/tests/test_coolest_slsim_interface.py @@ -108,7 +108,8 @@ def test_update_coolest_from_slsim_and_create_slsim_from_coolest( ) npt.assert_almost_equal( slsim_from_updated_coolest[1]["kwargs_lens"][1]["gamma1"], - expected_result[1]["kwargs_lens"][1]["gamma1"], decimal=6, + expected_result[1]["kwargs_lens"][1]["gamma1"], + decimal=6, ) npt.assert_almost_equal( slsim_from_updated_coolest[1]["kwargs_lens"][2]["kappa"],