Skip to content

Commit

Permalink
Rework exception tests
Browse files Browse the repository at this point in the history
  • Loading branch information
parejkoj committed Nov 21, 2018
1 parent 8e17a9a commit ea4ffd6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
17 changes: 16 additions & 1 deletion python/lsst/pipe/tasks/makeCoaddTempExp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import numpy

import lsst.pex.config as pexConfig
import lsst.daf.persistence as dafPersist
import lsst.afw.image as afwImage
import lsst.coadd.utils as coaddUtils
import lsst.pipe.base as pipeBase
Expand All @@ -34,6 +35,14 @@
__all__ = ["MakeCoaddTempExpTask"]


class MissingExposure(Exception):
"""Raised when data cannot be retrieved for an exposure.
When processing patches, sometimes one exposure is missing; this lets us
distinguish bewteen that case, and other errors.
"""
pass


class MakeCoaddTempExpConfig(CoaddBaseTask.ConfigClass):
"""Config for MakeCoaddTempExpTask
"""
Expand Down Expand Up @@ -438,12 +447,18 @@ def getCalibratedExposure(self, dataRef, bgSubtracted):
calexp's background background model and add it to the calexp.
@return calibrated exposure
@raises MissingExposureError If data for the exposure is not available.
If config.doApplyUberCal, the exposure will be photometrically
calibrated via the `jointcal_photoCalib` dataset and have its SkyWcs
updated to the `jointcal_wcs`, otherwise it will be calibrated via the
Exposure's own Calib and have the original SkyWcs.
"""
exposure = dataRef.get("calexp", immediate=True)
try:
exposure = dataRef.get("calexp", immediate=True)
except dafPersist.NoResults as e:
raise MissingExposure('"calexp" not found: %s ' % str(e)) from e

if not bgSubtracted:
background = dataRef.get("calexpBackground", immediate=True)
mi = exposure.getMaskedImage()
Expand Down
63 changes: 27 additions & 36 deletions tests/test_makeCoaddTempExp.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import lsst.utils.tests

import lsst.afw.image
import lsst.daf.persistence
from lsst.pipe.tasks.makeCoaddTempExp import MakeCoaddTempExpTask, MakeCoaddTempExpConfig
from lsst.daf.persistence import NoResults, ButlerDataRef
from lsst.pipe.tasks.makeCoaddTempExp import (MakeCoaddTempExpTask,
MakeCoaddTempExpConfig,
MissingExposure)


class MakeCoaddTempExpTestCase(lsst.utils.tests.TestCase):
class GetCalibratedExposureTestCase(lsst.utils.tests.TestCase):
def setUp(self):
np.random.seed(10)

Expand Down Expand Up @@ -61,30 +63,38 @@ def setUp(self):
self.exposure.getCalib().setFluxMag0(1/meanCalibration, calibrationErr/meanCalibration**2)
self.exposure.setWcs(self.skyWcs)

# set to False in a test to return self.jointcalPhotoCalib for get('jointcal_PhotoCalib')
self.raiseOnGetPhotoCalib = True
# set to False in a test to return self.jointcalWcs for get('jointcal_Wcs')
self.raiseOnGetWcs = True
# set to True in a test to raise NoResults for get('calexp')
self.raiseOnGetCalexp = False

def mockGet(datasetType, dataId=None, immediate=None):
"""Minimally fake a butler.get()."""
if "calexp" in datasetType:
return self.exposure.clone()
if "jointcal_photoCalib" in datasetType:
if self.raiseOnGetPhotoCalib:
raise lsst.daf.persistence.NoResults("failed!", datasetType, dataId)
if self.raiseOnGetCalexp:
raise NoResults("failed!", datasetType, dataId)
else:
return self.jointcalPhotoCalib
return self.exposure.clone()
if "jointcal_photoCalib" in datasetType:
return self.jointcalPhotoCalib
if "jointcal_wcs" in datasetType:
if self.raiseOnGetWcs:
raise lsst.daf.persistence.NoResults("failed!", datasetType, dataId)
else:
return self.jointcalSkyWcs
return self.jointcalSkyWcs

self.dataRef = unittest.mock.Mock(spec=lsst.daf.persistence.ButlerDataRef)
self.dataRef = unittest.mock.Mock(spec=ButlerDataRef)
self.dataRef.get.side_effect = mockGet
self.dataRef.dataId = {"ccd": 10000, "visit": 1}

def test_getCalibratedExposureGetCalexpRaises(self):
"""If get('calexp') raises NoResults, we should get a usefully
chained exception.
"""
task = MakeCoaddTempExpTask(config=self.config)

self.raiseOnGetCalexp = True

with self.assertRaises(MissingExposure) as cm:
task.getCalibratedExposure(self.dataRef, True)
self.assertIsInstance(cm.exception.__cause__, NoResults)
self.assertIn('"calexp" not found', str(cm.exception))

def test_getCalibratedExposure(self):
task = MakeCoaddTempExpTask(config=self.config)

Expand All @@ -97,29 +107,10 @@ def test_getCalibratedExposure(self):
self.assertEqual(result.getCalib().getFluxMag0()[0], 1/self.exposurePhotoCalib.getCalibrationMean())
self.assertEqual(result.getWcs(), self.skyWcs)

def test_getCalibratedExposureNoJointcalPhotoCalib(self):
self.config.doApplyUberCal = True
task = MakeCoaddTempExpTask(config=self.config)

with(self.assertRaises(lsst.daf.persistence.NoResults)):
task.getCalibratedExposure(self.dataRef, True)

def test_getCalibratedExposureNoJointcalWcs(self):
self.config.doApplyUberCal = True
task = MakeCoaddTempExpTask(config=self.config)

self.raiseOnGetPhotoCalib = False

with(self.assertRaises(lsst.daf.persistence.NoResults)):
task.getCalibratedExposure(self.dataRef, True)

def test_getCalibratedExposureJointcal(self):
self.config.doApplyUberCal = True
task = MakeCoaddTempExpTask(config=self.config)

self.raiseOnGetPhotoCalib = False
self.raiseOnGetWcs = False

expect = self.jointcalPhotoCalib.calibrateImage(self.exposure.maskedImage)
expect /= self.jointcalPhotoCalib.getCalibrationMean()
result = task.getCalibratedExposure(self.dataRef, True)
Expand Down

0 comments on commit ea4ffd6

Please sign in to comment.