23 from __future__
import absolute_import, division, print_function
24 from builtins
import zip
25 from lsst.pipe.base
import Struct
27 """Helper functions for coaddition.
29 We often want to use a data reference as a key in a dict (e.g., inputs as a
30 function of data reference for a warp/tempExp), but neither data references
31 (lsst.daf.persistence.ButlerDataRef) nor data identifiers (dict) are hashable.
32 One solution is to use tuples (which are hashable) of the data identifier
33 values, and carry the data identifier keys separately. Doing the key/value
34 gymnastics can be annoying, so we provide these helper functions to do this.
39 """Group data references by data identifier value-tuple.
41 Value-tuples are built from the values of the given keys.
42 The effect is that the data references in each group have the same
43 values for the provided keys.
45 @param keys: List of keys to consider when grouping (order is important)
46 @param dataRefIterable: Iterable of data references to group
47 @return Dict of <value-tuple>: <list of data references for group>
50 for dataRef
in dataRefIterable:
51 dataId = dataRef.dataId
52 values = tuple(dataId[key]
for key
in keys)
53 group = groupDict.get(values)
57 groupDict[values] = [dataRef]
63 tempExpDatasetType=
"deepCoadd_tempExp"):
64 """Group calibrated exposures overlapping a patch by the warped
65 (temporary) exposure they contribute to.
67 For example, if the instrument has a mosaic camera, each group would
68 consist of the subset of CCD exposures from a single camera exposure
69 that potentially overlap the patch.
72 - groups: Dict of <group tuple>: <list of data references for group>
73 - keys: List of keys for group tuple
75 butler = patchDataRef.getButler()
76 tempExpKeys = butler.getKeys(datasetType=tempExpDatasetType)
77 coaddKeys = sorted(butler.getKeys(datasetType=coaddDatasetType))
78 keys = sorted(set(tempExpKeys) - set(coaddKeys))
79 patchId = patchDataRef.dataId
83 coaddValues = tuple(patchId[k]
for k
in coaddKeys)
84 groups = dict((k + coaddValues, v)
for k, v
in groups.items())
85 keys += tuple(coaddKeys)
87 return Struct(groups=groups, keys=keys)
91 """Reconstitute a data identifier from a tuple and corresponding keys
93 @param groupTuple: Tuple with values specifying a group
94 @param keys: List of keys for group tuple
95 @return Data identifier dict
97 if len(groupTuple) != len(keys):
98 raise RuntimeError(
"Number of values (%d) and keys (%d) do not match" % (len(groupTuple), len(keys)))
99 return dict(zip(keys, groupTuple))
103 """Construct a data reference from a tuple and corresponding keys
105 @param butler: Data butler
106 @param datasetType: Name of dataset
107 @param groupTuple: Tuple with values specifying a group
108 @param keys: List of keys for group tuple
109 @return Data reference
112 return butler.dataRef(datasetType=datasetType, dataId=dataId)