Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add coordinate_system parameter to compute_tangential_and_cross_components and _compute_lensing_angles functions #624

Merged
merged 22 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ec3c8fc
feat: Add coordinate_system parameter to compute_tangential_and_cross…
Jul 15, 2024
86972dc
Update documentation to account for new coordinate_system option
Jul 15, 2024
10a6236
feat: Add coordinate_system conversion test to test_compute_lensing_a…
Jul 15, 2024
a2d4989
Add coordinate_system conversion test to test_compute_lensing_angles_…
Jul 15, 2024
fd3169b
Set coordinate_system option during creation of GalaxyCluster object
Jul 15, 2024
ca81e1f
Merge main into issue/613/ellipticity_coord_system
Jul 16, 2024
7cae62e
Removed debuggin print in test_dataops.py
Jul 16, 2024
a6e995a
Fixed typo and set default coordinate_system to "pixel"
Jul 16, 2024
c950229
Added option to choose coordinate system for generated mock catalog
Jul 16, 2024
9bdc6cd
Added ellipticity coordinate system conversion tests to test_galaxycl…
Jul 16, 2024
479f74a
Fixed formatting
Jul 16, 2024
4a1faa6
Updated logic for lensing angle conversion between coordinate systems…
Jul 16, 2024
5493cce
Revert "Updated logic for lensing angle conversion between coordinate…
Jul 17, 2024
2718680
Added coordinate_system validation and extra tests
Jul 17, 2024
d36a771
Changed example notebook to document new coordinate_system option
Jul 17, 2024
c84b83b
Fixed all the documentation for coordinate_system and changed names f…
Jul 18, 2024
d297f3b
Added reference for defitions of coordinate systems
Jul 18, 2024
fd31549
Fixed bug in coordinate system conversion in mock data generation
Jul 18, 2024
61dbec6
Test ValueError raise for coordinate_system in test_mockdata.py
Jul 18, 2024
070548b
Added more explanations about ellipticity coordinate system
Jul 18, 2024
7ff8809
Fixed typo
Jul 18, 2024
ba49faa
uodated tag to 1.12.4
marina-ricci Jul 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clmm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
)
from . import support

__version__ = "1.12.3"
__version__ = "1.12.4"
36 changes: 32 additions & 4 deletions clmm/dataops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Functions to compute polar/azimuthal averages in radial bins"""

import warnings
import numpy as np
import scipy
Expand All @@ -14,6 +15,7 @@
_validate_ra,
_validate_dec,
_validate_is_deltasigma_sigma_c,
_validate_coordinate_system,
)
from ..redshift import (
_integ_pzfuncs,
Expand All @@ -29,6 +31,7 @@ def compute_tangential_and_cross_components(
dec_source,
shear1,
shear2,
coordinate_system="euclidean",
geometry="curve",
is_deltasigma=False,
sigma_c=None,
Expand Down Expand Up @@ -95,6 +98,10 @@ def compute_tangential_and_cross_components(
The measured shear (or reduced shear or ellipticity) of the source galaxies
shear2: array
The measured shear (or reduced shear or ellipticity) of the source galaxies
coordinate_system: str, optional
Coordinate system of the ellipticity components. Must be either 'celestial' or
euclidean'. See https://doi.org/10.48550/arXiv.1407.7676 section 5.1 for more details.
Default is 'euclidean'.
geometry: str, optional
Sky geometry to compute angular separation.
Options are curve (uses astropy) or flat.
Expand Down Expand Up @@ -128,6 +135,7 @@ def compute_tangential_and_cross_components(
validate_argument(locals(), "shear2", "float_array")
validate_argument(locals(), "geometry", str)
validate_argument(locals(), "sigma_c", "float_array", none_ok=True)
_validate_coordinate_system(locals(), "coordinate_system", str)
ra_source_, dec_source_, shear1_, shear2_ = arguments_consistency(
[ra_source, dec_source, shear1, shear2],
names=("Ra", "Dec", "Shear1", "Shear2"),
Expand All @@ -147,9 +155,13 @@ def compute_tangential_and_cross_components(
)
# Compute the lensing angles
if geometry == "flat":
angsep, phi = _compute_lensing_angles_flatsky(ra_lens, dec_lens, ra_source_, dec_source_)
angsep, phi = _compute_lensing_angles_flatsky(
ra_lens, dec_lens, ra_source_, dec_source_, coordinate_system=coordinate_system
)
elif geometry == "curve":
angsep, phi = _compute_lensing_angles_astropy(ra_lens, dec_lens, ra_source_, dec_source_)
angsep, phi = _compute_lensing_angles_astropy(
ra_lens, dec_lens, ra_source_, dec_source_, coordinate_system=coordinate_system
)
else:
raise NotImplementedError(f"Sky geometry {geometry} is not currently supported")
# Compute the tangential and cross shears
Expand Down Expand Up @@ -329,7 +341,9 @@ def compute_galaxy_weights(
return w_ls


def _compute_lensing_angles_flatsky(ra_lens, dec_lens, ra_source_list, dec_source_list):
def _compute_lensing_angles_flatsky(
ra_lens, dec_lens, ra_source_list, dec_source_list, coordinate_system="euclidean"
):
r"""Compute the angular separation between the lens and the source and the azimuthal
angle from the lens to the source in radians.

Expand All @@ -353,6 +367,10 @@ def _compute_lensing_angles_flatsky(ra_lens, dec_lens, ra_source_list, dec_sourc
Right ascensions of each source galaxy in degrees
dec_source_list: array
Declinations of each source galaxy in degrees
coordinate_system: str, optional
Coordinate system of the ellipticity components. Must be either 'celestial' or
euclidean'. See https://doi.org/10.48550/arXiv.1407.7676 section 5.1 for more details.
Default is 'euclidean'.

Returns
-------
Expand All @@ -376,12 +394,16 @@ def _compute_lensing_angles_flatsky(ra_lens, dec_lens, ra_source_list, dec_sourc
phi[angsep == 0.0] = 0.0
else:
phi = 0.0 if angsep == 0.0 else phi
if coordinate_system == "celestial":
phi = np.pi - phi
if np.any(angsep > np.pi / 180.0):
warnings.warn("Using the flat-sky approximation with separations >1 deg may be inaccurate")
return angsep, phi


def _compute_lensing_angles_astropy(ra_lens, dec_lens, ra_source_list, dec_source_list):
def _compute_lensing_angles_astropy(
ra_lens, dec_lens, ra_source_list, dec_source_list, coordinate_system="euclidean"
):
r"""Compute the angular separation between the lens and the source and the azimuthal
angle from the lens to the source in radians.

Expand All @@ -395,6 +417,10 @@ def _compute_lensing_angles_astropy(ra_lens, dec_lens, ra_source_list, dec_sourc
Right ascensions of each source galaxy in degrees
dec_source_list: array
Declinations of each source galaxy in degrees
coordinate_system: str, optional
Coordinate system of the ellipticity components. Must be either 'celestial' or
euclidean'. See https://doi.org/10.48550/arXiv.1407.7676 section 5.1 for more details.
Default is 'euclidean'.

Returns
-------
Expand All @@ -414,6 +440,8 @@ def _compute_lensing_angles_astropy(ra_lens, dec_lens, ra_source_list, dec_sourc
else:
phi -= 2 * np.pi if phi > np.pi else 0
phi = 0 if angsep == 0 else phi
if coordinate_system == "celestial":
phi = np.pi - phi
return angsep, phi


Expand Down
19 changes: 18 additions & 1 deletion clmm/galaxycluster.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""@file galaxycluster.py
The GalaxyCluster class
"""

import pickle
import warnings
from .gcdata import GCData
Expand All @@ -17,6 +18,7 @@
_validate_ra,
_validate_dec,
_draw_random_points_from_tab_distribution,
_validate_coordinate_system,
)


Expand All @@ -35,6 +37,10 @@ class GalaxyCluster:
Redshift of galaxy cluster center
galcat : GCData
Table of background galaxy data containing at least galaxy_id, ra, dec, e1, e2, z
coordinate_system : str, optional
Coordinate system of the ellipticity components. Must be either 'celestial' or
euclidean'. See https://doi.org/10.48550/arXiv.1407.7676 section 5.1 for more details.
Default is 'euclidean'.
validate_input: bool
Validade each input argument
"""
Expand All @@ -51,13 +57,22 @@ def __init__(self, *args, validate_input=True, **kwargs):
self._check_types()
self.set_ra_lower(ra_low=0)

def _add_values(self, unique_id: str, ra: float, dec: float, z: float, galcat: GCData):
def _add_values(
self,
unique_id: str,
ra: float,
dec: float,
z: float,
galcat: GCData,
coordinate_system: str = "euclidean",
):
"""Add values for all attributes"""
self.unique_id = unique_id
self.ra = ra
self.dec = dec
self.z = z
self.galcat = galcat
self.coordinate_system = coordinate_system

def _check_types(self):
"""Check types of all attributes"""
Expand All @@ -66,6 +81,7 @@ def _check_types(self):
_validate_dec(vars(self), "dec", False)
validate_argument(vars(self), "z", (float, str), argmin=0, eqmin=True)
validate_argument(vars(self), "galcat", GCData)
_validate_coordinate_system(vars(self), "coordinate_system", str)
self.unique_id = str(self.unique_id)
self.ra = float(self.ra)
self.dec = float(self.dec)
Expand Down Expand Up @@ -281,6 +297,7 @@ def compute_tangential_and_cross_components(
dec_lens=self.dec,
geometry=geometry,
validate_input=self.validate_input,
coordinate_system=self.coordinate_system,
**cols,
)
if add:
Expand Down
14 changes: 14 additions & 0 deletions clmm/support/mock_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Functions to generate mock source galaxy distributions to demo lensing code"""

import warnings
import numpy as np
from astropy import units as u
Expand All @@ -12,6 +13,7 @@
validate_argument,
_draw_random_points_from_distribution,
gaussian,
_validate_coordinate_system,
)
from ..redshift import distributions as zdist

Expand All @@ -38,6 +40,7 @@ def generate_galaxy_catalog(
ngal_density=None,
pz_bins=101,
pzpdf_type="shared_bins",
coordinate_system="euclidean",
validate_input=True,
):
r"""Generates a mock dataset of sheared background galaxies.
Expand Down Expand Up @@ -156,6 +159,11 @@ def generate_galaxy_catalog(
The number density of galaxies (in galaxies per square arcminute, from z=0 to z=infty).
The number of galaxies to be drawn will then depend on the redshift distribution and
user-defined redshift range. If specified, the ngals argument will be ignored.
coordinate_system : str, optional
caioolivv marked this conversation as resolved.
Show resolved Hide resolved
Coordinate system of the ellipticity components. Must be either 'celestial' or
euclidean'. See https://doi.org/10.48550/arXiv.1407.7676 section 5.1 for more details.
Default is 'euclidean'.

validate_input: bool
Validade each input argument

Expand Down Expand Up @@ -208,6 +216,7 @@ def generate_galaxy_catalog(
validate_argument(locals(), "ngals", float, none_ok=True)
validate_argument(locals(), "ngal_density", float, none_ok=True)
validate_argument(locals(), "pz_bins", (int, "array"))
_validate_coordinate_system(locals(), "coordinate_system", str)

if zsrc_min is None:
zsrc_min = cluster_z + 0.1
Expand All @@ -231,6 +240,7 @@ def generate_galaxy_catalog(
"pz_bins": pz_bins,
"field_size": field_size,
"pzpdf_type": pzpdf_type,
"coordinate_system": coordinate_system,
}

if ngals is None and ngal_density is None:
Expand Down Expand Up @@ -361,6 +371,7 @@ def _generate_galaxy_catalog(
photoz_sigma_unscaled=None,
pz_bins=101,
pzpdf_type="shared_bins",
coordinate_system="euclidean",
field_size=None,
):
"""A private function that skips the sanity checks on derived properties. This
Expand Down Expand Up @@ -453,6 +464,9 @@ def _generate_galaxy_catalog(
cols += ["pzbins"]
cols += ["pzpdf"]

if coordinate_system == "celestial":
galaxy_catalog["e2"] *= -1 # flip e2 to match the celestial coordinate system

return galaxy_catalog[cols]


Expand Down
1 change: 1 addition & 0 deletions clmm/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
_validate_ra,
_validate_dec,
_validate_is_deltasigma_sigma_c,
_validate_coordinate_system,
)

from .units import (
Expand Down
21 changes: 21 additions & 0 deletions clmm/utils/validation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""General utility functions that are used in multiple modules"""

import numpy as np
from ..constants import Constants as const

Expand Down Expand Up @@ -224,3 +225,23 @@ def _validate_is_deltasigma_sigma_c(is_deltasigma, sigma_c):
raise TypeError("sigma_c (=None) must be provided when is_deltasigma=True")
if not is_deltasigma and sigma_c is not None:
raise TypeError(f"sigma_c (={sigma_c}) must be None when is_deltasigma=False")


def _validate_coordinate_system(loc, coordinate_system, valid_type):
r"""Validate the coordinate system.

Parameters
----------
loc: dict
Dictionary with all input arguments. Should be locals().
coordinate_system: str
Coordinate system of the ellipticity components. Must be either 'celestial' or 'euclidean'.
valid_type: str, type
Valid types for argument, options are object types, list/tuple of types, or:

* 'int_array' - interger, interger array
* 'float_array' - float, float array
"""
validate_argument(loc, coordinate_system, valid_type)
if loc[coordinate_system] not in ["celestial", "euclidean"]:
raise ValueError(f"{coordinate_system} must be 'celestial' or 'euclidean'.")
22 changes: 17 additions & 5 deletions examples/demo_dataops_functionality.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Define toy cluster parameters for mock data generation"
"Define toy cluster parameters for mock data generation. Notice that here we set the coordinate system for the ellipticity components as 'euclidean'. See https://doi.org/10.48550/arXiv.1407.7676 section 5.1 for more details."
]
},
{
Expand Down Expand Up @@ -139,6 +139,7 @@
" ngals=ngals,\n",
" cluster_ra=cluster_ra,\n",
" cluster_dec=cluster_dec,\n",
" coordinate_system=\"euclidean\",\n",
")"
]
},
Expand All @@ -155,7 +156,9 @@
"metadata": {},
"outputs": [],
"source": [
"cl = GalaxyCluster(cluster_id, cluster_ra, cluster_dec, cluster_z, noisy_data_z)"
"cl = GalaxyCluster(\n",
" cluster_id, cluster_ra, cluster_dec, cluster_z, noisy_data_z, coordinate_system=\"euclidean\"\n",
")"
]
},
{
Expand Down Expand Up @@ -252,6 +255,13 @@
"### 3.1 Compute angular separation, cross and tangential shear for each source galaxy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To keep things consistent, you also need to set the coordinate system to the same option."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -265,6 +275,7 @@
" dec_source=cl.galcat[\"dec\"],\n",
" shear1=cl.galcat[\"e1\"],\n",
" shear2=cl.galcat[\"e2\"],\n",
" coordinate_system=\"euclidean\",\n",
")"
]
},
Expand Down Expand Up @@ -550,7 +561,7 @@
"metadata": {},
"outputs": [],
"source": [
"cl.make_radial_profile(\"Mpc\", cosmo=cosmo, gal_ids_in_bins=True);"
"cl.make_radial_profile(\"Mpc\", cosmo=cosmo, gal_ids_in_bins=True)"
]
},
{
Expand Down Expand Up @@ -704,6 +715,7 @@
" tan_component_out='DeltaSigma_tan', cross_component_out='DeltaSigma_cross',\n",
" table_name='DeltaSigma_profile').pprint(max_width=-1)\n",
"\"\"\"\n",
"\n",
"cl.make_radial_profile(\n",
" \"Mpc\",\n",
" cosmo=cosmo,\n",
Expand All @@ -713,7 +725,7 @@
" cross_component_out=\"DeltaSigma_cross\",\n",
" table_name=\"DeltaSigma_profile\",\n",
" use_weights=True,\n",
");"
")"
]
},
{
Expand Down Expand Up @@ -796,7 +808,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down
Loading