LSSTApplications  20.0.0
LSSTDataManagementBasePackage
Classes | Functions | Variables
lsst.ip.isr.crosstalk Namespace Reference

Classes

class  CrosstalkConfig
 
class  CrosstalkTask
 
class  NullCrosstalkTask
 

Functions

def extractAmp (image, amp, corner, isTrimmed=False)
 
def calculateBackground (mi, badPixels=["BAD"])
 
def subtractCrosstalk (exposure, crosstalkCoeffs=None, badPixels=["BAD"], minPixelToMask=45000, crosstalkStr="CROSSTALK", isTrimmed=False, backgroundMethod="None")
 
def writeCrosstalkCoeffs (outputFileName, coeff, det=None, crosstalkName="Unknown", indent=2)
 

Variables

dictionary X_FLIP
 
dictionary Y_FLIP
 

Function Documentation

◆ calculateBackground()

def lsst.ip.isr.crosstalk.calculateBackground (   mi,
  badPixels = ["BAD"] 
)
Calculate median background in image

Getting a great background model isn't important for crosstalk correction,
since the crosstalk is at a low level. The median should be sufficient.

Parameters
----------
mi : `lsst.afw.image.MaskedImage`
    MaskedImage for which to measure background.
badPixels : `list` of `str`
    Mask planes to ignore.

Returns
-------
bg : `float`
    Median background level.

Definition at line 253 of file crosstalk.py.

253 def calculateBackground(mi, badPixels=["BAD"]):
254  """Calculate median background in image
255 
256  Getting a great background model isn't important for crosstalk correction,
257  since the crosstalk is at a low level. The median should be sufficient.
258 
259  Parameters
260  ----------
261  mi : `lsst.afw.image.MaskedImage`
262  MaskedImage for which to measure background.
263  badPixels : `list` of `str`
264  Mask planes to ignore.
265 
266  Returns
267  -------
268  bg : `float`
269  Median background level.
270  """
271  mask = mi.getMask()
273  stats.setAndMask(mask.getPlaneBitMask(badPixels))
274  return lsst.afw.math.makeStatistics(mi, lsst.afw.math.MEDIAN, stats).getValue()
275 
276 

◆ extractAmp()

def lsst.ip.isr.crosstalk.extractAmp (   image,
  amp,
  corner,
  isTrimmed = False 
)
Return an image of the amp

The returned image will have the amp's readout corner in the
nominated `corner`.

Parameters
----------
image : `lsst.afw.image.Image` or `lsst.afw.image.MaskedImage`
    Image containing the amplifier of interest.
amp : `lsst.afw.table.AmpInfoRecord`
    Amplifier information.
corner : `lsst.afw.table.ReadoutCorner` or `None`
    Corner in which to put the amp's readout corner, or `None` for
    no flipping.
isTrimmed : `bool`
    The image is already trimmed.
    This should no longer be needed once DM-15409 is resolved.

Returns
-------
output : `lsst.afw.image.Image`
    Image of the amplifier in the standard configuration.

Definition at line 221 of file crosstalk.py.

221 def extractAmp(image, amp, corner, isTrimmed=False):
222  """Return an image of the amp
223 
224  The returned image will have the amp's readout corner in the
225  nominated `corner`.
226 
227  Parameters
228  ----------
229  image : `lsst.afw.image.Image` or `lsst.afw.image.MaskedImage`
230  Image containing the amplifier of interest.
231  amp : `lsst.afw.table.AmpInfoRecord`
232  Amplifier information.
233  corner : `lsst.afw.table.ReadoutCorner` or `None`
234  Corner in which to put the amp's readout corner, or `None` for
235  no flipping.
236  isTrimmed : `bool`
237  The image is already trimmed.
238  This should no longer be needed once DM-15409 is resolved.
239 
240  Returns
241  -------
242  output : `lsst.afw.image.Image`
243  Image of the amplifier in the standard configuration.
244  """
245  output = image[amp.getBBox() if isTrimmed else amp.getRawDataBBox()]
246  ampCorner = amp.getReadoutCorner()
247  # Flipping is necessary only if the desired configuration doesn't match what we currently have
248  xFlip = X_FLIP[corner] ^ X_FLIP[ampCorner]
249  yFlip = Y_FLIP[corner] ^ Y_FLIP[ampCorner]
250  return lsst.afw.math.flipImage(output, xFlip, yFlip)
251 
252 

◆ subtractCrosstalk()

def lsst.ip.isr.crosstalk.subtractCrosstalk (   exposure,
  crosstalkCoeffs = None,
  badPixels = ["BAD"],
  minPixelToMask = 45000,
  crosstalkStr = "CROSSTALK",
  isTrimmed = False,
  backgroundMethod = "None" 
)
Subtract the intra-detector crosstalk from an exposure

We set the mask plane indicated by ``crosstalkStr`` in a target amplifier
for pixels in a source amplifier that exceed `minPixelToMask`. Note that
the correction is applied to all pixels in the amplifier, but only those
that have a substantial crosstalk are masked with ``crosstalkStr``.

The uncorrected image is used as a template for correction. This is good
enough if the crosstalk is small (e.g., coefficients < ~ 1e-3), but if it's
larger you may want to iterate.

This method needs unittests (DM-18876), but such testing requires
DM-18610 to allow the test detector to have the crosstalk
parameters set.

Parameters
----------
exposure : `lsst.afw.image.Exposure`
    Exposure for which to subtract crosstalk.
crosstalkCoeffs : `numpy.ndarray`
    Coefficients to use to correct crosstalk.
badPixels : `list` of `str`
    Mask planes to ignore.
minPixelToMask : `float`
    Minimum pixel value (relative to the background level) in
    source amplifier for which to set ``crosstalkStr`` mask plane
    in target amplifier.
crosstalkStr : `str`
    Mask plane name for pixels greatly modified by crosstalk.
isTrimmed : `bool`
    The image is already trimmed.
    This should no longer be needed once DM-15409 is resolved.
backgroundMethod : `str`
    Method used to subtract the background.  "AMP" uses
    amplifier-by-amplifier background levels, "DETECTOR" uses full
    exposure/maskedImage levels.  Any other value results in no
    background subtraction.

Definition at line 277 of file crosstalk.py.

277 def subtractCrosstalk(exposure, crosstalkCoeffs=None,
278  badPixels=["BAD"], minPixelToMask=45000,
279  crosstalkStr="CROSSTALK", isTrimmed=False,
280  backgroundMethod="None"):
281  """Subtract the intra-detector crosstalk from an exposure
282 
283  We set the mask plane indicated by ``crosstalkStr`` in a target amplifier
284  for pixels in a source amplifier that exceed `minPixelToMask`. Note that
285  the correction is applied to all pixels in the amplifier, but only those
286  that have a substantial crosstalk are masked with ``crosstalkStr``.
287 
288  The uncorrected image is used as a template for correction. This is good
289  enough if the crosstalk is small (e.g., coefficients < ~ 1e-3), but if it's
290  larger you may want to iterate.
291 
292  This method needs unittests (DM-18876), but such testing requires
293  DM-18610 to allow the test detector to have the crosstalk
294  parameters set.
295 
296  Parameters
297  ----------
298  exposure : `lsst.afw.image.Exposure`
299  Exposure for which to subtract crosstalk.
300  crosstalkCoeffs : `numpy.ndarray`
301  Coefficients to use to correct crosstalk.
302  badPixels : `list` of `str`
303  Mask planes to ignore.
304  minPixelToMask : `float`
305  Minimum pixel value (relative to the background level) in
306  source amplifier for which to set ``crosstalkStr`` mask plane
307  in target amplifier.
308  crosstalkStr : `str`
309  Mask plane name for pixels greatly modified by crosstalk.
310  isTrimmed : `bool`
311  The image is already trimmed.
312  This should no longer be needed once DM-15409 is resolved.
313  backgroundMethod : `str`
314  Method used to subtract the background. "AMP" uses
315  amplifier-by-amplifier background levels, "DETECTOR" uses full
316  exposure/maskedImage levels. Any other value results in no
317  background subtraction.
318  """
319  mi = exposure.getMaskedImage()
320  mask = mi.getMask()
321 
322  ccd = exposure.getDetector()
323  numAmps = len(ccd)
324  if crosstalkCoeffs is None:
325  coeffs = ccd.getCrosstalk()
326  else:
327  coeffs = crosstalkCoeffs
328  assert coeffs.shape == (numAmps, numAmps)
329 
330  # Set background level based on the requested method. The
331  # thresholdBackground holds the offset needed so that we only mask
332  # pixels high relative to the background, not in an absolute
333  # sense.
334  thresholdBackground = calculateBackground(mi, badPixels)
335 
336  backgrounds = [0.0 for amp in ccd]
337  if backgroundMethod is None:
338  pass
339  elif backgroundMethod == "AMP":
340  backgrounds = [calculateBackground(mi[amp.getBBox()], badPixels) for amp in ccd]
341  elif backgroundMethod == "DETECTOR":
342  backgrounds = [calculateBackground(mi, badPixels) for amp in ccd]
343 
344  # Set the crosstalkStr bit for the bright pixels (those which will have significant crosstalk correction)
345  crosstalkPlane = mask.addMaskPlane(crosstalkStr)
346  footprints = lsst.afw.detection.FootprintSet(mi, lsst.afw.detection.Threshold(minPixelToMask +
347  thresholdBackground))
348  footprints.setMask(mask, crosstalkStr)
349  crosstalk = mask.getPlaneBitMask(crosstalkStr)
350 
351  # Do pixel level crosstalk correction.
352  subtrahend = mi.Factory(mi.getBBox())
353  subtrahend.set((0, 0, 0))
354  for ii, iAmp in enumerate(ccd):
355  iImage = subtrahend[iAmp.getBBox() if isTrimmed else iAmp.getRawDataBBox()]
356  for jj, jAmp in enumerate(ccd):
357  if ii == jj:
358  assert coeffs[ii, jj] == 0.0
359  if coeffs[ii, jj] == 0.0:
360  continue
361 
362  jImage = extractAmp(mi, jAmp, iAmp.getReadoutCorner(), isTrimmed)
363  jImage.getMask().getArray()[:] &= crosstalk # Remove all other masks
364  jImage -= backgrounds[jj]
365 
366  iImage.scaledPlus(coeffs[ii, jj], jImage)
367 
368  # Set crosstalkStr bit only for those pixels that have been significantly modified (i.e., those
369  # masked as such in 'subtrahend'), not necessarily those that are bright originally.
370  mask.clearMaskPlane(crosstalkPlane)
371  mi -= subtrahend # also sets crosstalkStr bit for bright pixels
372 
373 

◆ writeCrosstalkCoeffs()

def lsst.ip.isr.crosstalk.writeCrosstalkCoeffs (   outputFileName,
  coeff,
  det = None,
  crosstalkName = "Unknown",
  indent = 2 
)
Write a yaml file containing the crosstalk coefficients

The coeff array is indexed by [i, j] where i and j are amplifiers
corresponding to the amplifiers in det

Parameters
----------
outputFileName : `str`
    Name of output yaml file
coeff : `numpy.array(namp, namp)`
    numpy array of coefficients
det : `lsst.afw.cameraGeom.Detector`
    Used to provide the list of amplifier names;
    if None use ['0', '1', ...]
ccdType : `str`
    Name of detector, used to index the yaml file
    If all detectors are identical could be the type (e.g. ITL)
indent : `int`
    Indent width to use when writing the yaml file

Definition at line 374 of file crosstalk.py.

374 def writeCrosstalkCoeffs(outputFileName, coeff, det=None, crosstalkName="Unknown", indent=2):
375  """Write a yaml file containing the crosstalk coefficients
376 
377  The coeff array is indexed by [i, j] where i and j are amplifiers
378  corresponding to the amplifiers in det
379 
380  Parameters
381  ----------
382  outputFileName : `str`
383  Name of output yaml file
384  coeff : `numpy.array(namp, namp)`
385  numpy array of coefficients
386  det : `lsst.afw.cameraGeom.Detector`
387  Used to provide the list of amplifier names;
388  if None use ['0', '1', ...]
389  ccdType : `str`
390  Name of detector, used to index the yaml file
391  If all detectors are identical could be the type (e.g. ITL)
392  indent : `int`
393  Indent width to use when writing the yaml file
394  """
395 
396  if det is None:
397  ampNames = [str(i) for i in range(coeff.shape[0])]
398  else:
399  ampNames = [a.getName() for a in det]
400 
401  assert coeff.shape == (len(ampNames), len(ampNames))
402 
403  dIndent = indent
404  indent = 0
405  with open(outputFileName, "w") as fd:
406  print(indent*" " + "crosstalk :", file=fd)
407  indent += dIndent
408  print(indent*" " + "%s :" % crosstalkName, file=fd)
409  indent += dIndent
410 
411  for i, ampNameI in enumerate(ampNames):
412  print(indent*" " + "%s : {" % ampNameI, file=fd)
413  indent += dIndent
414  print(indent*" ", file=fd, end='')
415 
416  for j, ampNameJ in enumerate(ampNames):
417  print("%s : %11.4e, " % (ampNameJ, coeff[i, j]), file=fd,
418  end='\n' + indent*" " if j%4 == 3 else '')
419  print("}", file=fd)
420 
421  indent -= dIndent

Variable Documentation

◆ X_FLIP

dictionary lsst.ip.isr.crosstalk.X_FLIP
Initial value:
1 = {lsst.afw.cameraGeom.ReadoutCorner.LL: False, lsst.afw.cameraGeom.ReadoutCorner.LR: True,
2  lsst.afw.cameraGeom.ReadoutCorner.UL: False, lsst.afw.cameraGeom.ReadoutCorner.UR: True}

Definition at line 210 of file crosstalk.py.

◆ Y_FLIP

dictionary lsst.ip.isr.crosstalk.Y_FLIP
Initial value:
1 = {lsst.afw.cameraGeom.ReadoutCorner.LL: False, lsst.afw.cameraGeom.ReadoutCorner.LR: False,
2  lsst.afw.cameraGeom.ReadoutCorner.UL: True, lsst.afw.cameraGeom.ReadoutCorner.UR: True}

Definition at line 212 of file crosstalk.py.

lsst::ip::isr.crosstalk.subtractCrosstalk
def subtractCrosstalk(exposure, crosstalkCoeffs=None, badPixels=["BAD"], minPixelToMask=45000, crosstalkStr="CROSSTALK", isTrimmed=False, backgroundMethod="None")
Definition: crosstalk.py:277
lsst::afw::math::makeStatistics
Statistics makeStatistics(lsst::afw::math::MaskedVector< EntryT > const &mv, std::vector< WeightPixel > const &vweights, int const flags, StatisticsControl const &sctrl=StatisticsControl())
The makeStatistics() overload to handle lsst::afw::math::MaskedVector<>
Definition: Statistics.h:520
lsst::afw::detection::FootprintSet
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:53
lsst::afw::detection::Threshold
A Threshold is used to pass a threshold value to detection algorithms.
Definition: Threshold.h:43
lsst::ip::isr.crosstalk.calculateBackground
def calculateBackground(mi, badPixels=["BAD"])
Definition: crosstalk.py:253
lsst::ip::isr.crosstalk.extractAmp
def extractAmp(image, amp, corner, isTrimmed=False)
Definition: crosstalk.py:221
lsst::afw::math::StatisticsControl
Pass parameters to a Statistics object.
Definition: Statistics.h:93
lsst::ip::isr.crosstalk.writeCrosstalkCoeffs
def writeCrosstalkCoeffs(outputFileName, coeff, det=None, crosstalkName="Unknown", indent=2)
Definition: crosstalk.py:374
lsst::afw::math::flipImage
std::shared_ptr< ImageT > flipImage(ImageT const &inImage, bool flipLR, bool flipTB)
Flip an image left–right and/or top–bottom.
Definition: rotateImage.cc:92