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

# 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() 

 

# TODO DM-10156: once Calib is replaced, this will be much cleaner 

meanCalibration = 1e-4 

calibrationErr = 1e-5 

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

# a "jointcal_photoCalib" calibration to return 

self.jointcalPhotoCalib = 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) 

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

# a "jointcal_wcs" skyWcs to return 

self.jointcalSkyWcs = lsst.afw.geom.makeSkyWcs(crpix, crval, jointcalCdMatrix) 

 

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 Calib and Wcs objects of this exposure. 

self.exposure.getCalib().setFluxMag0(1/meanCalibration, calibrationErr/meanCalibration**2) 

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.jointcalPhotoCalib 

if "jointcal_wcs" in datasetType: 

return self.jointcalSkyWcs 

 

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(doApplyUberCal=False, includeCalibVar=True) 

self._checkCalibratedExposure(doApplyUberCal=False, includeCalibVar=False) 

self._checkCalibratedExposure(doApplyUberCal=True, includeCalibVar=True) 

self._checkCalibratedExposure(doApplyUberCal=True, includeCalibVar=False) 

 

def _checkCalibratedExposure(self, doApplyUberCal, includeCalibVar): 

self.config.doApplyUberCal = doApplyUberCal 

self.config.includeCalibVar = includeCalibVar 

task = MakeCoaddTempExpTask(config=self.config) 

 

photoCalib = self.jointcalPhotoCalib if doApplyUberCal 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.getCalib().getFluxMag0()[0], 1/photoCalib.getCalibrationMean()) 

 

targetWcs = self.jointcalSkyWcs if doApplyUberCal else self.skyWcs 

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

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

pass 

 

 

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

lsst.utils.tests.init() 

unittest.main()