191 def run(self, mapperResults, exposure, **kwargs):
192 """Reduce a list of items produced by `ImageMapper`.
194 Either stitch the passed `mapperResults` list
195 together into a new Exposure (default) or pass it through
196 (if `self.config.reduceOperation` is 'none').
198 If `self.config.reduceOperation` is not 'none', then expect
199 that the `pipeBase.Struct`s in the `mapperResults` list
200 contain sub-exposures named 'subExposure', to be stitched back
201 into a single Exposure with the same dimensions, PSF, and mask
202 as the input `exposure`. Otherwise, the `mapperResults` list
203 is simply returned directly.
207 mapperResults : `list`
208 list of `lsst.pipe.base.Struct` returned by `ImageMapper.run`.
209 exposure : `lsst.afw.image.Exposure`
210 the original exposure which is cloned to use as the
211 basis for the resulting exposure (if
212 ``self.config.mapper.reduceOperation`` is not 'None')
214 additional keyword arguments propagated from
215 `ImageMapReduceTask.run`.
219 A `lsst.pipe.base.Struct` containing either an `lsst.afw.image.Exposure`
220 (named 'exposure') or a list (named 'result'),
221 depending on `config.reduceOperation`.
225 1. This currently correctly handles overlapping sub-exposures.
226 For overlapping sub-exposures, use `config.reduceOperation='average'`.
227 2. This correctly handles varying PSFs, constructing the resulting
228 exposure's PSF via CoaddPsf (DM-9629).
232 1. To be done: correct handling of masks (nearly there)
233 2. This logic currently makes *two* copies of the original exposure
234 (one here and one in `mapper.run()`). Possibly of concern
235 for large images on memory-constrained systems.
238 if self.config.reduceOperation ==
'none':
239 return pipeBase.Struct(result=mapperResults)
241 if self.config.reduceOperation ==
'coaddPsf':
243 coaddPsf = self._constructPsf(mapperResults, exposure)
244 return pipeBase.Struct(result=coaddPsf)
246 newExp = exposure.clone()
247 newMI = newExp.getMaskedImage()
249 reduceOp = self.config.reduceOperation
250 if reduceOp ==
'copy':
252 newMI.getImage()[:, :] = np.nan
253 newMI.getVariance()[:, :] = np.nan
255 newMI.getImage()[:, :] = 0.
256 newMI.getVariance()[:, :] = 0.
257 if reduceOp ==
'average':
258 weights = afwImage.ImageI(newMI.getBBox())
260 for item
in mapperResults:
261 item = item.subExposure
262 if not (isinstance(item, afwImage.ExposureF)
or isinstance(item, afwImage.ExposureI)
263 or isinstance(item, afwImage.ExposureU)
or isinstance(item, afwImage.ExposureD)):
264 raise TypeError(
"""Expecting an Exposure type, got %s.
265 Consider using `reduceOperation="none".""" % str(
type(item)))
266 subExp = newExp.Factory(newExp, item.getBBox())
267 subMI = subExp.getMaskedImage()
268 patchMI = item.getMaskedImage()
269 isValid = ~np.isnan(patchMI.getImage().getArray() * patchMI.getVariance().getArray())
271 if reduceOp ==
'copy':
272 subMI.getImage().getArray()[isValid] = patchMI.getImage().getArray()[isValid]
273 subMI.getVariance().getArray()[isValid] = patchMI.getVariance().getArray()[isValid]
274 subMI.getMask().getArray()[:, :] |= patchMI.getMask().getArray()
276 if reduceOp ==
'sum' or reduceOp ==
'average':
277 subMI.getImage().getArray()[isValid] += patchMI.getImage().getArray()[isValid]
278 subMI.getVariance().getArray()[isValid] += patchMI.getVariance().getArray()[isValid]
279 subMI.getMask().getArray()[:, :] |= patchMI.getMask().getArray()
280 if reduceOp ==
'average':
282 wtsView = afwImage.ImageI(weights, item.getBBox())
283 wtsView.getArray()[isValid] += 1
286 mask = newMI.getMask()
287 for m
in self.config.badMaskPlanes:
289 bad = mask.getPlaneBitMask(self.config.badMaskPlanes)
291 isNan = np.where(np.isnan(newMI.getImage().getArray() * newMI.getVariance().getArray()))
292 if len(isNan[0]) > 0:
294 mask.getArray()[isNan[0], isNan[1]] |= bad
296 if reduceOp ==
'average':
297 wts = weights.getArray().astype(np.float)
298 self.log.
info(
'AVERAGE: Maximum overlap: %f', np.nanmax(wts))
299 self.log.
info(
'AVERAGE: Average overlap: %f', np.nanmean(wts))
300 self.log.
info(
'AVERAGE: Minimum overlap: %f', np.nanmin(wts))
301 wtsZero = np.equal(wts, 0.)
302 wtsZeroInds = np.where(wtsZero)
303 wtsZeroSum = len(wtsZeroInds[0])
304 self.log.
info(
'AVERAGE: Number of zero pixels: %f (%f%%)', wtsZeroSum,
305 wtsZeroSum * 100. / wtsZero.size)
306 notWtsZero = ~wtsZero
307 tmp = newMI.getImage().getArray()
308 np.divide(tmp, wts, out=tmp, where=notWtsZero)
309 tmp = newMI.getVariance().getArray()
310 np.divide(tmp, wts, out=tmp, where=notWtsZero)
311 if len(wtsZeroInds[0]) > 0:
312 newMI.getImage().getArray()[wtsZeroInds] = np.nan
313 newMI.getVariance().getArray()[wtsZeroInds] = np.nan
316 mask.getArray()[wtsZeroInds] |= bad
319 if reduceOp ==
'sum' or reduceOp ==
'average':
320 psf = self._constructPsf(mapperResults, exposure)
323 return pipeBase.Struct(exposure=newExp)
def run(self, coaddExposures, bbox, wcs)