22 from __future__
import absolute_import, division, print_function
23 import lsst.pex.config
as pexConfig
24 import lsst.afw.math
as afwMath
25 import lsst.afw.detection
as afwDet
26 import lsst.meas.algorithms
as measAlg
27 import lsst.pipe.base
as pipeBase
28 from lsstDebug
import getDebugFrame
29 from lsst.afw.display
import getDisplay
34 doInterpolate = pexConfig.Field(
36 doc=
"Interpolate over defects? (ignored unless you provide a list of defects)",
39 doCosmicRay = pexConfig.Field(
41 doc=
"Find and mask out cosmic rays?",
44 cosmicray = pexConfig.ConfigField(
45 dtype=measAlg.FindCosmicRaysConfig,
46 doc=
"Options for finding and masking cosmic rays",
48 interp = pexConfig.ConfigurableField(
49 target=InterpImageTask,
50 doc=
"Interpolate over bad image pixels",
54 self.
interp.useFallbackValueAtEdge =
True 55 self.
interp.fallbackValueType =
"MEANCLIP" 56 self.
interp.negativeFallbackAllowed =
True 70 \brief Interpolate over defects in an exposure and handle cosmic rays 72 \section pipe_tasks_repair_Contents Contents 74 - \ref pipe_tasks_repair_Purpose 75 - \ref pipe_tasks_repair_Initialize 76 - \ref pipe_tasks_repair_IO 77 - \ref pipe_tasks_repair_Config 78 - \ref pipe_tasks_repair_Debug 79 - \ref pipe_tasks_repair_Example 81 \section pipe_tasks_repair_Purpose Description 85 This task operates on an lsst.afw.image.Exposure in place to interpolate over a set of 86 lsst.meas.algorithms.Defect objects. 87 It will also, optionally, find and interpolate any cosmic rays in the lsst.afw.image.Exposure. 89 \section pipe_tasks_repair_Initialize Task initialization 91 See: lsst.pipe.base.task.Task.__init__ 93 \section pipe_tasks_repair_IO Inputs/Outputs to the run method 97 \section pipe_tasks_repair_Config Configuration parameters 101 \section pipe_tasks_repair_Debug Debug variables 103 The \link lsst.pipe.base.cmdLineTask.CmdLineTask command line task\endlink interface supports a 104 flag \c -d to import \b debug.py from your \c PYTHONPATH; see <a 105 href="http://lsst-web.ncsa.illinois.edu/~buildbot/doxygen/x_masterDoxyDoc/base_debug.html"> 106 Using lsstDebug to control debugging output</a> for more about \b debug.py files. 108 The available variables in RepairTask are: 111 <DD> A dictionary containing debug point names as keys with frame number as value. Valid keys are: 114 <DD> display image before any repair is done 116 <DD> display image after cosmic ray and defect correction 119 <DD> If True, display the exposure on ds9's frame 1 and overlay bounding boxes around detects CRs. 121 \section pipe_tasks_repair_Example A complete example of using RepairTask 123 This code is in runRepair.py in the examples directory, and can be run as \em e.g. 125 examples/runRepair.py 127 \dontinclude runRepair.py 128 Import the task. There are other imports. Read the source file for more info. 131 For this example, we manufacture a test image to run on. 133 First, create a pure Poisson noise image and a Psf to go with it. The mask plane 134 and variance can be constructed at the same time. 138 Inject some cosmic rays and generate the Exposure. Exposures are MaskedImages (image + mask + variance) 139 with other metadata (e.g. Psf and Wcs objects). 143 Defects are represented as bad columns of random lengths. A defect list must be constructed to pass 144 on to the RepairTask. 145 \bug This is addressed in <a href="https://jira.lsstcorp.org/browse/DM-963"> DM-963</a> 150 Finally, the exposure can be repaired. Create an instance of the task and run it. The exposure 151 is modified in place. 156 To investigate the \ref pipe_tasks_repair_Debug, put something like 160 di = lsstDebug.getInfo(name) # N.b. lsstDebug.Info(name) would call us recursively 161 if name == "lsst.pipe.tasks.repair": 162 di.display = {'repair.before':2, 'repair.after':3} 166 lsstDebug.Info = DebugInfo 168 into your debug.py file and run runRepair.py with the \c --debug flag. 172 Display code should be updated once we settle on a standard way of controlling what is displayed. 174 ConfigClass = RepairConfig
175 _DefaultName =
"repair" 178 pipeBase.Task.__init__(self, **kwargs)
179 if self.config.doInterpolate:
180 self.makeSubtask(
"interp")
183 def run(self, exposure, defects=None, keepCRs=None):
184 """!Repair an Exposure's defects and cosmic rays 186 \param[in, out] exposure lsst.afw.image.Exposure to process. Exposure must have a valid Psf. 188 \param[in] defects an lsst.meas.algorithms.DefectListT object. If None, do no 190 \param[in] keepCRs don't interpolate over the CR pixels (defer to RepairConfig if None) 192 \throws AssertionError with the following strings: 195 <DT> No exposure provided 196 <DD> The object provided as exposure evaluates to False 198 <DD> The Exposure has no associated Psf 201 assert exposure,
"No exposure provided" 202 psf = exposure.getPsf()
203 assert psf,
"No PSF provided" 205 frame = getDebugFrame(self._display,
"repair.before")
207 getDisplay(frame).mtv(exposure)
209 if defects
is not None and self.config.doInterpolate:
210 self.interp.
run(exposure, defects=defects)
212 if self.config.doCosmicRay:
213 self.
cosmicRay(exposure, keepCRs=keepCRs)
215 frame = getDebugFrame(self._display,
"repair.after")
217 getDisplay(frame).mtv(exposure)
222 \param[in,out] exposure Exposure to process 223 \param[in] keepCRs Don't interpolate over the CR pixels (defer to pex_config if None) 226 display = lsstDebug.Info(__name__).display
227 displayCR = lsstDebug.Info(__name__).displayCR
229 assert exposure,
"No exposure provided" 230 psf = exposure.getPsf()
231 assert psf,
"No psf provided" 235 mask = exposure.getMaskedImage().getMask()
236 crBit = mask.getMaskPlane(
"CR")
237 mask.clearMaskPlane(crBit)
242 binSize = self.config.cosmicray.background.binSize
243 nx, ny = exposure.getWidth()/binSize, exposure.getHeight()/binSize
247 medianBg = afwMath.makeStatistics(exposure.getMaskedImage(), afwMath.MEDIAN).getValue()
253 exposure = exposure.Factory(exposure,
True)
254 subtractBackgroundTask = measAlg.SubtractBackgroundTask(config=self.config.cosmicray.background)
255 modelBg = subtractBackgroundTask.run(exposure).background
259 keepCRs = self.config.cosmicray.keepCRs
261 crs = measAlg.findCosmicRays(exposure.getMaskedImage(), psf, medianBg,
262 pexConfig.makePolicy(self.config.cosmicray), keepCRs)
265 img = exposure.getMaskedImage()
266 img += modelBg.getImageF()
269 exposure0.setMaskedImage(exposure.getMaskedImage())
273 import lsst.afw.display.ds9
as ds9
274 ds9.mtv(exposure0, title=
"Failed CR")
279 mask = exposure0.getMaskedImage().getMask()
280 crBit = mask.getPlaneBitMask(
"CR")
281 afwDet.setMaskFromFootprintList(mask, crs, crBit)
284 if display
and displayCR:
285 import lsst.afw.display.ds9
as ds9
286 import lsst.afw.display.utils
as displayUtils
288 ds9.incrDefaultFrame()
289 ds9.mtv(exposure0, title=
"Post-CR")
291 with ds9.Buffering():
293 displayUtils.drawBBox(cr.getBBox(), borderWidth=0.55)
295 self.log.info(
"Identified %s cosmic rays." % (num,))
def run(self, exposure, defects=None, keepCRs=None)
Repair an Exposure's defects and cosmic rays.
def __init__(self, kwargs)
def cosmicRay(self, exposure, keepCRs=None)
Interpolate over defects in an exposure and handle cosmic rays.