Skip to content

Commit

Permalink
Drop component support and nomenclature from new query interface.
Browse files Browse the repository at this point in the history
Since getting a component DatasetRef from a parent one is just a method
call, there's no need for this complexity in the query system.
  • Loading branch information
TallJimbo committed Jan 9, 2024
1 parent c68b78a commit 04dc549
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 47 deletions.
34 changes: 10 additions & 24 deletions python/lsst/daf/butler/_query_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"DataCoordinateQueryResults",
"DatasetQueryResults",
"DimensionRecordQueryResults",
"ParentDatasetQueryResults",
"SingleTypeDatasetQueryResults",
)

from abc import abstractmethod
from collections.abc import Iterable, Iterator, Sequence
from collections.abc import Iterable, Iterator
from contextlib import AbstractContextManager
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -422,15 +422,14 @@ class DatasetQueryResults(Iterable[DatasetRef]):
"""

@abstractmethod
def by_parent_dataset_type(self) -> Iterator[ParentDatasetQueryResults]:
"""Group results by parent dataset type.
def by_dataset_type(self) -> Iterator[SingleTypeDatasetQueryResults]:
"""Group results by dataset type.
Returns
-------
iter : `~collections.abc.Iterator` [ `ParentDatasetQueryResults` ]
iter : `~collections.abc.Iterator` [ `SingleTypeDatasetQueryResults` ]
An iterator over `DatasetQueryResults` instances that are each
responsible for a single parent dataset type (either just that
dataset type, one or more of its component dataset types, or both).
responsible for a single dataset type.
"""
raise NotImplementedError()

Expand Down Expand Up @@ -546,19 +545,19 @@ def explain_no_results(self, execute: bool = True) -> Iterable[str]:
raise NotImplementedError()


class ParentDatasetQueryResults(DatasetQueryResults):
class SingleTypeDatasetQueryResults(DatasetQueryResults):
"""An object that represents results from a query for datasets with a
single parent `DatasetType`.
"""

@abstractmethod
def materialize(self) -> AbstractContextManager[ParentDatasetQueryResults]:
def materialize(self) -> AbstractContextManager[SingleTypeDatasetQueryResults]:
# Docstring inherited from DatasetQueryResults.
raise NotImplementedError()

@property
@abstractmethod
def parent_dataset_type(self) -> DatasetType:
def dataset_type(self) -> DatasetType:
"""The parent dataset type for all datasets in this iterable
(`DatasetType`).
"""
Expand All @@ -576,20 +575,7 @@ def data_ids(self) -> DataCoordinateQueryResults:
"""
raise NotImplementedError()

@abstractmethod
def with_components(self, components: Sequence[str | None]) -> ParentDatasetQueryResults:
"""Return a new query results object for the same parent datasets but
different components.
Parameters
----------
components : `~collections.abc.Sequence` [ `str` or `None` ]
Names of components to include in iteration. `None` may be
included (at most once) to include the parent dataset type.
"""
raise NotImplementedError()

def expanded(self) -> ParentDatasetQueryResults:
def expanded(self) -> SingleTypeDatasetQueryResults:
# Docstring inherited from DatasetQueryResults.
raise NotImplementedError()

Expand Down
4 changes: 2 additions & 2 deletions python/lsst/daf/butler/direct_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
DirectDataCoordinateQueryResults,
DirectDatasetQueryResults,
DirectDimensionRecordQueryResults,
DirectParentDatasetQueryResults,
DirectSingleTypeDatasetQueryResults,
)
from .registry import queries as registry_queries
from .registry.sql_registry import SqlRegistry
Expand Down Expand Up @@ -103,7 +103,7 @@ def datasets(
**kwargs,
)
if isinstance(registry_query_result, registry_queries.ParentDatasetQueryResults):
return DirectParentDatasetQueryResults(registry_query_result)
return DirectSingleTypeDatasetQueryResults(registry_query_result)
else:
return DirectDatasetQueryResults(registry_query_result)

Expand Down
30 changes: 13 additions & 17 deletions python/lsst/daf/butler/direct_query_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@
"DirectDataCoordinateQueryResults",
"DirectDatasetQueryResults",
"DirectDimensionRecordQueryResults",
"DirectParentDatasetQueryResults",
"DirectSingleTypeDatasetQueryResults",
]

import contextlib
from collections.abc import Iterable, Iterator, Sequence
from collections.abc import Iterable, Iterator
from typing import TYPE_CHECKING, Any

from ._query_results import (
DataCoordinateQueryResults,
DatasetQueryResults,
DimensionRecordQueryResults,
ParentDatasetQueryResults,
SingleTypeDatasetQueryResults,
)
from .registry import queries as registry_queries

Expand Down Expand Up @@ -165,10 +165,10 @@ def __init__(self, registry_query_result: registry_queries.DatasetQueryResults):
def __iter__(self) -> Iterator[DatasetRef]:
return iter(self._registry_query_result)

def by_parent_dataset_type(self) -> Iterator[ParentDatasetQueryResults]:
def by_dataset_type(self) -> Iterator[SingleTypeDatasetQueryResults]:
# Docstring inherited.
for by_parent in self._registry_query_result.byParentDatasetType():
yield DirectParentDatasetQueryResults(by_parent)
yield DirectSingleTypeDatasetQueryResults(by_parent)

@contextlib.contextmanager
def materialize(self) -> Iterator[DatasetQueryResults]:
Expand All @@ -193,8 +193,8 @@ def explain_no_results(self, execute: bool = True) -> Iterable[str]:
return self._registry_query_result.explain_no_results(execute=execute)


class DirectParentDatasetQueryResults(ParentDatasetQueryResults):
"""Implementation of `ParentDatasetQueryResults` using query result
class DirectSingleTypeDatasetQueryResults(SingleTypeDatasetQueryResults):
"""Implementation of `SingleTypeDatasetQueryResults` using query result
obtained from registry.
Parameters
Expand All @@ -210,18 +210,18 @@ def __init__(self, registry_query_result: registry_queries.ParentDatasetQueryRes
def __iter__(self) -> Iterator[DatasetRef]:
return iter(self._registry_query_result)

def by_parent_dataset_type(self) -> Iterator[ParentDatasetQueryResults]:
def by_dataset_type(self) -> Iterator[SingleTypeDatasetQueryResults]:
# Docstring inherited.
yield self

@contextlib.contextmanager
def materialize(self) -> Iterator[ParentDatasetQueryResults]:
def materialize(self) -> Iterator[SingleTypeDatasetQueryResults]:
# Docstring inherited.
with self._registry_query_result.materialize() as result:
yield DirectParentDatasetQueryResults(result)
yield DirectSingleTypeDatasetQueryResults(result)

Check warning on line 221 in python/lsst/daf/butler/direct_query_results.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/direct_query_results.py#L221

Added line #L221 was not covered by tests

@property
def parent_dataset_type(self) -> DatasetType:
def dataset_type(self) -> DatasetType:
# Docstring inherited.
return self._registry_query_result.parentDatasetType

Expand All @@ -230,13 +230,9 @@ def data_ids(self) -> DataCoordinateQueryResults:
# Docstring inherited.
return DirectDataCoordinateQueryResults(self._registry_query_result.dataIds)

def with_components(self, components: Sequence[str | None]) -> ParentDatasetQueryResults:
def expanded(self) -> SingleTypeDatasetQueryResults:
# Docstring inherited.
return DirectParentDatasetQueryResults(self._registry_query_result.withComponents(components))

def expanded(self) -> ParentDatasetQueryResults:
# Docstring inherited.
return DirectParentDatasetQueryResults(self._registry_query_result.expanded())
return DirectSingleTypeDatasetQueryResults(self._registry_query_result.expanded())

Check warning on line 235 in python/lsst/daf/butler/direct_query_results.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/direct_query_results.py#L235

Added line #L235 was not covered by tests

def count(self, *, exact: bool = True, discard: bool = False) -> int:
# Docstring inherited.
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/daf/butler/tests/butler_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,9 @@ def _do_query(dataset: Any, **kwargs: Any) -> DatasetQueryResults:
self.assertTrue(result.any())
self.assertCountEqual([ref.dataId["detector"] for ref in result], [1, 2, 3, 2, 3, 4])

by_type = list(result.by_parent_dataset_type())
by_type = list(result.by_dataset_type())
self.assertEqual(len(by_type), 2)
self.assertEqual(set(item.parent_dataset_type.name for item in by_type), {"bias", "flat"})
self.assertEqual(set(item.dataset_type.name for item in by_type), {"bias", "flat"})

with result.materialize() as materialized:
result = materialized.expanded()
Expand Down
8 changes: 6 additions & 2 deletions tests/test_query_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
import re
import unittest

from lsst.daf.butler import DataCoordinateQueryResults, DimensionRecordQueryResults, ParentDatasetQueryResults
from lsst.daf.butler import (
DataCoordinateQueryResults,
DimensionRecordQueryResults,
SingleTypeDatasetQueryResults,
)
from lsst.daf.butler.registry import MissingSpatialOverlapError, RegistryConfig, _RegistryFactory
from lsst.daf.butler.transfers import YamlRepoImportBackend

Expand Down Expand Up @@ -98,7 +102,7 @@ def setUpClass(cls) -> None:
def assert_relation_str(
self,
expected: str,
*results: DataCoordinateQueryResults | DimensionRecordQueryResults | ParentDatasetQueryResults,
*results: DataCoordinateQueryResults | DimensionRecordQueryResults | SingleTypeDatasetQueryResults,
) -> None:
"""Assert that checks that one or more registry
queries have relation trees that match the given string.
Expand Down

0 comments on commit 04dc549

Please sign in to comment.