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

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

# 

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

# 

 

""" 

Test lsst.afw.image.Exposure 

""" 

 

import os.path 

import unittest 

 

import numpy as np 

from numpy.testing import assert_allclose 

 

import lsst.utils 

import lsst.utils.tests 

import lsst.geom 

import lsst.afw.image as afwImage 

from lsst.afw.image.utils import defineFilter 

from lsst.afw.coord import Weather 

import lsst.afw.geom as afwGeom 

import lsst.afw.table as afwTable 

import lsst.pex.exceptions as pexExcept 

from lsst.afw.fits import readMetadata, FitsError 

from lsst.afw.cameraGeom.testUtils import DetectorWrapper 

from lsst.log import Log 

from testTableArchivesLib import DummyPsf 

 

Log.getLogger("afw.image.Mask").setLevel(Log.INFO) 

 

try: 

dataDir = os.path.join(lsst.utils.getPackageDir("afwdata"), "data") 

except pexExcept.NotFoundError: 

dataDir = None 

else: 

InputMaskedImageName = "871034p_1_MI.fits" 

InputMaskedImageNameSmall = "small_MI.fits" 

InputImageNameSmall = "small" 

OutputMaskedImageName = "871034p_1_MInew.fits" 

 

currDir = os.path.abspath(os.path.dirname(__file__)) 

inFilePath = os.path.join(dataDir, InputMaskedImageName) 

inFilePathSmall = os.path.join(dataDir, InputMaskedImageNameSmall) 

inFilePathSmallImage = os.path.join(dataDir, InputImageNameSmall) 

 

 

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

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

""" 

A test case for the Exposure Class 

""" 

 

def setUp(self): 

maskedImage = afwImage.MaskedImageF(inFilePathSmall) 

maskedImageMD = readMetadata(inFilePathSmall) 

 

self.smallExposure = afwImage.ExposureF(inFilePathSmall) 

self.width = maskedImage.getWidth() 

self.height = maskedImage.getHeight() 

self.wcs = afwGeom.makeSkyWcs(maskedImageMD, False) 

self.md = maskedImageMD 

self.psf = DummyPsf(2.0) 

self.detector = DetectorWrapper().detector 

self.extras = {"misc": DummyPsf(3.5)} 

 

self.exposureBlank = afwImage.ExposureF() 

self.exposureMiOnly = afwImage.makeExposure(maskedImage) 

self.exposureMiWcs = afwImage.makeExposure(maskedImage, self.wcs) 

# n.b. the (100, 100, ...) form 

self.exposureCrWcs = afwImage.ExposureF(100, 100, self.wcs) 

# test with ExtentI(100, 100) too 

self.exposureCrOnly = afwImage.ExposureF(lsst.geom.ExtentI(100, 100)) 

 

afwImage.Filter.reset() 

afwImage.FilterProperty.reset() 

 

defineFilter("g", 470.0) 

 

def tearDown(self): 

del self.smallExposure 

del self.wcs 

del self.psf 

del self.detector 

del self.extras 

 

del self.exposureBlank 

del self.exposureMiOnly 

del self.exposureMiWcs 

del self.exposureCrWcs 

del self.exposureCrOnly 

 

def testGetMaskedImage(self): 

""" 

Test to ensure a MaskedImage can be obtained from each 

Exposure. An Exposure is required to have a MaskedImage, 

therefore each of the Exposures should return a MaskedImage. 

 

MaskedImage class should throw appropriate 

lsst::pex::exceptions::NotFound if the MaskedImage can not be 

obtained. 

""" 

maskedImageBlank = self.exposureBlank.getMaskedImage() 

blankWidth = maskedImageBlank.getWidth() 

blankHeight = maskedImageBlank.getHeight() 

if blankWidth != blankHeight != 0: 

self.fail("%s = %s != 0" % (blankWidth, blankHeight)) 

 

maskedImageMiOnly = self.exposureMiOnly.getMaskedImage() 

miOnlyWidth = maskedImageMiOnly.getWidth() 

miOnlyHeight = maskedImageMiOnly.getHeight() 

self.assertAlmostEqual(miOnlyWidth, self.width) 

self.assertAlmostEqual(miOnlyHeight, self.height) 

 

# NOTE: Unittests for Exposures created from a MaskedImage and 

# a WCS object are incomplete. No way to test the validity of 

# the WCS being copied/created. 

 

maskedImageMiWcs = self.exposureMiWcs.getMaskedImage() 

miWcsWidth = maskedImageMiWcs.getWidth() 

miWcsHeight = maskedImageMiWcs.getHeight() 

self.assertAlmostEqual(miWcsWidth, self.width) 

self.assertAlmostEqual(miWcsHeight, self.height) 

 

maskedImageCrWcs = self.exposureCrWcs.getMaskedImage() 

crWcsWidth = maskedImageCrWcs.getWidth() 

crWcsHeight = maskedImageCrWcs.getHeight() 

if crWcsWidth != crWcsHeight != 0: 

self.fail("%s != %s != 0" % (crWcsWidth, crWcsHeight)) 

 

maskedImageCrOnly = self.exposureCrOnly.getMaskedImage() 

crOnlyWidth = maskedImageCrOnly.getWidth() 

crOnlyHeight = maskedImageCrOnly.getHeight() 

if crOnlyWidth != crOnlyHeight != 0: 

self.fail("%s != %s != 0" % (crOnlyWidth, crOnlyHeight)) 

 

# Check Exposure.getWidth() returns the MaskedImage's width 

self.assertEqual(crOnlyWidth, self.exposureCrOnly.getWidth()) 

self.assertEqual(crOnlyHeight, self.exposureCrOnly.getHeight()) 

 

def testProperties(self): 

self.assertMaskedImagesEqual(self.exposureMiOnly.maskedImage, 

self.exposureMiOnly.getMaskedImage()) 

mi2 = afwImage.MaskedImageF(self.exposureMiOnly.getDimensions()) 

mi2.image.array[:] = 5.0 

mi2.variance.array[:] = 3.0 

mi2.mask.array[:] = 0x1 

self.exposureMiOnly.maskedImage = mi2 

self.assertMaskedImagesEqual(self.exposureMiOnly.maskedImage, mi2) 

self.assertImagesEqual(self.exposureMiOnly.image, 

self.exposureMiOnly.maskedImage.image) 

 

image3 = afwImage.ImageF(self.exposureMiOnly.getDimensions()) 

image3.array[:] = 3.0 

self.exposureMiOnly.image = image3 

self.assertImagesEqual(self.exposureMiOnly.image, image3) 

 

mask3 = afwImage.MaskX(self.exposureMiOnly.getDimensions()) 

mask3.array[:] = 0x2 

self.exposureMiOnly.mask = mask3 

self.assertMasksEqual(self.exposureMiOnly.mask, mask3) 

 

var3 = afwImage.ImageF(self.exposureMiOnly.getDimensions()) 

var3.array[:] = 2.0 

self.exposureMiOnly.variance = var3 

self.assertImagesEqual(self.exposureMiOnly.variance, var3) 

 

def testGetWcs(self): 

""" 

Test if a WCS can be obtained from each Exposure created with 

a WCS. 

 

Test that appropriate exceptions are thrown if a WCS is 

requested from an Exposure that was not created with a WCS. 

Python turns the pex::exceptions in the Exposure and 

MaskedImage classes into IndexErrors. 

 

The exposureBlank, exposureMiOnly, and exposureCrOnly 

Exposures should throw a lsst::pex::exceptions::NotFound. 

""" 

 

self.assertFalse(self.exposureBlank.getWcs()) 

self.assertFalse(self.exposureMiOnly.getWcs()) 

 

# These two should pass 

self.exposureMiWcs.getWcs() 

self.exposureCrWcs.getWcs() 

 

self.assertFalse(self.exposureCrOnly.getWcs()) 

 

def testExposureInfoConstructor(self): 

"""Test the Exposure(maskedImage, exposureInfo) constructor""" 

exposureInfo = afwImage.ExposureInfo() 

exposureInfo.setWcs(self.wcs) 

exposureInfo.setDetector(self.detector) 

gFilter = afwImage.Filter("g") 

exposureInfo.setFilter(gFilter) 

maskedImage = afwImage.MaskedImageF(inFilePathSmall) 

exposure = afwImage.ExposureF(maskedImage, exposureInfo) 

 

self.assertTrue(exposure.hasWcs()) 

self.assertEqual(exposure.getWcs().getPixelOrigin(), 

self.wcs.getPixelOrigin()) 

self.assertEqual(exposure.getDetector().getName(), 

self.detector.getName()) 

self.assertEqual(exposure.getDetector().getSerial(), 

self.detector.getSerial()) 

self.assertEqual(exposure.getFilter(), gFilter) 

 

self.assertTrue(exposure.getInfo().hasWcs()) 

self.assertEqual(exposure.getInfo().getWcs().getPixelOrigin(), 

self.wcs.getPixelOrigin()) 

self.assertEqual(exposure.getInfo().getDetector().getName(), 

self.detector.getName()) 

self.assertEqual(exposure.getInfo().getDetector().getSerial(), 

self.detector.getSerial()) 

self.assertEqual(exposure.getInfo().getFilter(), gFilter) 

 

def testNullWcs(self): 

"""Test that an Exposure constructed with second argument None is usable 

 

When the exposureInfo constructor was first added, trying to get a WCS 

or other info caused a segfault because the ExposureInfo did not exist. 

""" 

maskedImage = self.exposureMiOnly.getMaskedImage() 

exposure = afwImage.ExposureF(maskedImage, None) 

self.assertFalse(exposure.hasWcs()) 

self.assertFalse(exposure.hasPsf()) 

 

def testExposureInfoSetNone(self): 

exposureInfo = afwImage.ExposureInfo() 

exposureInfo.setDetector(None) 

exposureInfo.setValidPolygon(None) 

exposureInfo.setPsf(None) 

exposureInfo.setWcs(None) 

exposureInfo.setPhotoCalib(None) 

exposureInfo.setCoaddInputs(None) 

exposureInfo.setVisitInfo(None) 

exposureInfo.setApCorrMap(None) 

for key in self.extras: 

exposureInfo.setComponent(key, None) 

 

def testSetExposureInfo(self): 

exposureInfo = afwImage.ExposureInfo() 

exposureInfo.setWcs(self.wcs) 

exposureInfo.setDetector(self.detector) 

gFilter = afwImage.Filter("g") 

exposureInfo.setFilter(gFilter) 

maskedImage = afwImage.MaskedImageF(inFilePathSmall) 

exposure = afwImage.ExposureF(maskedImage) 

self.assertFalse(exposure.hasWcs()) 

 

exposure.setInfo(exposureInfo) 

 

self.assertTrue(exposure.hasWcs()) 

self.assertEqual(exposure.getWcs().getPixelOrigin(), 

self.wcs.getPixelOrigin()) 

self.assertEqual(exposure.getDetector().getName(), 

self.detector.getName()) 

self.assertEqual(exposure.getDetector().getSerial(), 

self.detector.getSerial()) 

self.assertEqual(exposure.getFilter(), gFilter) 

 

def testVisitInfoFitsPersistence(self): 

"""Test saving an exposure to FITS and reading it back in preserves (some) VisitInfo fields""" 

exposureId = 5 

exposureTime = 12.3 

boresightRotAngle = 45.6 * lsst.geom.degrees 

weather = Weather(1.1, 2.2, 0.3) 

visitInfo = afwImage.VisitInfo( 

exposureId=exposureId, 

exposureTime=exposureTime, 

boresightRotAngle=boresightRotAngle, 

weather=weather, 

) 

photoCalib = afwImage.PhotoCalib(3.4, 5.6) 

exposureInfo = afwImage.ExposureInfo() 

exposureInfo.setVisitInfo(visitInfo) 

exposureInfo.setPhotoCalib(photoCalib) 

exposureInfo.setDetector(self.detector) 

gFilter = afwImage.Filter("g") 

exposureInfo.setFilter(gFilter) 

maskedImage = afwImage.MaskedImageF(inFilePathSmall) 

exposure = afwImage.ExposureF(maskedImage, exposureInfo) 

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

exposure.writeFits(tmpFile) 

rtExposure = afwImage.ExposureF(tmpFile) 

rtVisitInfo = rtExposure.getInfo().getVisitInfo() 

self.assertEqual(rtVisitInfo.getWeather(), weather) 

self.assertEqual(rtExposure.getPhotoCalib(), photoCalib) 

self.assertEqual(rtExposure.getFilter(), gFilter) 

 

def testSetMembers(self): 

""" 

Test that the MaskedImage and the WCS of an Exposure can be set. 

""" 

exposure = afwImage.ExposureF() 

 

maskedImage = afwImage.MaskedImageF(inFilePathSmall) 

exposure.setMaskedImage(maskedImage) 

exposure.setWcs(self.wcs) 

exposure.setDetector(self.detector) 

exposure.setFilter(afwImage.Filter("g")) 

 

self.assertEqual(exposure.getDetector().getName(), 

self.detector.getName()) 

self.assertEqual(exposure.getDetector().getSerial(), 

self.detector.getSerial()) 

self.assertEqual(exposure.getFilter().getName(), "g") 

self.assertEqual(exposure.getWcs(), self.wcs) 

 

# The PhotoCalib tests are in test_photoCalib.py; 

# here we just check that it's gettable and settable. 

self.assertIsNone(exposure.getPhotoCalib()) 

 

photoCalib = afwImage.PhotoCalib(511.1, 44.4) 

exposure.setPhotoCalib(photoCalib) 

self.assertEqual(exposure.getPhotoCalib(), photoCalib) 

 

# Psfs next 

self.assertFalse(exposure.hasPsf()) 

exposure.setPsf(self.psf) 

self.assertTrue(exposure.hasPsf()) 

 

exposure.setPsf(DummyPsf(1.0)) # we can reset the Psf 

 

# extras next 

info = exposure.getInfo() 

for key, value in self.extras.items(): 

self.assertFalse(info.hasComponent(key)) 

self.assertIsNone(info.getComponent(key)) 

info.setComponent(key, value) 

self.assertTrue(info.hasComponent(key)) 

self.assertEqual(info.getComponent(key), value) 

info.removeComponent(key) 

self.assertFalse(info.hasComponent(key)) 

 

# Test that we can set the MaskedImage and WCS of an Exposure 

# that already has both 

self.exposureMiWcs.setMaskedImage(maskedImage) 

exposure.setWcs(self.wcs) 

 

def testHasWcs(self): 

""" 

Test if an Exposure has a WCS or not. 

""" 

self.assertFalse(self.exposureBlank.hasWcs()) 

 

self.assertFalse(self.exposureMiOnly.hasWcs()) 

self.assertTrue(self.exposureMiWcs.hasWcs()) 

self.assertTrue(self.exposureCrWcs.hasWcs()) 

self.assertFalse(self.exposureCrOnly.hasWcs()) 

 

def testGetSubExposure(self): 

""" 

Test that a subExposure of the original Exposure can be obtained. 

 

The MaskedImage class should throw a 

lsst::pex::exceptions::InvalidParameter if the requested 

subRegion is not fully contained within the original 

MaskedImage. 

 

""" 

# 

# This subExposure is valid 

# 

subBBox = lsst.geom.Box2I(lsst.geom.Point2I(40, 50), 

lsst.geom.Extent2I(10, 10)) 

subExposure = self.exposureCrWcs.Factory( 

self.exposureCrWcs, subBBox, afwImage.LOCAL) 

 

self.checkWcs(self.exposureCrWcs, subExposure) 

 

# this subRegion is not valid and should trigger an exception 

# from the MaskedImage class and should trigger an exception 

# from the WCS class for the MaskedImage 871034p_1_MI. 

 

subRegion3 = lsst.geom.Box2I(lsst.geom.Point2I(100, 100), 

lsst.geom.Extent2I(10, 10)) 

 

def getSubRegion(): 

self.exposureCrWcs.Factory( 

self.exposureCrWcs, subRegion3, afwImage.LOCAL) 

 

self.assertRaises(pexExcept.LengthError, getSubRegion) 

 

# this subRegion is not valid and should trigger an exception 

# from the MaskedImage class only for the MaskedImage small_MI. 

# small_MI (cols, rows) = (256, 256) 

 

subRegion4 = lsst.geom.Box2I(lsst.geom.Point2I(250, 250), 

lsst.geom.Extent2I(10, 10)) 

 

def getSubRegion(): 

self.exposureCrWcs.Factory( 

self.exposureCrWcs, subRegion4, afwImage.LOCAL) 

 

self.assertRaises(pexExcept.LengthError, getSubRegion) 

 

# check the sub- and parent- exposures are using the same Wcs 

# transformation 

subBBox = lsst.geom.Box2I(lsst.geom.Point2I(40, 50), 

lsst.geom.Extent2I(10, 10)) 

subExposure = self.exposureCrWcs.Factory( 

self.exposureCrWcs, subBBox, afwImage.LOCAL) 

parentSkyPos = self.exposureCrWcs.getWcs().pixelToSky(0, 0) 

 

subExpSkyPos = subExposure.getWcs().pixelToSky(0, 0) 

 

self.assertSpherePointsAlmostEqual(parentSkyPos, subExpSkyPos, msg="Wcs in sub image has changed") 

 

def testReadWriteFits(self): 

"""Test readFits and writeFits. 

""" 

# This should pass without an exception 

mainExposure = afwImage.ExposureF(inFilePathSmall) 

mainExposure.setDetector(self.detector) 

 

subBBox = lsst.geom.Box2I(lsst.geom.Point2I(10, 10), 

lsst.geom.Extent2I(40, 50)) 

subExposure = mainExposure.Factory( 

mainExposure, subBBox, afwImage.LOCAL) 

self.checkWcs(mainExposure, subExposure) 

det = subExposure.getDetector() 

self.assertTrue(det) 

 

subExposure = afwImage.ExposureF( 

inFilePathSmall, subBBox, afwImage.LOCAL) 

 

self.checkWcs(mainExposure, subExposure) 

 

# This should throw an exception 

def getExposure(): 

afwImage.ExposureF(inFilePathSmallImage) 

 

self.assertRaises(FitsError, getExposure) 

 

mainExposure.setPsf(self.psf) 

 

# Make sure we can write without an exception 

photoCalib = afwImage.PhotoCalib(1e-10, 1e-12) 

mainExposure.setPhotoCalib(photoCalib) 

 

mainInfo = mainExposure.getInfo() 

for key, value in self.extras.items(): 

mainInfo.setComponent(key, value) 

 

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

mainExposure.writeFits(tmpFile) 

 

readExposure = type(mainExposure)(tmpFile) 

 

# 

# Check the round-tripping 

# 

self.assertEqual(mainExposure.getFilter().getName(), 

readExposure.getFilter().getName()) 

 

self.assertEqual(photoCalib, readExposure.getPhotoCalib()) 

 

readInfo = readExposure.getInfo() 

for key, value in self.extras.items(): 

self.assertEqual(value, readInfo.getComponent(key)) 

 

psf = readExposure.getPsf() 

self.assertIsNotNone(psf) 

self.assertEqual(psf, self.psf) 

 

def checkWcs(self, parentExposure, subExposure): 

"""Compare WCS at corner points of a sub-exposure and its parent exposure 

By using the function indexToPosition, we should be able to convert the indices 

(of the four corners (of the sub-exposure)) to positions and use the wcs 

to get the same sky coordinates for each. 

""" 

subMI = subExposure.getMaskedImage() 

subDim = subMI.getDimensions() 

 

# Note: pixel positions must be computed relative to XY0 when working 

# with WCS 

mainWcs = parentExposure.getWcs() 

subWcs = subExposure.getWcs() 

 

for xSubInd in (0, subDim.getX()-1): 

for ySubInd in (0, subDim.getY()-1): 

self.assertSpherePointsAlmostEqual( 

mainWcs.pixelToSky( 

afwImage.indexToPosition(xSubInd), 

afwImage.indexToPosition(ySubInd), 

), 

subWcs.pixelToSky( 

afwImage.indexToPosition(xSubInd), 

afwImage.indexToPosition(ySubInd), 

)) 

 

def cmpExposure(self, e1, e2): 

self.assertEqual(e1.getDetector().getName(), 

e2.getDetector().getName()) 

self.assertEqual(e1.getDetector().getSerial(), 

e2.getDetector().getSerial()) 

self.assertEqual(e1.getFilter().getName(), e2.getFilter().getName()) 

xy = lsst.geom.Point2D(0, 0) 

self.assertEqual(e1.getWcs().pixelToSky(xy)[0], 

e2.getWcs().pixelToSky(xy)[0]) 

self.assertEqual(e1.getPhotoCalib(), e2.getPhotoCalib()) 

# check PSF identity 

if not e1.getPsf(): 

self.assertFalse(e2.getPsf()) 

else: 

self.assertEqual(e1.getPsf(), e2.getPsf()) 

# Check extra components 

i1 = e1.getInfo() 

i2 = e2.getInfo() 

for key in self.extras: 

self.assertEqual(i1.hasComponent(key), i2.hasComponent(key)) 

if i1.hasComponent(key): 

self.assertEqual(i1.getComponent(key), i2.getComponent(key)) 

 

def testCopyExposure(self): 

"""Copy an Exposure (maybe changing type)""" 

 

exposureU = afwImage.ExposureU(inFilePathSmall, allowUnsafe=True) 

exposureU.setWcs(self.wcs) 

exposureU.setDetector(self.detector) 

exposureU.setFilter(afwImage.Filter("g")) 

exposureU.setPsf(DummyPsf(4.0)) 

infoU = exposureU.getInfo() 

for key, value in self.extras.items(): 

infoU.setComponent(key, value) 

 

exposureF = exposureU.convertF() 

self.cmpExposure(exposureF, exposureU) 

 

nexp = exposureF.Factory(exposureF, False) 

self.cmpExposure(exposureF, nexp) 

 

# Ensure that the copy was deep. 

# (actually this test is invalid since getDetector() returns a shared_ptr) 

# cen0 = exposureU.getDetector().getCenterPixel() 

# x0,y0 = cen0 

# det = exposureF.getDetector() 

# det.setCenterPixel(lsst.geom.Point2D(999.0, 437.8)) 

# self.assertEqual(exposureU.getDetector().getCenterPixel()[0], x0) 

# self.assertEqual(exposureU.getDetector().getCenterPixel()[1], y0) 

 

def testDeepCopyData(self): 

"""Make sure a deep copy of an Exposure has its own data (ticket #2625) 

""" 

exp = afwImage.ExposureF(6, 7) 

mi = exp.getMaskedImage() 

mi.getImage().set(100) 

mi.getMask().set(5) 

mi.getVariance().set(200) 

 

expCopy = exp.clone() 

miCopy = expCopy.getMaskedImage() 

miCopy.getImage().set(-50) 

miCopy.getMask().set(2) 

miCopy.getVariance().set(175) 

 

self.assertFloatsAlmostEqual(miCopy.getImage().getArray(), -50) 

self.assertTrue(np.all(miCopy.getMask().getArray() == 2)) 

self.assertFloatsAlmostEqual(miCopy.getVariance().getArray(), 175) 

 

self.assertFloatsAlmostEqual(mi.getImage().getArray(), 100) 

self.assertTrue(np.all(mi.getMask().getArray() == 5)) 

self.assertFloatsAlmostEqual(mi.getVariance().getArray(), 200) 

 

def testDeepCopySubData(self): 

"""Make sure a deep copy of a subregion of an Exposure has its own data (ticket #2625) 

""" 

exp = afwImage.ExposureF(6, 7) 

mi = exp.getMaskedImage() 

mi.getImage().set(100) 

mi.getMask().set(5) 

mi.getVariance().set(200) 

 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(1, 0), lsst.geom.Extent2I(5, 4)) 

expCopy = exp.Factory(exp, bbox, afwImage.PARENT, True) 

miCopy = expCopy.getMaskedImage() 

miCopy.getImage().set(-50) 

miCopy.getMask().set(2) 

miCopy.getVariance().set(175) 

 

self.assertFloatsAlmostEqual(miCopy.getImage().getArray(), -50) 

self.assertTrue(np.all(miCopy.getMask().getArray() == 2)) 

self.assertFloatsAlmostEqual(miCopy.getVariance().getArray(), 175) 

 

self.assertFloatsAlmostEqual(mi.getImage().getArray(), 100) 

self.assertTrue(np.all(mi.getMask().getArray() == 5)) 

self.assertFloatsAlmostEqual(mi.getVariance().getArray(), 200) 

 

def testDeepCopyMetadata(self): 

"""Make sure a deep copy of an Exposure has a deep copy of metadata (ticket #2568) 

""" 

exp = afwImage.ExposureF(10, 10) 

expMeta = exp.getMetadata() 

expMeta.set("foo", 5) 

expCopy = exp.clone() 

expCopyMeta = expCopy.getMetadata() 

expCopyMeta.set("foo", 6) 

self.assertEqual(expCopyMeta.getScalar("foo"), 6) 

# this will fail if the bug is present 

self.assertEqual(expMeta.getScalar("foo"), 5) 

 

def testDeepCopySubMetadata(self): 

"""Make sure a deep copy of a subregion of an Exposure has a deep copy of metadata (ticket #2568) 

""" 

exp = afwImage.ExposureF(10, 10) 

expMeta = exp.getMetadata() 

expMeta.set("foo", 5) 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(1, 0), lsst.geom.Extent2I(5, 5)) 

expCopy = exp.Factory(exp, bbox, afwImage.PARENT, True) 

expCopyMeta = expCopy.getMetadata() 

expCopyMeta.set("foo", 6) 

self.assertEqual(expCopyMeta.getScalar("foo"), 6) 

# this will fail if the bug is present 

self.assertEqual(expMeta.getScalar("foo"), 5) 

 

def testMakeExposureLeaks(self): 

"""Test for memory leaks in makeExposure (the test is in lsst.utils.tests.MemoryTestCase)""" 

afwImage.makeMaskedImage(afwImage.ImageU(lsst.geom.Extent2I(10, 20))) 

afwImage.makeExposure(afwImage.makeMaskedImage( 

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

 

def testImageSlices(self): 

"""Test image slicing, which generate sub-images using Box2I under the covers""" 

exp = afwImage.ExposureF(10, 20) 

mi = exp.getMaskedImage() 

mi.image[9, 19] = 10 

# N.b. Exposures don't support setting/getting the pixels so can't 

# replicate e.g. Image's slice tests 

sexp = exp[1:4, 6:10] 

self.assertEqual(sexp.getDimensions(), lsst.geom.ExtentI(3, 4)) 

sexp = exp[:, -3:, afwImage.LOCAL] 

self.assertEqual(sexp.getDimensions(), 

lsst.geom.ExtentI(exp.getWidth(), 3)) 

self.assertEqual(sexp.maskedImage[-1, -1, afwImage.LOCAL], 

exp.maskedImage[-1, -1, afwImage.LOCAL]) 

 

def testConversionToScalar(self): 

"""Test that even 1-pixel Exposures can't be converted to scalars""" 

im = afwImage.ExposureF(10, 20) 

 

# only single pixel images may be converted 

self.assertRaises(TypeError, float, im) 

# actually, can't convert (img, msk, var) to scalar 

self.assertRaises(TypeError, float, im[0, 0]) 

 

def testReadMetadata(self): 

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

self.exposureCrWcs.getMetadata().set("FRAZZLE", True) 

# This will write the main metadata (inc. FRAZZLE) to the primary HDU, and the 

# WCS to subsequent HDUs, along with INHERIT=T. 

self.exposureCrWcs.writeFits(tmpFile) 

# This should read the first non-empty HDU (i.e. it skips the primary), but 

# goes back and reads it if it finds INHERIT=T. That should let us read 

# frazzle and the Wcs from the PropertySet returned by 

# testReadMetadata. 

md = readMetadata(tmpFile) 

wcs = afwGeom.makeSkyWcs(md, False) 

self.assertPairsAlmostEqual(wcs.getPixelOrigin(), self.wcs.getPixelOrigin()) 

self.assertSpherePointsAlmostEqual(wcs.getSkyOrigin(), self.wcs.getSkyOrigin()) 

assert_allclose(wcs.getCdMatrix(), self.wcs.getCdMatrix(), atol=1e-10) 

frazzle = md.getScalar("FRAZZLE") 

self.assertTrue(frazzle) 

 

def testArchiveKeys(self): 

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

exposure1 = afwImage.ExposureF(100, 100, self.wcs) 

exposure1.setPsf(self.psf) 

exposure1.writeFits(tmpFile) 

exposure2 = afwImage.ExposureF(tmpFile) 

self.assertFalse(exposure2.getMetadata().exists("AR_ID")) 

self.assertFalse(exposure2.getMetadata().exists("PSF_ID")) 

self.assertFalse(exposure2.getMetadata().exists("WCS_ID")) 

 

def testTicket2861(self): 

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

exposure1 = afwImage.ExposureF(100, 100, self.wcs) 

exposure1.setPsf(self.psf) 

schema = afwTable.ExposureTable.makeMinimalSchema() 

coaddInputs = afwImage.CoaddInputs(schema, schema) 

exposure1.getInfo().setCoaddInputs(coaddInputs) 

exposure2 = afwImage.ExposureF(exposure1, True) 

self.assertIsNotNone(exposure2.getInfo().getCoaddInputs()) 

exposure2.writeFits(tmpFile) 

exposure3 = afwImage.ExposureF(tmpFile) 

self.assertIsNotNone(exposure3.getInfo().getCoaddInputs()) 

 

def testGetCutout(self): 

wcs = self.smallExposure.getWcs() 

 

dimensions = [lsst.geom.Extent2I(100, 50), lsst.geom.Extent2I(15, 15), lsst.geom.Extent2I(0, 10), 

lsst.geom.Extent2I(25, 30), lsst.geom.Extent2I(15, -5), 

2*self.smallExposure.getDimensions()] 

locations = [("center", self._getExposureCenter(self.smallExposure)), 

("edge", wcs.pixelToSky(lsst.geom.Point2D(0, 0))), 

("rounding test", wcs.pixelToSky(lsst.geom.Point2D(0.2, 0.7))), 

("just inside", wcs.pixelToSky(lsst.geom.Point2D(-0.5 + 1e-4, -0.5 + 1e-4))), 

("just outside", wcs.pixelToSky(lsst.geom.Point2D(-0.5 - 1e-4, -0.5 - 1e-4))), 

("outside", wcs.pixelToSky(lsst.geom.Point2D(-1000, -1000)))] 

for cutoutSize in dimensions: 

for label, cutoutCenter in locations: 

msg = 'Cutout size = %s, location = %s' % (cutoutSize, label) 

if "outside" not in label and all(cutoutSize.gt(0)): 

cutout = self.smallExposure.getCutout(cutoutCenter, cutoutSize) 

centerInPixels = wcs.skyToPixel(cutoutCenter) 

precision = (1 + 1e-4)*np.sqrt(0.5)*wcs.getPixelScale(centerInPixels) 

self._checkCutoutProperties(cutout, cutoutSize, cutoutCenter, precision, msg) 

self._checkCutoutPixels( 

cutout, 

self._getValidCorners(self.smallExposure.getBBox(), cutout.getBBox()), 

msg) 

 

# Need a valid WCS 

with self.assertRaises(pexExcept.LogicError, msg=msg): 

self.exposureMiOnly.getCutout(cutoutCenter, cutoutSize) 

else: 

with self.assertRaises(pexExcept.InvalidParameterError, msg=msg): 

self.smallExposure.getCutout(cutoutCenter, cutoutSize) 

 

def _checkCutoutProperties(self, cutout, size, center, precision, msg): 

"""Test whether a cutout has the desired size and position. 

 

Parameters 

---------- 

cutout : `lsst.afw.image.Exposure` 

The cutout to test. 

size : `lsst.geom.Extent2I` 

The expected dimensions of ``cutout``. 

center : `lsst.geom.SpherePoint` 

The expected center of ``cutout``. 

precision : `lsst.geom.Angle` 

The precision to which ``center`` must match. 

msg : `str` 

An error message suffix describing test parameters. 

""" 

newCenter = self._getExposureCenter(cutout) 

self.assertIsNotNone(cutout, msg=msg) 

self.assertSpherePointsAlmostEqual(newCenter, center, maxSep=precision, msg=msg) 

self.assertEqual(cutout.getWidth(), size[0], msg=msg) 

self.assertEqual(cutout.getHeight(), size[1], msg=msg) 

 

def _checkCutoutPixels(self, cutout, validCorners, msg): 

"""Test whether a cutout has valid/empty pixels where expected. 

 

Parameters 

---------- 

cutout : `lsst.afw.image.Exposure` 

The cutout to test. 

validCorners : iterable of `lsst.geom.Point2I` 

The corners of ``cutout`` that should be drawn from the original image. 

msg : `str` 

An error message suffix describing test parameters. 

""" 

mask = cutout.getMaskedImage().getMask() 

edgeMask = mask.getPlaneBitMask("NO_DATA") 

 

for corner in cutout.getBBox().getCorners(): 

maskBitsSet = mask[corner] & edgeMask 

if corner in validCorners: 

self.assertEqual(maskBitsSet, 0, msg=msg) 

else: 

self.assertEqual(maskBitsSet, edgeMask, msg=msg) 

 

def _getExposureCenter(self, exposure): 

"""Return the sky coordinates of an Exposure's center. 

 

Parameters 

---------- 

exposure : `lsst.afw.image.Exposure` 

The image whose center is desired. 

 

Returns 

------- 

center : `lsst.geom.SpherePoint` 

The position at the center of ``exposure``. 

""" 

return exposure.getWcs().pixelToSky(lsst.geom.Box2D(exposure.getBBox()).getCenter()) 

 

def _getValidCorners(self, imageBox, cutoutBox): 

"""Return the corners of a cutout that are constrained by the original image. 

 

Parameters 

---------- 

imageBox: `lsst.geom.Extent2I` 

The bounding box of the original image. 

cutoutBox : `lsst.geom.Box2I` 

The bounding box of the cutout. 

 

Returns 

------- 

corners : iterable of `lsst.geom.Point2I` 

The corners that are drawn from the original image. 

""" 

return [corner for corner in cutoutBox.getCorners() if corner in imageBox] 

 

 

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

def setUp(self): 

super().setUp() 

 

afwImage.Filter.reset() 

afwImage.FilterProperty.reset() 

defineFilter("g", 470.0) 

 

self.wcs = afwGeom.makeSkyWcs(lsst.geom.Point2D(0.0, 0.0), 

lsst.geom.SpherePoint(2.0, 34.0, lsst.geom.degrees), 

np.identity(2), 

) 

self.photoCalib = afwImage.PhotoCalib(1.5) 

self.psf = DummyPsf(2.0) 

self.detector = DetectorWrapper().detector 

self.polygon = afwGeom.Polygon(lsst.geom.Box2D(lsst.geom.Point2D(0.0, 0.0), 

lsst.geom.Point2D(25.0, 20.0))) 

self.coaddInputs = afwImage.CoaddInputs() 

self.apCorrMap = afwImage.ApCorrMap() 

self.transmissionCurve = afwImage.TransmissionCurve.makeIdentity() 

 

self.exposureInfo = afwImage.ExposureInfo() 

gFilter = afwImage.Filter("g") 

self.exposureInfo.setFilter(gFilter) 

 

def _checkAlias(self, exposureInfo, key, value, has, get): 

self.assertFalse(has()) 

self.assertFalse(exposureInfo.hasComponent(key)) 

self.assertIsNone(get()) 

self.assertIsNone(exposureInfo.getComponent(key)) 

 

self.exposureInfo.setComponent(key, value) 

self.assertTrue(has()) 

self.assertTrue(exposureInfo.hasComponent(key)) 

self.assertIsNotNone(get()) 

self.assertIsNotNone(exposureInfo.getComponent(key)) 

self.assertEqual(get(), value) 

self.assertEqual(exposureInfo.getComponent(key), value) 

 

self.exposureInfo.removeComponent(key) 

self.assertFalse(has()) 

self.assertFalse(exposureInfo.hasComponent(key)) 

self.assertIsNone(get()) 

self.assertIsNone(exposureInfo.getComponent(key)) 

 

def testAliases(self): 

cls = type(self.exposureInfo) 

self._checkAlias(self.exposureInfo, cls.KEY_WCS, self.wcs, 

self.exposureInfo.hasWcs, self.exposureInfo.getWcs) 

self._checkAlias(self.exposureInfo, cls.KEY_PSF, self.psf, 

self.exposureInfo.hasPsf, self.exposureInfo.getPsf) 

self._checkAlias(self.exposureInfo, cls.KEY_PHOTO_CALIB, self.photoCalib, 

self.exposureInfo.hasPhotoCalib, self.exposureInfo.getPhotoCalib) 

self._checkAlias(self.exposureInfo, cls.KEY_DETECTOR, self.detector, 

self.exposureInfo.hasDetector, self.exposureInfo.getDetector) 

self._checkAlias(self.exposureInfo, cls.KEY_VALID_POLYGON, self.polygon, 

self.exposureInfo.hasValidPolygon, self.exposureInfo.getValidPolygon) 

self._checkAlias(self.exposureInfo, cls.KEY_COADD_INPUTS, self.coaddInputs, 

self.exposureInfo.hasCoaddInputs, self.exposureInfo.getCoaddInputs) 

self._checkAlias(self.exposureInfo, cls.KEY_AP_CORR_MAP, self.apCorrMap, 

self.exposureInfo.hasApCorrMap, self.exposureInfo.getApCorrMap) 

self._checkAlias(self.exposureInfo, cls.KEY_TRANSMISSION_CURVE, self.transmissionCurve, 

self.exposureInfo.hasTransmissionCurve, self.exposureInfo.getTransmissionCurve) 

 

def testCopy(self): 

# Test that ExposureInfos have independently settable state 

copy = afwImage.ExposureInfo(self.exposureInfo, True) 

self.assertEqual(self.exposureInfo.getWcs(), copy.getWcs()) 

 

newWcs = afwGeom.makeSkyWcs(lsst.geom.Point2D(-23.0, 8.0), 

lsst.geom.SpherePoint(0.0, 0.0, lsst.geom.degrees), 

np.identity(2), 

) 

copy.setWcs(newWcs) 

self.assertEqual(copy.getWcs(), newWcs) 

self.assertNotEqual(self.exposureInfo.getWcs(), copy.getWcs()) 

 

 

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

"""Tests of Exposure that don't require afwdata. 

 

These tests use the trivial exposures written to ``afw/tests/data``. 

""" 

def setUp(self): 

self.dataDir = os.path.join(os.path.split(__file__)[0], "data") 

 

# Check the values below against what was written by comparing with 

# the code in `afw/tests/data/makeTestExposure.py` 

nx = ny = 10 

image = afwImage.ImageF(np.arange(nx*ny, dtype='f').reshape(nx, ny)) 

variance = afwImage.ImageF(np.ones((nx, ny), dtype='f')) 

mask = afwImage.MaskX(nx, ny) 

mask.array[5, 5] = 5 

self.maskedImage = afwImage.MaskedImageF(image, mask, variance) 

 

self.v0PhotoCalib = afwImage.makePhotoCalibFromCalibZeroPoint(1e6, 2e4) 

self.v1PhotoCalib = afwImage.PhotoCalib(1e6, 2e4) 

 

def testReadUnversioned(self): 

"""Test that we can read an unversioned (implicit verison 0) file. 

""" 

filename = os.path.join(self.dataDir, "exposure-noversion.fits") 

exposure = afwImage.ExposureF.readFits(filename) 

 

self.assertMaskedImagesEqual(exposure.maskedImage, self.maskedImage) 

 

self.assertEqual(exposure.getPhotoCalib(), self.v0PhotoCalib) 

 

def testReadVersion0(self): 

"""Test that we can read an version 0 file. 

This file should be identical to the unversioned one, except that it 

is marked as ExposureInfo version 0 in the header. 

""" 

filename = os.path.join(self.dataDir, "exposure-version-0.fits") 

exposure = afwImage.ExposureF.readFits(filename) 

 

self.assertMaskedImagesEqual(exposure.maskedImage, self.maskedImage) 

 

self.assertEqual(exposure.getPhotoCalib(), self.v0PhotoCalib) 

 

# Check that the metadata reader parses the file correctly 

reader = afwImage.ExposureFitsReader(filename) 

self.assertEqual(reader.readExposureInfo().getPhotoCalib(), self.v0PhotoCalib) 

self.assertEqual(reader.readPhotoCalib(), self.v0PhotoCalib) 

 

def testReadVersion1(self): 

"""Test that we can read an version 1 file. 

Version 1 replaced Calib with PhotoCalib. 

""" 

filename = os.path.join(self.dataDir, "exposure-version-1.fits") 

exposure = afwImage.ExposureF.readFits(filename) 

 

self.assertMaskedImagesEqual(exposure.maskedImage, self.maskedImage) 

 

self.assertEqual(exposure.getPhotoCalib(), self.v1PhotoCalib) 

 

# Check that the metadata reader parses the file correctly 

reader = afwImage.ExposureFitsReader(filename) 

self.assertEqual(reader.readExposureInfo().getPhotoCalib(), self.v1PhotoCalib) 

self.assertEqual(reader.readPhotoCalib(), self.v1PhotoCalib) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()