Coverage for tests/test_ImportCentroidAlgorithm.py: 27%

79 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-04-01 02:15 -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 unittest 

23 

24import numpy as np 

25 

26import lsst.geom 

27import lsst.afw.image as afwImage 

28import lsst.afw.table as afwTable 

29import lsst.meas.base 

30import lsst.utils.tests 

31import testLib 

32 

33try: 

34 type(verbose) 

35except NameError: 

36 verbose = 0 

37 

38lsst.meas.base.wrapSimpleAlgorithm( 

39 testLib.SillyCentroidAlgorithm, 

40 # algorithm name is specified manually here because testLib isn't in a 

41 # normal Python package; normally this is unnecessary 

42 name="testLib_SillyCentroid", 

43 Control=testLib.SillyCentroidControl, 

44 executionOrder=lsst.meas.base.BasePlugin.CENTROID_ORDER, 

45) 

46 

47 

48class CentroidTestCase(lsst.utils.tests.TestCase): 

49 """A test case for centroiding. 

50 """ 

51 

52 def setUp(self): 

53 pass 

54 

55 def tearDown(self): 

56 pass 

57 

58 def testApplyCentroid(self): 

59 """Test that we can instantiate and play with SillyMeasureCentroid. 

60 """ 

61 

62 for imageFactory in ( 

63 afwImage.MaskedImageF, 

64 ): 

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

66 exp = afwImage.makeExposure(im) 

67 for offset in (0, 1, 2): 

68 control = testLib.SillyCentroidControl() 

69 control.param = offset 

70 x, y = 10, 20 

71 schema = afwTable.SourceTable.makeMinimalSchema() 

72 schema.addField("centroid_x", type=np.float64) 

73 schema.addField("centroid_y", type=np.float64) 

74 schema.getAliasMap().set("slot_Centroid", "centroid") 

75 plugin = testLib.SillyCentroidAlgorithm(control, "test", schema) 

76 measCat = afwTable.SourceCatalog(schema) 

77 source = measCat.makeRecord() 

78 source.set("centroid_x", x) 

79 source.set("centroid_y", y) 

80 plugin.measure(source, exp) 

81 self.assertEqual(x, source.get("test_x") - offset) 

82 self.assertEqual(y, source.get("test_y") - offset) 

83 

84 def testMeasureCentroid(self): 

85 """Test that we can use our silly centroid through the usual Tasks. 

86 """ 

87 testLib.SillyCentroidControl() 

88 x, y = 10, 20 

89 

90 im = afwImage.MaskedImageF(lsst.geom.ExtentI(512, 512)) 

91 im.set(0) 

92 arr = im.getImage().getArray() 

93 arr[y, x] = 1000 

94 exp = afwImage.makeExposure(im) 

95 

96 schema = afwTable.SourceTable.makeMinimalSchema() 

97 schema.addField("flags_negative", type="Flag", 

98 doc="set if source was detected as significantly negative") 

99 sfm_config = lsst.meas.base.sfm.SingleFrameMeasurementConfig() 

100 sfm_config.plugins = ["testLib_SillyCentroid"] 

101 sfm_config.plugins["testLib_SillyCentroid"].param = 5 

102 sfm_config.slots.centroid = "testLib_SillyCentroid" 

103 sfm_config.slots.shape = None 

104 sfm_config.slots.psfShape = None 

105 sfm_config.slots.psfFlux = None 

106 sfm_config.slots.gaussianFlux = None 

107 sfm_config.slots.calibFlux = None 

108 sfm_config.slots.apFlux = None 

109 sfm_config.slots.modelFlux = None 

110 sfm_config.doReplaceWithNoise = False 

111 task = lsst.meas.base.SingleFrameMeasurementTask(schema, config=sfm_config) 

112 measCat = afwTable.SourceCatalog(schema) 

113 measCat.defineCentroid("testLib_SillyCentroid") 

114 source = measCat.addNew() 

115 source.set("testLib_SillyCentroid_x", x) 

116 source.set("testLib_SillyCentroid_y", y) 

117 source.set("parent", 0) 

118 source.set("flags_negative", False) 

119 

120 # now run the SFM task with the test plugin 

121 task.run(measCat, exp) 

122 self.assertEqual(len(measCat), 1) 

123 self.assertEqual(measCat[0].getY(), y + 5) 

124 self.assertEqual(measCat[0].getX(), x + 5) 

125 

126 

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

128 pass 

129 

130 

131def setup_module(module): 

132 lsst.utils.tests.init() 

133 

134 

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

136 lsst.utils.tests.init() 

137 unittest.main()