Coverage for tests/test_filterLabel.py: 21%

92 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-22 02:38 -0700

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 testEqualsHash(self): 

93 self.assertEqual(hash(FilterLabel(band=self.band)), 

94 hash(FilterLabel(band=self.band))) 

95 self.assertEqual(hash(FilterLabel(physical=self.physicalName)), 

96 hash(FilterLabel(physical=self.physicalName))) 

97 

98 def testRepr(self): 

99 for label in self._labelVariants(): 

100 try: 

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

102 except (SyntaxError, ValueError): 

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

104 raise 

105 

106 def testPersistence(self): 

107 """Check persistence of a FilterLabel. 

108 """ 

109 for label in self._labelVariants(): 

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

111 label.writeFits(outFile) 

112 roundTrip = FilterLabel.readFits(outFile) 

113 self.assertEqual(roundTrip, label) 

114 

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

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

117 if value: 

118 self.assertTrue(has(label)) 

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

120 else: 

121 self.assertFalse(has(label)) 

122 with self.assertRaises(RuntimeError): 

123 property.__get__(label) 

124 

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

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

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

128 

129 def testFactories(self): 

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

131 """ 

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

133 self.band, self.physicalName) 

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

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

136 

137 def _checkCopy(self, label1, label2): 

138 self.assertEqual(label1, label2) 

139 self.assertIsNot(label1, label2) 

140 

141 def testCopy(self): 

142 for label in self._labelVariants(): 

143 copy1 = copy.copy(label) 

144 self._checkCopy(copy1, label) 

145 

146 copy2 = copy.deepcopy(label) 

147 self._checkCopy(copy2, label) 

148 

149 def testDatabaseLabel(self): 

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

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

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

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

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

155 

156 

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

158 pass 

159 

160 

161def setup_module(module): 

162 lsst.utils.tests.init() 

163 

164 

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

166 lsst.utils.tests.init() 

167 unittest.main()