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

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

# 

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

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.daf.base as dafBase 

import lsst.geom 

import lsst.afw.geom as afwGeom 

import lsst.afw.image as afwImage 

from lsst.afw.image.testUtils import imagesDiffer 

from lsst.afw.geom.utils import _compareWcsOverBBox 

 

 

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

"""Test test methods added to lsst.utils.tests.TestCase 

""" 

def testAssertWcsAlmostEqualOverBBox(self): 

"""Test assertWcsAlmostEqualOverBBox and wcsAlmostEqualOverBBox""" 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

lsst.geom.Extent2I(3001, 3001)) 

ctrPix = lsst.geom.Point2I(1500, 1500) 

metadata = dafBase.PropertySet() 

metadata.set("RADESYS", "FK5") 

metadata.set("EQUINOX", 2000.0) 

metadata.set("CTYPE1", "RA---TAN") 

metadata.set("CTYPE2", "DEC--TAN") 

metadata.set("CUNIT1", "deg") 

metadata.set("CUNIT2", "deg") 

metadata.set("CRVAL1", 215.5) 

metadata.set("CRVAL2", 53.0) 

metadata.set("CRPIX1", ctrPix[0] + 1) 

metadata.set("CRPIX2", ctrPix[1] + 1) 

metadata.set("CD1_1", 5.1e-05) 

metadata.set("CD1_2", 0.0) 

metadata.set("CD2_2", -5.1e-05) 

metadata.set("CD2_1", 0.0) 

wcs0 = lsst.afw.geom.makeSkyWcs(metadata, strip=False) 

metadata.set("CRVAL2", 53.000001) # tweak CRVAL2 for wcs1 

wcs1 = lsst.afw.geom.makeSkyWcs(metadata) 

 

self.assertWcsAlmostEqualOverBBox(wcs0, wcs0, bbox, 

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

self.assertTrue(afwGeom.wcsAlmostEqualOverBBox(wcs0, wcs0, bbox, 

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

 

self.assertWcsAlmostEqualOverBBox(wcs0, wcs1, bbox, 

maxDiffSky=0.04*lsst.geom.arcseconds, maxDiffPix=0.02) 

self.assertTrue(afwGeom.wcsAlmostEqualOverBBox(wcs0, wcs1, bbox, 

maxDiffSky=0.04*lsst.geom.arcseconds, maxDiffPix=0.02)) 

 

with self.assertRaises(AssertionError): 

self.assertWcsAlmostEqualOverBBox(wcs0, wcs1, bbox, 

maxDiffSky=0.001*lsst.geom.arcseconds, maxDiffPix=0.02) 

self.assertFalse(afwGeom.wcsAlmostEqualOverBBox(wcs0, wcs1, bbox, 

maxDiffSky=0.001*lsst.geom.arcseconds, 

maxDiffPix=0.02)) 

 

with self.assertRaises(AssertionError): 

self.assertWcsAlmostEqualOverBBox(wcs0, wcs1, bbox, 

maxDiffSky=0.04*lsst.geom.arcseconds, maxDiffPix=0.001) 

self.assertFalse(afwGeom.wcsAlmostEqualOverBBox(wcs0, wcs1, bbox, 

maxDiffSky=0.04*lsst.geom.arcseconds, 

maxDiffPix=0.001)) 

 

# check that doShortCircuit works in the private implementation 

errStr1 = _compareWcsOverBBox(wcs0, wcs1, bbox, 

maxDiffSky=0.001*lsst.geom.arcseconds, maxDiffPix=0.001, 

doShortCircuit=False) 

errStr2 = _compareWcsOverBBox(wcs0, wcs1, bbox, 

maxDiffSky=0.001*lsst.geom.arcseconds, maxDiffPix=0.001, 

doShortCircuit=True) 

self.assertNotEqual(errStr1, errStr2) 

 

def checkMaskedImage(self, mi): 

"""Run assertImage-like function tests on a masked image 

 

Compare the masked image to itself, then alter copies and check that the altered copy 

is or is not nearly equal the original, depending on the amount of change, rtol and atol 

""" 

epsilon = 1e-5 # margin to avoid roundoff error 

 

mi0 = mi.Factory(mi, True) # deep copy 

mi1 = mi.Factory(mi, True) 

 

# a masked image should be exactly equal to itself 

self.assertMaskedImagesEqual(mi0, mi1) 

self.assertMaskedImagesEqual(mi1, mi0) 

self.assertMaskedImagesAlmostEqual(mi0, mi1, atol=0, rtol=0) 

self.assertMaskedImagesAlmostEqual(mi0, mi1, atol=0, rtol=0) 

self.assertMaskedImagesAlmostEqual( 

mi0.getArrays(), mi1, atol=0, rtol=0) 

self.assertMaskedImagesAlmostEqual( 

mi0, mi1.getArrays(), atol=0, rtol=0) 

self.assertMaskedImagesAlmostEqual( 

mi0.getArrays(), mi1.getArrays(), atol=0, rtol=0) 

for getName in ("getImage", "getVariance"): 

plane0 = getattr(mi0, getName)() 

plane1 = getattr(mi1, getName)() 

self.assertImagesEqual(plane0, plane1) 

self.assertImagesEqual(plane1, plane0) 

self.assertImagesAlmostEqual(plane0, plane1, atol=0, rtol=0) 

self.assertImagesAlmostEqual(plane1, plane0, atol=0, rtol=0) 

self.assertImagesAlmostEqual( 

plane0.getArray(), plane1, atol=0, rtol=0) 

self.assertImagesAlmostEqual( 

plane0, plane1.getArray(), atol=0, rtol=0) 

self.assertImagesAlmostEqual( 

plane0.getArray(), plane1.getArray(), atol=0, rtol=0) 

self.assertMasksEqual(plane0, plane1) 

self.assertMasksEqual(plane1, plane0) 

self.assertMasksEqual(plane0.getArray(), plane1) 

self.assertMasksEqual(plane0, plane1.getArray()) 

self.assertMasksEqual(plane0.getArray(), plane1.getArray()) 

self.assertMasksEqual(mi0.getMask(), mi1.getMask()) 

self.assertMasksEqual(mi1.getMask(), mi0.getMask()) 

 

# alter image and variance planes and check the results 

for getName in ("getImage", "getVariance"): 

isFloat = getattr(mi, getName)().getArray().dtype.kind == "f" 

if isFloat: 

for errVal in (np.nan, np.inf, -np.inf): 

mi0 = mi.Factory(mi, True) 

mi1 = mi.Factory(mi, True) 

plane0 = getattr(mi0, getName)() 

plane1 = getattr(mi1, getName)() 

plane1[2, 2] = errVal 

with self.assertRaises(Exception): 

self.assertImagesAlmostEqual(plane0, plane1) 

with self.assertRaises(Exception): 

self.assertImagesAlmostEqual(plane0.getArray(), plane1) 

with self.assertRaises(Exception): 

self.assertImagesAlmostEqual(plane1, plane0) 

with self.assertRaises(Exception): 

self.assertMaskedImagesAlmostEqual(mi0, mi1) 

with self.assertRaises(Exception): 

self.assertMaskedImagesAlmostEqual( 

mi0, mi1.getArrays()) 

with self.assertRaises(Exception): 

self.assertMaskedImagesAlmostEqual(mi1, mi0) 

 

skipMask = mi.getMask().Factory(mi.getMask(), True) 

skipMaskArr = skipMask.getArray() 

skipMaskArr[:] = 0 

skipMaskArr[2, 2] = 1 

self.assertImagesAlmostEqual( 

plane0, plane1, skipMask=skipMaskArr, atol=0, rtol=0) 

self.assertImagesAlmostEqual( 

plane0, plane1, skipMask=skipMask, atol=0, rtol=0) 

self.assertMaskedImagesAlmostEqual( 

mi0, mi1, skipMask=skipMaskArr, atol=0, rtol=0) 

self.assertMaskedImagesAlmostEqual( 

mi0, mi1, skipMask=skipMask, atol=0, rtol=0) 

 

for dval in (0.001, 0.03): 

mi0 = mi.Factory(mi, True) 

mi1 = mi.Factory(mi, True) 

plane0 = getattr(mi0, getName)() 

plane1 = getattr(mi1, getName)() 

plane1[2, 2] += dval 

val1 = plane1.get(2, 2) 

self.assertImagesAlmostEqual( 

plane0, plane1, rtol=0, atol=dval + epsilon) 

self.assertImagesAlmostEqual( 

plane0, plane1, rtol=dval/val1 + epsilon, atol=0) 

self.assertMaskedImagesAlmostEqual( 

mi0, mi1, rtol=0, atol=dval + epsilon) 

self.assertMaskedImagesAlmostEqual( 

mi1, mi0, rtol=0, atol=dval + epsilon) 

with self.assertRaises(Exception): 

self.assertImagesAlmostEqual( 

plane0, plane1, rtol=0, atol=dval - epsilon) 

with self.assertRaises(Exception): 

self.assertImagesAlmostEqual( 

plane0, plane1, rtol=dval/val1 - epsilon, atol=0) 

with self.assertRaises(Exception): 

self.assertMaskedImagesAlmostEqual( 

mi0, mi1, rtol=0, atol=dval - epsilon) 

with self.assertRaises(Exception): 

self.assertMaskedImagesAlmostEqual( 

mi0, mi1, rtol=dval/val1 - epsilon, atol=0) 

else: 

# plane is an integer of some type 

for dval in (1, 3): 

mi0 = mi.Factory(mi, True) 

mi1 = mi.Factory(mi, True) 

plane0 = getattr(mi0, getName)() 

plane1 = getattr(mi1, getName)() 

plane1[2, 2] += dval 

val1 = plane1.get(2, 2) 

# int value and test is <= so epsilon not required for atol 

# but rtol is a fraction, so epsilon is still safest for 

# the rtol test 

self.assertImagesAlmostEqual( 

plane0, plane1, rtol=0, atol=dval) 

self.assertImagesAlmostEqual( 

plane0, plane1, rtol=dval/val1 + epsilon, atol=0) 

with self.assertRaises(Exception): 

self.assertImagesAlmostEqual( 

plane0, plane1, rtol=0, atol=dval - epsilon) 

with self.assertRaises(Exception): 

self.assertImagesAlmostEqual( 

plane0, plane1, rtol=dval/val1 - epsilon, atol=0) 

 

# alter mask and check the results 

mi0 = mi.Factory(mi, True) 

mi1 = mi.Factory(mi, True) 

mask0 = mi0.getMask() 

mask1 = mi1.getMask() 

for dval in (1, 3): 

# getArray avoids "unsupported operand type" failure 

mask1.getArray()[2, 2] += 1 

with self.assertRaises(Exception): 

self.assertMasksEqual(mask0, mask1) 

with self.assertRaises(Exception): 

self.assertMasksEqual(mask1, mask0) 

with self.assertRaises(Exception): 

self.assertMaskedImagesEqual(mi0, mi1) 

with self.assertRaises(Exception): 

self.assertMaskedImagesEqual(mi1, mi0) 

 

skipMask = mi.getMask().Factory(mi.getMask(), True) 

skipMaskArr = skipMask.getArray() 

skipMaskArr[:] = 0 

skipMaskArr[2, 2] = 1 

self.assertMasksEqual(mask0, mask1, skipMask=skipMaskArr) 

self.assertMasksEqual(mask0, mask1, skipMask=skipMask) 

self.assertMaskedImagesAlmostEqual( 

mi0, mi1, skipMask=skipMaskArr, atol=0, rtol=0) 

self.assertMaskedImagesAlmostEqual( 

mi0, mi1, skipMask=skipMask, atol=0, rtol=0) 

 

def testAssertImagesAlmostEqual(self): 

"""Test assertImagesAlmostEqual, assertMasksEqual and assertMaskedImagesAlmostEqual 

""" 

width = 10 

height = 9 

 

for miType in (afwImage.MaskedImageF, afwImage.MaskedImageD, afwImage.MaskedImageI, 

afwImage.MaskedImageU): 

mi = makeRampMaskedImageWithNans(width, height, miType) 

self.checkMaskedImage(mi) 

 

for invalidType in (np.zeros([width+1, height]), str, self.assertRaises): 

mi = makeRampMaskedImageWithNans(width, height, miType) 

with self.assertRaises(TypeError): 

self.assertMasksEqual(mi.getMask(), invalidType) 

with self.assertRaises(TypeError): 

self.assertMasksEqual(invalidType, mi.getMask()) 

with self.assertRaises(TypeError): 

self.assertMasksEqual( 

mi.getMask(), mi.getMask(), skipMask=invalidType) 

 

with self.assertRaises(TypeError): 

self.assertImagesAlmostEqual(mi.getImage(), invalidType) 

with self.assertRaises(TypeError): 

self.assertImagesAlmostEqual(invalidType, mi.getImage()) 

with self.assertRaises(TypeError): 

self.assertImagesAlmostEqual( 

mi.getImage(), mi.getImage(), skipMask=invalidType) 

 

with self.assertRaises(TypeError): 

self.assertMaskedImagesAlmostEqual(mi, invalidType) 

with self.assertRaises(TypeError): 

self.assertMaskedImagesAlmostEqual(invalidType, mi) 

with self.assertRaises(TypeError): 

self.assertMaskedImagesAlmostEqual( 

mi, mi, skipMask=invalidType) 

 

with self.assertRaises(TypeError): 

self.assertMaskedImagesAlmostEqual( 

mi.getImage(), mi.getImage()) 

 

def testUnsignedImages(self): 

"""Unsigned images can give incorrect differences unless the test code is careful 

""" 

image0 = np.zeros([5, 5], dtype=np.uint8) 

image1 = np.zeros([5, 5], dtype=np.uint8) 

image0[0, 0] = 1 

image1[0, 1] = 2 

 

# arrays differ by a maximum of 2 

errMsg1 = imagesDiffer(image0, image1) 

match = re.match(r"maxDiff *= *(\d+)", errMsg1, re.IGNORECASE) 

self.assertIsNotNone(match) 

self.assertEqual(match.group(1), "2") 

 

# arrays are equal to within 5 

self.assertImagesAlmostEqual(image0, image1, atol=5) 

 

 

def makeRampMaskedImageWithNans(width, height, imgClass=afwImage.MaskedImageF): 

"""Make a masked image that is a ramp with additional non-finite values 

 

Make a masked image with the following additional non-finite values 

in the variance plane and (if image is of some floating type) image plane: 

- nan at [0, 0] 

- inf at [1, 0] 

- -inf at [0, 1] 

""" 

mi = makeRampMaskedImage(width, height, imgClass) 

 

var = mi.getVariance() 

var[0, 0] = np.nan 

var[1, 0] = np.inf 

var[0, 1] = -np.inf 

 

im = mi.getImage() 

try: 

np.array([np.nan], dtype=im.getArray().dtype) 

except Exception: 

# image plane does not support nan, etc. (presumably an int of some 

# variety) 

pass 

else: 

# image plane does support nan, etc. 

im[0, 0] = np.nan 

im[1, 0] = np.inf 

im[0, 1] = -np.inf 

return mi 

 

 

def makeRampMaskedImage(width, height, imgClass=afwImage.MaskedImageF): 

"""Make a ramp image of the specified size and image class 

 

Image values start from 0 at the lower left corner and increase by 1 along rows 

Variance values equal image values + 100 

Mask values equal image values modulo 8 bits (leaving plenty of unused values) 

""" 

mi = imgClass(width, height) 

image = mi.getImage() 

mask = mi.getMask() 

variance = mi.getVariance() 

val = 0 

for yInd in range(height): 

for xInd in range(width): 

image.set(xInd, yInd, val) 

variance.set(xInd, yInd, val + 100) 

mask.set(xInd, yInd, val % 0x100) 

val += 1 

return mi 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()