Coverage for tests/test_centroid.py: 26%

112 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-04-07 01:31 -0700

1# This file is part of meas_base. 

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 

22import math 

23import unittest 

24 

25import lsst.geom 

26import lsst.afw.image as afwImage 

27import lsst.afw.geom as afwGeom 

28import lsst.afw.table as afwTable 

29import lsst.meas.base as measBase 

30import lsst.utils.tests as utilsTests 

31import lsst.afw.detection as afwDetection 

32 

33try: 

34 type(verbose) 

35except NameError: 

36 display = False 

37 verbose = 0 

38 

39 

40class CentroidTestCase(utilsTests.TestCase): 

41 """A test case for centroiding. 

42 """ 

43 

44 def setUp(self): 

45 pass 

46 

47 def tearDown(self): 

48 pass 

49 

50 def testCleanup(self): 

51 """Test that tearDown does""" 

52 pass 

53 

54 def do_testAstrometry(self, alg, bkgd, control): 

55 """Test that we can instantiate and play with a centroiding algorithm. 

56 """ 

57 

58 schema = afwTable.SourceTable.makeMinimalSchema() 

59 schema.getAliasMap().set("slot_Centroid", "test") 

60 centroider = alg(control, "test", schema) 

61 table = afwTable.SourceCatalog(schema) 

62 

63 x0, y0 = 12345, 54321 

64 for imageFactory in (afwImage.MaskedImageF,): 

65 

66 im = imageFactory(lsst.geom.ExtentI(100, 100)) 

67 im.setXY0(lsst.geom.Point2I(x0, y0)) 

68 

69 # This fixed DoubleGaussianPsf replaces a computer generated one. 

70 # The values are not anything in particular, just a reasonable size. 

71 psf = afwDetection.GaussianPsf(15, 15, 3.0) 

72 exp = afwImage.makeExposure(im) 

73 exp.setPsf(psf) 

74 

75 im.set(bkgd) 

76 x, y = 30, 20 

77 im.image[x, y, afwImage.LOCAL] = 1010 

78 

79 source = table.addNew() 

80 spans = afwGeom.SpanSet(exp.getBBox(afwImage.LOCAL)) 

81 foot = afwDetection.Footprint(spans) 

82 foot.addPeak(x + x0, y + y0, 1010) 

83 source.setFootprint(foot) 

84 centroider.measure(source, exp) 

85 

86 self.assertFloatsAlmostEqual(x + x0, source.getX(), rtol=.00001) 

87 self.assertFloatsAlmostEqual(y + y0, source.getY(), rtol=.00001) 

88 self.assertFalse(source.get("test_flag")) 

89 

90 im.set(bkgd) 

91 im.image[10, 20, afwImage.LOCAL] = 1010 

92 im.image[10, 21, afwImage.LOCAL] = 1010 

93 im.image[11, 20, afwImage.LOCAL] = 1010 

94 im.image[11, 21, afwImage.LOCAL] = 1010 

95 

96 x, y = 10.5 + x0, 20.5 + y0 

97 source = table.addNew() 

98 source.set('test_x', x) 

99 source.set('test_y', y) 

100 centroider.measure(source, exp) 

101 self.assertFloatsAlmostEqual(x, source.getX(), rtol=.00001) 

102 self.assertFloatsAlmostEqual(y, source.getY(), rtol=.00001) 

103 self.assertFalse(source.get("test_flag")) 

104 

105 def testNaiveMeasureCentroid(self): 

106 """Test that we can instantiate and play with naive centroids. 

107 """ 

108 bkgd = 10.0 

109 afwTable.SourceTable.makeMinimalSchema() 

110 control = measBase.NaiveCentroidControl() 

111 control.background = bkgd 

112 control.doFootprintCheck = False 

113 self.do_testAstrometry(measBase.NaiveCentroidAlgorithm, bkgd, control) 

114 

115 def testSdssMeasureCentroid(self): 

116 """Test that we can instantiate and play with SDSS centroids""" 

117 control = measBase.SdssCentroidControl() 

118 control.doFootprintCheck = False 

119 self.do_testAstrometry(measBase.SdssCentroidAlgorithm, 10.0, control) 

120 

121 

122class SingleFrameMeasurementTaskTestCase(utilsTests.TestCase): 

123 """A test case for the SingleFrameMeasurementTask. 

124 """ 

125 

126 def mySetup(self): 

127 msConfig = measBase.SingleFrameMeasurementConfig() 

128 msConfig.algorithms.names = ["base_SdssCentroid"] 

129 msConfig.slots.centroid = "base_SdssCentroid" 

130 msConfig.slots.shape = None 

131 msConfig.slots.psfShape = None 

132 msConfig.slots.apFlux = None 

133 msConfig.slots.modelFlux = None 

134 msConfig.slots.psfFlux = None 

135 msConfig.slots.gaussianFlux = None 

136 msConfig.slots.calibFlux = None 

137 schema = afwTable.SourceTable.makeMinimalSchema() 

138 task = measBase.SingleFrameMeasurementTask(schema, config=msConfig) 

139 measCat = afwTable.SourceCatalog(schema) 

140 

141 source = measCat.addNew() 

142 spans = afwGeom.SpanSet(self.exp.getBBox(afwImage.LOCAL)) 

143 fp = afwDetection.Footprint(spans) 

144 fp.addPeak(50, 50, 1000.0) 

145 source.setFootprint(fp) 

146 # Then run the default SFM task. Results not checked 

147 task.run(measCat, self.exp) 

148 return source 

149 

150 def setUp(self): 

151 """Make the image we'll measure. 

152 """ 

153 

154 self.exp = afwImage.ExposureF(100, 100) 

155 psf = afwDetection.GaussianPsf(15, 15, 3.0) 

156 self.exp.setPsf(psf) 

157 self.I0, self.xcen, self.ycen = 1000.0, 50.5, 50.0 

158 im = self.exp.getMaskedImage().getImage() 

159 

160 for i in (-1, 0, 1): 

161 for j in (-1, 0, 1): 

162 im[int(self.xcen) + i, int(self.ycen) + j, afwImage.LOCAL] = \ 

163 self.I0*(1 - 0.5*math.hypot(i - 0.5, j)) 

164 

165 def tearDown(self): 

166 del self.exp 

167 

168 def testCentroider(self): 

169 """Measure the centroid. 

170 """ 

171 s = self.mySetup() 

172 

173 # this does not match exactly, and it used to 

174 self.assertFloatsAlmostEqual(s.getCentroid(), lsst.geom.PointD(self.xcen, self.ycen), rtol=.01) 

175 

176 

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

178 pass 

179 

180 

181def setup_module(module): 

182 lsst.utils.tests.init() 

183 

184 

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

186 lsst.utils.tests.init() 

187 unittest.main()