Coverage for tests/test_imageStatistics.py: 16%

124 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-04 03:09 -0700

1# 

2# LSST Data Management System 

3# Copyright 2008-2016 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 unittest 

23 

24 

25import lsst.utils.tests 

26import lsst.afw.image as afwImage 

27import lsst.afw.math as afwMath 

28import lsst.geom as geom 

29import lsst.ip.diffim as ipDiffim 

30import lsst.utils.logging as logUtils 

31import lsst.pex.config as pexConfig 

32import numpy as num 

33 

34verbosity = 0 

35logUtils.trace_set_at("lsst.ip.diffim", verbosity) 

36 

37 

38class DiffimTestCases(unittest.TestCase): 

39 

40 def setUp(self): 

41 self.config = ipDiffim.PsfMatchConfigDF() 

42 self.ps = pexConfig.makePropertySet(self.config) 

43 

44 def tearDown(self): 

45 del self.ps 

46 

47 def testImageStatisticsNan(self, core=3): 

48 numArray = num.zeros((20, 20)) 

49 mi = afwImage.MaskedImageF(geom.Extent2I(20, 20)) 

50 for j in range(mi.getHeight()): 

51 for i in range(mi.getWidth()): 

52 mi[i, j, afwImage.LOCAL] = (numArray[j][i], 0x0, 0) 

53 

54 # inverse variance weight of 0 is NaN 

55 imstat = ipDiffim.ImageStatisticsF(self.ps) 

56 imstat.apply(mi) 

57 self.assertEqual(imstat.getNpix(), 0) 

58 

59 imstat = ipDiffim.ImageStatisticsF(self.ps) 

60 imstat.apply(mi, core) 

61 self.assertEqual(imstat.getNpix(), 0) 

62 

63 def testImageStatisticsZero(self): 

64 numArray = num.zeros((20, 20)) 

65 mi = afwImage.MaskedImageF(geom.Extent2I(20, 20)) 

66 for j in range(mi.getHeight()): 

67 for i in range(mi.getWidth()): 

68 mi[i, j, afwImage.LOCAL] = (numArray[j][i], 0x0, 1) 

69 

70 imstat = ipDiffim.ImageStatisticsF(self.ps) 

71 imstat.apply(mi) 

72 

73 self.assertEqual(imstat.getMean(), 0) 

74 self.assertEqual(imstat.getRms(), 0) 

75 self.assertEqual(imstat.getNpix(), 20*20) 

76 

77 def testImageStatisticsOne(self): 

78 numArray = num.ones((20, 20)) 

79 mi = afwImage.MaskedImageF(geom.Extent2I(20, 20)) 

80 for j in range(mi.getHeight()): 

81 for i in range(mi.getWidth()): 

82 mi[i, j, afwImage.LOCAL] = (numArray[j][i], 0x0, 1) 

83 

84 imstat = ipDiffim.ImageStatisticsF(self.ps) 

85 imstat.apply(mi) 

86 

87 self.assertEqual(imstat.getMean(), 1) 

88 self.assertEqual(imstat.getRms(), 0) 

89 self.assertEqual(imstat.getNpix(), 20*20) 

90 

91 def testImageStatisticsCore(self, core=3): 

92 numArray = num.ones((20, 20)) 

93 mi = afwImage.MaskedImageF(geom.Extent2I(20, 20)) 

94 for j in range(mi.getHeight()): 

95 for i in range(mi.getWidth()): 

96 mi[i, j, afwImage.LOCAL] = (numArray[j][i], 0x0, 1) 

97 

98 imstat = ipDiffim.ImageStatisticsF(self.ps) 

99 imstat.apply(mi, core) 

100 

101 self.assertEqual(imstat.getMean(), 1) 

102 self.assertEqual(imstat.getRms(), 0) 

103 self.assertEqual(imstat.getNpix(), (2*core+1)**2) 

104 

105 def testImageStatisticsGeneral(self): 

106 numArray = num.ones((20, 20)) 

107 mi = afwImage.MaskedImageF(geom.Extent2I(20, 20)) 

108 for j in range(mi.getHeight()): 

109 for i in range(mi.getWidth()): 

110 val = i + 2.3 * j 

111 mi[i, j, afwImage.LOCAL] = (val, 0x0, 1) 

112 numArray[j][i] = val 

113 

114 imstat = ipDiffim.ImageStatisticsF(self.ps) 

115 imstat.apply(mi) 

116 

117 self.assertAlmostEqual(imstat.getMean(), numArray.mean()) 

118 # note that these don't agree exactly... 

119 self.assertAlmostEqual(imstat.getRms(), numArray.std(), 1) 

120 self.assertEqual(imstat.getNpix(), 20 * 20) 

121 

122 afwStat = afwMath.makeStatistics(mi.image, afwMath.MEAN | afwMath.STDEV) 

123 self.assertAlmostEqual(imstat.getMean(), afwStat.getValue(afwMath.MEAN)) 

124 # even though these do 

125 self.assertAlmostEqual(imstat.getRms(), afwStat.getValue(afwMath.STDEV)) 

126 

127 def testImageStatisticsMask1(self): 

128 # Mask value that gets ignored 

129 maskPlane = self.ps.getArray("badMaskPlanes")[0] 

130 maskVal = afwImage.Mask.getPlaneBitMask(maskPlane) 

131 numArray = num.ones((20, 19)) 

132 mi = afwImage.MaskedImageF(geom.Extent2I(20, 20)) 

133 for j in range(mi.getHeight()): 

134 for i in range(mi.getWidth()): 

135 val = i + 2.3 * j 

136 

137 if i == 19: 

138 mi[i, j, afwImage.LOCAL] = (val, maskVal, 1) 

139 else: 

140 mi[i, j, afwImage.LOCAL] = (val, 0x0, 1) 

141 numArray[j][i] = val 

142 

143 imstat = ipDiffim.ImageStatisticsF(self.ps) 

144 imstat.apply(mi) 

145 

146 self.assertAlmostEqual(imstat.getMean(), numArray.mean()) 

147 # note that these don't agree exactly... 

148 self.assertAlmostEqual(imstat.getRms(), numArray.std(), 1) 

149 self.assertEqual(imstat.getNpix(), 20 * (20 - 1)) 

150 

151 def testImageStatisticsMask2(self): 

152 # Mask value that does not get ignored 

153 maskPlanes = self.ps.getArray("badMaskPlanes") 

154 for maskPlane in ("BAD", "EDGE", "CR", "SAT", "INTRP"): 

155 if maskPlane not in maskPlanes: 

156 maskVal = afwImage.Mask.getPlaneBitMask(maskPlane) 

157 break 

158 self.assertGreater(maskVal, 0) 

159 

160 numArray = num.ones((20, 20)) 

161 mi = afwImage.MaskedImageF(geom.Extent2I(20, 20)) 

162 for j in range(mi.getHeight()): 

163 for i in range(mi.getWidth()): 

164 val = i + 2.3 * j 

165 

166 if i == 19: 

167 mi[i, j, afwImage.LOCAL] = (val, maskVal, 1) 

168 numArray[j][i] = val 

169 else: 

170 mi[i, j, afwImage.LOCAL] = (val, 0x0, 1) 

171 numArray[j][i] = val 

172 

173 imstat = ipDiffim.ImageStatisticsF(self.ps) 

174 imstat.apply(mi) 

175 

176 self.assertAlmostEqual(imstat.getMean(), numArray.mean()) 

177 # note that these don't agree exactly... 

178 self.assertAlmostEqual(imstat.getRms(), numArray.std(), 1) 

179 self.assertEqual(imstat.getNpix(), 20 * 20) 

180 

181 

182##### 

183 

184class TestMemory(lsst.utils.tests.MemoryTestCase): 

185 pass 

186 

187 

188def setup_module(module): 

189 lsst.utils.tests.init() 

190 

191 

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

193 lsst.utils.tests.init() 

194 unittest.main()