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

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

# This file is part of afw. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# 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 GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

import math 

import os.path 

import unittest 

import pickle 

from functools import reduce 

 

import numpy as np 

 

import lsst.utils 

import lsst.utils.tests 

import lsst.pex.exceptions 

from lsst.daf.base import PropertySet 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

import lsst.afw.display as afwDisplay 

import lsst.pex.exceptions as pexExcept 

 

# Set to True to display debug messages and images 

debugMode = False 

 

afwDisplay.setDefaultMaskTransparency(75) 

try: 

AfwdataDir = lsst.utils.getPackageDir("afwdata") 

except pexExcept.NotFoundError: 

AfwdataDir = None 

 

 

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

 

def setUp(self): 

np.random.seed(1) 

self.val = 10 

self.image = afwImage.ImageF(lsst.geom.Box2I( 

lsst.geom.Point2I(1000, 500), lsst.geom.Extent2I(100, 200))) 

self.image.set(self.val) 

 

def tearDown(self): 

del self.image 

 

def testOddSize(self): 

"""Test for ticket #1781 -- without it, in oddly-sized images 

there is a chunk of pixels on the right/bottom that do not go 

into the fit and are extrapolated. After this ticket, the 

subimage boundaries are spread more evenly so the last pixels 

get fit as well. This slightly strange test case checks that 

the interpolant is close to the function at the end. I could 

not think of an interpolant that would fit exactly, so this 

just puts a limit on the errors. 

""" 

W, H = 2, 99 

image = afwImage.ImageF(lsst.geom.Extent2I(W, H)) 

NY = 10 

bgCtrl = afwMath.BackgroundControl(2, NY) 

for y in range(H): 

for x in range(W): 

B = 89 

if y < B: 

image[x, y, afwImage.LOCAL] = y 

else: 

image[x, y, afwImage.LOCAL] = B + (y-B)*-1. 

bobj = afwMath.makeBackground(image, bgCtrl) 

back = bobj.getImageF(afwMath.Interpolate.LINEAR) 

 

for iy, by in zip([image[0, y, afwImage.LOCAL] for y in range(H)], 

[back[0, y, afwImage.LOCAL] for y in range(H)]): 

self.assertLess(abs(iy - by), 5) 

 

def testgetPixel(self): 

"""Tests basic functionality of getPixel() method (floats)""" 

xcen, ycen = 50, 100 

bgCtrl = afwMath.BackgroundControl(10, 10) 

bgCtrl.setNxSample(5) 

bgCtrl.setNySample(5) 

bgCtrl.getStatisticsControl().setNumIter(3) 

bgCtrl.getStatisticsControl().setNumSigmaClip(3) 

back = afwMath.makeBackground(self.image, bgCtrl) 

 

with self.assertWarns(FutureWarning): 

self.assertEqual(back.getPixel(xcen, ycen), self.val) 

 

@unittest.skipIf(AfwdataDir is None, "afwdata not setup") 

def testBackgroundTestImages(self): 

"""Tests Laher's afwdata/Statistics/*.fits images (doubles)""" 

imginfolist = [] 

# cooked to known value 

imginfolist.append(["v1_i1_g_m400_s20_f.fits", 399.9912966583894]) 

 

for imginfo in imginfolist: 

imgfile, centerValue = imginfo 

imgPath = os.path.join(AfwdataDir, "Statistics", imgfile) 

 

# get the image and header 

dimg = afwImage.DecoratedImageF(imgPath) 

img = dimg.getImage() 

fitsHdr = dimg.getMetadata() # the FITS header 

 

# get the True values of the mean and stdev 

reqMean = fitsHdr.getAsDouble("MEANREQ") 

reqStdev = fitsHdr.getAsDouble("SIGREQ") 

naxis1 = img.getWidth() 

naxis2 = img.getHeight() 

 

# create a background control object 

bctrl = afwMath.BackgroundControl(5, 5) 

 

# run the background constructor and call the getImage() function. 

backobj = afwMath.makeBackground(img, bctrl) 

 

pixPerSubimage = img.getWidth()*img.getHeight() / \ 

(bctrl.getNxSample()*bctrl.getNySample()) 

stdevInterp = reqStdev/math.sqrt(pixPerSubimage) 

 

# test getImage() by checking the center pixel 

bimg = backobj.getImageF(afwMath.Interpolate.AKIMA_SPLINE) 

testImgval = bimg[naxis1//2, naxis2//2, afwImage.LOCAL] 

self.assertLess(abs(testImgval - reqMean), 2*stdevInterp) 

 

def testRamp(self): 

"""tests Laher's afwdata/Statistics/*.fits images (doubles)""" 

# make a ramping image (spline should be exact for linear increasing 

# image 

nx = 512 

ny = 512 

x0, y0 = 9876, 54321 

box = lsst.geom.Box2I(lsst.geom.Point2I(x0, y0), lsst.geom.Extent2I(nx, ny)) 

rampimg = afwImage.ImageF(box) 

dzdx, dzdy, z0 = 0.1, 0.2, 10000.0 

for x in range(nx): 

for y in range(ny): 

rampimg[x, y, afwImage.LOCAL] = dzdx*x + dzdy*y + z0 

 

# check corner, edge, and center pixels 

bctrl = afwMath.BackgroundControl(10, 10) 

bctrl.setNxSample(6) 

bctrl.setNySample(6) 

# large enough to entirely avoid clipping 

bctrl.getStatisticsControl().setNumSigmaClip(20.0) 

bctrl.getStatisticsControl().setNumIter(1) 

backobj = afwMath.makeBackground(rampimg, bctrl) 

 

if debugMode: 

print(rampimg.getArray()) 

 

frame = 1 

for interp in ("CONSTANT", "LINEAR", "NATURAL_SPLINE", "AKIMA_SPLINE"): 

diff = backobj.getImageF(interp) 

if debugMode: 

afwDisplay.Display(frame=frame).mtv(diff, title=self._testMethodName + " diff") 

frame += 1 

diff -= rampimg 

if debugMode: 

print(interp, diff.getArray().mean(), diff.getArray().std()) 

if debugMode: 

afwDisplay.Display(frame=frame).mtv(diff, title=self._testMethodName + " diff-ramping") 

frame += 1 

if debugMode: 

afwDisplay.Display(frame=frame).mtv(rampimg, title=self._testMethodName + " ramping") 

frame += 1 

afwDisplay.Display(frame=frame).mtv(backobj.getStatsImage(), 

title=self._testMethodName + " bkgd StatsImage") 

frame += 1 

 

xpixels = [0, nx//2, nx - 1] 

ypixels = [0, ny//2, ny - 1] 

for xpix in xpixels: 

for ypix in ypixels: 

testval = backobj.getImageF(afwMath.Interpolate.CUBIC_SPLINE)[xpix, ypix, afwImage.LOCAL] 

self.assertAlmostEqual(testval/rampimg[xpix, ypix, afwImage.LOCAL], 1, 6) 

 

# Test pickle 

new = pickle.loads(pickle.dumps(backobj)) 

self.assertBackgroundEqual(backobj, new) 

 

# Check creation of sub-image 

box = lsst.geom.Box2I(lsst.geom.Point2I(123, 45), 

lsst.geom.Extent2I(45, 123)) 

box.shift(lsst.geom.Extent2I(x0, y0)) 

bgImage = backobj.getImageF("AKIMA_SPLINE") 

bgSubImage = afwImage.ImageF(bgImage, box) 

testImage = backobj.getImageF(box, "AKIMA_SPLINE") 

self.assertEqual(testImage.getXY0(), bgSubImage.getXY0()) 

self.assertEqual(testImage.getDimensions(), bgSubImage.getDimensions()) 

self.assertImagesEqual(testImage, bgSubImage) 

 

def getParabolaImage(self, nx, ny, pars=(1.0e-4, 1.0e-4, 0.1, 0.2, 10.0)): 

"""Make sure a quadratic map is *well* reproduced by the spline model 

""" 

parabimg = afwImage.ImageF(lsst.geom.Extent2I(nx, ny)) 

d2zdx2, d2zdy2, dzdx, dzdy, z0 = pars # no cross-terms 

x, y = np.meshgrid(np.arange(nx, dtype=int), np.arange(ny, dtype=int)) 

parabimg.array[:, :] = d2zdx2*x**2 + d2zdy2*y**2 + dzdx*x + dzdy*y + z0 

return parabimg 

 

@unittest.skipIf(AfwdataDir is None, "afwdata not setup") 

def testTicket987(self): 

"""This code used to abort; so the test is that it doesn't""" 

imagePath = os.path.join( 

AfwdataDir, "DC3a-Sim", "sci", "v5-e0", "v5-e0-c011-a00.sci.fits") 

mimg = afwImage.MaskedImageF(imagePath) 

binsize = 512 

nx = int(mimg.getWidth()/binsize) + 1 

ny = int(mimg.getHeight()/binsize) + 1 

bctrl = afwMath.BackgroundControl(nx, ny) 

 

image = mimg.getImage() 

backobj = afwMath.makeBackground(image, bctrl) 

# note: by default undersampleStyle is THROW_EXCEPTION 

image -= backobj.getImageF("NATURAL_SPLINE", "REDUCE_INTERP_ORDER") 

 

def testTicket1781(self): 

"""Test an unusual-sized image""" 

nx = 526 

ny = 154 

 

parabimg = self.getParabolaImage(nx, ny) 

 

bctrl = afwMath.BackgroundControl(16, 4) 

bctrl.getStatisticsControl().setNumSigmaClip(10.0) 

bctrl.getStatisticsControl().setNumIter(1) 

afwMath.makeBackground(parabimg, bctrl) 

 

def testParabola(self): 

"""Test an image which varies parabolicly (spline should be exact for 2rd order polynomial)""" 

nx = 512 

ny = 512 

 

parabimg = self.getParabolaImage(nx, ny) 

 

# check corner, edge, and center pixels 

bctrl = afwMath.BackgroundControl(24, 24) 

bctrl.getStatisticsControl().setNumSigmaClip(10.0) 

bctrl.getStatisticsControl().setNumIter(1) 

backobj = afwMath.makeBackground(parabimg, bctrl) 

 

segmentCenter = int(0.5*nx/bctrl.getNxSample()) 

xpixels = [segmentCenter, nx//2, nx - segmentCenter] 

ypixels = [segmentCenter, ny//2, ny - segmentCenter] 

for xpix in xpixels: 

for ypix in ypixels: 

testval = backobj.getImageF(afwMath.Interpolate.CUBIC_SPLINE)[xpix, ypix] 

realval = parabimg[xpix, ypix, afwImage.LOCAL] 

# quadratic terms skew the averages of the subimages and the clipped mean for 

# a subimage != value of center pixel. 1/20 counts on a 10000 count sky 

# is a fair (if arbitrary) test. 

self.assertLess(abs(testval - realval), 0.5) 

 

@unittest.skipIf(AfwdataDir is None, "afwdata not setup") 

def testCFHT_oldAPI(self): 

"""Test background subtraction on some real CFHT data""" 

mi = afwImage.MaskedImageF(os.path.join( 

AfwdataDir, "CFHT", "D4", "cal-53535-i-797722_1.fits")) 

mi = mi.Factory(mi, 

lsst.geom.Box2I(lsst.geom.Point2I(32, 2), 

lsst.geom.Point2I(2079, 4609)), 

afwImage.LOCAL) 

 

bctrl = afwMath.BackgroundControl(16, 16) 

bctrl.getStatisticsControl().setNumSigmaClip(3.0) 

bctrl.getStatisticsControl().setNumIter(2) 

backobj = afwMath.makeBackground(mi.getImage(), bctrl) 

 

if debugMode: 

afwDisplay.Display(frame=0).mtv(mi, title=self._testMethodName + " image") 

 

im = mi.getImage() 

im -= backobj.getImageF(afwMath.Interpolate.AKIMA_SPLINE) 

 

if debugMode: 

afwDisplay.Display(frame=1).mtv(mi, title=self._testMethodName + " image-back") 

 

def getCfhtImage(self): 

"""Get a portion of a CFHT image as a MaskedImageF""" 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(500, 2000), 

lsst.geom.Point2I(2079, 4609)) 

imagePath = os.path.join( 

AfwdataDir, "CFHT", "D4", "cal-53535-i-797722_1.fits") 

return afwImage.MaskedImageF(imagePath, PropertySet(), bbox) 

 

@unittest.skipIf(AfwdataDir is None, "afwdata not setup") 

def testXY0(self): 

"""Test fitting the background to an image with nonzero xy0 

 

The statsImage and background image should not vary with xy0 

""" 

bgImageList = [] # list of background images, one per xy0 

statsImageList = [] # list of stats images, one per xy0 

for xy0 in (lsst.geom.Point2I(0, 0), 

lsst.geom.Point2I(-100, -999), 

lsst.geom.Point2I(1000, 500)): 

mi = self.getCfhtImage() 

mi.setXY0(xy0) 

 

bctrl = afwMath.BackgroundControl( 

mi.getWidth()//128, mi.getHeight()//128) 

backobj = afwMath.makeBackground(mi.getImage(), bctrl) 

bgImage = backobj.getImageF(afwMath.Interpolate.AKIMA_SPLINE) 

self.assertEqual(bgImage.getBBox(), mi.getBBox()) 

bgImageList.append(bgImage) 

 

statsImage = backobj.getStatsImage() 

statsImageList.append(statsImage) 

 

# changing the bounding box should make no difference to the pixel values, 

# so compare pixels using exact equality 

for bgImage in bgImageList[1:]: 

self.assertImagesEqual(bgImage, bgImageList[0]) 

for statsImage in statsImageList[1:]: 

self.assertMaskedImagesEqual(statsImage, statsImageList[0]) 

 

@unittest.skipIf(AfwdataDir is None, "afwdata not setup") 

def testSubImage(self): 

"""Test getImage on a subregion of the full background image 

 

Using real image data is a cheap way to get a variable background 

""" 

mi = self.getCfhtImage() 

 

bctrl = afwMath.BackgroundControl( 

mi.getWidth()//128, mi.getHeight()//128) 

backobj = afwMath.makeBackground(mi.getImage(), bctrl) 

subBBox = lsst.geom.Box2I(lsst.geom.Point2I(1000, 3000), 

lsst.geom.Extent2I(100, 100)) 

 

bgFullImage = backobj.getImageF(afwMath.Interpolate.AKIMA_SPLINE) 

self.assertEqual(bgFullImage.getBBox(), mi.getBBox()) 

 

subFullArr = afwImage.ImageF(bgFullImage, subBBox).getArray() 

 

bgSubImage = backobj.getImageF(subBBox, afwMath.Interpolate.AKIMA_SPLINE) 

subArr = bgSubImage.getArray() 

 

# the pixels happen to be identical but it is safer not to rely on 

# that; close is good enough 

self.assertFloatsEqual(subArr, subFullArr) 

 

@unittest.skipIf(AfwdataDir is None, "afwdata not setup") 

def testCFHT(self): 

"""Test background subtraction on some real CFHT data""" 

mi = self.getCfhtImage() 

 

bctrl = afwMath.BackgroundControl( 

mi.getWidth()//128, mi.getHeight()//128) 

bctrl.getStatisticsControl().setNumSigmaClip(3.0) 

bctrl.getStatisticsControl().setNumIter(2) 

backobj = afwMath.makeBackground(mi.getImage(), bctrl) 

 

if debugMode: 

afwDisplay.Display(frame=0).mtv(mi, title=self._testMethodName + " image") 

 

im = mi.getImage() 

im -= backobj.getImageF("AKIMA_SPLINE") 

 

if debugMode: 

afwDisplay.Display(frame=1).mtv(mi, title=self._testMethodName + " image-back") 

 

statsImage = backobj.getStatsImage() 

 

if debugMode: 

afwDisplay.Display(frame=2).mtv(statsImage, title=self._testMethodName + " bkgd StatsImage") 

afwDisplay.Display(frame=3).mtv(statsImage.getVariance(), 

title=self._testMethodName + " bkgd Variance") 

 

def testUndersample(self): 

"""Test how the program handles nx,ny being too small for requested interp style.""" 

nx = 64 

ny = 64 

img = afwImage.ImageF(lsst.geom.Extent2I(nx, ny)) 

 

# make a background control object 

bctrl = afwMath.BackgroundControl(10, 10) 

bctrl.setNxSample(3) 

bctrl.setNySample(3) 

 

# put nx,ny back to 2 and see if it adjusts the interp style down to 

# linear 

bctrl.setNxSample(2) 

bctrl.setNySample(2) 

backobj = afwMath.makeBackground(img, bctrl) 

# Need to interpolate background to discover what we actually needed 

backobj.getImageF(afwMath.Interpolate.CUBIC_SPLINE, afwMath.UndersampleStyle.REDUCE_INTERP_ORDER) 

self.assertEqual(backobj.getAsUsedInterpStyle(), 

afwMath.Interpolate.LINEAR) 

 

def tst(img, bctrl): 

backobj = afwMath.makeBackground(img, bctrl) 

# only now do we see that we have too few points 

backobj.getImageF("CUBIC_SPLINE") 

self.assertRaises(lsst.pex.exceptions.InvalidParameterError, 

tst, img, bctrl) 

 

def testOnlyOneGridCell(self): 

"""Test how the program handles nxSample,nySample being 1x1.""" 

# try a ramping image ... has an easy analytic solution 

nx = 64 

ny = 64 

img = afwImage.ImageF(lsst.geom.Extent2I(nx, ny), 10) 

 

dzdx, dzdy, z0 = 0.1, 0.2, 10000.0 

mean = z0 + dzdx*(nx - 1)/2 + dzdy*(ny - 1)/2 # the analytic solution 

for x in range(nx): 

for y in range(ny): 

img[x, y, afwImage.LOCAL] = dzdx*x + dzdy*y + z0 

 

# make a background control object 

bctrl = afwMath.BackgroundControl(10, 10) 

bctrl.setNxSample(1) 

bctrl.setNySample(1) 

bctrl.setUndersampleStyle(afwMath.THROW_EXCEPTION) 

backobj = afwMath.makeBackground(img, bctrl) 

 

xpixels = [0, nx//2, nx - 1] 

ypixels = [0, ny//2, ny - 1] 

for xpix in xpixels: 

for ypix in ypixels: 

testval = backobj.getImageF(afwMath.Interpolate.CONSTANT)[xpix, ypix] 

self.assertAlmostEqual(testval/mean, 1) 

 

def testAdjustLevel(self): 

"""Test that we can adjust a background level""" 

sky = 100 

im = afwImage.ImageF(40, 40) 

im.set(sky) 

nx, ny = im.getWidth()//2, im.getHeight()//2 

bctrl = afwMath.BackgroundControl(nx, ny) 

bkd = afwMath.makeBackground(im, bctrl) 

 

self.assertEqual( 

afwMath.makeStatistics(bkd.getImageF("LINEAR"), afwMath.MEAN).getValue(), 

sky) 

 

delta = 123 

bkd += delta 

self.assertEqual( 

afwMath.makeStatistics(bkd.getImageF("LINEAR"), afwMath.MEAN).getValue(), 

sky + delta) 

bkd -= delta 

self.assertEqual(afwMath.makeStatistics(bkd.getImageF("LINEAR"), afwMath.MEAN).getValue(), 

sky) 

 

def testNaNFromMaskedImage(self): 

"""Check that an extensively masked image doesn't lead to NaNs in the background estimation""" 

image = afwImage.MaskedImageF(800, 800) 

msk = image.getMask() 

bbox = lsst.geom.BoxI(lsst.geom.PointI(560, 0), lsst.geom.PointI(799, 335)) 

smsk = msk.Factory(msk, bbox) 

smsk.set(msk.getPlaneBitMask("DETECTED")) 

 

binSize = 256 

nx = image.getWidth()//binSize + 1 

ny = image.getHeight()//binSize + 1 

 

sctrl = afwMath.StatisticsControl() 

sctrl.setAndMask(reduce(lambda x, y: x | image.getMask().getPlaneBitMask(y), 

['EDGE', 'DETECTED', 'DETECTED_NEGATIVE'], 0x0)) 

 

bctrl = afwMath.BackgroundControl(nx, ny, sctrl, "MEANCLIP") 

 

bkgd = afwMath.makeBackground(image, bctrl) 

bkgdImage = bkgd.getImageF("NATURAL_SPLINE", "THROW_EXCEPTION") 

if debugMode: 

afwDisplay.Display(frame=0).mtv(image, title=self._testMethodName + " image") 

afwDisplay.Display(frame=1).mtv(bkgdImage, title=self._testMethodName + " bkgdImage") 

 

self.assertFalse(np.isnan(bkgdImage[0, 0, afwImage.LOCAL])) 

 

# Check that the non-string API works too 

bkgdImage = bkgd.getImageF( 

afwMath.Interpolate.NATURAL_SPLINE, afwMath.THROW_EXCEPTION) 

 

def testBadAreaFailsSpline(self): 

"""Check that a NaN in the stats image doesn't cause spline interpolation to fail (#2734)""" 

image = afwImage.ImageF(15, 9) 

for y in range(image.getHeight()): 

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

# n.b. linear, which is what the interpolation will fall back 

# to 

image[x, y, afwImage.LOCAL] = 1 + 2*y 

 

# Set the right corner to NaN. This will mean that we have too few 

# points for a spline interpolator 

binSize = 3 

image[-binSize:, -binSize:, afwImage.LOCAL] = np.nan 

 

nx = image.getWidth()//binSize 

ny = image.getHeight()//binSize 

 

sctrl = afwMath.StatisticsControl() 

bctrl = afwMath.BackgroundControl(nx, ny, sctrl, afwMath.MEANCLIP) 

 

bkgd = afwMath.makeBackground(image, bctrl) 

if debugMode: 

afwDisplay.Display(frame=0).mtv(image, title=self._testMethodName + " image") 

afwDisplay.Display(frame=1).mtv(bkgd.getStatsImage(), 

title=self._testMethodName + " bkgd StatsImage") 

# Should throw if we don't permit REDUCE_INTERP_ORDER 

self.assertRaises(lsst.pex.exceptions.OutOfRangeError, 

bkgd.getImageF, afwMath.Interpolate.NATURAL_SPLINE) 

# The interpolation should fall back to linear for the right part of the image 

# where the NaNs don't permit spline interpolation (n.b. this happens 

# to be exact) 

bkgdImage = bkgd.getImageF( 

afwMath.Interpolate.NATURAL_SPLINE, afwMath.REDUCE_INTERP_ORDER) 

 

if debugMode: 

afwDisplay.Display(frame=2).mtv(bkgdImage, title=self._testMethodName + " bkgdImage") 

 

image -= bkgdImage 

self.assertEqual(afwMath.makeStatistics(image, afwMath.MEAN).getValue(), 

0.0) 

 

def testBadPatch(self): 

"""Test that a large bad patch of an image doesn't cause an absolute failure""" 

initialValue = 20 

mi = afwImage.MaskedImageF(500, 200) 

mi.set((initialValue, 0x0, 1.0)) 

mi.image[0:200, :] = np.nan 

badBits = mi.mask.getPlaneBitMask( 

['EDGE', 'DETECTED', 'DETECTED_NEGATIVE']) 

mi.mask[0:400, :] |= badBits 

 

if debugMode: 

afwDisplay.Display(frame=0).mtv(mi, title=self._testMethodName + " image") 

 

sctrl = afwMath.StatisticsControl() 

sctrl.setAndMask(badBits) 

nx, ny = 17, 17 

bctrl = afwMath.BackgroundControl(nx, ny, sctrl, afwMath.MEANCLIP) 

 

bkgd = afwMath.makeBackground(mi, bctrl) 

statsImage = bkgd.getStatsImage() 

if debugMode: 

afwDisplay.Display(frame=1).mtv(statsImage, title=self._testMethodName + " bkgd StatsImage") 

 

# the test is that this doesn't fail if the bug (#2297) is fixed 

bkgdImage = bkgd.getImageF( 

afwMath.Interpolate.NATURAL_SPLINE, afwMath.REDUCE_INTERP_ORDER) 

self.assertEqual( 

np.mean(bkgdImage[0:100, 0:100].array), initialValue) 

if debugMode: 

afwDisplay.Display(frame=2).mtv(bkgdImage, title=self._testMethodName + " bkgdImage") 

# Check that we can fix the NaNs in the statsImage 

sim = statsImage.getImage().getArray() 

sim[np.isnan(sim)] = initialValue # replace NaN by initialValue 

bkgdImage = bkgd.getImageF( 

afwMath.Interpolate.NATURAL_SPLINE, afwMath.REDUCE_INTERP_ORDER) 

 

self.assertAlmostEqual( 

np.mean(bkgdImage[0:100, 0:100].array, dtype=np.float64), 

initialValue) 

 

def testBadRows(self): 

"""Test that a bad set of rows in an image doesn't cause a failure""" 

initialValue = 20 

mi = afwImage.MaskedImageF(500, 200) 

mi.set((initialValue, 0x0, 1.0)) 

mi.image[:, 0:100] = np.nan 

badBits = mi.mask.getPlaneBitMask( 

['EDGE', 'DETECTED', 'DETECTED_NEGATIVE']) 

mi.mask[0:400, :] |= badBits 

 

if debugMode: 

afwDisplay.Display(frame=0).mtv(mi, title=self._testMethodName + " image") 

 

sctrl = afwMath.StatisticsControl() 

sctrl.setAndMask(badBits) 

nx, ny = 17, 17 

bctrl = afwMath.BackgroundControl(nx, ny, sctrl, afwMath.MEANCLIP) 

 

bkgd = afwMath.makeBackground(mi, bctrl) 

statsImage = bkgd.getStatsImage() 

if debugMode: 

afwDisplay.Display(frame=1).mtv(statsImage, title=self._testMethodName + " bkgd StatsImage") 

 

# the test is that this doesn't fail if the bug (#2297) is fixed 

for frame, interpStyle in enumerate([afwMath.Interpolate.CONSTANT, afwMath.Interpolate.LINEAR, 

afwMath.Interpolate.NATURAL_SPLINE, 

afwMath.Interpolate.AKIMA_SPLINE], 2): 

bkgdImage = bkgd.getImageF( 

interpStyle, afwMath.REDUCE_INTERP_ORDER) 

self.assertEqual( 

np.mean(bkgdImage[0:100, 0:100].array), initialValue) 

if debugMode: 

afwDisplay.Display(frame=frame).mtv(bkgdImage, title=self._testMethodName + " bkgdImage: " + 

interpStyle.__str__()) 

 

def testBadImage(self): 

"""Test that an entirely bad image doesn't cause an absolute failure""" 

initialValue = 20 

mi = afwImage.MaskedImageF(500, 200) 

# Check that no good values don't crash (they return NaN), and that a single good value 

# is enough to redeem the entire image 

for pix00 in [np.nan, initialValue]: 

mi.image[:] = np.nan 

mi.image[0, 0] = pix00 

 

sctrl = afwMath.StatisticsControl() 

nx, ny = 17, 17 

bctrl = afwMath.BackgroundControl(nx, ny, sctrl, afwMath.MEANCLIP) 

 

bkgd = afwMath.makeBackground(mi, bctrl) 

 

for interpStyle in [afwMath.Interpolate.CONSTANT, afwMath.Interpolate.LINEAR, 

afwMath.Interpolate.NATURAL_SPLINE, afwMath.Interpolate.AKIMA_SPLINE]: 

# the test is that this doesn't fail if the bug (#2297) is 

# fixed 

bkgdImage = bkgd.getImageF( 

interpStyle, afwMath.REDUCE_INTERP_ORDER) 

val = np.mean(bkgdImage[0:100, 0:100].array) 

 

if np.isfinite(pix00): 

self.assertEqual(val, pix00) 

else: 

self.assertTrue(np.isnan(val)) 

 

def testBackgroundFromStatsImage(self): 

"""Check that we can rebuild a Background from a BackgroundMI.getStatsImage()""" 

bgCtrl = afwMath.BackgroundControl(10, 10) 

bkgd = afwMath.makeBackground(self.image, bgCtrl) 

 

interpStyle = afwMath.Interpolate.AKIMA_SPLINE 

undersampleStyle = afwMath.REDUCE_INTERP_ORDER 

bkgdImage = bkgd.getImageF(interpStyle, undersampleStyle) 

self.assertEqual(np.mean(bkgdImage.getArray()), self.val) 

self.assertEqual(interpStyle, bkgd.getAsUsedInterpStyle()) 

self.assertEqual(undersampleStyle, bkgd.getAsUsedUndersampleStyle()) 

 

# OK, we have our background. Make a copy 

bkgd2 = afwMath.BackgroundMI( 

self.image.getBBox(), bkgd.getStatsImage()) 

del bkgd # we should be handling the memory correctly, but let's check 

bkgdImage2 = bkgd2.getImageF(interpStyle) 

 

self.assertEqual(np.mean(bkgdImage2.getArray()), self.val) 

 

def testBackgroundList(self): 

"""Test that a BackgroundLists behaves like a list""" 

bgCtrl = afwMath.BackgroundControl(10, 10) 

interpStyle = afwMath.Interpolate.AKIMA_SPLINE 

undersampleStyle = afwMath.REDUCE_INTERP_ORDER 

approxStyle = afwMath.ApproximateControl.UNKNOWN 

approxOrderX = 0 

approxOrderY = 0 

approxWeighting = False 

 

backgroundList = afwMath.BackgroundList() 

 

for i in range(2): 

bkgd = afwMath.makeBackground(self.image, bgCtrl) 

if i == 0: 

# no need to call getImage 

backgroundList.append((bkgd, interpStyle, undersampleStyle, 

approxStyle, approxOrderX, approxOrderY, approxWeighting)) 

else: 

# Relies on having called getImage; deprecated 

with self.assertWarns(FutureWarning): 

backgroundList.append(bkgd) 

 

def assertBackgroundList(bgl): 

self.assertEqual(len(bgl), 2) # check that len() works 

for a in bgl: # check that we can iterate 

pass 

self.assertEqual(len(bgl[0]), 7) # check that we can index 

# check that we always have a tuple (bkgd, interp, under, 

# approxStyle, orderX, orderY, weighting) 

self.assertEqual(len(bgl[1]), 7) 

 

assertBackgroundList(backgroundList) 

 

# Check pickling 

new = pickle.loads(pickle.dumps(backgroundList)) 

assertBackgroundList(new) 

self.assertEqual(len(new), len(backgroundList)) 

for i, j in zip(new, backgroundList): 

self.assertBackgroundEqual(i[0], j[0]) 

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

 

def assertBackgroundEqual(self, lhs, rhs): 

lhsStats, rhsStats = lhs.getStatsImage(), rhs.getStatsImage() 

self.assertEqual(lhs.getImageBBox(), rhs.getImageBBox()) 

self.assertMaskedImagesEqual(lhsStats, rhsStats) 

lhsImage, rhsImage = lhs.getImageF("LINEAR"), rhs.getImageF("LINEAR") 

self.assertImagesEqual(lhsImage, rhsImage) 

 

def testApproximate(self): 

"""Test I/O for BackgroundLists with Approximate""" 

# approx and interp should be very close, but not the same 

img = self.getParabolaImage(256, 256) 

 

# try regular interpolated image (the default) 

interpStyle = afwMath.Interpolate.AKIMA_SPLINE 

undersampleStyle = afwMath.REDUCE_INTERP_ORDER 

bgCtrl = afwMath.BackgroundControl(6, 6) 

bgCtrl.setUndersampleStyle(undersampleStyle) 

bkgd = afwMath.makeBackground(img, bgCtrl) 

interpImage = bkgd.getImageF(interpStyle) 

 

with lsst.utils.tests.getTempFilePath("_bgi.fits") as bgiFile, \ 

lsst.utils.tests.getTempFilePath("_bga.fits") as bgaFile: 

bglInterp = afwMath.BackgroundList() 

bglInterp.append((bkgd, interpStyle, undersampleStyle, 

afwMath.ApproximateControl.UNKNOWN, 0, 0, True)) 

bglInterp.writeFits(bgiFile) 

 

# try an approx background 

approxStyle = afwMath.ApproximateControl.CHEBYSHEV 

approxOrder = 2 

actrl = afwMath.ApproximateControl(approxStyle, approxOrder) 

bkgd.getBackgroundControl().setApproximateControl(actrl) 

approxImage = bkgd.getImageF(interpStyle) 

bglApprox = afwMath.BackgroundList() 

bglApprox.append((bkgd, interpStyle, undersampleStyle, 

approxStyle, approxOrder, approxOrder, True)) 

bglApprox.writeFits(bgaFile) 

 

# take a difference and make sure the two are very similar 

interpNp = interpImage.getArray() 

diff = np.abs(interpNp - approxImage.getArray())/interpNp 

 

# the image and interp/approx parameters are chosen so these limits 

# will be greater than machine precision for float. The two methods 

# should be measurably different (so we know we're not just getting the 

# same thing from the getImage() method. But they should be very close 

# since they're both doing the same sort of thing. 

tolSame = 1.0e-3 # should be the same to this order 

tolDiff = 1.0e-4 # should be different here 

self.assertLess(diff.max(), tolSame) 

self.assertGreater(diff.max(), tolDiff) 

 

# now see if we can reload them from files and get the same images 

# we wrote 

interpImage2 = afwMath.BackgroundList().readFits(bgiFile).getImage() 

approxImage2 = afwMath.BackgroundList().readFits(bgaFile).getImage() 

 

idiff = interpImage.getArray() - interpImage2.getArray() 

adiff = approxImage.getArray() - approxImage2.getArray() 

self.assertEqual(idiff.max(), 0.0) 

self.assertEqual(adiff.max(), 0.0) 

 

def testBackgroundListIO(self): 

"""Test I/O for BackgroundLists""" 

bgCtrl = afwMath.BackgroundControl(10, 10) 

interpStyle = afwMath.Interpolate.AKIMA_SPLINE 

undersampleStyle = afwMath.REDUCE_INTERP_ORDER 

approxOrderX = 6 

approxOrderY = 6 

approxWeighting = True 

 

im = self.image.Factory( 

self.image, self.image.getBBox()) 

arr = im.getArray() 

arr += np.random.normal(size=(im.getHeight(), im.getWidth())) 

 

for astyle in afwMath.ApproximateControl.UNKNOWN, afwMath.ApproximateControl.CHEBYSHEV: 

actrl = afwMath.ApproximateControl(astyle, approxOrderX) 

bgCtrl.setApproximateControl(actrl) 

 

backgroundList = afwMath.BackgroundList() 

backImage = afwImage.ImageF(im.getDimensions()) 

for i in range(2): 

bkgd = afwMath.makeBackground(im, bgCtrl) 

if i == 0: 

# no need to call getImage 

backgroundList.append((bkgd, interpStyle, undersampleStyle, 

astyle, approxOrderX, approxOrderY, approxWeighting)) 

else: 

# Relies on having called getImage; deprecated 

with self.assertWarns(FutureWarning): 

backgroundList.append(bkgd) 

 

backImage += bkgd.getImageF(interpStyle, undersampleStyle) 

 

with lsst.utils.tests.getTempFilePath(".fits") as fileName: 

backgroundList.writeFits(fileName) 

 

backgrounds = afwMath.BackgroundList.readFits(fileName) 

 

img = backgrounds.getImage() 

# Check that the read-back image is identical to that generated from the backgroundList 

# round-tripped to disk 

backImage -= img 

 

self.assertEqual(np.min(backImage.getArray()), 0.0) 

self.assertEqual(np.max(backImage.getArray()), 0.0) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()