Coverage for tests/test_flatAndIlluminationCorrection.py: 26%

88 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-12 03:09 -0700

1# 

2# LSST Data Management System 

3# Copyright 2008, 2009, 2010 LSST Corporation. 

4# 

5# This product includes software developed by the 

6# LSST Project (http://www.lsst.org/). 

7# 

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

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

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

11# (at your option) any later version. 

12# 

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

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

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the LSST License Statement and 

19# the GNU General Public License along with this program. If not, 

20# see <http://www.lsstcorp.org/LegalNotices/>. 

21# 

22 

23import unittest 

24 

25import math 

26import numpy as np 

27 

28import lsst.utils.tests 

29import lsst.geom 

30import lsst.afw.image as afwImage 

31import lsst.ip.isr as ipIsr 

32 

33from lsst.afw.cameraGeom import Amplifier 

34from lsst.afw.cameraGeom.testUtils import DetectorWrapper 

35from lsst.ip.isr import IsrTask 

36 

37 

38class IsrTestCases(unittest.TestCase): 

39 

40 def setUp(self): 

41 self.pmin = lsst.geom.Point2I(1, 1) 

42 self.pmax = lsst.geom.Point2I(10, 10) 

43 self.flatScaleKeyword = "IMMODE" 

44 self.filenameKeyword = "filename" 

45 

46 def tearDown(self): 

47 del self.pmin 

48 del self.pmax 

49 del self.flatScaleKeyword 

50 del self.filenameKeyword 

51 

52 def doFlat(self, scaling): 

53 maskedImage = afwImage.MaskedImageF(lsst.geom.Box2I(self.pmin, self.pmax)) 

54 maskedImage.getImage().set(10) 

55 

56 flat = afwImage.MaskedImageF(lsst.geom.Box2I(self.pmin, self.pmax)) 

57 flat.getImage().set(1) 

58 flatexposure = afwImage.ExposureF(flat, None) 

59 dmetadata = flatexposure.getMetadata() 

60 dmetadata.setString(self.filenameKeyword, 'Unittest Flat') 

61 

62 ipIsr.flatCorrection(maskedImage, flatexposure.getMaskedImage(), 'USER', scaling) 

63 

64 height = maskedImage.getHeight() 

65 width = maskedImage.getWidth() 

66 for j in range(height): 

67 for i in range(width): 

68 self.assertAlmostEqual(maskedImage.image[i, j, afwImage.LOCAL], 10 / (1./scaling), 5) 

69 

70 def testFlat1(self): 

71 self.doFlat(scaling=10) 

72 

73 def testFlat2(self): 

74 self.doFlat(scaling=0.1) 

75 

76 def testFlat3(self): 

77 self.doFlat(scaling=3.7) 

78 

79 def doIllum(self, scaling): 

80 maskedImage = afwImage.MaskedImageF(lsst.geom.Box2I(self.pmin, self.pmax)) 

81 maskedImage.getImage().set(10) 

82 

83 illum = afwImage.MaskedImageF(lsst.geom.Box2I(self.pmin, self.pmax)) 

84 illum.getImage().set(1) 

85 illumexposure = afwImage.ExposureF(illum, None) 

86 dmetadata = illumexposure.getMetadata() 

87 dmetadata.setString(self.filenameKeyword, 'Unittest Illum') 

88 

89 ipIsr.illuminationCorrection(maskedImage, illumexposure.getMaskedImage(), scaling) 

90 

91 height = maskedImage.getHeight() 

92 width = maskedImage.getWidth() 

93 for j in range(height): 

94 for i in range(width): 

95 self.assertAlmostEqual(maskedImage.image[i, j, afwImage.LOCAL], 10 / (1./scaling), 5) 

96 

97 def testIllum1(self): 

98 self.doIllum(scaling=10) 

99 

100 def testIllum2(self): 

101 self.doIllum(scaling=0.1) 

102 

103 def testIllum3(self): 

104 self.doIllum(scaling=3.7) 

105 

106 def testGainAndReadnoise(self): 

107 

108 isrTask = IsrTask() 

109 

110 detector = DetectorWrapper().detector 

111 raw = afwImage.ExposureF(detector.getBBox()) 

112 

113 level = 10 

114 readNoise = 1.5 

115 raw.image.set(level) 

116 

117 amp = detector[0] 

118 

119 for gain in [-1, 0, 0.1, 1, np.NaN]: 

120 # Because amplifiers are immutable, we can't change the gain or 

121 # read noise in-place. Instead, we clone, and update the clone. 

122 testAmp = Amplifier.Builder() 

123 testAmp.assign(amp) 

124 testAmp.setReadNoise(readNoise) 

125 testAmp.setGain(gain) 

126 testAmp.finish() 

127 

128 isrTask.updateVariance(raw, testAmp) 

129 if gain <= 0: # behave the same way as amp.setGain 

130 gain = 1 

131 if math.isnan(gain): 

132 gain = 1 

133 self.assertEqual(raw.variance[0, 0, afwImage.LOCAL], level/gain + readNoise**2) 

134 

135 

136class MemoryTester(lsst.utils.tests.MemoryTestCase): 

137 pass 

138 

139 

140def setup_module(module): 

141 lsst.utils.tests.init() 

142 

143 

144if __name__ == "__main__": 144 ↛ 145line 144 didn't jump to line 145, because the condition on line 144 was never true

145 lsst.utils.tests.init() 

146 unittest.main()