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

# 

# LSST Data Management System 

# Copyright 2008-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 

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

import unittest 

 

import numpy as np 

 

import lsst.geom 

import lsst.afw.math 

import lsst.afw.geom 

import lsst.afw.image 

import lsst.meas.algorithms 

import lsst.pex.exceptions 

import lsst.utils.tests 

 

 

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

 

def makeRandomWcs(self, crval, maxOffset=10.0): 

"""Make a random TAN Wcs that's complex enough to create an interesting test, by combining 

random offsets and rotations. We don't include any random scaling, because we don't want 

the Jacobian to be significantly different from one in these tests, as we want to compare 

warped images (which implicitly include the Jacobian) against the behavior of CoaddBoundedField 

(which intentionally does not). 

""" 

crpix = lsst.geom.Point2D(*np.random.uniform(low=-maxOffset, high=maxOffset, size=2)) 

scale = 0.01*lsst.geom.arcseconds 

orientation = np.pi*np.random.rand()*lsst.geom.radians 

cdMatrix = lsst.afw.geom.makeCdMatrix(scale=scale, orientation=orientation) 

return lsst.afw.geom.makeSkyWcs(crpix=crpix, crval=crval, cdMatrix=cdMatrix) 

 

def makeRandomField(self, bbox): 

"""Create a random ChebyshevBoundedField""" 

coefficients = np.random.randn(3, 3) 

# We add a positive DC offset to make sure our fields more likely to combine constructively; 

# this makes the test more stringent, because we get less accidental small numbers. 

coefficients[0, 0] = np.random.uniform(low=4, high=6) 

return lsst.afw.math.ChebyshevBoundedField(bbox, coefficients) 

 

def constructElements(self, validBox): 

"""Construct the elements of a CoaddBoundedField.""" 

np.random.seed(50) 

validPolygon = lsst.afw.geom.Polygon(lsst.geom.Box2D(validBox)) if validBox else None 

elements = [] 

validBoxes = [] 

 

for i in range(10): 

elements.append( 

lsst.meas.algorithms.CoaddBoundedField.Element( 

self.makeRandomField(self.elementBBox), 

self.makeRandomWcs(self.crval), 

validPolygon, 

np.random.uniform(low=0.5, high=1.5) 

) 

) 

validBoxes.append(validBox) 

return elements, validBoxes 

 

def setUp(self): 

self.crval = lsst.geom.SpherePoint(45.0, 45.0, lsst.geom.degrees) 

self.elementBBox = lsst.geom.Box2I(lsst.geom.Point2I(-50, -50), lsst.geom.Point2I(50, 50)) 

self.coaddWcs = self.makeRandomWcs(self.crval, maxOffset=0.0) 

self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(-75, -75), lsst.geom.Point2I(75, 75)) 

self.possibleValidBoxes = (None, lsst.geom.Box2I(lsst.geom.Point2I(-25, -25), 

lsst.geom.Point2I(25, 25))) 

 

def testEvaluate(self): 

"""Test the main implementation of CoaddBoundedField::evaluate() by creating images of 

each constituent field, warping them, and coadding them to form a check image. 

 

We run this test twice: once with a regular ValidPolygon, and once 

with the polygon undefined (ie, set to None); see DM-11008. 

""" 

def runTest(elements, validBoxes): 

field = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, elements, 0.0) 

coaddImage = lsst.afw.image.ImageF(self.bbox) 

warpCtrl = lsst.afw.math.WarpingControl("bilinear") 

weightMap = lsst.afw.image.ImageF(self.bbox) 

for element, validBox in zip(elements, validBoxes): 

elementImage = (lsst.afw.image.ImageF(validBox) if validBox 

else lsst.afw.image.ImageF(element.field.getBBox())) 

element.field.fillImage(elementImage, True) 

warp = lsst.afw.image.ImageF(self.bbox) 

lsst.afw.math.warpImage(warp, self.coaddWcs, elementImage, element.wcs, warpCtrl, 0.0) 

coaddImage.scaledPlus(element.weight, warp) 

warp.getArray()[warp.getArray() != 0.0] = element.weight 

weightMap += warp 

coaddImage /= weightMap 

coaddImage.getArray()[np.isnan(coaddImage.getArray())] = 0.0 

fieldImage = lsst.afw.image.ImageF(self.bbox) 

field.fillImage(fieldImage) 

 

# The coaddImage we're comparing to has artifacts at the edges of all the input exposures, 

# due to the interpolation, so we just check that the number of unequal pixels is small (<10%) 

# This can depend on the random number seed, so if a failure is seen, uncomment the line below 

# to enable a plot of the differences, and verify by eye that they look like edge artifacts. If 

# they do, just modify the seed (at the top of this file) or change number-of-pixels threshold 

# until the test passes. 

 

diff = np.abs(fieldImage.getArray() - coaddImage.getArray()) 

relTo = np.abs(fieldImage.getArray()) 

rtol = 1E-2 

atol = 1E-7 

bad = np.logical_and(diff > rtol*relTo, diff > atol) 

 

if False: # enable this to see a plot of the comparison (but it will always fail, since 

# it doesn't take into account the artifacts in coaddImage) 

self.assertFloatsAlmostEqual(fieldImage.getArray(), coaddImage.getArray(), plotOnFailure=True, 

rtol=rtol, atol=atol, relTo=relTo) 

 

self.assertLess(bad.sum(), 0.10*self.bbox.getArea()) 

 

for validBox in self.possibleValidBoxes: 

elements, validBoxes = self.constructElements(validBox) 

self.assertNotEqual(elements[0], elements[1]) 

runTest(elements, validBoxes) 

 

def testEquality(self): 

def runTest(elements, validBoxes): 

field1 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, elements, 0.0) 

field2 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, elements, 0.0) 

self.assertEqual(field1, field2) 

 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(-75, -75), lsst.geom.Point2I(75, 75)) 

field3 = lsst.meas.algorithms.CoaddBoundedField(bbox, self.coaddWcs, elements, 0.0) 

self.assertEqual(field1, field3) 

 

field4 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, elements, 0.0) 

self.assertEqual(field1, field4) 

 

# NOTE: make a copy of the list; [:] to copy segfaults, 

# copy.copy() doesn't behave nicely on py2 w/our pybind11 objects, 

# and .elements.copy() doesn't exist on py2. 

elementsCopy = list(elements) 

field5 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, elementsCopy, 0.0) 

self.assertEqual(field1, field5) 

 

# inequality tests below here 

field6 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, elements, 3.0) 

self.assertNotEqual(field1, field6) 

field7 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, [], 0.0) 

self.assertNotEqual(field1, field7) 

 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(-74, -75), lsst.geom.Point2I(75, 75)) 

field8 = lsst.meas.algorithms.CoaddBoundedField(bbox, self.coaddWcs, elements, 0.0) 

self.assertNotEqual(field1, field8) 

 

crval = lsst.geom.SpherePoint(45.0, 45.0, lsst.geom.degrees) 

coaddWcs = self.makeRandomWcs(crval, maxOffset=2.0) 

field9 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, coaddWcs, elements, 0.0) 

self.assertNotEqual(field1, field9) 

 

elementsCopy = list(elements) 

elementsCopy[2].weight = 1000000 

field10 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, elementsCopy, 0.0) 

self.assertNotEqual(field1, field10) 

elementsCopy = list(elements) 

elementsCopy.pop(0) 

field11 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, elementsCopy, 0.0) 

self.assertNotEqual(field1, field11) 

 

for validBox in self.possibleValidBoxes: 

runTest(*self.constructElements(validBox)) 

 

def testPersistence(self): 

"""Test that we can round-trip a CoaddBoundedField through FITS.""" 

def runTest(elements, validBoxes): 

filename = "testCoaddBoundedField.fits" 

field1 = lsst.meas.algorithms.CoaddBoundedField(self.bbox, self.coaddWcs, elements, 0.0) 

field1.writeFits(filename) 

field2 = lsst.meas.algorithms.CoaddBoundedField.readFits(filename) 

self.assertEqual(field1, field2) 

elements2 = field2.getElements() 

self.assertEqual(elements, elements2) 

for el1, el2 in zip(elements, elements2): 

self.assertEqual(el1, el2) 

self.assertEqual(el1.field, el2.field) 

self.assertEqual(el1.wcs, el2.wcs) 

self.assertWcsAlmostEqualOverBBox(el1.wcs, el2.wcs, self.bbox, 

maxDiffPix=0, maxDiffSky=0*lsst.geom.arcseconds) 

self.assertEqual(el1.validPolygon, el2.validPolygon) 

self.assertEqual(el1.weight, el2.weight) 

 

self.assertEqual(field1.getCoaddWcs(), field2.getCoaddWcs()) 

self.assertWcsAlmostEqualOverBBox(self.coaddWcs, field2.getCoaddWcs(), self.bbox, 

maxDiffPix=0, maxDiffSky=0*lsst.geom.arcseconds) 

self.assertEqual(field1.getDefault(), field2.getDefault()) 

image1 = lsst.afw.image.ImageD(self.bbox) 

image2 = lsst.afw.image.ImageD(self.bbox) 

field1.fillImage(image1) 

field2.fillImage(image2) 

self.assertImagesEqual(image1, image2) 

os.remove(filename) 

 

for validBox in self.possibleValidBoxes: 

runTest(*self.constructElements(validBox)) 

 

def tearDown(self): 

del self.coaddWcs 

del self.bbox 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()