11"""Container class to hold catalog metadata and partition iteration"""
22from __future__ import annotations
3- from typing import List , Tuple , Union
43
54import dataclasses
5+ from typing import List
66
77import healpy as hp
88import numpy as np
9- import pandas as pd
10-
119from typing_extensions import TypeAlias
10+
1211from hipscat .catalog .catalog_info import CatalogInfo
1312from hipscat .catalog .catalog_type import CatalogType
14- from hipscat .catalog .dataset .dataset import Dataset
15- from hipscat .catalog .partition_info import PartitionInfo
16- from hipscat .io import FilePointer , file_io , paths
13+ from hipscat .catalog .healpix_dataset .healpix_dataset import HealpixDataset , PixelInputTypes
1714from hipscat .pixel_math import HealpixPixel
1815from hipscat .pixel_math .cone_filter import filter_pixels_by_cone
1916from hipscat .pixel_tree .pixel_node_type import PixelNodeType
20- from hipscat .pixel_tree .pixel_tree import PixelTree
21- from hipscat .pixel_tree .pixel_tree_builder import PixelTreeBuilder
2217
2318
24- class Catalog (Dataset ):
19+ class Catalog (HealpixDataset ):
2520 """A HiPSCat Catalog with data stored in a HEALPix Hive partitioned structure
2621
2722 Catalogs of this type are partitioned spatially, contain `partition_info` metadata specifying
2823 the pixels in Catalog, and on disk conform to the parquet partitioning structure
2924 `Norder=/Dir=/Npix=.parquet`
3025 """
3126
32- PixelInputTypes = Union [pd .DataFrame , PartitionInfo , PixelTree , List [HealpixPixel ]]
3327 HIPS_CATALOG_TYPES = [CatalogType .OBJECT , CatalogType .SOURCE , CatalogType .MARGIN ]
3428
3529 # Update CatalogInfoClass, used to check if the catalog_info is the correct type, and
@@ -42,7 +36,7 @@ def __init__(
4236 catalog_info : CatalogInfoClass ,
4337 pixels : PixelInputTypes ,
4438 catalog_path : str = None ,
45- storage_options : dict = None
39+ storage_options : dict = None ,
4640 ) -> None :
4741 """Initializes a Catalog
4842
@@ -60,76 +54,7 @@ def __init__(
6054 f"Catalog info `catalog_type` must be one of "
6155 f"{ ', ' .join ([t .value for t in self .HIPS_CATALOG_TYPES ])} "
6256 )
63- super ().__init__ (catalog_info , catalog_path , storage_options )
64- self .partition_info = self ._get_partition_info_from_pixels (pixels )
65- self .pixel_tree = self ._get_pixel_tree_from_pixels (pixels )
66-
67- @staticmethod
68- def _get_partition_info_from_pixels (pixels : PixelInputTypes ) -> PartitionInfo :
69- if isinstance (pixels , PartitionInfo ):
70- return pixels
71- if isinstance (pixels , pd .DataFrame ):
72- return PartitionInfo (pixels )
73- if isinstance (pixels , PixelTree ):
74- return PartitionInfo .from_healpix (
75- [
76- HealpixPixel (node .hp_order , node .hp_pixel )
77- for node in pixels .root_pixel .get_all_leaf_descendants ()
78- ]
79- )
80- if pd .api .types .is_list_like (pixels ):
81- return PartitionInfo .from_healpix (pixels )
82- raise TypeError ("Pixels must be of type PartitionInfo, Dataframe, PixelTree, or List[HealpixPixel]" )
83-
84- @staticmethod
85- def _get_pixel_tree_from_pixels (pixels : PixelInputTypes ) -> PixelTree :
86- if isinstance (pixels , PartitionInfo ):
87- return PixelTreeBuilder .from_partition_info_df (pixels .data_frame )
88- if isinstance (pixels , pd .DataFrame ):
89- return PixelTreeBuilder .from_partition_info_df (pixels )
90- if isinstance (pixels , PixelTree ):
91- return pixels
92- if pd .api .types .is_list_like (pixels ):
93- return PixelTreeBuilder .from_healpix (pixels )
94- raise TypeError ("Pixels must be of type PartitionInfo, Dataframe, PixelTree, or List[HealpixPixel]" )
95-
96- def get_pixels (self ):
97- """Get all healpix pixels that are contained in the catalog
98-
99- Returns:
100- data frame with per-pixel data.
101-
102- The data frame contains the following columns:
103-
104- - order: order of the destination pixel
105- - pixel: pixel number *at the above order*
106- - num_objects: the number of rows in the pixel's partition
107- """
108- return self .partition_info .data_frame
109-
110- def get_healpix_pixels (self ) -> List [HealpixPixel ]:
111- """Get healpix pixel objects for all pixels contained in the catalog.
112-
113- Returns:
114- List of HealpixPixel
115- """
116- return self .partition_info .get_healpix_pixels ()
117-
118- @classmethod
119- def _read_args (
120- cls , catalog_base_dir : FilePointer , storage_options : dict = None
121- ) -> Tuple [CatalogInfoClass , PartitionInfo ]:
122- args = super ()._read_args (catalog_base_dir , storage_options = storage_options )
123- partition_info_file = paths .get_partition_info_pointer (catalog_base_dir )
124- partition_info = PartitionInfo .read_from_file (partition_info_file , storage_options = storage_options )
125- return args + (partition_info ,)
126-
127- @classmethod
128- def _check_files_exist (cls , catalog_base_dir : FilePointer , storage_options : dict = None ):
129- super ()._check_files_exist (catalog_base_dir , storage_options = storage_options )
130- partition_info_file = paths .get_partition_info_pointer (catalog_base_dir )
131- if not file_io .does_file_or_directory_exist (partition_info_file , storage_options = storage_options ):
132- raise FileNotFoundError (f"No partition info found where expected: { str (partition_info_file )} " )
57+ super ().__init__ (catalog_info , pixels , catalog_path , storage_options )
13358
13459 def filter_by_cone (self , ra : float , dec : float , radius : float ) -> Catalog :
13560 """Filter the pixels in the catalog to only include the pixels that overlap with a cone
0 commit comments