Coverage for tests/test_makeCoaddTempExp.py: 18%
80 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-07 20:35 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-07 20:35 +0000
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/>.
22import unittest
23import unittest.mock
25import numpy as np
27import lsst.utils.tests
29import lsst.afw.image
30from lsst.daf.persistence import NoResults, ButlerDataRef
31from lsst.pipe.tasks.makeCoaddTempExp import (MakeCoaddTempExpTask,
32 MakeCoaddTempExpConfig,
33 MissingExposureError)
36class GetCalibratedExposureTestCase(lsst.utils.tests.TestCase):
37 def setUp(self):
38 np.random.seed(10)
40 self.config = MakeCoaddTempExpConfig()
42 meanCalibration = 1e-4
43 calibrationErr = 1e-5
44 self.exposurePhotoCalib = lsst.afw.image.PhotoCalib(meanCalibration, calibrationErr)
45 # An external photoCalib calibration to return
46 self.externalPhotoCalib = lsst.afw.image.PhotoCalib(1e-6, 1e-8)
48 crpix = lsst.geom.Point2D(0, 0)
49 crval = lsst.geom.SpherePoint(0, 45, lsst.geom.degrees)
50 cdMatrix = lsst.afw.geom.makeCdMatrix(scale=1.0*lsst.geom.arcseconds)
51 self.skyWcs = lsst.afw.geom.makeSkyWcs(crpix, crval, cdMatrix)
52 externalCdMatrix = lsst.afw.geom.makeCdMatrix(scale=0.9*lsst.geom.arcseconds)
53 # An external skyWcs to return
54 self.externalSkyWcs = lsst.afw.geom.makeSkyWcs(crpix, crval, externalCdMatrix)
56 self.exposure = lsst.afw.image.ExposureF(10, 10)
57 self.exposure.maskedImage.image.array = np.random.random((10, 10)).astype(np.float32) * 1000
58 self.exposure.maskedImage.variance.array = np.random.random((10, 10)).astype(np.float32)
59 # mask at least one pixel
60 self.exposure.maskedImage.mask[5, 5] = 3
61 # set the PhotoCalib and Wcs objects of this exposure.
62 self.exposure.setPhotoCalib(lsst.afw.image.PhotoCalib(meanCalibration, calibrationErr))
63 self.exposure.setWcs(self.skyWcs)
65 # set to True in a test to raise NoResults for get('calexp')
66 self.raiseOnGetCalexp = False
68 def mockGet(datasetType, dataId=None, immediate=None):
69 """Minimally fake a butler.get()."""
70 if "calexp" in datasetType:
71 if self.raiseOnGetCalexp:
72 raise NoResults("failed!", datasetType, dataId)
73 else:
74 return self.exposure.clone()
75 if "jointcal_photoCalib" in datasetType:
76 return self.externalPhotoCalib
77 if "fgcm_photoCalib" in datasetType:
78 return self.externalPhotoCalib
79 if "fgcm_tract_photoCalib" in datasetType:
80 return self.externalPhotoCalib
81 if "jointcal_wcs" in datasetType:
82 return self.externalSkyWcs
84 self.dataRef = unittest.mock.Mock(spec=ButlerDataRef)
85 self.dataRef.get.side_effect = mockGet
86 self.dataRef.dataId = {"ccd": 10000, "visit": 1}
88 def test_getCalibratedExposureGetCalexpRaises(self):
89 """If get('calexp') raises NoResults, we should get a usefully
90 chained exception.
91 """
92 task = MakeCoaddTempExpTask(config=self.config)
94 self.raiseOnGetCalexp = True
96 with self.assertRaises(MissingExposureError) as cm:
97 task.getCalibratedExposure(self.dataRef, True)
98 self.assertIsInstance(cm.exception.__cause__, NoResults)
99 self.assertIn('Exposure not found', str(cm.exception))
101 def test_getCalibratedExposure(self):
102 """Test that getCalibratedExposure returns expected Calib and WCS
103 """
104 self._checkCalibratedExposure(doApplyExternalPhotoCalib=False, doApplyExternalSkyWcs=False,
105 includeCalibVar=True,
106 externalPhotoCalibName='jointcal',
107 externalSkyWcsName='jointcal')
108 self._checkCalibratedExposure(doApplyExternalPhotoCalib=False, doApplyExternalSkyWcs=False,
109 includeCalibVar=False,
110 externalPhotoCalibName='jointcal',
111 externalSkyWcsName='jointcal')
112 self._checkCalibratedExposure(doApplyExternalPhotoCalib=True, doApplyExternalSkyWcs=True,
113 includeCalibVar=True,
114 externalPhotoCalibName='jointcal',
115 externalSkyWcsName='jointcal')
116 self._checkCalibratedExposure(doApplyExternalPhotoCalib=True, doApplyExternalSkyWcs=True,
117 includeCalibVar=False,
118 externalPhotoCalibName='jointcal',
119 externalSkyWcsName='jointcal')
120 self._checkCalibratedExposure(doApplyExternalPhotoCalib=True, doApplyExternalSkyWcs=True,
121 includeCalibVar=True,
122 externalPhotoCalibName='fgcm',
123 externalSkyWcsName='jointcal')
124 self._checkCalibratedExposure(doApplyExternalPhotoCalib=True, doApplyExternalSkyWcs=True,
125 includeCalibVar=True,
126 externalPhotoCalibName='fgcm_tract',
127 externalSkyWcsName='jointcal')
129 def _checkCalibratedExposure(self, doApplyExternalPhotoCalib, doApplyExternalSkyWcs,
130 includeCalibVar,
131 externalPhotoCalibName, externalSkyWcsName):
132 self.config.doApplyExternalPhotoCalib = doApplyExternalPhotoCalib
133 self.config.doApplyExternalSkyWcs = doApplyExternalSkyWcs
134 self.config.externalPhotoCalibName = externalPhotoCalibName
135 self.config.externalSkyWcsName = externalSkyWcsName
136 self.config.includeCalibVar = includeCalibVar
137 task = MakeCoaddTempExpTask(config=self.config)
139 photoCalib = self.externalPhotoCalib if doApplyExternalPhotoCalib else self.exposurePhotoCalib
140 expect = photoCalib.calibrateImage(self.exposure.maskedImage, includeCalibVar)
141 expect /= photoCalib.getCalibrationMean()
142 result = task.getCalibratedExposure(self.dataRef, True)
143 self.assertMaskedImagesEqual(result.maskedImage, expect)
144 # TODO: once RFC-545 is implemented, this should be 1.0
145 self.assertEqual(result.getPhotoCalib(), photoCalib)
147 targetWcs = self.externalSkyWcs if doApplyExternalSkyWcs else self.skyWcs
148 self.assertEqual(result.getWcs(), targetWcs)
151def setup_module(module):
152 lsst.utils.tests.init()
155class MatchMemoryTestCase(lsst.utils.tests.MemoryTestCase):
156 pass
159if __name__ == "__main__": 159 ↛ 160line 159 didn't jump to line 160, because the condition on line 159 was never true
160 lsst.utils.tests.init()
161 unittest.main()