|
| 1 | +# This file is part of pipe_tasks. |
| 2 | +# |
| 3 | +# Developed for the LSST Data Management System. |
| 4 | +# This product includes software developed by the LSST Project |
| 5 | +# (https://www.lsst.org). |
| 6 | +# See the COPYRIGHT file at the top-level directory of this distribution |
| 7 | +# for details of code ownership. |
| 8 | +# |
| 9 | +# This program is free software: you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU General Public License as published by |
| 11 | +# the Free Software Foundation, either version 3 of the License, or |
| 12 | +# (at your option) any later version. |
| 13 | +# |
| 14 | +# This program is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU General Public License |
| 20 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +import unittest |
| 23 | +import unittest.mock |
| 24 | + |
| 25 | +import numpy as np |
| 26 | + |
| 27 | +import lsst.utils.tests |
| 28 | + |
| 29 | +import lsst.afw.image |
| 30 | +from lsst.daf.persistence import NoResults, ButlerDataRef |
| 31 | +from lsst.pipe.tasks.makeCoaddTempExp import (MakeCoaddTempExpTask, |
| 32 | + MakeCoaddTempExpConfig, |
| 33 | + MissingExposureError) |
| 34 | + |
| 35 | + |
| 36 | +class GetCalibratedExposureTestCase(lsst.utils.tests.TestCase): |
| 37 | + def setUp(self): |
| 38 | + np.random.seed(10) |
| 39 | + |
| 40 | + self.config = MakeCoaddTempExpConfig() |
| 41 | + |
| 42 | + # TODO DM-10156: once Calib is replaced, this will be much cleaner |
| 43 | + meanCalibration = 1e-4 |
| 44 | + calibrationErr = 1e-5 |
| 45 | + self.exposurePhotoCalib = lsst.afw.image.PhotoCalib(meanCalibration, calibrationErr) |
| 46 | + # a "jointcal_photoCalib" calibration to return |
| 47 | + self.jointcalPhotoCalib = lsst.afw.image.PhotoCalib(1e-6, 1e-8) |
| 48 | + |
| 49 | + crpix = lsst.geom.Point2D(0, 0) |
| 50 | + crval = lsst.geom.SpherePoint(0, 45, lsst.geom.degrees) |
| 51 | + cdMatrix = lsst.afw.geom.makeCdMatrix(scale=1.0*lsst.geom.arcseconds) |
| 52 | + self.skyWcs = lsst.afw.geom.makeSkyWcs(crpix, crval, cdMatrix) |
| 53 | + jointcalCdMatrix = lsst.afw.geom.makeCdMatrix(scale=0.9*lsst.geom.arcseconds) |
| 54 | + # a "jointcal_wcs" skyWcs to return |
| 55 | + self.jointcalSkyWcs = lsst.afw.geom.makeSkyWcs(crpix, crval, jointcalCdMatrix) |
| 56 | + |
| 57 | + self.exposure = lsst.afw.image.ExposureF(10, 10) |
| 58 | + self.exposure.maskedImage.image.array = np.random.random((10, 10)).astype(np.float32) * 1000 |
| 59 | + self.exposure.maskedImage.variance.array = np.random.random((10, 10)).astype(np.float32) |
| 60 | + # mask at least one pixel |
| 61 | + self.exposure.maskedImage.mask[5, 5] = 3 |
| 62 | + # set the Calib and Wcs objects of this exposure. |
| 63 | + self.exposure.getCalib().setFluxMag0(1/meanCalibration, calibrationErr/meanCalibration**2) |
| 64 | + self.exposure.setWcs(self.skyWcs) |
| 65 | + |
| 66 | + # set to True in a test to raise NoResults for get('calexp') |
| 67 | + self.raiseOnGetCalexp = False |
| 68 | + |
| 69 | + def mockGet(datasetType, dataId=None, immediate=None): |
| 70 | + """Minimally fake a butler.get().""" |
| 71 | + if "calexp" in datasetType: |
| 72 | + if self.raiseOnGetCalexp: |
| 73 | + raise NoResults("failed!", datasetType, dataId) |
| 74 | + else: |
| 75 | + return self.exposure.clone() |
| 76 | + if "jointcal_photoCalib" in datasetType: |
| 77 | + return self.jointcalPhotoCalib |
| 78 | + if "jointcal_wcs" in datasetType: |
| 79 | + return self.jointcalSkyWcs |
| 80 | + |
| 81 | + self.dataRef = unittest.mock.Mock(spec=ButlerDataRef) |
| 82 | + self.dataRef.get.side_effect = mockGet |
| 83 | + self.dataRef.dataId = {"ccd": 10000, "visit": 1} |
| 84 | + |
| 85 | + def test_getCalibratedExposureGetCalexpRaises(self): |
| 86 | + """If get('calexp') raises NoResults, we should get a usefully |
| 87 | + chained exception. |
| 88 | + """ |
| 89 | + task = MakeCoaddTempExpTask(config=self.config) |
| 90 | + |
| 91 | + self.raiseOnGetCalexp = True |
| 92 | + |
| 93 | + with self.assertRaises(MissingExposureError) as cm: |
| 94 | + task.getCalibratedExposure(self.dataRef, True) |
| 95 | + self.assertIsInstance(cm.exception.__cause__, NoResults) |
| 96 | + self.assertIn('Exposure not found', str(cm.exception)) |
| 97 | + |
| 98 | + def test_getCalibratedExposure(self): |
| 99 | + task = MakeCoaddTempExpTask(config=self.config) |
| 100 | + |
| 101 | + expect = self.exposurePhotoCalib.calibrateImage(self.exposure.maskedImage) |
| 102 | + expect /= self.exposurePhotoCalib.getCalibrationMean() |
| 103 | + result = task.getCalibratedExposure(self.dataRef, True) |
| 104 | + |
| 105 | + self.assertMaskedImagesEqual(result.maskedImage, expect) |
| 106 | + # TODO: once RFC-545 is implemented, this should be 1.0 |
| 107 | + self.assertEqual(result.getCalib().getFluxMag0()[0], 1/self.exposurePhotoCalib.getCalibrationMean()) |
| 108 | + self.assertEqual(result.getWcs(), self.skyWcs) |
| 109 | + |
| 110 | + def test_getCalibratedExposureJointcal(self): |
| 111 | + self.config.doApplyUberCal = True |
| 112 | + task = MakeCoaddTempExpTask(config=self.config) |
| 113 | + |
| 114 | + expect = self.jointcalPhotoCalib.calibrateImage(self.exposure.maskedImage) |
| 115 | + expect /= self.jointcalPhotoCalib.getCalibrationMean() |
| 116 | + result = task.getCalibratedExposure(self.dataRef, True) |
| 117 | + self.assertMaskedImagesEqual(result.maskedImage, expect) |
| 118 | + # TODO: once RFC-545 is implemented, this should be 1.0 |
| 119 | + self.assertEqual(result.getCalib().getFluxMag0()[0], 1/self.jointcalPhotoCalib.getCalibrationMean()) |
| 120 | + self.assertEqual(result.getWcs(), self.jointcalSkyWcs) |
| 121 | + |
| 122 | + |
| 123 | +def setup_module(module): |
| 124 | + lsst.utils.tests.init() |
| 125 | + |
| 126 | + |
| 127 | +class MatchMemoryTestCase(lsst.utils.tests.MemoryTestCase): |
| 128 | + pass |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + lsst.utils.tests.init() |
| 133 | + unittest.main() |
0 commit comments