23
23
__all__ = ("SubtractBackgroundConfig" , "SubtractBackgroundTask" , "backgroundFlatContext" )
24
24
25
25
import itertools
26
+ import math
26
27
27
28
from contextlib import contextmanager
28
29
import numpy
@@ -88,6 +89,8 @@ class TooManyMaskedPixelsError(pipeBase.AlgorithmError):
88
89
"""Raised when all pixels in the image are masked and no background
89
90
can be estimated.
90
91
"""
92
+
93
+ @property
91
94
def metadata (self ) -> dict :
92
95
"""There is no metadata associated with this error.
93
96
"""
@@ -146,7 +149,6 @@ class SubtractBackgroundConfig(pexConfig.Config):
146
149
ignoredPixelMask = pexConfig .ListField (
147
150
doc = "Names of mask planes to ignore while estimating the background" ,
148
151
dtype = str , default = ["BAD" , "EDGE" , "DETECTED" , "DETECTED_NEGATIVE" , "NO_DATA" , ],
149
- itemCheck = lambda x : x in afwImage .Mask ().getMaskPlaneDict ().keys (),
150
152
)
151
153
isNanSafe = pexConfig .Field (
152
154
doc = "Ignore NaNs when estimating the background" ,
@@ -196,6 +198,11 @@ class SubtractBackgroundTask(pipeBase.Task):
196
198
ConfigClass = SubtractBackgroundConfig
197
199
_DefaultName = "subtractBackground"
198
200
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
+
199
206
def run (self , exposure , background = None , stats = True , statsKeys = None , backgroundToPhotometricRatio = None ):
200
207
"""Fit and subtract the background of an exposure.
201
208
@@ -315,13 +322,10 @@ def fitBackground(self, maskedImage, nx=0, ny=0, algorithm=None):
315
322
of failure.
316
323
"""
317
324
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
-
321
325
if not nx :
322
- nx = maskedImage .getWidth ()// binSizeX + 1
326
+ nx = math . ceil ( maskedImage .getWidth () / self . binSizeX )
323
327
if not ny :
324
- ny = maskedImage .getHeight ()// binSizeY + 1
328
+ ny = math . ceil ( maskedImage .getHeight () / self . binSizeY )
325
329
326
330
unsubFrame = getDebugFrame (self ._display , "unsubtracted" )
327
331
if unsubFrame :
@@ -380,7 +384,7 @@ def fitBackground(self, maskedImage, nx=0, ny=0, algorithm=None):
380
384
"[min(%d, %d) < %d]" , nx , ny , order )
381
385
if self .config .undersampleStyle == "THROW_EXCEPTION" :
382
386
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 ))
384
388
elif self .config .undersampleStyle == "REDUCE_INTERP_ORDER" :
385
389
if order < 1 :
386
390
raise ValueError ("Cannot reduce approxOrder below 0. "
@@ -397,7 +401,7 @@ def fitBackground(self, maskedImage, nx=0, ny=0, algorithm=None):
397
401
bctrl .setNxSample (newNx )
398
402
bctrl .setNySample (newNy )
399
403
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 )
401
405
402
406
actrl = afwMath .ApproximateControl (afwMath .ApproximateControl .CHEBYSHEV , order , order ,
403
407
self .config .weighting )
0 commit comments