Skip to content

Commit 7492aa6

Browse files
committed
Rework exception handling and add tests.
We can continue processing if individual exposures are missing, but we should raise other exceptions up the chain.
1 parent be3b003 commit 7492aa6

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

python/lsst/pipe/tasks/makeCoaddTempExp.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -363,19 +363,16 @@ def run(self, calexpRefList, skyInfo, visitId=0):
363363
for calExpInd, calExpRef in enumerate(calexpRefList):
364364
self.log.info("Processing calexp %d of %d for this Warp: id=%s",
365365
calExpInd+1, len(calexpRefList), calExpRef.dataId)
366-
try:
367-
ccdId = calExpRef.get("ccdExposureId", immediate=True)
368-
except Exception:
369-
ccdId = calExpInd
366+
ccdId = calExpRef.get("ccdExposureId", immediate=True)
370367
try:
371368
# We augment the dataRef here with the tract, which is harmless for loading things
372-
# like calexps that don't need the tract, and necessary for meas_mosaic outputs,
369+
# like calexps that don't need the tract, and necessary for jointcal outputs,
373370
# which do.
374371
calExpRef = calExpRef.butlerSubset.butler.dataRef("calexp", dataId=calExpRef.dataId,
375372
tract=skyInfo.tractInfo.getId())
376373
calExp = self.getCalibratedExposure(calExpRef, bgSubtracted=self.config.bgSubtracted)
377-
except Exception as e:
378-
self.log.warn("Calexp %s not found; skipping it: %s", calExpRef.dataId, e)
374+
except MissingExposureError as e:
375+
self.log.warn("Skipping missing data: %s", e)
379376
continue
380377

381378
if self.config.doApplySkyCorr:

tests/test_makeCoaddTempExp.py

+58
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535

3636
class GetCalibratedExposureTestCase(lsst.utils.tests.TestCase):
37+
"""Tests of MakeCoaddTempExpTask.getCalibratedExposure()"""
3738
def setUp(self):
3839
np.random.seed(10)
3940

@@ -120,6 +121,63 @@ def test_getCalibratedExposureJointcal(self):
120121
self.assertEqual(result.getWcs(), self.jointcalSkyWcs)
121122

122123

124+
class MakeCoaddTempExpRunTestCase(lsst.utils.tests.TestCase):
125+
"""Tests of MakeCoaddTempExpTask.run()."""
126+
def setUp(self):
127+
self.config = MakeCoaddTempExpConfig()
128+
self.task = MakeCoaddTempExpTask(self.config)
129+
dataId = "visit=mock"
130+
fakeDataRef = unittest.mock.NonCallableMock(lsst.daf.persistence.ButlerDataRef,
131+
dataId=dataId,
132+
butlerSubset=unittest.mock.Mock())
133+
self.calexpRefList = [fakeDataRef]
134+
135+
bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(100, 100))
136+
self.skyInfo = unittest.mock.Mock(bbox=bbox)
137+
138+
self.fakeImage = lsst.afw.image.ExposureF(bbox)
139+
self.fakeImage.getMaskedImage().set(np.nan, lsst.afw.image.Mask.getPlaneBitMask("NO_DATA"), np.inf)
140+
141+
target = "lsst.pipe.tasks.makeCoaddTempExp.MakeCoaddTempExpTask._prepareEmptyExposure"
142+
preparePatch = unittest.mock.patch(target, return_value=self.fakeImage)
143+
preparePatch.start()
144+
self.addCleanup(preparePatch.stop)
145+
146+
def testGetCalibratedExposureRaisesRuntimeError(self):
147+
"""If getCalibratedExposure() raises anything other than
148+
MissingExposureError, it should be passed up the chain.
149+
150+
Previously, all Exceptions were caught, logged at `warn` level,
151+
and then dropped.
152+
"""
153+
mockErr = "Mock Error!"
154+
target = "lsst.pipe.tasks.makeCoaddTempExp.MakeCoaddTempExpTask.getCalibratedExposure"
155+
patch = unittest.mock.patch(target,
156+
new_callable=unittest.mock.Mock,
157+
side_effect=RuntimeError(mockErr))
158+
patch.start()
159+
self.addCleanup(patch.stop)
160+
with self.assertRaises(RuntimeError) as cm:
161+
self.task.run(self.calexpRefList, self.skyInfo)
162+
self.assertIn(mockErr, str(cm.exception))
163+
164+
def testGetCalibratedExposureRaisesMissingExposureError(self):
165+
"""If getCalibratedExposure() raises MissingExposureError,
166+
processing should continue uninterrupted.
167+
In this case, that means no data is returned, because there is only
168+
one dataRef available (`self.fakeImage`).
169+
"""
170+
mockErr = "No data files exist."
171+
target = "lsst.pipe.tasks.makeCoaddTempExp.MakeCoaddTempExpTask.getCalibratedExposure"
172+
patch = unittest.mock.patch(target,
173+
new_callable=unittest.mock.Mock,
174+
side_effect=MissingExposureError(mockErr))
175+
patch.start()
176+
self.addCleanup(patch.stop)
177+
result = self.task.run(self.calexpRefList, self.skyInfo)
178+
self.assertEqual(result.exposures, {"direct": None})
179+
180+
123181
def setup_module(module):
124182
lsst.utils.tests.init()
125183

0 commit comments

Comments
 (0)