34 """Make a double Gaussian PSF
36 @param[in] fwhm FWHM of double Gaussian smoothing kernel
37 @return measAlg.DoubleGaussianPsf
39 ksize = 4*int(fwhm) + 1
43 """Calculate effective gain
45 @param[in] maskedImage afw.image.MaskedImage to process
46 @return (median gain, mean gain) in e-/ADU
48 im = afwImage.ImageF(maskedImage.getImage(),
True)
49 var = maskedImage.getVariance()
53 return medgain, meangain
56 """Make a transposed copy of a masked image
58 @param[in] maskedImage afw.image.MaskedImage to process
59 @return transposed masked image
61 transposed = maskedImage.Factory(
afwGeom.Extent2I(maskedImage.getHeight(), maskedImage.getWidth()))
62 transposed.getImage().getArray()[:] = maskedImage.getImage().getArray().T
63 transposed.getMask().getArray()[:] = maskedImage.getMask().getArray().T
64 transposed.getVariance().getArray()[:] = maskedImage.getVariance().getArray().T
68 """Interpolate over defects specified in a defect list
70 @param[in,out] maskedImage masked image to process
71 @param[in] defectList defect list
72 @param[in] fwhm FWHM of double Gaussian smoothing kernel
73 @param[in] fallbackValue fallback value if an interpolated value cannot be determined;
74 if None then use clipped mean image value
77 if fallbackValue
is None:
79 if 'INTRP' not in maskedImage.getMask().getMaskPlaneDict().keys():
80 maskedImage.getMask.addMaskPlane(
'INTRP')
84 """Compute a defect list from a footprint list, optionally growing the footprints
86 @param[in] fpList footprint list
87 @param[in] growFootprints amount by which to grow footprints of detected regions
88 @return meas.algorithms.DefectListT
90 defectList = measAlg.DefectListT()
92 if growFootprints > 0:
100 defectList.push_back(defect)
104 """Make a transposed copy of a defect list
106 @param[in] defectList defect list
107 @return meas.algorithms.DefectListT with transposed defects
109 retDefectList = measAlg.DefectListT()
110 for defect
in defectList:
111 bbox = defect.getBBox()
118 """Set mask plane based on a defect list
120 @param[in,out] maskedImage afw.image.MaskedImage to process; mask plane is updated
121 @param[in] defectList meas.algorithms.DefectListT
122 @param[in] maskName mask plane name
125 mask = maskedImage.getMask()
126 bitmask = mask.getPlaneBitMask(maskName)
127 for defect
in defectList:
128 bbox = defect.getBBox()
132 """Compute a defect list from a specified mask plane
134 @param[in] maskedImage masked image to process
135 @param[in] maskName mask plane name
136 @param[in] growFootprints amount by which to grow footprints of detected regions
137 @return meas.algrithms.DefectListT of regions in mask
139 mask = maskedImage.getMask()
140 workmask = afwImage.MaskU(mask,
True)
141 workmask &= mask.getPlaneBitMask(maskName)
143 maskimg = afwImage.ImageU(workmask.getBBox())
146 fpList = ds.getFootprints()
150 """Mask pixels based on threshold detection
152 @param[in,out] maskedImage afw.image.MaskedImage to process; the mask is altered
153 @param[in] threshold detection threshold
154 @param[in] growFootprints amount by which to grow footprints of detected regions
155 @param[in] maskName mask plane name
156 @return meas.algorihtms.DefectListT of regions set in the mask.
162 if growFootprints > 0:
165 fpList = fs.getFootprints()
167 mask = maskedImage.getMask()
168 bitmask = mask.getPlaneBitMask(maskName)
174 """Interpolate over defects identified by a particular mask plane
176 @param[in,out] maskedImage afw.image.MaskedImage to process
177 @param[in] fwhm FWHM of double Gaussian smoothing kernel
178 @param[in] growFootprints amount by which to grow footprints of detected regions
179 @param[in] maskName mask plane name
180 @param[in] fallbackValue value of last resort for interpolation
185 def saturationCorrection(maskedImage, saturation, fwhm, growFootprints=1, interpolate=True, maskName='SAT',
187 """Mark saturated pixels and optionally interpolate over them
189 @param[in,out] maskedImage afw.image.MaskedImage to process
190 @param[in] saturation saturation level (used as a detection threshold)
191 @param[in] fwhm FWHM of double Gaussian smoothing kernel
192 @param[in] growFootprints amount by which to grow footprints of detected regions
193 @param[in] interpolate interpolate over saturated pixels?
194 @param[in] maskName mask plane name
195 @param[in] fallbackValue value of last resort for interpolation
198 maskedImage = maskedImage,
199 threshold = saturation,
200 growFootprints = growFootprints,
207 """Apply bias correction in place
209 @param[in,out] maskedImage masked image to correct
210 @param[in] biasMaskedImage bias, as a masked image
212 maskedImage -= biasMaskedImage
215 """Apply dark correction in place
217 maskedImage -= dark * expScaling / darkScaling
219 @param[in,out] maskedImage afw.image.MaskedImage to correct
220 @param[in] darkMaskedImage dark afw.image.MaskedImage
221 @param[in] expScale exposure scale
222 @param[in] darkScale dark scale
224 if maskedImage.getBBox(afwImage.LOCAL) != darkMaskedImage.getBBox(afwImage.LOCAL):
225 raise RuntimeError(
"maskedImage bbox %s != darkMaskedImage bbox %s" % \
226 (maskedImage.getBBox(afwImage.LOCAL), darkMaskedImage.getBBox(afwImage.LOCAL)))
228 scale = expScale / darkScale
229 maskedImage.scaledMinus(scale, darkMaskedImage)
232 """Set the variance plane based on the image plane
234 @param[in,out] maskedImage afw.image.MaskedImage; image plane is read and variance plane is written
235 @param[in] gain amplifier gain (e-/ADU)
236 @param[in] readNoise amplifier read noise (ADU/pixel)
238 var = maskedImage.getVariance()
239 var <<= maskedImage.getImage()
244 """Apply flat correction in place
246 @param[in,out] maskedImage afw.image.MaskedImage to correct
247 @param[in] flatMaskedImage flat field afw.image.MaskedImage
248 @param[in] scalingType how to compute flat scale; one of 'MEAN', 'MEDIAN' or 'USER'
249 @param[in] userScale scale to use if scalingType is 'USER', else ignored
251 if maskedImage.getBBox(afwImage.LOCAL) != flatMaskedImage.getBBox(afwImage.LOCAL):
252 raise RuntimeError(
"maskedImage bbox %s != flatMaskedImage bbox %s" % \
253 (maskedImage.getBBox(afwImage.LOCAL), flatMaskedImage.getBBox(afwImage.LOCAL)))
258 if scalingType ==
'MEAN':
260 elif scalingType ==
'MEDIAN':
262 elif scalingType ==
'USER':
263 flatScale = userScale
267 maskedImage.scaledDivides(1.0/flatScale, flatMaskedImage)
270 """Apply illumination correction in place
272 @param[in,out] maskedImage afw.image.MaskedImage to correct
273 @param[in] illumMaskedImage illumination correction masked image
274 @param[in] illumScale scale value for illumination correction
276 if maskedImage.getBBox(afwImage.LOCAL) != illumMaskedImage.getBBox(afwImage.LOCAL):
277 raise RuntimeError(
"maskedImage bbox %s != illumMaskedImage bbox %s" % \
278 (maskedImage.getBBox(afwImage.LOCAL), illumMaskedImage.getBBox(afwImage.LOCAL)))
280 maskedImage.scaledDivides(1./illumScale, illumMaskedImage)
282 def overscanCorrection(ampMaskedImage, overscanImage, fitType='MEDIAN', order=1, collapseRej=3.0,
284 """Apply overscan correction in place
286 @param[in,out] ampMaskedImage masked image to correct
287 @param[in] overscanImage overscan data as an afw.image.IMage
288 @param[in] fitType type of fit for overscan correction; one of:
291 - 'POLY' (ordinary polynomial)
292 - 'CHEB' (Chebyshev polynomial)
293 - 'LEG' (Legendre polynomial)
294 - 'NATURAL_SPLINE', 'CUBIC_SPLINE', 'AKIMA_SPLINE' (splines)
295 @param[in] order polynomial order or spline knots (ignored unless fitType
296 indicates a polynomial or spline)
297 @param[in] collapseRej Rejection threshold (sigma) for collapsing dimension of overscan
298 @param[in] statControl Statistics control object
300 ampImage = ampMaskedImage.getImage()
301 if statControl
is None:
303 if fitType ==
'MEAN':
305 elif fitType ==
'MEDIAN':
307 elif fitType
in (
'POLY',
'CHEB',
'LEG',
'NATURAL_SPLINE',
'CUBIC_SPLINE',
'AKIMA_SPLINE'):
308 if hasattr(overscanImage,
"getImage"):
309 biasArray = overscanImage.getImage().getArray()
310 biasArray = numpy.ma.masked_where(overscanImage.getMask().getArray() & statControl.getAndMask(),
313 biasArray = overscanImage.getArray()
315 shortInd = numpy.argmin(biasArray.shape)
318 biasArray = numpy.transpose(biasArray)
321 percentiles = numpy.percentile(biasArray, [25.0, 50.0, 75.0], axis=1)
322 medianBiasArr = percentiles[1]
323 stdevBiasArr = 0.74*(percentiles[2] - percentiles[0])
324 diff = numpy.abs(biasArray - medianBiasArr[:,numpy.newaxis])
325 biasMaskedArr = numpy.ma.masked_where(diff > collapseRej*stdevBiasArr[:,numpy.newaxis], biasArray)
326 collapsed = numpy.mean(biasMaskedArr, axis=1)
327 del biasArray, percentiles, stdevBiasArr, diff, biasMaskedArr
330 collapsed = numpy.transpose(collapsed)
333 indices = 2.0*numpy.arange(num)/float(num) - 1.0
335 if fitType
in (
'POLY',
'CHEB',
'LEG'):
337 poly = numpy.polynomial
338 fitter, evaler = {
"POLY": (poly.polynomial.polyfit, poly.polynomial.polyval),
339 "CHEB": (poly.chebyshev.chebfit, poly.chebyshev.chebval),
340 "LEG": (poly.legendre.legfit, poly.legendre.legval),
343 coeffs = fitter(indices, collapsed, order)
344 fitBiasArr = evaler(indices, coeffs)
345 elif 'SPLINE' in fitType:
354 collapsedMask = collapsed.mask
356 if collapsedMask == numpy.ma.nomask:
357 collapsedMask = numpy.array(len(collapsed)*[numpy.ma.nomask])
361 numPerBin, binEdges = numpy.histogram(indices, bins=numBins,
362 weights=1-collapsedMask.astype(int))
365 values = numpy.histogram(indices, bins=numBins, weights=collapsed)[0]/numPerBin
366 binCenters = numpy.histogram(indices, bins=numBins, weights=indices)[0]/numPerBin
368 values.astype(float),
370 fitBiasArr = numpy.array([interp.interpolate(i)
for i
in indices])
374 import matplotlib.pyplot
as plot
375 figure = plot.figure(1)
377 axes = figure.add_axes((0.1, 0.1, 0.8, 0.8))
378 axes.plot(indices, collapsed,
'k+')
379 axes.plot(indices, fitBiasArr,
'r-')
381 prompt =
"Press Enter or c to continue [chp]... "
383 ans = raw_input(prompt).lower()
384 if ans
in (
"",
"c",):
387 import pdb; pdb.set_trace()
389 print "h[elp] c[ontinue] p[db]"
392 offImage = ampImage.Factory(ampImage.getDimensions())
393 offArray = offImage.getArray()
395 offArray[:,:] = fitBiasArr[:,numpy.newaxis]
397 offArray[:,:] = fitBiasArr[numpy.newaxis,:]
400 (
"overscanCorrection", fitType)
Interpolate::Style stringToInterpStyle(std::string const &style)
Conversion function to switch a string to an Interpolate::Style.
boost::shared_ptr< Footprint > growFootprint(Footprint const &foot, int nGrow, bool left, bool right, bool up, bool down)
Grow a Footprint in at least one of the cardinal directions, returning a new Footprint.
boost::shared_ptr< Interpolate > makeInterpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style=Interpolate::AKIMA_SPLINE)
A Threshold is used to pass a threshold value to detection algorithms.
An integer coordinate rectangle.
def maskPixelsFromDefectList
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.
Represent a Psf as a circularly symmetrical double Gaussian.
def interpolateDefectList
Pass parameters to a Statistics objectA class to pass parameters which control how the stats are calc...
def illuminationCorrection
def getDefectListFromMask
std::vector< lsst::afw::geom::Box2I > footprintToBBoxList(Footprint const &foot)
MaskT setMaskFromFootprint(lsst::afw::image::Mask< MaskT > *mask, Footprint const &footprint, MaskT const bitmask)
OR bitmask into all the Mask's pixels that are in the Footprint.
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.
Encapsulate information about a bad portion of a detector.
def defectListFromFootprintList