204 def fitBackground(self, maskedImage, nx=0, ny=0, algorithm=None):
205 """Estimate the background of a masked image
206
207 Parameters
208 ----------
209 maskedImage : `lsst.afw.image.maskedImage`
210 Masked image whose background is to be computed
211 nx : 'int`
212 Number of x bands; if 0 compute from width and `self.config.binSizeX`
213 ny : `int`
214 Number of y bands; if 0 compute from height and `self.config.binSizeY`
215 algorithm : `str`
216 Name of interpolation algorithm; if None use `self.config.algorithm`
217
218 Returns
219 -------
220 bg : `lsst.afw.math.Background`
221 A fit background
222
223 Raises
224 ------
225 RuntimeError
226 Raised if lsst.afw.math.makeBackground returns None, an indicator
227 of failure.
228 """
229
230 binSizeX = self.config.binSize if self.config.binSizeX == 0 else self.config.binSizeX
231 binSizeY = self.config.binSize if self.config.binSizeY == 0 else self.config.binSizeY
232
233 if not nx:
234 nx = maskedImage.getWidth()//binSizeX + 1
235 if not ny:
236 ny = maskedImage.getHeight()//binSizeY + 1
237
238 unsubFrame = getDebugFrame(self._display, "unsubtracted")
239 if unsubFrame:
240 unsubDisp = afwDisplay.getDisplay(frame=unsubFrame)
241 unsubDisp.mtv(maskedImage, title="unsubtracted")
242 xPosts = numpy.rint(numpy.linspace(0, maskedImage.getWidth() + 1, num=nx, endpoint=True))
243 yPosts = numpy.rint(numpy.linspace(0, maskedImage.getHeight() + 1, num=ny, endpoint=True))
244 with unsubDisp.Buffering():
245 for (xMin, xMax), (yMin, yMax) in itertools.product(zip(xPosts[:-1], xPosts[1:]),
246 zip(yPosts[:-1], yPosts[1:])):
247 unsubDisp.line([(xMin, yMin), (xMin, yMax), (xMax, yMax), (xMax, yMin), (xMin, yMin)])
248
250 badMask = maskedImage.mask.getPlaneBitMask(self.config.ignoredPixelMask)
251
252 sctrl.setAndMask(badMask)
253 sctrl.setNanSafe(self.config.isNanSafe)
254
255 self.log.debug("Ignoring mask planes: %s", ", ".join(self.config.ignoredPixelMask))
256 if (maskedImage.mask.getArray() & badMask).all():
257 raise pipeBase.TaskError("All pixels masked. Cannot estimate background")
258
259 if algorithm is None:
260 algorithm = self.config.algorithm
261
262
263
264
265
266
267
268 with suppress_deprecations():
270 self.config.undersampleStyle, sctrl,
271 self.config.statisticsProperty)
272
273
274
275
276
277
278
279
280
281
282
283
284
285 if self.config.useApprox:
286 if self.config.approxOrderY not in (self.config.approxOrderX, -1):
287 raise ValueError("Error: approxOrderY not in (approxOrderX, -1)")
288 order = self.config.approxOrderX
289 minNumberGridPoints = order + 1
290 if min(nx, ny) <= order:
291 self.log.warning("Too few points in grid to constrain fit: min(nx, ny) < approxOrder) "
292 "[min(%d, %d) < %d]", nx, ny, order)
293 if self.config.undersampleStyle == "THROW_EXCEPTION":
294 raise ValueError("Too few points in grid (%d, %d) for order (%d) and binSize (%d, %d)" %
295 (nx, ny, order, binSizeX, binSizeY))
296 elif self.config.undersampleStyle == "REDUCE_INTERP_ORDER":
297 if order < 1:
298 raise ValueError("Cannot reduce approxOrder below 0. "
299 "Try using undersampleStyle = \"INCREASE_NXNYSAMPLE\" instead?")
300 order =
min(nx, ny) - 1
301 self.log.warning("Reducing approxOrder to %d", order)
302 elif self.config.undersampleStyle == "INCREASE_NXNYSAMPLE":
303
304 newBinSize =
min(maskedImage.getWidth(), maskedImage.getHeight())//(minNumberGridPoints-1)
305 if newBinSize < 1:
306 raise ValueError("Binsize must be greater than 0")
307 newNx = maskedImage.getWidth()//newBinSize + 1
308 newNy = maskedImage.getHeight()//newBinSize + 1
309 bctrl.setNxSample(newNx)
310 bctrl.setNySample(newNy)
311 self.log.warning("Decreasing binSize from (%d, %d) to %d for a grid of (%d, %d)",
312 binSizeX, binSizeY, newBinSize, newNx, newNy)
313
315 self.config.weighting)
316 bctrl.setApproximateControl(actrl)
317
319 if bg is None:
320 raise RuntimeError("lsst.afw.math.makeBackground failed to fit a background model")
321 return bg
Control how to make an approximation.
Pass parameters to a Background object.
Pass parameters to a Statistics object.
std::shared_ptr< Background > makeBackground(ImageT const &img, BackgroundControl const &bgCtrl)
A convenience function that uses function overloading to make the correct type of Background.