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# LSST Data Management System 

3# Copyright 2018 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# 

22import numpy as np 

23import unittest 

24 

25import lsst.utils.tests 

26 

27from lsst.daf.base import PropertyList 

28import lsst.afw.cameraGeom as cameraGeom 

29from lsst.geom import Point2I, Extent2I, Box2I, Extent2D 

30from lsst.afw.image import ExposureF, VisitInfo 

31 

32from lsst.ip.isr.isrTask import IsrTask 

33 

34 

35def makeAmplifier(name, bbox, rawImageBox, overscanBox, gain, readNoise, saturation): 

36 amp = cameraGeom.Amplifier.Builder() 

37 amp.setName(name) 

38 amp.setBBox(bbox) 

39 amp.setRawDataBBox(rawImageBox) 

40 amp.setRawHorizontalOverscanBBox(overscanBox) 

41 amp.setGain(gain) 

42 amp.setReadNoise(readNoise) 

43 amp.setSaturation(saturation) 

44 amp.setSuspectLevel(np.nan) 

45 return amp 

46 

47 

48class EmpiricalVarianceTestCast(lsst.utils.tests.TestCase): 

49 def setUp(self): 

50 """Constructs a CCD with two amplifiers and prepares for ISR""" 

51 np.random.seed(12345) 

52 baseValue = 100.0 

53 gain = 1.0 

54 readNoise = 123456789.0 

55 saturation = 987654321.0 

56 height = 234 

57 imageSize = Extent2I(123, height) 

58 overscanSize = Extent2I(16, height) 

59 self.sigma = 1.234 

60 

61 # Set up the various regions 

62 overscan1 = Box2I(Point2I(0, 0), overscanSize) 

63 image1 = Box2I(Point2I(overscanSize[0], 0), imageSize) 

64 image2 = Box2I(Point2I(overscanSize[0] + imageSize[0], 0), imageSize) 

65 overscan2 = Box2I(Point2I(overscanSize[0] + 2*imageSize[0], 0), overscanSize) 

66 

67 leftBox = Box2I(overscan1.getMin(), Extent2I(overscan1.getWidth() + image1.getWidth(), height)) 

68 rightBox = Box2I(image2.getMin(), Extent2I(image2.getWidth() + overscan2.getWidth(), height)) 

69 

70 target1 = Box2I(Point2I(0, 0), imageSize) 

71 target2 = Box2I(Point2I(image1.getWidth(), 0), imageSize) 

72 

73 # Set the pixels 

74 exposure = ExposureF(Box2I(Point2I(0, 0), Extent2I(imageSize[0]*2 + overscanSize[0]*2, height))) 

75 yy = np.arange(0, height, 1, dtype=np.float32) 

76 leftImage = ExposureF(exposure, leftBox) 

77 leftImage.image.array[:] = baseValue + yy[:, np.newaxis] 

78 rightImage = ExposureF(exposure, rightBox) 

79 rightImage.image.array[:] = baseValue - yy[:, np.newaxis] 

80 

81 leftOverscan = ExposureF(exposure, overscan1) 

82 leftOverscan.image.array += np.random.normal(0.0, self.sigma, leftOverscan.image.array.shape) 

83 rightOverscan = ExposureF(exposure, overscan2) 

84 rightOverscan.image.array += np.random.normal(0.0, self.sigma, leftOverscan.image.array.shape) 

85 exposure.mask.array[:] = 0.0 

86 exposure.variance.array[:] = np.nan 

87 

88 # Construct the detectors 

89 amp1 = makeAmplifier("left", target1, image1, overscan1, gain, readNoise, saturation) 

90 amp2 = makeAmplifier("right", target2, image2, overscan2, gain, readNoise, saturation) 

91 ccdBox = Box2I(Point2I(0, 0), Extent2I(image1.getWidth() + image2.getWidth(), height)) 

92 camBuilder = cameraGeom.Camera.Builder("fakeCam") 

93 detBuilder = camBuilder.add("detector", 1) 

94 detBuilder.setSerial("det1") 

95 detBuilder.setBBox(ccdBox) 

96 detBuilder.setPixelSize(Extent2D(1.0, 1.0)) 

97 detBuilder.setOrientation(cameraGeom.Orientation()) 

98 detBuilder.append(amp1) 

99 detBuilder.append(amp2) 

100 cam = camBuilder.finish() 

101 exposure.setDetector(cam.get('detector')) 

102 

103 header = PropertyList() 

104 header.add("EXPTIME", 0.0) 

105 exposure.getInfo().setVisitInfo(VisitInfo(header)) 

106 

107 self.exposure = exposure 

108 self.config = IsrTask.ConfigClass() 

109 

110 # Disable everything we don't care about 

111 self.config.doBias = False 

112 self.config.doDark = False 

113 self.config.doFlat = False 

114 self.config.doFringe = False 

115 self.config.doDefect = False 

116 self.config.doWrite = False 

117 self.config.expectWcs = False 

118 self.config.doLinearize = False 

119 self.config.doCrosstalk = False 

120 self.config.doBrighterFatter = False 

121 self.config.doAttachTransmissionCurve = False 

122 self.config.doAssembleCcd = False 

123 self.config.doNanMasking = False 

124 self.config.doInterpolate = False 

125 

126 # Set the things that match our test setup 

127 self.config.overscan.fitType = "CHEB" 

128 self.config.overscan.order = 1 

129 self.config.doEmpiricalReadNoise = True 

130 

131 self.task = IsrTask(config=self.config) 

132 

133 def tearDown(self): 

134 del self.exposure 

135 

136 def testEmpiricalVariance(self): 

137 results = self.task.run(self.exposure) 

138 postIsr = results.exposure 

139 

140 self.assertFloatsEqual(postIsr.mask.array, 0) 

141 # Image is not exactly zero because the noise in the overscan (required to be able to set 

142 # the empirical variance) leads to a slight misestimate in the polynomial fit. 

143 self.assertFloatsAlmostEqual(np.median(postIsr.image.array), 0.0, atol=5.0e-2) 

144 self.assertFloatsAlmostEqual(np.nanmedian(postIsr.variance.array), self.sigma**2, rtol=5.0e-2) 

145 

146 

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

148 pass 

149 

150 

151def setup_module(module): 

152 lsst.utils.tests.init() 

153 

154 

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

156 import sys 

157 setup_module(sys.modules[__name__]) 

158 unittest.main()