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

# This file is part of meas_base. 

# 

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

 

"""Definition of measurement plugins. 

 

This module defines and registers a series of pure-Python measurement plugins 

which have trivial implementations. It also wraps measurement algorithms 

defined in C++ to expose them to the measurement framework. 

""" 

 

import numpy as np 

 

import lsst.pex.exceptions 

import lsst.geom 

import lsst.afw.detection 

import lsst.afw.geom 

 

from .pluginRegistry import register 

from .pluginsBase import BasePlugin 

from .baseMeasurement import BaseMeasurementPluginConfig 

from .sfm import SingleFramePluginConfig, SingleFramePlugin 

from .forcedMeasurement import ForcedPluginConfig, ForcedPlugin 

from .wrappers import wrapSimpleAlgorithm, wrapTransform, GenericPlugin 

from .transforms import SimpleCentroidTransform 

 

from .apertureFlux import ApertureFluxControl, ApertureFluxTransform 

from .transform import BaseTransform 

from .blendedness import BlendednessAlgorithm, BlendednessControl 

from .circularApertureFlux import CircularApertureFluxAlgorithm 

from .gaussianFlux import GaussianFluxAlgorithm, GaussianFluxControl, GaussianFluxTransform 

from .exceptions import MeasurementError 

from .localBackground import LocalBackgroundControl, LocalBackgroundAlgorithm, LocalBackgroundTransform 

from .naiveCentroid import NaiveCentroidAlgorithm, NaiveCentroidControl, NaiveCentroidTransform 

from .peakLikelihoodFlux import PeakLikelihoodFluxAlgorithm, PeakLikelihoodFluxControl, \ 

PeakLikelihoodFluxTransform 

from .pixelFlags import PixelFlagsAlgorithm, PixelFlagsControl 

from .psfFlux import PsfFluxAlgorithm, PsfFluxControl, PsfFluxTransform 

from .scaledApertureFlux import ScaledApertureFluxAlgorithm, ScaledApertureFluxControl, \ 

ScaledApertureFluxTransform 

from .sdssCentroid import SdssCentroidAlgorithm, SdssCentroidControl, SdssCentroidTransform 

from .sdssShape import SdssShapeAlgorithm, SdssShapeControl, SdssShapeTransform 

 

__all__ = ( 

"SingleFrameFPPositionConfig", "SingleFrameFPPositionPlugin", 

"SingleFrameJacobianConfig", "SingleFrameJacobianPlugin", 

"VarianceConfig", "SingleFrameVariancePlugin", "ForcedVariancePlugin", 

"InputCountConfig", "SingleFrameInputCountPlugin", "ForcedInputCountPlugin", 

"SingleFramePeakCentroidConfig", "SingleFramePeakCentroidPlugin", 

"SingleFrameSkyCoordConfig", "SingleFrameSkyCoordPlugin", 

"ForcedPeakCentroidConfig", "ForcedPeakCentroidPlugin", 

"ForcedTransformedCentroidConfig", "ForcedTransformedCentroidPlugin", 

"ForcedTransformedShapeConfig", "ForcedTransformedShapePlugin", 

"EvaluateLocalPhotoCalibPlugin", "EvaluateLocalPhotoCalibPluginConfig", 

"EvaluateLocalWcsPlugin", "EvaluateLocalWcsPluginConfig", 

) 

 

 

wrapSimpleAlgorithm(PsfFluxAlgorithm, Control=PsfFluxControl, 

TransformClass=PsfFluxTransform, executionOrder=BasePlugin.FLUX_ORDER, 

shouldApCorr=True, hasLogName=True) 

wrapSimpleAlgorithm(PeakLikelihoodFluxAlgorithm, Control=PeakLikelihoodFluxControl, 

TransformClass=PeakLikelihoodFluxTransform, executionOrder=BasePlugin.FLUX_ORDER) 

wrapSimpleAlgorithm(GaussianFluxAlgorithm, Control=GaussianFluxControl, 

TransformClass=GaussianFluxTransform, executionOrder=BasePlugin.FLUX_ORDER, 

shouldApCorr=True) 

wrapSimpleAlgorithm(NaiveCentroidAlgorithm, Control=NaiveCentroidControl, 

TransformClass=NaiveCentroidTransform, executionOrder=BasePlugin.CENTROID_ORDER) 

wrapSimpleAlgorithm(SdssCentroidAlgorithm, Control=SdssCentroidControl, 

TransformClass=SdssCentroidTransform, executionOrder=BasePlugin.CENTROID_ORDER) 

wrapSimpleAlgorithm(PixelFlagsAlgorithm, Control=PixelFlagsControl, 

executionOrder=BasePlugin.FLUX_ORDER) 

wrapSimpleAlgorithm(SdssShapeAlgorithm, Control=SdssShapeControl, 

TransformClass=SdssShapeTransform, executionOrder=BasePlugin.SHAPE_ORDER) 

wrapSimpleAlgorithm(ScaledApertureFluxAlgorithm, Control=ScaledApertureFluxControl, 

TransformClass=ScaledApertureFluxTransform, executionOrder=BasePlugin.FLUX_ORDER) 

 

wrapSimpleAlgorithm(CircularApertureFluxAlgorithm, needsMetadata=True, Control=ApertureFluxControl, 

TransformClass=ApertureFluxTransform, executionOrder=BasePlugin.FLUX_ORDER) 

wrapSimpleAlgorithm(BlendednessAlgorithm, Control=BlendednessControl, 

TransformClass=BaseTransform, executionOrder=BasePlugin.SHAPE_ORDER) 

 

wrapSimpleAlgorithm(LocalBackgroundAlgorithm, Control=LocalBackgroundControl, 

TransformClass=LocalBackgroundTransform, executionOrder=BasePlugin.FLUX_ORDER) 

 

wrapTransform(PsfFluxTransform) 

wrapTransform(PeakLikelihoodFluxTransform) 

wrapTransform(GaussianFluxTransform) 

wrapTransform(NaiveCentroidTransform) 

wrapTransform(SdssCentroidTransform) 

wrapTransform(SdssShapeTransform) 

wrapTransform(ScaledApertureFluxTransform) 

wrapTransform(ApertureFluxTransform) 

wrapTransform(LocalBackgroundTransform) 

 

 

class SingleFrameFPPositionConfig(SingleFramePluginConfig): 

"""Configuration for the focal plane position measurment algorithm. 

""" 

 

pass 

 

 

@register("base_FPPosition") 

class SingleFrameFPPositionPlugin(SingleFramePlugin): 

"""Algorithm to calculate the position of a centroid on the focal plane. 

 

Parameters 

---------- 

config : `SingleFrameFPPositionConfig` 

Plugin configuraion. 

name : `str` 

Plugin name. 

schema : `lsst.afw.table.Schema` 

The schema for the measurement output catalog. New fields will be 

added to hold measurements produced by this plugin. 

metadata : `lsst.daf.base.PropertySet` 

Plugin metadata that will be attached to the output catalog 

""" 

 

ConfigClass = SingleFrameFPPositionConfig 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.SHAPE_ORDER 

 

def __init__(self, config, name, schema, metadata): 

SingleFramePlugin.__init__(self, config, name, schema, metadata) 

self.focalValue = lsst.afw.table.Point2DKey.addFields(schema, name, "Position on the focal plane", 

"mm") 

self.focalFlag = schema.addField(name + "_flag", type="Flag", doc="Set to True for any fatal failure") 

self.detectorFlag = schema.addField(name + "_missingDetector_flag", type="Flag", 

doc="Set to True if detector object is missing") 

 

def measure(self, measRecord, exposure): 

det = exposure.getDetector() 

if not det: 

measRecord.set(self.detectorFlag, True) 

fp = lsst.geom.Point2D(np.nan, np.nan) 

else: 

center = measRecord.getCentroid() 

fp = det.transform(center, lsst.afw.cameraGeom.PIXELS, lsst.afw.cameraGeom.FOCAL_PLANE) 

measRecord.set(self.focalValue, fp) 

 

def fail(self, measRecord, error=None): 

measRecord.set(self.focalFlag, True) 

 

 

class SingleFrameJacobianConfig(SingleFramePluginConfig): 

"""Configuration for the Jacobian calculation plugin. 

""" 

 

pixelScale = lsst.pex.config.Field(dtype=float, default=0.5, doc="Nominal pixel size (arcsec)") 

 

 

@register("base_Jacobian") 

class SingleFrameJacobianPlugin(SingleFramePlugin): 

"""Compute the Jacobian and its ratio with a nominal pixel area. 

 

This enables one to compare relative, rather than absolute, pixel areas. 

 

Parameters 

---------- 

config : `SingleFrameJacobianConfig` 

Plugin configuraion. 

name : `str` 

Plugin name. 

schema : `lsst.afw.table.Schema` 

The schema for the measurement output catalog. New fields will be 

added to hold measurements produced by this plugin. 

metadata : `lsst.daf.base.PropertySet` 

Plugin metadata that will be attached to the output catalog 

""" 

 

ConfigClass = SingleFrameJacobianConfig 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.SHAPE_ORDER 

 

def __init__(self, config, name, schema, metadata): 

SingleFramePlugin.__init__(self, config, name, schema, metadata) 

self.jacValue = schema.addField(name + '_value', type="D", doc="Jacobian correction") 

self.jacFlag = schema.addField(name + '_flag', type="Flag", doc="Set to 1 for any fatal failure") 

# Calculate one over the area of a nominal reference pixel, where area is in arcsec^2 

self.scale = pow(self.config.pixelScale, -2) 

 

def measure(self, measRecord, exposure): 

center = measRecord.getCentroid() 

# Compute the area of a pixel at a source record's centroid, and take 

# the ratio of that with the defined reference pixel area. 

result = np.abs(self.scale*exposure.getWcs().linearizePixelToSky( 

center, 

lsst.geom.arcseconds).getLinear().computeDeterminant()) 

measRecord.set(self.jacValue, result) 

 

def fail(self, measRecord, error=None): 

measRecord.set(self.jacFlag, True) 

 

 

class VarianceConfig(BaseMeasurementPluginConfig): 

"""Configuration for the variance calculation plugin. 

""" 

scale = lsst.pex.config.Field(dtype=float, default=5.0, optional=True, 

doc="Scale factor to apply to shape for aperture") 

mask = lsst.pex.config.ListField(doc="Mask planes to ignore", dtype=str, 

default=["DETECTED", "DETECTED_NEGATIVE", "BAD", "SAT"]) 

 

 

class VariancePlugin(GenericPlugin): 

"""Compute the median variance corresponding to a footprint. 

 

The aim here is to measure the background variance, rather than that of 

the object itself. In order to achieve this, the variance is calculated 

over an area scaled up from the shape of the input footprint. 

 

Parameters 

---------- 

config : `VarianceConfig` 

Plugin configuraion. 

name : `str` 

Plugin name. 

schema : `lsst.afw.table.Schema` 

The schema for the measurement output catalog. New fields will be 

added to hold measurements produced by this plugin. 

metadata : `lsst.daf.base.PropertySet` 

Plugin metadata that will be attached to the output catalog 

""" 

 

ConfigClass = VarianceConfig 

 

FAILURE_BAD_CENTROID = 1 

"""Denotes failures due to bad centroiding (`int`). 

""" 

 

FAILURE_EMPTY_FOOTPRINT = 2 

"""Denotes failures due to a lack of usable pixels (`int`). 

""" 

 

@classmethod 

def getExecutionOrder(cls): 

return BasePlugin.FLUX_ORDER 

 

def __init__(self, config, name, schema, metadata): 

GenericPlugin.__init__(self, config, name, schema, metadata) 

self.varValue = schema.addField(name + '_value', type="D", doc="Variance at object position") 

self.emptyFootprintFlag = schema.addField(name + '_flag_emptyFootprint', type="Flag", 

doc="Set to True when the footprint has no usable pixels") 

 

# Alias the badCentroid flag to that which is defined for the target 

# of the centroid slot. We do not simply rely on the alias because 

# that could be changed post-measurement. 

schema.getAliasMap().set(name + '_flag_badCentroid', schema.getAliasMap().apply("slot_Centroid_flag")) 

 

def measure(self, measRecord, exposure, center): 

# Create an aperture and grow it by scale value defined in config to 

# ensure there are enough pixels around the object to get decent 

# statistics 

if not np.all(np.isfinite(measRecord.getCentroid())): 

raise MeasurementError("Bad centroid and/or shape", self.FAILURE_BAD_CENTROID) 

aperture = lsst.afw.geom.Ellipse(measRecord.getShape(), measRecord.getCentroid()) 

aperture.scale(self.config.scale) 

ellipse = lsst.afw.geom.SpanSet.fromShape(aperture) 

foot = lsst.afw.detection.Footprint(ellipse) 

foot.clipTo(exposure.getBBox(lsst.afw.image.PARENT)) 

# Filter out any pixels which have mask bits set corresponding to the 

# planes to be excluded (defined in config.mask) 

maskedImage = exposure.getMaskedImage() 

pixels = lsst.afw.detection.makeHeavyFootprint(foot, maskedImage) 

maskBits = maskedImage.getMask().getPlaneBitMask(self.config.mask) 

logicalMask = np.logical_not(pixels.getMaskArray() & maskBits) 

# Compute the median variance value for each pixel not excluded by the 

# mask and write the record. Numpy median is used here instead of 

# afw.math makeStatistics because of an issue with data types being 

# passed into the C++ layer (DM-2379). 

if np.any(logicalMask): 

medVar = np.median(pixels.getVarianceArray()[logicalMask]) 

measRecord.set(self.varValue, medVar) 

else: 

raise MeasurementError("Footprint empty, or all pixels are masked, can't compute median", 

self.FAILURE_EMPTY_FOOTPRINT) 

 

def fail(self, measRecord, error=None): 

# Check that we have an error object and that it is of type 

# MeasurementError 

if isinstance(error, MeasurementError): 

assert error.getFlagBit() in (self.FAILURE_BAD_CENTROID, self.FAILURE_EMPTY_FOOTPRINT) 

# FAILURE_BAD_CENTROID handled by alias to centroid record. 

if error.getFlagBit() == self.FAILURE_EMPTY_FOOTPRINT: 

measRecord.set(self.emptyFootprintFlag, True) 

measRecord.set(self.varValue, np.nan) 

GenericPlugin.fail(self, measRecord, error) 

 

 

SingleFrameVariancePlugin = VariancePlugin.makeSingleFramePlugin("base_Variance") 

"""Single-frame version of `VariancePlugin`. 

""" 

 

ForcedVariancePlugin = VariancePlugin.makeForcedPlugin("base_Variance") 

"""Forced version of `VariancePlugin`. 

""" 

 

 

class InputCountConfig(BaseMeasurementPluginConfig): 

"""Configuration for the input image counting plugin. 

""" 

pass 

 

 

class InputCountPlugin(GenericPlugin): 

"""Count the number of input images which contributed to a a source. 

 

Parameters 

---------- 

config : `InputCountConfig` 

Plugin configuraion. 

name : `str` 

Plugin name. 

schema : `lsst.afw.table.Schema` 

The schema for the measurement output catalog. New fields will be 

added to hold measurements produced by this plugin. 

metadata : `lsst.daf.base.PropertySet` 

Plugin metadata that will be attached to the output catalog 

 

Notes 

----- 

Information is derived from the image's `~lsst.afw.image.CoaddInputs`. 

Note these limitation: 

 

- This records the number of images which contributed to the pixel in the 

center of the source footprint, rather than to any or all pixels in the 

source. 

- Clipping in the coadd is not taken into account. 

""" 

 

ConfigClass = InputCountConfig 

 

FAILURE_BAD_CENTROID = 1 

"""Denotes failures due to bad centroiding (`int`). 

""" 

 

FAILURE_NO_INPUTS = 2 

"""Denotes failures due to the image not having coadd inputs. (`int`) 

""" 

 

@classmethod 

def getExecutionOrder(cls): 

return BasePlugin.SHAPE_ORDER 

 

def __init__(self, config, name, schema, metadata): 

GenericPlugin.__init__(self, config, name, schema, metadata) 

self.numberKey = schema.addField(name + '_value', type="I", 

doc="Number of images contributing at center, not including any" 

"clipping") 

self.noInputsFlag = schema.addField(name + '_flag_noInputs', type="Flag", 

doc="No coadd inputs available") 

# Alias the badCentroid flag to that which is defined for the target of the centroid slot. 

# We do not simply rely on the alias because that could be changed post-measurement. 

schema.getAliasMap().set(name + '_flag_badCentroid', schema.getAliasMap().apply("slot_Centroid_flag")) 

 

def measure(self, measRecord, exposure, center): 

if not exposure.getInfo().getCoaddInputs(): 

raise MeasurementError("No coadd inputs defined.", self.FAILURE_NO_INPUTS) 

if not np.all(np.isfinite(center)): 

raise MeasurementError("Source has a bad centroid.", self.FAILURE_BAD_CENTROID) 

 

ccds = exposure.getInfo().getCoaddInputs().ccds 

measRecord.set(self.numberKey, len(ccds.subsetContaining(center, exposure.getWcs()))) 

 

def fail(self, measRecord, error=None): 

if error is not None: 

assert error.getFlagBit() in (self.FAILURE_BAD_CENTROID, self.FAILURE_NO_INPUTS) 

# FAILURE_BAD_CENTROID handled by alias to centroid record. 

if error.getFlagBit() == self.FAILURE_NO_INPUTS: 

measRecord.set(self.noInputsFlag, True) 

GenericPlugin.fail(self, measRecord, error) 

 

 

SingleFrameInputCountPlugin = InputCountPlugin.makeSingleFramePlugin("base_InputCount") 

"""Single-frame version of `InputCoutPlugin`. 

""" 

 

ForcedInputCountPlugin = InputCountPlugin.makeForcedPlugin("base_InputCount") 

"""Forced version of `InputCoutPlugin`. 

""" 

 

 

class EvaluateLocalPhotoCalibPluginConfig(BaseMeasurementPluginConfig): 

"""Configuration for the variance calculation plugin. 

""" 

pass 

 

 

class EvaluateLocalPhotoCalibPlugin(GenericPlugin): 

"""Evaluate the local value of the Photometric Calibration in the exposure. 

 

The aim is to store the local calib value within the catalog for later 

use in the Science Data Model functors. 

""" 

ConfigClass = EvaluateLocalPhotoCalibPluginConfig 

 

@classmethod 

def getExecutionOrder(cls): 

return BasePlugin.FLUX_ORDER 

 

def __init__(self, config, name, schema, metadata): 

GenericPlugin.__init__(self, config, name, schema, metadata) 

self.photoKey = schema.addField( 

name, 

type="D", 

doc="Local approximation of the PhotoCalib calibration factor at " 

"the location of the src.") 

self.photoErrKey = schema.addField( 

"%sErr" % name, 

type="D", 

doc="Error on the local approximation of the PhotoCalib " 

"calibration factor at the location of the src.") 

 

def measure(self, measRecord, exposure, center): 

 

photoCalib = exposure.getPhotoCalib() 

calib = photoCalib.getLocalCalibration(center) 

measRecord.set(self.photoKey, calib) 

 

calibErr = photoCalib.getCalibrationErr() 

measRecord.set(self.photoErrKey, calibErr) 

 

 

SingleFrameEvaluateLocalPhotoCalibPlugin = EvaluateLocalPhotoCalibPlugin.makeSingleFramePlugin( 

"base_LocalPhotoCalib") 

"""Single-frame version of `EvaluatePhotoCalibPlugin`. 

""" 

 

ForcedEvaluateLocalPhotoCalibPlugin = EvaluateLocalPhotoCalibPlugin.makeForcedPlugin( 

"base_LocalPhotoCalib") 

"""Forced version of `EvaluatePhotoCalibPlugin`. 

""" 

 

 

class EvaluateLocalWcsPluginConfig(BaseMeasurementPluginConfig): 

"""Configuration for the variance calculation plugin. 

""" 

pass 

 

 

class EvaluateLocalWcsPlugin(GenericPlugin): 

"""Evaluate the local, linear approximation of the Wcs. 

 

The aim is to store the local calib value within the catalog for later 

use in the Science Data Model functors. 

""" 

ConfigClass = EvaluateLocalWcsPluginConfig 

_scale = (1.0 * lsst.geom.arcseconds).asDegrees() 

 

@classmethod 

def getExecutionOrder(cls): 

return BasePlugin.FLUX_ORDER 

 

def __init__(self, config, name, schema, metadata): 

GenericPlugin.__init__(self, config, name, schema, metadata) 

self.cdMatrix11Key = schema.addField( 

f"{name}_CDMatrix_1_1", 

type="D", 

doc=f"(1, 1) element of the CDMatrix for the linear approximation " 

"of the WCS at the src location. Gives units in radians.") 

self.cdMatrix12Key = schema.addField( 

f"{name}_CDMatrix_1_2", 

type="D", 

doc=f"(1, 2) element of the CDMatrix for the linear approximation " 

"of the WCS at the src location. Gives units in radians.") 

self.cdMatrix21Key = schema.addField( 

f"{name}_CDMatrix_2_1", 

type="D", 

doc=f"(2, 1) element of the CDMatrix for the linear approximation " 

"of the WCS at the src location. Gives units in radians.") 

self.cdMatrix22Key = schema.addField( 

f"{name}_CDMatrix_2_2", 

type="D", 

doc=f"(2, 2) element of the CDMatrix for the linear approximation " 

"of the WCS at the src location. Gives units in radians.") 

 

def measure(self, measRecord, exposure, center): 

wcs = exposure.getWcs() 

localMatrix = self.makeLocalTransformMatrix(wcs, center) 

measRecord.set(self.cdMatrix11Key, localMatrix[0, 0]) 

measRecord.set(self.cdMatrix12Key, localMatrix[0, 1]) 

measRecord.set(self.cdMatrix21Key, localMatrix[1, 0]) 

measRecord.set(self.cdMatrix22Key, localMatrix[1, 1]) 

 

def makeLocalTransformMatrix(self, wcs, center): 

"""Create a local, linear approximation of the wcs transformation 

matrix. 

 

The approximation is created as if the center is at RA=0, DEC=0. All 

comparing x,y coordinate are relative to the position of center. Matrix 

is initially calculated with units arcseconds and then converted to 

radians. This yields higher precision results due to quirks in AST. 

 

Parameters 

---------- 

wcs : `lsst.afw.geom.SkyWcs` 

Wcs to approximate 

center : `lsst.geom.Point2D` 

Point at which to evaluate the LocalWcs. 

 

Returns 

------- 

localMatrix : `numpy.ndarray` 

Matrix representation the local wcs approximation with units 

radians. 

""" 

skyCenter = wcs.pixelToSky(center) 

localGnomonicWcs = lsst.afw.geom.makeSkyWcs( 

center, skyCenter, np.diag((self._scale, self._scale))) 

measurementToLocalGnomonic = wcs.getTransform().then( 

localGnomonicWcs.getTransform().inverted() 

) 

localMatrix = measurementToLocalGnomonic.getJacobian(center) 

return np.radians(localMatrix / 3600) 

 

 

SingleFrameEvaluateLocalWcsPlugin = EvaluateLocalWcsPlugin.makeSingleFramePlugin("base_LocalWcs") 

"""Single-frame version of `EvaluateLocalWcsPlugin`. 

""" 

 

ForcedEvaluateLocalWcsPlugin = EvaluateLocalWcsPlugin.makeForcedPlugin("base_LocalWcs") 

"""Forced version of `EvaluateLocalWcsPlugin`. 

""" 

 

 

class SingleFramePeakCentroidConfig(SingleFramePluginConfig): 

"""Configuration for the single frame peak centroiding algorithm. 

""" 

pass 

 

 

@register("base_PeakCentroid") 

class SingleFramePeakCentroidPlugin(SingleFramePlugin): 

"""Record the highest peak in a source footprint as its centroid. 

 

This is of course a relatively poor measure of the true centroid of the 

object; this algorithm is provided mostly for testing and debugging. 

 

Parameters 

---------- 

config : `SingleFramePeakCentroidConfig` 

Plugin configuraion. 

name : `str` 

Plugin name. 

schema : `lsst.afw.table.Schema` 

The schema for the measurement output catalog. New fields will be 

added to hold measurements produced by this plugin. 

metadata : `lsst.daf.base.PropertySet` 

Plugin metadata that will be attached to the output catalog 

""" 

 

ConfigClass = SingleFramePeakCentroidConfig 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.CENTROID_ORDER 

 

def __init__(self, config, name, schema, metadata): 

SingleFramePlugin.__init__(self, config, name, schema, metadata) 

self.keyX = schema.addField(name + "_x", type="D", doc="peak centroid", units="pixel") 

self.keyY = schema.addField(name + "_y", type="D", doc="peak centroid", units="pixel") 

self.flag = schema.addField(name + "_flag", type="Flag", doc="Centroiding failed") 

 

def measure(self, measRecord, exposure): 

peak = measRecord.getFootprint().getPeaks()[0] 

measRecord.set(self.keyX, peak.getFx()) 

measRecord.set(self.keyY, peak.getFy()) 

 

def fail(self, measRecord, error=None): 

measRecord.set(self.flag, True) 

 

@staticmethod 

def getTransformClass(): 

return SimpleCentroidTransform 

 

 

class SingleFrameSkyCoordConfig(SingleFramePluginConfig): 

"""Configuration for the sky coordinates algorithm. 

""" 

pass 

 

 

@register("base_SkyCoord") 

class SingleFrameSkyCoordPlugin(SingleFramePlugin): 

"""Record the sky position of an object based on its centroid slot and WCS. 

 

The position is record in the ``coord`` field, which is part of the 

`~lsst.afw.table.SourceCatalog` minimal schema. 

 

Parameters 

---------- 

config : `SingleFrameSkyCoordConfig` 

Plugin configuraion. 

name : `str` 

Plugin name. 

schema : `lsst.afw.table.Schema` 

The schema for the measurement output catalog. New fields will be 

added to hold measurements produced by this plugin. 

metadata : `lsst.daf.base.PropertySet` 

Plugin metadata that will be attached to the output catalog 

""" 

 

ConfigClass = SingleFrameSkyCoordConfig 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.SHAPE_ORDER 

 

def measure(self, measRecord, exposure): 

# There should be a base class method for handling this exception. Put 

# this on a later ticket. Also, there should be a python Exception of 

# the appropriate type for this error 

if not exposure.hasWcs(): 

raise Exception("Wcs not attached to exposure. Required for " + self.name + " algorithm") 

measRecord.updateCoord(exposure.getWcs()) 

 

def fail(self, measRecord, error=None): 

# Override fail() to do nothing in the case of an exception: this is 

# not ideal, but we don't have a place to put failures because we 

# don't allocate any fields. Should consider fixing as part of 

# DM-1011 

pass 

 

 

class ForcedPeakCentroidConfig(ForcedPluginConfig): 

"""Configuration for the forced peak centroid algorithm. 

""" 

pass 

 

 

@register("base_PeakCentroid") 

class ForcedPeakCentroidPlugin(ForcedPlugin): 

"""Record the highest peak in a source footprint as its centroid. 

 

This is of course a relatively poor measure of the true centroid of the 

object; this algorithm is provided mostly for testing and debugging. 

 

This is similar to `SingleFramePeakCentroidPlugin`, except that transforms 

the peak coordinate from the original (reference) coordinate system to the 

coordinate system of the exposure being measured. 

 

Parameters 

---------- 

config : `ForcedPeakCentroidConfig` 

Plugin configuraion. 

name : `str` 

Plugin name. 

schemaMapper : `lsst.afw.table.SchemaMapper` 

A mapping from reference catalog fields to output 

catalog fields. Output fields are added to the output schema. 

metadata : `lsst.daf.base.PropertySet` 

Plugin metadata that will be attached to the output catalog. 

""" 

 

ConfigClass = ForcedPeakCentroidConfig 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.CENTROID_ORDER 

 

def __init__(self, config, name, schemaMapper, metadata): 

ForcedPlugin.__init__(self, config, name, schemaMapper, metadata) 

schema = schemaMapper.editOutputSchema() 

self.keyX = schema.addField(name + "_x", type="D", doc="peak centroid", units="pixel") 

self.keyY = schema.addField(name + "_y", type="D", doc="peak centroid", units="pixel") 

 

def measure(self, measRecord, exposure, refRecord, refWcs): 

targetWcs = exposure.getWcs() 

peak = refRecord.getFootprint().getPeaks()[0] 

result = lsst.geom.Point2D(peak.getFx(), peak.getFy()) 

result = targetWcs.skyToPixel(refWcs.pixelToSky(result)) 

measRecord.set(self.keyX, result.getX()) 

measRecord.set(self.keyY, result.getY()) 

 

@staticmethod 

def getTransformClass(): 

return SimpleCentroidTransform 

 

 

class ForcedTransformedCentroidConfig(ForcedPluginConfig): 

"""Configuration for the forced transformed centroid algorithm. 

""" 

pass 

 

 

@register("base_TransformedCentroid") 

class ForcedTransformedCentroidPlugin(ForcedPlugin): 

"""Record the transformation of the reference catalog centroid. 

 

The centroid recorded in the reference catalog is tranformed to the 

measurement coordinate system and stored. 

 

Parameters 

---------- 

config : `ForcedTransformedCentroidConfig` 

Plugin configuration 

name : `str` 

Plugin name 

schemaMapper : `lsst.afw.table.SchemaMapper` 

A mapping from reference catalog fields to output 

catalog fields. Output fields are added to the output schema. 

metadata : `lsst.daf.base.PropertySet` 

Plugin metadata that will be attached to the output catalog. 

 

Notes 

----- 

This is used as the slot centroid by default in forced measurement, 

allowing subsequent measurements to simply refer to the slot value just as 

they would in single-frame measurement. 

""" 

 

ConfigClass = ForcedTransformedCentroidConfig 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.CENTROID_ORDER 

 

def __init__(self, config, name, schemaMapper, metadata): 

ForcedPlugin.__init__(self, config, name, schemaMapper, metadata) 

schema = schemaMapper.editOutputSchema() 

# Allocate x and y fields, join these into a single FunctorKey for ease-of-use. 

xKey = schema.addField(name + "_x", type="D", doc="transformed reference centroid column", 

units="pixel") 

yKey = schema.addField(name + "_y", type="D", doc="transformed reference centroid row", 

units="pixel") 

self.centroidKey = lsst.afw.table.Point2DKey(xKey, yKey) 

# Because we're taking the reference position as given, we don't bother transforming its 

# uncertainty and reporting that here, so there are no sigma or cov fields. We do propagate 

# the flag field, if it exists. 

if "slot_Centroid_flag" in schemaMapper.getInputSchema(): 

self.flagKey = schema.addField(name + "_flag", type="Flag", 

doc="whether the reference centroid is marked as bad") 

else: 

self.flagKey = None 

 

def measure(self, measRecord, exposure, refRecord, refWcs): 

targetWcs = exposure.getWcs() 

if not refWcs == targetWcs: 

targetPos = targetWcs.skyToPixel(refWcs.pixelToSky(refRecord.getCentroid())) 

measRecord.set(self.centroidKey, targetPos) 

else: 

measRecord.set(self.centroidKey, refRecord.getCentroid()) 

if self.flagKey is not None: 

measRecord.set(self.flagKey, refRecord.getCentroidFlag()) 

 

 

class ForcedTransformedShapeConfig(ForcedPluginConfig): 

"""Configuration for the forced transformed shape algorithm. 

""" 

pass 

 

 

@register("base_TransformedShape") 

class ForcedTransformedShapePlugin(ForcedPlugin): 

"""Record the transformation of the reference catalog shape. 

 

The shape recorded in the reference catalog is tranformed to the 

measurement coordinate system and stored. 

 

Parameters 

---------- 

config : `ForcedTransformedShapeConfig` 

Plugin configuration 

name : `str` 

Plugin name 

schemaMapper : `lsst.afw.table.SchemaMapper` 

A mapping from reference catalog fields to output 

catalog fields. Output fields are added to the output schema. 

metadata : `lsst.daf.base.PropertySet` 

Plugin metadata that will be attached to the output catalog. 

 

Notes 

----- 

This is used as the slot shape by default in forced measurement, allowing 

subsequent measurements to simply refer to the slot value just as they 

would in single-frame measurement. 

""" 

 

ConfigClass = ForcedTransformedShapeConfig 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.SHAPE_ORDER 

 

def __init__(self, config, name, schemaMapper, metadata): 

ForcedPlugin.__init__(self, config, name, schemaMapper, metadata) 

schema = schemaMapper.editOutputSchema() 

# Allocate xx, yy, xy fields, join these into a single FunctorKey for ease-of-use. 

xxKey = schema.addField(name + "_xx", type="D", doc="transformed reference shape x^2 moment", 

units="pixel^2") 

yyKey = schema.addField(name + "_yy", type="D", doc="transformed reference shape y^2 moment", 

units="pixel^2") 

xyKey = schema.addField(name + "_xy", type="D", doc="transformed reference shape xy moment", 

units="pixel^2") 

self.shapeKey = lsst.afw.table.QuadrupoleKey(xxKey, yyKey, xyKey) 

# Because we're taking the reference position as given, we don't bother transforming its 

# uncertainty and reporting that here, so there are no sigma or cov fields. We do propagate 

# the flag field, if it exists. 

if "slot_Shape_flag" in schemaMapper.getInputSchema(): 

self.flagKey = schema.addField(name + "_flag", type="Flag", 

doc="whether the reference shape is marked as bad") 

else: 

self.flagKey = None 

 

def measure(self, measRecord, exposure, refRecord, refWcs): 

targetWcs = exposure.getWcs() 

if not refWcs == targetWcs: 

fullTransform = lsst.afw.geom.makeWcsPairTransform(refWcs, targetWcs) 

localTransform = lsst.afw.geom.linearizeTransform(fullTransform, refRecord.getCentroid()) 

measRecord.set(self.shapeKey, refRecord.getShape().transform(localTransform.getLinear())) 

else: 

measRecord.set(self.shapeKey, refRecord.getShape()) 

if self.flagKey is not None: 

measRecord.set(self.flagKey, refRecord.getShapeFlag())