Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 columns to be filled 

39 # 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, so they 

49 # 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 testing only cares 

62 # 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, second input only) 

67 schema = SourceCatalog.Table.makeMinimalSchema() 

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

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

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

71 schema=schema, name="subaru_FilterFraction", 

72 metadata=PropertyList()) 

73 catalog = SourceCatalog(schema) 

74 self.record1 = catalog.addNew() 

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

76 self.record12 = catalog.addNew() 

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

78 self.record2 = catalog.addNew() 

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

80 

81 def tearDown(self): 

82 del self.coadd 

83 del self.input1 

84 del self.input2 

85 del self.record1 

86 del self.record2 

87 del self.record12 

88 del self.plugin 

89 

90 def testSingleFilter(self): 

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

92 self.input1.set(self.filterKey, "g") 

93 self.input2.set(self.filterKey, "g") 

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

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

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

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

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

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

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

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

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

103 

104 def testTwoFiltersI(self): 

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

106 self.input1.set(self.filterKey, "i") 

107 self.input2.set(self.filterKey, "i2") 

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

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

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

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

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

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

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

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

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

117 

118 def testTwoFiltersR(self): 

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

120 self.input1.set(self.filterKey, "r") 

121 self.input2.set(self.filterKey, "r2") 

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

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

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

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

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

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

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

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

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

131 

132 def testInvalidCombination(self): 

133 """Test that we get a fatal exception for weird combinations of filters.""" 

134 self.input1.set(self.filterKey, "i") 

135 self.input2.set(self.filterKey, "r") 

136 with self.assertRaises(FatalAlgorithmError): 

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

138 

139 

140def setup_module(module): 

141 lsst.utils.tests.init() 

142 

143 

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

145 lsst.utils.tests.init() 

146 unittest.main()