106 def __init__(self, config, skymap, tract, values=None, numbers=None, variances=None):
113 tractDimX, tractDimY = self.
tractInfo.getBBox().getDimensions()
117 values = afwImage.ImageF(self.
dims)
120 values = values.clone()
121 assert values.getDimensions() == self.
dims
124 numbers = afwImage.ImageF(self.
dims)
127 numbers = numbers.clone()
128 assert numbers.getDimensions() == self.
dims
130 if variances
is None:
131 variances = afwImage.ImageF(self.
dims)
134 variances = variances.clone()
135 assert variances.getDimensions() == self.
dims
154 """Bins masked images of warps and adds these values into a blank image
155 with the binned tract dimensions at the location of the warp in the
160 warp : `lsst.afw.image.ExposureF`
161 Warped image corresponding to a single patch in a single visit.
163 image = warp.getMaskedImage()
164 maskVal = image.getMask().getPlaneBitMask(self.
config.mask)
166 warped = afwImage.ImageF(self.
_values.getBBox())
167 warpedCounts = afwImage.ImageF(self.
_numbers.getBBox())
168 warpedVariances = afwImage.ImageF(self.
_variances.getBBox())
170 stats = afwMath.StatisticsControl()
171 stats.setAndMask(maskVal)
172 stats.setNanSafe(
True)
175 pixels = itertools.product(
176 warped.getBBox().x.arange(),
177 warped.getBBox().y.arange(),
179 for xx, yy
in pixels:
186 bbox.clip(image.getBBox())
189 subImage = image.Factory(image, bbox)
190 result = afwMath.makeStatistics(
191 subImage, afwMath.MEANCLIP | afwMath.NPOINT | afwMath.VARIANCECLIP, stats
193 mean = result.getValue(afwMath.MEANCLIP)
194 num = result.getValue(afwMath.NPOINT)
195 var = result.getValue(afwMath.VARIANCECLIP)
196 if not numpy.isfinite(mean)
or not numpy.isfinite(num):
198 warped[xx, yy, afwImage.LOCAL] = mean * num
199 warpedCounts[xx, yy, afwImage.LOCAL] = num
200 warpedVariances[xx, yy, afwImage.LOCAL] = var * num
236 """Produce a background model for a warp
238 The superpixel model is transformed back to the native pixel
239 resolution, for application to the background of an individual warp.
243 warp : `lsst.afw.image.ExposureF`
244 Warped image corresponding to a single patch in a single visit.
248 bg : `lsst.afw.math.BackgroundList`
249 Background model for warp.
252 binTransform = geom.AffineTransform.makeScaling(self.
config.binning)
257 coo = wcs.pixelToSky(1, 1)
259 ptchDimX, ptchDimY = ptch.getInnerBBox().getDimensions()
260 if ptchDimX != ptchDimY:
262 "Patch dimensions %d,%d are unequal: cannot proceed as written." % (ptchDimX, ptchDimY)
264 overlap = (warp.getBBox().getDimensions().getX() - ptchDimX) // 2
265 corner = warp.getBBox().getMin()
266 if corner[0] % ptchDimX != 0:
271 geom.AffineTransform.makeTranslation(
geom.Extent2D(-0.5, -0.5))
272 * geom.AffineTransform.makeScaling(1.0 / self.
config.xBin, 1.0 / self.
config.yBin)
273 * geom.AffineTransform.makeTranslation(offset)
275 transform = afwGeom.makeTransform(tractTransform)
278 toSample = afwGeom.makeTransform(binTransform).then(transform)
282 tpNorm = afwImage.ImageF(tractPlane.getBBox())
286 bbox = warp.getBBox()
287 image = afwImage.ImageF(bbox.getDimensions() // self.
config.binning)
288 norm = afwImage.ImageF(image.getBBox())
291 ctrl = afwMath.WarpingControl(
"bilinear")
292 afwMath.warpImage(image, tractPlane, toSample.inverted(), ctrl)
293 afwMath.warpImage(norm, tpNorm, toSample.inverted(), ctrl)
297 mask = afwImage.Mask(image.getBBox())
298 isBad = numpy.isnan(image.getArray())
299 mask.getArray()[isBad] = mask.getPlaneBitMask(
"BAD")
300 image.getArray()[isBad] = image.getArray()[~isBad].mean()
302 return afwMath.BackgroundList(
304 afwMath.BackgroundMI(warp.getBBox(), afwImage.makeMaskedImage(image, mask)),
305 afwMath.stringToInterpStyle(self.
config.interpolation),
306 afwMath.stringToUndersampleStyle(
"REDUCE_INTERP_ORDER"),
307 afwMath.ApproximateControl.UNKNOWN,
315 """Return the background model data
317 This is the measurement of the background for each of the superpixels.
323 isBad = self.
_numbers.getArray() < thresh
325 array = values.getArray()
326 array[:] = smoothArray(array, isBad, self.
config.smoothScale)
327 isBad = numpy.isnan(values.array)
330 interpolateBadPixels(values.getArray(), isBad, self.
config.interpolation)