Skip to content

Commit

Permalink
Change RuntimeError to AlgorithmError
Browse files Browse the repository at this point in the history
  • Loading branch information
cmsaunders committed Aug 13, 2024
1 parent a44b09e commit 1f213ed
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
58 changes: 48 additions & 10 deletions python/lsst/meas/algorithms/normalizedCalibrationFlux.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

__all__ = ["NormalizedCalibrationFluxConfig", "NormalizedCalibrationFluxTask"]
__all__ = ["NormalizedCalibrationFluxConfig", "NormalizedCalibrationFluxTask",
"NormalizedCalibrationFluxError"]

import numpy as np

Expand All @@ -31,6 +32,35 @@
from .sourceSelector import sourceSelectorRegistry


class NormalizedCalibrationFluxError(lsst.pipe.base.AlgorithmError):
"""Raised if Aperture Correction fails in a non-recoverable way.
Parameters
----------
initNSources : `int`
Number of sources selected by the fallback source selector.
nCalibFluxFlag : `int`
Number of selected sources with raw calibration flux flag unset.
nRefFluxFlag : `int`
Number of selected sources with reference flux flag unset.
"""
def __init__(self, *, initNSources, nCalibFluxFlag, nRefFluxFlag):
msg = "There are no valid stars to compute normalized calibration fluxes."
msg += (f" Of {initNSources} initially selected sources, {nCalibFluxFlag} have good raw calibration "
f"fluxes and {nRefFluxFlag} have good reference fluxes.")
super().__init__(msg)
self.initNSources = initNSources
self.nCalibFluxFlag = nCalibFluxFlag
self.nRefFluxFlag = nRefFluxFlag

@property
def metadata(self):
metadata = {"nInitSources": self.initNSources,
"nCalibFluxFlag": self.nCalibFluxFlag,
"nRefFluxFlag": self.nRefFluxFlag}
return metadata


class NormalizedCalibrationFluxConfig(lsst.pex.config.Config):
"""Configuration parameters for NormalizedCalibrationFluxTask.
"""
Expand Down Expand Up @@ -99,6 +129,11 @@ class NormalizedCalibrationFluxTask(lsst.pipe.base.Task):
Schema for the input table; will be modified in place.
**kwargs : `dict`
Additional kwargs to pass to lsst.pipe.base.Task.__init__()
Raises
------
NormalizedCalibrationFluxError if there are not enough sources to
calculate normalization.
"""
ConfigClass = NormalizedCalibrationFluxConfig
_DefaultName = "normalizedCalibrationFlux"
Expand Down Expand Up @@ -265,18 +300,21 @@ def _measure_aperture_correction(self, exposure, catalog):
).apCorrMap

ap_corr_field = ap_corr_map.get(raw_name + "_instFlux")
except MeasureApCorrError:
self.log.warning("Failed to measure full aperture correction for %s", raw_name)
except MeasureApCorrError as e:
self.log.warning("Failed to measure full aperture correction for %s with the following error %s",
raw_name, e)

sel = self.fallback_source_selector.run(catalog, exposure=exposure).selected
sel &= (~catalog[self.config.raw_calibflux_name + "_flag"]
& ~catalog[self.config.measure_ap_corr.refFluxName + "_flag"])
initSel = self.fallback_source_selector.run(catalog, exposure=exposure).selected
sel = (initSel & ~catalog[self.config.raw_calibflux_name + "_flag"]
& ~catalog[self.config.measure_ap_corr.refFluxName + "_flag"])

n_sel = sel.sum()

if n_sel == 0:
if (n_sel := sel.sum()) == 0:
# This is a fatal error.
raise RuntimeError("There are no valid stars to compute normalized calibration fluxes.")
raise NormalizedCalibrationFluxError(
initNSources=initSel.sum(),
nCalibFluxFlag=(initSel & ~catalog[self.config.raw_calibflux_name + "_flag"]).sum(),
nRefFluxFlag=(initSel & ~catalog[self.config.measure_ap_corr.refFluxName + "_flag"]).sum()
)
self.log.info("Measuring normalized flux correction with %d stars from fallback selector.",
n_sel)

Expand Down
16 changes: 15 additions & 1 deletion tests/test_normalizedCalibrationFlux.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import lsst.afw.image
import lsst.afw.table
import lsst.utils.tests
from lsst.meas.algorithms import NormalizedCalibrationFluxTask
from lsst.meas.algorithms import NormalizedCalibrationFluxTask, NormalizedCalibrationFluxError


class NormalizedCalibrationFluxTestCase(lsst.utils.tests.TestCase):
Expand Down Expand Up @@ -260,6 +260,20 @@ def testNormalizedCalibrationFluxApplyOnlyFail(self):
warnings = '\n'.join(cm.output)
self.assertIn("aperture correction map is missing base_CompensatedTophatFlux_12_instFlux", warnings)

def testNormalizedCalibrationFluxError(self):

np.random.seed(12345)
norm_task = self._make_task()
catalog = self._make_catalog(norm_task.schema)
catalog[norm_task.config.raw_calibflux_name + "_flag"] = True
nStars = len(catalog)

error_string = (f"There are no valid stars to compute normalized calibration fluxes. Of {nStars} "
"initially selected sources, 0 have good raw calibration fluxes and {nStars} have "
"good reference fluxes.")
with self.assertRaises(NormalizedCalibrationFluxError, msg=error_string):
norm_task.run(catalog=catalog, exposure=self.exposure)


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

0 comments on commit 1f213ed

Please sign in to comment.