Skip to content

Commit

Permalink
Merge pull request #286 from nkhadka21/test_fix
Browse files Browse the repository at this point in the history
Fixed some testing functions and bug in Lens class.
  • Loading branch information
sibirrer authored Nov 22, 2024
2 parents 70f6a07 + 4ed9aea commit 94bf863
Show file tree
Hide file tree
Showing 86 changed files with 2,077 additions and 1,751 deletions.
26 changes: 16 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
ci:
autofix_commit_msg: |
[pre-commit.ci] auto fixes from pre-commit.com hooks
Autofix formatting from pre-commit.com hooks
for more information, see https://pre-commit.ci
autofix_prs: true
autoupdate_branch: ''
autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate'
Expand All @@ -13,18 +12,18 @@ ci:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.5.6
rev: v0.7.4
hooks:
- id: ruff
exclude: docs/.*
args: ["--line-length=88"]
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.8.7
rev: 1.9.1
hooks:
- id: nbqa-ruff
args: ["--line-length=120"]
- repo: https://github.com/psf/black
rev: 24.8.0
rev: 24.10.0
hooks:
- id: black
# It is recommended to specify the latest version of Python
Expand All @@ -33,13 +32,20 @@ repos:
# https://pre-commit.com/#top_level-default_language_version
language_version: python3
- repo: https://github.com/psf/black
rev: 24.8.0
rev: 24.10.0
hooks:
- id: black-jupyter
language_version: python3
- repo: https://github.com/PyCQA/docformatter
rev: v1.7.5
- repo: local
hooks:
- id: docformatter
additional_dependencies: [tomli]
args: [-r, --black, --in-place]
name: docformatter
description: Formats docstrings to follow PEP 257.
entry: python -Im docformatter
additional_dependencies:
- docformatter == 1.7.5
args:
- --in-place
language: python
types:
- python
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
" deflector_population=lens_galaxies,\n",
" source_population=source_galaxies,\n",
" cosmo=cosmo,\n",
" sky_area=sky_area\n",
" sky_area=sky_area,\n",
")\n",
"kwargs_lens_cut = {}\n",
"supernova_lens_class = supernova_lens_pop.select_lens_at_random(**kwargs_lens_cut)\n",
Expand Down
5 changes: 1 addition & 4 deletions notebooks/lensed_quasar_population_with_variability.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@
"source": [
"# generate galaxy population using skypy pipeline.\n",
"galaxy_simulation_pipeline = pipelines.SkyPyPipeline(\n",
" skypy_config=None,\n",
" sky_area=galaxy_sky_area,\n",
" filters=None,\n",
" cosmo=cosmo\n",
" skypy_config=None, sky_area=galaxy_sky_area, filters=None, cosmo=cosmo\n",
")"
]
},
Expand Down
25 changes: 13 additions & 12 deletions notebooks/supernovae_injection_to_opsim_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"from astropy.units import Quantity\n",
"from slsim.lens_pop import LensPop\n",
"import numpy as np\n",
"from astropy.units import Quantity\n",
"import slsim.Sources as sources\n",
"import slsim.Deflectors as deflectors\n",
"import slsim.Pipelines as pipelines\n",
Expand All @@ -20,8 +19,7 @@
"import astropy.coordinates as coord\n",
"import astropy.units as u\n",
"from slsim.LsstSciencePipeline import opsim_pipeline\n",
"from slsim.LsstSciencePipeline.util_lsst import opsim_variable_lens_injection\n",
"from slsim.Plots.plot_functions import create_image_montage_from_image_list"
"from slsim.LsstSciencePipeline.util_lsst import opsim_variable_lens_injection"
]
},
{
Expand Down Expand Up @@ -68,7 +66,7 @@
"kwargs_deflector_cut = {\"band\": \"g\", \"band_max\": 24, \"z_min\": 0.01, \"z_max\": 2.5}\n",
"kwargs_source_cut = {\"z_min\": 0.1, \"z_max\": 5.0}\n",
"\n",
"time_range = np.linspace(-50, 100, 500)\n"
"time_range = np.linspace(-50, 100, 500)"
]
},
{
Expand Down Expand Up @@ -185,7 +183,7 @@
" deflector_population=lens_galaxies,\n",
" source_population=supernovae_sourece,\n",
" sky_area=sky_area_full,\n",
" cosmo=cosmo\n",
" cosmo=cosmo,\n",
")"
]
},
Expand Down Expand Up @@ -315,7 +313,7 @@
"metadata": {},
"outputs": [],
"source": [
"#np.random.seed(1)\n",
"# np.random.seed(1)\n",
"N = 10\n",
"\n",
"ra_points = coord.Angle(np.random.uniform(low=0, high=360, size=N) * u.degree)\n",
Expand Down Expand Up @@ -345,16 +343,16 @@
}
],
"source": [
"# opsim_path is a path for opsim summary data. One need to download baseline_v3.4_10yrs.db \n",
"# to run this pipeline. Please download this data from and replace opsim_path with your path: \n",
"# opsim_path is a path for opsim summary data. One need to download baseline_v3.4_10yrs.db\n",
"# to run this pipeline. Please download this data from and replace opsim_path with your path:\n",
"# https://epyc.astro.washington.edu/~lynnej/opsim_downloads/fbs_3.4/initial/initial_v3.4_10yrs.db\n",
"exposure_data = opsim_pipeline.opsim_time_series_images_data(\n",
" ra_points,\n",
" dec_points,\n",
" \"baseline_v3.0_10yrs\",\n",
" print_warning=True,\n",
" num_pix=63,\n",
" opsim_path=\"/Users/narayankhadka/downloads/baseline_v3.4_10yrs.db\"\n",
" opsim_path=\"/Users/narayankhadka/downloads/baseline_v3.4_10yrs.db\",\n",
")"
]
},
Expand All @@ -371,7 +369,7 @@
"metadata": {},
"outputs": [],
"source": [
"#There are multiple time series datasets, so I chose the first one.\n",
"# There are multiple time series datasets, so I chose the first one.\n",
"index = 0\n",
"bands = [\"i\"]\n",
"num_pix = 63\n",
Expand Down Expand Up @@ -401,8 +399,11 @@
"source": [
"## Plot first 10 images. One can plot any number of images\n",
"plot_montage = create_image_montage_from_image_list(\n",
" num_rows=2, num_cols=5, images=images[\"injected_lens\"][:10], image_center=[pix_coord[0][0], pix_coord[1][0]],\n",
" time=images[\"obs_time\"][:10]\n",
" num_rows=2,\n",
" num_cols=5,\n",
" images=images[\"injected_lens\"][:10],\n",
" image_center=[pix_coord[0][0], pix_coord[1][0]],\n",
" time=images[\"obs_time\"][:10],\n",
")"
]
},
Expand Down
2 changes: 1 addition & 1 deletion notebooks/supernovae_plus_extended_source_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"kwargs_deflector_cut = {\"band\": \"g\", \"band_max\": 23, \"z_min\": 0.01, \"z_max\": 2.5}\n",
"kwargs_source_cut = {\"z_min\": 0.1, \"z_max\": 5.0}\n",
"\n",
"# This file can be downloaded from here: https://github.com/LSST-strong-lensing/data_public . \n",
"# This file can be downloaded from here: https://github.com/LSST-strong-lensing/data_public .\n",
"# Please download this file and replace the path with your path.\n",
"sn_modeldir = \"/Users/narayankhadka/downloads/sncosmo_sn_models/SALT3.NIR_WAVEEXT\""
]
Expand Down
10 changes: 6 additions & 4 deletions slsim/Deflectors/DeflectorTypes/deflector_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@


class DeflectorBase(ABC):
"""Class of a single deflector with quantities only related to the deflector
(independent of the source)"""
"""Class of a single deflector with quantities only related to the
deflector (independent of the source)"""

def __init__(self, deflector_dict):
"""
Expand Down Expand Up @@ -100,7 +100,8 @@ def mass_ellipticity(self):

@abstractmethod
def mass_model_lenstronomy(self, lens_cosmo):
"""Returns lens model instance and parameters in lenstronomy conventions.
"""Returns lens model instance and parameters in lenstronomy
conventions.
:param lens_cosmo: lens cosmology model
:type lens_cosmo: ~lenstronomy.Cosmo.LensCosmo instance
Expand All @@ -110,7 +111,8 @@ def mass_model_lenstronomy(self, lens_cosmo):

@abstractmethod
def light_model_lenstronomy(self, band=None):
"""Returns lens model instance and parameters in lenstronomy conventions.
"""Returns lens model instance and parameters in lenstronomy
conventions.
:param band: imaging band
:type band: str
Expand Down
6 changes: 4 additions & 2 deletions slsim/Deflectors/DeflectorTypes/epl_sersic.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def light_ellipticity(self):
return e1_light, e2_light

def mass_model_lenstronomy(self, lens_cosmo):
"""Returns lens model instance and parameters in lenstronomy conventions.
"""Returns lens model instance and parameters in lenstronomy
conventions.
:param lens_cosmo: lens cosmology model
:type lens_cosmo: ~lenstronomy.Cosmo.LensCosmo instance
Expand Down Expand Up @@ -73,7 +74,8 @@ def mass_model_lenstronomy(self, lens_cosmo):
return lens_mass_model_list, kwargs_lens_mass

def light_model_lenstronomy(self, band=None):
"""Returns lens model instance and parameters in lenstronomy conventions.
"""Returns lens model instance and parameters in lenstronomy
conventions.
:param band: imaging band
:type band: str
Expand Down
18 changes: 10 additions & 8 deletions slsim/Deflectors/DeflectorTypes/nfw_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@


class NFWCluster(DeflectorBase):
"""Class of a NFW halo lens model with subhalos. Each subhalo is a EPLSersic
instance with its own mass and light.
"""Class of a NFW halo lens model with subhalos. Each subhalo is a
EPLSersic instance with its own mass and light.
required quantities in dictionary:
- 'halo_mass': halo mass in physical M_sol
Expand All @@ -29,8 +29,8 @@ def __init__(self, deflector_dict):
super(NFWCluster, self).__init__(deflector_dict)

def velocity_dispersion(self, cosmo=None):
"""Velocity dispersion of deflector. Simplified assumptions on anisotropy and
averaged over the characteristic radius.
"""Velocity dispersion of deflector. Simplified assumptions on
anisotropy and averaged over the characteristic radius.
:param cosmo: cosmology
:type cosmo: ~astropy.cosmology class
Expand All @@ -40,7 +40,8 @@ def velocity_dispersion(self, cosmo=None):
return vel_disp_nfw(m_halo, c_halo, cosmo, self.redshift)

def mass_model_lenstronomy(self, lens_cosmo):
"""Returns lens model instance and parameters in lenstronomy conventions.
"""Returns lens model instance and parameters in lenstronomy
conventions.
:param lens_cosmo: lens cosmology model
:type lens_cosmo: ~lenstronomy.Cosmo.LensCosmo instance
Expand All @@ -58,8 +59,8 @@ def mass_model_lenstronomy(self, lens_cosmo):
return lens_mass_model_list, kwargs_lens_mass

def _halo_mass_model_lenstronomy(self, lens_cosmo):
"""Returns lens model instance and parameters in lenstronomy conventions for the
main halo.
"""Returns lens model instance and parameters in lenstronomy
conventions for the main halo.
:param lens_cosmo: lens cosmology model
:type lens_cosmo: ~lenstronomy.Cosmo.LensCosmo instance
Expand All @@ -86,7 +87,8 @@ def _halo_mass_model_lenstronomy(self, lens_cosmo):
return lens_mass_model_list, kwargs_lens_mass

def light_model_lenstronomy(self, band=None):
"""Returns lens model instance and parameters in lenstronomy conventions.
"""Returns lens model instance and parameters in lenstronomy
conventions.
:param band: imaging band
:type band: str
Expand Down
10 changes: 6 additions & 4 deletions slsim/Deflectors/DeflectorTypes/nfw_hernquist.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class NFWHernquist(DeflectorBase):
"""

def velocity_dispersion(self, cosmo=None):
"""Velocity dispersion of deflector. Simplified assumptions on anisotropy and
averaged over the half-light radius.
"""Velocity dispersion of deflector. Simplified assumptions on
anisotropy and averaged over the half-light radius.
:param cosmo: cosmology
:type cosmo: ~astropy.cosmology class
Expand Down Expand Up @@ -55,7 +55,8 @@ def velocity_dispersion(self, cosmo=None):
return self._deflector_dict["vel_disp"]

def mass_model_lenstronomy(self, lens_cosmo):
"""Returns lens model instance and parameters in lenstronomy conventions.
"""Returns lens model instance and parameters in lenstronomy
conventions.
:param lens_cosmo: lens cosmology model
:type lens_cosmo: ~lenstronomy.Cosmo.LensCosmo instance
Expand Down Expand Up @@ -100,7 +101,8 @@ def mass_model_lenstronomy(self, lens_cosmo):
return lens_mass_model_list, kwargs_lens_mass

def light_model_lenstronomy(self, band=None):
"""Returns lens model instance and parameters in lenstronomy conventions.
"""Returns lens model instance and parameters in lenstronomy
conventions.
:param band: imaging band
:type band: str
Expand Down
11 changes: 6 additions & 5 deletions slsim/Deflectors/all_lens_galaxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def __init__(
:param sky_area: Sky area over which galaxies are sampled. Must be in units of
solid angle.
:param gamma_pl: power law slope in EPL profile.
:type gamma_pl: A float or a dictionary with given mean and standard deviation
of a density slope for gaussian distribution or minimum and maximum values of
:type gamma_pl: A float or a dictionary with given mean and standard deviation
of a density slope for gaussian distribution or minimum and maximum values of
gamma for uniform distribution. eg: gamma_pl=2.1, gamma_pl={"mean": a, "std_dev": b},
gamma_pl={"gamma_min": c, "gamma_max": d}
:param catalog_type: type of the catalog. If user is using deflector catalog
Expand Down Expand Up @@ -72,7 +72,7 @@ def __init__(
kwargs_cut=kwargs_cut,
cosmo=cosmo,
sky_area=sky_area,
gamma_pl=gamma_pl
gamma_pl=gamma_pl,
)

n = len(galaxy_list)
Expand Down Expand Up @@ -126,8 +126,9 @@ def draw_deflector(self):
deflector["e2_mass"] = e2_mass
if deflector["n_sersic"] == -1:
deflector["n_sersic"] = 4 # TODO make a better estimate with scatter
deflector_class = Deflector(deflector_type=self.deflector_profile,
deflector_dict=deflector)
deflector_class = Deflector(
deflector_type=self.deflector_profile, deflector_dict=deflector
)
return deflector_class


Expand Down
25 changes: 15 additions & 10 deletions slsim/Deflectors/cluster_deflectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@


class ClusterDeflectors(DeflectorsBase):
"""Class describing cluster lens model with a NFW profile for the dark matter halo
and EPL profile for the subhalos (cluster members). It makes use of a group/cluster
catalog and a group/cluster member catalog (e.g. redMaPPer).
"""Class describing cluster lens model with a NFW profile for the dark
matter halo and EPL profile for the subhalos (cluster members). It makes
use of a group/cluster catalog and a group/cluster member catalog (e.g.
redMaPPer).
This class is called by setting deflector_type == "cluster-catalog" in LensPop.
This class is called by setting deflector_type == "cluster-catalog"
in LensPop.
"""

def __init__(
Expand Down Expand Up @@ -121,8 +123,9 @@ def draw_deflector(self):
deflector = self.draw_cluster(index)
members = self.draw_members(deflector["cluster_id"], **self.kwargs_draw_members)
deflector["subhalos"] = members
deflector_class = Deflector(deflector_type=self.deflector_profile,
deflector_dict=deflector)
deflector_class = Deflector(
deflector_type=self.deflector_profile, deflector_dict=deflector
)
return deflector_class

def draw_cluster(self, index):
Expand Down Expand Up @@ -211,10 +214,11 @@ def assign_similar_galaxy(
bands=("g", "r", "i", "z", "Y"),
max_gals=10000,
):
"""Assigns a similar galaxy to each member of a group/cluster member catalog by
comparing their magnitudes and redshifts.
"""Assigns a similar galaxy to each member of a group/cluster member
catalog by comparing their magnitudes and redshifts.
:param members_list: astropy table with columns 'mag_{band}', 'z'
:param members_list: astropy table with columns 'mag_{band}',
'z'
:type members_list: astropy.table.Table
:param galaxy_list: astropy table with columns 'mag_{band}', 'z'
:type galaxy_list: astropy.table.Table
Expand Down Expand Up @@ -323,7 +327,8 @@ def preprocess_members(self, cluster_list, members_list, galaxy_list):
return members_list

def set_cosmo(self):
"""Set the cosmology in colossus to match the astropy.cosmology instance."""
"""Set the cosmology in colossus to match the astropy.cosmology
instance."""
params = dict(
flat=(self.cosmo.Ok0 == 0.0),
H0=self.cosmo.H0.value,
Expand Down
Loading

0 comments on commit 94bf863

Please sign in to comment.