Skip to content

Commit 3150b1e

Browse files
committed
Add final pedetstal backgrond in calibrateImage
1 parent b3f698e commit 3150b1e

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

python/lsst/pipe/tasks/calibrateImage.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,49 @@ def _remeasure_star_background(self, result, background_to_photometric_ratio=Non
18911891
).background
18921892
result.background.append(star_background[0])
18931893

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

tests/test_calibrateImage.py

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

263-
self._check_run(calibrate, result, expect_n_background=1)
263+
self._check_run(calibrate, result, expect_n_background=2)
264264

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

0 commit comments

Comments
 (0)