Skip to content

Commit

Permalink
Merge pull request #224 from astronomy-commons/issue/189/remove-slow-…
Browse files Browse the repository at this point in the history
…wrapping-call

Remove slow right ascension wrapping call
  • Loading branch information
camposandro authored Feb 28, 2024
2 parents 29fb459 + fdb7022 commit de86705
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 29 deletions.
2 changes: 1 addition & 1 deletion benchmarks/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def time_test_cone_filter_multiple_order():
)
pixels = [HealpixPixel(6, 30), HealpixPixel(7, 124), HealpixPixel(7, 5000)]
catalog = Catalog(catalog_info, pixels)
filtered_catalog = catalog.filter_by_cone(47.1, 6, 30)
filtered_catalog = catalog.filter_by_cone(47.1, 6, 30 * 3600)
assert filtered_catalog.get_healpix_pixels() == [HealpixPixel(6, 30), HealpixPixel(7, 124)]


Expand Down
4 changes: 2 additions & 2 deletions src/hipscat/catalog/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from hipscat.catalog.catalog_type import CatalogType
from hipscat.catalog.healpix_dataset.healpix_dataset import HealpixDataset, PixelInputTypes
from hipscat.pixel_math import HealpixPixel
from hipscat.pixel_math.box_filter import filter_pixels_by_box, wrap_ra_values
from hipscat.pixel_math.box_filter import filter_pixels_by_box, wrap_ra_angles
from hipscat.pixel_math.cone_filter import filter_pixels_by_cone
from hipscat.pixel_math.polygon_filter import (
CartesianCoordinates,
Expand Down Expand Up @@ -96,7 +96,7 @@ def filter_by_box(
Returns:
A new catalog with only the pixels that overlap with the specified region
"""
ra = wrap_ra_values(ra)
ra = tuple(wrap_ra_angles(ra)) if ra else None
validate_box_search(ra, dec)
return self.filter_from_pixel_list(filter_pixels_by_box(self.pixel_tree, ra, dec))

Expand Down
27 changes: 6 additions & 21 deletions src/hipscat/pixel_math/box_filter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from __future__ import annotations

from typing import List, Tuple
from typing import Iterable, List, Tuple

import astropy.units as u
import healpy as hp
import numpy as np
from astropy.coordinates import Angle

from hipscat.pixel_math import HealpixPixel
from hipscat.pixel_math.filter import get_filtered_pixel_list
Expand Down Expand Up @@ -51,29 +49,16 @@ def filter_pixels_by_box(
return result_pixels


def wrap_ra_values(ra: Tuple[float, float] | None) -> Tuple[float, float] | None:
"""Wraps right ascension values to the [0,360] degree range.
Args:
ra (Tuple[float, float]): The right ascension values, in degrees
Returns:
The right ascension values wrapped to the [0,360] degree range,
or None if they do not exist.
"""
return tuple(wrap_angles(list(ra))) if ra else None


def wrap_angles(ra: List[float]) -> List[float]:
def wrap_ra_angles(ra: np.ndarray | Iterable | int | float) -> np.ndarray:
"""Wraps angles to the [0,360] degree range.
Arguments:
ra (List[float]): List of right ascension values
Args:
ra (ndarray | Iterable | int | float): Right ascension values, in degrees
Returns:
A list of right ascension values, wrapped to the [0,360] degree range.
A numpy array of right ascension values, wrapped to the [0,360] degree range.
"""
return Angle(ra, u.deg).wrap_at(360 * u.deg).degree
return np.asarray(ra, dtype=float) % 360


def _generate_ra_strip_pixel_tree(ra_range: Tuple[float, float], order: int) -> PixelTree:
Expand Down
10 changes: 5 additions & 5 deletions src/hipscat/pixel_math/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ValidatorsErrors(str, Enum):
def validate_radius(radius_arcsec: float):
"""Validates that a cone search radius is positive
Arguments:
Args:
radius_arcsec (float): The cone radius, in arcseconds
Raises:
Expand All @@ -33,7 +33,7 @@ def validate_radius(radius_arcsec: float):
def validate_declination_values(dec: float | List[float]):
"""Validates that declination values are in the [-90,90] degree range
Arguments:
Args:
dec (float | List[float]): The declination values to be validated
Raises:
Expand All @@ -49,7 +49,7 @@ def validate_polygon(vertices: np.ndarray):
"""Checks if the polygon contain a minimum of three vertices, that they are
unique and that the polygon does not fall on a great circle.
Arguments:
Args:
vertices (np.ndarray): The polygon vertices, in cartesian coordinates
Raises:
Expand All @@ -67,7 +67,7 @@ def is_polygon_degenerate(vertices: np.ndarray) -> bool:
"""Checks if all the vertices of the polygon are contained in a same plane.
If the plane intersects the center of the sphere, the polygon is degenerate.
Arguments:
Args:
vertices (np.ndarray): The polygon vertices, in cartesian coordinates
Returns:
Expand Down Expand Up @@ -97,7 +97,7 @@ def validate_box_search(ra: Tuple[float, float] | None, dec: Tuple[float, float]
- Declination values, if existing, must be in ascending order
- Declination values, if existing, must be in the [-90,90] degree range
Arguments:
Args:
ra (Tuple[float, float]): Right ascension range, in degrees
dec (Tuple[float, float]): Declination range, in degrees
"""
Expand Down

0 comments on commit de86705

Please sign in to comment.