38 """Make a double Gaussian PSF 40 @param[in] fwhm FWHM of double Gaussian smoothing kernel 41 @return measAlg.DoubleGaussianPsf 43 ksize = 4*int(fwhm) + 1
44 return measAlg.DoubleGaussianPsf(ksize, ksize, fwhm/(2*math.sqrt(2*math.log(2))))
48 """Make a transposed copy of a masked image 50 @param[in] maskedImage afw.image.MaskedImage to process 51 @return transposed masked image 53 transposed = maskedImage.Factory(afwGeom.Extent2I(maskedImage.getHeight(), maskedImage.getWidth()))
54 transposed.getImage().getArray()[:] = maskedImage.getImage().getArray().T
55 transposed.getMask().getArray()[:] = maskedImage.getMask().getArray().T
56 transposed.getVariance().getArray()[:] = maskedImage.getVariance().getArray().T
61 """Interpolate over defects specified in a defect list 63 @param[in,out] maskedImage masked image to process 64 @param[in] defectList defect list 65 @param[in] fwhm FWHM of double Gaussian smoothing kernel 66 @param[in] fallbackValue fallback value if an interpolated value cannot be determined; 67 if None then use clipped mean image value 70 if fallbackValue
is None:
71 fallbackValue = afwMath.makeStatistics(maskedImage.getImage(), afwMath.MEANCLIP).getValue()
72 if 'INTRP' not in maskedImage.getMask().getMaskPlaneDict():
73 maskedImage.getMask.addMaskPlane(
'INTRP')
74 measAlg.interpolateOverDefects(maskedImage, psf, defectList, fallbackValue,
True)
78 """Compute a defect list from a footprint list, optionally growing the footprints 80 @param[in] fpList footprint list 84 for bbox
in afwDetection.footprintToBBoxList(fp):
85 defect = measAlg.Defect(bbox)
86 defectList.append(defect)
91 """Make a transposed copy of a defect list 93 @param[in] defectList a list of defects (afw.meas.algorithms.Defect) 94 @return a defect list with transposed defects 97 for defect
in defectList:
98 bbox = defect.getBBox()
99 nbbox = afwGeom.Box2I(afwGeom.Point2I(bbox.getMinY(), bbox.getMinX()),
100 afwGeom.Extent2I(bbox.getDimensions()[1], bbox.getDimensions()[0]))
101 retDefectList.append(measAlg.Defect(nbbox))
106 """Set mask plane based on a defect list 108 @param[in,out] maskedImage afw.image.MaskedImage to process; mask plane is updated 109 @param[in] defectList a list of defects (afw.meas.algorithms.Defect) 110 @param[in] maskName mask plane name 113 mask = maskedImage.getMask()
114 bitmask = mask.getPlaneBitMask(maskName)
115 for defect
in defectList:
116 bbox = defect.getBBox()
117 afwGeom.SpanSet(bbox).clippedTo(mask.getBBox()).setMask(mask, bitmask)
121 """Compute a defect list from a specified mask plane 123 @param[in] maskedImage masked image to process 124 @param[in] maskName mask plane name, or list of names 126 mask = maskedImage.getMask()
127 thresh = afwDetection.Threshold(mask.getPlaneBitMask(maskName), afwDetection.Threshold.BITMASK)
128 fpList = afwDetection.FootprintSet(mask, thresh).getFootprints()
133 """Mask pixels based on threshold detection 135 @param[in,out] maskedImage afw.image.MaskedImage to process; the mask is altered 136 @param[in] threshold detection threshold 137 @param[in] growFootprints amount by which to grow footprints of detected regions 138 @param[in] maskName mask plane name 139 @return a list of defects (meas.algrithms.Defect) of regions set in the mask. 142 thresh = afwDetection.Threshold(threshold)
143 fs = afwDetection.FootprintSet(maskedImage, thresh)
145 if growFootprints > 0:
146 fs = afwDetection.FootprintSet(fs, growFootprints)
148 fpList = fs.getFootprints()
150 mask = maskedImage.getMask()
151 bitmask = mask.getPlaneBitMask(maskName)
152 afwDetection.setMaskFromFootprintList(mask, fpList, bitmask)
158 """Interpolate over defects identified by a particular mask plane 160 @param[in,out] maskedImage afw.image.MaskedImage to process 161 @param[in] fwhm FWHM of double Gaussian smoothing kernel 162 @param[in] growFootprints amount by which to grow footprints of detected regions 163 @param[in] maskName mask plane name 164 @param[in] fallbackValue value of last resort for interpolation 166 mask = maskedImage.getMask()
167 thresh = afwDetection.Threshold(mask.getPlaneBitMask(maskName), afwDetection.Threshold.BITMASK)
168 fpSet = afwDetection.FootprintSet(mask, thresh)
169 if growFootprints > 0:
170 fpSet = afwDetection.FootprintSet(fpSet, rGrow=growFootprints, isotropic=
False)
173 fpSet.setMask(mask, maskName)
178 def saturationCorrection(maskedImage, saturation, fwhm, growFootprints=1, interpolate=True, maskName='SAT',
180 """Mark saturated pixels and optionally interpolate over them 182 @param[in,out] maskedImage afw.image.MaskedImage to process 183 @param[in] saturation saturation level (used as a detection threshold) 184 @param[in] fwhm FWHM of double Gaussian smoothing kernel 185 @param[in] growFootprints amount by which to grow footprints of detected regions 186 @param[in] interpolate interpolate over saturated pixels? 187 @param[in] maskName mask plane name 188 @param[in] fallbackValue value of last resort for interpolation 191 maskedImage=maskedImage,
192 threshold=saturation,
193 growFootprints=growFootprints,
201 """Apply bias correction in place 203 @param[in,out] maskedImage masked image to correct 204 @param[in] biasMaskedImage bias, as a masked image 206 if maskedImage.getBBox(afwImage.LOCAL) != biasMaskedImage.getBBox(afwImage.LOCAL):
207 raise RuntimeError(
"maskedImage bbox %s != biasMaskedImage bbox %s" %
208 (maskedImage.getBBox(afwImage.LOCAL), biasMaskedImage.getBBox(afwImage.LOCAL)))
209 maskedImage -= biasMaskedImage
212 def darkCorrection(maskedImage, darkMaskedImage, expScale, darkScale, invert=False):
213 """Apply dark correction in place 215 maskedImage -= dark * expScaling / darkScaling 217 @param[in,out] maskedImage afw.image.MaskedImage to correct 218 @param[in] darkMaskedImage dark afw.image.MaskedImage 219 @param[in] expScale exposure scale 220 @param[in] darkScale dark scale 221 @param[in] invert if True, remove the dark from an already-corrected image 223 if maskedImage.getBBox(afwImage.LOCAL) != darkMaskedImage.getBBox(afwImage.LOCAL):
224 raise RuntimeError(
"maskedImage bbox %s != darkMaskedImage bbox %s" %
225 (maskedImage.getBBox(afwImage.LOCAL), darkMaskedImage.getBBox(afwImage.LOCAL)))
227 scale = expScale / darkScale
229 maskedImage.scaledMinus(scale, darkMaskedImage)
231 maskedImage.scaledPlus(scale, darkMaskedImage)
235 """Set the variance plane based on the image plane 237 @param[in,out] maskedImage afw.image.MaskedImage; image plane is read and variance plane is written 238 @param[in] gain amplifier gain (e-/ADU) 239 @param[in] readNoise amplifier read noise (ADU/pixel) 241 var = maskedImage.getVariance()
242 var[:] = maskedImage.getImage()
247 def flatCorrection(maskedImage, flatMaskedImage, scalingType, userScale=1.0, invert=False):
248 """Apply flat correction in place 250 @param[in,out] maskedImage afw.image.MaskedImage to correct 251 @param[in] flatMaskedImage flat field afw.image.MaskedImage 252 @param[in] scalingType how to compute flat scale; one of 'MEAN', 'MEDIAN' or 'USER' 253 @param[in] userScale scale to use if scalingType is 'USER', else ignored 254 @param[in] invert if True, unflatten an already-flattened image instead. 256 if maskedImage.getBBox(afwImage.LOCAL) != flatMaskedImage.getBBox(afwImage.LOCAL):
257 raise RuntimeError(
"maskedImage bbox %s != flatMaskedImage bbox %s" %
258 (maskedImage.getBBox(afwImage.LOCAL), flatMaskedImage.getBBox(afwImage.LOCAL)))
263 if scalingType ==
'MEAN':
264 flatScale = afwMath.makeStatistics(flatMaskedImage.getImage(), afwMath.MEAN).getValue(afwMath.MEAN)
265 elif scalingType ==
'MEDIAN':
266 flatScale = afwMath.makeStatistics(flatMaskedImage.getImage(),
267 afwMath.MEDIAN).getValue(afwMath.MEDIAN)
268 elif scalingType ==
'USER':
269 flatScale = userScale
271 raise pexExcept.Exception(
'%s : %s not implemented' % (
"flatCorrection", scalingType))
274 maskedImage.scaledDivides(1.0/flatScale, flatMaskedImage)
276 maskedImage.scaledMultiplies(1.0/flatScale, flatMaskedImage)
280 """Apply illumination correction in place 282 @param[in,out] maskedImage afw.image.MaskedImage to correct 283 @param[in] illumMaskedImage illumination correction masked image 284 @param[in] illumScale scale value for illumination correction 286 if maskedImage.getBBox(afwImage.LOCAL) != illumMaskedImage.getBBox(afwImage.LOCAL):
287 raise RuntimeError(
"maskedImage bbox %s != illumMaskedImage bbox %s" %
288 (maskedImage.getBBox(afwImage.LOCAL), illumMaskedImage.getBBox(afwImage.LOCAL)))
290 maskedImage.scaledDivides(1./illumScale, illumMaskedImage)
293 def overscanCorrection(ampMaskedImage, overscanImage, fitType='MEDIAN', order=1, collapseRej=3.0,
295 """Apply overscan correction in-place 297 The ``ampMaskedImage`` and ``overscanImage`` are modified, with the fit 298 subtracted. Note that the ``overscanImage`` should not be a subimage of 299 the ``ampMaskedImage``, to avoid being subtracted twice. 303 ampMaskedImage : `lsst.afw.image.MaskedImage` 304 Image of amplifier to correct; modified. 305 overscanImage : `lsst.afw.image.Image` or `lsst.afw.image.MaskedImage` 306 Image of overscan; modified. 308 Type of fit for overscan correction. May be one of: 310 - ``MEAN``: use mean of overscan. 311 - ``MEDIAN``: use median of overscan. 312 - ``POLY``: fit with ordinary polynomial. 313 - ``CHEB``: fit with Chebyshev polynomial. 314 - ``LEG``: fit with Legendre polynomial. 315 - ``NATURAL_SPLINE``: fit with natural spline. 316 - ``CUBIC_SPLINE``: fit with cubic spline. 317 - ``AKIMA_SPLINE``: fit with Akima spline. 320 Polynomial order or number of spline knots; ignored unless 321 ``fitType`` indicates a polynomial or spline. 322 collapseRej : `float` 323 Rejection threshold (sigma) for collapsing dimension of overscan. 324 statControl : `lsst.afw.math.StatisticsControl` 325 Statistics control object. 329 result : `lsst.pipe.base.Struct` 330 Result struct with components: 332 - ``imageFit``: Value(s) removed from image (scalar or 333 `lsst.afw.image.Image`) 334 - ``overscanFit``: Value(s) removed from overscan (scalar or 335 `lsst.afw.image.Image`) 337 ampImage = ampMaskedImage.getImage()
338 if statControl
is None:
339 statControl = afwMath.StatisticsControl()
340 if fitType ==
'MEAN':
341 offImage = afwMath.makeStatistics(overscanImage, afwMath.MEAN, statControl).getValue(afwMath.MEAN)
342 overscanFit = offImage
343 elif fitType ==
'MEDIAN':
344 offImage = afwMath.makeStatistics(overscanImage, afwMath.MEDIAN, statControl).getValue(afwMath.MEDIAN)
345 overscanFit = offImage
346 elif fitType
in (
'POLY',
'CHEB',
'LEG',
'NATURAL_SPLINE',
'CUBIC_SPLINE',
'AKIMA_SPLINE'):
347 if hasattr(overscanImage,
"getImage"):
348 biasArray = overscanImage.getImage().getArray()
349 biasArray = numpy.ma.masked_where(overscanImage.getMask().getArray() & statControl.getAndMask(),
352 biasArray = overscanImage.getArray()
354 shortInd = numpy.argmin(biasArray.shape)
357 biasArray = numpy.transpose(biasArray)
360 percentiles = numpy.percentile(biasArray, [25.0, 50.0, 75.0], axis=1)
361 medianBiasArr = percentiles[1]
362 stdevBiasArr = 0.74*(percentiles[2] - percentiles[0])
363 diff = numpy.abs(biasArray - medianBiasArr[:, numpy.newaxis])
364 biasMaskedArr = numpy.ma.masked_where(diff > collapseRej*stdevBiasArr[:, numpy.newaxis], biasArray)
365 collapsed = numpy.mean(biasMaskedArr, axis=1)
366 if collapsed.mask.sum() > 0:
367 collapsed.data[collapsed.mask] = numpy.mean(biasArray.data[collapsed.mask], axis=1)
368 del biasArray, percentiles, stdevBiasArr, diff, biasMaskedArr
371 collapsed = numpy.transpose(collapsed)
374 indices = 2.0*numpy.arange(num)/float(num) - 1.0
376 if fitType
in (
'POLY',
'CHEB',
'LEG'):
378 poly = numpy.polynomial
379 fitter, evaler = {
"POLY": (poly.polynomial.polyfit, poly.polynomial.polyval),
380 "CHEB": (poly.chebyshev.chebfit, poly.chebyshev.chebval),
381 "LEG": (poly.legendre.legfit, poly.legendre.legval),
384 coeffs = fitter(indices, collapsed, order)
385 fitBiasArr = evaler(indices, coeffs)
386 elif 'SPLINE' in fitType:
395 collapsedMask = collapsed.mask
397 if collapsedMask == numpy.ma.nomask:
398 collapsedMask = numpy.array(len(collapsed)*[numpy.ma.nomask])
402 numPerBin, binEdges = numpy.histogram(indices, bins=numBins,
403 weights=1-collapsedMask.astype(int))
406 with numpy.errstate(invalid=
"ignore"):
407 values = numpy.histogram(indices, bins=numBins,
408 weights=collapsed.data*~collapsedMask)[0]/numPerBin
409 binCenters = numpy.histogram(indices, bins=numBins,
410 weights=indices*~collapsedMask)[0]/numPerBin
411 interp = afwMath.makeInterpolate(binCenters.astype(float)[numPerBin > 0],
412 values.astype(float)[numPerBin > 0],
413 afwMath.stringToInterpStyle(fitType))
414 fitBiasArr = numpy.array([interp.interpolate(i)
for i
in indices])
418 import matplotlib.pyplot
as plot
419 figure = plot.figure(1)
421 axes = figure.add_axes((0.1, 0.1, 0.8, 0.8))
422 axes.plot(indices[~collapsedMask], collapsed[~collapsedMask],
'k+')
423 if collapsedMask.sum() > 0:
424 axes.plot(indices[collapsedMask], collapsed.data[collapsedMask],
'b+')
425 axes.plot(indices, fitBiasArr,
'r-')
427 prompt =
"Press Enter or c to continue [chp]... " 429 ans = input(prompt).lower()
430 if ans
in (
"",
"c",):
436 print(
"h[elp] c[ontinue] p[db]")
439 offImage = ampImage.Factory(ampImage.getDimensions())
440 offArray = offImage.getArray()
441 overscanFit = afwImage.ImageF(overscanImage.getDimensions())
442 overscanArray = overscanFit.getArray()
444 offArray[:, :] = fitBiasArr[:, numpy.newaxis]
445 overscanArray[:, :] = fitBiasArr[:, numpy.newaxis]
447 offArray[:, :] = fitBiasArr[numpy.newaxis, :]
448 overscanArray[:, :] = fitBiasArr[numpy.newaxis, :]
456 mask = ampMaskedImage.getMask()
457 maskArray = mask.getArray()
if shortInd == 1
else mask.getArray().transpose()
458 suspect = mask.getPlaneBitMask(
"SUSPECT")
460 if collapsed.mask == numpy.ma.nomask:
464 for low
in range(num):
465 if not collapsed.mask[low]:
468 maskArray[:low, :] |= suspect
469 for high
in range(1, num):
470 if not collapsed.mask[-high]:
473 maskArray[-high:, :] |= suspect
476 raise pexExcept.Exception(
'%s : %s an invalid overscan type' % (
"overscanCorrection", fitType))
478 overscanImage -= overscanFit
479 return Struct(imageFit=offImage, overscanFit=overscanFit)
483 sensorTransmission=None, atmosphereTransmission=None):
484 """Attach a TransmissionCurve to an Exposure, given separate curves for 485 different components. 489 exposure : `lsst.afw.image.Exposure` 490 Exposure object to modify by attaching the product of all given 491 ``TransmissionCurves`` in post-assembly trimmed detector coordinates. 492 Must have a valid ``Detector`` attached that matches the detector 493 associated with sensorTransmission. 494 opticsTransmission : `lsst.afw.image.TransmissionCurve` 495 A ``TransmissionCurve`` that represents the throughput of the optics, 496 to be evaluated in focal-plane coordinates. 497 filterTransmission : `lsst.afw.image.TransmissionCurve` 498 A ``TransmissionCurve`` that represents the throughput of the filter 499 itself, to be evaluated in focal-plane coordinates. 500 sensorTransmission : `lsst.afw.image.TransmissionCurve` 501 A ``TransmissionCurve`` that represents the throughput of the sensor 502 itself, to be evaluated in post-assembly trimmed detector coordinates. 503 atmosphereTransmission : `lsst.afw.image.TransmissionCurve` 504 A ``TransmissionCurve`` that represents the throughput of the 505 atmosphere, assumed to be spatially constant. 507 All ``TransmissionCurve`` arguments are optional; if none are provided, the 508 attached ``TransmissionCurve`` will have unit transmission everywhere. 512 combined : ``lsst.afw.image.TransmissionCurve`` 513 The TransmissionCurve attached to the exposure. 515 combined = afwImage.TransmissionCurve.makeIdentity()
516 if atmosphereTransmission
is not None:
517 combined *= atmosphereTransmission
518 if opticsTransmission
is not None:
519 combined *= opticsTransmission
520 if filterTransmission
is not None:
521 combined *= filterTransmission
522 detector = exposure.getDetector()
523 fpToPix = detector.getTransform(fromSys=camGeom.FOCAL_PLANE,
524 toSys=camGeom.PIXELS)
525 combined = combined.transformedBy(fpToPix)
526 if sensorTransmission
is not None:
527 combined *= sensorTransmission
528 exposure.getInfo().setTransmissionCurve(combined)
def darkCorrection(maskedImage, darkMaskedImage, expScale, darkScale, invert=False)
def illuminationCorrection(maskedImage, illumMaskedImage, illumScale)
def saturationCorrection(maskedImage, saturation, fwhm, growFootprints=1, interpolate=True, maskName='SAT', fallbackValue=None)
def transposeDefectList(defectList)
def getDefectListFromMask(maskedImage, maskName)
def interpolateDefectList(maskedImage, defectList, fwhm, fallbackValue=None)
def defectListFromFootprintList(fpList)
def transposeMaskedImage(maskedImage)
def biasCorrection(maskedImage, biasMaskedImage)
def interpolateFromMask(maskedImage, fwhm, growFootprints=1, maskName='SAT', fallbackValue=None)
def attachTransmissionCurve(exposure, opticsTransmission=None, filterTransmission=None, sensorTransmission=None, atmosphereTransmission=None)
def makeThresholdMask(maskedImage, threshold, growFootprints=1, maskName='SAT')
def overscanCorrection(ampMaskedImage, overscanImage, fitType='MEDIAN', order=1, collapseRej=3.0, statControl=None)
def flatCorrection(maskedImage, flatMaskedImage, scalingType, userScale=1.0, invert=False)
def updateVariance(maskedImage, gain, readNoise)
def maskPixelsFromDefectList(maskedImage, defectList, maskName='BAD')