Coverage for tests/test_centroid.py: 21%

93 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-04 02:37 -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 

33 

34class CentroidTestCase(utilsTests.TestCase): 

35 """A test case for centroiding. 

36 """ 

37 

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

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

40 """ 

41 schema = afwTable.SourceTable.makeMinimalSchema() 

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

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

44 table = afwTable.SourceCatalog(schema) 

45 

46 x0, y0 = 12345, 54321 

47 for imageFactory in (afwImage.MaskedImageF,): 

48 

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

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

51 

52 # This fixed DoubleGaussianPsf replaces a computer generated one. 

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

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

55 exp = afwImage.makeExposure(im) 

56 exp.setPsf(psf) 

57 

58 im.set(bkgd) 

59 x, y = 30, 20 

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

61 

62 source = table.addNew() 

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

64 foot = afwDetection.Footprint(spans) 

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

66 source.setFootprint(foot) 

67 centroider.measure(source, exp) 

68 

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

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

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

72 

73 im.set(bkgd) 

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

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

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

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

78 

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

80 source = table.addNew() 

81 source.set('test_x', x) 

82 source.set('test_y', y) 

83 centroider.measure(source, exp) 

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

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

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

87 

88 def testSdssMeasureCentroid(self): 

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

90 control = measBase.SdssCentroidControl() 

91 control.doFootprintCheck = False 

92 # Test fails with the default of 1, and even 5 

93 control.maxDistToPeak = -1.0 

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

95 

96 

97class SingleFrameMeasurementTaskTestCase(utilsTests.TestCase): 

98 """A test case for the SingleFrameMeasurementTask. 

99 """ 

100 

101 def mySetup(self): 

102 msConfig = measBase.SingleFrameMeasurementConfig() 

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

104 msConfig.slots.centroid = "base_SdssCentroid" 

105 msConfig.slots.shape = None 

106 msConfig.slots.psfShape = None 

107 msConfig.slots.apFlux = None 

108 msConfig.slots.modelFlux = None 

109 msConfig.slots.psfFlux = None 

110 msConfig.slots.gaussianFlux = None 

111 msConfig.slots.calibFlux = None 

112 schema = afwTable.SourceTable.makeMinimalSchema() 

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

114 measCat = afwTable.SourceCatalog(schema) 

115 

116 source = measCat.addNew() 

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

118 fp = afwDetection.Footprint(spans) 

119 fp.addPeak(50, 50, 1000.0) 

120 source.setFootprint(fp) 

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

122 task.run(measCat, self.exp) 

123 return source 

124 

125 def setUp(self): 

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

127 """ 

128 

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

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

131 self.exp.setPsf(psf) 

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

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

134 

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

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

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

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

139 

140 def testCentroider(self): 

141 """Measure the centroid. 

142 """ 

143 s = self.mySetup() 

144 

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

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

147 

148 

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

150 pass 

151 

152 

153def setup_module(module): 

154 lsst.utils.tests.init() 

155 

156 

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

158 lsst.utils.tests.init() 

159 unittest.main()