Skip to content

Commit

Permalink
Expand error handling tests to cover more failure modes.
Browse files Browse the repository at this point in the history
  • Loading branch information
erykoff committed May 11, 2024
1 parent 8118582 commit a938eec
Showing 1 changed file with 72 additions and 15 deletions.
87 changes: 72 additions & 15 deletions tests/test_gaap.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,43 +264,100 @@ def testFail(self, scalingFactors=[100.], sigmas=[500.]):
exposure, catalog = self.dataset.realize(0.0, sfmTask.schema)
self.recordPsfShape(catalog)

# Expected error messages in the logs when running `sfmTask`.
# Expected debug messages in the logs when running `sfmTask`.
errorMessage = [("Failed to solve for PSF matching kernel in GAaP for (100.000000, 670.000000): "
"Problematic scaling factors = 100.0 "
"Errors: RuntimeError('Unable to determine kernel sum; 0 candidates')"),
("MeasurementError in ext_gaap_GaapFlux.measure on record 1: "
"Failed to solve for PSF matching kernel"),
("Failed to solve for PSF matching kernel in GAaP for (100.000000, 870.000000): "
"Problematic scaling factors = 100.0 "
"Errors: RuntimeError('Unable to determine kernel sum; 0 candidates')"),
("MeasurementError in ext_gaap_GaapFlux.measure on record 2: "
"Failed to solve for PSF matching kernel"),
("Failed to solve for PSF matching kernel in GAaP for (-10.000000, -20.000000): "
"Problematic scaling factors = 100.0 "
"Errors: RuntimeError('Unable to determine kernel sum; 0 candidates')")]
"Errors: RuntimeError('Unable to determine kernel sum; 0 candidates')"),
("MeasurementError in ext_gaap_GaapFlux.measure on record 3: "
"Failed to solve for PSF matching kernel")]

testCatalog = catalog.copy(deep=True)
plugin_logger_name = sfmTask.log.getChild(algName).name
self.assertEqual(plugin_logger_name, "lsst.measurement.ext_gaap_GaapFlux")
with self.assertLogs(plugin_logger_name, "ERROR") as cm:
sfmTask.run(catalog, exposure)
with self.assertLogs(plugin_logger_name, "DEBUG") as cm:
sfmTask.run(testCatalog, exposure)
self.assertEqual([record.message for record in cm.records], errorMessage)

self._checkAllFlags(
testCatalog,
algName,
scalingFactors,
sigmas,
gaapConfig,
specificFlag="flag_gaussianization",
)

# Trigger a "not (psfSigma > 0) error":
exposureJunkPsf = exposure.clone()
testCatalog = catalog.copy(deep=True)
junkPsf = afwDetection.GaussianPsf(1, 1, 0)
exposureJunkPsf.setPsf(junkPsf)
sfmTask.run(testCatalog, exposureJunkPsf)

self._checkAllFlags(
testCatalog,
algName,
scalingFactors,
sigmas,
gaapConfig,
specificFlag="flag_gaussianization",
)

# Trigger a NoPixelError.
testCatalog = catalog.copy(deep=True)
testCatalog[0].setFootprint(afwDetection.Footprint())
with self.assertLogs(plugin_logger_name, "DEBUG") as cm:
sfmTask.run(testCatalog, exposure)

self.assertEqual(
cm.records[0].message,
"MeasurementError in ext_gaap_GaapFlux.measure on record 1: No good pixels in footprint",
)
self.assertEqual(testCatalog[f"{algName}_flag_no_pixel"][0], True)
self.assertEqual(testCatalog[f"{algName}_flag"][0], True)

self._checkAllFlags(testCatalog[0: 1], algName, scalingFactors, sigmas, gaapConfig, allFailFlag=True)

# Try and "fail" with no PSF.
# Since fatal exceptions are not caught by the measurement framework,
# use a context manager and catch it here.
exposure.setPsf(None)
with self.assertRaises(lsst.meas.base.FatalAlgorithmError):
sfmTask.run(catalog, exposure)

def _checkAllFlags(
self,
catalog,
algName,
scalingFactors,
sigmas,
gaapConfig,
specificFlag=None,
allFailFlag=False
):
for record in catalog:
self.assertFalse(record[algName + "_flag"])
self.assertEqual(record[algName + "_flag"], allFailFlag)
for scalingFactor in scalingFactors:
flagName = gaapConfig._getGaapResultName(scalingFactor, "flag_gaussianization", algName)
self.assertTrue(record[flagName])
if specificFlag is not None:
flagName = gaapConfig._getGaapResultName(scalingFactor, specificFlag, algName)
self.assertTrue(record[flagName])
for sigma in sigmas + ["Optimal"]:
baseName = gaapConfig._getGaapResultName(scalingFactor, sigma, algName)
self.assertTrue(record[baseName + "_flag"])
self.assertFalse(record[baseName + "_flag_bigPsf"])

baseName = gaapConfig._getGaapResultName(scalingFactor, "PsfFlux", algName)
self.assertTrue(record[baseName + "_flag"])

# Try and "fail" with no PSF.
# Since fatal exceptions are not caught by the measurement framework,
# use a context manager and catch it here.
exposure.setPsf(None)
with self.assertRaises(lsst.meas.base.FatalAlgorithmError):
sfmTask.run(catalog, exposure)

def testFlags(self, sigmas=[0.4, 0.5, 0.7], scalingFactors=[1.15, 1.25, 1.4, 100.]):
"""Test that GAaP flags are set properly.
Expand Down

0 comments on commit a938eec

Please sign in to comment.