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

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

 

import lsst.pex.config 

from .pluginsBase import BasePlugin 

from .pluginRegistry import generateAlgorithmName, register 

from .apCorrRegistry import addApCorrName 

from .sfm import SingleFramePlugin, SingleFramePluginConfig 

from .forcedMeasurement import ForcedPlugin, ForcedPluginConfig 

 

__all__ = ("wrapSingleFrameAlgorithm", "wrapForcedAlgorithm", "wrapSimpleAlgorithm", 

"wrapAlgorithm", "wrapAlgorithmControl", "wrapTransform", "GenericPlugin") 

 

 

class WrappedSingleFramePlugin(SingleFramePlugin): 

 

def __init__(self, config, name, schema, metadata, logName=None): 

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

if hasattr(self, "hasLogName") and self.hasLogName and logName is not None: 

self.cpp = self.factory(config, name, schema, metadata, logName=logName) 

else: 

self.cpp = self.factory(config, name, schema, metadata) 

 

def measure(self, measRecord, exposure): 

self.cpp.measure(measRecord, exposure) 

 

def measureN(self, measCat, exposure): 

self.cpp.measureN(measCat, exposure) 

 

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

self.cpp.fail(measRecord, error.cpp if error is not None else None) 

 

 

class WrappedForcedPlugin(ForcedPlugin): 

 

def __init__(self, config, name, schemaMapper, metadata, logName=None): 

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

if hasattr(self, "hasLogName") and self.hasLogName and logName is not None: 

self.cpp = self.factory(config, name, schemaMapper, metadata, logName=logName) 

else: 

self.cpp = self.factory(config, name, schemaMapper, metadata) 

 

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

self.cpp.measureForced(measRecord, exposure, refRecord, refWcs) 

 

def measureN(self, measCat, exposure, refCat, refWcs): 

self.cpp.measureNForced(measCat, exposure, refCat, refWcs) 

 

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

self.cpp.fail(measRecord, error.cpp if error is not None else None) 

 

 

def wrapAlgorithmControl(Base, Control, module=None, hasMeasureN=False): 

"""Wrap a C++ algorithm's control class into a Python config class. 

 

Parameters 

---------- 

Base : `SingleFramePluginConfig` or `ForcedPluginConfig` 

Base class for the returned config. 

Control : pybind11-wrapped version of a C++ class. 

Control class to be wrapped. 

module : module, `str`, `int`, or `None`; optional 

Either a module object, a string specifying the name of the module, or 

an integer specifying how far back in the stack to look for the module 

to use: ``0`` is `lsst.pex.config.wrap`, ``1`` is 

`lsst.meas.base.wrappers`, ``2`` is the immediate caller, etc. This 

will be used to set ``__module__`` for the new config class, and the 

class will also be added to the module. The default is none in which 

case module will be looked up from Control. 

hasMeasureN : `bool`, optional 

Whether the plugin supports fitting multiple objects at once (if so, a 

config option to enable/disable this will be added). 

 

Returns 

------- 

ConfigClass : `lsst.pex.config.Config` 

A new subclass of lsst.pex.config.Config. 

 

Notes 

----- 

This function is generally only called by `wrapAlgorithm`; it is unlikely 

users will have to call it directly. 

""" 

102 ↛ 108line 102 didn't jump to line 108, because the condition on line 102 was never true if hasMeasureN: 

# We need to add a Config field to enable multi-object measurement, to 

# replace the simple bool class attribute that's on the base class. 

# To do that, we create the Config class dynamically here, then call 

# makeControlClass to finish it off by adding fields from the control 

# object. 

cls = type( 

Control.__name__.replace("Control", "Config"), 

(Base,), 

{"doMeasureN": lsst.pex.config.Field(dtype=bool, default=True, 

doc="whether to run this plugin in multi-object mode")} 

) 

ConfigClass = lsst.pex.config.makeConfigClass(Control, module=module, cls=cls) 

else: 

# If we don't have to add that Config field, we can delegate all of 

# the work to pex_config's makeControlClass 

ConfigClass = lsst.pex.config.makeConfigClass(Control, module=module, base=Base) 

return ConfigClass 

 

 

def wrapAlgorithm(Base, AlgClass, factory, executionOrder, name=None, Control=None, 

ConfigClass=None, TransformClass=None, doRegister=True, shouldApCorr=False, 

apCorrList=(), hasLogName=False, **kwds): 

"""Wrap a C++ algorithm class to create a measurement plugin. 

 

Parameters 

---------- 

Base : `SingleFramePlugin` or `ForcedPlugin` 

Base class for the returned Plugin. 

AlgClass : API compatible with `SingleFrameAlgorithm` or `ForcedAlgorithm` 

C++ algorithm class to convert. May either derive directly from 

`SingleFrameAlgorithm` or `ForcedAlgorithm`, or be an unrelated class 

which has the same ``measure`` and ``measureN`` signatures. 

factory : callable 

A callable that is used to construct an instance of ``AlgClass``. It 

must take four arguments, either ``(config, name, schema, metadata)`` 

or ``(config, name, schemaMapper, metadata)``, depending on whether 

the algorithm is single-frame or forced. 

executionOrder : `float` 

The order this plugin should be run, relative to others 

(see `BasePlugin.getExecutionOrder`). 

name : `str`, optional 

String to use when registering the algorithm. Ignored if 

``doRegistry=False``, set to ``generateAlgorithmName(AlgClass)`` if 

`None`. 

Control : Pybind11-wrapped version of a C++ class, optional 

Pybind11-wrapped C++ Control class for the algorithm; 

``AlgClass.Control`` is used if `None`. Ignored if ``ConfigClass`` 

is not `None`. 

ConfigClass : subclass of `BaseMeasurementPluginConfig` 

Python config class that wraps the C++ algorithm's pybind11-wrapped 

Control class. If `None`, `wrapAlgorithmControl` is called to 

generate a Config class using the ``Control`` argument. 

TransformClass : subclass of `MeasurementTransform`, optional 

Transformation which may be used to post-process the results of 

measurement. If `None`, the default defined by `BasePlugin` is 

used. 

doRegister : `bool`, optional 

If `True` (the default), register the plugin with ``Base``'s 

registry, allowing it to be used by measurement tasks. 

shouldApCorr : `bool`, optional 

Does this algorithm measure an instFlux that can be aperture 

corrected? This is shorthand for ``apCorrList=[name]`` and is ignored 

if ``apCorrList`` is specified. 

apCorrList : iterable of `str`, optional 

Field name prefixes for instFlux fields to be aperture corrected. If 

an algorithm measures a single instFlux that should be aperture 

corrected, then it is simpler to set ``shouldApCorr=True``. However, 

if an algorithm produces multiple such fields, then specify 

``apCorrList`` instead. For example, ``modelfit_CModel`` produces 

three such fields: ``apCorrList= ("modelfit_CModel_exp", 

"modelfit_CModel_exp", "modelfit_CModel_def")`` If ``apCorrList`` is 

not empty then ``shouldApCorr`` is ignored. If non-empty and 

``doRegister`` is `True` then the names are added to the set 

retrieved by ``getApCorrNameSet``. 

hasLogName : `bool`, optional 

`True` if the C++ algorithm supports ``logName`` as a constructor 

argument. 

**kwds 

Additional keyword arguments passed to generateAlgorithmControl, which 

may include: 

 

- ``hasMeasureN``: Whether the plugin supports fitting multiple 

objects at once ;if so, a config option to enable/disable this will 

be added (`bool`). 

- ``executionOrder``: If not `None`, an override for the default 

execution order for this plugin (the default is ``2.0``, which is 

usually appropriate for fluxes; `bool`). 

 

Returns 

------- 

PluginClass : subclass of ``Base`` 

The new plugin class. 

""" 

196 ↛ 201line 196 didn't jump to line 201, because the condition on line 196 was never false if ConfigClass is None: 

197 ↛ 198line 197 didn't jump to line 198, because the condition on line 197 was never true if Control is None: 

Control = AlgClass.Control 

ConfigClass = wrapAlgorithmControl(Base.ConfigClass, Control, **kwds) 

 

def getExecutionOrder(): 

return executionOrder 

typeDict = dict(AlgClass=AlgClass, ConfigClass=ConfigClass, factory=staticmethod(factory), 

getExecutionOrder=staticmethod(getExecutionOrder)) 

if TransformClass: 

206 ↛ exitline 206 didn't run the lambda on line 206 typeDict['getTransformClass'] = staticmethod(lambda: TransformClass) 

PluginClass = type(AlgClass.__name__ + Base.__name__, (Base,), typeDict) 

208 ↛ 214line 208 didn't jump to line 214, because the condition on line 208 was never false if doRegister: 

if name is None: 

name = generateAlgorithmName(AlgClass) 

Base.registry.register(name, PluginClass) 

if shouldApCorr: 

addApCorrName(name) 

PluginClass.hasLogName = hasLogName 

return PluginClass 

 

 

def wrapSingleFrameAlgorithm(AlgClass, executionOrder, name=None, needsMetadata=False, hasMeasureN=False, 

hasLogName=False, **kwds): 

"""Expose a C++ ``SingleFrameAlgorithm`` class as a measurement plugin. 

 

Parameters 

---------- 

AlgClass : API compatible with `SingleFrameAlgorithm` 

C++ algorithm class to convert. May either derive directly from 

`SingleFrameAlgorithm` or be an unrelated class which has the same 

``measure``, ``measureN`` and ``fail`` signatures. 

executionOrder : `float` 

The order this plugin should be run, relative to others 

(see `BasePlugin.getExecutionOrder`). 

name : `str`, optional 

Name to use when registering the algorithm. Ignored if 

``doRegistry=False``; set to ``generateAlgorithmName(AlgClass)`` if 

`None`. 

needsMetadata : `bool`, optional 

Sets whether the ``AlgClass``'s constructor should be passed a 

`~lsst.daf.base.PropertySet` metadata argument. 

hasMeasureN : `bool`, optional 

Does the algorithm support simultaneous measurement of multiple 

sources? If `True`, a `bool` ``doMeasureN`` field will be added to 

the generated config class, and its value will be passed as the last 

argument when calling the ``AlgClass`` constructor. 

hasLogName : `bool`, optional 

`True` if the C++ algorithm supports ``logName`` as a constructor 

argument. 

**kwds 

Additional keyword arguments are passed to the lower-level 

`wrapAlgorithm` and `wrapAlgorithmControl` classes. 

 

Returns 

------- 

singleFramePlugin : subclass of `SingleFramePlugin` 

The new measurement plugin class. 

 

Notes 

----- 

The first three arguments to the C++ constructor are expected to be 

``Control const & ctrl, std::string const & name, Schema & schema``. 

 

If ``needsMetadata`` is `True`, we also append ``PropertySet & metadata``. 

 

If ``hasMeasureN`` is `True`, we also append ``bool doMeasureN``. 

 

If ``hasLogName`` is `True`, we also append ``std::string logName``. 

 

If more than one of the above is `True`, the metadata ``PropertySet`` 

precedes the ``doMeasureN`` ``bool`` and the ``logName`` comes last of the 

three. 

""" 

270 ↛ 271line 270 didn't jump to line 271, because the condition on line 270 was never true if hasMeasureN: 

if needsMetadata: 

def factory(config, name, schema, metadata, **kwargs): 

return AlgClass(config.makeControl(), name, schema, metadata, config.doMeasureN, **kwargs) 

else: 

def factory(config, name, schema, metadata, **kwargs): 

return AlgClass(config.makeControl(), name, schema, config.doMeasureN, **kwargs) 

else: 

if needsMetadata: 

def factory(config, name, schema, metadata, **kwargs): 

return AlgClass(config.makeControl(), name, schema, metadata, **kwargs) 

else: 

def factory(config, name, schema, metadata, **kwargs): 

return AlgClass(config.makeControl(), name, schema, **kwargs) 

 

return wrapAlgorithm(WrappedSingleFramePlugin, AlgClass, executionOrder=executionOrder, name=name, 

factory=factory, hasMeasureN=hasMeasureN, hasLogName=hasLogName, **kwds) 

 

 

def wrapForcedAlgorithm(AlgClass, executionOrder, name=None, needsMetadata=False, 

hasMeasureN=False, needsSchemaOnly=False, hasLogName=False, **kwds): 

"""Expose a C++ ``ForcedAlgorithm`` class as a measurement plugin. 

 

Parameters 

---------- 

AlgClass : API compatible with `ForcedAlgorithm` 

C++ algorithm class to convert. May either derive directly from 

`ForcedAlgorithm` or be an unrelated class which has the same 

``measure``, ``measureN`` and ``fail`` signatures. 

executionOrder : `float` 

The order this plugin should be run, relative to others 

(see `BasePlugin.getExecutionOrder`). 

name : `str`, optional 

Name to use when registering the algorithm. Ignored if 

``doRegistry=False``; set to ``generateAlgorithmName(AlgClass)`` if 

`None`. 

needsMetadata : `bool`, optional 

Sets whether the ``AlgClass``'s constructor should be passed a 

`~lsst.daf.base.PropertySet` metadata argument. 

hasMeasureN : `bool`, optional 

Does the algorithm support simultaneous measurement of multiple 

sources? If `True`, a `bool` ``doMeasureN`` field will be added to 

the generated config class, and its value will be passed as the last 

argument when calling the ``AlgClass`` constructor. 

hasLogName : `bool`, optional 

`True` if the C++ algorithm supports ``logName`` as a constructor 

argument. 

needsSchemaOnly : `bool`, optional 

Whether the algorithm constructor expects a Schema argument 

(representing the output `~lsst.afw.table.Schema`) rather than the 

full `~lsst.afw.table.SchemaMapper` (which provides access to both the 

reference schema and the output schema). 

**kwds 

Additional keyword arguments are passed to the lower-level 

`wrapAlgorithm` and `wrapAlgorithmControl` classes. 

 

Returns 

------- 

forcedPlugin : subclass of `ForcedPlugin` 

The new measurement plugin class. 

 

Notes 

----- 

The first two arguments to the C++ constructor are expected to be 

``Control const & ctrl, std::string const & name`` 

 

If ``needsSchemaOnly`` is `True`, then the third argument will be 

``Schema & schema``; otherwise, it will be ``SchemaMapper & 

schemaMapper``. 

 

If ``needsMetadata`` is `True`, we also append ``PropertySet & 

metadata``. 

 

If ``hasMeasureN`` is `True`, we also append ``bool doMeasureN``. 

 

If ``hasLogName`` is `True`, we also append ``std::string logName``. 

 

If more than one of the above is `True`, the metadata ``PropertySet`` 

precedes the ``doMeasureN`` ``bool`` and the ``logName`` comes last of the 

three. 

""" 

351 ↛ 355line 351 didn't jump to line 355, because the condition on line 351 was never false if needsSchemaOnly: 

def extractSchemaArg(m): 

return m.editOutputSchema() 

else: 

def extractSchemaArg(m): 

return m 

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

if needsMetadata: 

def factory(config, name, schemaMapper, metadata, **kwargs): 

return AlgClass(config.makeControl(), name, extractSchemaArg(schemaMapper), 

metadata, config.doMeasureN, **kwargs) 

else: 

def factory(config, name, schemaMapper, metadata, **kwargs): 

return AlgClass(config.makeControl(), name, extractSchemaArg(schemaMapper), 

config.doMeasureN, **kwargs) 

else: 

if needsMetadata: 

def factory(config, name, schemaMapper, metadata, **kwargs): 

return AlgClass(config.makeControl(), name, extractSchemaArg(schemaMapper), 

metadata, **kwargs) 

else: 

def factory(config, name, schemaMapper, metadata, **kwargs): 

return AlgClass(config.makeControl(), name, extractSchemaArg(schemaMapper), **kwargs) 

 

return wrapAlgorithm(WrappedForcedPlugin, AlgClass, executionOrder=executionOrder, name=name, 

factory=factory, hasLogName=hasLogName, **kwds) 

 

 

def wrapSimpleAlgorithm(AlgClass, executionOrder, name=None, needsMetadata=False, hasMeasureN=False, 

hasLogName=False, **kwds): 

r"""Expose a C++ ``SimpleAlgorithm`` class as a measurement plugin. 

 

``SimpleAlgorithm``\ s are made available as both `SingleFramePlugin`\ s 

and `ForcedPlugin`\ s. 

 

Parameters 

---------- 

AlgClass : Subclass of C++ ``SimpleAlgorithm``, or API compatible 

Algorithm class to convert. The C++ class should be wrapped with 

Pybind11, and must provide ``measure()``, ``measureN()`` and ``fail()` 

signatures equivalent to ``SimpleAlgorithm``. 

executionOrder : `float` 

The order this plugin should be run, relative to others 

(see `~BasePlugin.getExecutionOrder`). 

name : `str`, optional 

Name to use when registering the algorithm. Ignored if 

``doRegistry=False``; set to ``generateAlgorithmName(AlgClass)`` if 

`None`. 

needsMetadata : `bool`, optional 

Sets whether the ``AlgClass``'s constructor should be passed a 

`~lsst.daf.base.PropertySet` metadata argument. 

hasMeasureN : `bool`, optional 

Does the algorithm support simultaneous measurement of multiple 

sources? If `True`, a `bool` ``doMeasureN`` field will be added to 

the generated config class, and its value will be passed as the last 

argument when calling the ``AlgClass`` constructor. 

hasLogName : `bool`, optional 

`True` if the C++ algorithm supports ``logName`` as a constructor 

argument. 

**kwds 

Additional keyword arguments are passed to the lower-level 

`wrapAlgorithm` and `wrapAlgorithmControl` classes. 

 

Returns 

------- 

singleFramePlugin : subclass of `SingleFramePlugin` 

The new single frame measurement plugin class. 

forcedPlugin : subclass of `ForcedPlugin` 

The new forced measurement plugin class. 

 

Notes 

----- 

The first three arguments to the C++ constructor are expected to be 

``Control const & ctrl, std::string const & name, Schema & schema``. 

 

If ``needsMetadata`` is `True`, we also append ``PropertySet & 

metadata``. 

 

If ``hasMeasureN`` is `True`, we also append ``bool doMeasureN``. 

 

If ``hasLogName`` is `True`, we also append ``std::string logName``. 

 

If more than one of the above is `True`, the metadata ``PropertySet`` 

precedes the ``doMeasureN`` ``bool`` and the ``logName`` comes last of the 

three. 

""" 

return (wrapSingleFrameAlgorithm(AlgClass, executionOrder=executionOrder, name=name, 

needsMetadata=needsMetadata, hasLogName=hasLogName, **kwds), 

wrapForcedAlgorithm(AlgClass, executionOrder=executionOrder, name=name, 

needsMetadata=needsMetadata, hasLogName=hasLogName, 

needsSchemaOnly=True, **kwds)) 

 

 

def wrapTransform(transformClass, hasLogName=False): 

"""Modify a C++ transform to accept either a ``Config`` or a ``Control``. 

 

That is, the configuration may either be provided as a (C++) ``Control`` 

object or an instance of a Python class derived from 

`~lsst.meas.base.BasePluginConfig`. 

 

Parameters 

---------- 

transformClass : Subclass of C++ ``BaseTransform`` 

A C++ transform class, wrapped with pybind11. Its constructor must 

take a ``Control`` object, a ``std::string``, and a 

`~lsst.afw.table.SchemaMapper`, in that order. 

hasLogName : `bool`, optional 

Unused. 

""" 

oldInit = transformClass.__init__ 

 

def _init(self, ctrl, name, mapper, logName=None): 

if hasattr(ctrl, "makeControl"): 

ctrl = ctrl.makeControl() 

# logName signature needs to be on this Class __init__, but is not 

# needed by the C++ plugin. 

oldInit(self, ctrl, name, mapper) 

 

transformClass.__init__ = _init 

 

 

class GenericPlugin(BasePlugin): 

"""Abstract base class for a generic plugin. 

 

Parameters 

---------- 

config : `lsst.pex.config.Config` 

An instance of this class' ``ConfigClass``. 

name : `str` 

Name of this measurement plguin, for registering. 

schema : `lsst.afw.table.Schema` 

The catalog schema. New fields should be added here to 

hold measurements produced by this plugin. 

metadata : `lsst.daf.base.PropertySet` 

Metadata that will be attached to the output catalog. 

logName : `str`, optional 

Name of log component. 

 

Notes 

----- 

A generic plugin can be used with the `singleFramePluginFromGeneric` 

and/or `forcedPluginFromGeneric` wrappers to create classes that can be 

used for single frame measurement and/or forced measurement (as 

appropriate). The only real difference between `SingleFramePlugin` and 

`ForcedPlugin` is the ``measure`` method; this class introduces a shared 

signature for `measure` that, in combination with the aforementioned 

wrappers, allows both plugin styles to share a single implementation. 

 

This doesn't use `abc.ABCMeta` because I couldn't get it to work 

with a superclass. 

 

Sub-classes should set `ConfigClass` and implement the `measure` and 

`measureN` methods. They may optionally provide alternative 

implementations for the `__init__`, `fail` and `getExecutionOrder` 

methods. 

 

This default implementation simply adds a field for recording 

a fatal failure of the measurement plugin. 

""" 

ConfigClass = None 

 

@classmethod 

def getExecutionOrder(cls): 

return 0 

 

def __init__(self, config, name, schema, metadata, logName=None): 

BasePlugin.__init__(self, config, name, logName=logName) 

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

 

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

"""Measure a single source. 

 

It is the responsibility of this method to perform the desired 

measurement and record the result in the `measRecord`. 

 

Parameters 

---------- 

measRecord : `lsst.afw.table.SourceRecord` 

Catalog record for the source being measured. 

exposure : `lsst.afw.image.Exposure` 

Exposure on which the source is being measured. 

center : `lsst.geom.Point2D` 

Pixel coordinates of the object. 

 

Raises 

------ 

MeasurementError 

Raised if the measurement fails for a known/justifiable reason. 

""" 

raise NotImplementedError() 

 

def measureN(self, measCat, exposure, refCat, refWcs): 

"""Measure multiple sources. 

 

It is the responsibility of this method to perform the desired 

measurement and record the result in the `measCat`. 

 

Parameters 

---------- 

measCat : `lsst.afw.table.SourceCatalog` 

Catalog for the sources being measured. 

exposure : `lsst.afw.image.Exposure` 

Exposure on which the source is being measured. 

refCat : `lsst.afw.table.SourceCatalog` 

Reference catalog. 

refWcs : `lsst.afw.image.Wcs` 

Astrometric solution for the reference image. 

 

Raises 

------ 

MeasurementError 

Raised if the measurement fails for a known/justifiable reason. 

""" 

raise NotImplementedError() 

 

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

"""Record a measurement failure. 

 

This default implementation simply records the failure in the source 

record. 

 

Parameters 

---------- 

measRecord : `lsst.afw.table.SourceRecord` 

Catalog record for the source being measured. 

error : `Exception` 

Error causing failure, or `None`. 

""" 

measRecord.set(self._failKey, True) 

 

@classmethod 

def makeSingleFramePlugin(cls, name): 

"""Produce a SingleFramePlugin subclass from this GenericPlugin class. 

 

The class is also registered. 

 

Parameters 

---------- 

name : `str` 

Name of plugin to register. 

""" 

class SingleFrameFromGenericConfig(cls.ConfigClass, SingleFramePluginConfig): 

pass 

 

@register(name) 

class SingleFrameFromGenericPlugin(SingleFramePlugin): 

ConfigClass = SingleFrameFromGenericConfig 

 

def __init__(self, config, name, schema, metadata, logName=None): 

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

self._generic = cls(config, name, schema, metadata) 

 

def measure(self, measRecord, exposure): 

center = measRecord.getCentroid() 

return self._generic.measure(measRecord, exposure, center) 

 

def measureN(self, measCat, exposure, refCat, refWcs): 

return self._generic.measureN(measCat, exposure, refCat, refWcs) 

 

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

self._generic.fail(measRecord, error if error is not None else None) 

 

@staticmethod 

def getExecutionOrder(): 

return cls.getExecutionOrder() 

 

def getTransformClass(self): 

return self._generic.getTransformClass() 

 

return SingleFrameFromGenericPlugin 

 

@classmethod 

def makeForcedPlugin(cls, name): 

"""Produce a ForcedPlugin subclass from this GenericPlugin class. 

 

The class is also registered. 

 

Parameters 

---------- 

name : `str` 

Name of plugin to register. 

""" 

class ForcedFromGenericConfig(cls.ConfigClass, ForcedPluginConfig): 

pass 

 

@register(name) 

class ForcedFromGenericPlugin(ForcedPlugin): 

ConfigClass = ForcedFromGenericConfig 

 

def __init__(self, config, name, schemaMapper, metadata, logName=None): 

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

schema = schemaMapper.editOutputSchema() 

self._generic = cls(config, name, schema, metadata) 

 

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

center = exposure.getWcs().skyToPixel(refWcs.pixelToSky(refRecord.getCentroid())) 

return self._generic.measure(measRecord, exposure, center) 

 

def measureN(self, measCat, exposure, refCat, refWcs): 

return self._generic.measureN(measCat, exposure, refCat, refWcs) 

 

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

self._generic.fail(measRecord, error if error is not None else None) 

 

@staticmethod 

def getExecutionOrder(): 

return cls.getExecutionOrder() 

 

def getTransformClass(self): 

return self._generic.getTransformClass() 

 

return ForcedFromGenericPlugin