288 """Apply overscan correction in place
290 @param[in,out] ampMaskedImage masked image to correct
291 @param[in] overscanImage overscan data as an afw.image.Image or afw.image.MaskedImage.
292 If a masked image is passed in the mask plane will be used
293 to constrain the fit of the bias level.
294 @param[in] fitType type of fit for overscan correction; one of:
297 - 'POLY' (ordinary polynomial)
298 - 'CHEB' (Chebyshev polynomial)
299 - 'LEG' (Legendre polynomial)
300 - 'NATURAL_SPLINE', 'CUBIC_SPLINE', 'AKIMA_SPLINE' (splines)
301 @param[in] order polynomial order or spline knots (ignored unless fitType
302 indicates a polynomial or spline)
303 @param[in] collapseRej Rejection threshold (sigma) for collapsing dimension of overscan
304 @param[in] statControl Statistics control object
306 ampImage = ampMaskedImage.getImage()
307 if statControl
is None:
309 if fitType ==
'MEAN':
311 elif fitType ==
'MEDIAN':
313 elif fitType
in (
'POLY',
'CHEB',
'LEG',
'NATURAL_SPLINE',
'CUBIC_SPLINE',
'AKIMA_SPLINE'):
314 if hasattr(overscanImage,
"getImage"):
315 biasArray = overscanImage.getImage().getArray()
316 biasArray = numpy.ma.masked_where(overscanImage.getMask().getArray() & statControl.getAndMask(),
319 biasArray = overscanImage.getArray()
321 shortInd = numpy.argmin(biasArray.shape)
324 biasArray = numpy.transpose(biasArray)
327 percentiles = numpy.percentile(biasArray, [25.0, 50.0, 75.0], axis=1)
328 medianBiasArr = percentiles[1]
329 stdevBiasArr = 0.74*(percentiles[2] - percentiles[0])
330 diff = numpy.abs(biasArray - medianBiasArr[:,numpy.newaxis])
331 biasMaskedArr = numpy.ma.masked_where(diff > collapseRej*stdevBiasArr[:,numpy.newaxis], biasArray)
332 collapsed = numpy.mean(biasMaskedArr, axis=1)
333 del biasArray, percentiles, stdevBiasArr, diff, biasMaskedArr
336 collapsed = numpy.transpose(collapsed)
339 indices = 2.0*numpy.arange(num)/float(num) - 1.0
341 if fitType
in (
'POLY',
'CHEB',
'LEG'):
343 poly = numpy.polynomial
344 fitter, evaler = {
"POLY": (poly.polynomial.polyfit, poly.polynomial.polyval),
345 "CHEB": (poly.chebyshev.chebfit, poly.chebyshev.chebval),
346 "LEG": (poly.legendre.legfit, poly.legendre.legval),
349 coeffs = fitter(indices, collapsed, order)
350 fitBiasArr = evaler(indices, coeffs)
351 elif 'SPLINE' in fitType:
360 collapsedMask = collapsed.mask
362 if collapsedMask == numpy.ma.nomask:
363 collapsedMask = numpy.array(len(collapsed)*[numpy.ma.nomask])
367 numPerBin, binEdges = numpy.histogram(indices, bins=numBins,
368 weights=1-collapsedMask.astype(int))
371 values = numpy.histogram(indices, bins=numBins, weights=collapsed)[0]/numPerBin
372 binCenters = numpy.histogram(indices, bins=numBins, weights=indices)[0]/numPerBin
374 values.astype(float)[numPerBin > 0],
376 fitBiasArr = numpy.array([interp.interpolate(i)
for i
in indices])
380 import matplotlib.pyplot
as plot
381 figure = plot.figure(1)
383 axes = figure.add_axes((0.1, 0.1, 0.8, 0.8))
384 axes.plot(indices, collapsed,
'k+')
385 axes.plot(indices, fitBiasArr,
'r-')
387 prompt =
"Press Enter or c to continue [chp]... "
389 ans = raw_input(prompt).lower()
390 if ans
in (
"",
"c",):
393 import pdb; pdb.set_trace()
395 print "h[elp] c[ontinue] p[db]"
398 offImage = ampImage.Factory(ampImage.getDimensions())
399 offArray = offImage.getArray()
401 offArray[:,:] = fitBiasArr[:,numpy.newaxis]
403 offArray[:,:] = fitBiasArr[numpy.newaxis,:]
411 mask = ampMaskedImage.getMask()
412 maskArray = mask.getArray()
if shortInd == 1
else mask.getArray().transpose()
413 suspect = mask.getPlaneBitMask(
"SUSPECT")
415 if collapsed.mask == numpy.ma.nomask:
419 for low
in xrange(num):
420 if not collapsed.mask[low]:
423 maskArray[:low,:] |= suspect
424 for high
in xrange(1, num):
425 if not collapsed.mask[-high]:
428 maskArray[-high:,:] |= suspect
432 (
"overscanCorrection", fitType)
Interpolate::Style stringToInterpStyle(std::string const &style)
Conversion function to switch a string to an Interpolate::Style.
boost::shared_ptr< Interpolate > makeInterpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style=Interpolate::AKIMA_SPLINE)
Pass parameters to a Statistics objectA class to pass parameters which control how the stats are calc...
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.