Source code for hats.catalog.catalog_collection
from __future__ import annotations
from upath import UPath
from hats.catalog.catalog import Catalog
from hats.catalog.catalog_type import CatalogType
from hats.catalog.dataset.collection_properties import CollectionProperties
from hats.catalog.dataset.table_properties import TableProperties
from hats.io import file_io
from hats.pixel_math import HealpixPixel
[docs]
class CatalogCollection:
"""A collection of HATS Catalog with data stored in a HEALPix Hive partitioned structure
Catalogs of this type are described by a `collection.properties` file which specifies
the underlying main catalog, margin catalog and index catalog paths. These catalogs are
stored at the root of the collection, each in its separate directory::
catalog_collection/
├── main_catalog/
├── margin_catalog/
├── index_catalog/
├── extension_catalog.properties
├── collection.properties
Margin and index catalogs are optional but there could also be multiple of them. The
catalogs used by default are specified in the `collection.properties` file in the
`default_margin` and `default_index` keywords. Extensions are optional too, and each is
described by an `<extension>.properties` file, listed by alias in the `all_extensions` keyword.
That file usually sits at the root of the collection, but it may live elsewhere, e.g.
remotely, and be listed by its absolute path.
"""
def __init__(
self,
collection_path: UPath,
collection_properties: CollectionProperties,
main_catalog: Catalog,
storage_options: dict | None = None,
):
[docs]
self.collection_path = collection_path
[docs]
self.collection_properties = collection_properties
if not isinstance(main_catalog, Catalog):
raise TypeError(f"HATS at {main_catalog.catalog_path} is not of type `Catalog`")
[docs]
self.main_catalog = main_catalog
[docs]
self.storage_options = storage_options
@property
[docs]
def main_catalog_dir(self) -> UPath:
"""Path to the main catalog directory"""
return self.resolve_inner_path(
self.collection_path, self.collection_properties.hats_primary_table_url, self.storage_options
)
@property
[docs]
def all_margins(self) -> list[str] | None:
"""The list of margin catalog names in the collection"""
return self.collection_properties.all_margins
@property
[docs]
def default_margin(self) -> str | None:
"""The name of the default margin"""
return self.collection_properties.default_margin
@property
[docs]
def default_margin_catalog_dir(self) -> UPath | None:
"""Path to the default margin catalog directory"""
if self.default_margin is None:
return None
return self.resolve_inner_path(self.collection_path, self.default_margin, self.storage_options)
@property
[docs]
def all_extensions(self) -> dict[str, str] | None:
"""The mapping of extensions in the collection"""
return self.collection_properties.all_extensions
[docs]
def get_extension_path(self, extension_alias: str) -> UPath:
"""Path to the properties file of an extension in the collection"""
if self.all_extensions is None or extension_alias not in self.all_extensions:
raise ValueError(f"Extension `{extension_alias}` is not specified in all_extensions")
extension_path = self.all_extensions[extension_alias]
return self.resolve_inner_path(self.collection_path, extension_path, self.storage_options)
@property
[docs]
def all_indexes(self) -> dict[str, str] | None:
"""The mapping of indexes in the collection"""
return self.collection_properties.all_indexes
@property
[docs]
def default_index_field(self) -> str | None:
"""The name of the default index field"""
return self.collection_properties.default_index
@property
[docs]
def default_index_catalog_dir(self) -> UPath | None:
"""Path to the default index catalog directory"""
if self.default_index_field is None:
return None
default_index_dir = self.all_indexes[self.default_index_field]
return self.collection_path / default_index_dir
[docs]
def get_index_dir_for_field(self, field_name: str | None = None) -> UPath | None:
"""Path to the field's index catalog directory"""
if field_name is None:
return self.default_index_catalog_dir
if self.all_indexes is None or field_name not in self.all_indexes:
raise ValueError(f"Index for field `{field_name}` is not specified in all_indexes")
index_dir = self.all_indexes[field_name]
return self.resolve_inner_path(self.collection_path, index_dir, self.storage_options)
[docs]
def get_healpix_pixels(self) -> list[HealpixPixel]:
"""The list of HEALPix pixels of the main catalog"""
return self.main_catalog.get_healpix_pixels()
[docs]
def plot_pixels(self, **kwargs):
"""Create a visual map of the pixel density of the collection's main catalog.
Parameters
----------
**kwargs
Additional args to pass to `hats.inspection.visualize_catalog.plot_healpix_map`
"""
return self.main_catalog.plot_pixels(**kwargs)
[docs]
def plot_density(self, **kwargs):
"""Create a visual map of the density of input points of the collection's main catalog.
Parameters
----------
**kwargs
Additional args to pass to `hats.inspection.visualize_catalog.plot_density`
Raises
------
ValueError
if the main catalog is not on disk.
"""
return self.main_catalog.plot_density(**kwargs)
[docs]
def plot_moc(self, **kwargs):
"""Create a visual map of the coverage of the collection's main catalog.
Parameters
----------
**kwargs
Additional args to pass to `hats.inspection.visualize_catalog.plot_moc`
"""
return self.main_catalog.plot_moc(**kwargs)
[docs]
def get_margin_thresholds(self) -> dict[str, float]:
"""Get the margin thresholds for all margin catalogs in the collection.
Returns
-------
dict[str, float]
A dictionary mapping margin catalog names to their threshold values.
Raises
------
ValueError
if a catalog listed as a margin catalog is not configured as a margin catalog.
"""
if self.all_margins is None:
return {}
thresholds = {}
for margin_name in self.all_margins:
margin_path = self.collection_path / margin_name
margin_properties = TableProperties.read_from_dir(margin_path)
if margin_properties.catalog_type != CatalogType.MARGIN:
raise ValueError("Catalog `{margin_name}` is not a margin catalog")
# It should be non-None for a margin catalog
thresholds[margin_name] = margin_properties.margin_threshold
return thresholds
@classmethod
[docs]
def resolve_inner_path(cls, collection_path, path, storage_options: dict | None = None) -> UPath:
"""Convenience method to find the path of a related catalog, either relative or absolute.
Parameters
----------
collection_path: UPath
fully-specified path to the collection root
path: UPath | Path | str
location of the related catalog
storage_options: dict
dictionary of storage options for the connection to the related catalog
Returns
-------
UPath
fully-specified path to the related catalog.
"""
if storage_options is None:
storage_options = {}
path = file_io.get_upath(path, **storage_options)
if "local" not in path.fs.protocol or path.is_absolute():
return path # remote URL or absolute local path - use as-is
collection_path = file_io.get_upath(collection_path)
return collection_path / path