Coverage for tests/test_statisticsOverloads.py: 24%

91 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-02 02:18 -0700

1# This file is part of afw. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22""" 

23Tests for Statistics 

24 

25Run with: 

26 python test_statisticsOverloads.py 

27or 

28 pytest test_statisticsOverloads.py 

29""" 

30 

31import unittest 

32 

33import lsst.utils.tests 

34import lsst.pex.exceptions 

35import lsst.geom 

36import lsst.afw.image as afwImage 

37import lsst.afw.math as afwMath 

38 

39 

40class StatisticsTestCase(unittest.TestCase): 

41 

42 """A test case to check all overloaded makeStatistics() factories for Statistics""" 

43 

44 def setUp(self): 

45 self.val = 10 

46 self.nRow, self.nCol = 100, 200 

47 self.sctrl = afwMath.StatisticsControl() 

48 

49 # Integers 

50 self.mimgI = afwImage.MaskedImageI( 

51 lsst.geom.Extent2I(self.nRow, self.nCol)) 

52 self.mimgI.set(self.val, 0x0, self.val) 

53 self.imgI = afwImage.ImageI( 

54 lsst.geom.Extent2I(self.nRow, self.nCol), self.val) 

55 # TODO: pybind11, this should probably be ndarray 

56 self.vecI = [self.val for i in range(self.nRow*self.nCol)] 

57 

58 # floats 

59 self.mimgF = afwImage.MaskedImageF( 

60 lsst.geom.Extent2I(self.nRow, self.nCol)) 

61 self.mimgF.set(self.val, 0x0, self.val) 

62 self.imgF = afwImage.ImageF( 

63 lsst.geom.Extent2I(self.nRow, self.nCol), self.val) 

64 # TODO: pybind11, this should probably be ndarray 

65 self.vecF = [float(self.val) for i in range(self.nRow*self.nCol)] 

66 

67 # doubles 

68 self.mimgD = afwImage.MaskedImageD( 

69 lsst.geom.Extent2I(self.nRow, self.nCol)) 

70 self.mimgD.set(self.val, 0x0, self.val) 

71 self.imgD = afwImage.ImageD( 

72 lsst.geom.Extent2I(self.nRow, self.nCol), self.val) 

73 # TODO: pybind11, this should probably be ndarray 

74 self.vecD = [float(self.val) for i in range(self.nRow*self.nCol)] 

75 

76 self.imgList = [self.imgI, self.imgF, self.imgD] 

77 self.mimgList = [self.mimgI, self.mimgF, self.mimgD] 

78 self.vecList = [self.vecI, self.vecF, self.vecD] 

79 

80 def tearDown(self): 

81 del self.mimgI 

82 del self.mimgF 

83 del self.mimgD 

84 del self.imgI 

85 del self.imgF 

86 del self.imgD 

87 del self.vecI 

88 del self.vecF 

89 del self.vecD 

90 

91 del self.mimgList 

92 del self.imgList 

93 del self.vecList 

94 

95 # The guts of the testing: grab a mean, stddev, and sum for whatever 

96 # you're called with 

97 def compareMakeStatistics(self, image, n): 

98 stats = afwMath.makeStatistics(image, afwMath.NPOINT | afwMath.STDEV 

99 | afwMath.MEAN | afwMath.SUM, self.sctrl) 

100 

101 self.assertEqual(stats.getValue(afwMath.NPOINT), n) 

102 self.assertEqual(stats.getValue(afwMath.NPOINT)*stats.getValue(afwMath.MEAN), 

103 stats.getValue(afwMath.SUM)) 

104 self.assertEqual(stats.getValue(afwMath.MEAN), self.val) 

105 self.assertEqual(stats.getValue(afwMath.STDEV), 0) 

106 

107 # same as compareMakeStatistics but calls constructor directly (only for 

108 # masked image) 

109 def compareStatistics(self, stats, n): 

110 self.assertEqual(stats.getValue(afwMath.NPOINT), n) 

111 self.assertEqual(stats.getValue(afwMath.NPOINT)*stats.getValue(afwMath.MEAN), 

112 stats.getValue(afwMath.SUM)) 

113 self.assertEqual(stats.getValue(afwMath.MEAN), self.val) 

114 self.assertEqual(stats.getValue(afwMath.STDEV), 0) 

115 

116 # Test regular image::Image 

117 def testImage(self): 

118 for img in self.imgList: 

119 self.compareMakeStatistics(img, img.getWidth()*img.getHeight()) 

120 

121 # Test the image::MaskedImages 

122 def testMaskedImage(self): 

123 for mimg in self.mimgList: 

124 self.compareMakeStatistics(mimg, mimg.getWidth()*mimg.getHeight()) 

125 

126 # Test the std::vectors 

127 def testVector(self): 

128 for vec in self.vecList: 

129 self.compareMakeStatistics(vec, len(vec)) 

130 

131 def testWeightedVector(self): 

132 """Test std::vector, but with weights""" 

133 sctrl = afwMath.StatisticsControl() 

134 

135 nval = len(self.vecList[0]) 

136 weight = 10 

137 weights = [i*weight/float(nval - 1) for i in range(nval)] 

138 

139 for vec in self.vecList: 

140 stats = afwMath.makeStatistics(vec, weights, 

141 afwMath.NPOINT | afwMath.STDEV | afwMath.MEAN | afwMath.SUM, sctrl) 

142 

143 self.assertAlmostEqual( 

144 0.5*weight*sum(vec)/stats.getValue(afwMath.SUM), 1.0) 

145 self.assertAlmostEqual( 

146 sum(vec)/len(vec), stats.getValue(afwMath.MEAN)) 

147 

148 # Try calling the Statistics constructor directly 

149 def testStatisticsConstructor(self): 

150 if False: 

151 statsI = afwMath.StatisticsI(self.mimgI.getImage(), self.mimgI.getMask(), 

152 afwMath.NPOINT | afwMath.STDEV | afwMath.MEAN | afwMath.SUM, 

153 self.sctrl) 

154 statsF = afwMath.StatisticsF(self.mimgF.getImage(), self.mimgF.getMask(), 

155 afwMath.NPOINT | afwMath.STDEV | afwMath.MEAN | afwMath.SUM, 

156 self.sctrl) 

157 statsD = afwMath.StatisticsD(self.mimgD.getImage(), self.mimgD.getMask(), 

158 afwMath.NPOINT | afwMath.STDEV | afwMath.MEAN | afwMath.SUM, 

159 self.sctrl) 

160 

161 self.compareStatistics( 

162 statsI, self.mimgI.getWidth()*self.mimgI.getHeight()) 

163 self.compareStatistics( 

164 statsF, self.mimgF.getWidth()*self.mimgF.getHeight()) 

165 self.compareStatistics( 

166 statsD, self.mimgD.getWidth()*self.mimgD.getHeight()) 

167 

168 # Test the Mask specialization 

169 def testMask(self): 

170 mask = afwImage.Mask(lsst.geom.Extent2I(10, 10)) 

171 mask.set(0x0) 

172 

173 mask[1, 1, afwImage.LOCAL] = 0x10 

174 mask[3, 1, afwImage.LOCAL] = 0x08 

175 mask[5, 4, afwImage.LOCAL] = 0x08 

176 mask[4, 5, afwImage.LOCAL] = 0x02 

177 

178 stats = afwMath.makeStatistics(mask, afwMath.SUM | afwMath.NPOINT) 

179 self.assertEqual(mask.getWidth()*mask.getHeight(), 

180 stats.getValue(afwMath.NPOINT)) 

181 self.assertEqual(0x1a, stats.getValue(afwMath.SUM)) 

182 

183 def tst(): 

184 afwMath.makeStatistics(mask, afwMath.MEAN) 

185 self.assertRaises(lsst.pex.exceptions.InvalidParameterError, tst) 

186 

187 

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

189 pass 

190 

191 

192def setup_module(module): 

193 lsst.utils.tests.init() 

194 

195 

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

197 lsst.utils.tests.init() 

198 unittest.main()