LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Static Public Attributes | List of all members
lsst.pipe.tasks.repair.RepairTask Class Reference

Interpolate over defects in an exposure and handle cosmic rays. More...

Inheritance diagram for lsst.pipe.tasks.repair.RepairTask:

Public Member Functions

def run
 Repair an Exposure's defects and cosmic rays. More...
 
def interpolate
 
def cosmicRay
 

Static Public Attributes

 ConfigClass = RepairConfig
 

Detailed Description

Interpolate over defects in an exposure and handle cosmic rays.

Contents

Description

RepairTask

This task operates on an lsst.afw.image.Exposure in place to interpolate over a set of lsst.meas.algorithms.Defect objects. It will also, optionally, find and interpolate any cosmic rays in the lsst.afw.image.Exposure.

Task initialization

See: lsst.pipe.base.task.Task.__init__

Inputs/Outputs to the run method

Repair an Exposure's defects and cosmic rays.

Parameters
[in,out]exposurelsst.afw.image.Exposure to process. Exposure must have a valid Psf. Modified in place.
[in]defectsan lsst.meas.algorithms.DefectListT object. If None, do no defect correction.
[in]keepCRsdon't interpolate over the CR pixels (defer to RepairConfig if None)
Exceptions
AssertionErrorwith the following strings:
No exposure provided
The object provided as exposure evaluates to False
No PSF provided
The Exposure has no associated Psf

Configuration parameters

See RepairConfig

Debug variables

The command line task interface supports a flag -d to import debug.py from your PYTHONPATH; see Using lsstDebug to control debugging output for more about debug.py files.

The available variables in RepairTask are:

display
A dictionary containing debug point names as keys with frame number as value. Valid keys are:
repair.before
display image before any repair is done
repair.after
display image after cosmic ray and defect correction
displayCR
If True, display the exposure on ds9's frame 1 and overlay bounding boxes around detects CRs.

A complete example of using RepairTask

This code is in runRepair.py in the examples directory, and can be run as e.g.

1 examples/runRepair.py
Import the task. There are other imports. Read the source file for more info.
1 from lsst.pipe.tasks.repair import RepairTask

For this example, we manufacture a test image to run on.

First, create a pure Poisson noise image and a Psf to go with it. The mask plane and variance can be constructed at the same time.

1  randArr = numpy.random.poisson(1000., xsize*ysize)
2  randArr = numpy.array(randArr.reshape(ysize, xsize), dtype=numpy.float32) # force to ImageF
3  factory = measAlg.GaussianPsfFactory()
4  factory.addWing = False
5  psf = factory.apply(4) # FWHM in pixels
6 
7  img = afwImage.makeImageFromArray(randArr)
8  var = afwImage.ImageF(img, True) #copy constructor
9  mask = afwImage.MaskU(xsize, ysize)

Inject some cosmic rays and generate the Exposure. Exposures are MaskedImages (image + mask + variance) with other metadata (e.g. Psf and Wcs objects).

1  # set some CRs
2  for xi, yi in zip(xind, yind):
3  xi, yi = int(xi), int(yi)
4  img.set(xi, yi, 1e6)
5 
6  mi = afwImage.makeMaskedImage(img, mask, var)
7  exp = afwImage.makeExposure(mi)
8  exp.setPsf(psf)

Defects are represented as bad columns of random lengths. A defect list must be constructed to pass on to the RepairTask.

Bug:
This is addressed in DM-963

1 def addDefects(exp, nBadCols=10):
2  img = exp.getMaskedImage().getImage()
3  (xsize, ysize) = img.getDimensions()
4  defectList = measAlg.DefectListT()
5 
6  # set some bad cols and add them to a defect list
7  for xi in numpy.random.randint(0, xsize, nBadCols):
8  yi = numpy.random.randint(0, ysize)
9  xi, yi = int(xi), int(yi)
10  bbox = afwGeom.Box2I(afwGeom.PointI(xi, 0), afwGeom.ExtentI(1, yi+1))
11  subIm = afwImage.ImageF(img, bbox)
12  subIm.set(1e7)
13  defectList.push_back(measAlg.Defect(bbox))

Finally, the exposure can be repaired. Create an instance of the task and run it. The exposure is modified in place.

1  repair = RepairTask(name="RepairTask")
2  repair.run(exp, defects=defectList)


To investigate the Debug variables, put something like

1 import lsstDebug
2 def DebugInfo(name):
3  di = lsstDebug.getInfo(name) # N.b. lsstDebug.Info(name) would call us recursively
4  if name == "lsst.pipe.tasks.repair":
5  di.display = {'repair.before':2, 'repair.after':3}
6  di.displayCR = True
7  return di
8 
9 lsstDebug.Info = DebugInfo

into your debug.py file and run runRepair.py with the –debug flag.

Conversion notes: Display code should be updated once we settle on a standard way of controlling what is displayed.

Definition at line 53 of file repair.py.

Member Function Documentation

def lsst.pipe.tasks.repair.RepairTask.cosmicRay (   self,
  exposure,
  keepCRs = None 
)
Mask cosmic rays

@param[in,out] exposure Exposure to process
@param keepCRs  Don't interpolate over the CR pixels (defer to pex_config if None)

Definition at line 208 of file repair.py.

209  def cosmicRay(self, exposure, keepCRs=None):
210  """Mask cosmic rays
211 
212  @param[in,out] exposure Exposure to process
213  @param keepCRs Don't interpolate over the CR pixels (defer to pex_config if None)
214  """
215  import lsstDebug
216  display = lsstDebug.Info(__name__).display
217  displayCR = lsstDebug.Info(__name__).displayCR
218 
219  assert exposure, "No exposure provided"
220  psf = exposure.getPsf()
221  assert psf, "No psf provided"
222 
223  # Blow away old mask
224  try:
225  mask = exposure.getMaskedImage().getMask()
226  crBit = mask.getMaskPlane("CR")
227  mask.clearMaskPlane(crBit)
228  except Exception:
229  pass
230 
231  mi = exposure.getMaskedImage()
232  bg = afwMath.makeStatistics(mi, afwMath.MEDIAN).getValue()
233 
234  if keepCRs is None:
235  keepCRs = self.config.cosmicray.keepCRs
236  try:
237  crs = measAlg.findCosmicRays(mi, psf, bg, pexConfig.makePolicy(self.config.cosmicray), keepCRs)
238  except Exception, e:
239  if display:
240  import lsst.afw.display.ds9 as ds9
241  ds9.mtv(exposure, title="Failed CR")
242  raise
243 
244  num = 0
245  if crs is not None:
246  mask = mi.getMask()
247  crBit = mask.getPlaneBitMask("CR")
248  afwDet.setMaskFromFootprintList(mask, crs, crBit)
249  num = len(crs)
250 
251  if display and displayCR:
252  import lsst.afw.display.ds9 as ds9
253  import lsst.afw.display.utils as displayUtils
254 
255  ds9.incrDefaultFrame()
256  ds9.mtv(exposure, title="Post-CR")
257 
258  with ds9.Buffering():
259  for cr in crs:
260  displayUtils.drawBBox(cr.getBBox(), borderWidth=0.55)
261 
262  self.log.info("Identified %s cosmic rays." % (num,))
263 
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.
Definition: CR.cc:341
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1082
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&#39;s pixels which are in the set of Footprints.
def lsst.pipe.tasks.repair.RepairTask.interpolate (   self,
  exposure,
  defects 
)
Interpolate over defects

@param[in,out] exposure Exposure to process
@param defects Defect list

Definition at line 192 of file repair.py.

193  def interpolate(self, exposure, defects):
194  """Interpolate over defects
195 
196  @param[in,out] exposure Exposure to process
197  @param defects Defect list
198  """
199  assert exposure, "No exposure provided"
200  assert defects is not None, "No defects provided"
201  psf = exposure.getPsf()
202  assert psf, "No psf provided"
203 
204  mi = exposure.getMaskedImage()
205  fallbackValue = afwMath.makeStatistics(mi, afwMath.MEANCLIP).getValue()
206  measAlg.interpolateOverDefects(mi, psf, defects, fallbackValue)
207  self.log.info("Interpolated over %d defects." % len(defects))
void interpolateOverDefects(MaskedImageT &image, lsst::afw::detection::Psf const &psf, std::vector< Defect::Ptr > &badList, double fallbackValue=0.0, bool useFallbackValueAtEdge=false)
Process a set of known bad pixels in an image.
Definition: Interp.cc:2058
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1082
def lsst.pipe.tasks.repair.RepairTask.run (   self,
  exposure,
  defects = None,
  keepCRs = None 
)

Repair an Exposure's defects and cosmic rays.

Parameters
[in,out]exposurelsst.afw.image.Exposure to process. Exposure must have a valid Psf. Modified in place.
[in]defectsan lsst.meas.algorithms.DefectListT object. If None, do no defect correction.
[in]keepCRsdon't interpolate over the CR pixels (defer to RepairConfig if None)
Exceptions
AssertionErrorwith the following strings:
No exposure provided
The object provided as exposure evaluates to False
No PSF provided
The Exposure has no associated Psf

Definition at line 163 of file repair.py.

164  def run(self, exposure, defects=None, keepCRs=None):
165  """!Repair an Exposure's defects and cosmic rays
166 
167  \param[in, out] exposure lsst.afw.image.Exposure to process. Exposure must have a valid Psf. Modified in place.
168  \param[in] defects an lsst.meas.algorithms.DefectListT object. If None, do no defect correction.
169  \param[in] keepCRs don't interpolate over the CR pixels (defer to RepairConfig if None)
170 
171  \throws AssertionError with the following strings:
172 
173  <DL>
174  <DT> No exposure provided
175  <DD> The object provided as exposure evaluates to False
176  <DT> No PSF provided
177  <DD> The Exposure has no associated Psf
178  </DL>
179  """
180  assert exposure, "No exposure provided"
181  psf = exposure.getPsf()
182  assert psf, "No PSF provided"
183 
184  self.display('repair.before', exposure=exposure)
185  if defects is not None and self.config.doInterpolate:
186  self.interpolate(exposure, defects)
187 
188  if self.config.doCosmicRay:
189  self.cosmicRay(exposure, keepCRs=keepCRs)
190 
191  self.display('repair.after', exposure=exposure)
def run
Repair an Exposure&#39;s defects and cosmic rays.
Definition: repair.py:163

Member Data Documentation

lsst.pipe.tasks.repair.RepairTask.ConfigClass = RepairConfig
static

Definition at line 160 of file repair.py.


The documentation for this class was generated from the following file: