Skip to content

Commit

Permalink
Merge pull request #382 from lsst/tickets/DM-45002
Browse files Browse the repository at this point in the history
DM-45002: Warn instead of raising with incomplete normalization aperture correction.
  • Loading branch information
erykoff authored Jul 9, 2024
2 parents 49a8992 + 8706eaa commit 7805677
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
28 changes: 20 additions & 8 deletions python/lsst/meas/algorithms/normalizedCalibrationFlux.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,28 @@ def run(self, *, exposure, catalog):
if self.config.do_measure_ap_corr:
ap_corr_field, ap_corr_err_field = self._measure_aperture_correction(exposure, catalog)
else:
use_identity = False
ap_corr_map = exposure.info.getApCorrMap()
if ap_corr_map is None:
raise RuntimeError("NormalizedCalibrationFluxTask is configured with "
"do_measure_ap_corr=False but exposure has no aperture correction map.")
ap_corr_field = ap_corr_map.get(raw_flux_name)
ap_corr_err_field = ap_corr_map.get(raw_fluxerr_name)
if not ap_corr_field or not ap_corr_err_field:
raise RuntimeError("NormalizedCalibrationFlux is configured with "
f"do_measure_ap_corr=False but {raw_flux_name}/{raw_fluxerr_name} "
"not in exposure aperture correction map.")
self.log.warning(
"Exposure does not have a valid normalization map; using identity normalization.",
)
use_identity = True
else:
ap_corr_field = ap_corr_map.get(raw_flux_name)
ap_corr_err_field = ap_corr_map.get(raw_fluxerr_name)
if not ap_corr_field or not ap_corr_err_field:
self.log.warning(
"Exposure aperture correction map is missing %s/%s for normalization; "
"using identity normalization.",
raw_flux_name,
raw_fluxerr_name,
)
use_identity = True

if use_identity:
ap_corr_field = ChebyshevBoundedField(exposure.getBBox(), np.array([[1.0]]))
ap_corr_err_field = ChebyshevBoundedField(exposure.getBBox(), np.array([[0.0]]))

corrections = ap_corr_field.evaluate(
catalog["slot_Centroid_x"],
Expand Down
13 changes: 9 additions & 4 deletions tests/test_normalizedCalibrationFlux.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import unittest

import numpy as np
import logging

import lsst.afw.image
import lsst.afw.table
Expand Down Expand Up @@ -245,15 +246,19 @@ def testNormalizedCalibrationFluxApplyOnlyFail(self):
catalog_run2 = self._make_catalog(norm_task.schema)

# Try without setting an aperture correction map at all.
with self.assertRaisesRegex(RuntimeError, "no aperture correction map"):
norm_task2.run(catalog=catalog_run2, exposure=exposure_run1)
with self.assertLogs(level=logging.WARNING) as cm:
_ = norm_task2.run(catalog=catalog_run2, exposure=exposure_run1)
warnings = '\n'.join(cm.output)
self.assertIn("does not have a valid normalization", warnings)

# Try again after setting an incomplete aperture correction map.
ap_corr_map_blank = lsst.afw.image.ApCorrMap()
exposure_run1.info.setApCorrMap(ap_corr_map_blank)

with self.assertRaisesRegex(RuntimeError, "not in exposure aperture correction map"):
norm_task2.run(catalog=catalog_run2, exposure=exposure_run1)
with self.assertLogs(level=logging.WARNING) as cm:
_ = norm_task2.run(catalog=catalog_run2, exposure=exposure_run1)
warnings = '\n'.join(cm.output)
self.assertIn("aperture correction map is missing base_CompensatedTophatFlux_12_instFlux", warnings)


class TestMemory(lsst.utils.tests.MemoryTestCase):
Expand Down

0 comments on commit 7805677

Please sign in to comment.