-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #216 from astronomy-commons/sandro/catalog-factory…
…-method Dataset factory method
- Loading branch information
Showing
14 changed files
with
147 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
"""High-level namespace, hipscat""" | ||
|
||
from . import catalog, io, pixel_math | ||
from . import catalog, inspection, io, pixel_math | ||
from .loaders import read_from_hipscat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .read_from_hipscat import read_from_hipscat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Type | ||
|
||
from hipscat import io | ||
from hipscat.catalog import AssociationCatalog, Catalog, CatalogType, Dataset, MarginCatalog | ||
from hipscat.catalog.dataset import BaseCatalogInfo | ||
from hipscat.catalog.index.index_catalog import IndexCatalog | ||
|
||
CATALOG_TYPE_TO_CLASS = { | ||
CatalogType.OBJECT: Catalog, | ||
CatalogType.SOURCE: Catalog, | ||
CatalogType.ASSOCIATION: AssociationCatalog, | ||
CatalogType.INDEX: IndexCatalog, | ||
CatalogType.MARGIN: MarginCatalog, | ||
} | ||
|
||
|
||
def read_from_hipscat( | ||
catalog_path: str, | ||
catalog_type: CatalogType | None = None, | ||
storage_options: dict | None = None, | ||
) -> Dataset: | ||
"""Reads a HiPSCat Catalog from a HiPSCat directory | ||
Args: | ||
catalog_path (str): path to the root directory of the catalog | ||
catalog_type (CatalogType): Default `None`. By default, the type of the catalog is loaded | ||
from the catalog info and the corresponding object type is returned. Python's type hints | ||
cannot allow a return type specified by a loaded value, so to use the correct return | ||
type for type checking, the type of the catalog can be specified here. Use by specifying | ||
the hipscat class for that catalog. | ||
storage_options (dict): dictionary that contains abstract filesystem credentials | ||
Returns: | ||
The initialized catalog object | ||
""" | ||
catalog_type_to_use = ( | ||
_read_dataset_class_from_metadata(catalog_path, storage_options=storage_options) | ||
if catalog_type is None | ||
else catalog_type | ||
) | ||
loader = _get_loader_from_catalog_type(catalog_type_to_use) | ||
return loader.read_from_hipscat(catalog_path) | ||
|
||
|
||
def _read_dataset_class_from_metadata( | ||
catalog_base_path: str, storage_options: dict | None = None | ||
) -> CatalogType: | ||
catalog_base_dir = io.file_io.get_file_pointer_from_path(catalog_base_path) | ||
catalog_info_path = io.paths.get_catalog_info_pointer(catalog_base_dir) | ||
catalog_info = BaseCatalogInfo.read_from_metadata_file(catalog_info_path, storage_options=storage_options) | ||
return catalog_info.catalog_type | ||
|
||
|
||
def _get_loader_from_catalog_type(catalog_type: CatalogType) -> Type[Dataset]: | ||
if catalog_type not in CATALOG_TYPE_TO_CLASS: | ||
raise NotImplementedError(f"Cannot load catalog of type {catalog_type}") | ||
return CATALOG_TYPE_TO_CLASS[catalog_type] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import pytest | ||
|
||
from hipscat.catalog import CatalogType | ||
from hipscat.loaders import read_from_hipscat | ||
|
||
|
||
def test_read_from_hipscat_wrong_catalog_type(small_sky_dir): | ||
with pytest.raises(ValueError, match="must have type"): | ||
read_from_hipscat(small_sky_dir, catalog_type=CatalogType.ASSOCIATION) | ||
with pytest.raises(NotImplementedError, match="load catalog of type"): | ||
read_from_hipscat(small_sky_dir, catalog_type="unknown") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.