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

24import numpy as np 

25 

26import lsst.utils.tests 

27import lsst.geom 

28import lsst.afw.image as afwImage 

29import lsst.ip.isr as ipIsr 

30 

31 

32class IsrTestCases(lsst.utils.tests.TestCase): 

33 

34 def setUp(self): 

35 self.overscanKeyword = "BIASSEC" 

36 

37 def tearDown(self): 

38 del self.overscanKeyword 

39 

40 def checkOverscanCorrectionY(self, **kwargs): 

41 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

42 lsst.geom.Point2I(9, 12)) 

43 maskedImage = afwImage.MaskedImageF(bbox) 

44 maskedImage.set(10, 0x0, 1) 

45 

46 dataBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(10, 10)) 

47 dataImage = afwImage.MaskedImageF(maskedImage, dataBox) 

48 

49 # these should be functionally equivalent 

50 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 10), 

51 lsst.geom.Point2I(9, 12)) 

52 biassec = '[1:10,11:13]' 

53 overscan = afwImage.MaskedImageF(maskedImage, bbox) 

54 overscan.set(2, 0x0, 1) 

55 exposure = afwImage.ExposureF(maskedImage, None) 

56 metadata = exposure.getMetadata() 

57 metadata.setString(self.overscanKeyword, biassec) 

58 

59 ipIsr.overscanCorrection(dataImage, overscan.getImage(), **kwargs) 

60 

61 height = maskedImage.getHeight() 

62 width = maskedImage.getWidth() 

63 for j in range(height): 

64 for i in range(width): 

65 if j >= 10: 

66 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], 0) 

67 else: 

68 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], 8) 

69 

70 def checkOverscanCorrectionX(self, **kwargs): 

71 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

72 lsst.geom.Point2I(12, 9)) 

73 maskedImage = afwImage.MaskedImageF(bbox) 

74 maskedImage.set(10, 0x0, 1) 

75 

76 dataBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(10, 10)) 

77 dataImage = afwImage.MaskedImageF(maskedImage, dataBox) 

78 

79 # these should be functionally equivalent 

80 bbox = lsst.geom.Box2I(lsst.geom.Point2I(10, 0), 

81 lsst.geom.Point2I(12, 9)) 

82 biassec = '[11:13,1:10]' 

83 overscan = afwImage.MaskedImageF(maskedImage, bbox) 

84 overscan.set(2, 0x0, 1) 

85 

86 exposure = afwImage.ExposureF(maskedImage, None) 

87 metadata = exposure.getMetadata() 

88 metadata.setString(self.overscanKeyword, biassec) 

89 

90 ipIsr.overscanCorrection(dataImage, overscan.getImage(), **kwargs) 

91 

92 height = maskedImage.getHeight() 

93 width = maskedImage.getWidth() 

94 for j in range(height): 

95 for i in range(width): 

96 if i >= 10: 

97 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], 0) 

98 else: 

99 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], 8) 

100 

101 def checkOverscanCorrectionSineWave(self, **kwargs): 

102 """vertical sine wave along long direction""" 

103 

104 # Full image: (500,100) 

105 longAxis = 500 

106 shortAxis = 100 

107 overscanWidth = 30 

108 

109 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

110 lsst.geom.Point2I(shortAxis-1, longAxis-1)) 

111 maskedImage = afwImage.MaskedImageF(bbox) 

112 maskedImage.set(50.0, 0x0, 1) 

113 

114 # vertical sine wave along long direction 

115 x = np.linspace(0, 2*3.14159, longAxis) 

116 a, w = 15, 50*3.14159 

117 sineWave = 20 + a*np.sin(w*x) 

118 sineWave = sineWave.astype(int) 

119 

120 fullImage = np.repeat(sineWave, shortAxis).reshape((longAxis, shortAxis)) 

121 maskedImage.image.array += fullImage 

122 

123 # data part of the full image: (500,70) 

124 dataBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(shortAxis-overscanWidth, 

125 longAxis)) 

126 dataImage = afwImage.MaskedImageF(maskedImage, dataBox) 

127 # these should be functionally equivalent 

128 bbox = lsst.geom.Box2I(lsst.geom.Point2I(shortAxis-overscanWidth, 0), 

129 lsst.geom.Point2I(shortAxis-1, longAxis-1)) 

130 biassec = '[1:500,71:100]' 

131 overscan = afwImage.MaskedImageF(maskedImage, bbox) 

132 overscan.image.array -= 50.0 # subtract initial pedestal 

133 

134 exposure = afwImage.ExposureF(maskedImage, None) 

135 metadata = exposure.getMetadata() 

136 metadata.setString(self.overscanKeyword, biassec) 

137 

138 ipIsr.overscanCorrection(dataImage, overscan.getImage(), **kwargs) 

139 

140 height = maskedImage.getHeight() 

141 width = maskedImage.getWidth() 

142 

143 for j in range(height): 

144 for i in range(width): 

145 if i >= 70: 

146 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], 0.0) 

147 else: 

148 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], 50.0) 

149 

150 def test_MedianPerRowOverscanCorrection(self): 

151 self.checkOverscanCorrectionY(fitType="MEDIAN_PER_ROW") 

152 self.checkOverscanCorrectionY(fitType="MEDIAN_PER_ROW") 

153 self.checkOverscanCorrectionSineWave(fitType="MEDIAN_PER_ROW") 

154 

155 def test_MedianOverscanCorrection(self): 

156 self.checkOverscanCorrectionY(fitType="MEDIAN") 

157 self.checkOverscanCorrectionX(fitType="MEDIAN") 

158 

159 def checkPolyOverscanCorrectionX(self, **kwargs): 

160 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

161 lsst.geom.Point2I(12, 9)) 

162 maskedImage = afwImage.MaskedImageF(bbox) 

163 maskedImage.set(10, 0x0, 1) 

164 

165 dataBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(10, 10)) 

166 dataImage = afwImage.MaskedImageF(maskedImage, dataBox) 

167 # these should be functionally equivalent 

168 bbox = lsst.geom.Box2I(lsst.geom.Point2I(10, 0), 

169 lsst.geom.Point2I(12, 9)) 

170 overscan = afwImage.MaskedImageF(maskedImage, bbox) 

171 overscan.set(2, 0x0, 1) 

172 for i in range(bbox.getDimensions()[1]): 

173 for j, off in enumerate([-0.5, 0.0, 0.5]): 

174 overscan.image[j, i, afwImage.LOCAL] = 2+i+off 

175 ipIsr.overscanCorrection(dataImage, overscan.getImage(), **kwargs) 

176 

177 height = maskedImage.getHeight() 

178 width = maskedImage.getWidth() 

179 for j in range(height): 

180 for i in range(width): 

181 if i == 10: 

182 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], -0.5) 

183 elif i == 11: 

184 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], 0) 

185 elif i == 12: 

186 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], 0.5) 

187 else: 

188 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], 10 - 2 - j) 

189 

190 def checkPolyOverscanCorrectionY(self, **kwargs): 

191 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

192 lsst.geom.Point2I(9, 12)) 

193 maskedImage = afwImage.MaskedImageF(bbox) 

194 maskedImage.set(10, 0x0, 1) 

195 

196 dataBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(10, 10)) 

197 dataImage = afwImage.MaskedImageF(maskedImage, dataBox) 

198 

199 # these should be functionally equivalent 

200 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 10), 

201 lsst.geom.Point2I(9, 12)) 

202 overscan = afwImage.MaskedImageF(maskedImage, bbox) 

203 overscan.set(2, 0x0, 1) 

204 for i in range(bbox.getDimensions()[0]): 

205 for j, off in enumerate([-0.5, 0.0, 0.5]): 

206 overscan.image[i, j, afwImage.LOCAL] = 2+i+off 

207 

208 ipIsr.overscanCorrection(dataImage, overscan.getImage(), **kwargs) 

209 

210 height = maskedImage.getHeight() 

211 width = maskedImage.getWidth() 

212 for j in range(height): 

213 for i in range(width): 

214 if j == 10: 

215 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], -0.5) 

216 elif j == 11: 

217 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], 0) 

218 elif j == 12: 

219 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], 0.5) 

220 else: 

221 self.assertEqual(maskedImage.image[i, j, afwImage.LOCAL], 10 - 2 - i) 

222 

223 def testPolyOverscanCorrection(self): 

224 for fitType in ("POLY", "CHEB", "LEG"): 

225 self.checkPolyOverscanCorrectionX(fitType=fitType) 

226 self.checkPolyOverscanCorrectionY(fitType=fitType) 

227 

228 def testSplineOverscanCorrection(self): 

229 for fitType in ("NATURAL_SPLINE", "CUBIC_SPLINE", "AKIMA_SPLINE"): 

230 self.checkPolyOverscanCorrectionX(fitType=fitType, order=5) 

231 self.checkPolyOverscanCorrectionY(fitType=fitType, order=5) 

232 

233 

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

235 pass 

236 

237 

238def setup_module(module): 

239 lsst.utils.tests.init() 

240 

241 

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

243 lsst.utils.tests.init() 

244 unittest.main()