diff --git a/src/hats/catalog/healpix_dataset/healpix_dataset.py b/src/hats/catalog/healpix_dataset/healpix_dataset.py index da1d6282..b5971717 100644 --- a/src/hats/catalog/healpix_dataset/healpix_dataset.py +++ b/src/hats/catalog/healpix_dataset/healpix_dataset.py @@ -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) diff --git a/src/hats/pixel_tree/moc_filter.py b/src/hats/pixel_tree/moc_filter.py index 56183773..79ddc371 100644 --- a/src/hats/pixel_tree/moc_filter.py +++ b/src/hats/pixel_tree/moc_filter.py @@ -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)) diff --git a/tests/hats/catalog/test_catalog.py b/tests/hats/catalog/test_catalog.py index efc398df..988e04a8 100644 --- a/tests/hats/catalog/test_catalog.py +++ b/tests/hats/catalog/test_catalog.py @@ -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 @@ -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 diff --git a/tests/hats/pixel_tree/test_moc_filter.py b/tests/hats/pixel_tree/test_moc_filter.py index dc7da579..5b5fefe0 100644 --- a/tests/hats/pixel_tree/test_moc_filter.py +++ b/tests/hats/pixel_tree/test_moc_filter.py @@ -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): @@ -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([])