26 import lsst.pex.config
as pexConfig
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 overscanIsInt = pexConfig.Field(
64 doc=
"Treat overscan as an integer image for purposes of fitType=MEDIAN" +
65 " and fitType=MEDIAN_PER_ROW.",
71 """Correction task for overscan.
73 This class contains a number of utilities that are easier to
74 understand and use when they are not embedded in nested if/else
79 statControl : `lsst.afw.math.StatisticsControl`, optional
80 Statistics control object.
82 ConfigClass = OverscanCorrectionTaskConfig
83 _DefaultName =
"overscan"
85 def __init__(self, statControl=None, **kwargs):
91 self.
statControl.setNumSigmaClip(self.config.numSigmaClip)
93 def run(self, ampImage, overscanImage):
94 """Measure and remove an overscan from an amplifier image.
98 ampImage : `lsst.afw.image.Image`
99 Image data that will have the overscan removed.
100 overscanImage : `lsst.afw.image.Image`
101 Overscan data that the overscan is measured from.
105 overscanResults : `lsst.pipe.base.Struct`
106 Result struct with components:
109 Value or fit subtracted from the amplifier image data
110 (scalar or `lsst.afw.image.Image`).
112 Value or fit subtracted from the overscan image data
113 (scalar or `lsst.afw.image.Image`).
115 Image of the overscan region with the overscan
116 correction applied (`lsst.afw.image.Image`). This
117 quantity is used to estimate the amplifier read noise
123 Raised if an invalid overscan type is set.
126 if self.config.fitType
in (
'MEAN',
'MEANCLIP',
'MEDIAN'):
128 overscanValue = overscanResult.overscanValue
129 offImage = overscanValue
130 overscanModel = overscanValue
132 elif self.config.fitType
in (
'MEDIAN_PER_ROW',
'POLY',
'CHEB',
'LEG',
133 'NATURAL_SPLINE',
'CUBIC_SPLINE',
'AKIMA_SPLINE'):
135 overscanValue = overscanResult.overscanValue
136 maskArray = overscanResult.maskArray
137 isTransposed = overscanResult.isTransposed
139 offImage = afwImage.ImageF(ampImage.getDimensions())
140 offArray = offImage.getArray()
141 overscanModel = afwImage.ImageF(overscanImage.getDimensions())
142 overscanArray = overscanModel.getArray()
144 if hasattr(ampImage,
'getMask'):
145 maskSuspect = afwImage.Mask(ampImage.getDimensions())
150 offArray[:, :] = overscanValue[np.newaxis, :]
151 overscanArray[:, :] = overscanValue[np.newaxis, :]
153 maskSuspect.getArray()[:, maskArray] |= ampImage.getMask().getPlaneBitMask(
"SUSPECT")
155 offArray[:, :] = overscanValue[:, np.newaxis]
156 overscanArray[:, :] = overscanValue[:, np.newaxis]
158 maskSuspect.getArray()[maskArray, :] |= ampImage.getMask().getPlaneBitMask(
"SUSPECT")
160 raise RuntimeError(
'%s : %s an invalid overscan type' %
161 (
"overscanCorrection", self.config.fitType))
163 self.
debugView(overscanImage, overscanValue)
167 ampImage.getMask().getArray()[:, :] |= maskSuspect.getArray()[:, :]
168 overscanImage -= overscanModel
169 return pipeBase.Struct(imageFit=offImage,
170 overscanFit=overscanModel,
171 overscanImage=overscanImage,
172 edgeMask=maskSuspect)
176 """Return an integer version of the input image.
180 image : `numpy.ndarray`, `lsst.afw.image.Image` or `MaskedImage`
181 Image to convert to integers.
185 outI : `numpy.ndarray`, `lsst.afw.image.Image` or `MaskedImage`
186 The integer converted image.
191 Raised if the input image could not be converted.
193 if hasattr(image,
"image"):
195 imageI = image.image.convertI()
196 outI = afwImage.MaskedImageI(imageI, image.mask, image.variance)
197 elif hasattr(image,
"convertI"):
199 outI = image.convertI()
200 elif hasattr(image,
"astype"):
202 outI = image.astype(int)
204 raise RuntimeError(
"Could not convert this to integers: %s %s %s",
205 image, type(image), dir(image))
210 """Measure a constant overscan value.
214 image : `lsst.afw.image.Image` or `lsst.afw.image.MaskedImage`
215 Image data to measure the overscan from.
219 results : `lsst.pipe.base.Struct`
220 Overscan result with entries:
221 - ``overscanValue``: Overscan value to subtract (`float`)
222 - ``maskArray``: Placeholder for a mask array (`list`)
223 - ``isTransposed``: Orientation of the overscan (`bool`)
225 if self.config.fitType ==
'MEDIAN':
230 fitType = afwMath.stringToStatisticsProperty(self.config.fitType)
231 overscanValue = afwMath.makeStatistics(calcImage, fitType, self.
statControl).getValue()
233 return pipeBase.Struct(overscanValue=overscanValue,
240 """Extract the numpy array from the input image.
244 image : `lsst.afw.image.Image` or `lsst.afw.image.MaskedImage`
245 Image data to pull array from.
247 calcImage : `numpy.ndarray`
248 Image data array for numpy operating.
250 if hasattr(image,
"getImage"):
251 calcImage = image.getImage().getArray()
253 calcImage = image.getArray()
258 """Transpose input numpy array if necessary.
262 imageArray : `numpy.ndarray`
263 Image data to transpose.
267 imageArray : `numpy.ndarray`
268 Transposed image data.
269 isTransposed : `bool`
270 Indicates whether the input data was transposed.
272 if np.argmin(imageArray.shape) == 0:
273 return np.transpose(imageArray),
True
275 return imageArray,
False
278 """Mask outliers in a row of overscan data from a robust sigma
283 imageArray : `numpy.ndarray`
284 Image to filter along numpy axis=1.
288 maskedArray : `numpy.ma.masked_array`
289 Masked image marking outliers.
291 lq, median, uq = np.percentile(imageArray, [25.0, 50.0, 75.0], axis=1)
293 axisStdev = 0.74*(uq - lq)
295 diff = np.abs(imageArray - axisMedians[:, np.newaxis])
296 return np.ma.masked_where(diff > self.
statControl.getNumSigmaClip() *
297 axisStdev[:, np.newaxis], imageArray)
301 """Collapse overscan array (and mask) to a 1-D vector of values.
305 maskedArray : `numpy.ma.masked_array`
306 Masked array of input overscan data.
310 collapsed : `numpy.ma.masked_array`
311 Single dimensional overscan data, combined with the mean.
313 collapsed = np.mean(maskedArray, axis=1)
314 if collapsed.mask.sum() > 0:
315 collapsed.data[collapsed.mask] = np.mean(maskedArray.data[collapsed.mask], axis=1)
319 """Collapse overscan array (and mask) to a 1-D vector of using the
320 correct integer median of row-values.
324 maskedArray : `numpy.ma.masked_array`
325 Masked array of input overscan data.
329 collapsed : `numpy.ma.masked_array`
330 Single dimensional overscan data, combined with the afwMath median.
335 fitType = afwMath.stringToStatisticsProperty(
'MEDIAN')
336 for row
in integerMI:
337 rowMedian = afwMath.makeStatistics(row, fitType, self.
statControl).getValue()
338 collapsed.append(rowMedian)
340 return np.array(collapsed)
343 """Wrapper function to match spline fit API to polynomial fit API.
347 indices : `numpy.ndarray`
348 Locations to evaluate the spline.
349 collapsed : `numpy.ndarray`
350 Collapsed overscan values corresponding to the spline
353 Number of bins to use in constructing the spline.
357 interp : `lsst.afw.math.Interpolate`
358 Interpolation object for later evaluation.
360 if not np.ma.is_masked(collapsed):
361 collapsed.mask = np.array(len(collapsed)*[np.ma.nomask])
363 numPerBin, binEdges = np.histogram(indices, bins=numBins,
364 weights=1 - collapsed.mask.astype(int))
365 with np.errstate(invalid=
"ignore"):
366 values = np.histogram(indices, bins=numBins,
367 weights=collapsed.data*~collapsed.mask)[0]/numPerBin
368 binCenters = np.histogram(indices, bins=numBins,
369 weights=indices*~collapsed.mask)[0]/numPerBin
370 interp = afwMath.makeInterpolate(binCenters.astype(float)[numPerBin > 0],
371 values.astype(float)[numPerBin > 0],
372 afwMath.stringToInterpStyle(self.config.fitType))
377 """Wrapper function to match spline evaluation API to polynomial fit API.
381 indices : `numpy.ndarray`
382 Locations to evaluate the spline.
383 interp : `lsst.afw.math.interpolate`
384 Interpolation object to use.
388 values : `numpy.ndarray`
389 Evaluated spline values at each index.
392 return interp.interpolate(indices.astype(float))
396 """Create mask if edges are extrapolated.
400 collapsed : `numpy.ma.masked_array`
401 Masked array to check the edges of.
405 maskArray : `numpy.ndarray`
406 Boolean numpy array of pixels to mask.
408 maskArray = np.full_like(collapsed,
False, dtype=bool)
409 if np.ma.is_masked(collapsed):
411 for low
in range(num):
412 if not collapsed.mask[low]:
415 maskArray[:low] =
True
416 for high
in range(1, num):
417 if not collapsed.mask[-high]:
420 maskArray[-high:] =
True
424 """Calculate the 1-d vector overscan from the input overscan image.
428 image : `lsst.afw.image.MaskedImage`
429 Image containing the overscan data.
433 results : `lsst.pipe.base.Struct`
434 Overscan result with entries:
435 - ``overscanValue``: Overscan value to subtract (`float`)
436 - ``maskArray`` : `list` [ `bool` ]
437 List of rows that should be masked as ``SUSPECT`` when the
438 overscan solution is applied.
439 - ``isTransposed`` : `bool`
440 Indicates if the overscan data was transposed during
441 calcuation, noting along which axis the overscan should be
447 calcImage, isTransposed = self.
transpose(calcImage)
450 if self.config.fitType ==
'MEDIAN_PER_ROW':
457 indices = 2.0*np.arange(num)/float(num) - 1.0
461 'POLY': (poly.polynomial.polyfit, poly.polynomial.polyval),
462 'CHEB': (poly.chebyshev.chebfit, poly.chebyshev.chebval),
463 'LEG': (poly.legendre.legfit, poly.legendre.legval),
467 }[self.config.fitType]
469 coeffs = fitter(indices, collapsed, self.config.order)
470 overscanVector = evaler(indices, coeffs)
472 return pipeBase.Struct(overscanValue=np.array(overscanVector),
474 isTransposed=isTransposed)
477 """Debug display for the final overscan solution.
481 image : `lsst.afw.image.Image`
482 Input image the overscan solution was determined from.
483 model : `numpy.ndarray` or `float`
484 Overscan model determined for the image.
491 calcImage, isTransposed = self.
transpose(calcImage)
496 indices = 2.0 * np.arange(num)/float(num) - 1.0
498 if np.ma.is_masked(collapsed):
499 collapsedMask = collapsed.mask
501 collapsedMask = np.array(num*[np.ma.nomask])
503 import matplotlib.pyplot
as plot
504 figure = plot.figure(1)
506 axes = figure.add_axes((0.1, 0.1, 0.8, 0.8))
507 axes.plot(indices[~collapsedMask], collapsed[~collapsedMask],
'k+')
508 if collapsedMask.sum() > 0:
509 axes.plot(indices[collapsedMask], collapsed.data[collapsedMask],
'b+')
510 if isinstance(model, np.ndarray):
513 plotModel = np.zeros_like(indices)
515 axes.plot(indices, plotModel,
'r-')
516 plot.xlabel(
"centered/scaled position along overscan region")
517 plot.ylabel(
"pixel value/fit value")
519 prompt =
"Press Enter or c to continue [chp]..."
521 ans = input(prompt).lower()
522 if ans
in (
"",
" ",
"c",):
528 print(
"[h]elp [c]ontinue [p]db")