28 __all__ = [
"OverscanCorrectionTaskConfig",
"OverscanCorrectionTask"]
32 """Overscan correction options.
34 fitType = pexConfig.ChoiceField(
36 doc=
"The method for fitting the overscan bias level.",
39 "POLY":
"Fit ordinary polynomial to the longest axis of the overscan region",
40 "CHEB":
"Fit Chebyshev polynomial to the longest axis of the overscan region",
41 "LEG":
"Fit Legendre polynomial to the longest axis of the overscan region",
42 "NATURAL_SPLINE":
"Fit natural spline to the longest axis of the overscan region",
43 "CUBIC_SPLINE":
"Fit cubic spline to the longest axis of the overscan region",
44 "AKIMA_SPLINE":
"Fit Akima spline to the longest axis of the overscan region",
45 "MEAN":
"Correct using the mean of the overscan region",
46 "MEANCLIP":
"Correct using a clipped mean of the overscan region",
47 "MEDIAN":
"Correct using the median of the overscan region",
48 "MEDIAN_PER_ROW":
"Correct using the median per row of the overscan region",
51 order = pexConfig.Field(
53 doc=(
"Order of polynomial to fit if overscan fit type is a polynomial, "
54 "or number of spline knots if overscan fit type is a spline."),
57 numSigmaClip = pexConfig.Field(
59 doc=
"Rejection threshold (sigma) for collapsing overscan before fit",
62 maskPlanes = pexConfig.ListField(
64 doc=
"Mask planes to reject when measuring overscan",
67 overscanIsInt = pexConfig.Field(
69 doc=
"Treat overscan as an integer image for purposes of fitType=MEDIAN"
70 " and fitType=MEDIAN_PER_ROW.",
76 """Correction task for overscan.
78 This class contains a number of utilities that are easier to
79 understand and use when they are not embedded in nested if/else
84 statControl : `lsst.afw.math.StatisticsControl`, optional
85 Statistics control object.
87 ConfigClass = OverscanCorrectionTaskConfig
88 _DefaultName =
"overscan"
90 def __init__(self, statControl=None, **kwargs):
96 self.
statControl.setNumSigmaClip(self.config.numSigmaClip)
97 self.
statControl.setAndMask(afwImage.Mask.getPlaneBitMask(self.config.maskPlanes))
99 def run(self, ampImage, overscanImage):
100 """Measure and remove an overscan from an amplifier image.
104 ampImage : `lsst.afw.image.Image`
105 Image data that will have the overscan removed.
106 overscanImage : `lsst.afw.image.Image`
107 Overscan data that the overscan is measured from.
111 overscanResults : `lsst.pipe.base.Struct`
112 Result struct with components:
115 Value or fit subtracted from the amplifier image data
116 (scalar or `lsst.afw.image.Image`).
118 Value or fit subtracted from the overscan image data
119 (scalar or `lsst.afw.image.Image`).
121 Image of the overscan region with the overscan
122 correction applied (`lsst.afw.image.Image`). This
123 quantity is used to estimate the amplifier read noise
129 Raised if an invalid overscan type is set.
132 if self.config.fitType
in (
'MEAN',
'MEANCLIP',
'MEDIAN'):
134 overscanValue = overscanResult.overscanValue
135 offImage = overscanValue
136 overscanModel = overscanValue
138 elif self.config.fitType
in (
'MEDIAN_PER_ROW',
'POLY',
'CHEB',
'LEG',
139 'NATURAL_SPLINE',
'CUBIC_SPLINE',
'AKIMA_SPLINE'):
141 overscanValue = overscanResult.overscanValue
142 maskArray = overscanResult.maskArray
143 isTransposed = overscanResult.isTransposed
145 offImage = afwImage.ImageF(ampImage.getDimensions())
146 offArray = offImage.getArray()
147 overscanModel = afwImage.ImageF(overscanImage.getDimensions())
148 overscanArray = overscanModel.getArray()
150 if hasattr(ampImage,
'getMask'):
151 maskSuspect = afwImage.Mask(ampImage.getDimensions())
156 offArray[:, :] = overscanValue[np.newaxis, :]
157 overscanArray[:, :] = overscanValue[np.newaxis, :]
159 maskSuspect.getArray()[:, maskArray] |= ampImage.getMask().getPlaneBitMask(
"SUSPECT")
161 offArray[:, :] = overscanValue[:, np.newaxis]
162 overscanArray[:, :] = overscanValue[:, np.newaxis]
164 maskSuspect.getArray()[maskArray, :] |= ampImage.getMask().getPlaneBitMask(
"SUSPECT")
166 raise RuntimeError(
'%s : %s an invalid overscan type' %
167 (
"overscanCorrection", self.config.fitType))
169 self.
debugView(overscanImage, overscanValue)
173 ampImage.getMask().getArray()[:, :] |= maskSuspect.getArray()[:, :]
174 overscanImage -= overscanModel
175 return pipeBase.Struct(imageFit=offImage,
176 overscanFit=overscanModel,
177 overscanImage=overscanImage,
178 edgeMask=maskSuspect)
182 """Return an integer version of the input image.
186 image : `numpy.ndarray`, `lsst.afw.image.Image` or `MaskedImage`
187 Image to convert to integers.
191 outI : `numpy.ndarray`, `lsst.afw.image.Image` or `MaskedImage`
192 The integer converted image.
197 Raised if the input image could not be converted.
199 if hasattr(image,
"image"):
201 imageI = image.image.convertI()
202 outI = afwImage.MaskedImageI(imageI, image.mask, image.variance)
203 elif hasattr(image,
"convertI"):
205 outI = image.convertI()
206 elif hasattr(image,
"astype"):
208 outI = image.astype(int)
210 raise RuntimeError(
"Could not convert this to integers: %s %s %s",
211 image, type(image), dir(image))
216 """Measure a constant overscan value.
220 image : `lsst.afw.image.Image` or `lsst.afw.image.MaskedImage`
221 Image data to measure the overscan from.
225 results : `lsst.pipe.base.Struct`
226 Overscan result with entries:
227 - ``overscanValue``: Overscan value to subtract (`float`)
228 - ``maskArray``: Placeholder for a mask array (`list`)
229 - ``isTransposed``: Orientation of the overscan (`bool`)
231 if self.config.fitType ==
'MEDIAN':
236 fitType = afwMath.stringToStatisticsProperty(self.config.fitType)
237 overscanValue = afwMath.makeStatistics(calcImage, fitType, self.
statControl).getValue()
239 return pipeBase.Struct(overscanValue=overscanValue,
245 """Extract the numpy array from the input image.
249 image : `lsst.afw.image.Image` or `lsst.afw.image.MaskedImage`
250 Image data to pull array from.
252 calcImage : `numpy.ndarray`
253 Image data array for numpy operating.
255 if hasattr(image,
"getImage"):
256 calcImage = image.getImage().getArray()
257 calcImage = np.ma.masked_where(image.getMask().getArray() & self.
statControl.getAndMask(),
260 calcImage = image.getArray()
265 """Transpose input numpy array if necessary.
269 imageArray : `numpy.ndarray`
270 Image data to transpose.
274 imageArray : `numpy.ndarray`
275 Transposed image data.
276 isTransposed : `bool`
277 Indicates whether the input data was transposed.
279 if np.argmin(imageArray.shape) == 0:
280 return np.transpose(imageArray),
True
282 return imageArray,
False
285 """Mask outliers in a row of overscan data from a robust sigma
290 imageArray : `numpy.ndarray`
291 Image to filter along numpy axis=1.
295 maskedArray : `numpy.ma.masked_array`
296 Masked image marking outliers.
298 lq, median, uq = np.percentile(imageArray, [25.0, 50.0, 75.0], axis=1)
300 axisStdev = 0.74*(uq - lq)
302 diff = np.abs(imageArray - axisMedians[:, np.newaxis])
303 return np.ma.masked_where(diff > self.
statControl.getNumSigmaClip()
304 * axisStdev[:, np.newaxis], imageArray)
308 """Collapse overscan array (and mask) to a 1-D vector of values.
312 maskedArray : `numpy.ma.masked_array`
313 Masked array of input overscan data.
317 collapsed : `numpy.ma.masked_array`
318 Single dimensional overscan data, combined with the mean.
320 collapsed = np.mean(maskedArray, axis=1)
321 if collapsed.mask.sum() > 0:
322 collapsed.data[collapsed.mask] = np.mean(maskedArray.data[collapsed.mask], axis=1)
326 """Collapse overscan array (and mask) to a 1-D vector of using the
327 correct integer median of row-values.
331 maskedArray : `numpy.ma.masked_array`
332 Masked array of input overscan data.
336 collapsed : `numpy.ma.masked_array`
337 Single dimensional overscan data, combined with the afwMath median.
342 fitType = afwMath.stringToStatisticsProperty(
'MEDIAN')
343 for row
in integerMI:
344 newRow = row.compressed()
345 rowMedian = afwMath.makeStatistics(newRow, fitType, self.
statControl).getValue()
346 collapsed.append(rowMedian)
348 return np.array(collapsed)
351 """Wrapper function to match spline fit API to polynomial fit API.
355 indices : `numpy.ndarray`
356 Locations to evaluate the spline.
357 collapsed : `numpy.ndarray`
358 Collapsed overscan values corresponding to the spline
361 Number of bins to use in constructing the spline.
365 interp : `lsst.afw.math.Interpolate`
366 Interpolation object for later evaluation.
368 if not np.ma.is_masked(collapsed):
369 collapsed.mask = np.array(len(collapsed)*[np.ma.nomask])
371 numPerBin, binEdges = np.histogram(indices, bins=numBins,
372 weights=1 - collapsed.mask.astype(int))
373 with np.errstate(invalid=
"ignore"):
374 values = np.histogram(indices, bins=numBins,
375 weights=collapsed.data*~collapsed.mask)[0]/numPerBin
376 binCenters = np.histogram(indices, bins=numBins,
377 weights=indices*~collapsed.mask)[0]/numPerBin
378 interp = afwMath.makeInterpolate(binCenters.astype(float)[numPerBin > 0],
379 values.astype(float)[numPerBin > 0],
380 afwMath.stringToInterpStyle(self.config.fitType))
385 """Wrapper function to match spline evaluation API to polynomial fit API.
389 indices : `numpy.ndarray`
390 Locations to evaluate the spline.
391 interp : `lsst.afw.math.interpolate`
392 Interpolation object to use.
396 values : `numpy.ndarray`
397 Evaluated spline values at each index.
400 return interp.interpolate(indices.astype(float))
404 """Create mask if edges are extrapolated.
408 collapsed : `numpy.ma.masked_array`
409 Masked array to check the edges of.
413 maskArray : `numpy.ndarray`
414 Boolean numpy array of pixels to mask.
416 maskArray = np.full_like(collapsed,
False, dtype=bool)
417 if np.ma.is_masked(collapsed):
419 for low
in range(num):
420 if not collapsed.mask[low]:
423 maskArray[:low] =
True
424 for high
in range(1, num):
425 if not collapsed.mask[-high]:
428 maskArray[-high:] =
True
432 """Calculate the 1-d vector overscan from the input overscan image.
436 image : `lsst.afw.image.MaskedImage`
437 Image containing the overscan data.
441 results : `lsst.pipe.base.Struct`
442 Overscan result with entries:
443 - ``overscanValue``: Overscan value to subtract (`float`)
444 - ``maskArray`` : `list` [ `bool` ]
445 List of rows that should be masked as ``SUSPECT`` when the
446 overscan solution is applied.
447 - ``isTransposed`` : `bool`
448 Indicates if the overscan data was transposed during
449 calcuation, noting along which axis the overscan should be
455 calcImage, isTransposed = self.
transpose(calcImage)
458 if self.config.fitType ==
'MEDIAN_PER_ROW':
465 indices = 2.0*np.arange(num)/float(num) - 1.0
469 'POLY': (poly.polynomial.polyfit, poly.polynomial.polyval),
470 'CHEB': (poly.chebyshev.chebfit, poly.chebyshev.chebval),
471 'LEG': (poly.legendre.legfit, poly.legendre.legval),
475 }[self.config.fitType]
477 coeffs = fitter(indices, collapsed, self.config.order)
478 overscanVector = evaler(indices, coeffs)
480 return pipeBase.Struct(overscanValue=np.array(overscanVector),
482 isTransposed=isTransposed)
485 """Debug display for the final overscan solution.
489 image : `lsst.afw.image.Image`
490 Input image the overscan solution was determined from.
491 model : `numpy.ndarray` or `float`
492 Overscan model determined for the image.
499 calcImage, isTransposed = self.
transpose(calcImage)
504 indices = 2.0 * np.arange(num)/float(num) - 1.0
506 if np.ma.is_masked(collapsed):
507 collapsedMask = collapsed.mask
509 collapsedMask = np.array(num*[np.ma.nomask])
511 import matplotlib.pyplot
as plot
512 figure = plot.figure(1)
514 axes = figure.add_axes((0.1, 0.1, 0.8, 0.8))
515 axes.plot(indices[~collapsedMask], collapsed[~collapsedMask],
'k+')
516 if collapsedMask.sum() > 0:
517 axes.plot(indices[collapsedMask], collapsed.data[collapsedMask],
'b+')
518 if isinstance(model, np.ndarray):
521 plotModel = np.zeros_like(indices)
523 axes.plot(indices, plotModel,
'r-')
524 plot.xlabel(
"centered/scaled position along overscan region")
525 plot.ylabel(
"pixel value/fit value")
527 prompt =
"Press Enter or c to continue [chp]..."
529 ans = input(prompt).lower()
530 if ans
in (
"",
" ",
"c",):
536 print(
"[h]elp [c]ontinue [p]db")