Coverage for tests/test_diaCatalogSourceSelector.py: 21%

79 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-25 16:19 +0000

1# 

2# LSST Data Management System 

3# Copyright 2008-2016 LSST Corporation. 

4# 

5# This product includes software developed by the 

6# LSST Project (http://www.lsst.org/). 

7# 

8# This program is free software: you can redistribute it and/or modify 

9# it under the terms of the GNU General Public License as published by 

10# the Free Software Foundation, either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# This program is distributed in the hope that it will be useful, 

14# but WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the LSST License Statement and 

19# the GNU General Public License along with this program. If not, 

20# see <http://www.lsstcorp.org/LegalNotices/>. 

21# 

22import unittest 

23 

24import numpy as np 

25 

26import lsst.utils.tests 

27import lsst.afw.table as afwTable 

28import lsst.afw.image as afwImage 

29import lsst.geom as geom 

30from lsst.meas.algorithms import convertReferenceCatalog, getRefFluxField 

31import lsst.ip.diffim as ipDiffim 

32 

33 

34class DiaCatalogSourceSelectorTest(lsst.utils.tests.TestCase): 

35 

36 def setUp(self): 

37 schema = afwTable.SourceTable.makeMinimalSchema() 

38 schema.addField("test_instFlux", type=float) 

39 schema.addField("test_instFluxErr", type=float) 

40 self.sourceSelector = ipDiffim.DiaCatalogSourceSelectorTask() 

41 for flag in self.sourceSelector.config.badFlags: 

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

43 table = afwTable.SourceTable.make(schema) 

44 table.definePsfFlux("test") 

45 self.srcCat = afwTable.SourceCatalog(table) 

46 self.exposure = afwImage.ExposureF() 

47 

48 def tearDown(self): 

49 del self.sourceSelector 

50 del self.exposure 

51 del self.srcCat 

52 

53 def makeRefCatalog(self): 

54 schema = convertReferenceCatalog._makeSchema(filterNameList=["g", "r"], addIsPhotometric=True, 

55 addIsResolved=True) 

56 catalog = afwTable.SimpleCatalog(schema) 

57 return catalog 

58 

59 def makeMatches(self, refCat, srcCat, nSrc): 

60 for i in range(nSrc): 

61 

62 refSrc = refCat.addNew() 

63 srcSrc = srcCat.addNew() 

64 

65 raDeg, decDeg = np.random.randn(2) 

66 coord = geom.SpherePoint(raDeg, decDeg, geom.degrees) 

67 

68 refSrc.set("g_flux", 10**(-0.4*18)) 

69 refSrc.set("r_flux", 10**(-0.4*18)) 

70 refSrc.set("resolved", False) 

71 refSrc.set("photometric", True) 

72 refSrc.setCoord(coord) 

73 

74 srcSrc.setCoord(coord) 

75 srcSrc.set("slot_PsfFlux_instFlux", 10.) 

76 srcSrc.set("slot_PsfFlux_instFluxErr", 1.) 

77 for flag in self.sourceSelector.config.badFlags: 

78 srcSrc.set(flag, False) 

79 

80 mc = afwTable.MatchControl() 

81 mc.symmetricMatch = False 

82 mat = afwTable.matchRaDec(refCat, srcCat, 1.0 * geom.arcseconds, mc) 

83 self.assertEqual(len(mat), nSrc) 

84 return mat 

85 

86 def testCuts(self): 

87 nSrc = 5 

88 

89 refCat = self.makeRefCatalog() 

90 

91 matches = self.makeMatches(refCat, self.srcCat, nSrc) 

92 sources = self.sourceSelector.run(self.srcCat, matches=matches, exposure=self.exposure).sourceCat 

93 self.assertEqual(len(sources), nSrc) 

94 

95 # Set one of the source flags to be bad 

96 matches[0].second.set(self.sourceSelector.config.badFlags[0], True) 

97 sources = self.sourceSelector.run(self.srcCat, matches=matches, exposure=self.exposure).sourceCat 

98 self.assertEqual(len(sources), nSrc-1) 

99 

100 # Set one of the ref flags to be bad 

101 matches[1].first.set("photometric", False) 

102 sources = self.sourceSelector.run(self.srcCat, matches=matches, exposure=self.exposure).sourceCat 

103 self.assertEqual(len(sources), nSrc-2) 

104 

105 # Set one of the colors to be bad 

106 grMin = self.sourceSelector.config.grMin 

107 rFluxField = getRefFluxField(refCat.schema, "r") 

108 gFluxField = getRefFluxField(refCat.schema, "g") 

109 gFlux = 10**(-0.4 * (grMin - 0.1)) * matches[2].first.get(rFluxField) 

110 matches[2].first.set(gFluxField, gFlux) 

111 sources = self.sourceSelector.run(self.srcCat, matches=matches, exposure=self.exposure).sourceCat 

112 self.assertEqual(len(sources), nSrc-3) 

113 

114 # Set one of the types to be bad 

115 if self.sourceSelector.config.selectStar and not self.sourceSelector.config.selectGalaxy: 

116 matches[3].first.set("resolved", True) 

117 sources = self.sourceSelector.run(self.srcCat, matches=matches, exposure=self.exposure).sourceCat 

118 self.assertEqual(len(sources), nSrc-4) 

119 

120 

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

122 pass 

123 

124 

125def setup_module(module): 

126 lsst.utils.tests.init() 

127 

128 

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

130 lsst.utils.tests.init() 

131 unittest.main()