Skip to content

Commit

Permalink
Merge pull request #1066 from lsst/tickets/DM-45860
Browse files Browse the repository at this point in the history
DM-45860: Add property to DimensionGroup to make it easy to get best time/region dim for query
  • Loading branch information
timj authored Sep 6, 2024
2 parents 767a556 + ff525f4 commit aa622dc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/changes/DM-45860.api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``DimensionGroup.region_dimension`` and ``DimensionGroup.timespan_dimension`` properties to make it easy to ask which dimension in the group is the best one to use for region or time calculations.
25 changes: 25 additions & 0 deletions python/lsst/daf/butler/dimensions/_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,31 @@ def add_to_order(element: DimensionElement) -> None:
order.extend(element for element in self.elements if element not in done)
return tuple(order)

def _choose_dimension(self, families: NamedValueAbstractSet[TopologicalFamily]) -> str | None:
if len(families) != 1:
return None
return list(families)[0].choose(self.elements, self.universe).name

@property
def region_dimension(self) -> str | None:
"""Return the most appropriate spatial dimension to use when looking
up a region.
Returns `None` if there are no appropriate dimensions or more than one
spatial family.
"""
return self._choose_dimension(self.spatial)

@property
def timespan_dimension(self) -> str | None:
"""Return the most appropriate temporal dimension to use when looking
up a time span.
Returns `None` if there are no appropriate dimensions or more than one
temporal family.
"""
return self._choose_dimension(self.temporal)

@property
def spatial(self) -> NamedValueAbstractSet[TopologicalFamily]:
"""Families represented by the spatial elements in this graph."""
Expand Down
7 changes: 7 additions & 0 deletions tests/test_dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ def testCalibrationDimensions(self):
self.assertCountEqual(group.implied, ("band",))
self.assertCountEqual(group.elements, group.names)
self.assertCountEqual(group.governors, {"instrument"})
self.assertIsNone(group.region_dimension)
self.assertIsNone(group.timespan_dimension)

def testObservationDimensions(self):
group = self.universe.conform(["exposure", "detector", "visit"])
Expand All @@ -298,6 +300,8 @@ def testObservationDimensions(self):
self.assertCountEqual(group.spatial.names, ("observation_regions",))
self.assertCountEqual(group.temporal.names, ("observation_timespans",))
self.assertCountEqual(group.governors, {"instrument"})
self.assertEqual(group.region_dimension, "visit_detector_region")
self.assertEqual(group.timespan_dimension, "exposure")
self.assertEqual(group.spatial.names, {"observation_regions"})
self.assertEqual(group.temporal.names, {"observation_timespans"})
self.assertEqual(next(iter(group.spatial)).governor, self.universe["instrument"])
Expand Down Expand Up @@ -334,6 +338,9 @@ def testSubsetCalculation(self):
group = self.universe.conform(["visit", "detector", "tract", "patch", "htm7", "exposure"])
self.assertCountEqual(group.spatial.names, ("observation_regions", "skymap_regions", "htm"))
self.assertCountEqual(group.temporal.names, ("observation_timespans",))
self.assertEqual(group.timespan_dimension, "exposure")
# Can not choose between visit_detector_region or htm7 or tract/patch.
self.assertIsNone(group.region_dimension)

def testSchemaGeneration(self):
tableSpecs: NamedKeyDict[DimensionElement, ddl.TableSpec] = NamedKeyDict({})
Expand Down

0 comments on commit aa622dc

Please sign in to comment.