Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

# This file is part of pipe_tasks. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

import unittest 

import unittest.mock 

 

import numpy as np 

 

import lsst.utils.tests 

 

import lsst.afw.image 

from lsst.daf.persistence import NoResults, ButlerDataRef 

from lsst.pipe.tasks.makeCoaddTempExp import (MakeCoaddTempExpTask, 

MakeCoaddTempExpConfig, 

MissingExposureError) 

 

 

class GetCalibratedExposureTestCase(lsst.utils.tests.TestCase): 

def setUp(self): 

np.random.seed(10) 

 

self.config = MakeCoaddTempExpConfig() 

 

meanCalibration = 1e-4 

calibrationErr = 1e-5 

self.exposurePhotoCalib = lsst.afw.image.PhotoCalib(meanCalibration, calibrationErr) 

# An external photoCalib calibration to return 

self.externalPhotoCalib = lsst.afw.image.PhotoCalib(1e-6, 1e-8) 

 

crpix = lsst.geom.Point2D(0, 0) 

crval = lsst.geom.SpherePoint(0, 45, lsst.geom.degrees) 

cdMatrix = lsst.afw.geom.makeCdMatrix(scale=1.0*lsst.geom.arcseconds) 

self.skyWcs = lsst.afw.geom.makeSkyWcs(crpix, crval, cdMatrix) 

externalCdMatrix = lsst.afw.geom.makeCdMatrix(scale=0.9*lsst.geom.arcseconds) 

# An external skyWcs to return 

self.externalSkyWcs = lsst.afw.geom.makeSkyWcs(crpix, crval, externalCdMatrix) 

 

self.exposure = lsst.afw.image.ExposureF(10, 10) 

self.exposure.maskedImage.image.array = np.random.random((10, 10)).astype(np.float32) * 1000 

self.exposure.maskedImage.variance.array = np.random.random((10, 10)).astype(np.float32) 

# mask at least one pixel 

self.exposure.maskedImage.mask[5, 5] = 3 

# set the PhotoCalib and Wcs objects of this exposure. 

self.exposure.setPhotoCalib(lsst.afw.image.PhotoCalib(meanCalibration, calibrationErr)) 

self.exposure.setWcs(self.skyWcs) 

 

# 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: 

if self.raiseOnGetCalexp: 

raise NoResults("failed!", datasetType, dataId) 

else: 

return self.exposure.clone() 

if "jointcal_photoCalib" in datasetType: 

return self.externalPhotoCalib 

if "fgcm_photoCalib" in datasetType: 

return self.externalPhotoCalib 

if "fgcm_tract_photoCalib" in datasetType: 

return self.externalPhotoCalib 

if "jointcal_wcs" in datasetType: 

return self.externalSkyWcs 

 

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(MissingExposureError) as cm: 

task.getCalibratedExposure(self.dataRef, True) 

self.assertIsInstance(cm.exception.__cause__, NoResults) 

self.assertIn('Exposure not found', str(cm.exception)) 

 

def test_getCalibratedExposure(self): 

"""Test that getCalibratedExposure returns expected Calib and WCS 

""" 

self._checkCalibratedExposure(doApplyExternalPhotoCalib=False, doApplyExternalSkyWcs=False, 

includeCalibVar=True, 

externalPhotoCalibName='jointcal', 

externalSkyWcsName='jointcal') 

self._checkCalibratedExposure(doApplyExternalPhotoCalib=False, doApplyExternalSkyWcs=False, 

includeCalibVar=False, 

externalPhotoCalibName='jointcal', 

externalSkyWcsName='jointcal') 

self._checkCalibratedExposure(doApplyExternalPhotoCalib=True, doApplyExternalSkyWcs=True, 

includeCalibVar=True, 

externalPhotoCalibName='jointcal', 

externalSkyWcsName='jointcal') 

self._checkCalibratedExposure(doApplyExternalPhotoCalib=True, doApplyExternalSkyWcs=True, 

includeCalibVar=False, 

externalPhotoCalibName='jointcal', 

externalSkyWcsName='jointcal') 

self._checkCalibratedExposure(doApplyExternalPhotoCalib=True, doApplyExternalSkyWcs=True, 

includeCalibVar=True, 

externalPhotoCalibName='fgcm', 

externalSkyWcsName='jointcal') 

self._checkCalibratedExposure(doApplyExternalPhotoCalib=True, doApplyExternalSkyWcs=True, 

includeCalibVar=True, 

externalPhotoCalibName='fgcm_tract', 

externalSkyWcsName='jointcal') 

 

def _checkCalibratedExposure(self, doApplyExternalPhotoCalib, doApplyExternalSkyWcs, 

includeCalibVar, 

externalPhotoCalibName, externalSkyWcsName): 

self.config.doApplyExternalPhotoCalib = doApplyExternalPhotoCalib 

self.config.doApplyExternalSkyWcs = doApplyExternalSkyWcs 

self.config.externalPhotoCalibName = externalPhotoCalibName 

self.config.externalSkyWcsName = externalSkyWcsName 

self.config.includeCalibVar = includeCalibVar 

task = MakeCoaddTempExpTask(config=self.config) 

 

photoCalib = self.externalPhotoCalib if doApplyExternalPhotoCalib else self.exposurePhotoCalib 

expect = photoCalib.calibrateImage(self.exposure.maskedImage, includeCalibVar) 

expect /= photoCalib.getCalibrationMean() 

result = task.getCalibratedExposure(self.dataRef, True) 

self.assertMaskedImagesEqual(result.maskedImage, expect) 

# TODO: once RFC-545 is implemented, this should be 1.0 

self.assertEqual(result.getPhotoCalib(), photoCalib) 

 

targetWcs = self.externalSkyWcs if doApplyExternalSkyWcs else self.skyWcs 

self.assertEqual(result.getWcs(), targetWcs) 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class MatchMemoryTestCase(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

159 ↛ 160line 159 didn't jump to line 160, because the condition on line 159 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()