Coverage for tests/test_MomentsClassifier.py: 23%

62 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-03 03:00 -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 lsst.meas.base as measBase 

25import lsst.meas.base.tests 

26import lsst.utils.tests 

27import numpy as np 

28 

29 

30class MomentsClassificationTestCase(lsst.meas.base.tests.AlgorithmTestCase, lsst.utils.tests.TestCase): 

31 

32 def setUp(self): 

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

34 lsst.geom.Extent2I(250, 150)) 

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

36 

37 self.n_stars = 1 

38 self.n_gals = 1 

39 # First 10 sources are point sources 

40 self.dataset.addSource(1000.0, lsst.geom.Point2D(50.1, 49.8)) 

41 # Following 10 sources are extended sources 

42 self.dataset.addSource(5000.0, lsst.geom.Point2D(149.9, 50.3), 

43 lsst.afw.geom.Quadrupole(1, 1.2, 0.3)) 

44 

45 def tearDown(self): 

46 del self.bbox 

47 del self.dataset 

48 

49 def testSingleFramePlugin(self): 

50 config = measBase.SingleFrameMeasurementConfig() 

51 task = self.makeSingleFrameMeasurementTask(config=config) 

52 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=3) 

53 task.run(catalog, exposure) 

54 for ii in range(self.n_stars): 

55 self.assertLess(catalog[ii].get("base_ClassificationSizeExtendedness_value"), 0.1) 

56 for ii in range(self.n_stars, self.n_stars + self.n_gals): 

57 self.assertGreater(catalog[ii].get("base_ClassificationSizeExtendedness_value"), 0.02) 

58 

59 def testFailure(self): 

60 """Test that MeasurementError is raised properly if shape flag is set. 

61 """ 

62 config = measBase.SingleFrameMeasurementConfig() 

63 task = self.makeSingleFrameMeasurementTask(config=config) 

64 exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=5) 

65 plugin = task.plugins["base_ClassificationSizeExtendedness"] 

66 plugin_order = plugin.getExecutionOrder() 

67 task.run(catalog, exposure, endOrder=plugin_order) 

68 # Set the shape flags by hand to trigger a failure. 

69 catalog["slot_Shape_flag"] = 1 

70 for record in catalog: 

71 with self.subTest(id=record["id"]): 

72 with self.assertRaisesRegex(measBase.MeasurementError, "Shape flag is set"): 

73 plugin.measure(record, exposure) 

74 

75 @lsst.utils.tests.methodParameters(noise=(0.001, 0.01)) 

76 def testMonteCarlo(self, noise: float, n_trials: int = 100): 

77 """Test an ideal simulation, with no noise. 

78 

79 Demonstrate that: 

80 

81 - We get exactly the right answer, and 

82 - The reported uncertainty agrees with a Monte Carlo test of the noise. 

83 

84 Parameters 

85 ---------- 

86 noise : float 

87 Noise level to use in the simulation. 

88 n_trials : int 

89 Number of trials to use in the Monte Carlo test. 

90 """ 

91 config = measBase.SingleFrameMeasurementConfig() 

92 task = self.makeSingleFrameMeasurementTask(config=config) 

93 

94 star_measures, galaxy_measures = [], [] 

95 for ii in range(n_trials): 

96 exposure, catalog = self.dataset.realize(1000.0*noise, task.schema, randomSeed=ii) 

97 task.run(catalog, exposure) 

98 for ii in range(self.n_stars): 

99 star_measures.append(catalog[ii].get("base_ClassificationSizeExtendedness_value")) 

100 for ii in range(self.n_stars, self.n_stars + self.n_gals): 

101 galaxy_measures.append(catalog[ii].get("base_ClassificationSizeExtendedness_value")) 

102 

103 # Mapping noise level to thresholds for stars and galaxies 

104 star_threshold = { 

105 0.001: 0.01, 

106 0.01: 0.1 

107 } 

108 galaxy_threshold = { 

109 0.001: 0.25, 

110 0.01: 0.25, 

111 } 

112 self.assertLess(np.mean(star_measures), star_threshold[noise]) 

113 self.assertLess(np.percentile(star_measures, 50), star_threshold[noise]) 

114 self.assertGreater(np.mean(galaxy_measures), galaxy_threshold[noise]) 

115 self.assertGreater(np.percentile(galaxy_measures, 50), galaxy_threshold[noise]) 

116 

117 

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

119 pass 

120 

121 

122def setup_module(module): 

123 lsst.utils.tests.init() 

124 

125 

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

127 lsst.utils.tests.init() 

128 unittest.main()