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# This file is part of afw. 

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 copy 

23import unittest 

24 

25import lsst.utils.tests 

26 

27from lsst.afw.image import FilterLabel, getDatabaseFilterLabel 

28 

29 

30class FilterLabelTestCase(lsst.utils.tests.TestCase): 

31 def setUp(self): 

32 self.className = "FilterLabel" 

33 self.physicalName = "k-0324" 

34 self.band = "k" 

35 

36 def _labelVariants(self): 

37 # Contains some redundant entries to check that equality behaves 

38 # consistently across both construction methods. 

39 return iter({FilterLabel(physical=self.physicalName, band=self.band), 

40 FilterLabel.fromBand(self.band), 

41 FilterLabel(band=self.band), 

42 FilterLabel(physical=self.physicalName), 

43 }) 

44 

45 def testInit(self): 

46 with self.assertRaises(ValueError): 

47 FilterLabel() 

48 self.assertEqual(FilterLabel(physical=self.physicalName), 

49 FilterLabel.fromPhysical(self.physicalName)) 

50 self.assertEqual(FilterLabel(band=self.band), 

51 FilterLabel.fromBand(self.band)) 

52 self.assertEqual(FilterLabel(physical=self.physicalName, band=self.band), 

53 FilterLabel.fromBandPhysical(self.band, self.physicalName)) 

54 

55 with self.assertRaises(TypeError): 

56 FilterLabel(physical=42) 

57 with self.assertRaises(TypeError): 

58 FilterLabel(band=("g", "r")) 

59 

60 def testEqualsBasic(self): 

61 # Reflexivity 

62 for label in self._labelVariants(): 

63 self.assertEqual(label, label) 

64 self.assertFalse(label != label) 

65 

66 # Symmetry 

67 for labelA in self._labelVariants(): 

68 for labelB in self._labelVariants(): 

69 self.assertEqual(labelA == labelB, labelB == labelA) 

70 self.assertEqual(labelA != labelB, labelB != labelA) 

71 

72 # Transitivity 

73 for labelA in self._labelVariants(): 

74 for labelB in self._labelVariants(): 

75 for labelC in self._labelVariants(): 

76 if labelA == labelB and labelB == labelC: 

77 self.assertEqual(labelA, labelC) 

78 # The logical implications if A != B or B != C are handled 

79 # on a different iteration/permutation of (A, B, C). 

80 

81 def testEqualsIdentical(self): 

82 self.assertEqual(FilterLabel(physical=self.physicalName), FilterLabel(physical=self.physicalName)) 

83 

84 def testEqualsSameText(self): 

85 # Ensure different kinds of labels are distinguishable, even if they have the same string 

86 self.assertNotEqual(FilterLabel(band=self.band), FilterLabel(physical=self.band)) 

87 

88 def testEqualsMissingField(self): 

89 self.assertNotEqual(FilterLabel(band=self.band), 

90 FilterLabel(band=self.band, physical=self.physicalName)) 

91 

92 def testRepr(self): 

93 for label in self._labelVariants(): 

94 try: 

95 self.assertEqual(eval(repr(label)), label) 

96 except (SyntaxError, ValueError): 

97 print(f"repr(label) = '{label!r}'") 

98 raise 

99 

100 def testPersistence(self): 

101 """Check persistence of a FilterLabel. 

102 """ 

103 for label in self._labelVariants(): 

104 with lsst.utils.tests.getTempFilePath(".fits") as outFile: 

105 label.writeFits(outFile) 

106 roundTrip = FilterLabel.readFits(outFile) 

107 self.assertEqual(roundTrip, label) 

108 

109 def _checkProperty(self, label, has, property, value): 

110 # For consistency with C++ API, getting a missing label raises instead of returning None 

111 if value: 

112 self.assertTrue(has(label)) 

113 self.assertEqual(property.__get__(label), value) 

114 else: 

115 self.assertFalse(has(label)) 

116 with self.assertRaises(RuntimeError): 

117 property.__get__(label) 

118 

119 def _checkFactory(self, label, band, physical): 

120 self._checkProperty(label, FilterLabel.hasBandLabel, FilterLabel.bandLabel, band) 

121 self._checkProperty(label, FilterLabel.hasPhysicalLabel, FilterLabel.physicalLabel, physical) 

122 

123 def testFactories(self): 

124 """This method tests the getters as well as the factories, since their behaviors are linked. 

125 """ 

126 self._checkFactory(FilterLabel.fromBandPhysical(self.band, self.physicalName), 

127 self.band, self.physicalName) 

128 self._checkFactory(FilterLabel.fromBand(self.band), self.band, None) 

129 self._checkFactory(FilterLabel.fromPhysical(self.physicalName), None, self.physicalName) 

130 

131 def _checkCopy(self, label1, label2): 

132 self.assertEqual(label1, label2) 

133 self.assertIsNot(label1, label2) 

134 

135 def testCopy(self): 

136 for label in self._labelVariants(): 

137 copy1 = copy.copy(label) 

138 self._checkCopy(copy1, label) 

139 

140 copy2 = copy.deepcopy(label) 

141 self._checkCopy(copy2, label) 

142 

143 def testDatabaseLabel(self): 

144 self.assertEqual(getDatabaseFilterLabel("k"), "k") 

145 self.assertEqual(getDatabaseFilterLabel("k-0234"), "k_0234") 

146 self.assertEqual(getDatabaseFilterLabel("g decam 0101"), "g_decam_0101") 

147 self.assertEqual(getDatabaseFilterLabel("speci@l band"), "speci_l_band") 

148 self.assertEqual(getDatabaseFilterLabel("\"hacky, (42);"), "_hacky___42__") 

149 

150 

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

152 pass 

153 

154 

155def setup_module(module): 

156 lsst.utils.tests.init() 

157 

158 

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

160 lsst.utils.tests.init() 

161 unittest.main()