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

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

# 

# LSST Data Management System 

# 

# Copyright 2008-2016 AURA/LSST. 

# 

# 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 <https://www.lsstcorp.org/LegalNotices/>. 

# 

 

import unittest 

import numpy as np 

 

import lsst.utils.tests 

import lsst.geom 

import lsst.afw.geom as afwGeom 

import lsst.afw.geom.ellipses as afwGeomEllipses 

import lsst.afw.image as afwImage 

 

 

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

''' 

This is a python level unit test of the SpanSets class. It is mean to work in conjuction 

with the c++ unit test. The C++ test has much more coverage, and tests some features which 

are not exposed to python. This test serves mainly as a way to test that the python bindings 

correctly work. Many of these tests are smaller versions of the C++ tests. 

''' 

 

def testNullSpanSet(self): 

nullSS = afwGeom.SpanSet() 

self.assertEqual(nullSS.getArea(), 0) 

self.assertEqual(len(nullSS), 0) 

self.assertEqual(nullSS.getBBox().getDimensions().getX(), 0) 

self.assertEqual(nullSS.getBBox().getDimensions().getY(), 0) 

 

def testBBoxSpanSet(self): 

boxSS = afwGeom.SpanSet(lsst.geom.Box2I(lsst.geom.Point2I(2, 2), 

lsst.geom.Point2I(6, 6))) 

self.assertEqual(boxSS.getArea(), 25) 

bBox = boxSS.getBBox() 

self.assertEqual(bBox.getMinX(), 2) 

self.assertEqual(bBox.getMinY(), 2) 

 

def testIteratorConstructor(self): 

spans = [afwGeom.Span(0, 2, 4), afwGeom.Span(1, 2, 4), 

afwGeom.Span(2, 2, 4)] 

spanSetFromList = afwGeom.SpanSet(spans) 

spanSetFromArray = afwGeom.SpanSet(np.array(spans)) 

 

self.assertEqual(spanSetFromList.getBBox().getMinX(), 2) 

self.assertEqual(spanSetFromList.getBBox().getMaxX(), 4) 

self.assertEqual(spanSetFromList.getBBox().getMinY(), 0) 

 

self.assertEqual(spanSetFromArray.getBBox().getMinX(), 2) 

self.assertEqual(spanSetFromArray.getBBox().getMaxX(), 4) 

self.assertEqual(spanSetFromArray.getBBox().getMinY(), 0) 

 

def testIsContiguous(self): 

spanSetConList = [afwGeom.Span(0, 2, 5), afwGeom.Span(1, 5, 8)] 

spanSetCon = afwGeom.SpanSet(spanSetConList) 

self.assertTrue(spanSetCon.isContiguous()) 

 

spanSetNotConList = [afwGeom.Span(0, 2, 5), afwGeom.Span(1, 20, 25)] 

spanSetNotCon = afwGeom.SpanSet(spanSetNotConList) 

self.assertFalse(spanSetNotCon.isContiguous()) 

 

def testSplit(self): 

spanSetOne = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX).shiftedBy(2, 2) 

spanSetTwo = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX).shiftedBy(8, 8) 

 

spanSetList = [] 

for spn in spanSetOne: 

spanSetList.append(spn) 

for spn in spanSetTwo: 

spanSetList.append(spn) 

spanSetTogether = afwGeom.SpanSet(spanSetList) 

 

spanSetSplit = spanSetTogether.split() 

self.assertEqual(len(spanSetSplit), 2) 

 

for a, b in zip(spanSetOne, spanSetSplit[0]): 

self.assertEqual(a, b) 

 

for a, b in zip(spanSetTwo, spanSetSplit[1]): 

self.assertEqual(a, b) 

 

def testTransform(self): 

transform = lsst.geom.LinearTransform(np.array([[2.0, 0.0], [0.0, 2.0]])) 

spanSetPreScale = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.CIRCLE) 

spanSetPostScale = spanSetPreScale.transformedBy(transform) 

 

self.assertEqual(spanSetPostScale.getBBox().getMinX(), -4) 

self.assertEqual(spanSetPostScale.getBBox().getMinY(), -4) 

 

def testOverlaps(self): 

spanSetNoShift = afwGeom.SpanSet.fromShape(4, afwGeom.Stencil.CIRCLE) 

spanSetShift = spanSetNoShift.shiftedBy(2, 2) 

 

self.assertTrue(spanSetNoShift.overlaps(spanSetShift)) 

 

def testContains(self): 

spanSetLarge = afwGeom.SpanSet.fromShape(4, afwGeom.Stencil.CIRCLE) 

spanSetSmall = afwGeom.SpanSet.fromShape(1, afwGeom.Stencil.CIRCLE) 

 

self.assertTrue(spanSetLarge.contains(spanSetSmall)) 

self.assertFalse(spanSetSmall.contains(lsst.geom.Point2I(100, 100))) 

 

def testComputeCentroid(self): 

spanSetShape = afwGeom.SpanSet.fromShape(4, afwGeom.Stencil.CIRCLE).shiftedBy(2, 2) 

center = spanSetShape.computeCentroid() 

 

self.assertEqual(center.getX(), 2) 

self.assertEqual(center.getY(), 2) 

 

def testComputeShape(self): 

spanSetShape = afwGeom.SpanSet.fromShape(1, afwGeom.Stencil.CIRCLE) 

quad = spanSetShape.computeShape() 

 

self.assertEqual(quad.getIxx(), 0.4) 

self.assertEqual(quad.getIyy(), 0.4) 

self.assertEqual(quad.getIxy(), 0) 

 

def testdilated(self): 

spanSetPredilated = afwGeom.SpanSet.fromShape(1, afwGeom.Stencil.CIRCLE) 

spanSetPostdilated = spanSetPredilated.dilated(1) 

 

bBox = spanSetPostdilated.getBBox() 

self.assertEqual(bBox.getMinX(), -2) 

self.assertEqual(bBox.getMinY(), -2) 

 

def testErode(self): 

spanSetPreErode = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.CIRCLE) 

spanSetPostErode = spanSetPreErode.eroded(1) 

 

bBox = spanSetPostErode.getBBox() 

self.assertEqual(bBox.getMinX(), -1) 

self.assertEqual(bBox.getMinY(), -1) 

 

def testFlatten(self): 

# Give an initial value to an input array 

inputArray = np.ones((6, 6)) * 9 

inputArray[1, 1] = 1 

inputArray[1, 2] = 2 

inputArray[2, 1] = 3 

inputArray[2, 2] = 4 

 

inputSpanSet = afwGeom.SpanSet([afwGeom.Span(0, 0, 1), 

afwGeom.Span(1, 0, 1)]) 

flatArr = inputSpanSet.flatten(inputArray, lsst.geom.Point2I(-1, -1)) 

 

self.assertEqual(flatArr.size, inputSpanSet.getArea()) 

 

# Test flatttening a 3D array 

spanSetArea = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX) 

spanSetArea = spanSetArea.shiftedBy(2, 2) 

 

testArray = np.arange(5*5*3).reshape(5, 5, 3) 

flattened2DArray = spanSetArea.flatten(testArray) 

 

truthArray = np.arange(5*5*3).reshape(5*5, 3) 

self.assertFloatsAlmostEqual(flattened2DArray, truthArray) 

 

def testUnflatten(self): 

inputArray = np.ones(6) * 4 

inputSpanSet = afwGeom.SpanSet([afwGeom.Span(9, 2, 3), 

afwGeom.Span(10, 3, 4), 

afwGeom.Span(11, 2, 3)]) 

outputArray = inputSpanSet.unflatten(inputArray) 

 

arrayShape = outputArray.shape 

bBox = inputSpanSet.getBBox() 

self.assertEqual(arrayShape[0], bBox.getHeight()) 

self.assertEqual(arrayShape[1], bBox.getWidth()) 

 

# Test unflattening a 2D array 

spanSetArea = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX) 

spanSetArea = spanSetArea.shiftedBy(2, 2) 

 

testArray = np.arange(5*5*3).reshape(5*5, 3) 

unflattened3DArray = spanSetArea.unflatten(testArray) 

 

truthArray = np.arange(5*5*3).reshape(5, 5, 3) 

self.assertFloatsAlmostEqual(unflattened3DArray, truthArray) 

 

def populateMask(self): 

msk = afwImage.Mask(10, 10, 1) 

spanSetMask = afwGeom.SpanSet.fromShape(3, afwGeom.Stencil.CIRCLE).shiftedBy(5, 5) 

spanSetMask.setMask(msk, 2) 

return msk, spanSetMask 

 

def testSetMask(self): 

mask, spanSetMask = self.populateMask() 

mskArray = mask.getArray() 

for i in range(mskArray.shape[0]): 

for j in range(mskArray.shape[1]): 

if lsst.geom.Point2I(i, j) in spanSetMask: 

self.assertEqual(mskArray[i, j], 3) 

else: 

self.assertEqual(mskArray[i, j], 1) 

 

def testClearMask(self): 

mask, spanSetMask = self.populateMask() 

spanSetMask.clearMask(mask, 2) 

mskArray = mask.getArray() 

for i in range(mskArray.shape[0]): 

for j in range(mskArray.shape[1]): 

self.assertEqual(mskArray[i, j], 1) 

 

def makeOverlapSpanSets(self): 

firstSpanSet = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX).shiftedBy(2, 4) 

secondSpanSet = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX).shiftedBy(2, 2) 

return firstSpanSet, secondSpanSet 

 

def makeMaskAndSpanSetForOperationTest(self): 

firstMaskPart = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX).shiftedBy(3, 2) 

secondMaskPart = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX).shiftedBy(3, 8) 

spanSetMaskOperation = afwGeom.SpanSet.fromShape(2, afwGeom.Stencil.BOX).shiftedBy(3, 5) 

 

mask = afwImage.Mask(20, 20) 

firstMaskPart.setMask(mask, 3) 

secondMaskPart.setMask(mask, 3) 

spanSetMaskOperation.setMask(mask, 4) 

 

return mask, spanSetMaskOperation 

 

def testIntersection(self): 

firstSpanSet, secondSpanSet = self.makeOverlapSpanSets() 

 

overlap = firstSpanSet.intersect(secondSpanSet) 

for i, span in enumerate(overlap): 

self.assertEqual(span.getY(), i+2) 

self.assertEqual(span.getMinX(), 0) 

self.assertEqual(span.getMaxX(), 4) 

 

mask, spanSetMaskOperation = self.makeMaskAndSpanSetForOperationTest() 

spanSetIntersectMask = spanSetMaskOperation.intersect(mask, 2) 

 

expectedYRange = [3, 4, 6, 7] 

for expected, val in zip(expectedYRange, spanSetIntersectMask): 

self.assertEqual(expected, val.getY()) 

 

def testIntersectNot(self): 

firstSpanSet, secondSpanSet = self.makeOverlapSpanSets() 

 

overlap = firstSpanSet.intersectNot(secondSpanSet) 

for yVal, span in enumerate(overlap): 

self.assertEqual(span.getY(), yVal+5) 

self.assertEqual(span.getMinX(), 0) 

self.assertEqual(span.getMaxX(), 4) 

 

mask, spanSetMaskOperation = self.makeMaskAndSpanSetForOperationTest() 

 

spanSetIntersectNotMask = spanSetMaskOperation.intersectNot(mask, 2) 

 

self.assertEqual(len(spanSetIntersectNotMask), 1) 

self.assertEqual(next(iter(spanSetIntersectNotMask)).getY(), 5) 

 

# More complicated intersection with disconnected SpanSets 

spanList1 = [afwGeom.Span(0, 0, 10), 

afwGeom.Span(1, 0, 10), 

afwGeom.Span(2, 0, 10)] 

 

spanList2 = [afwGeom.Span(1, 2, 4), afwGeom.Span(1, 7, 8)] 

 

resultList = [afwGeom.Span(0, 0, 10), 

afwGeom.Span(1, 0, 1), 

afwGeom.Span(1, 5, 6), 

afwGeom.Span(1, 9, 10), 

afwGeom.Span(2, 0, 10)] 

 

spanSet1 = afwGeom.SpanSet(spanList1) 

spanSet2 = afwGeom.SpanSet(spanList2) 

expectedSpanSet = afwGeom.SpanSet(resultList) 

 

outputSpanSet = spanSet1.intersectNot(spanSet2) 

 

self.assertEqual(outputSpanSet, expectedSpanSet) 

 

numIntersectNotTrials = 100 

spanRow = 5 

# Set a seed for random functions 

np.random.seed(400) 

for N in range(numIntersectNotTrials): 

# Create two random SpanSets, both with holes in them 

listOfRandomSpanSets = [] 

for i in range(2): 

# Make two rectangles to be turned into a SpanSet 

rand1 = np.random.randint(0, 26, 2) 

rand2 = np.random.randint(rand1.max(), 51, 2) 

tempList = [afwGeom.Span(spanRow, rand1.min(), rand1.max()), 

afwGeom.Span(spanRow, rand2.min(), rand2.max())] 

listOfRandomSpanSets.append(afwGeom.SpanSet(tempList)) 

 

# IntersectNot the SpanSets, randomly choosing which one is the one 

# to be the negated SpanSet 

randChoice = np.random.randint(0, 2) 

negatedRandChoice = int(not randChoice) 

sourceSpanSet = listOfRandomSpanSets[randChoice] 

targetSpanSet = listOfRandomSpanSets[negatedRandChoice] 

resultSpanSet = sourceSpanSet.intersectNot(targetSpanSet) 

for span in resultSpanSet: 

for point in span: 

self.assertTrue(sourceSpanSet.contains(point)) 

self.assertFalse(targetSpanSet.contains(point)) 

 

for x in range(51): 

point = lsst.geom.Point2I(x, spanRow) 

if sourceSpanSet.contains(point) and not\ 

targetSpanSet.contains(point): 

self.assertTrue(resultSpanSet.contains(point)) 

 

def testUnion(self): 

firstSpanSet, secondSpanSet = self.makeOverlapSpanSets() 

 

overlap = firstSpanSet.union(secondSpanSet) 

 

for yVal, span in enumerate(overlap): 

self.assertEqual(span.getY(), yVal) 

self.assertEqual(span.getMinX(), 0) 

self.assertEqual(span.getMaxX(), 4) 

 

mask, spanSetMaskOperation = self.makeMaskAndSpanSetForOperationTest() 

 

spanSetUnion = spanSetMaskOperation.union(mask, 2) 

 

for yVal, span in enumerate(spanSetUnion): 

self.assertEqual(span.getY(), yVal) 

 

def testMaskToSpanSet(self): 

mask, _ = self.makeMaskAndSpanSetForOperationTest() 

spanSetFromMask = afwGeom.SpanSet.fromMask(mask) 

 

for yCoord, span in enumerate(spanSetFromMask): 

self.assertEqual(span, afwGeom.Span(yCoord, 1, 5)) 

 

def testFromMask(self): 

xy0 = lsst.geom.Point2I(12345, 67890) # xy0 for image 

dims = lsst.geom.Extent2I(123, 45) # Dimensions of image 

box = lsst.geom.Box2I(xy0, dims) # Bounding box of image 

value = 32 

other = 16 

assert value & other == 0 # Setting 'other' unsets 'value' 

point = lsst.geom.Point2I(3 + xy0.getX(), 3 + xy0.getY()) # Point in the image 

 

mask = afwImage.Mask(box) 

mask.set(value) 

 

# We can create a SpanSet from bit planes 

spans = afwGeom.SpanSet.fromMask(mask, value) 

self.assertEqual(spans.getArea(), box.getArea()) 

self.assertEqual(spans.getBBox(), box) 

self.assertTrue(point in spans) 

 

# Pixels not matching the desired bit plane are ignored as they should be 

mask[point] = other # unset one pixel 

spans = afwGeom.SpanSet.fromMask(mask, value) 

self.assertEqual(spans.getArea(), box.getArea() - 1) 

self.assertEqual(spans.getBBox(), box) 

self.assertFalse(point in spans) 

 

def testEquality(self): 

firstSpanSet, secondSpanSet = self.makeOverlapSpanSets() 

secondSpanSetShift = secondSpanSet.shiftedBy(0, 2) 

 

self.assertFalse(firstSpanSet == secondSpanSet) 

self.assertTrue(firstSpanSet != secondSpanSet) 

self.assertTrue(firstSpanSet == secondSpanSetShift) 

self.assertFalse(firstSpanSet != secondSpanSetShift) 

 

def testSpanSetFromEllipse(self): 

axes = afwGeomEllipses.Axes(6, 6, 0) 

ellipse = afwGeom.Ellipse(axes, lsst.geom.Point2D(5, 6)) 

spanSet = afwGeom.SpanSet.fromShape(ellipse) 

for ss, es in zip(spanSet, afwGeomEllipses.PixelRegion(ellipse)): 

self.assertEqual(ss, es) 

 

def testfromShapeOffset(self): 

shift = lsst.geom.Point2I(2, 2) 

spanSetShifted = afwGeom.SpanSet.fromShape(2, offset=shift) 

bbox = spanSetShifted.getBBox() 

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

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

 

def testFindEdgePixels(self): 

spanSet = afwGeom.SpanSet.fromShape(6, afwGeom.Stencil.CIRCLE) 

spanSetEdge = spanSet.findEdgePixels() 

 

truthSpans = [afwGeom.Span(-6, 0, 0), 

afwGeom.Span(-5, -3, -1), 

afwGeom.Span(-5, 1, 3), 

afwGeom.Span(-4, -4, -4), 

afwGeom.Span(-4, 4, 4), 

afwGeom.Span(-3, -5, -5), 

afwGeom.Span(-3, 5, 5), 

afwGeom.Span(-2, -5, -5), 

afwGeom.Span(-2, 5, 5), 

afwGeom.Span(-1, -5, -5), 

afwGeom.Span(-1, 5, 5), 

afwGeom.Span(0, -6, -6), 

afwGeom.Span(0, 6, 6), 

afwGeom.Span(1, -5, -5), 

afwGeom.Span(1, 5, 5), 

afwGeom.Span(2, -5, -5), 

afwGeom.Span(2, 5, 5), 

afwGeom.Span(3, -5, -5), 

afwGeom.Span(3, 5, 5), 

afwGeom.Span(4, -4, -4), 

afwGeom.Span(4, 4, 4), 

afwGeom.Span(5, -3, -1), 

afwGeom.Span(5, 1, 3), 

afwGeom.Span(6, 0, 0)] 

truthSpanSet = afwGeom.SpanSet(truthSpans) 

self.assertEqual(spanSetEdge, truthSpanSet) 

 

def testIndices(self): 

dataArray = np.zeros((5, 5)) 

spanSet = afwGeom.SpanSet.fromShape(2, 

afwGeom.Stencil.BOX, 

offset=(2, 2)) 

yind, xind = spanSet.indices() 

dataArray[yind, xind] = 9 

self.assertTrue((dataArray == 9).all()) 

 

def testSpanIteration(self): 

span = afwGeom.Span(4, 3, 8) 

points = list(span) 

self.assertEqual(len(span), len(points)) 

self.assertEqual(points, [lsst.geom.Point2I(x, 4) for x in range(3, 9)]) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()