Coverage for tests/test_objectSizeStarSelector.py: 19%

93 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-04 22:30 +0000

1# This file is part of meas_algorithms. 

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 

23import numpy as np 

24 

25import lsst.afw.table as afwTable 

26from lsst.meas.algorithms import sourceSelector 

27import lsst.meas.base.tests 

28import lsst.utils.tests 

29 

30fluxField = "base_GaussianFlux_instFlux" 

31 

32 

33class TestObjectSizeSourceSelector(lsst.utils.tests.TestCase): 

34 

35 def addGoodSource(self, sourceCat, num=0): 

36 """Insert a likely-good source into the catalog. 

37 

38 Parameters 

39 ---------- 

40 sourceCat : `lsst.afw.table.SourceCatalog` 

41 The source catalog for which a "good" source is to be added for testing. 

42 num : `float` or `int` 

43 A number that is added to various values to distinguish them in catalogs with multiple objects. 

44 """ 

45 sourceCat.addNew() 

46 sourceCat["coord_ra"][-1] = 1.0 + num 

47 sourceCat["coord_dec"][-1] = 2.0 + num 

48 # Add some variability to the fluxes to form a cluster with non-finite "width" in the mag-size plane 

49 fluxFactor = self.rng.uniform(low=0.98, high=1.02) 

50 sourceCat[fluxField][-1] = 100.0*fluxFactor # We set fluxMin = 50 in setUp 

51 sourceCat[fluxField + "Err"][-1] = 1.0 

52 # Add some variability to the shapes to form a cluster with non-finite "width" in the mag-size plane 

53 widthFactor = self.rng.uniform(low=0.98, high=1.02) 

54 sourceCat["truth_xx"][-1] = 3.0*widthFactor 

55 sourceCat["truth_yy"][-1] = 3.0*widthFactor 

56 sourceCat["truth_xy"][-1] = 1.0 

57 

58 def setUp(self): 

59 self.rng = np.random.Generator(np.random.MT19937(1)) 

60 

61 self.sourceSelector = sourceSelector.sourceSelectorRegistry["objectSize"]() 

62 self.badFlags = self.sourceSelector.config.badFlags 

63 schema = lsst.meas.base.tests.TestDataset.makeMinimalSchema() 

64 self.sourceSelector.config.sourceFluxField = fluxField 

65 self.sourceSelector.config.fluxMin = 50.0 

66 schema.addField(fluxField, type=np.float64) 

67 schema.addField(fluxField + "Err", type=np.float64) 

68 for flag in self.badFlags: 

69 schema.addField(flag, type="Flag") 

70 self.sourceCat = afwTable.SourceCatalog(schema) 

71 

72 def tearDown(self): 

73 del self.sourceCat 

74 del self.badFlags 

75 del self.sourceSelector 

76 

77 def testSelectSourcesGood(self): 

78 for i in range(5): 

79 self.addGoodSource(self.sourceCat, i) 

80 result = self.sourceSelector.selectSources(self.sourceCat) 

81 for src in self.sourceCat["id"]: 

82 self.assertIn(src, self.sourceCat[result.selected]["id"]) 

83 

84 def testSelectSourcesIndividualBadFlags(self): 

85 for i in range(len(self.badFlags)): 

86 self.addGoodSource(self.sourceCat, i) 

87 for i in range(len(self.badFlags)): 

88 for j, flag in enumerate(self.badFlags): 

89 self.sourceCat[j].set(flag, True) if i == j else self.sourceCat[j].set(flag, False) 

90 result = self.sourceSelector.selectSources(self.sourceCat) 

91 self.assertNotIn(self.sourceCat[i]["id"], self.sourceCat[result.selected]["id"], 

92 "should not have found %s" % flag) 

93 

94 def testSelectSourcesAllBadFlags(self): 

95 for i, flag in enumerate(self.badFlags): 

96 self.addGoodSource(self.sourceCat, i) 

97 self.sourceCat[i].set(flag, True) 

98 with self.assertRaises(RuntimeError): 

99 self.sourceSelector.selectSources(self.sourceCat) 

100 

101 def testSelectSourcesSignalToNoiseCuts(self): 

102 for i in range(10): 

103 self.addGoodSource(self.sourceCat, i) 

104 

105 self.sourceCat[fluxField + "Err"][0] = self.sourceCat[fluxField][0]/20.0 

106 self.sourceCat[fluxField + "Err"][1] = self.sourceCat[fluxField][1]/500.0 

107 self.sourceCat[fluxField + "Err"][2] = self.sourceCat[fluxField][2]/2000.0 

108 

109 self.sourceSelector.config.signalToNoiseMax = 1000 

110 result = self.sourceSelector.run(self.sourceCat) 

111 self.assertNotIn(self.sourceCat[0]["id"], result.sourceCat["id"]) 

112 self.assertIn(self.sourceCat[1]["id"], result.sourceCat["id"]) 

113 self.assertNotIn(self.sourceCat[2]["id"], result.sourceCat["id"]) 

114 

115 def testSelectSourcesNoSignalToNoiseCut(self): 

116 for i in range(5): 

117 self.addGoodSource(self.sourceCat, i) 

118 self.sourceCat[fluxField + "Err"][0] = self.sourceCat[fluxField][0]*1e+9 # S/N ~1e-9 

119 self.sourceSelector.config.signalToNoiseMin = 0 

120 result = self.sourceSelector.run(self.sourceCat) 

121 self.assertIn(self.sourceCat[0]["id"], result.sourceCat["id"]) 

122 

123 def testSelectSourcesFluxCuts(self): 

124 for i in range(10): 

125 self.addGoodSource(self.sourceCat, i) 

126 

127 self.sourceSelector.config.doSignalToNoiseLimit = False 

128 self.sourceSelector.config.doFluxLimit = True 

129 self.sourceSelector.config.fluxMin = 98.0 # Just outside of range allowed inself.addGoodSource() 

130 self.sourceCat[fluxField][0] = 97.9 

131 self.sourceSelector.config.fluxMax = 101.3 # Just outside of range allowed inself.addGoodSource() 

132 self.sourceCat[fluxField][2] = 101.4 

133 result = self.sourceSelector.run(self.sourceCat) 

134 self.assertNotIn(self.sourceCat[0]["id"], result.sourceCat["id"]) 

135 self.assertIn(self.sourceCat[1]["id"], result.sourceCat["id"]) 

136 self.assertNotIn(self.sourceCat[2]["id"], result.sourceCat["id"]) 

137 

138 

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

140 pass 

141 

142 

143def setup_module(module): 

144 lsst.utils.tests.init() 

145 

146 

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

148 lsst.utils.tests.init() 

149 unittest.main()