Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
camposandro committed Nov 8, 2024
1 parent e772386 commit 98bc347
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/hats/catalog/healpix_dataset/healpix_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ def filter_by_moc(self, moc: MOC) -> Self:
Returns:
A new catalog with only the pixels that overlap with the moc. Note that we reset the total_rows
to 0, as updating would require a scan over the new pixel sizes."""
if len(self.pixel_tree) == 0:
raise ValueError("Cannot filter empty catalog")
filtered_tree = filter_by_moc(self.pixel_tree, moc)
filtered_moc = self.moc.intersection(moc) if self.moc is not None else None
filtered_catalog_info = self.catalog_info.copy_and_update(total_rows=0)
Expand Down
2 changes: 2 additions & 0 deletions src/hats/pixel_tree/moc_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def filter_by_moc(
Returns:
A new PixelTree object with only the pixels from the input tree that overlap with the moc.
"""
if len(tree) == 0:
return tree
moc_ranges = moc.to_depth29_ranges
# Convert tree intervals to order 29 to match moc intervals
tree_29_ranges = tree.tree << (2 * (29 - tree.tree_order))
Expand Down
7 changes: 7 additions & 0 deletions tests/hats/catalog/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import hats.pixel_math.healpix_shim as hp
from hats.catalog import Catalog, PartitionInfo, TableProperties
from hats.catalog.healpix_dataset.healpix_dataset import HealpixDataset
from hats.io import paths
from hats.io.file_io import read_fits_image
from hats.loaders import read_hats
Expand Down Expand Up @@ -147,6 +148,12 @@ def test_max_coverage_order(small_sky_order1_catalog):
)


def test_max_coverage_order_empty_catalog(catalog_info):
empty_catalog = HealpixDataset(catalog_info, PixelTree.from_healpix([]))
with pytest.raises(ValueError, match="empty catalog"):
empty_catalog.get_max_coverage_order()


def test_cone_filter(small_sky_order1_catalog):
ra = 315
dec = -66.443
Expand Down
10 changes: 10 additions & 0 deletions tests/hats/pixel_tree/test_moc_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from hats.pixel_math import HealpixPixel
from hats.pixel_tree.moc_filter import filter_by_moc
from hats.pixel_tree.pixel_tree import PixelTree


def test_moc_filter(pixel_tree_2):
Expand Down Expand Up @@ -43,6 +44,15 @@ def test_moc_filter_higher_order(pixel_tree_2):
]


def test_moc_filter_with_empty_tree():
orders = np.array([1, 1, 2])
pixels = np.array([45, 46, 128])
moc = MOC.from_healpix_cells(pixels, orders, 2)
empty_tree = PixelTree.from_healpix([])
filtered_tree = filter_by_moc(empty_tree, moc)
assert filtered_tree.get_healpix_pixels() == []


def test_moc_filter_empty_moc(pixel_tree_2):
orders = np.array([])
pixels = np.array([])
Expand Down

0 comments on commit 98bc347

Please sign in to comment.