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

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

# 

# 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 math 

import os 

import unittest 

 

import lsst.geom 

import lsst.afw.geom as afwGeom 

import lsst.afw.table as afwTable 

import lsst.utils.tests 

import lsst.pex.exceptions as pexExcept 

from lsst.meas.algorithms import LoadReferenceObjectsTask 

import lsst.meas.astrom.sip.genDistortedImage as distort 

import lsst.meas.astrom as measAstrom 

 

 

class TestMatchOptimisticB(unittest.TestCase): 

 

def setUp(self): 

 

self.config = measAstrom.MatchOptimisticBTask.ConfigClass() 

self.matchOptimisticB = measAstrom.MatchOptimisticBTask(config=self.config) 

self.wcs = afwGeom.makeSkyWcs(crpix=lsst.geom.Point2D(791.4, 559.7), 

crval=lsst.geom.SpherePoint(36.930640, -4.939560, lsst.geom.degrees), 

cdMatrix=afwGeom.makeCdMatrix(scale=5.17e-5*lsst.geom.degrees)) 

self.distortedWcs = self.wcs 

 

self.filename = os.path.join(os.path.dirname(__file__), "cat.xy.fits") 

self.tolArcsec = .4 

self.tolPixel = .1 

 

def tearDown(self): 

del self.config 

del self.matchOptimisticB 

del self.wcs 

del self.distortedWcs 

 

def testLinearXDistort(self): 

self.singleTestInstance(self.filename, distort.linearXDistort) 

 

def testLinearYDistort(self): 

self.singleTestInstance(self.filename, distort.linearYDistort) 

 

def testQuadraticDistort(self): 

self.singleTestInstance(self.filename, distort.quadraticDistort) 

 

def testLargeDistortion(self): 

# This transform is about as extreme as I can get: 

# using 0.0005 in the last value appears to produce numerical issues. 

# It produces a maximum deviation of 459 pixels, which should be sufficient. 

pixelsToTanPixels = afwGeom.makeRadialTransform([0.0, 1.1, 0.0004]) 

self.distortedWcs = afwGeom.makeModifiedWcs(pixelTransform=pixelsToTanPixels, 

wcs=self.wcs, 

modifyActualPixels=False) 

 

def applyDistortion(src): 

out = src.table.copyRecord(src) 

out.set(out.table.getCentroidKey(), 

pixelsToTanPixels.applyInverse(src.getCentroid())) 

return out 

 

self.singleTestInstance(self.filename, applyDistortion) 

 

def singleTestInstance(self, filename, distortFunc, doPlot=False): 

sourceCat = self.loadSourceCatalog(self.filename) 

refCat = self.computePosRefCatalog(sourceCat) 

distortedCat = distort.distortList(sourceCat, distortFunc) 

 

89 ↛ 90line 89 didn't jump to line 90, because the condition on line 89 was never true if doPlot: 

import matplotlib.pyplot as plt 

undistorted = [self.wcs.skyToPixel(self.distortedWcs.pixelToSky(ss.getCentroid())) for 

ss in distortedCat] 

refs = [self.wcs.skyToPixel(ss.getCoord()) for ss in refCat] 

 

def plot(catalog, symbol): 

plt.plot([ss.getX() for ss in catalog], [ss.getY() for ss in catalog], symbol) 

 

# plot(sourceCat, 'k+') # Original positions: black + 

plot(distortedCat, 'b+') # Distorted positions: blue + 

plot(undistorted, 'g+') # Undistorted positions: green + 

plot(refs, 'rx') # Reference catalog: red x 

# The green + should overlap with the red x, because that's how matchOptimisticB does it. 

# The black + happens to overlap with those also, but that's beside the point. 

plt.show() 

 

sourceCat = distortedCat 

 

matchRes = self.matchOptimisticB.matchObjectsToSources( 

refCat=refCat, 

sourceCat=sourceCat, 

wcs=self.distortedWcs, 

refFluxField="r_flux", 

) 

matches = matchRes.matches 

115 ↛ 116line 115 didn't jump to line 116, because the condition on line 115 was never true if doPlot: 

measAstrom.plotAstrometry(matches=matches, refCat=refCat, sourceCat=sourceCat) 

self.assertEqual(len(matches), 183) 

 

refCoordKey = afwTable.CoordKey(refCat.schema["coord"]) 

srcCoordKey = afwTable.CoordKey(sourceCat.schema["coord"]) 

refCentroidKey = afwTable.Point2DKey(refCat.getSchema()["centroid"]) 

maxDistErr = 0*lsst.geom.radians 

for refObj, source, distRad in matches: 

sourceCoord = source.get(srcCoordKey) 

refCoord = refObj.get(refCoordKey) 

predDist = sourceCoord.separation(refCoord) 

distErr = abs(predDist - distRad*lsst.geom.radians) 

maxDistErr = max(distErr, maxDistErr) 

 

130 ↛ 131line 130 didn't jump to line 131, because the condition on line 130 was never true if refObj.getId() != source.getId(): 

refCentroid = refObj.get(refCentroidKey) 

sourceCentroid = source.getCentroid() 

radius = math.hypot(*(refCentroid - sourceCentroid)) 

self.fail("ID mismatch: %s at %s != %s at %s; error = %0.1f pix" % 

(refObj.getId(), refCentroid, source.getId(), sourceCentroid, radius)) 

 

self.assertLess(maxDistErr.asArcseconds(), 1e-7) 

 

def computePosRefCatalog(self, sourceCat): 

"""Generate a position reference catalog from a source catalog 

""" 

minimalPosRefSchema = LoadReferenceObjectsTask.makeMinimalSchema( 

filterNameList=["r"], 

addFluxErr=True, 

) 

refCat = afwTable.SimpleCatalog(minimalPosRefSchema) 

for source in sourceCat: 

refObj = refCat.addNew() 

refObj.setCoord(source.getCoord()) 

refObj.set("centroid_x", source.getX()) 

refObj.set("centroid_y", source.getY()) 

refObj.set("hasCentroid", True) 

refObj.set("r_flux", source.get("slot_ApFlux_flux")) 

refObj.set("r_fluxErr", source.get("slot_ApFlux_fluxErr")) 

refObj.setId(source.getId()) 

return refCat 

 

def loadSourceCatalog(self, filename): 

"""Load a list of xy points from a file, set coord, and return a SourceSet of points 

""" 

sourceCat = afwTable.SourceCatalog.readFits(filename) 

aliasMap = sourceCat.schema.getAliasMap() 

aliasMap.set("slot_ApFlux", "base_PsfFlux") 

fluxKey = sourceCat.schema["slot_ApFlux_flux"].asKey() 

fluxErrKey = sourceCat.schema["slot_ApFlux_fluxErr"].asKey() 

 

# print("schema=", sourceCat.schema) 

 

# Source x,y positions are ~ (500,1500) x (500,1500) 

centroidKey = sourceCat.table.getCentroidKey() 

for src in sourceCat: 

adjCentroid = src.get(centroidKey) - lsst.geom.Extent2D(500, 500) 

src.set(centroidKey, adjCentroid) 

src.set(fluxKey, 1000) 

src.set(fluxErrKey, 1) 

 

# Set catalog coord 

for src in sourceCat: 

src.updateCoord(self.wcs) 

return sourceCat 

 

def testArgumentErrors(self): 

"""Test argument sanity checking in matchOptimisticB 

""" 

matchControl = measAstrom.MatchOptimisticBControl() 

 

sourceCat = self.loadSourceCatalog(self.filename) 

emptySourceCat = afwTable.SourceCatalog(sourceCat.schema) 

 

refCat = self.computePosRefCatalog(sourceCat) 

emptyRefCat = afwTable.SimpleCatalog(refCat.schema) 

 

with self.assertRaises(pexExcept.InvalidParameterError): 

measAstrom.matchOptimisticB( 

emptyRefCat, 

sourceCat, 

matchControl, 

self.wcs, 

0, 

) 

with self.assertRaises(pexExcept.InvalidParameterError): 

measAstrom.matchOptimisticB( 

refCat, 

emptySourceCat, 

matchControl, 

self.wcs, 

0, 

) 

with self.assertRaises(pexExcept.InvalidParameterError): 

measAstrom.matchOptimisticB( 

refCat, 

sourceCat, 

matchControl, 

self.wcs, 

len(refCat), 

) 

with self.assertRaises(pexExcept.InvalidParameterError): 

measAstrom.matchOptimisticB( 

refCat, 

sourceCat, 

matchControl, 

self.wcs, 

-1, 

) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

 

lsst.utils.tests.init() 

unittest.main()