27 from lsstDebug
import getDebugFrame
32 doInterpolate = pexConfig.Field(
34 doc =
"Interpolate over defects? (ignored unless you provide a list of defects)",
37 doCosmicRay = pexConfig.Field(
39 doc =
"Find and mask out cosmic rays?",
42 cosmicray = pexConfig.ConfigField(
43 dtype = measAlg.FindCosmicRaysConfig,
44 doc =
"Options for finding and masking cosmic rays",
46 interp = pexConfig.ConfigurableField(
47 target = InterpImageTask,
48 doc =
"Interpolate over bad image pixels",
52 self.interp.useFallbackValueAtEdge =
True
53 self.interp.fallbackValueType =
"MEANCLIP"
54 self.interp.negativeFallbackAllowed =
True
67 \brief Interpolate over defects in an exposure and handle cosmic rays
69 \section pipe_tasks_repair_Contents Contents
71 - \ref pipe_tasks_repair_Purpose
72 - \ref pipe_tasks_repair_Initialize
73 - \ref pipe_tasks_repair_IO
74 - \ref pipe_tasks_repair_Config
75 - \ref pipe_tasks_repair_Debug
76 - \ref pipe_tasks_repair_Example
78 \section pipe_tasks_repair_Purpose Description
82 This task operates on an lsst.afw.image.Exposure in place to interpolate over a set of
83 lsst.meas.algorithms.Defect objects.
84 It will also, optionally, find and interpolate any cosmic rays in the lsst.afw.image.Exposure.
86 \section pipe_tasks_repair_Initialize Task initialization
88 See: lsst.pipe.base.task.Task.__init__
90 \section pipe_tasks_repair_IO Inputs/Outputs to the run method
94 \section pipe_tasks_repair_Config Configuration parameters
98 \section pipe_tasks_repair_Debug Debug variables
100 The \link lsst.pipe.base.cmdLineTask.CmdLineTask command line task\endlink interface supports a
101 flag \c -d to import \b debug.py from your \c PYTHONPATH; see <a
102 href="http://lsst-web.ncsa.illinois.edu/~buildbot/doxygen/x_masterDoxyDoc/base_debug.html">
103 Using lsstDebug to control debugging output</a> for more about \b debug.py files.
105 The available variables in RepairTask are:
108 <DD> A dictionary containing debug point names as keys with frame number as value. Valid keys are:
111 <DD> display image before any repair is done
113 <DD> display image after cosmic ray and defect correction
116 <DD> If True, display the exposure on ds9's frame 1 and overlay bounding boxes around detects CRs.
118 \section pipe_tasks_repair_Example A complete example of using RepairTask
120 This code is in runRepair.py in the examples directory, and can be run as \em e.g.
122 examples/runRepair.py
124 \dontinclude runRepair.py
125 Import the task. There are other imports. Read the source file for more info.
128 For this example, we manufacture a test image to run on.
130 First, create a pure Poisson noise image and a Psf to go with it. The mask plane
131 and variance can be constructed at the same time.
135 Inject some cosmic rays and generate the Exposure. Exposures are MaskedImages (image + mask + variance)
136 with other metadata (e.g. Psf and Wcs objects).
140 Defects are represented as bad columns of random lengths. A defect list must be constructed to pass
141 on to the RepairTask.
142 \bug This is addressed in <a href="https://jira.lsstcorp.org/browse/DM-963"> DM-963</a>
147 Finally, the exposure can be repaired. Create an instance of the task and run it. The exposure
148 is modified in place.
153 To investigate the \ref pipe_tasks_repair_Debug, put something like
157 di = lsstDebug.getInfo(name) # N.b. lsstDebug.Info(name) would call us recursively
158 if name == "lsst.pipe.tasks.repair":
159 di.display = {'repair.before':2, 'repair.after':3}
163 lsstDebug.Info = DebugInfo
165 into your debug.py file and run runRepair.py with the \c --debug flag.
169 Display code should be updated once we settle on a standard way of controlling what is displayed.
171 ConfigClass = RepairConfig
172 _DefaultName =
"repair"
175 pipeBase.Task.__init__(self, **kwargs)
176 if self.config.doInterpolate:
177 self.makeSubtask(
"interp")
180 def run(self, exposure, defects=None, keepCRs=None):
181 """!Repair an Exposure's defects and cosmic rays
183 \param[in, out] exposure lsst.afw.image.Exposure to process. Exposure must have a valid Psf.
185 \param[in] defects an lsst.meas.algorithms.DefectListT object. If None, do no
187 \param[in] keepCRs don't interpolate over the CR pixels (defer to RepairConfig if None)
189 \throws AssertionError with the following strings:
192 <DT> No exposure provided
193 <DD> The object provided as exposure evaluates to False
195 <DD> The Exposure has no associated Psf
198 assert exposure,
"No exposure provided"
199 psf = exposure.getPsf()
200 assert psf,
"No PSF provided"
206 if defects
is not None and self.config.doInterpolate:
207 self.interp.run(exposure, defects=defects)
209 if self.config.doCosmicRay:
210 self.
cosmicRay(exposure, keepCRs=keepCRs)
219 \param[in,out] exposure Exposure to process
220 \param[in] keepCRs Don't interpolate over the CR pixels (defer to pex_config if None)
226 assert exposure,
"No exposure provided"
227 psf = exposure.getPsf()
228 assert psf,
"No psf provided"
232 mask = exposure.getMaskedImage().getMask()
233 crBit = mask.getMaskPlane(
"CR")
234 mask.clearMaskPlane(crBit)
239 binSize = self.config.cosmicray.background.binSize
240 nx, ny = exposure.getWidth()/binSize, exposure.getHeight()/binSize
250 exposure = exposure.Factory(exposure,
True)
251 subtractBackgroundTask = measAlg.SubtractBackgroundTask(config=self.config.cosmicray.background)
252 modelBg = subtractBackgroundTask.run(exposure).background
256 keepCRs = self.config.cosmicray.keepCRs
259 pexConfig.makePolicy(self.config.cosmicray), keepCRs)
262 img = exposure.getMaskedImage()
263 img += modelBg.getImageF()
266 exposure0.setMaskedImage(exposure.getMaskedImage())
271 ds9.mtv(exposure0, title=
"Failed CR")
276 mask = exposure0.getMaskedImage().getMask()
277 crBit = mask.getPlaneBitMask(
"CR")
281 if display
and displayCR:
285 ds9.incrDefaultFrame()
286 ds9.mtv(exposure0, title=
"Post-CR")
288 with ds9.Buffering():
290 displayUtils.drawBBox(cr.getBBox(), borderWidth=0.55)
292 self.log.info(
"Identified %s cosmic rays." % (num,))
def run
Repair an Exposure's defects and cosmic rays.
std::vector< detection::Footprint::Ptr > findCosmicRays(MaskedImageT &mimage, detection::Psf const &psf, double const bkgd, lsst::pex::policy::Policy const &policy, bool const keep)
Find cosmic rays in an Image, and mask and remove them.
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
MaskT setMaskFromFootprintList(lsst::afw::image::Mask< MaskT > *mask, boost::shared_ptr< std::vector< boost::shared_ptr< Footprint >> const > const &footprints, MaskT const bitmask)
OR bitmask into all the Mask's pixels which are in the set of Footprints.
Interpolate over defects in an exposure and handle cosmic rays.