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

239

240

# 

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

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

import lsst.afw.math.detail as mathDetail 

from lsst.log import Log 

 

# Change the level to Log.DEBUG to see debug messages 

Log.getLogger("TRACE5.afw.math.convolve").setLevel(Log.INFO) 

 

LocNameDict = { 

mathDetail.KernelImagesForRegion.BOTTOM_LEFT: "BOTTOM_LEFT", 

mathDetail.KernelImagesForRegion.BOTTOM_RIGHT: "BOTTOM_RIGHT", 

mathDetail.KernelImagesForRegion.TOP_LEFT: "TOP_LEFT", 

mathDetail.KernelImagesForRegion.TOP_RIGHT: "TOP_RIGHT", 

} 

 

NameLocDict = dict((name, loc) for (loc, name) in LocNameDict.items()) 

 

 

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

 

def setUp(self): 

boxCorner = lsst.geom.Point2I(11, 50) 

boxExtent = lsst.geom.Extent2I(100, 99) 

self.bbox = lsst.geom.Box2I(boxCorner, boxExtent) 

self.xy0 = lsst.geom.Point2I(100, 251) 

self.kernel = self.makeKernel() 

 

def tearDown(self): 

self.bbox = None 

self.kernel = None 

 

def assertRegionCorrect(self, region): 

"""Assert that a region has correct corner images 

 

This test is only relevant for operations that try to reuse the image array data 

""" 

regionCopy = mathDetail.KernelImagesForRegion( 

region.getKernel(), region.getBBox(), region.getXY0(), region.getDoNormalize()) 

 

for location in ( 

region.BOTTOM_LEFT, 

region.BOTTOM_RIGHT, 

region.TOP_LEFT, 

region.TOP_RIGHT, 

): 

actImage = region.getImage(location) 

actImArr = actImage.getArray().transpose().copy() 

desImage = regionCopy.getImage(location) 

desImArr = desImage.getArray().transpose().copy() 

actImArr -= desImArr 

79 ↛ 80line 79 didn't jump to line 80, because the condition on line 79 was never true if not np.allclose(actImArr, 0): 

actImage.writeFits("actImage%s.fits" % (location,)) 

desImage.writeFits("desImage%s.fits" % (location,)) 

self.fail("failed on location %s" % (location,)) 

 

def makeKernel(self): 

kCols = 7 

kRows = 6 

 

# create spatial model 

sFunc = afwMath.PolynomialFunction2D(1) 

 

minSigma = 0.1 

maxSigma = 3.0 

 

# spatial parameters are a list of entries, one per kernel parameter; 

# each entry is a list of spatial parameters 

xSlope = (maxSigma - minSigma) / self.bbox.getWidth() 

ySlope = (maxSigma - minSigma) / self.bbox.getHeight() 

xOrigin = minSigma - (self.xy0[0] * xSlope) 

yOrigin = minSigma - (self.xy0[1] * ySlope) 

sParams = ( 

(xOrigin, xSlope, 0.0), 

(yOrigin, 0.0, ySlope), 

(0.0, 0.0, 0.0), 

) 

 

kFunc = afwMath.GaussianFunction2D(1.0, 1.0, 0.0) 

kernel = afwMath.AnalyticKernel(kCols, kRows, kFunc, sFunc) 

kernel.setSpatialParameters(sParams) 

return kernel 

 

def testDoNormalize(self): 

"""Test getDoNormalize 

""" 

kernel = self.makeKernel() 

for doNormalize in (False, True): 

region = mathDetail.KernelImagesForRegion( 

kernel, self.bbox, self.xy0, doNormalize) 

self.assertEqual(region.getDoNormalize(), doNormalize) 

 

def testGetPixelIndex(self): 

"""Test getPixelIndex method 

""" 

region = mathDetail.KernelImagesForRegion( 

self.kernel, self.bbox, self.xy0, False) 

leftInd = self.bbox.getMinX() 

rightInd = self.bbox.getMaxX() + 1 

bottomInd = self.bbox.getMinY() 

topInd = self.bbox.getMaxY() + 1 

int(round((leftInd + rightInd) / 2.0)) 

int(round((bottomInd + topInd) / 2.0)) 

 

for location, desIndex in ( 

(region.BOTTOM_LEFT, (leftInd, bottomInd)), 

(region.BOTTOM_RIGHT, (rightInd, bottomInd)), 

(region.TOP_LEFT, (leftInd, topInd)), 

(region.TOP_RIGHT, (rightInd, topInd)), 

): 

desPixIndex = lsst.geom.Point2I(desIndex[0], desIndex[1]) 

self.assertEqual(region.getPixelIndex(location), desPixIndex, "getPixelIndex(%s) = %s != %s" % 

(LocNameDict[location], region.getPixelIndex(location), desPixIndex)) 

 

def testComputeNextRow(self): 

"""Test computeNextRow method and the resulting RowOfKernelImagesForRegion 

""" 

nx = 6 

ny = 5 

regionRow = mathDetail.RowOfKernelImagesForRegion(nx, ny) 

self.assertFalse(regionRow.hasData()) 

self.assertFalse(regionRow.isLastRow()) 

self.assertEqual(regionRow.getYInd(), -1) 

 

region = mathDetail.KernelImagesForRegion( 

self.kernel, self.bbox, self.xy0, False) 

floatWidth = self.bbox.getWidth() / float(nx) 

validWidths = (int(math.floor(floatWidth)), int(math.ceil(floatWidth))) 

floatHeight = self.bbox.getHeight() / float(ny) 

validHeights = (int(math.floor(floatHeight)), 

int(math.ceil(floatHeight))) 

 

totalHeight = 0 

prevBBox = None 

prevFirstBBox = None 

for yInd in range(ny): 

rowWidth = 0 

isOK = region.computeNextRow(regionRow) 

self.assertTrue(isOK) 

self.assertTrue(regionRow.hasData()) 

self.assertEqual(regionRow.isLastRow(), (yInd + 1 >= ny)) 

self.assertEqual(regionRow.getYInd(), yInd) 

firstBBox = regionRow.getRegion(0).getBBox() 

self.assertEqual(firstBBox.getMinX(), self.bbox.getMinX()) 

if yInd == 0: 

self.assertEqual(firstBBox.getMinY(), self.bbox.getMinY()) 

firstBBoxHeight = firstBBox.getHeight() 

self.assertTrue(firstBBoxHeight in validHeights) 

totalHeight += firstBBoxHeight 

if yInd > 0: 

self.assertEqual(firstBBox.getMinY(), 

prevFirstBBox.getMaxY() + 1) 

if yInd == ny - 1: 

self.assertEqual(firstBBox.getMaxY(), self.bbox.getMaxY()) 

prevFirstBBox = firstBBox 

for xInd in range(nx): 

subregion = regionRow.getRegion(xInd) 

try: 

self.assertRegionCorrect(subregion) 

except Exception: 

print("failed on xInd=%s, yInd=%s" % (xInd, yInd)) 

raise 

bbox = subregion.getBBox() 

rowWidth += bbox.getWidth() 

self.assertTrue(bbox.getWidth() in validWidths) 

self.assertEqual(bbox.getHeight(), firstBBoxHeight) 

if xInd > 0: 

self.assertEqual(bbox.getMinX(), prevBBox.getMaxX() + 1) 

self.assertEqual(bbox.getMinY(), prevBBox.getMinY()) 

self.assertEqual(bbox.getMaxY(), prevBBox.getMaxY()) 

if xInd == nx - 1: 

self.assertEqual(bbox.getMaxX(), self.bbox.getMaxX()) 

prevBBox = bbox 

self.assertEqual(rowWidth, self.bbox.getWidth()) 

self.assertEqual(totalHeight, self.bbox.getHeight()) 

self.assertTrue(not region.computeNextRow(regionRow)) 

 

def testExactImages(self): 

"""Confirm that kernel image at each location is correct 

""" 

desImage = afwImage.ImageD(lsst.geom.Extent2I( 

self.kernel.getWidth(), self.kernel.getHeight())) 

 

for doNormalize in (False, True): 

region = mathDetail.KernelImagesForRegion( 

self.kernel, self.bbox, self.xy0, doNormalize) 

for location in ( 

region.BOTTOM_LEFT, 

region.BOTTOM_RIGHT, 

region.TOP_LEFT, 

region.TOP_RIGHT, 

): 

pixelIndex = region.getPixelIndex(location) 

xPos = afwImage.indexToPosition(pixelIndex[0] + self.xy0[0]) 

yPos = afwImage.indexToPosition(pixelIndex[1] + self.xy0[1]) 

self.kernel.computeImage(desImage, doNormalize, xPos, yPos) 

 

actImage = region.getImage(location) 

msg = "exact image(%s) incorrect" % (LocNameDict[location],) 

self.assertImagesAlmostEqual(actImage, desImage, msg=msg) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()