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

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

# 

# 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/>. 

# 

 

""" 

Tests for offsetting images in (dx, dy) 

 

Run with: 

python offsetImage.py 

or 

python 

>>> import offsetImage; offsetImage.run() 

""" 

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.display.ds9 as ds9 

 

try: 

type(display) 

except NameError: 

display = False 

 

 

class OffsetImageTestCase(unittest.TestCase): 

"""A test case for offsetImage""" 

 

def setUp(self): 

self.inImage = afwImage.ImageF(200, 100) 

self.background = 200 

self.inImage.set(self.background) 

 

def tearDown(self): 

del self.inImage 

 

def testSetFluxConvervation(self): 

"""Test that flux is preserved""" 

 

for algorithm in ("lanczos5", "bilinear", "nearest"): 

outImage = afwMath.offsetImage(self.inImage, 0, 0, algorithm) 

self.assertEqual(outImage.get(50, 50), self.background) 

 

outImage = afwMath.offsetImage(self.inImage, 0.5, 0, algorithm) 

self.assertAlmostEqual(outImage.get(50, 50), self.background, 4) 

 

outImage = afwMath.offsetImage(self.inImage, 0.5, 0.5, algorithm) 

self.assertAlmostEqual(outImage.get(50, 50), self.background, 4) 

 

def testSetIntegerOffset(self): 

"""Test that we can offset by positive and negative amounts""" 

 

self.inImage.set(50, 50, 400) 

 

78 ↛ 79line 78 didn't jump to line 79, because the condition on line 78 was never true if False and display: 

frame = 0 

ds9.mtv(self.inImage, frame=frame) 

ds9.pan(50, 50, frame=frame) 

ds9.dot("+", 50, 50, frame=frame) 

 

for algorithm in ("lanczos5", "bilinear", "nearest"): 

for delta in [-0.49, 0.51]: 

for dx, dy in [(2, 3), (-2, 3), (-2, -3), (2, -3)]: 

outImage = afwMath.offsetImage( 

self.inImage, dx + delta, dy + delta, algorithm) 

 

90 ↛ 91line 90 didn't jump to line 91, because the condition on line 90 was never true if False and display: 

frame += 1 

ds9.mtv(outImage, frame=frame) 

ds9.pan(50, 50, frame=frame) 

ds9.dot("+", 50 + dx + delta - outImage.getX0(), 50 + dy + delta - outImage.getY0(), 

frame=frame) 

 

def calcGaussian(self, im, x, y, amp, sigma1): 

"""Insert a Gaussian into the image centered at (x, y)""" 

 

x = x - im.getX0() 

y = y - im.getY0() 

 

for ix in range(im.getWidth()): 

for iy in range(im.getHeight()): 

r2 = math.pow(x - ix, 2) + math.pow(y - iy, 2) 

val = math.exp(-r2/(2.0*pow(sigma1, 2))) 

im.set(ix, iy, amp*val) 

 

def testOffsetGaussian(self): 

"""Insert a Gaussian, offset, and check the residuals""" 

 

size = 50 

refIm = afwImage.ImageF(size, size) 

unshiftedIm = afwImage.ImageF(size, size) 

 

xc, yc = size/2.0, size/2.0 

 

amp, sigma1 = 1.0, 3 

 

# 

# Calculate Gaussian directly at (xc, yc) 

# 

self.calcGaussian(refIm, xc, yc, amp, sigma1) 

 

for dx in (-55.5, -1.500001, -1.5, -1.499999, -1.00001, -1.0, -0.99999, -0.5, 

0.0, 0.5, 0.99999, 1.0, 1.00001, 1.499999, 1.5, 1.500001, 99.3): 

for dy in (-3.7, -1.500001, -1.5, -1.499999, -1.00001, -1.0, -0.99999, -0.5, 

0.0, 0.5, 0.99999, 1.0, 1.00001, 1.499999, 1.5, 1.500001, 2.99999): 

dOrigX, dOrigY, dFracX, dFracY = getOrigFracShift(dx, dy) 

self.calcGaussian(unshiftedIm, xc - dFracX, 

yc - dFracY, amp, sigma1) 

 

for algorithm, maxMean, maxLim in ( 

("lanczos5", 1e-8, 0.0015), 

("bilinear", 1e-8, 0.03), 

("nearest", 1e-8, 0.2), 

): 

im = afwImage.ImageF(size, size) 

im = afwMath.offsetImage(unshiftedIm, dx, dy, algorithm) 

 

141 ↛ 142line 141 didn't jump to line 142, because the condition on line 141 was never true if display: 

ds9.mtv(im, frame=0) 

 

im -= refIm 

 

146 ↛ 147line 146 didn't jump to line 147, because the condition on line 146 was never true if display: 

ds9.mtv(im, frame=1) 

 

imArr = im.getArray() 

imGoodVals = np.ma.array( 

imArr, copy=False, mask=np.isnan(imArr)).compressed() 

 

try: 

imXY0 = tuple(im.getXY0()) 

self.assertEqual(imXY0, (dOrigX, dOrigY)) 

self.assertLess(abs(imGoodVals.mean()), maxMean*amp) 

self.assertLess(abs(imGoodVals.max()), maxLim*amp) 

self.assertLess(abs(imGoodVals.min()), maxLim*amp) 

except Exception: 

print("failed on algorithm=%s; dx = %s; dy = %s" % 

(algorithm, dx, dy)) 

raise 

 

# the following would be preferable if there was an easy way to NaN pixels 

# 

# stats = afwMath.makeStatistics(im, afwMath.MEAN | afwMath.MAX | afwMath.MIN) 

# 

# if not False: 

# print "mean = %g, min = %g, max = %g" % (stats.getValue(afwMath.MEAN), 

# stats.getValue(afwMath.MIN), 

# stats.getValue(afwMath.MAX)) 

# 

# self.assertTrue(abs(stats.getValue(afwMath.MEAN)) < 1e-7) 

# self.assertTrue(abs(stats.getValue(afwMath.MIN)) < 1.2e-3*amp) 

# self.assertTrue(abs(stats.getValue(afwMath.MAX)) < 1.2e-3*amp) 

 

 

def getOrigFracShift(dx, dy): 

"""Return the predicted integer shift to XY0 and the fractional shift that offsetImage will use 

 

offsetImage preserves the origin if dx and dy both < 1 pixel; larger shifts are to the nearest pixel. 

""" 

if (abs(dx) < 1) and (abs(dy) < 1): 

return (0, 0, dx, dy) 

 

dOrigX = math.floor(dx + 0.5) 

dOrigY = math.floor(dy + 0.5) 

dFracX = dx - dOrigX 

dFracY = dy - dOrigY 

return (int(dOrigX), int(dOrigY), dFracX, dFracY) 

 

 

class TransformImageTestCase(unittest.TestCase): 

"""A test case for rotating images""" 

 

def setUp(self): 

self.inImage = afwImage.ImageF(20, 10) 

self.inImage.set(0, 0, 100) 

self.inImage.set(10, 0, 50) 

 

def tearDown(self): 

del self.inImage 

 

def testRotate(self): 

"""Test that we end up with the correct image after rotating by 90 degrees""" 

 

for nQuarter, x, y in [(0, 0, 0), 

(1, 9, 0), 

(2, 19, 9), 

(3, 0, 19)]: 

outImage = afwMath.rotateImageBy90(self.inImage, nQuarter) 

212 ↛ 213line 212 didn't jump to line 213, because the condition on line 212 was never true if display: 

ds9.mtv(outImage, frame=nQuarter, title="out %d" % nQuarter) 

self.assertEqual(self.inImage.get(0, 0), outImage.get(x, y)) 

 

def testFlip(self): 

"""Test that we end up with the correct image after flipping it""" 

 

frame = 2 

for flipLR, flipTB, x, y in [(True, False, 19, 0), 

(True, True, 19, 9), 

(False, True, 0, 9), 

(False, False, 0, 0)]: 

outImage = afwMath.flipImage(self.inImage, flipLR, flipTB) 

225 ↛ 226line 225 didn't jump to line 226, because the condition on line 225 was never true if display: 

ds9.mtv(outImage, frame=frame, title="%s %s" % 

(flipLR, flipTB)) 

frame += 1 

self.assertEqual(self.inImage.get(0, 0), outImage.get(x, y)) 

 

def testMask(self): 

"""Test that we can flip a Mask""" 

mask = afwImage.Mask(10, 20) 

# for a while, swig couldn't handle the resulting std::shared_ptr<Mask> 

afwMath.flipImage(mask, True, False) 

 

 

class BinImageTestCase(unittest.TestCase): 

"""A test case for binning images""" 

 

def setUp(self): 

pass 

 

def tearDown(self): 

pass 

 

def testBin(self): 

"""Test that we can bin images""" 

 

inImage = afwImage.ImageF(203, 131) 

inImage.set(1) 

bin = 4 

 

outImage = afwMath.binImage(inImage, bin) 

 

self.assertEqual(outImage.getWidth(), inImage.getWidth()//bin) 

self.assertEqual(outImage.getHeight(), inImage.getHeight()//bin) 

 

stats = afwMath.makeStatistics(outImage, afwMath.MAX | afwMath.MIN) 

self.assertEqual(stats.getValue(afwMath.MIN), 1) 

self.assertEqual(stats.getValue(afwMath.MAX), 1) 

 

def testBin2(self): 

"""Test that we can bin images anisotropically""" 

 

inImage = afwImage.ImageF(203, 131) 

val = 1 

inImage.set(val) 

binX, binY = 2, 4 

 

outImage = afwMath.binImage(inImage, binX, binY) 

 

self.assertEqual(outImage.getWidth(), inImage.getWidth()//binX) 

self.assertEqual(outImage.getHeight(), inImage.getHeight()//binY) 

 

stats = afwMath.makeStatistics(outImage, afwMath.MAX | afwMath.MIN) 

self.assertEqual(stats.getValue(afwMath.MIN), val) 

self.assertEqual(stats.getValue(afwMath.MAX), val) 

 

inImage.set(0) 

subImg = inImage.Factory(inImage, lsst.geom.BoxI(lsst.geom.PointI(4, 4), lsst.geom.ExtentI(4, 8)), 

afwImage.LOCAL) 

subImg.set(100) 

del subImg 

outImage = afwMath.binImage(inImage, binX, binY) 

 

287 ↛ 288line 287 didn't jump to line 288, because the condition on line 287 was never true if display: 

ds9.mtv(inImage, frame=2, title="unbinned") 

ds9.mtv(outImage, frame=3, title="binned %dx%d" % (binX, binY)) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()