22__all__ = [
"AssembleCoaddTask",
"AssembleCoaddConnections",
"AssembleCoaddConfig",
23 "CompareWarpAssembleCoaddTask",
"CompareWarpAssembleCoaddConfig"]
42from .coaddBase
import CoaddBaseTask, makeSkyInfo, reorderAndPadList
43from .interpImage
import InterpImageTask
44from .scaleZeroPoint
import ScaleZeroPointTask
45from .maskStreaks
import MaskStreaksTask
46from .healSparseMapping
import HealSparseInputMapTask
48from lsst.utils.timer
import timeMethod
49from deprecated.sphinx
import deprecated
51log = logging.getLogger(__name__)
55 dimensions=(
"tract",
"patch",
"band",
"skymap"),
56 defaultTemplates={
"inputCoaddName":
"deep",
57 "outputCoaddName":
"deep",
59 "warpTypeSuffix":
""}):
61 inputWarps = pipeBase.connectionTypes.Input(
62 doc=(
"Input list of warps to be assemebled i.e. stacked."
63 "WarpType (e.g. direct, psfMatched) is controlled by the warpType config parameter"),
64 name=
"{inputCoaddName}Coadd_{warpType}Warp",
65 storageClass=
"ExposureF",
66 dimensions=(
"tract",
"patch",
"skymap",
"visit",
"instrument"),
70 skyMap = pipeBase.connectionTypes.Input(
71 doc=
"Input definition of geometry/bbox and projection/wcs for coadded exposures",
72 name=BaseSkyMap.SKYMAP_DATASET_TYPE_NAME,
73 storageClass=
"SkyMap",
74 dimensions=(
"skymap", ),
76 selectedVisits = pipeBase.connectionTypes.Input(
77 doc=
"Selected visits to be coadded.",
78 name=
"{outputCoaddName}Visits",
79 storageClass=
"StructuredDataDict",
80 dimensions=(
"instrument",
"tract",
"patch",
"skymap",
"band")
82 brightObjectMask = pipeBase.connectionTypes.PrerequisiteInput(
83 doc=(
"Input Bright Object Mask mask produced with external catalogs to be applied to the mask plane"
85 name=
"brightObjectMask",
86 storageClass=
"ObjectMaskCatalog",
87 dimensions=(
"tract",
"patch",
"skymap",
"band"),
90 coaddExposure = pipeBase.connectionTypes.Output(
91 doc=
"Output coadded exposure, produced by stacking input warps",
92 name=
"{outputCoaddName}Coadd{warpTypeSuffix}",
93 storageClass=
"ExposureF",
94 dimensions=(
"tract",
"patch",
"skymap",
"band"),
96 nImage = pipeBase.connectionTypes.Output(
97 doc=
"Output image of number of input images per pixel",
98 name=
"{outputCoaddName}Coadd_nImage",
99 storageClass=
"ImageU",
100 dimensions=(
"tract",
"patch",
"skymap",
"band"),
102 inputMap = pipeBase.connectionTypes.Output(
103 doc=
"Output healsparse map of input images",
104 name=
"{outputCoaddName}Coadd_inputMap",
105 storageClass=
"HealSparseMap",
106 dimensions=(
"tract",
"patch",
"skymap",
"band"),
109 def __init__(self, *, config=None):
110 super().__init__(config=config)
112 if not config.doMaskBrightObjects:
113 self.prerequisiteInputs.remove(
"brightObjectMask")
115 if not config.doSelectVisits:
116 self.inputs.remove(
"selectedVisits")
118 if not config.doNImage:
119 self.outputs.remove(
"nImage")
121 if not self.config.doInputMap:
122 self.outputs.remove(
"inputMap")
125class AssembleCoaddConfig(CoaddBaseTask.ConfigClass, pipeBase.PipelineTaskConfig,
126 pipelineConnections=AssembleCoaddConnections):
127 warpType = pexConfig.Field(
128 doc=
"Warp name: one of 'direct' or 'psfMatched'",
132 subregionSize = pexConfig.ListField(
134 doc=
"Width, height of stack subregion size; "
135 "make small enough that a full stack of images will fit into memory at once.",
137 default=(2000, 2000),
139 statistic = pexConfig.Field(
141 doc=
"Main stacking statistic for aggregating over the epochs.",
144 doOnlineForMean = pexConfig.Field(
146 doc=
"Perform online coaddition when statistic=\"MEAN\" to save memory?",
149 doSigmaClip = pexConfig.Field(
151 doc=
"Perform sigma clipped outlier rejection with MEANCLIP statistic? (DEPRECATED)",
154 sigmaClip = pexConfig.Field(
156 doc=
"Sigma for outlier rejection; ignored if non-clipping statistic selected.",
159 clipIter = pexConfig.Field(
161 doc=
"Number of iterations of outlier rejection; ignored if non-clipping statistic selected.",
164 calcErrorFromInputVariance = pexConfig.Field(
166 doc=
"Calculate coadd variance from input variance by stacking statistic."
167 "Passed to StatisticsControl.setCalcErrorFromInputVariance()",
170 scaleZeroPoint = pexConfig.ConfigurableField(
171 target=ScaleZeroPointTask,
172 doc=
"Task to adjust the photometric zero point of the coadd temp exposures",
174 doInterp = pexConfig.Field(
175 doc=
"Interpolate over NaN pixels? Also extrapolate, if necessary, but the results are ugly.",
179 interpImage = pexConfig.ConfigurableField(
180 target=InterpImageTask,
181 doc=
"Task to interpolate (and extrapolate) over NaN pixels",
183 doWrite = pexConfig.Field(
184 doc=
"Persist coadd?",
188 doNImage = pexConfig.Field(
189 doc=
"Create image of number of contributing exposures for each pixel",
193 doUsePsfMatchedPolygons = pexConfig.Field(
194 doc=
"Use ValidPolygons from shrunk Psf-Matched Calexps? Should be set to True by CompareWarp only.",
198 maskPropagationThresholds = pexConfig.DictField(
201 doc=(
"Threshold (in fractional weight) of rejection at which we propagate a mask plane to "
202 "the coadd; that is, we set the mask bit on the coadd if the fraction the rejected frames "
203 "would have contributed exceeds this value."),
204 default={
"SAT": 0.1},
206 removeMaskPlanes = pexConfig.ListField(dtype=str, default=[
"NOT_DEBLENDED"],
207 doc=
"Mask planes to remove before coadding")
208 doMaskBrightObjects = pexConfig.Field(dtype=bool, default=
False,
209 doc=
"Set mask and flag bits for bright objects?")
210 brightObjectMaskName = pexConfig.Field(dtype=str, default=
"BRIGHT_OBJECT",
211 doc=
"Name of mask bit used for bright objects")
212 coaddPsf = pexConfig.ConfigField(
213 doc=
"Configuration for CoaddPsf",
214 dtype=measAlg.CoaddPsfConfig,
216 doAttachTransmissionCurve = pexConfig.Field(
217 dtype=bool, default=
False, optional=
False,
218 doc=(
"Attach a piecewise TransmissionCurve for the coadd? "
219 "(requires all input Exposures to have TransmissionCurves).")
221 hasFakes = pexConfig.Field(
224 doc=
"Should be set to True if fake sources have been inserted into the input data."
226 doSelectVisits = pexConfig.Field(
227 doc=
"Coadd only visits selected by a SelectVisitsTask",
231 doInputMap = pexConfig.Field(
232 doc=
"Create a bitwise map of coadd inputs",
236 inputMapper = pexConfig.ConfigurableField(
237 doc=
"Input map creation subtask.",
238 target=HealSparseInputMapTask,
243 self.badMaskPlanes = [
"NO_DATA",
"BAD",
"SAT",
"EDGE"]
250 log.warning(
"Config doPsfMatch deprecated. Setting warpType='psfMatched'")
251 self.warpType =
'psfMatched'
252 if self.doSigmaClip
and self.statistic !=
"MEANCLIP":
253 log.warning(
'doSigmaClip deprecated. To replicate behavior, setting statistic to "MEANCLIP"')
254 self.statistic =
"MEANCLIP"
255 if self.doInterp
and self.statistic
not in [
'MEAN',
'MEDIAN',
'MEANCLIP',
'VARIANCE',
'VARIANCECLIP']:
256 raise ValueError(
"Must set doInterp=False for statistic=%s, which does not "
257 "compute and set a non-zero coadd variance estimate." % (self.statistic))
259 unstackableStats = [
'NOTHING',
'ERROR',
'ORMASK']
260 if not hasattr(afwMath.Property, self.statistic)
or self.statistic
in unstackableStats:
261 stackableStats = [str(k)
for k
in afwMath.Property.__members__.keys()
262 if str(k)
not in unstackableStats]
263 raise ValueError(
"statistic %s is not allowed. Please choose one of %s."
264 % (self.statistic, stackableStats))
268 """Assemble a coadded image from a set of warps.
270 Each Warp that goes into a coadd will typically have an independent
271 photometric zero-point. Therefore, we must scale each Warp to set it to
272 a common photometric zeropoint. WarpType may be one of 'direct' or
273 'psfMatched',
and the boolean configs `config.makeDirect`
and
274 `config.makePsfMatched` set which of the warp types will be coadded.
275 The coadd
is computed
as a mean
with optional outlier rejection.
276 Criteria
for outlier rejection are set
in `AssembleCoaddConfig`.
277 Finally, Warps can have bad
'NaN' pixels which received no input
from the
278 source calExps. We interpolate over these bad (NaN) pixels.
280 `AssembleCoaddTask` uses several sub-tasks. These are
282 - `~lsst.pipe.tasks.ScaleZeroPointTask`
283 - create
and use an ``imageScaler`` object to scale the photometric zeropoint
for each Warp
284 - `~lsst.pipe.tasks.InterpImageTask`
285 - interpolate across bad pixels (NaN)
in the final coadd
287 You can retarget these subtasks
if you wish.
292 Raised
if unable to define mask plane
for bright objects.
297 `AssembleCoaddTask` has no debug variables of its own. Some of the
298 subtasks may support `~lsst.base.lsstDebug` variables. See the
299 documentation
for the subtasks
for further information.
303 `AssembleCoaddTask` assembles a set of warped images into a coadded image.
304 The `AssembleCoaddTask` can be invoked by running ``assembleCoadd.py``
305 with the flag
'--legacyCoadd'. Usage of assembleCoadd.py expects two
306 inputs: a data reference to the tract patch
and filter to be coadded,
and
307 a list of Warps to attempt to coadd. These are specified using ``--id``
and
308 ``--selectId``, respectively:
312 --id = [KEY=VALUE1[^VALUE2[^VALUE3...] [KEY=VALUE1[^VALUE2[^VALUE3...] ...]]
313 --selectId [KEY=VALUE1[^VALUE2[^VALUE3...] [KEY=VALUE1[^VALUE2[^VALUE3...] ...]]
315 Only the Warps that cover the specified tract
and patch will be coadded.
316 A list of the available optional arguments can be obtained by calling
317 ``assembleCoadd.py``
with the ``--help`` command line argument:
321 assembleCoadd.py --help
323 To demonstrate usage of the `AssembleCoaddTask`
in the larger context of
324 multi-band processing, we will generate the HSC-I & -R band coadds
from
325 HSC engineering test data provided
in the ``ci_hsc`` package. To begin,
326 assuming that the lsst stack has been already set up, we must set up the
327 obs_subaru
and ``ci_hsc`` packages. This defines the environment variable
328 ``$CI_HSC_DIR``
and points at the location of the package. The raw HSC
329 data live
in the ``$CI_HSC_DIR/raw directory``. To begin assembling the
330 coadds, we must first run:
333 - process the individual ccds
in $CI_HSC_RAW to produce calibrated exposures
335 - create a skymap that covers the area of the sky present
in the raw exposures
337 - warp the individual calibrated exposures to the tangent plane of the coadd
339 We can perform all of these steps by running
343 $CI_HSC_DIR scons warp-903986 warp-904014 warp-903990 warp-904010 warp-903988
345 This will produce warped exposures
for each visit. To coadd the warped
346 data, we call assembleCoadd.py
as follows:
350 assembleCoadd.py --legacyCoadd $CI_HSC_DIR/DATA --id patch=5,4 tract=0 filter=HSC-I \
351 --selectId visit=903986 ccd=16 --selectId visit=903986 ccd=22 --selectId visit=903986 ccd=23 \
352 --selectId visit=903986 ccd=100 --selectId visit=904014 ccd=1 --selectId visit=904014 ccd=6 \
353 --selectId visit=904014 ccd=12 --selectId visit=903990 ccd=18 --selectId visit=903990 ccd=25 \
354 --selectId visit=904010 ccd=4 --selectId visit=904010 ccd=10 --selectId visit=904010 ccd=100 \
355 --selectId visit=903988 ccd=16 --selectId visit=903988 ccd=17 --selectId visit=903988 ccd=23 \
356 --selectId visit=903988 ccd=24
358 that will process the HSC-I band data. The results are written
in
359 ``$CI_HSC_DIR/DATA/deepCoadd-results/HSC-I``.
361 You may also choose to run:
365 scons warp-903334 warp-903336 warp-903338 warp-903342 warp-903344 warp-903346
366 assembleCoadd.py --legacyCoadd $CI_HSC_DIR/DATA --id patch=5,4 tract=0 filter=HSC-R \
367 --selectId visit=903334 ccd=16 --selectId visit=903334 ccd=22 --selectId visit=903334 ccd=23 \
368 --selectId visit=903334 ccd=100 --selectId visit=903336 ccd=17 --selectId visit=903336 ccd=24 \
369 --selectId visit=903338 ccd=18 --selectId visit=903338 ccd=25 --selectId visit=903342 ccd=4 \
370 --selectId visit=903342 ccd=10 --selectId visit=903342 ccd=100 --selectId visit=903344 ccd=0 \
371 --selectId visit=903344 ccd=5 --selectId visit=903344 ccd=11 --selectId visit=903346 ccd=1 \
372 --selectId visit=903346 ccd=6 --selectId visit=903346 ccd=12
374 to generate the coadd
for the HSC-R band
if you are interested
in
375 following multiBand Coadd processing
as discussed
in `pipeTasks_multiBand`
376 (but note that normally, one would use the `SafeClipAssembleCoaddTask`
377 rather than `AssembleCoaddTask` to make the coadd.
380 ConfigClass = AssembleCoaddConfig
381 _DefaultName = "assembleCoadd"
383 def __init__(self, *args, **kwargs):
386 argNames = [
"config",
"name",
"parentTask",
"log"]
387 kwargs.update({k: v
for k, v
in zip(argNames, args)})
388 warnings.warn(
"AssembleCoadd received positional args, and casting them as kwargs: %s. "
389 "PipelineTask will not take positional args" % argNames, FutureWarning,
392 super().__init__(**kwargs)
393 self.makeSubtask(
"interpImage")
394 self.makeSubtask(
"scaleZeroPoint")
396 if self.config.doMaskBrightObjects:
400 except pexExceptions.LsstCppException:
401 raise RuntimeError(
"Unable to define mask plane for bright objects; planes used are %s" %
402 mask.getMaskPlaneDict().keys())
405 if self.config.doInputMap:
406 self.makeSubtask(
"inputMapper")
410 @utils.inheritDoc(pipeBase.PipelineTask)
412 inputData = butlerQC.get(inputRefs)
416 skyMap = inputData[
"skyMap"]
417 outputDataId = butlerQC.quantum.dataId
419 inputData[
'skyInfo'] = makeSkyInfo(skyMap,
420 tractId=outputDataId[
'tract'],
421 patchId=outputDataId[
'patch'])
423 if self.config.doSelectVisits:
424 warpRefList = self.
filterWarps(inputData[
'inputWarps'], inputData[
'selectedVisits'])
426 warpRefList = inputData[
'inputWarps']
429 self.log.info(
"Found %d %s", len(inputs.tempExpRefList),
431 if len(inputs.tempExpRefList) == 0:
432 raise pipeBase.NoWorkFound(
"No coadd temporary exposures found")
435 retStruct = self.
run(inputData[
'skyInfo'], inputs.tempExpRefList, inputs.imageScalerList,
436 inputs.weightList, supplementaryData=supplementaryData)
438 inputData.setdefault(
'brightObjectMask',
None)
439 if self.config.doMaskBrightObjects
and inputData[
"brightObjectMask"]
is None:
440 log.warning(
"doMaskBrightObjects is set to True, but brightObjectMask not loaded")
441 self.
processResults(retStruct.coaddExposure, inputData[
'brightObjectMask'], outputDataId)
443 if self.config.doWrite:
444 butlerQC.put(retStruct, outputRefs)
448 """Interpolate over missing data and mask bright stars.
453 The coadded exposure to process.
455 Table of bright objects to mask.
456 dataId : `lsst.daf.butler.DataId`
or `
None`, optional
459 if self.config.doInterp:
460 self.interpImage.run(coaddExposure.getMaskedImage(), planeName=
"NO_DATA")
462 varArray = coaddExposure.variance.array
463 with numpy.errstate(invalid=
"ignore"):
464 varArray[:] = numpy.where(varArray > 0, varArray, numpy.inf)
466 if self.config.doMaskBrightObjects:
470 """Make additional inputs to run() specific to subclasses (Gen3).
472 Duplicates interface of `runQuantum` method.
473 Available to be implemented by subclasses only if they need the
474 coadd dataRef
for performing preliminary processing before
475 assembling the coadd.
479 butlerQC : `~lsst.pipe.base.ButlerQuantumContext`
480 Gen3 Butler object
for fetching additional data products before
481 running the Task specialized
for quantum being processed.
482 inputRefs : `~lsst.pipe.base.InputQuantizedConnection`
483 Attributes are the names of the connections describing input dataset types.
484 Values are DatasetRefs that task consumes
for corresponding dataset type.
485 DataIds are guaranteed to match data objects
in ``inputData``.
486 outputRefs : `~lsst.pipe.base.OutputQuantizedConnection`
487 Attributes are the names of the connections describing output dataset types.
488 Values are DatasetRefs that task
is to produce
489 for corresponding dataset type.
491 return pipeBase.Struct()
494 reason=
"makeSupplementaryDataGen3 is deprecated in favor of _makeSupplementaryData",
496 category=FutureWarning
502 """Prepare the input warps for coaddition by measuring the weight for
503 each warp and the scaling
for the photometric zero point.
505 Each Warp has its own photometric zeropoint
and background variance.
506 Before coadding these Warps together, compute a scale factor to
507 normalize the photometric zeropoint
and compute the weight
for each Warp.
512 List of data references to tempExp.
516 result : `~lsst.pipe.base.Struct`
517 Results
as a struct
with attributes:
520 `list` of data references to tempExp.
522 `list` of weightings.
524 `list` of image scalers.
527 statsCtrl.setNumSigmaClip(self.config.sigmaClip)
528 statsCtrl.setNumIter(self.config.clipIter)
530 statsCtrl.setNanSafe(True)
538 for tempExpRef
in refList:
539 tempExp = tempExpRef.get()
541 if numpy.isnan(tempExp.image.array).all():
543 maskedImage = tempExp.getMaskedImage()
544 imageScaler = self.scaleZeroPoint.computeImageScaler(
549 imageScaler.scaleMaskedImage(maskedImage)
550 except Exception
as e:
551 self.log.warning(
"Scaling failed for %s (skipping it): %s", tempExpRef.dataId, e)
554 afwMath.MEANCLIP, statsCtrl)
555 meanVar, meanVarErr = statObj.getResult(afwMath.MEANCLIP)
556 weight = 1.0 / float(meanVar)
557 if not numpy.isfinite(weight):
558 self.log.warning(
"Non-finite weight for %s: skipping", tempExpRef.dataId)
560 self.log.info(
"Weight of %s %s = %0.3f", tempExpName, tempExpRef.dataId, weight)
565 tempExpRefList.append(tempExpRef)
566 weightList.append(weight)
567 imageScalerList.append(imageScaler)
569 return pipeBase.Struct(tempExpRefList=tempExpRefList, weightList=weightList,
570 imageScalerList=imageScalerList)
573 """Prepare the statistics for coadding images.
577 mask : `int`, optional
578 Bit mask value to exclude from coaddition.
582 stats : `~lsst.pipe.base.Struct`
583 Statistics
as a struct
with attributes:
588 Statistic
for coadd (`~lsst.afw.math.Property`).
593 statsCtrl.setNumSigmaClip(self.config.sigmaClip)
594 statsCtrl.setNumIter(self.config.clipIter)
595 statsCtrl.setAndMask(mask)
596 statsCtrl.setNanSafe(
True)
597 statsCtrl.setWeighted(
True)
598 statsCtrl.setCalcErrorFromInputVariance(self.config.calcErrorFromInputVariance)
599 for plane, threshold
in self.config.maskPropagationThresholds.items():
600 bit = afwImage.Mask.getMaskPlane(plane)
601 statsCtrl.setMaskPropagationThreshold(bit, threshold)
603 return pipeBase.Struct(ctrl=statsCtrl, flags=statsFlags)
606 def run(self, skyInfo, tempExpRefList, imageScalerList, weightList,
607 altMaskList=None, mask=None, supplementaryData=None):
608 """Assemble a coadd from input warps.
610 Assemble the coadd using the provided list of coaddTempExps. Since
611 the full coadd covers a patch (a large area), the assembly is
612 performed over small areas on the image at a time
in order to
613 conserve memory usage. Iterate over subregions within the outer
614 bbox of the patch using `assembleSubregion` to stack the corresponding
615 subregions
from the coaddTempExps
with the statistic specified.
616 Set the edge bits the coadd mask based on the weight map.
620 skyInfo : `~lsst.pipe.base.Struct`
621 Struct
with geometric information about the patch.
622 tempExpRefList : `list`
623 List of data references to Warps (previously called CoaddTempExps).
624 imageScalerList : `list`
625 List of image scalers.
628 altMaskList : `list`, optional
629 List of alternate masks to use rather than those stored
with
631 mask : `int`, optional
632 Bit mask value to exclude
from coaddition.
633 supplementaryData : `~lsst.pipe.base.Struct`, optional
634 Struct
with additional data products needed to assemble coadd.
635 Only used by subclasses that implement ``_makeSupplementaryData``
640 result : `~lsst.pipe.base.Struct`
641 Results
as a struct
with attributes:
648 Bit-wise map of inputs,
if requested.
650 Input list of refs to the warps (``lsst.daf.butler.DeferredDatasetHandle``)
653 Input list of image scalers (`list`) (unmodified).
655 Input list of weights (`list`) (unmodified).
659 lsst.pipe.base.NoWorkFound
660 Raised
if no data references are provided.
663 self.log.info("Assembling %s %s", len(tempExpRefList), tempExpName)
664 if not tempExpRefList:
665 raise pipeBase.NoWorkFound(
"No exposures provided for co-addition.")
669 if altMaskList
is None:
670 altMaskList = [
None]*len(tempExpRefList)
672 coaddExposure = afwImage.ExposureF(skyInfo.bbox, skyInfo.wcs)
673 coaddExposure.setPhotoCalib(self.scaleZeroPoint.getPhotoCalib())
674 coaddExposure.getInfo().setCoaddInputs(self.inputRecorder.makeCoaddInputs())
676 coaddMaskedImage = coaddExposure.getMaskedImage()
677 subregionSizeArr = self.config.subregionSize
678 subregionSize =
geom.Extent2I(subregionSizeArr[0], subregionSizeArr[1])
680 if self.config.doNImage:
681 nImage = afwImage.ImageU(skyInfo.bbox)
686 if self.config.doInputMap:
687 self.inputMapper.build_ccd_input_map(skyInfo.bbox,
689 coaddExposure.getInfo().getCoaddInputs().ccds)
691 if self.config.doOnlineForMean
and self.config.statistic ==
"MEAN":
694 weightList, altMaskList, stats.ctrl,
696 except Exception
as e:
697 self.log.exception(
"Cannot compute online coadd %s", e)
700 for subBBox
in self.
_subBBoxIter(skyInfo.bbox, subregionSize):
703 weightList, altMaskList, stats.flags, stats.ctrl,
705 except Exception
as e:
706 self.log.exception(
"Cannot compute coadd %s: %s", subBBox, e)
710 if self.config.doInputMap:
711 self.inputMapper.finalize_ccd_input_map_mask()
712 inputMap = self.inputMapper.ccd_input_map
720 return pipeBase.Struct(coaddExposure=coaddExposure, nImage=nImage,
721 warpRefList=tempExpRefList, imageScalerList=imageScalerList,
722 weightList=weightList, inputMap=inputMap)
725 """Set the metadata for the coadd.
727 This basic implementation sets the filter from the first input.
732 The target exposure
for the coadd.
733 tempExpRefList : `list`
734 List of data references to tempExp.
741 Raised
if there
is a length mismatch.
743 assert len(tempExpRefList) == len(weightList),
"Length mismatch"
750 tempExpList = [tempExpRef.get(parameters={
'bbox': bbox})
for tempExpRef
in tempExpRefList]
752 numCcds = sum(len(tempExp.getInfo().getCoaddInputs().ccds)
for tempExp
in tempExpList)
757 coaddInputs = coaddExposure.getInfo().getCoaddInputs()
758 coaddInputs.ccds.reserve(numCcds)
759 coaddInputs.visits.reserve(len(tempExpList))
761 for tempExp, weight
in zip(tempExpList, weightList):
762 self.inputRecorder.addVisitToCoadd(coaddInputs, tempExp, weight)
764 if self.config.doUsePsfMatchedPolygons:
767 coaddInputs.visits.sort()
768 coaddInputs.ccds.sort()
774 modelPsfList = [tempExp.getPsf()
for tempExp
in tempExpList]
775 modelPsfWidthList = [modelPsf.computeBBox(modelPsf.getAveragePosition()).getWidth()
776 for modelPsf
in modelPsfList]
777 psf = modelPsfList[modelPsfWidthList.index(
max(modelPsfWidthList))]
779 psf = measAlg.CoaddPsf(coaddInputs.ccds, coaddExposure.getWcs(),
780 self.config.coaddPsf.makeControl())
781 coaddExposure.setPsf(psf)
782 apCorrMap = measAlg.makeCoaddApCorrMap(coaddInputs.ccds, coaddExposure.getBBox(afwImage.PARENT),
783 coaddExposure.getWcs())
784 coaddExposure.getInfo().setApCorrMap(apCorrMap)
785 if self.config.doAttachTransmissionCurve:
786 transmissionCurve = measAlg.makeCoaddTransmissionCurve(coaddExposure.getWcs(), coaddInputs.ccds)
787 coaddExposure.getInfo().setTransmissionCurve(transmissionCurve)
790 altMaskList, statsFlags, statsCtrl, nImage=None):
791 """Assemble the coadd for a sub-region.
793 For each coaddTempExp, check for (
and swap
in) an alternative mask
794 if one
is passed. Remove mask planes listed
in
795 `config.removeMaskPlanes`. Finally, stack the actual exposures using
796 `lsst.afw.math.statisticsStack`
with the statistic specified by
797 statsFlags. Typically, the statsFlag will be one of lsst.afw.math.MEAN
for
798 a mean-stack
or `lsst.afw.math.MEANCLIP`
for outlier rejection using
799 an N-sigma clipped mean where N
and iterations are specified by
800 statsCtrl. Assign the stacked subregion back to the coadd.
805 The target exposure
for the coadd.
806 bbox : `lsst.geom.Box`
808 tempExpRefList : `list`
809 List of data reference to tempExp.
810 imageScalerList : `list`
811 List of image scalers.
815 List of alternate masks to use rather than those stored
with
816 tempExp,
or None. Each element
is dict
with keys = mask plane
817 name to which to add the spans.
818 statsFlags : `lsst.afw.math.Property`
819 Property object
for statistic
for coadd.
821 Statistics control object
for coadd.
822 nImage : `lsst.afw.image.ImageU`, optional
823 Keeps track of exposure count
for each pixel.
825 self.log.debug("Computing coadd over %s", bbox)
827 coaddExposure.mask.addMaskPlane(
"REJECTED")
828 coaddExposure.mask.addMaskPlane(
"CLIPPED")
829 coaddExposure.mask.addMaskPlane(
"SENSOR_EDGE")
831 clipped = afwImage.Mask.getPlaneBitMask(
"CLIPPED")
833 if nImage
is not None:
834 subNImage = afwImage.ImageU(bbox.getWidth(), bbox.getHeight())
835 for tempExpRef, imageScaler, altMask
in zip(tempExpRefList, imageScalerList, altMaskList):
837 exposure = tempExpRef.get(parameters={
'bbox': bbox})
839 maskedImage = exposure.getMaskedImage()
840 mask = maskedImage.getMask()
841 if altMask
is not None:
843 imageScaler.scaleMaskedImage(maskedImage)
847 if nImage
is not None:
848 subNImage.getArray()[maskedImage.getMask().getArray() & statsCtrl.getAndMask() == 0] += 1
849 if self.config.removeMaskPlanes:
851 maskedImageList.append(maskedImage)
853 if self.config.doInputMap:
854 visit = exposure.getInfo().getCoaddInputs().visits[0].getId()
855 self.inputMapper.mask_warp_bbox(bbox, visit, mask, statsCtrl.getAndMask())
857 with self.timer(
"stack"):
861 coaddExposure.maskedImage.assign(coaddSubregion, bbox)
862 if nImage
is not None:
863 nImage.assign(subNImage, bbox)
866 altMaskList, statsCtrl, nImage=None):
867 """Assemble the coadd using the "online" method.
869 This method takes a running sum of images and weights to save memory.
870 It only works
for MEAN statistics.
875 The target exposure
for the coadd.
876 tempExpRefList : `list`
877 List of data reference to tempExp.
878 imageScalerList : `list`
879 List of image scalers.
883 List of alternate masks to use rather than those stored
with
884 tempExp,
or None. Each element
is dict
with keys = mask plane
885 name to which to add the spans.
887 Statistics control object
for coadd.
888 nImage : `lsst.afw.image.ImageU`, optional
889 Keeps track of exposure count
for each pixel.
891 self.log.debug("Computing online coadd.")
893 coaddExposure.mask.addMaskPlane(
"REJECTED")
894 coaddExposure.mask.addMaskPlane(
"CLIPPED")
895 coaddExposure.mask.addMaskPlane(
"SENSOR_EDGE")
897 thresholdDict = AccumulatorMeanStack.stats_ctrl_to_threshold_dict(statsCtrl)
899 bbox = coaddExposure.maskedImage.getBBox()
902 coaddExposure.image.array.shape,
903 statsCtrl.getAndMask(),
904 mask_threshold_dict=thresholdDict,
906 no_good_pixels_mask=statsCtrl.getNoGoodPixelsMask(),
907 calc_error_from_input_variance=self.config.calcErrorFromInputVariance,
908 compute_n_image=(nImage
is not None)
911 for tempExpRef, imageScaler, altMask, weight
in zip(tempExpRefList,
915 exposure = tempExpRef.get()
916 maskedImage = exposure.getMaskedImage()
917 mask = maskedImage.getMask()
918 if altMask
is not None:
920 imageScaler.scaleMaskedImage(maskedImage)
921 if self.config.removeMaskPlanes:
924 stacker.add_masked_image(maskedImage, weight=weight)
926 if self.config.doInputMap:
927 visit = exposure.getInfo().getCoaddInputs().visits[0].getId()
928 self.inputMapper.mask_warp_bbox(bbox, visit, mask, statsCtrl.getAndMask())
930 stacker.fill_stacked_masked_image(coaddExposure.maskedImage)
932 if nImage
is not None:
933 nImage.array[:, :] = stacker.n_image
936 """Unset the mask of an image for mask planes specified in the config.
941 The masked image to be modified.
945 InvalidParameterError
946 Raised if no mask plane
with that name was found.
948 mask = maskedImage.getMask()
949 for maskPlane
in self.config.removeMaskPlanes:
951 mask &= ~mask.getPlaneBitMask(maskPlane)
953 self.log.debug(
"Unable to remove mask plane %s: no mask plane with that name was found.",
958 """Map certain mask planes of the warps to new planes for the coadd.
960 If a pixel is rejected due to a mask value other than EDGE, NO_DATA,
961 or CLIPPED, set it to REJECTED on the coadd.
962 If a pixel
is rejected due to EDGE, set the coadd pixel to SENSOR_EDGE.
963 If a pixel
is rejected due to CLIPPED, set the coadd pixel to CLIPPED.
968 Statistics control object
for coadd.
972 maskMap : `list` of `tuple` of `int`
973 A list of mappings of mask planes of the warped exposures to
974 mask planes of the coadd.
976 edge = afwImage.Mask.getPlaneBitMask("EDGE")
977 noData = afwImage.Mask.getPlaneBitMask(
"NO_DATA")
978 clipped = afwImage.Mask.getPlaneBitMask(
"CLIPPED")
979 toReject = statsCtrl.getAndMask() & (~noData) & (~edge) & (~clipped)
980 maskMap = [(toReject, afwImage.Mask.getPlaneBitMask(
"REJECTED")),
981 (edge, afwImage.Mask.getPlaneBitMask(
"SENSOR_EDGE")),
986 """Apply in place alt mask formatted as SpanSets to a mask.
992 altMaskSpans : `dict`
993 SpanSet lists to apply. Each element contains the new mask
994 plane name (e.g. "CLIPPED and/or "NO_DATA
") as the key,
995 and list of SpanSets to apply to the mask.
1002 if self.config.doUsePsfMatchedPolygons:
1003 if (
"NO_DATA" in altMaskSpans)
and (
"NO_DATA" in self.config.badMaskPlanes):
1008 for spanSet
in altMaskSpans[
'NO_DATA']:
1009 spanSet.clippedTo(mask.getBBox()).clearMask(mask, self.
getBadPixelMask())
1011 for plane, spanSetList
in altMaskSpans.items():
1012 maskClipValue = mask.addMaskPlane(plane)
1013 for spanSet
in spanSetList:
1014 spanSet.clippedTo(mask.getBBox()).setMask(mask, 2**maskClipValue)
1018 """Shrink coaddInputs' ccds' ValidPolygons in place.
1020 Either modify each ccd's validPolygon in place, or if CoaddInputs
1021 does not have a validPolygon, create one
from its bbox.
1025 coaddInputs : `lsst.afw.image.coaddInputs`
1028 for ccd
in coaddInputs.ccds:
1029 polyOrig = ccd.getValidPolygon()
1030 validPolyBBox = polyOrig.getBBox()
if polyOrig
else ccd.getBBox()
1031 validPolyBBox.grow(-self.config.matchingKernelSize//2)
1033 validPolygon = polyOrig.intersectionSingle(validPolyBBox)
1035 validPolygon = afwGeom.polygon.Polygon(
geom.Box2D(validPolyBBox))
1036 ccd.setValidPolygon(validPolygon)
1039 """Set the bright object masks.
1044 Exposure under consideration.
1046 Table of bright objects to mask.
1047 dataId : `lsst.daf.butler.DataId`, optional
1048 Data identifier dict for patch.
1050 if brightObjectMasks
is None:
1051 self.log.warning(
"Unable to apply bright object mask: none supplied")
1053 self.log.info(
"Applying %d bright object masks to %s", len(brightObjectMasks), dataId)
1054 mask = exposure.getMaskedImage().getMask()
1055 wcs = exposure.getWcs()
1056 plateScale = wcs.getPixelScale().asArcseconds()
1058 for rec
in brightObjectMasks:
1059 center =
geom.PointI(wcs.skyToPixel(rec.getCoord()))
1060 if rec[
"type"] ==
"box":
1061 assert rec[
"angle"] == 0.0, (
"Angle != 0 for mask object %s" % rec[
"id"])
1062 width = rec[
"width"].asArcseconds()/plateScale
1063 height = rec[
"height"].asArcseconds()/plateScale
1066 bbox =
geom.Box2I(center - halfSize, center + halfSize)
1069 geom.PointI(int(center[0] + 0.5*width), int(center[1] + 0.5*height)))
1071 elif rec[
"type"] ==
"circle":
1072 radius = int(rec[
"radius"].asArcseconds()/plateScale)
1073 spans = afwGeom.SpanSet.fromShape(radius, offset=center)
1075 self.log.warning(
"Unexpected region type %s at %s", rec[
"type"], center)
1080 """Set INEXACT_PSF mask plane.
1082 If any of the input images isn't represented in the coadd (due to
1083 clipped pixels or chip gaps), the `CoaddPsf` will be inexact. Flag
1089 Coadded exposure
's mask, modified in-place.
1091 mask.addMaskPlane("INEXACT_PSF")
1092 inexactPsf = mask.getPlaneBitMask(
"INEXACT_PSF")
1093 sensorEdge = mask.getPlaneBitMask(
"SENSOR_EDGE")
1094 clipped = mask.getPlaneBitMask(
"CLIPPED")
1095 rejected = mask.getPlaneBitMask(
"REJECTED")
1096 array = mask.getArray()
1097 selected = array & (sensorEdge | clipped | rejected) > 0
1098 array[selected] |= inexactPsf
1102 """Iterate over subregions of a bbox.
1107 Bounding box over which to iterate.
1114 Next sub-bounding box of size ``subregionSize`` or smaller; each ``subBBox``
1115 is contained within ``bbox``, so it may be smaller than ``subregionSize`` at
1116 the edges of ``bbox``, but it will never be empty.
1121 Raised
if any of the following occur:
1122 - The given bbox
is empty.
1123 - The subregionSize
is 0.
1126 raise RuntimeError(
"bbox %s is empty" % (bbox,))
1127 if subregionSize[0] < 1
or subregionSize[1] < 1:
1128 raise RuntimeError(
"subregionSize %s must be nonzero" % (subregionSize,))
1130 for rowShift
in range(0, bbox.getHeight(), subregionSize[1]):
1131 for colShift
in range(0, bbox.getWidth(), subregionSize[0]):
1134 if subBBox.isEmpty():
1135 raise RuntimeError(
"Bug: empty bbox! bbox=%s, subregionSize=%s, "
1136 "colShift=%s, rowShift=%s" %
1137 (bbox, subregionSize, colShift, rowShift))
1141 """Return list of only inputRefs with visitId in goodVisits ordered by goodVisit.
1145 inputs : `list` of `~lsst.pipe.base.connections.DeferredDatasetRef`
1146 List of `lsst.pipe.base.connections.DeferredDatasetRef` with dataId containing visit.
1148 Dictionary
with good visitIds
as the keys. Value ignored.
1152 filteredInputs : `list` of `~lsst.pipe.base.connections.DeferredDatasetRef`
1153 Filtered
and sorted list of inputRefs
with visitId
in goodVisits ordered by goodVisit.
1155 inputWarpDict = {inputRef.ref.dataId['visit']: inputRef
for inputRef
in inputs}
1157 for visit
in goodVisits.keys():
1158 if visit
in inputWarpDict:
1159 filteredInputs.append(inputWarpDict[visit])
1160 return filteredInputs
1164 """Function to count the number of pixels with a specific mask in a
1167 Find the intersection of mask & footprint. Count all pixels in the mask
1168 that are
in the intersection that have bitmask set but do
not have
1169 ignoreMask set. Return the count.
1174 Mask to define intersection region by.
1176 Footprint to define the intersection region by.
1178 Specific mask that we wish to count the number of occurances of.
1179 ignoreMask : `Unknown`
1180 Pixels to
not consider.
1185 Number of pixels
in footprint
with specified mask.
1187 bbox = footprint.getBBox()
1188 bbox.clip(mask.getBBox(afwImage.PARENT))
1190 subMask = mask.Factory(mask, bbox, afwImage.PARENT)
1191 footprint.spans.setMask(fp, bitmask)
1192 return numpy.logical_and((subMask.getArray() & fp.getArray()) > 0,
1193 (subMask.getArray() & ignoreMask) == 0).sum()
1197 psfMatchedWarps = pipeBase.connectionTypes.Input(
1198 doc=(
"PSF-Matched Warps are required by CompareWarp regardless of the coadd type requested. "
1199 "Only PSF-Matched Warps make sense for image subtraction. "
1200 "Therefore, they must be an additional declared input."),
1201 name=
"{inputCoaddName}Coadd_psfMatchedWarp",
1202 storageClass=
"ExposureF",
1203 dimensions=(
"tract",
"patch",
"skymap",
"visit"),
1207 templateCoadd = pipeBase.connectionTypes.Output(
1208 doc=(
"Model of the static sky, used to find temporal artifacts. Typically a PSF-Matched, "
1209 "sigma-clipped coadd. Written if and only if assembleStaticSkyModel.doWrite=True"),
1210 name=
"{outputCoaddName}CoaddPsfMatched",
1211 storageClass=
"ExposureF",
1212 dimensions=(
"tract",
"patch",
"skymap",
"band"),
1217 if not config.assembleStaticSkyModel.doWrite:
1218 self.outputs.remove(
"templateCoadd")
1223 pipelineConnections=CompareWarpAssembleCoaddConnections):
1224 assembleStaticSkyModel = pexConfig.ConfigurableField(
1225 target=AssembleCoaddTask,
1226 doc=
"Task to assemble an artifact-free, PSF-matched Coadd to serve as a"
1227 " naive/first-iteration model of the static sky.",
1229 detect = pexConfig.ConfigurableField(
1230 target=SourceDetectionTask,
1231 doc=
"Detect outlier sources on difference between each psfMatched warp and static sky model"
1233 detectTemplate = pexConfig.ConfigurableField(
1234 target=SourceDetectionTask,
1235 doc=
"Detect sources on static sky model. Only used if doPreserveContainedBySource is True"
1237 maskStreaks = pexConfig.ConfigurableField(
1238 target=MaskStreaksTask,
1239 doc=
"Detect streaks on difference between each psfMatched warp and static sky model. Only used if "
1240 "doFilterMorphological is True. Adds a mask plane to an exposure, with the mask plane name set by"
1243 streakMaskName = pexConfig.Field(
1246 doc=
"Name of mask bit used for streaks"
1248 maxNumEpochs = pexConfig.Field(
1249 doc=
"Charactistic maximum local number of epochs/visits in which an artifact candidate can appear "
1250 "and still be masked. The effective maxNumEpochs is a broken linear function of local "
1251 "number of epochs (N): min(maxFractionEpochsLow*N, maxNumEpochs + maxFractionEpochsHigh*N). "
1252 "For each footprint detected on the image difference between the psfMatched warp and static sky "
1253 "model, if a significant fraction of pixels (defined by spatialThreshold) are residuals in more "
1254 "than the computed effective maxNumEpochs, the artifact candidate is deemed persistant rather "
1255 "than transient and not masked.",
1259 maxFractionEpochsLow = pexConfig.RangeField(
1260 doc=
"Fraction of local number of epochs (N) to use as effective maxNumEpochs for low N. "
1261 "Effective maxNumEpochs = "
1262 "min(maxFractionEpochsLow * N, maxNumEpochs + maxFractionEpochsHigh * N)",
1267 maxFractionEpochsHigh = pexConfig.RangeField(
1268 doc=
"Fraction of local number of epochs (N) to use as effective maxNumEpochs for high N. "
1269 "Effective maxNumEpochs = "
1270 "min(maxFractionEpochsLow * N, maxNumEpochs + maxFractionEpochsHigh * N)",
1275 spatialThreshold = pexConfig.RangeField(
1276 doc=
"Unitless fraction of pixels defining how much of the outlier region has to meet the "
1277 "temporal criteria. If 0, clip all. If 1, clip none.",
1281 inclusiveMin=
True, inclusiveMax=
True
1283 doScaleWarpVariance = pexConfig.Field(
1284 doc=
"Rescale Warp variance plane using empirical noise?",
1288 scaleWarpVariance = pexConfig.ConfigurableField(
1289 target=ScaleVarianceTask,
1290 doc=
"Rescale variance on warps",
1292 doPreserveContainedBySource = pexConfig.Field(
1293 doc=
"Rescue artifacts from clipping that completely lie within a footprint detected"
1294 "on the PsfMatched Template Coadd. Replicates a behavior of SafeClip.",
1298 doPrefilterArtifacts = pexConfig.Field(
1299 doc=
"Ignore artifact candidates that are mostly covered by the bad pixel mask, "
1300 "because they will be excluded anyway. This prevents them from contributing "
1301 "to the outlier epoch count image and potentially being labeled as persistant."
1302 "'Mostly' is defined by the config 'prefilterArtifactsRatio'.",
1306 prefilterArtifactsMaskPlanes = pexConfig.ListField(
1307 doc=
"Prefilter artifact candidates that are mostly covered by these bad mask planes.",
1309 default=(
'NO_DATA',
'BAD',
'SAT',
'SUSPECT'),
1311 prefilterArtifactsRatio = pexConfig.Field(
1312 doc=
"Prefilter artifact candidates with less than this fraction overlapping good pixels",
1316 doFilterMorphological = pexConfig.Field(
1317 doc=
"Filter artifact candidates based on morphological criteria, i.g. those that appear to "
1322 growStreakFp = pexConfig.Field(
1323 doc=
"Grow streak footprints by this number multiplied by the PSF width",
1329 AssembleCoaddConfig.setDefaults(self)
1335 if "EDGE" in self.badMaskPlanes:
1336 self.badMaskPlanes.remove(
'EDGE')
1337 self.removeMaskPlanes.append(
'EDGE')
1346 self.
detect.doTempLocalBackground =
False
1347 self.
detect.reEstimateBackground =
False
1348 self.
detect.returnOriginalFootprints =
False
1349 self.
detect.thresholdPolarity =
"both"
1350 self.
detect.thresholdValue = 5
1351 self.
detect.minPixels = 4
1352 self.
detect.isotropicGrow =
True
1353 self.
detect.thresholdType =
"pixel_stdev"
1354 self.
detect.nSigmaToGrow = 0.4
1365 raise ValueError(
"No dataset type exists for a PSF-Matched Template N Image."
1366 "Please set assembleStaticSkyModel.doNImage=False")
1369 raise ValueError(
"warpType (%s) == assembleStaticSkyModel.warpType (%s) and will compete for "
1370 "the same dataset name. Please set assembleStaticSkyModel.doWrite to False "
1371 "or warpType to 'direct'. assembleStaticSkyModel.warpType should ways be "
1376 """Assemble a compareWarp coadded image from a set of warps
1377 by masking artifacts detected by comparing PSF-matched warps.
1379 In ``AssembleCoaddTask``, we compute the coadd as an clipped mean (i.e.,
1380 we clip outliers). The problem
with doing this
is that when computing the
1381 coadd PSF at a given location, individual visit PSFs
from visits
with
1382 outlier pixels contribute to the coadd PSF
and cannot be treated correctly.
1383 In this task, we correct
for this behavior by creating a new badMaskPlane
1384 'CLIPPED' which marks pixels
in the individual warps suspected to contain
1385 an artifact. We populate this plane on the input warps by comparing
1386 PSF-matched warps
with a PSF-matched median coadd which serves
as a
1387 model of the static sky. Any group of pixels that deviates
from the
1388 PSF-matched template coadd by more than config.detect.threshold sigma,
1389 is an artifact candidate. The candidates are then filtered to remove
1390 variable sources
and sources that are difficult to subtract such
as
1391 bright stars. This filter
is configured using the config parameters
1392 ``temporalThreshold``
and ``spatialThreshold``. The temporalThreshold
is
1393 the maximum fraction of epochs that the deviation can appear
in and still
1394 be considered an artifact. The spatialThreshold
is the maximum fraction of
1395 pixels
in the footprint of the deviation that appear
in other epochs
1396 (where other epochs
is defined by the temporalThreshold). If the deviant
1397 region meets this criteria of having a significant percentage of pixels
1398 that deviate
in only a few epochs, these pixels have the
'CLIPPED' bit
1399 set
in the mask. These regions will
not contribute to the final coadd.
1400 Furthermore, any routine to determine the coadd PSF can now be cognizant
1401 of clipped regions. Note that the algorithm implemented by this task
is
1402 preliminary
and works correctly
for HSC data. Parameter modifications
and
1403 or considerable redesigning of the algorithm
is likley required
for other
1406 ``CompareWarpAssembleCoaddTask`` sub-classes
1407 ``AssembleCoaddTask``
and instantiates ``AssembleCoaddTask``
1408 as a subtask to generate the TemplateCoadd (the model of the static sky).
1413 This task supports the following debug variables:
1415 If
True then save the Epoch Count Image
as a fits file
in the `figPath`
1417 Path to save the debug fits images
and figures
1420 ConfigClass = CompareWarpAssembleCoaddConfig
1421 _DefaultName = "compareWarpAssembleCoadd"
1423 def __init__(self, *args, **kwargs):
1424 AssembleCoaddTask.__init__(self, *args, **kwargs)
1425 self.makeSubtask(
"assembleStaticSkyModel")
1426 detectionSchema = afwTable.SourceTable.makeMinimalSchema()
1427 self.makeSubtask(
"detect", schema=detectionSchema)
1428 if self.config.doPreserveContainedBySource:
1429 self.makeSubtask(
"detectTemplate", schema=afwTable.SourceTable.makeMinimalSchema())
1430 if self.config.doScaleWarpVariance:
1431 self.makeSubtask(
"scaleWarpVariance")
1432 if self.config.doFilterMorphological:
1433 self.makeSubtask(
"maskStreaks")
1435 @utils.inheritDoc(AssembleCoaddTask)
1437 """Generate a templateCoadd to use as a naive model of static sky to
1438 subtract from PSF-Matched warps.
1442 result : `~lsst.pipe.base.Struct`
1443 Results
as a struct
with attributes:
1448 Keeps track of exposure count
for each pixel (`lsst.afw.image.ImageU`).
1453 Raised
if ``templateCoadd``
is `
None`.
1456 staticSkyModelInputRefs = copy.deepcopy(inputRefs)
1457 staticSkyModelInputRefs.inputWarps = inputRefs.psfMatchedWarps
1461 staticSkyModelOutputRefs = copy.deepcopy(outputRefs)
1462 if self.config.assembleStaticSkyModel.doWrite:
1463 staticSkyModelOutputRefs.coaddExposure = staticSkyModelOutputRefs.templateCoadd
1466 del outputRefs.templateCoadd
1467 del staticSkyModelOutputRefs.templateCoadd
1470 if 'nImage' in staticSkyModelOutputRefs.keys():
1471 del staticSkyModelOutputRefs.nImage
1473 templateCoadd = self.assembleStaticSkyModel.
runQuantum(butlerQC, staticSkyModelInputRefs,
1474 staticSkyModelOutputRefs)
1475 if templateCoadd
is None:
1478 return pipeBase.Struct(templateCoadd=templateCoadd.coaddExposure,
1479 nImage=templateCoadd.nImage,
1480 warpRefList=templateCoadd.warpRefList,
1481 imageScalerList=templateCoadd.imageScalerList,
1482 weightList=templateCoadd.weightList)
1485 warpName = (warpType[0].upper() + warpType[1:])
1486 message =
"""No %(warpName)s warps were found to build the template coadd which is
1487 required to run CompareWarpAssembleCoaddTask. To continue assembling this type of coadd,
1488 first either rerun makeCoaddTempExp
with config.make%(warpName)s=
True or
1489 coaddDriver
with config.makeCoadTempExp.make%(warpName)s=
True, before assembleCoadd.
1491 Alternatively, to use another algorithm
with existing warps, retarget the CoaddDriverConfig to
1492 another algorithm like:
1495 config.assemble.retarget(SafeClipAssembleCoaddTask)
1496 """ % {"warpName": warpName}
1499 @utils.inheritDoc(AssembleCoaddTask)
1501 def run(self, skyInfo, tempExpRefList, imageScalerList, weightList,
1503 """Assemble the coadd.
1505 Find artifacts and apply them to the warps
' masks creating a list of
1506 alternative masks with a new
"CLIPPED" plane
and updated
"NO_DATA"
1507 plane. Then
pass these alternative masks to the base
class's ``run``
1513 dataIds = [ref.dataId
for ref
in tempExpRefList]
1514 psfMatchedDataIds = [ref.dataId
for ref
in supplementaryData.warpRefList]
1516 if dataIds != psfMatchedDataIds:
1517 self.log.info(
"Reordering and or/padding PSF-matched visit input list")
1518 supplementaryData.warpRefList = reorderAndPadList(supplementaryData.warpRefList,
1519 psfMatchedDataIds, dataIds)
1520 supplementaryData.imageScalerList = reorderAndPadList(supplementaryData.imageScalerList,
1521 psfMatchedDataIds, dataIds)
1524 spanSetMaskList = self.
findArtifacts(supplementaryData.templateCoadd,
1525 supplementaryData.warpRefList,
1526 supplementaryData.imageScalerList)
1528 badMaskPlanes = self.config.badMaskPlanes[:]
1529 badMaskPlanes.append(
"CLIPPED")
1530 badPixelMask = afwImage.Mask.getPlaneBitMask(badMaskPlanes)
1532 result = AssembleCoaddTask.run(self, skyInfo, tempExpRefList, imageScalerList, weightList,
1533 spanSetMaskList, mask=badPixelMask)
1537 self.
applyAltEdgeMask(result.coaddExposure.maskedImage.mask, spanSetMaskList)
1541 """Propagate alt EDGE mask to SENSOR_EDGE AND INEXACT_PSF planes.
1547 altMaskList : `list` of `dict`
1548 List of Dicts containing ``spanSet`` lists.
1549 Each element contains the new mask plane name (e.g. "CLIPPED
1550 and/
or "NO_DATA")
as the key,
and list of ``SpanSets`` to apply to
1553 maskValue = mask.getPlaneBitMask(["SENSOR_EDGE",
"INEXACT_PSF"])
1554 for visitMask
in altMaskList:
1555 if "EDGE" in visitMask:
1556 for spanSet
in visitMask[
'EDGE']:
1557 spanSet.clippedTo(mask.getBBox()).setMask(mask, maskValue)
1562 Loop through warps twice. The first loop builds a map with the count
1563 of how many epochs each pixel deviates
from the templateCoadd by more
1564 than ``config.chiThreshold`` sigma. The second loop takes each
1565 difference image
and filters the artifacts detected
in each using
1566 count map to filter out variable sources
and sources that are
1567 difficult to subtract cleanly.
1572 Exposure to serve
as model of static sky.
1573 tempExpRefList : `list`
1574 List of data references to warps.
1575 imageScalerList : `list`
1576 List of image scalers.
1580 altMasks : `list` of `dict`
1581 List of dicts containing information about CLIPPED
1582 (i.e., artifacts), NO_DATA,
and EDGE pixels.
1584 self.log.debug("Generating Count Image, and mask lists.")
1585 coaddBBox = templateCoadd.getBBox()
1586 slateIm = afwImage.ImageU(coaddBBox)
1587 epochCountImage = afwImage.ImageU(coaddBBox)
1588 nImage = afwImage.ImageU(coaddBBox)
1589 spanSetArtifactList = []
1590 spanSetNoDataMaskList = []
1591 spanSetEdgeList = []
1592 spanSetBadMorphoList = []
1596 templateCoadd.mask.clearAllMaskPlanes()
1598 if self.config.doPreserveContainedBySource:
1599 templateFootprints = self.detectTemplate.detectFootprints(templateCoadd)
1601 templateFootprints =
None
1603 for warpRef, imageScaler
in zip(tempExpRefList, imageScalerList):
1605 if warpDiffExp
is not None:
1607 nImage.array += (numpy.isfinite(warpDiffExp.image.array)
1608 * ((warpDiffExp.mask.array & badPixelMask) == 0)).astype(numpy.uint16)
1609 fpSet = self.detect.detectFootprints(warpDiffExp, doSmooth=
False, clearMask=
True)
1610 fpSet.positive.merge(fpSet.negative)
1611 footprints = fpSet.positive
1613 spanSetList = [footprint.spans
for footprint
in footprints.getFootprints()]
1616 if self.config.doPrefilterArtifacts:
1620 self.detect.clearMask(warpDiffExp.mask)
1621 for spans
in spanSetList:
1622 spans.setImage(slateIm, 1, doClip=
True)
1623 spans.setMask(warpDiffExp.mask, warpDiffExp.mask.getPlaneBitMask(
"DETECTED"))
1624 epochCountImage += slateIm
1626 if self.config.doFilterMorphological:
1627 maskName = self.config.streakMaskName
1628 _ = self.maskStreaks.run(warpDiffExp)
1629 streakMask = warpDiffExp.mask
1630 spanSetStreak = afwGeom.SpanSet.fromMask(streakMask,
1631 streakMask.getPlaneBitMask(maskName)).split()
1633 psf = warpDiffExp.getPsf()
1634 for s, sset
in enumerate(spanSetStreak):
1635 psfShape = psf.computeShape(sset.computeCentroid())
1636 dilation = self.config.growStreakFp * psfShape.getDeterminantRadius()
1637 sset_dilated = sset.dilated(int(dilation))
1638 spanSetStreak[s] = sset_dilated
1644 nans = numpy.where(numpy.isnan(warpDiffExp.maskedImage.image.array), 1, 0)
1646 nansMask.setXY0(warpDiffExp.getXY0())
1647 edgeMask = warpDiffExp.mask
1648 spanSetEdgeMask = afwGeom.SpanSet.fromMask(edgeMask,
1649 edgeMask.getPlaneBitMask(
"EDGE")).split()
1653 nansMask = afwImage.MaskX(coaddBBox, 1)
1655 spanSetEdgeMask = []
1658 spanSetNoDataMask = afwGeom.SpanSet.fromMask(nansMask).split()
1660 spanSetNoDataMaskList.append(spanSetNoDataMask)
1661 spanSetArtifactList.append(spanSetList)
1662 spanSetEdgeList.append(spanSetEdgeMask)
1663 if self.config.doFilterMorphological:
1664 spanSetBadMorphoList.append(spanSetStreak)
1667 path = self._dataRef2DebugPath(
"epochCountIm", tempExpRefList[0], coaddLevel=
True)
1668 epochCountImage.writeFits(path)
1670 for i, spanSetList
in enumerate(spanSetArtifactList):
1672 filteredSpanSetList = self.
filterArtifacts(spanSetList, epochCountImage, nImage,
1674 spanSetArtifactList[i] = filteredSpanSetList
1675 if self.config.doFilterMorphological:
1676 spanSetArtifactList[i] += spanSetBadMorphoList[i]
1679 for artifacts, noData, edge
in zip(spanSetArtifactList, spanSetNoDataMaskList, spanSetEdgeList):
1680 altMasks.append({
'CLIPPED': artifacts,
1686 """Remove artifact candidates covered by bad mask plane.
1688 Any future editing of the candidate list that does not depend on
1689 temporal information should go
in this method.
1694 List of SpanSets representing artifact candidates.
1696 Exposure containing mask planes used to prefilter.
1701 List of SpanSets
with artifacts.
1703 badPixelMask = exp.mask.getPlaneBitMask(self.config.prefilterArtifactsMaskPlanes)
1704 goodArr = (exp.mask.array & badPixelMask) == 0
1705 returnSpanSetList = []
1706 bbox = exp.getBBox()
1707 x0, y0 = exp.getXY0()
1708 for i, span
in enumerate(spanSetList):
1709 y, x = span.clippedTo(bbox).indices()
1710 yIndexLocal = numpy.array(y) - y0
1711 xIndexLocal = numpy.array(x) - x0
1712 goodRatio = numpy.count_nonzero(goodArr[yIndexLocal, xIndexLocal])/span.getArea()
1713 if goodRatio > self.config.prefilterArtifactsRatio:
1714 returnSpanSetList.append(span)
1715 return returnSpanSetList
1717 def filterArtifacts(self, spanSetList, epochCountImage, nImage, footprintsToExclude=None):
1718 """Filter artifact candidates.
1723 List of SpanSets representing artifact candidates.
1725 Image of accumulated number of warpDiff detections.
1726 nImage : `lsst.afw.image.ImageU`
1727 Image of the accumulated number of total epochs contributing.
1731 maskSpanSetList : `list`
1732 List of SpanSets with artifacts.
1734 maskSpanSetList = []
1735 x0, y0 = epochCountImage.getXY0()
1736 for i, span
in enumerate(spanSetList):
1737 y, x = span.indices()
1738 yIdxLocal = [y1 - y0
for y1
in y]
1739 xIdxLocal = [x1 - x0
for x1
in x]
1740 outlierN = epochCountImage.array[yIdxLocal, xIdxLocal]
1741 totalN = nImage.array[yIdxLocal, xIdxLocal]
1744 effMaxNumEpochsHighN = (self.config.maxNumEpochs
1745 + self.config.maxFractionEpochsHigh*numpy.mean(totalN))
1746 effMaxNumEpochsLowN = self.config.maxFractionEpochsLow * numpy.mean(totalN)
1747 effectiveMaxNumEpochs = int(
min(effMaxNumEpochsLowN, effMaxNumEpochsHighN))
1748 nPixelsBelowThreshold = numpy.count_nonzero((outlierN > 0)
1749 & (outlierN <= effectiveMaxNumEpochs))
1750 percentBelowThreshold = nPixelsBelowThreshold / len(outlierN)
1751 if percentBelowThreshold > self.config.spatialThreshold:
1752 maskSpanSetList.append(span)
1754 if self.config.doPreserveContainedBySource
and footprintsToExclude
is not None:
1756 filteredMaskSpanSetList = []
1757 for span
in maskSpanSetList:
1759 for footprint
in footprintsToExclude.positive.getFootprints():
1760 if footprint.spans.contains(span):
1764 filteredMaskSpanSetList.append(span)
1765 maskSpanSetList = filteredMaskSpanSetList
1767 return maskSpanSetList
1770 """Fetch a warp from the butler and return a warpDiff.
1774 warpRef : `lsst.daf.butler.DeferredDatasetHandle`
1775 Handle for the warp.
1777 An image scaler object.
1779 Exposure to be substracted
from the scaled warp.
1784 Exposure of the image difference between the warp
and template.
1791 warp = warpRef.get()
1793 imageScaler.scaleMaskedImage(warp.getMaskedImage())
1794 mi = warp.getMaskedImage()
1795 if self.config.doScaleWarpVariance:
1797 self.scaleWarpVariance.run(mi)
1798 except Exception
as exc:
1799 self.log.warning(
"Unable to rescale variance of warp (%s); leaving it as-is", exc)
1800 mi -= templateCoadd.getMaskedImage()
A compact representation of a collection of pixels.
A class to contain the data, WCS, and other information needed to describe an image of the sky.
A group of labels for a filter in an exposure or coadd.
A class to represent a 2-dimensional array of pixels.
Represent a 2-dimensional array of bitmask pixels.
A class to manipulate images, masks, and variance as a single object.
Pass parameters to a Statistics object.
A floating-point coordinate rectangle geometry.
An integer coordinate rectangle.
Reports invalid arguments.
removeMaskPlanes(self, maskedImage)
runQuantum(self, butlerQC, inputRefs, outputRefs)
assembleMetadata(self, coaddExposure, tempExpRefList, weightList)
assembleOnlineMeanCoadd(self, coaddExposure, tempExpRefList, imageScalerList, weightList, altMaskList, statsCtrl, nImage=None)
processResults(self, coaddExposure, brightObjectMasks=None, dataId=None)
shrinkValidPolygons(self, coaddInputs)
assembleSubregion(self, coaddExposure, bbox, tempExpRefList, imageScalerList, weightList, altMaskList, statsFlags, statsCtrl, nImage=None)
setBrightObjectMasks(self, exposure, brightObjectMasks, dataId=None)
_subBBoxIter(bbox, subregionSize)
_makeSupplementaryData(self, butlerQC, inputRefs, outputRefs)
makeSupplementaryDataGen3(self, butlerQC, inputRefs, outputRefs)
filterWarps(self, inputs, goodVisits)
setRejectedMaskMapping(statsCtrl)
setInexactPsf(self, mask)
prepareStats(self, mask=None)
applyAltMaskPlanes(self, mask, altMaskSpans)
run(self, skyInfo, tempExpRefList, imageScalerList, weightList, altMaskList=None, mask=None, supplementaryData=None)
prepareInputs(self, refList)
__init__(self, *config=None)
_readAndComputeWarpDiff(self, warpRef, imageScaler, templateCoadd)
applyAltEdgeMask(self, mask, altMaskList)
_makeSupplementaryData(self, butlerQC, inputRefs, outputRefs)
findArtifacts(self, templateCoadd, tempExpRefList, imageScalerList)
_noTemplateMessage(self, warpType)
prefilterArtifacts(self, spanSetList, exp)
filterArtifacts(self, spanSetList, epochCountImage, nImage, footprintsToExclude=None)
getTempExpDatasetName(self, warpType="direct")
std::shared_ptr< lsst::afw::image::Image< PixelT > > statisticsStack(std::vector< std::shared_ptr< lsst::afw::image::Image< PixelT > > > &images, Property flags, StatisticsControl const &sctrl=StatisticsControl(), std::vector< lsst::afw::image::VariancePixel > const &wvector=std::vector< lsst::afw::image::VariancePixel >(0))
A function to compute some statistics of a stack of Images.
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
Property stringToStatisticsProperty(std::string const property)
Conversion function to switch a string to a Property (see Statistics.h)
void setCoaddEdgeBits(lsst::afw::image::Mask< lsst::afw::image::MaskPixel > &coaddMask, lsst::afw::image::Image< WeightPixelT > const &weightMap)
set edge bits of coadd mask based on weight map
countMaskFromFootprint(mask, footprint, bitmask, ignoreMask)