Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-32750: Complain if unrecognized dataId/kwarg dimension keys are given in Butler APIs #610

Merged
merged 3 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions python/lsst/daf/butler/_butler.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ def _rewrite_data_id(self, dataId: Optional[DataId], datasetType: DatasetType,

# Go through the missing dimensions and associate the
# given names with records within those dimensions
matched_dims = set()
for dimensionName in candidateDimensions:
dimension = self.registry.dimensions.getStaticDimensions()[dimensionName]
fields = dimension.metadata.names | dimension.uniqueKeys.names
Expand All @@ -739,6 +740,13 @@ def _rewrite_data_id(self, dataId: Optional[DataId], datasetType: DatasetType,
guessedAssociation[dimensionName][field] = not_dimensions[field]
counter[dimensionName] += 1
assigned[field].add(dimensionName)
matched_dims.add(field)

# Calculate the fields that matched nothing.
never_found = set(not_dimensions) - matched_dims

if never_found:
raise ValueError(f"Unrecognized keyword args given: {never_found}")

# There is a chance we have allocated a single dataId item
# to multiple dimensions. Need to decide which should be retained.
Expand Down
3 changes: 1 addition & 2 deletions python/lsst/daf/butler/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,4 @@ def addDataset(self, dataId, run=None, datasetType=None):
self.butler.put(metric,
self.datasetType if datasetType is None else datasetType,
dataId,
run=run,
tags=())
run=run)
6 changes: 3 additions & 3 deletions tests/test_butler.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ def testButlerRewriteDataId(self):
"list": [2*x for x in range(i)]}

# Use the seq_num for the put to test rewriting.
dataId = {"seq_num": i, "day_obs": dayobs, "detector": 1, "instrument": "DummyCamComp",
dataId = {"seq_num": i, "day_obs": dayobs, "instrument": "DummyCamComp",
"physical_filter": "d-r"}
ref = butler.put(metric, datasetTypeName, dataId=dataId)

Expand Down Expand Up @@ -1829,7 +1829,7 @@ def assertButlerTransfers(self, id_gen_map=None, purge=False, storageClassName="
run = "distraction"
butler = Butler(butler=self.source_butler, run=run)
butler.put(makeExampleMetrics(), datasetTypeName,
exposure=1, detector=1, instrument="DummyCamComp", physical_filter="d-r")
exposure=1, instrument="DummyCamComp", physical_filter="d-r")

# Write some example metrics to the source
butler = Butler(butler=self.source_butler)
Expand All @@ -1851,7 +1851,7 @@ def assertButlerTransfers(self, id_gen_map=None, purge=False, storageClassName="
"output": {"text": "metric"},
"data": [2*x for x in range(i)]}
metric = MetricsExample(**metric_data)
dataId = {"exposure": i, "detector": 1, "instrument": "DummyCamComp", "physical_filter": "d-r"}
dataId = {"exposure": i, "instrument": "DummyCamComp", "physical_filter": "d-r"}
ref = butler.put(metric, datasetTypeName, dataId=dataId, run=run)

# Remove the datastore record using low-level API
Expand Down
11 changes: 11 additions & 0 deletions tests/test_simpleButler.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,17 @@ def testGetCalibration(self):
collections="calibs", instrument="Cam1")
self.assertEqual(bias3b_id, bias3b.id)

# Ensure that spurious kwargs cause an exception.
with self.assertRaises(ValueError):
butler.get("bias", {"exposure.obs_id": "four", "immediate": True,
"detector.full_name": "Ba"},
collections="calibs", instrument="Cam1")

with self.assertRaises(ValueError):
butler.get("bias", day_obs=20211114, seq_num=42,
raft="B", name_in_raft="a",
collections="calibs", instrument="Cam1", immediate=True)

def testRegistryDefaults(self):
"""Test that we can default the collections and some data ID keys when
constructing a butler.
Expand Down