Coverage for tests/test_combineMetadata.py: 13%

93 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-11 02:44 -0800

1# 

2# LSST Data Management System 

3# Copyright 2017 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# 

22 

23import unittest 

24 

25import lsst.utils.tests 

26from lsst.daf.base import PropertyList 

27from lsst.afw.fits import combineMetadata 

28 

29 

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

31 

32 def assertMetadataEqual(self, md1, md2): 

33 names1 = md1.getOrderedNames() 

34 names2 = md2.getOrderedNames() 

35 self.assertEqual(names1, names2) 

36 for name in names1: 

37 item1 = md1.getArray(name) 

38 item2 = md2.getArray(name) 

39 self.assertEqual(item1, item2) 

40 self.assertEqual(type(item1), type(item2)) 

41 

42 def testNoConflicts(self): 

43 """Test combination with valid values and no overlap, 

44 except COMMENT and HISTORY, which are combined 

45 """ 

46 md1 = PropertyList() 

47 md1.set("int1", [1, 2]) 

48 md1.set("float1", 1.23) 

49 md1.set("string1", "md1 string1 value") 

50 md1.set("COMMENT", "md1 comment") 

51 md1.set("HISTORY", "md1 history") 

52 md1Copy = md1.deepCopy() 

53 

54 md2 = PropertyList() 

55 md2.set("int2", 2) 

56 md2.set("float2", [2.34, -3.45]) 

57 md2.set("string2", "md2 string2 value") 

58 md2.set("COMMENT", "md2 comment") 

59 md2.set("HISTORY", "md2 history") 

60 md2Copy = md2.deepCopy() 

61 

62 result = combineMetadata(md1, md2) 

63 self.assertEqual(result.getOrderedNames(), 

64 ["int1", "float1", "string1", "COMMENT", "HISTORY", 

65 "int2", "float2", "string2"]) 

66 self.assertEqual(result.getArray("COMMENT"), ["md1 comment", "md2 comment"]) 

67 self.assertEqual(result.getArray("HISTORY"), ["md1 history", "md2 history"]) 

68 for name in md1.getOrderedNames(): 

69 if name in ("COMMENT", "HISTORY"): 

70 continue 

71 self.assertEqual(result.getScalar(name), md1.getArray(name)[-1]) 

72 for name in md2.getOrderedNames(): 

73 if name in ("COMMENT", "HISTORY"): 

74 continue 

75 self.assertEqual(result.getScalar(name), md2.getArray(name)[-1]) 

76 

77 # input should be unchanged 

78 self.assertMetadataEqual(md1, md1Copy) 

79 self.assertMetadataEqual(md2, md2Copy) 

80 

81 def testIgnoreInvalid(self): 

82 """Test that invalid items in the either argument are ignored 

83 """ 

84 md1 = PropertyList() 

85 # Set COMMENT and HISTORY to invalid values -- anything other than string 

86 # (regardless if it is a scalar or an array); 

87 # for md1 use arrays and md2 use scalars, just to try both 

88 md1.set("COMMENT", [5, 6]) 

89 md1.set("HISTORY", [3.5, 6.1]) 

90 md1Copy = md1.deepCopy() 

91 

92 md2 = PropertyList() 

93 # Set COMMENT and HISTORY to invalid values; see comment above md1.set("COMMENT", ...) 

94 md2.set("COMMENT", 7) 

95 md2.set("HISTORY", 1.06) 

96 md2Copy = md2.deepCopy() 

97 

98 result = combineMetadata(md1, md2) 

99 resultNames = result.getOrderedNames() 

100 self.assertEqual(resultNames, []) 

101 

102 # input should be unchanged 

103 self.assertMetadataEqual(md1, md1Copy) 

104 self.assertMetadataEqual(md2, md2Copy) 

105 

106 def testReplaceDuplicates(self): 

107 """Test that names in `second` override those in `first`, regardless of type 

108 """ 

109 # names that start with "item" appear in both sets of metadata 

110 md1 = PropertyList() 

111 md1.set("int1", 5) 

112 md1.set("itema", [1, 2]) 

113 md1.set("float1", 3.1) 

114 md1.set("itemb", 1.23) 

115 md1.set("string1", "md1 string1 value") 

116 md1.set("itemc", "md1 string value") 

117 md1Copy = md1.deepCopy() 

118 

119 md2 = PropertyList() 

120 md2.set("itemc", 2) 

121 md2.set("int2", 2) 

122 md2.set("itemb", ["some data", "more data"]) 

123 md2.set("float2", 2.34) 

124 md2.set("itema", 5.27) 

125 md2.set("string2", "md2 string value") 

126 md2Names = md2.getOrderedNames() 

127 md2Copy = md2.deepCopy() 

128 

129 result = combineMetadata(md1, md2) 

130 expectedNames = ["int1", "float1", "string1"] + list(md2Names) 

131 self.assertEqual(result.getOrderedNames(), expectedNames) 

132 md2NameSet = set(md2Names) 

133 for name in result.getOrderedNames(): 

134 if name in md2NameSet: 

135 self.assertEqual(result.getScalar(name), md2.getArray(name)[-1]) 

136 else: 

137 self.assertEqual(result.getScalar(name), md1.getArray(name)[-1]) 

138 

139 # input should be unchanged 

140 self.assertMetadataEqual(md1, md1Copy) 

141 self.assertMetadataEqual(md2, md2Copy) 

142 

143 

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

145 pass 

146 

147 

148def setup_module(module): 

149 lsst.utils.tests.init() 

150 

151 

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

153 import sys 

154 setup_module(sys.modules[__name__]) 

155 unittest.main()