27from lsstDebug
import getDebugFrame
30from lsst.utils.timer
import timeMethod
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.
interpinterp.useFallbackValueAtEdge =
True
55 self.
interpinterp.fallbackValueType =
"MEANCLIP"
56 self.
interpinterp.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
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 command line task interface supports a
104 flag
@c -d to
import @b debug.py
from your
@c PYTHONPATH; see <a
105 href=
"https://developer.lsst.io/stack/debug.html">Debugging Tasks
with lsstDebug</a>
for more
106 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 display
'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
161 if name ==
"lsst.pipe.tasks.repair":
162 di.display = {
'repair.before':2,
'repair.after':3}
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
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"
207 afwDisplay.Display(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.
cosmicRaycosmicRay(exposure, keepCRs=keepCRs)
217 afwDisplay.Display(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)
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
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.makePropertySet(self.config.cosmicray), keepCRs)
265 img = exposure.getMaskedImage()
266 img += modelBg.getImageF()
269 exposure0.setMaskedImage(exposure.getMaskedImage())
273 afwDisplay.Display().
mtv(exposure0, title=
"Failed CR")
278 mask = exposure0.getMaskedImage().getMask()
279 crBit = mask.getPlaneBitMask(
"CR")
280 afwDet.setMaskFromFootprintList(mask, crs, crBit)
283 if display
and displayCR:
284 disp = afwDisplay.Display()
285 disp.incrDefaultFrame()
286 disp.mtv(exposure0, title=
"Post-CR")
288 with disp.Buffering():
290 afwDisplay.utils.drawBBox(cr.getBBox(), borderWidth=0.55)
292 self.log.
info(
"Identified %s cosmic rays.", num)
A class to contain the data, WCS, and other information needed to describe an image of the sky.
Encapsulate information about a bad portion of a detector.
def __init__(self, **kwargs)
def run(self, exposure, defects=None, keepCRs=None)
Repair an Exposure's defects and cosmic rays.
def cosmicRay(self, exposure, keepCRs=None)
def mtv(data, frame=None, title="", wcs=None, *args, **kwargs)
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)
Fit spatial kernel using approximate fluxes for candidates, and solving a linear system of equations.
def getDebugFrame(debugDisplay, name)