Coverage for tests/test_imageStatistics.py: 16%

125 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-02-05 18:32 -0800

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.log.utils as logUtils 

31import lsst.pex.config as pexConfig 

32import numpy as num 

33 

34verbosity = 0 

35logUtils.traceSetAt("ip.diffim", verbosity) 

36 

37 

38class DiffimTestCases(unittest.TestCase): 

39 

40 def setUp(self): 

41 self.config = ipDiffim.ImagePsfMatchTask.ConfigClass() 

42 self.subconfig = self.config.kernel["DF"] 

43 self.ps = pexConfig.makePropertySet(self.subconfig) 

44 

45 def tearDown(self): 

46 del self.ps 

47 

48 def testImageStatisticsNan(self, core=3): 

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

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

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

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

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

54 

55 # inverse variance weight of 0 is NaN 

56 imstat = ipDiffim.ImageStatisticsF(self.ps) 

57 imstat.apply(mi) 

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

59 

60 imstat = ipDiffim.ImageStatisticsF(self.ps) 

61 imstat.apply(mi, core) 

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

63 

64 def testImageStatisticsZero(self): 

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

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

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

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

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

70 

71 imstat = ipDiffim.ImageStatisticsF(self.ps) 

72 imstat.apply(mi) 

73 

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

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

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

77 

78 def testImageStatisticsOne(self): 

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

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

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

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

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

84 

85 imstat = ipDiffim.ImageStatisticsF(self.ps) 

86 imstat.apply(mi) 

87 

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

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

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

91 

92 def testImageStatisticsCore(self, core=3): 

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

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

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

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

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

98 

99 imstat = ipDiffim.ImageStatisticsF(self.ps) 

100 imstat.apply(mi, core) 

101 

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

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

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

105 

106 def testImageStatisticsGeneral(self): 

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

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

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

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

111 val = i + 2.3 * j 

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

113 numArray[j][i] = val 

114 

115 imstat = ipDiffim.ImageStatisticsF(self.ps) 

116 imstat.apply(mi) 

117 

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

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

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

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

122 

123 afwStat = afwMath.makeStatistics(mi.getImage(), afwMath.MEAN | afwMath.STDEV) 

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

125 # even though these do 

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

127 

128 def testImageStatisticsMask1(self): 

129 # Mask value that gets ignored 

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

131 maskVal = afwImage.Mask.getPlaneBitMask(maskPlane) 

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

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

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

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

136 val = i + 2.3 * j 

137 

138 if i == 19: 

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

140 else: 

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

142 numArray[j][i] = val 

143 

144 imstat = ipDiffim.ImageStatisticsF(self.ps) 

145 imstat.apply(mi) 

146 

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

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

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

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

151 

152 def testImageStatisticsMask2(self): 

153 # Mask value that does not get ignored 

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

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

156 if maskPlane not in maskPlanes: 

157 maskVal = afwImage.Mask.getPlaneBitMask(maskPlane) 

158 break 

159 self.assertGreater(maskVal, 0) 

160 

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

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

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

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

165 val = i + 2.3 * j 

166 

167 if i == 19: 

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

169 numArray[j][i] = val 

170 else: 

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

172 numArray[j][i] = val 

173 

174 imstat = ipDiffim.ImageStatisticsF(self.ps) 

175 imstat.apply(mi) 

176 

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

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

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

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

181 

182 

183##### 

184 

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

186 pass 

187 

188 

189def setup_module(module): 

190 lsst.utils.tests.init() 

191 

192 

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

194 lsst.utils.tests.init() 

195 unittest.main()