Skip to content

Commit

Permalink
Added coordinate_system validation and extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Caio Lima de Oliveira committed Jul 17, 2024
1 parent 5493cce commit 2718680
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 15 deletions.
3 changes: 3 additions & 0 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 Down Expand Up @@ -132,6 +134,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 Down
2 changes: 2 additions & 0 deletions clmm/galaxycluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
_validate_ra,
_validate_dec,
_draw_random_points_from_tab_distribution,
_validate_coordinate_system,
)


Expand Down Expand Up @@ -79,6 +80,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
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 used for the input data.
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 ["sky", "pixel"]:
raise ValueError(f"{coordinate_system} must be 'sky' or 'pixel'.")
24 changes: 24 additions & 0 deletions tests/test_dataops.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,30 @@ def test_compute_tangential_and_cross_components(modeling_data):
**TOLERANCE,
err_msg="Cross Shear not correct when passing lists",
)
# Test invalid coordinate system name
assert_raises(
ValueError,
da.compute_tangential_and_cross_components,
ra_l,
dec_l,
ra_s,
dec_s + 10.0,
shear1,
shear2,
coordinate_system="crazy",
)
# Test invalid coordinate system type
assert_raises(
ValueError,
da.compute_tangential_and_cross_components,
ra_l,
dec_l,
ra_s,
dec_s + 10.0,
shear1,
shear2,
coordinate_system=1,
)
# Use the cluster method
cluster = clmm.GalaxyCluster(
unique_id="blah", ra=ra_lens, dec=dec_lens, z=z_lens, galcat=gals["ra", "dec", "e1", "e2"]
Expand Down
147 changes: 132 additions & 15 deletions tests/test_galaxycluster.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests for datatype and galaxycluster
"""

import os
import numpy as np
from numpy.testing import assert_raises, assert_equal, assert_allclose, assert_warns
Expand All @@ -13,14 +14,22 @@

def test_initialization():
"""test initialization"""
testdict1 = {"unique_id": "1", "ra": 161.3, "dec": 34.0, "z": 0.3, "galcat": GCData()}
testdict1 = {
"unique_id": "1",
"ra": 161.3,
"dec": 34.0,
"z": 0.3,
"galcat": GCData(),
"coordinate_system": "pixel",
}
cl1 = clmm.GalaxyCluster(**testdict1)

assert_equal(testdict1["unique_id"], cl1.unique_id)
assert_equal(testdict1["ra"], cl1.ra)
assert_equal(testdict1["dec"], cl1.dec)
assert_equal(testdict1["z"], cl1.z)
assert isinstance(cl1.galcat, GCData)
assert_equal(testdict1["coordinate_system"], cl1.coordinate_system)


def test_integrity(): # Converge on name
Expand All @@ -33,43 +42,150 @@ def test_integrity(): # Converge on name

# Test that we get errors when we pass in values outside of the domains
assert_raises(
ValueError, clmm.GalaxyCluster, unique_id=1, ra=-360.3, dec=34.0, z=0.3, galcat=GCData()
ValueError,
clmm.GalaxyCluster,
unique_id=1,
ra=-360.3,
dec=34.0,
z=0.3,
galcat=GCData(),
coordinate_system="pixel",
)
assert_raises(
ValueError, clmm.GalaxyCluster, unique_id=1, ra=360.3, dec=34.0, z=0.3, galcat=GCData()
ValueError,
clmm.GalaxyCluster,
unique_id=1,
ra=360.3,
dec=34.0,
z=0.3,
galcat=GCData(),
coordinate_system="pixel",
)
assert_raises(
ValueError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=95.0, z=0.3, galcat=GCData()
ValueError,
clmm.GalaxyCluster,
unique_id=1,
ra=161.3,
dec=95.0,
z=0.3,
galcat=GCData(),
coordinate_system="pixel",
)
assert_raises(
ValueError,
clmm.GalaxyCluster,
unique_id=1,
ra=161.3,
dec=-95.0,
z=0.3,
galcat=GCData(),
coordinate_system="pixel",
)
assert_raises(
ValueError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=-95.0, z=0.3, galcat=GCData()
ValueError,
clmm.GalaxyCluster,
unique_id=1,
ra=161.3,
dec=34.0,
z=-0.3,
galcat=GCData(),
coordinate_system="pixel",
)
assert_raises(
ValueError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34.0, z=-0.3, galcat=GCData()
ValueError,
clmm.GalaxyCluster,
unique_id=1,
ra=161.3,
dec=34.0,
z=0.3,
galcat=GCData(),
coordinate_system="blah",
)

# Test that inputs are the correct type
assert_raises(
TypeError, clmm.GalaxyCluster, unique_id=None, ra=161.3, dec=34.0, z=0.3, galcat=GCData()
TypeError,
clmm.GalaxyCluster,
unique_id=None,
ra=161.3,
dec=34.0,
z=0.3,
galcat=GCData(),
coordinate_system="pixel",
)
assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34.0, z=0.3, galcat=1)
assert_raises(TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34.0, z=0.3, galcat=[])
assert_raises(
TypeError, clmm.GalaxyCluster, unique_id=1, ra=None, dec=34.0, z=0.3, galcat=GCData()
TypeError,
clmm.GalaxyCluster,
unique_id=1,
ra=161.3,
dec=34.0,
z=0.3,
galcat=1,
coordinate_system="pixel",
)
assert_raises(
TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=None, z=0.3, galcat=GCData()
TypeError,
clmm.GalaxyCluster,
unique_id=1,
ra=161.3,
dec=34.0,
z=0.3,
galcat=[],
coordinate_system="pixel",
)
assert_raises(
TypeError, clmm.GalaxyCluster, unique_id=1, ra=161.3, dec=34.0, z=None, galcat=GCData()
TypeError,
clmm.GalaxyCluster,
unique_id=1,
ra=None,
dec=34.0,
z=0.3,
galcat=GCData(),
coordinate_system="pixel",
)
assert_raises(
TypeError,
clmm.GalaxyCluster,
unique_id=1,
ra=161.3,
dec=None,
z=0.3,
galcat=GCData(),
coordinate_system="pixel",
)
assert_raises(
TypeError,
clmm.GalaxyCluster,
unique_id=1,
ra=161.3,
dec=34.0,
z=None,
galcat=GCData(),
coordinate_system="pixel",
)
assert_raises(
TypeError,
clmm.GalaxyCluster,
unique_id=1,
ra=161.3,
dec=34.0,
z=0.3,
galcat=None,
coordinate_system=2,
)

# Test that id can support numbers and strings
assert isinstance(
clmm.GalaxyCluster(unique_id=1, ra=161.3, dec=34.0, z=0.3, galcat=GCData()).unique_id, str
clmm.GalaxyCluster(
unique_id=1, ra=161.3, dec=34.0, z=0.3, galcat=GCData(), coordinate_system="pixel"
).unique_id,
str,
)
assert isinstance(
clmm.GalaxyCluster(unique_id="1", ra=161.3, dec=34.0, z=0.3, galcat=GCData()).unique_id, str
clmm.GalaxyCluster(
unique_id="1", ra=161.3, dec=34.0, z=0.3, galcat=GCData(), coordinate_system="pixel"
).unique_id,
str,
)

# Test that ra/dec/z can be converted from int/str to float if needed
Expand Down Expand Up @@ -394,7 +510,8 @@ def test_coordinate_system():
dec=dec_lens,
z=z_lens,
galcat=GCData(
[ra_source, dec_source, shear1, shear2_pixel, z_src], names=("ra", "dec", "e1", "e2", "z")
[ra_source, dec_source, shear1, shear2_pixel, z_src],
names=("ra", "dec", "e1", "e2", "z"),
),
coordinate_system="pixel",
)
Expand Down

0 comments on commit 2718680

Please sign in to comment.