Coverage for tests/test_flatAndIlluminationCorrection.py: 25%

94 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-05 03:23 -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 

36from lsst.ip.isr import PhotonTransferCurveDataset 

37 

38 

39class IsrTestCases(unittest.TestCase): 

40 

41 def setUp(self): 

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

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

44 self.flatScaleKeyword = "IMMODE" 

45 self.filenameKeyword = "filename" 

46 

47 def tearDown(self): 

48 del self.pmin 

49 del self.pmax 

50 del self.flatScaleKeyword 

51 del self.filenameKeyword 

52 

53 def doFlat(self, scaling): 

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

55 maskedImage.getImage().set(10) 

56 

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

58 flat.getImage().set(1) 

59 flatexposure = afwImage.ExposureF(flat, None) 

60 dmetadata = flatexposure.getMetadata() 

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

62 

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

64 

65 height = maskedImage.getHeight() 

66 width = maskedImage.getWidth() 

67 for j in range(height): 

68 for i in range(width): 

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

70 

71 def testFlat1(self): 

72 self.doFlat(scaling=10) 

73 

74 def testFlat2(self): 

75 self.doFlat(scaling=0.1) 

76 

77 def testFlat3(self): 

78 self.doFlat(scaling=3.7) 

79 

80 def doIllum(self, scaling): 

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

82 maskedImage.getImage().set(10) 

83 

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

85 illum.getImage().set(1) 

86 illumexposure = afwImage.ExposureF(illum, None) 

87 dmetadata = illumexposure.getMetadata() 

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

89 

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

91 

92 height = maskedImage.getHeight() 

93 width = maskedImage.getWidth() 

94 for j in range(height): 

95 for i in range(width): 

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

97 

98 def testIllum1(self): 

99 self.doIllum(scaling=10) 

100 

101 def testIllum2(self): 

102 self.doIllum(scaling=0.1) 

103 

104 def testIllum3(self): 

105 self.doIllum(scaling=3.7) 

106 

107 def testGainAndReadnoise(self): 

108 

109 isrTask = IsrTask() 

110 

111 detector = DetectorWrapper().detector 

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

113 

114 level = 10 

115 readNoise = 1.5 

116 raw.image.set(level) 

117 

118 amp = detector[0] 

119 ampName = amp.getName() 

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

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

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

123 testAmp = Amplifier.Builder() 

124 testAmp.assign(amp) 

125 testAmp.setReadNoise(readNoise) 

126 testAmp.setGain(gain) 

127 testAmp.finish() 

128 

129 # Effective PTC will have the gain and the noise 

130 effectivePtc = PhotonTransferCurveDataset([ampName], "TEST_PTC", 1) 

131 effectivePtc.gain[ampName] = gain 

132 effectivePtc.noise[ampName] = readNoise 

133 effectivePtc.validateGainNoiseTurnoffValues(ampName) 

134 isrTask.updateVariance(raw, testAmp, effectivePtc) 

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

136 gain = 1 

137 if math.isnan(gain): 

138 gain = 1 

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

140 

141 

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

143 pass 

144 

145 

146def setup_module(module): 

147 lsst.utils.tests.init() 

148 

149 

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

151 lsst.utils.tests.init() 

152 unittest.main()