Coverage for tests/test_PeakCentroid.py: 49%

63 statements  

« prev     ^ index     » next       coverage.py v7.2.4, created at 2023-04-30 02:10 -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 

22from contextlib import contextmanager 

23import unittest 

24 

25import lsst.geom 

26import lsst.daf.base 

27import lsst.meas.base 

28import lsst.utils.tests 

29 

30from lsst.meas.base.tests import (AlgorithmTestCase, CentroidTransformTestCase, 

31 SingleFramePluginTransformSetupHelper, ForcedPluginTransformSetupHelper) 

32 

33 

34@contextmanager 

35def onlyLogFatal(log): 

36 """For the duration of this context, only log ``FATAL`` errors. 

37 

38 This is convenient when testing algorithms under failure conditions: we 

39 want to be able to check that they have set appropriate flags without 

40 spewing alarming & confusing error messages to the console. 

41 """ 

42 oldLevel = log.level 

43 log.setLevel(log.FATAL) 

44 try: 

45 yield 

46 finally: 

47 log.setLevel(oldLevel) 

48 

49 

50class SingleFramePeakCentroidTestCase(AlgorithmTestCase, lsst.utils.tests.TestCase): 

51 

52 def setUp(self): 

53 self.center = lsst.geom.Point2D(50.1, 49.8) 

54 self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(-20, -30), 

55 lsst.geom.Extent2I(140, 160)) 

56 self.dataset = lsst.meas.base.tests.TestDataset(self.bbox) 

57 self.dataset.addSource(1000000.0, self.center) 

58 self.task = self.makeSingleFrameMeasurementTask("base_PeakCentroid") 

59 self.exposure, self.catalog = self.dataset.realize(10.0, self.task.schema, randomSeed=0) 

60 

61 def tearDown(self): 

62 del self.center 

63 del self.bbox 

64 del self.dataset 

65 del self.task 

66 del self.exposure 

67 del self.catalog 

68 

69 def testSingleFramePlugin(self): 

70 """Check that we recover the correct location of the centroid. 

71 """ 

72 self.task.run(self.catalog, self.exposure) 

73 x = self.catalog[0].get("base_PeakCentroid_x") 

74 y = self.catalog[0].get("base_PeakCentroid_y") 

75 self.assertFalse(self.catalog[0].get("base_PeakCentroid_flag")) 

76 self.assertFloatsAlmostEqual(x, self.center.getX(), atol=None, rtol=.02) 

77 self.assertFloatsAlmostEqual(y, self.center.getY(), atol=None, rtol=.02) 

78 

79 def testFlags(self): 

80 """Test that a flag is set when centroid measurement is impossible. 

81 

82 Notes 

83 ----- 

84 In this case, it is impossible to measure the centroid because we 

85 remove the peaks from the `~lsst.afw.table.SourceRecord`. 

86 """ 

87 self.catalog[0].getFootprint().getPeaks().clear() 

88 # The decorator suppresses alarming but expected errors on the console. 

89 with onlyLogFatal(self.task.log): 

90 self.task.run(self.catalog, self.exposure) 

91 self.assertTrue(self.catalog[0].get("base_PeakCentroid_flag")) 

92 

93 

94class SingleFramePeakCentroidTransformTestCase(CentroidTransformTestCase, 

95 SingleFramePluginTransformSetupHelper, 

96 lsst.utils.tests.TestCase): 

97 

98 class SingleFramePeakCentroidPluginFactory: 

99 """Supply an empty ``PropertyList`` to `XingleFramePeakCentroidPlugin`. 

100 

101 This is a helper class to make testing more convenient. 

102 """ 

103 

104 def __call__(self, control, name, inputSchema): 

105 return lsst.meas.base.SingleFramePeakCentroidPlugin(control, name, inputSchema, 

106 lsst.daf.base.PropertyList()) 

107 controlClass = lsst.meas.base.SingleFramePeakCentroidConfig 

108 algorithmClass = SingleFramePeakCentroidPluginFactory() 

109 transformClass = lsst.meas.base.SimpleCentroidTransform 

110 flagNames = () 

111 singleFramePlugins = ("base_PeakCentroid",) 

112 

113 

114class ForcedPeakCentroidTransformTestCase(CentroidTransformTestCase, 

115 ForcedPluginTransformSetupHelper, 

116 lsst.utils.tests.TestCase): 

117 controlClass = lsst.meas.base.ForcedPeakCentroidConfig 

118 algorithmClass = lsst.meas.base.ForcedPeakCentroidPlugin 

119 transformClass = lsst.meas.base.SimpleCentroidTransform 

120 flagNames = () 

121 forcedPlugins = ("base_PeakCentroid",) 

122 

123 

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

125 pass 

126 

127 

128def setup_module(module): 

129 lsst.utils.tests.init() 

130 

131 

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

133 lsst.utils.tests.init() 

134 unittest.main()