Skip to content

Commit 2aa0488

Browse files
committed
Add final pedetstal backgrond in calibrateImage
1 parent fb78891 commit 2aa0488

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

python/lsst/pipe/tasks/calibrateImage.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,46 @@ def _remeasure_star_background(self, result):
18821882
star_background = self.star_background.run(exposure=result.exposure).background
18831883
result.background.append(star_background[0])
18841884

1885+
# Perform one more round of background subtraction that is just an
1886+
# overall pedestal (order = 0). This is intended to account for
1887+
# any potential gross oversubtraction imposed by the higher-order
1888+
# subtraction.
1889+
# Dilate DETECTED mask a bit more if it's below 50% detected.
1890+
nPixToDilate = 2
1891+
if detected_fraction < 0.5:
1892+
dilatedMask = result.exposure.mask.clone()
1893+
for maskName in detected_mask_planes:
1894+
# Compute the grown detection mask plane using SpanSet
1895+
detectedMaskBit = dilatedMask.getPlaneBitMask(maskName)
1896+
detectedMaskSpanSet = SpanSet.fromMask(dilatedMask, detectedMaskBit)
1897+
detectedMaskSpanSet = detectedMaskSpanSet.dilated(nPixToDilate)
1898+
detectedMaskSpanSet = detectedMaskSpanSet.clippedTo(dilatedMask.getBBox())
1899+
# Clear the detected mask plane
1900+
detectedMask = dilatedMask.getMaskPlane(maskName)
1901+
dilatedMask.clearMaskPlane(detectedMask)
1902+
# Set the mask plane to the dilated one
1903+
detectedMaskSpanSet.setMask(dilatedMask, detectedMaskBit)
1904+
1905+
detected_fraction_dilated = self._compute_mask_fraction(dilatedMask,
1906+
detected_mask_planes,
1907+
bad_mask_planes)
1908+
result.exposure.mask = dilatedMask
1909+
self.log.debug("detected_fraction_orig = %.3f detected_fraction_dilated = %.3f",
1910+
detected_fraction_orig, detected_fraction_dilated)
1911+
1912+
pedestalBackgroundConfig = lsst.meas.algorithms.SubtractBackgroundConfig()
1913+
pedestalBackgroundConfig.statisticsProperty = "MEDIAN"
1914+
pedestalBackgroundConfig.approxOrderX = 0
1915+
pedestalBackgroundConfig.binSize = 64
1916+
pedestalBackgroundTask = lsst.meas.algorithms.SubtractBackgroundTask(config=pedestalBackgroundConfig)
1917+
pedestalBackgroundList = pedestalBackgroundTask.run(
1918+
exposure=result.exposure, background=result.background).background
1919+
# Isolate the final pedestal background to log the computed value
1920+
pedestalBackground = afwMath.BackgroundList()
1921+
pedestalBackground.append(pedestalBackgroundList[1])
1922+
pedestalBackgroundLevel = pedestalBackground.getImage().array[0, 0]
1923+
self.log.warning("Subtracted pedestalBackgroundLevel = %.4f", pedestalBackgroundLevel)
1924+
18851925
# Clear detected mask plane before final round of detection
18861926
mask = result.exposure.mask
18871927
for mp in detected_mask_planes:

tests/test_calibrateImage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def test_run_adaptive_threshold_deteection(self):
261261
subString = "Using adaptive threshold detection "
262262
self.assertTrue(any(subString in s for s in cm.output))
263263

264-
self._check_run(calibrate, result, expect_n_background=1)
264+
self._check_run(calibrate, result, expect_n_background=2)
265265

266266
def test_run_downsample(self):
267267
"""Test that run() runs with downsample.

0 commit comments

Comments
 (0)