Coverage for tests/test_filterFraction.py: 21%

94 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-02 10:51 +0000

1# 

2# LSST Data Management System 

3# 

4# Copyright 2017 AURA/LSST. 

5# 

6# This product includes software developed by the 

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

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 LSST License Statement and 

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

21# see <https://www.lsstcorp.org/LegalNotices/>. 

22# 

23import unittest 

24 

25import lsst.utils.tests 

26from lsst.afw.image import ExposureF, CoaddInputs 

27from lsst.afw.table import ExposureCatalog, SourceCatalog, Point2DKey 

28from lsst.afw.geom import makeCdMatrix, makeSkyWcs 

29from lsst.geom import degrees, Point2D, Box2I, Point2I, SpherePoint 

30from lsst.daf.base import PropertyList 

31from lsst.meas.base import FatalAlgorithmError 

32from lsst.obs.subaru.filterFraction import FilterFractionPlugin 

33 

34 

35class FilterFractionTest(lsst.utils.tests.TestCase): 

36 

37 def setUp(self): 

38 # Set up a Coadd with CoaddInputs tables that have blank filter 

39 # columns to be filled in by later test code. 

40 self.coadd = ExposureF(30, 90) 

41 # WCS is arbitrary, since it'll be the same for all images 

42 wcs = makeSkyWcs(crpix=Point2D(0, 0), crval=SpherePoint(45.0, 45.0, degrees), 

43 cdMatrix=makeCdMatrix(scale=0.17*degrees)) 

44 self.coadd.setWcs(wcs) 

45 schema = ExposureCatalog.Table.makeMinimalSchema() 

46 self.filterKey = schema.addField("filter", type=str, doc="", size=16) 

47 weightKey = schema.addField("weight", type=float, doc="") 

48 # First input image covers the first 2/3, second covers the last 2/3, 

49 # so they overlap in the middle 1/3. 

50 inputs = ExposureCatalog(schema) 

51 self.input1 = inputs.addNew() 

52 self.input1.setId(1) 

53 self.input1.setBBox(Box2I(Point2I(0, 0), Point2I(29, 59))) 

54 self.input1.setWcs(wcs) 

55 self.input1.set(weightKey, 2.0) 

56 self.input2 = inputs.addNew() 

57 self.input2.setId(2) 

58 self.input2.setBBox(Box2I(Point2I(0, 30), Point2I(29, 89))) 

59 self.input2.setWcs(wcs) 

60 self.input2.set(weightKey, 3.0) 

61 # Use the same catalog for visits and CCDs since the algorithm we're 

62 # testing only cares about CCDs. 

63 self.coadd.getInfo().setCoaddInputs(CoaddInputs(inputs, inputs)) 

64 

65 # Set up a catalog with centroids and a FilterFraction plugin. 

66 # We have one record in each region (first input only, both inputs, 

67 # second input only) 

68 schema = SourceCatalog.Table.makeMinimalSchema() 

69 centroidKey = Point2DKey.addFields(schema, "centroid", doc="position", unit="pixel") 

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

71 self.plugin = FilterFractionPlugin(config=FilterFractionPlugin.ConfigClass(), 

72 schema=schema, name="subaru_FilterFraction", 

73 metadata=PropertyList()) 

74 catalog = SourceCatalog(schema) 

75 self.record1 = catalog.addNew() 

76 self.record1.set(centroidKey, Point2D(14.0, 14.0)) 

77 self.record12 = catalog.addNew() 

78 self.record12.set(centroidKey, Point2D(14.0, 44.0)) 

79 self.record2 = catalog.addNew() 

80 self.record2.set(centroidKey, Point2D(14.0, 74.0)) 

81 

82 def tearDown(self): 

83 del self.coadd 

84 del self.input1 

85 del self.input2 

86 del self.record1 

87 del self.record2 

88 del self.record12 

89 del self.plugin 

90 

91 def testSingleFilter(self): 

92 """Test that we get FilterFraction=1 for filters with only one version. 

93 """ 

94 self.input1.set(self.filterKey, "HSC-G") 

95 self.input2.set(self.filterKey, "HSC-G") 

96 self.plugin.measure(self.record1, self.coadd) 

97 self.plugin.measure(self.record12, self.coadd) 

98 self.plugin.measure(self.record2, self.coadd) 

99 self.assertEqual(self.record1.get("subaru_FilterFraction_unweighted"), 1.0) 

100 self.assertEqual(self.record12.get("subaru_FilterFraction_unweighted"), 1.0) 

101 self.assertEqual(self.record2.get("subaru_FilterFraction_unweighted"), 1.0) 

102 self.assertEqual(self.record1.get("subaru_FilterFraction_weighted"), 1.0) 

103 self.assertEqual(self.record12.get("subaru_FilterFraction_weighted"), 1.0) 

104 self.assertEqual(self.record2.get("subaru_FilterFraction_weighted"), 1.0) 

105 

106 def testTwoFiltersI(self): 

107 """Test that we get the right answers for a mix of i and i2.""" 

108 self.input1.set(self.filterKey, "HSC-I") 

109 self.input2.set(self.filterKey, "HSC-I2") 

110 self.plugin.measure(self.record1, self.coadd) 

111 self.plugin.measure(self.record12, self.coadd) 

112 self.plugin.measure(self.record2, self.coadd) 

113 self.assertEqual(self.record1.get("subaru_FilterFraction_unweighted"), 0.0) 

114 self.assertEqual(self.record12.get("subaru_FilterFraction_unweighted"), 0.5) 

115 self.assertEqual(self.record2.get("subaru_FilterFraction_unweighted"), 1.0) 

116 self.assertEqual(self.record1.get("subaru_FilterFraction_weighted"), 0.0) 

117 self.assertEqual(self.record12.get("subaru_FilterFraction_weighted"), 0.6) 

118 self.assertEqual(self.record2.get("subaru_FilterFraction_weighted"), 1.0) 

119 

120 def testTwoFiltersR(self): 

121 """Test that we get the right answers for a mix of r and r2.""" 

122 self.input1.set(self.filterKey, "HSC-R") 

123 self.input2.set(self.filterKey, "HSC-R2") 

124 self.plugin.measure(self.record1, self.coadd) 

125 self.plugin.measure(self.record12, self.coadd) 

126 self.plugin.measure(self.record2, self.coadd) 

127 self.assertEqual(self.record1.get("subaru_FilterFraction_unweighted"), 0.0) 

128 self.assertEqual(self.record12.get("subaru_FilterFraction_unweighted"), 0.5) 

129 self.assertEqual(self.record2.get("subaru_FilterFraction_unweighted"), 1.0) 

130 self.assertEqual(self.record1.get("subaru_FilterFraction_weighted"), 0.0) 

131 self.assertEqual(self.record12.get("subaru_FilterFraction_weighted"), 0.6) 

132 self.assertEqual(self.record2.get("subaru_FilterFraction_weighted"), 1.0) 

133 

134 def testInvalidCombination(self): 

135 """Test that we get a fatal exception for weird combinations of 

136 filters. 

137 """ 

138 self.input1.set(self.filterKey, "HSC-I") 

139 self.input2.set(self.filterKey, "HSC-R") 

140 with self.assertRaises(FatalAlgorithmError): 

141 self.plugin.measure(self.record12, self.coadd) 

142 

143 

144def setup_module(module): 

145 lsst.utils.tests.init() 

146 

147 

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

149 lsst.utils.tests.init() 

150 unittest.main()