Skip to content

Commit 90aefe8

Browse files
authored
Merge pull request #432 from lsst/tickets/DM-50980
DM-50980: improvements to SubtractBackgroundTask to support tract-level background subtraction
2 parents 8a4a211 + 8d924bf commit 90aefe8

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

python/lsst/meas/algorithms/subtractBackground.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
__all__ = ("SubtractBackgroundConfig", "SubtractBackgroundTask", "backgroundFlatContext")
2424

2525
import itertools
26+
import math
2627

2728
from contextlib import contextmanager
2829
import numpy
@@ -88,6 +89,8 @@ class TooManyMaskedPixelsError(pipeBase.AlgorithmError):
8889
"""Raised when all pixels in the image are masked and no background
8990
can be estimated.
9091
"""
92+
93+
@property
9194
def metadata(self) -> dict:
9295
"""There is no metadata associated with this error.
9396
"""
@@ -146,7 +149,6 @@ class SubtractBackgroundConfig(pexConfig.Config):
146149
ignoredPixelMask = pexConfig.ListField(
147150
doc="Names of mask planes to ignore while estimating the background",
148151
dtype=str, default=["BAD", "EDGE", "DETECTED", "DETECTED_NEGATIVE", "NO_DATA", ],
149-
itemCheck=lambda x: x in afwImage.Mask().getMaskPlaneDict().keys(),
150152
)
151153
isNanSafe = pexConfig.Field(
152154
doc="Ignore NaNs when estimating the background",
@@ -196,6 +198,11 @@ class SubtractBackgroundTask(pipeBase.Task):
196198
ConfigClass = SubtractBackgroundConfig
197199
_DefaultName = "subtractBackground"
198200

201+
def __init__(self, config=None, *, name=None, parentTask=None, log=None):
202+
super().__init__(config, name=name, parentTask=parentTask, log=log)
203+
self.binSizeX = self.config.binSize if self.config.binSizeX == 0 else self.config.binSizeX
204+
self.binSizeY = self.config.binSize if self.config.binSizeY == 0 else self.config.binSizeY
205+
199206
def run(self, exposure, background=None, stats=True, statsKeys=None, backgroundToPhotometricRatio=None):
200207
"""Fit and subtract the background of an exposure.
201208
@@ -315,13 +322,10 @@ def fitBackground(self, maskedImage, nx=0, ny=0, algorithm=None):
315322
of failure.
316323
"""
317324

318-
binSizeX = self.config.binSize if self.config.binSizeX == 0 else self.config.binSizeX
319-
binSizeY = self.config.binSize if self.config.binSizeY == 0 else self.config.binSizeY
320-
321325
if not nx:
322-
nx = maskedImage.getWidth()//binSizeX + 1
326+
nx = math.ceil(maskedImage.getWidth() / self.binSizeX)
323327
if not ny:
324-
ny = maskedImage.getHeight()//binSizeY + 1
328+
ny = math.ceil(maskedImage.getHeight() / self.binSizeY)
325329

326330
unsubFrame = getDebugFrame(self._display, "unsubtracted")
327331
if unsubFrame:
@@ -380,7 +384,7 @@ def fitBackground(self, maskedImage, nx=0, ny=0, algorithm=None):
380384
"[min(%d, %d) < %d]", nx, ny, order)
381385
if self.config.undersampleStyle == "THROW_EXCEPTION":
382386
raise ValueError("Too few points in grid (%d, %d) for order (%d) and binSize (%d, %d)" %
383-
(nx, ny, order, binSizeX, binSizeY))
387+
(nx, ny, order, self.binSizeX, self.binSizeY))
384388
elif self.config.undersampleStyle == "REDUCE_INTERP_ORDER":
385389
if order < 1:
386390
raise ValueError("Cannot reduce approxOrder below 0. "
@@ -397,7 +401,7 @@ def fitBackground(self, maskedImage, nx=0, ny=0, algorithm=None):
397401
bctrl.setNxSample(newNx)
398402
bctrl.setNySample(newNy)
399403
self.log.warning("Decreasing binSize from (%d, %d) to %d for a grid of (%d, %d)",
400-
binSizeX, binSizeY, newBinSize, newNx, newNy)
404+
self.binSizeX, self.binSizeY, newBinSize, newNx, newNy)
401405

402406
actrl = afwMath.ApproximateControl(afwMath.ApproximateControl.CHEBYSHEV, order, order,
403407
self.config.weighting)

0 commit comments

Comments
 (0)