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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

# 

# LSST Data Management System 

# Copyright 2017 LSST Corporation. 

# 

# This product includes software developed by the 

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

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

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

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

import unittest 

 

import lsst.utils.tests 

from lsst.daf.base import PropertyList 

from lsst.afw.fits import combineMetadata 

 

 

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

 

def assertMetadataEqual(self, md1, md2): 

names1 = md1.getOrderedNames() 

names2 = md2.getOrderedNames() 

self.assertEqual(names1, names2) 

for name in names1: 

item1 = md1.getArray(name) 

item2 = md2.getArray(name) 

self.assertEqual(item1, item2) 

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

 

def testNoConflicts(self): 

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

except COMMENT and HISTORY, which are combined 

""" 

md1 = PropertyList() 

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

md1.set("float1", 1.23) 

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

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

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

md1Copy = md1.deepCopy() 

 

md2 = PropertyList() 

md2.set("int2", 2) 

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

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

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

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

md2Copy = md2.deepCopy() 

 

result = combineMetadata(md1, md2) 

self.assertEqual(result.getOrderedNames(), 

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

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

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

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

for name in md1.getOrderedNames(): 

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

continue 

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

for name in md2.getOrderedNames(): 

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

continue 

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

 

# input should be unchanged 

self.assertMetadataEqual(md1, md1Copy) 

self.assertMetadataEqual(md2, md2Copy) 

 

def testIgnoreInvalid(self): 

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

""" 

md1 = PropertyList() 

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

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

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

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

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

md1Copy = md1.deepCopy() 

 

md2 = PropertyList() 

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

md2.set("COMMENT", 7) 

md2.set("HISTORY", 1.06) 

md2Copy = md2.deepCopy() 

 

result = combineMetadata(md1, md2) 

resultNames = result.getOrderedNames() 

self.assertEqual(resultNames, []) 

 

# input should be unchanged 

self.assertMetadataEqual(md1, md1Copy) 

self.assertMetadataEqual(md2, md2Copy) 

 

def testReplaceDuplicates(self): 

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

""" 

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

md1 = PropertyList() 

md1.set("int1", 5) 

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

md1.set("float1", 3.1) 

md1.set("itemb", 1.23) 

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

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

md1Copy = md1.deepCopy() 

 

md2 = PropertyList() 

md2.set("itemc", 2) 

md2.set("int2", 2) 

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

md2.set("float2", 2.34) 

md2.set("itema", 5.27) 

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

md2Names = md2.getOrderedNames() 

md2Copy = md2.deepCopy() 

 

result = combineMetadata(md1, md2) 

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

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

md2NameSet = set(md2Names) 

for name in result.getOrderedNames(): 

if name in md2NameSet: 

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

else: 

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

 

# input should be unchanged 

self.assertMetadataEqual(md1, md1Copy) 

self.assertMetadataEqual(md2, md2Copy) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

import sys 

setup_module(sys.modules[__name__]) 

unittest.main()