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

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

# 

# 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 Footprints, and FootprintSets 

 

Run with: 

footprint2.py 

or 

python 

>>> import footprint2; footprint2.run() 

""" 

 

import unittest 

 

import lsst.utils.tests 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.geom as afwGeom 

import lsst.afw.detection as afwDetect 

import lsst.afw.display.ds9 as ds9 

 

try: 

type(display) 

except NameError: 

display = False 

 

 

def toString(*args): 

"""toString written in python""" 

50 ↛ 53line 50 didn't jump to line 53, because the condition on line 50 was never false if len(args) == 1: 

args = args[0] 

 

y, x0, x1 = args 

return "%d: %d..%d" % (y, x0, x1) 

 

 

def peakFromImage(im, pos): 

"""Function to extract the sort key of peak height. Sort by decreasing peak height.""" 

val = im.get(pos[0], pos[1])[0] 

return -1.0 * val 

 

 

class Object: 

 

def __init__(self, val, spans): 

self.val = val 

self.spans = spans 

 

def insert(self, im): 

"""Insert self into an image""" 

for sp in self.spans: 

y, x0, x1 = sp 

for x in range(x0, x1+1): 

im.set(x, y, self.val) 

 

def __eq__(self, other): 

for osp, sp in zip(other.getSpans(), self.spans): 

78 ↛ 79line 78 didn't jump to line 79, because the condition on line 78 was never true if osp.toString() != toString(sp): 

return False 

 

return True 

 

 

class FootprintSetTestCase(unittest.TestCase): 

"""A test case for FootprintSet""" 

 

def setUp(self): 

self.im = afwImage.ImageU(lsst.geom.Extent2I(12, 8)) 

# 

# Objects that we should detect 

# 

self.objects = [] 

self.objects += [Object(10, [(1, 4, 4), (2, 3, 5), (3, 4, 4)])] 

self.objects += [Object(20, [(5, 7, 8), (5, 10, 10), (6, 8, 9)])] 

self.objects += [Object(20, [(6, 3, 3)])] 

 

self.im.set(0) # clear image 

for obj in self.objects: 

obj.insert(self.im) 

 

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

ds9.mtv(self.im, frame=0) 

 

def tearDown(self): 

del self.im 

 

def testGC(self): 

"""Check that FootprintSets are automatically garbage collected (when MemoryTestCase runs)""" 

 

afwDetect.FootprintSet(afwImage.ImageU(lsst.geom.Extent2I(10, 20)), 

afwDetect.Threshold(10)) 

 

def testFootprints(self): 

"""Check that we found the correct number of objects and that they are correct""" 

ds = afwDetect.FootprintSet(self.im, afwDetect.Threshold(10)) 

 

objects = ds.getFootprints() 

 

self.assertEqual(len(objects), len(self.objects)) 

for i in range(len(objects)): 

self.assertEqual(objects[i], self.objects[i]) 

 

def testFootprints2(self): 

"""Check that we found the correct number of objects using FootprintSet""" 

ds = afwDetect.FootprintSet(self.im, afwDetect.Threshold(10)) 

 

objects = ds.getFootprints() 

 

self.assertEqual(len(objects), len(self.objects)) 

for i in range(len(objects)): 

self.assertEqual(objects[i], self.objects[i]) 

 

def testFootprintsImageId(self): 

"""Check that we can insert footprints into an Image""" 

ds = afwDetect.FootprintSet(self.im, afwDetect.Threshold(10)) 

objects = ds.getFootprints() 

 

idImage = afwImage.ImageU(self.im.getDimensions()) 

idImage.set(0) 

 

for foot in objects: 

foot.spans.setImage(idImage, foot.getId()) 

 

if False: 

ds9.mtv(idImage, frame=2) 

 

for i in range(len(objects)): 

for sp in objects[i].getSpans(): 

for x in range(sp.getX0(), sp.getX1() + 1): 

self.assertEqual(idImage.get(x, sp.getY()), 

objects[i].getId()) 

 

def testFootprintSetImageId(self): 

"""Check that we can insert a FootprintSet into an Image, setting relative IDs""" 

ds = afwDetect.FootprintSet(self.im, afwDetect.Threshold(10)) 

objects = ds.getFootprints() 

 

idImage = ds.insertIntoImage(True) 

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

ds9.mtv(idImage, frame=2) 

 

for i in range(len(objects)): 

for sp in objects[i].getSpans(): 

for x in range(sp.getX0(), sp.getX1() + 1): 

self.assertEqual(idImage.get(x, sp.getY()), i + 1) 

 

def testFootprintsImage(self): 

"""Check that we can search Images as well as MaskedImages""" 

ds = afwDetect.FootprintSet(self.im, afwDetect.Threshold(10)) 

 

objects = ds.getFootprints() 

 

self.assertEqual(len(objects), len(self.objects)) 

for i in range(len(objects)): 

self.assertEqual(objects[i], self.objects[i]) 

 

def testGrow2(self): 

"""Grow some more interesting shaped Footprints. Informative with display, but no numerical tests""" 

# Can't set mask plane as the image is not a masked image. 

ds = afwDetect.FootprintSet(self.im, afwDetect.Threshold(10)) 

 

idImage = afwImage.ImageU(self.im.getDimensions()) 

idImage.set(0) 

 

i = 1 

for foot in ds.getFootprints()[0:1]: 

foot.dilate(3, afwGeom.Stencil.MANHATTAN) 

foot.spans.setImage(idImage, i, doClip=True) 

i += 1 

 

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

ds9.mtv(self.im, frame=0) 

ds9.mtv(idImage, frame=1) 

 

def testGrow(self): 

"""Grow footprints using the FootprintSet constructor""" 

fs = afwDetect.FootprintSet(self.im, afwDetect.Threshold(10)) 

self.assertEqual(len(fs.getFootprints()), len(self.objects)) 

for isotropic in (True, False, afwDetect.FootprintControl(True),): 

grown = afwDetect.FootprintSet(fs, 1, isotropic) 

self.assertEqual(len(fs.getFootprints()), len(self.objects)) 

 

self.assertGreater(len(grown.getFootprints()), 0) 

self.assertLessEqual(len(grown.getFootprints()), 

len(fs.getFootprints())) 

 

def testFootprintControl(self): 

"""Test the FootprintControl constructor""" 

fctrl = afwDetect.FootprintControl() 

self.assertFalse(fctrl.isCircular()[0]) # not set 

self.assertFalse(fctrl.isIsotropic()[0]) # not set 

 

fctrl.growIsotropic(False) 

self.assertTrue(fctrl.isCircular()[0]) 

self.assertTrue(fctrl.isIsotropic()[0]) 

self.assertTrue(fctrl.isCircular()[1]) 

self.assertFalse(fctrl.isIsotropic()[1]) 

 

fctrl = afwDetect.FootprintControl() 

fctrl.growLeft(False) 

self.assertTrue(fctrl.isLeft()[0]) # it's now set 

self.assertFalse(fctrl.isLeft()[1]) # ... but False 

 

fctrl = afwDetect.FootprintControl(True, False, False, False) 

self.assertTrue(fctrl.isLeft()[0]) 

self.assertTrue(fctrl.isRight()[0]) 

self.assertTrue(fctrl.isUp()[0]) 

self.assertTrue(fctrl.isDown()[0]) 

 

self.assertTrue(fctrl.isLeft()[1]) 

self.assertFalse(fctrl.isRight()[1]) 

 

def testGrowCircular(self): 

"""Grow footprints in all 4 directions using the FootprintSet/FootprintControl constructor """ 

im = afwImage.MaskedImageF(11, 11) 

im.set(5, 5, (10,)) 

fs = afwDetect.FootprintSet(im, afwDetect.Threshold(10)) 

self.assertEqual(len(fs.getFootprints()), 1) 

 

radius = 3 # How much to grow by 

for fctrl in (afwDetect.FootprintControl(), 

afwDetect.FootprintControl(True), 

afwDetect.FootprintControl(True, True), 

): 

grown = afwDetect.FootprintSet(fs, radius, fctrl) 

afwDetect.setMaskFromFootprintList( 

im.getMask(), grown.getFootprints(), 0x10) 

 

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

ds9.mtv(im) 

 

foot = grown.getFootprints()[0] 

 

if not fctrl.isCircular()[0]: 

self.assertEqual(foot.getArea(), 1) 

256 ↛ 241line 256 didn't jump to line 241, because the condition on line 256 was never false elif fctrl.isCircular()[0]: 

assert radius == 3 

if fctrl.isIsotropic()[1]: 

self.assertEqual(foot.getArea(), 29) 

else: 

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

 

def testGrowLRUD(self): 

"""Grow footprints in various directions using the FootprintSet/FootprintControl constructor """ 

im = afwImage.MaskedImageF(11, 11) 

x0, y0, ny = 5, 5, 3 

for y in range(y0 - ny//2, y0 + ny//2 + 1): 

im.set(x0, y, (10,)) 

fs = afwDetect.FootprintSet(im, afwDetect.Threshold(10)) 

self.assertEqual(len(fs.getFootprints()), 1) 

 

ngrow = 2 # How much to grow by 

# 

# Test growing to the left and/or right 

# 

for fctrl in ( 

afwDetect.FootprintControl(False, True, False, False), 

afwDetect.FootprintControl(True, False, False, False), 

afwDetect.FootprintControl(True, True, False, False), 

): 

fs = afwDetect.FootprintSet(im, afwDetect.Threshold(10)) 

grown = afwDetect.FootprintSet(fs, ngrow, fctrl) 

im.getMask().set(0) 

afwDetect.setMaskFromFootprintList( 

im.getMask(), grown.getFootprints(), 0x10) 

 

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

ds9.mtv(im) 

 

foot = grown.getFootprints()[0] 

nextra = 0 

if fctrl.isLeft()[1]: 

nextra += ngrow 

for y in range(y0 - ny//2, y0 + ny//2 + 1): 

self.assertNotEqual(im.getMask().get(x0 - 1, y), 0) 

 

if fctrl.isRight()[1]: 

nextra += ngrow 

for y in range(y0 - ny//2, y0 + ny//2 + 1): 

self.assertNotEqual(im.getMask().get(x0 + 1, y), 0) 

 

self.assertEqual(foot.getArea(), (1 + nextra)*ny) 

# 

# Test growing to up and/or down 

# 

for fctrl in ( 

afwDetect.FootprintControl(False, False, True, False), 

afwDetect.FootprintControl(False, False, False, True), 

afwDetect.FootprintControl(False, False, True, True), 

): 

grown = afwDetect.FootprintSet(fs, ngrow, fctrl) 

im.getMask().set(0) 

afwDetect.setMaskFromFootprintList( 

im.getMask(), grown.getFootprints(), 0x10) 

 

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

ds9.mtv(im) 

 

foot = grown.getFootprints()[0] 

nextra = 0 

if fctrl.isUp()[1]: 

nextra += ngrow 

for y in range(y0 + ny//2 + 1, y0 + ny//2 + ngrow + 1): 

self.assertNotEqual(im.getMask().get(x0, y), 0) 

 

if fctrl.isDown()[1]: 

nextra += ngrow 

328 ↛ 329line 328 didn't jump to line 329, because the loop on line 328 never started for y in range(y0 - ny//2 - 1, y0 - ny//2 - ngrow - 1): 

self.assertNotEqual(im.getMask().get(x0, y), 0) 

 

self.assertEqual(foot.getArea(), ny + nextra) 

 

def testGrowLRUD2(self): 

"""Grow footprints in various directions using the FootprintSet/FootprintControl constructor 

 

Check that overlapping grown Footprints give the expected answers 

""" 

ngrow = 3 # How much to grow by 

for fctrl, xy in [ 

(afwDetect.FootprintControl(True, True, 

False, False), [(4, 5), (5, 6), (6, 5)]), 

(afwDetect.FootprintControl(False, False, 

True, True), [(5, 4), (6, 5), (5, 6)]), 

]: 

im = afwImage.MaskedImageF(11, 11) 

for x, y in xy: 

im.set(x, y, (10,)) 

fs = afwDetect.FootprintSet(im, afwDetect.Threshold(10)) 

self.assertEqual(len(fs.getFootprints()), 1) 

 

grown = afwDetect.FootprintSet(fs, ngrow, fctrl) 

im.getMask().set(0) 

afwDetect.setMaskFromFootprintList( 

im.getMask(), grown.getFootprints(), 0x10) 

 

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

ds9.mtv(im) 

 

self.assertEqual(len(grown.getFootprints()), 1) 

foot = grown.getFootprints()[0] 

 

npix = 1 + 2*ngrow 

npix += 3 + 2*ngrow # 3: distance between pair of set pixels 000X0X000 

self.assertEqual(foot.getArea(), npix) 

 

def testInf(self): 

"""Test detection for images with Infs""" 

 

im = afwImage.MaskedImageF(lsst.geom.Extent2I(10, 20)) 

im.set(0) 

 

import numpy 

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

im.set(x, im.getHeight() - 1, (numpy.Inf, 0x0, 0)) 

 

ds = afwDetect.FootprintSet(im, afwDetect.createThreshold(100)) 

 

objects = ds.getFootprints() 

afwDetect.setMaskFromFootprintList(im.getMask(), objects, 0x10) 

 

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

ds9.mtv(im) 

 

self.assertEqual(len(objects), 1) 

 

 

class PeaksInFootprintsTestCase(unittest.TestCase): 

"""A test case for detecting Peaks within Footprints""" 

 

def doSetUp(self, dwidth=0, dheight=0, x0=0, y0=0): 

width, height = 14 + x0 + dwidth, 10 + y0 + dheight 

self.im = afwImage.MaskedImageF(lsst.geom.Extent2I(width, height)) 

# 

# Objects that we should detect 

# 

self.objects, self.peaks = [], [] 

self.objects.append( 

[[4, 1, 10], [3, 2, 10], [4, 2, 20], [5, 2, 10], [4, 3, 10], ]) 

self.peaks.append([[4, 2]]) 

self.objects.append( 

[[9, 7, 30], [10, 7, 29], [12, 7, 25], [10, 8, 27], [11, 8, 26], ]) 

self.peaks.append([[9, 7]]) 

self.objects.append([[3, 8, 10], [4, 8, 10], ]) 

self.peaks.append([[3, 8], [4, 8], ]) 

 

for pp in self.peaks: # allow for x0, y0 

for p in pp: 

p[0] += x0 

p[1] += y0 

for oo in self.objects: 

for o in oo: 

o[0] += x0 

o[1] += y0 

 

self.im.set((0, 0x0, 0)) # clear image 

for obj in self.objects: 

for x, y, I in obj: 

self.im.getImage().set(x, y, I) 

 

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

ds9.mtv(self.im, frame=0) 

 

def setUp(self): 

self.im, self.fs = None, None 

 

def tearDown(self): 

del self.im 

del self.fs 

 

def doTestPeaks(self, dwidth=0, dheight=0, x0=0, y0=0, threshold=10, callback=None, polarity=True, 

grow=0): 

"""Worker routine for tests 

polarity: True if should search for +ve pixels""" 

 

self.doSetUp(dwidth, dheight, x0, y0) 

if not polarity: 

self.im *= -1 

 

if callback: 

callback() 

 

def peakDescending(p): 

"""Sort self.peaks in decreasing peak height to match Footprint.getPeaks()""" 

return p[2] * -1.0 

for i, peaks in enumerate(self.peaks): 

self.peaks[i] = sorted([(x, y, self.im.getImage().get(x, y)) for x, y in peaks], 

key=peakDescending) 

 

threshold = afwDetect.Threshold( 

threshold, afwDetect.Threshold.VALUE, polarity) 

fs = afwDetect.FootprintSet(self.im, threshold, "BINNED1") 

 

if grow: 

fs = afwDetect.FootprintSet(fs, grow, True) 

msk = self.im.getMask() 

afwDetect.setMaskFromFootprintList( 

msk, fs.getFootprints(), msk.getPlaneBitMask("DETECTED")) 

del msk 

 

self.fs = fs 

self.checkPeaks(dwidth, dheight, frame=3) 

 

def checkPeaks(self, dwidth=0, dheight=0, frame=3): 

"""Check that we got the peaks right""" 

feet = self.fs.getFootprints() 

# 

# Check that we found all the peaks 

# 

self.assertEqual(sum([len(f.getPeaks()) for f in feet]), 

sum([len(f.getPeaks()) for f in feet])) 

 

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

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

 

with ds9.Buffering(): 

for i, foot in enumerate(feet): 

for p in foot.getPeaks(): 

ds9.dot("+", p.getIx(), p.getIy(), 

size=0.4, frame=frame) 

 

if i < len(self.peaks): 

for trueX, trueY, peakVal in self.peaks[i]: 

ds9.dot("x", trueX, trueY, size=0.4, 

ctype=ds9.RED, frame=frame) 

 

for i, foot in enumerate(feet): 

npeak = None 

# 

# Peaks that touch the edge are handled differently, as only the single highest/lowest pixel 

# is treated as a Peak 

# 

if (dwidth != 0 or dheight != 0): 

if (foot.getBBox().getMinX() == 0 or foot.getBBox().getMaxX() == self.im.getWidth() - 1 or 

foot.getBBox().getMinY() == 0 or foot.getBBox().getMaxY() == self.im.getHeight() - 1): 

npeak = 1 

 

if npeak is None: 

npeak = len(self.peaks[i]) 

 

500 ↛ 501line 500 didn't jump to line 501, because the condition on line 500 was never true if npeak != len(foot.getPeaks()): 

print("RHL", foot.repr()) 

# print "RHL", [(p.repr().split(":")[0], p.getIx(), p.getIy()) 

# for p in foot.getPeaks()] 

print("RHL", [(p.getId(), p.getIx(), p.getIy()) 

for p in foot.getPeaks()]) 

print("RHL", [p[0:2] for p in self.peaks[i]]) 

 

self.assertEqual(len(foot.getPeaks()), npeak) 

 

for j, p in enumerate(foot.getPeaks()): 

trueX, trueY, peakVal = self.peaks[i][j] 

512 ↛ 513line 512 didn't jump to line 513, because the condition on line 512 was never true if (p.getIx(), p.getIy()) != (trueX, trueY): 

print("RHL", [(pp.getId(), pp.getIx(), pp.getIy()) 

for pp in foot.getPeaks()]) 

print("RHL", [pp[0:2] for pp in self.peaks[i]]) 

 

self.assertEqual((p.getIx(), p.getIy()), (trueX, trueY)) 

 

def testSinglePeak(self): 

"""Test that we can find single Peaks in Footprints""" 

 

self.doTestPeaks() 

 

def testSingleNegativePeak(self): 

"""Test that we can find single Peaks in Footprints when looking for -ve detections""" 

 

self.doTestPeaks(polarity=False) 

 

def testSinglePeakAtEdge(self): 

"""Test that we handle Peaks correctly at the edge""" 

 

self.doTestPeaks(dheight=-1) 

 

def testSingleNegativePeakAtEdge(self): 

"""Test that we handle -ve Peaks correctly at the edge""" 

 

self.doTestPeaks(dheight=-1, polarity=False) 

 

def testMultiPeak(self): 

"""Test that multiple peaks are handled correctly""" 

def callback(): 

x, y = 12, 7 

self.im.getImage().set(x, y, 100) 

self.peaks[1].append((x, y)) 

 

self.doTestPeaks(callback=callback) 

 

def testMultiNegativePeak(self): 

"""Test that multiple negative peaks are handled correctly""" 

def callback(): 

x, y = 12, 7 

self.im.getImage().set(x, y, -100) 

self.peaks[1].append((x, y)) 

 

self.doTestPeaks(polarity=False, callback=callback) 

 

def testGrowFootprints(self): 

"""Test that we can grow footprints, correctly merging those that now touch""" 

def callback(): 

self.im.getImage().set(10, 4, 20) 

self.peaks[-2].append((10, 4,)) 

 

self.doTestPeaks(dwidth=1, dheight=1, callback=callback, grow=1) 

 

def testGrowFootprints2(self): 

"""Test that we can grow footprints, correctly merging those that now overlap 

N.b. this caused RHL's initial implementation to crash 

""" 

def callback(): 

self.im.getImage().set(10, 4, 20) 

self.peaks[-2].append((10, 4, )) 

 

def peaksSortKey(p): 

return peakFromImage(self.im, p) 

self.peaks[0] = sorted(sum(self.peaks, []), key=peaksSortKey) 

 

self.doTestPeaks(x0=0, y0=2, dwidth=2, dheight=2, 

callback=callback, grow=2) 

 

def testGrowFootprints3(self): 

"""Test that we can grow footprints, correctly merging those that now totally overwritten""" 

 

self.im = afwImage.MaskedImageF(14, 11) 

 

self.im.getImage().set(0) 

self.peaks = [] 

 

value = 11 

for x, y in [(4, 7), (5, 7), (6, 7), (7, 7), (8, 7), 

(4, 6), (8, 6), 

(4, 5), (8, 5), 

(4, 4), (8, 4), 

(4, 3), (8, 3), 

]: 

self.im.getImage().set(x, y, value) 

value -= 1e-3 

 

self.im.getImage().set(4, 7, 15) 

self.peaks.append([(4, 7,), ]) 

 

self.im.getImage().set(6, 5, 30) 

self.peaks[0].append((6, 5,)) 

 

self.fs = afwDetect.FootprintSet( 

self.im, afwDetect.Threshold(10), "BINNED1") 

# 

# The disappearing Footprint special case only shows up if the outer Footprint is grown 

# _after_ the inner one. So arrange the order properly 

feet = self.fs.getFootprints() 

feet[0], feet[1] = feet[1], feet[0] 

 

msk = self.im.getMask() 

 

grow = 2 

self.fs = afwDetect.FootprintSet(self.fs, grow, False) 

afwDetect.setMaskFromFootprintList(msk, self.fs.getFootprints(), 

msk.getPlaneBitMask("DETECTED_NEGATIVE")) 

 

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

frame = 0 

 

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

 

with ds9.Buffering(): 

for i, foot in enumerate(self.fs.getFootprints()): 

for p in foot.getPeaks(): 

ds9.dot("+", p.getIx(), p.getIy(), 

size=0.4, frame=frame) 

 

if i < len(self.peaks): 

for trueX, trueY in self.peaks[i]: 

ds9.dot("x", trueX, trueY, size=0.4, 

ctype=ds9.RED, frame=frame) 

 

self.assertEqual(len(self.fs.getFootprints()), 1) 

self.assertEqual(len(self.fs.getFootprints()[ 

0].getPeaks()), len(self.peaks[0])) 

 

def testMergeFootprints(self): # YYYY 

"""Merge positive and negative Footprints""" 

x0, y0 = 5, 6 

dwidth, dheight = 6, 7 

 

def callback(): 

x, y, value = x0 + 10, y0 + 4, -20 

self.im.getImage().set(x, y, value) 

peaks2.append((x, y, value)) 

 

for grow1, grow2 in [(1, 1), (3, 3), (6, 6), ]: 

peaks2 = [] 

self.doTestPeaks(threshold=10, callback=callback, grow=0, 

x0=x0, y0=y0, dwidth=dwidth, dheight=dheight) 

 

threshold = afwDetect.Threshold( 

10, afwDetect.Threshold.VALUE, False) 

fs2 = afwDetect.FootprintSet(self.im, threshold) 

 

msk = self.im.getMask() 

afwDetect.setMaskFromFootprintList( 

msk, fs2.getFootprints(), msk.getPlaneBitMask("DETECTED_NEGATIVE")) 

 

self.fs.merge(fs2, grow1, grow2) 

self.peaks[-2] += peaks2 

 

if grow1 + grow2 > 2: # grow merged all peaks 

def peaksSortKey(p): 

return peakFromImage(self.im, p) 

self.peaks[0] = sorted(sum(self.peaks, []), key=peaksSortKey) 

 

afwDetect.setMaskFromFootprintList( 

msk, self.fs.getFootprints(), msk.getPlaneBitMask("EDGE")) 

 

self.checkPeaks(frame=3) 

 

def testMergeFootprintsEngulf(self): 

"""Merge two Footprints when growing one Footprint totally replaces the other""" 

def callback(): 

self.im.set(0) 

self.peaks, self.objects = [], [] 

 

for x, y, I in [[6, 4, 20], [6, 5, 10]]: 

self.im.getImage().set(x, y, I) 

self.peaks.append([[6, 4]]) 

 

x, y, value = 8, 4, -20 

self.im.getImage().set(x, y, value) 

peaks2.append((x, y, value)) 

 

grow1, grow2 = 0, 3 

peaks2 = [] 

self.doTestPeaks(threshold=10, callback=callback, grow=0) 

 

threshold = afwDetect.Threshold(10, afwDetect.Threshold.VALUE, False) 

fs2 = afwDetect.FootprintSet(self.im, threshold) 

 

msk = self.im.getMask() 

afwDetect.setMaskFromFootprintList( 

msk, fs2.getFootprints(), msk.getPlaneBitMask("DETECTED_NEGATIVE")) 

 

self.fs.merge(fs2, grow1, grow2) 

self.peaks[0] += peaks2 

 

def peaksSortKey(p): 

return peakFromImage(self.im, p) 

self.peaks[0] = sorted(sum(self.peaks, []), key=peaksSortKey) 

 

afwDetect.setMaskFromFootprintList( 

msk, self.fs.getFootprints(), msk.getPlaneBitMask("EDGE")) 

 

self.checkPeaks(frame=3) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()