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

156

157

158

159

160

161

162

163

164

165

166

167

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 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.geom 

import lsst.afw.table as afwTable 

import lsst.utils.tests 

 

 

class MatchXyTest(unittest.TestCase): 

"""Test that matching sources by centroid works as expected, 

even when some of the centroids contain NaN. 

""" 

 

def setUp(self): 

nan = float('nan') 

self.schema = afwTable.SourceTable.makeMinimalSchema() 

centroidKey = afwTable.Point2DKey.addFields( 

self.schema, "cen", "center", "pixels") 

self.table = afwTable.SourceTable.make(self.schema) 

self.table.defineCentroid("cen") 

idKey = self.table.getIdKey() 

self.cat1 = afwTable.SourceCatalog(self.table) 

self.cat2 = afwTable.SourceCatalog(self.table) 

self.nobj = 10 

self.nUniqueMatch = 0 

self.matchRadius = 0.1 # Matching radius to use in tests (pixels) 

for i in range(self.nobj): 

j = self.nobj - i - 1 

r1, r2 = self.cat1.addNew(), self.cat2.addNew() 

r1.set(idKey, i) 

r2.set(idKey, self.nobj + j) 

if i % 3 != 0: 

# These will provide the exact matches, though the two records we're setting right now won't 

# match each other (because cat2 counts down in reverse). 

r1.set(centroidKey, lsst.geom.Point2D(i, i)) 

r2.set(centroidKey, lsst.geom.Point2D(j, j)) 

self.nUniqueMatch += 1 

elif i == 3: 

# Deliberately offset position in cat2 by 2 pixels and a bit so that it will match a 

# different source. This gives us an extra match when we're not just getting the closest. 

# The "2 pixels" makes it line up with a different source. 

# The "a bit" is half a match radius, so that it's still within the matching radius, but it 

# doesn't match another source exactly. If it matches another source exactly, then it's not 

# clear which one will be taken as the match (in fact, it 

# appears to depend on the compiler). 

offset = 2 + 0.5*self.matchRadius 

r1.set(centroidKey, lsst.geom.Point2D(i, i)) 

r2.set(centroidKey, lsst.geom.Point2D(j + offset, j + offset)) 

else: 

# Test that we can handle NANs 

r1.set(centroidKey, lsst.geom.Point2D(nan, nan)) 

r2.set(centroidKey, lsst.geom.Point2D(nan, nan)) 

 

def tearDown(self): 

del self.cat2 

del self.cat1 

del self.table 

del self.schema 

 

def testMatchXy(self): 

matches = afwTable.matchXy(self.cat1, self.cat2, self.matchRadius) 

self.assertEqual(len(matches), self.nUniqueMatch) 

 

for m in matches: 

self.assertEqual(m.first.getId() + self.nobj, m.second.getId()) 

self.assertEqual(m.distance, 0.0) 

 

def testMatchXyMatchControl(self): 

"""Test using MatchControl to return all matches 

 

Also tests closest==False at the same time 

""" 

for closest in (True, False): 

for includeMismatches in (True, False): 

mc = afwTable.MatchControl() 

mc.findOnlyClosest = closest 

mc.includeMismatches = includeMismatches 

matches = afwTable.matchXy( 

self.cat1, self.cat2, self.matchRadius, mc) 

 

if False: 

for m in matches: 

print(closest, m.first.getId(), 

m.second.getId(), m.distance) 

 

if includeMismatches: 

catMatches = afwTable.SourceCatalog(self.table) 

catMismatches = afwTable.SourceCatalog(self.table) 

for m in matches: 

if m[1] is not None: 

if not any(x == m[0] for x in catMatches): 

catMatches.append(m[0]) 

else: 

catMismatches.append(m[0]) 

matches = afwTable.matchXy( 

catMatches, self.cat2, self.matchRadius, mc) 

mc.includeMismatches = False 

noMatches = afwTable.matchXy( 

catMismatches, self.cat2, self.matchRadius, mc) 

self.assertEqual(len(noMatches), 0) 

 

# If we're not getting only the closest match, then we get an extra match due to the 

# source we offset by 2 pixels and a bit. Everything else 

# should match exactly. 

self.assertEqual( 

len(matches), 

self.nUniqueMatch if closest else self.nUniqueMatch + 1) 

self.assertEqual( 

sum(1 for m in matches if m.distance == 0.0), 

self.nUniqueMatch) 

for m in matches: 

if closest: 

self.assertEqual(m.first.getId() + 

self.nobj, m.second.getId()) 

else: 

self.assertLessEqual(m.distance, self.matchRadius) 

 

def testSelfMatchXy(self): 

"""Test doing a self-matches""" 

for symmetric in (True, False): 

mc = afwTable.MatchControl() 

mc.symmetricMatch = symmetric 

matches = afwTable.matchXy(self.cat2, self.matchRadius, mc) 

 

if False: 

for m in matches: 

print(m.first.getId(), m.second.getId(), m.distance) 

 

# There is only one source that matches another source when we do a self-match: the one 

# offset by 2 pixels and a bit. 

# If we're getting symmetric matches, that multiplies the expected number by 2 because it 

# produces s1,s2 and s2,s1. 

self.assertEqual(len(matches), 2 if symmetric else 1) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()