95 def run(self, maskedImage):
96 """Rescale the variance in a maskedImage in place.
100 maskedImage : `lsst.afw.image.MaskedImage`
101 Image for which to determine the variance rescaling factor. The image
102 is modified in place.
107 Variance rescaling factor.
112 If the estimated variance rescaling factor by both methods exceed the
117 The task calculates and applies the pixel-based correction unless
118 it is over the ``config.limit`` threshold. In this case, the image-based
123 if factor > self.config.limit:
124 self.log.warning(
"Pixel-based variance rescaling factor (%f) exceeds configured limit (%f); "
125 "trying image-based method", factor, self.config.limit)
127 if factor > self.config.limit:
128 raise RuntimeError(
"Variance rescaling factor (%f) exceeds configured limit (%f)" %
129 (factor, self.config.limit))
130 self.log.info(
"Renormalizing variance by %f", factor)
131 maskedImage.variance *= factor
156 """Determine the variance rescaling factor from pixel statistics
158 We calculate SNR = image/sqrt(variance), and the distribution
159 for most of the background-subtracted image should have a standard
160 deviation of unity. We use the interquartile range as a robust estimator
161 of the SNR standard deviation. The variance rescaling factor is the
162 factor that brings that distribution to have unit standard deviation.
164 This may not work well if the image has a lot of structure in it, as
165 the assumptions are violated. In that case, use an alternate
170 maskedImage : `lsst.afw.image.MaskedImage`
171 Image for which to determine the variance rescaling factor.
176 Variance rescaling factor or 1 if all pixels are masked or non-finite.
179 maskVal = maskedImage.mask.getPlaneBitMask(self.config.maskPlanes)
180 isGood = (((maskedImage.mask.array & maskVal) == 0)
181 & np.isfinite(maskedImage.image.array)
182 & np.isfinite(maskedImage.variance.array)
183 & (maskedImage.variance.array > 0))
185 nGood = np.sum(isGood)
186 self.log.debug(
"Number of selected background pixels: %d of %d.", nGood, isGood.size)
192 snr = maskedImage.image.array[isGood]/np.sqrt(maskedImage.variance.array[isGood])
193 q1, q3 = np.percentile(snr, (25, 75))
194 stdev = 0.74*(q3 - q1)
198 """Determine the variance rescaling factor from image statistics
200 We calculate average(SNR) = stdev(image)/median(variance), and
201 the value should be unity. We use the interquartile range as a robust
202 estimator of the stdev. The variance rescaling factor is the
203 factor that brings this value to unity.
205 This may not work well if the pixels from which we measure the
206 standard deviation of the image are not effectively the same pixels
207 from which we measure the median of the variance. In that case, use
212 maskedImage : `lsst.afw.image.MaskedImage`
213 Image for which to determine the variance rescaling factor.
218 Variance rescaling factor or 1 if all pixels are masked or non-finite.
220 maskVal = maskedImage.mask.getPlaneBitMask(self.config.maskPlanes)
221 isGood = (((maskedImage.mask.array & maskVal) == 0)
222 & np.isfinite(maskedImage.image.array)
223 & np.isfinite(maskedImage.variance.array)
224 & (maskedImage.variance.array > 0))
225 nGood = np.sum(isGood)
226 self.log.debug(
"Number of selected background pixels: %d of %d.", nGood, isGood.size)
232 q1, q3 = np.percentile(maskedImage.image.array[isGood], (25, 75))
233 ratio = 0.74*(q3 - q1)/np.sqrt(np.median(maskedImage.variance.array[isGood]))