Skip to content

Commit

Permalink
specify sign of columns when defining maps
Browse files Browse the repository at this point in the history
  • Loading branch information
ntessore committed Nov 30, 2023
1 parent bad37eb commit 45359fc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
4 changes: 2 additions & 2 deletions examples/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@
"* A position map (P) for angular clustering and galaxy-galaxy lensing;\n",
"* A shear map (G) for cosmic shear and galaxy-galaxy lensing.\n",
"\n",
"We set that the shear maps should flip the sign of the \"G2\" column (`conjugate=True`)."
"We set that the shear maps should flip the sign of the \"G2\" column."
]
},
{
Expand All @@ -473,7 +473,7 @@
"\n",
"maps = {\n",
" 'P': PositionMap(nside, *lonlat),\n",
" 'G': ShearMap(nside, *lonlat, 'G1', 'G2', 'WEIGHT', conjugate=True),\n",
" 'G': ShearMap(nside, *lonlat, 'G1', '-G2', 'WEIGHT'),\n",
"}"
]
},
Expand Down
15 changes: 12 additions & 3 deletions heracles/catalog/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,20 @@ def __init__(self, data: Mapping) -> None:
v.flags.writeable = False
self._update()

def _getdata(self, key):
"""
Return data for one single column. Supports negating columns
(``-COLUMN``), possibly more in the future.
"""
if key[:1] == "-":
return -self._data[key[1:]]
return self._data[key]

def __getitem__(self, col):
"""Return one or more columns without checking."""
if isinstance(col, (list, tuple)):
return tuple(self._data[c] for c in col)
return self._data[col]
return tuple(self._getdata(c) for c in col)
return self._getdata(col)

def __len__(self):
"""Number of columns in the page."""
Expand Down Expand Up @@ -89,7 +98,7 @@ def get(self, *col):
"""Return one or more columns with checking."""
val = []
for c in col:
v = self._data[c]
v = self._getdata(c)
if np.any(np.isnan(v)):
msg = f'invalid values in column "{c}"'
raise ValueError(msg)
Expand Down
19 changes: 0 additions & 19 deletions heracles/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,6 @@ class ComplexMap(HealpixMap, NormalizableMap, RandomizableMap):
Complex maps can have non-zero spin weight, set using the ``spin=``
parameter.
Can optionally flip the sign of the second shear component,
depending on the ``conjugate`` property.
"""

def __init__(
Expand All @@ -470,15 +467,13 @@ def __init__(
weight: t.Optional[str] = None,
*,
spin: int = 0,
conjugate: bool = False,
normalize: bool = True,
randomize: bool = False,
rng: t.Optional[np.random.Generator] = None,
) -> None:
"""Create a new shear map."""

self._spin: int = spin
self._conjugate: bool = conjugate
super().__init__(
columns=(lon, lat, real, imag, weight),
nside=nside,
Expand All @@ -497,24 +492,13 @@ def spin(self, spin: int) -> None:
"""Set the spin weight."""
self._spin = spin

@property
def conjugate(self) -> bool:
"""Flag to conjugate shear maps."""
return self._conjugate

@conjugate.setter
def conjugate(self, conjugate: bool) -> None:
"""Set the conjugate flag."""
self._conjugate = conjugate

def __call__(self, catalog: "Catalog") -> MapGenerator:
"""Map shears from catalogue to HEALPix map."""

# get the column definition of the catalogue
*col, wcol = self.columns

# get the map properties
conjugate = self.conjugate
randomize = self.randomize

# number of pixels for nside
Expand Down Expand Up @@ -544,9 +528,6 @@ def mapper(page: "CatalogPage") -> None:
else:
w = page.get(wcol)

if conjugate:
im = -im

if randomize:
a = self.rng.uniform(0.0, 2 * np.pi, size=page.size)
r = np.hypot(re, im)
Expand Down
5 changes: 3 additions & 2 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def _pages(self, selection):
def test_catalog_page():
from heracles.catalog import CatalogPage

a = [1.0, 2.0, 3.0, 4.0]
b = [5.0, 6.0, 7.0, 8.0]
a = np.array([1.0, 2.0, 3.0, 4.0])
b = np.array([5.0, 6.0, 7.0, 8.0])

page = CatalogPage({"a": a, "b": b})

Expand All @@ -54,6 +54,7 @@ def test_catalog_page():
npt.assert_array_equal(page["b"], b)
npt.assert_array_equal(page["a", "b"], [a, b])
npt.assert_array_equal(page[["a", "b"]], [a, b])
npt.assert_array_equal(page[["a", "-b"]], [a, -b])

# test names attribute
assert page.names == ["a", "b"]
Expand Down

0 comments on commit 45359fc

Please sign in to comment.