Skip to content

Commit

Permalink
Sort all input lists by calexpList detector order
Browse files Browse the repository at this point in the history
  • Loading branch information
yalsayyad committed Mar 5, 2021
1 parent d3b4266 commit 51388bc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 32 deletions.
32 changes: 1 addition & 31 deletions python/lsst/pipe/tasks/assembleCoadd.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import lsstDebug
import lsst.utils as utils
from lsst.skymap import BaseSkyMap
from .coaddBase import CoaddBaseTask, SelectDataIdContainer, makeSkyInfo, makeCoaddSuffix
from .coaddBase import CoaddBaseTask, SelectDataIdContainer, makeSkyInfo, makeCoaddSuffix, reorderAndPadList
from .interpImage import InterpImageTask
from .scaleZeroPoint import ScaleZeroPointTask
from .coaddHelpers import groupPatchExposures, getGroupDataRef
Expand Down Expand Up @@ -2442,33 +2442,3 @@ def _dataRef2DebugPath(self, prefix, warpRef, coaddLevel=False):
directory = lsstDebug.Info(__name__).figPath if lsstDebug.Info(__name__).figPath else "."
filename = "%s-%s.fits" % (prefix, '-'.join([str(warpRef.dataId[k]) for k in keyList]))
return os.path.join(directory, filename)


def reorderAndPadList(inputList, inputKeys, outputKeys, padWith=None):
"""Match the order of one list to another, padding if necessary
Parameters
----------
inputList : list
List to be reordered and padded. Elements can be any type.
inputKeys : iterable
Iterable of values to be compared with outputKeys.
Length must match `inputList`
outputKeys : iterable
Iterable of values to be compared with inputKeys.
padWith :
Any value to be inserted where inputKey not in outputKeys
Returns
-------
list
Copy of inputList reordered per outputKeys and padded with `padWith`
so that the length matches length of outputKeys.
"""
outputList = []
for d in outputKeys:
if d in inputKeys:
outputList.append(inputList[inputKeys.index(d)])
else:
outputList.append(padWith)
return outputList
30 changes: 30 additions & 0 deletions python/lsst/pipe/tasks/coaddBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,33 @@ def makeCoaddSuffix(warpType="direct"):
"""
suffix = "" if warpType == "direct" else warpType[0].upper() + warpType[1:]
return suffix


def reorderAndPadList(inputList, inputKeys, outputKeys, padWith=None):
"""Match the order of one list to another, padding if necessary
Parameters
----------
inputList : list
List to be reordered and padded. Elements can be any type.
inputKeys : iterable
Iterable of values to be compared with outputKeys.
Length must match `inputList`
outputKeys : iterable
Iterable of values to be compared with inputKeys.
padWith :
Any value to be inserted where inputKey not in outputKeys
Returns
-------
list
Copy of inputList reordered per outputKeys and padded with `padWith`
so that the length matches length of outputKeys.
"""
outputList = []
for d in outputKeys:
if d in inputKeys:
outputList.append(inputList[inputKeys.index(d)])
else:
outputList.append(padWith)
return outputList
42 changes: 41 additions & 1 deletion python/lsst/pipe/tasks/makeCoaddTempExp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
import lsst.geom
from lsst.meas.algorithms import CoaddPsf, CoaddPsfConfig
from lsst.skymap import BaseSkyMap
from .coaddBase import CoaddBaseTask, makeSkyInfo
from .coaddBase import CoaddBaseTask, makeSkyInfo, reorderAndPadList
from .warpAndPsfMatch import WarpAndPsfMatchTask
from .coaddHelpers import groupPatchExposures, getGroupDataRef
from collections.abc import Iterable

__all__ = ["MakeCoaddTempExpTask", "MakeWarpTask", "MakeWarpConfig"]

Expand Down Expand Up @@ -736,6 +737,11 @@ def runQuantum(self, butlerQC, inputRefs, outputRefs):
PipelineTask (Gen3) entry point to warp and optionally PSF-match
calexps. This method is analogous to `runDataRef`.
"""

# Ensure all input lists are in same detector order as the calExpList
detectorOrder = [ref.datasetRef.dataId['detector'] for ref in inputRefs.calExpList]
inputRefs = reorderInputRefs(inputRefs, detectorOrder, dataIdKey='detector')

# Read in all inputs.
inputs = butlerQC.get(inputRefs)

Expand Down Expand Up @@ -875,3 +881,37 @@ def prepareCalibratedExposures(self, calExpList, backgroundList=None, skyCorrLis
# Apply skycorr
if self.config.doApplySkyCorr:
mi -= skyCorr.getImage()


def reorderInputRefs(inputRefs, outputKeyOrder, dataIdKey):
"""Reorder inputRefs per outputKeyOrder
Any inputRefs which are lists will be resorted per specified key e.g.,
detector. Only iterables will be reordered, and values can be of type
`lsst.pipe.base.connections.DeferredDatasetRef` or
`lsst.daf.butler.core.datasets.ref.DatasetRef`
Parameters
----------
inputRefs : `lsst.pipe.base.connections.InputQuantizedConnection`
Input references to be reordered and padded.
outputKeyOrder : iterable
Iterable of values to be compared with inputRef's dataIdKey.
Must be hashable
dataIdKey : `str`
dataIdKey in the dataRefs to compare with the outputKeyOrder.
Returns:
--------
inputRefs: `lsst.pipe.base.connections.InputQuantizedConnection`
Sorted inputRefs.
"""
for key, refs in inputRefs:
if isinstance(refs, Iterable):
if hasattr(refs[0], "dataId"):
inputKeyOrder = [ref.dataId[dataIdKey] for ref in refs]
else:
inputKeyOrder = [ref.datasetRef.dataId[dataIdKey] for ref in refs]
if inputKeyOrder != outputKeyOrder:
setattr(inputRefs, key, reorderAndPadList(refs, inputKeyOrder, outputKeyOrder))
return inputRefs

0 comments on commit 51388bc

Please sign in to comment.