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

969

970

971

# 

# LSST Data Management System 

# 

# Copyright 2008-2017 AURA/LSST. 

# 

# 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 <https://www.lsstcorp.org/LegalNotices/>. 

# 

 

__all__ = ("SourceDetectionConfig", "SourceDetectionTask", "addExposures") 

 

from contextlib import contextmanager 

 

import numpy as np 

 

import lsst.geom 

import lsst.afw.display as afwDisplay 

import lsst.afw.detection as afwDet 

import lsst.afw.geom as afwGeom 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

import lsst.afw.table as afwTable 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

from .subtractBackground import SubtractBackgroundTask 

 

 

class SourceDetectionConfig(pexConfig.Config): 

"""!Configuration parameters for the SourceDetectionTask 

""" 

minPixels = pexConfig.RangeField( 

doc="detected sources with fewer than the specified number of pixels will be ignored", 

dtype=int, optional=False, default=1, min=0, 

) 

isotropicGrow = pexConfig.Field( 

doc="Pixels should be grown as isotropically as possible (slower)", 

dtype=bool, optional=False, default=False, 

) 

combinedGrow = pexConfig.Field( 

doc="Grow all footprints at the same time? This allows disconnected footprints to merge.", 

dtype=bool, default=True, 

) 

nSigmaToGrow = pexConfig.Field( 

doc="Grow detections by nSigmaToGrow * [PSF RMS width]; if 0 then do not grow", 

dtype=float, default=2.4, # 2.4 pixels/sigma is roughly one pixel/FWHM 

) 

returnOriginalFootprints = pexConfig.Field( 

doc="Grow detections to set the image mask bits, but return the original (not-grown) footprints", 

dtype=bool, optional=False, default=False, 

) 

thresholdValue = pexConfig.RangeField( 

doc="Threshold for footprints; exact meaning and units depend on thresholdType.", 

dtype=float, optional=False, default=5.0, min=0.0, 

) 

includeThresholdMultiplier = pexConfig.RangeField( 

doc="Include threshold relative to thresholdValue", 

dtype=float, default=1.0, min=0.0, 

) 

thresholdType = pexConfig.ChoiceField( 

doc="specifies the desired flavor of Threshold", 

dtype=str, optional=False, default="stdev", 

allowed={ 

"variance": "threshold applied to image variance", 

"stdev": "threshold applied to image std deviation", 

"value": "threshold applied to image value", 

"pixel_stdev": "threshold applied to per-pixel std deviation", 

}, 

) 

thresholdPolarity = pexConfig.ChoiceField( 

doc="specifies whether to detect positive, or negative sources, or both", 

dtype=str, optional=False, default="positive", 

allowed={ 

"positive": "detect only positive sources", 

"negative": "detect only negative sources", 

"both": "detect both positive and negative sources", 

}, 

) 

adjustBackground = pexConfig.Field( 

dtype=float, 

doc="Fiddle factor to add to the background; debugging only", 

default=0.0, 

) 

reEstimateBackground = pexConfig.Field( 

dtype=bool, 

doc="Estimate the background again after final source detection?", 

default=True, optional=False, 

) 

background = pexConfig.ConfigurableField( 

doc="Background re-estimation; ignored if reEstimateBackground false", 

target=SubtractBackgroundTask, 

) 

tempLocalBackground = pexConfig.ConfigurableField( 

doc=("A local (small-scale), temporary background estimation step run between " 

"detecting above-threshold regions and detecting the peaks within " 

"them; used to avoid detecting spuerious peaks in the wings."), 

target=SubtractBackgroundTask, 

) 

doTempLocalBackground = pexConfig.Field( 

dtype=bool, 

doc="Enable temporary local background subtraction? (see tempLocalBackground)", 

default=True, 

) 

tempWideBackground = pexConfig.ConfigurableField( 

doc=("A wide (large-scale) background estimation and removal before footprint and peak detection. " 

"It is added back into the image after detection. The purpose is to suppress very large " 

"footprints (e.g., from large artifacts) that the deblender may choke on."), 

target=SubtractBackgroundTask, 

) 

doTempWideBackground = pexConfig.Field( 

dtype=bool, 

doc="Do temporary wide (large-scale) background subtraction before footprint detection?", 

default=False, 

) 

nPeaksMaxSimple = pexConfig.Field( 

dtype=int, 

doc=("The maximum number of peaks in a Footprint before trying to " 

"replace its peaks using the temporary local background"), 

default=1, 

) 

nSigmaForKernel = pexConfig.Field( 

dtype=float, 

doc=("Multiple of PSF RMS size to use for convolution kernel bounding box size; " 

"note that this is not a half-size. The size will be rounded up to the nearest odd integer"), 

default=7.0, 

) 

statsMask = pexConfig.ListField( 

dtype=str, 

doc="Mask planes to ignore when calculating statistics of image (for thresholdType=stdev)", 

default=['BAD', 'SAT', 'EDGE', 'NO_DATA'], 

) 

 

def setDefaults(self): 

self.tempLocalBackground.binSize = 64 

self.tempLocalBackground.algorithm = "AKIMA_SPLINE" 

self.tempLocalBackground.useApprox = False 

# Background subtraction to remove a large-scale background (e.g., scattered light); restored later. 

# Want to keep it from exceeding the deblender size limit of 1 Mpix, so half that is reasonable. 

self.tempWideBackground.binSize = 512 

self.tempWideBackground.algorithm = "AKIMA_SPLINE" 

self.tempWideBackground.useApprox = False 

# Ensure we can remove even bright scattered light that is DETECTED 

for maskPlane in ("DETECTED", "DETECTED_NEGATIVE"): 

if maskPlane in self.tempWideBackground.ignoredPixelMask: 

self.tempWideBackground.ignoredPixelMask.remove(maskPlane) 

 

## @addtogroup LSST_task_documentation 

## @{ 

## @page sourceDetectionTask 

## @ref SourceDetectionTask_ "SourceDetectionTask" 

## @copybrief SourceDetectionTask 

## @} 

 

 

class SourceDetectionTask(pipeBase.Task): 

r"""! 

@anchor SourceDetectionTask_ 

 

@brief Detect positive and negative sources on an exposure and return a new @link table.SourceCatalog@endlink. 

 

@section meas_algorithms_detection_Contents Contents 

 

- @ref meas_algorithms_detection_Purpose 

- @ref meas_algorithms_detection_Initialize 

- @ref meas_algorithms_detection_Invoke 

- @ref meas_algorithms_detection_Config 

- @ref meas_algorithms_detection_Debug 

- @ref meas_algorithms_detection_Example 

 

@section meas_algorithms_detection_Purpose Description 

 

@copybrief SourceDetectionTask 

 

@section meas_algorithms_detection_Initialize Task initialisation 

 

@copydoc \_\_init\_\_ 

 

@section meas_algorithms_detection_Invoke Invoking the Task 

 

@copydoc run 

 

@section meas_algorithms_detection_Config Configuration parameters 

 

See @ref SourceDetectionConfig 

 

@section meas_algorithms_detection_Debug Debug variables 

 

The @link lsst.pipe.base.cmdLineTask.CmdLineTask command line task@endlink interface supports a 

flag @c -d to import @b debug.py from your @c PYTHONPATH; see @ref baseDebug for more about @b debug.py files. 

 

The available variables in SourceDetectionTask are: 

<DL> 

<DT> @c display 

<DD> 

- If True, display the exposure on afwDisplay.Display's frame 0. 

+ve detections in blue, -ve detections in cyan 

- If display > 1, display the convolved exposure on frame 1 

</DL> 

 

@section meas_algorithms_detection_Example A complete example of using SourceDetectionTask 

 

This code is in @link measAlgTasks.py@endlink in the examples directory, and can be run as @em e.g. 

@code 

examples/measAlgTasks.py --doDisplay 

@endcode 

@dontinclude measAlgTasks.py 

The example also runs the SingleFrameMeasurementTask; see @ref meas_algorithms_measurement_Example for more 

explanation. 

 

Import the task (there are some other standard imports; read the file if you're confused) 

@skipline SourceDetectionTask 

 

We need to create our task before processing any data as the task constructor 

can add an extra column to the schema, but first we need an almost-empty Schema 

@skipline makeMinimalSchema 

after which we can call the constructor: 

@skip SourceDetectionTask.ConfigClass 

@until detectionTask 

 

We're now ready to process the data (we could loop over multiple exposures/catalogues using the same 

task objects). First create the output table: 

@skipline afwTable 

 

And process the image 

@skipline result 

(You may not be happy that the threshold was set in the config before creating the Task rather than being set 

separately for each exposure. You @em can reset it just before calling the run method if you must, but we 

should really implement a better solution). 

 

We can then unpack and use the results: 

@skip sources 

@until print 

 

<HR> 

To investigate the @ref meas_algorithms_detection_Debug, put something like 

@code{.py} 

import lsstDebug 

def DebugInfo(name): 

di = lsstDebug.getInfo(name) # N.b. lsstDebug.Info(name) would call us recursively 

if name == "lsst.meas.algorithms.detection": 

di.display = 1 

 

return di 

 

lsstDebug.Info = DebugInfo 

@endcode 

into your debug.py file and run measAlgTasks.py with the @c --debug flag. 

""" 

ConfigClass = SourceDetectionConfig 

_DefaultName = "sourceDetection" 

 

def __init__(self, schema=None, **kwds): 

"""!Create the detection task. Most arguments are simply passed onto pipe.base.Task. 

 

@param schema An lsst::afw::table::Schema used to create the output lsst.afw.table.SourceCatalog 

@param **kwds Keyword arguments passed to lsst.pipe.base.task.Task.__init__. 

 

If schema is not None and configured for 'both' detections, 

a 'flags.negative' field will be added to label detections made with a 

negative threshold. 

 

@note This task can add fields to the schema, so any code calling this task must ensure that 

these columns are indeed present in the input match list; see @ref Example 

""" 

pipeBase.Task.__init__(self, **kwds) 

if schema is not None and self.config.thresholdPolarity == "both": 

self.negativeFlagKey = schema.addField( 

"flags_negative", type="Flag", 

doc="set if source was detected as significantly negative" 

) 

else: 

if self.config.thresholdPolarity == "both": 

self.log.warn("Detection polarity set to 'both', but no flag will be " 

"set to distinguish between positive and negative detections") 

self.negativeFlagKey = None 

if self.config.reEstimateBackground: 

self.makeSubtask("background") 

if self.config.doTempLocalBackground: 

self.makeSubtask("tempLocalBackground") 

if self.config.doTempWideBackground: 

self.makeSubtask("tempWideBackground") 

 

@pipeBase.timeMethod 

def run(self, table, exposure, doSmooth=True, sigma=None, clearMask=True, expId=None): 

"""Run source detection and create a SourceCatalog of detections. 

 

Parameters 

---------- 

table : `lsst.afw.table.SourceTable` 

Table object that will be used to create the SourceCatalog. 

exposure : `lsst.afw.image.Exposure` 

Exposure to process; DETECTED mask plane will be set in-place. 

doSmooth : `bool` 

If True, smooth the image before detection using a Gaussian of width 

``sigma``, or the measured PSF width. Set to False when running on 

e.g. a pre-convolved image, or a mask plane. 

sigma : `float` 

Sigma of PSF (pixels); used for smoothing and to grow detections; 

if None then measure the sigma of the PSF of the exposure 

clearMask : `bool` 

Clear DETECTED{,_NEGATIVE} planes before running detection. 

expId : `int` 

Exposure identifier; unused by this implementation, but used for 

RNG seed by subclasses. 

 

Returns 

------- 

result : `lsst.pipe.base.Struct` 

``sources`` 

The detected sources (`lsst.afw.table.SourceCatalog`) 

``fpSets`` 

The result resturned by `detectFootprints` 

(`lsst.pipe.base.Struct`). 

 

Raises 

------ 

ValueError 

If flags.negative is needed, but isn't in table's schema. 

lsst.pipe.base.TaskError 

If sigma=None, doSmooth=True and the exposure has no PSF. 

 

Notes 

----- 

If you want to avoid dealing with Sources and Tables, you can use 

detectFootprints() to just get the `lsst.afw.detection.FootprintSet`s. 

""" 

if self.negativeFlagKey is not None and self.negativeFlagKey not in table.getSchema(): 

raise ValueError("Table has incorrect Schema") 

results = self.detectFootprints(exposure=exposure, doSmooth=doSmooth, sigma=sigma, 

clearMask=clearMask, expId=expId) 

sources = afwTable.SourceCatalog(table) 

sources.reserve(results.numPos + results.numNeg) 

if results.negative: 

results.negative.makeSources(sources) 

if self.negativeFlagKey: 

for record in sources: 

record.set(self.negativeFlagKey, True) 

if results.positive: 

results.positive.makeSources(sources) 

results.fpSets = results.copy() # Backward compatibility 

results.sources = sources 

return results 

 

## An alias for run @deprecated Remove this alias after checking for where it's used 

makeSourceCatalog = run 

 

def display(self, exposure, results, convolvedImage=None): 

"""Display detections if so configured 

 

Displays the ``exposure`` in frame 0, overlays the detection peaks. 

 

Requires that ``lsstDebug`` has been set up correctly, so that 

``lsstDebug.Info("lsst.meas.algorithms.detection")`` evaluates `True`. 

 

If the ``convolvedImage`` is non-`None` and 

``lsstDebug.Info("lsst.meas.algorithms.detection") > 1``, the 

``convolvedImage`` will be displayed in frame 1. 

 

Parameters 

---------- 

exposure : `lsst.afw.image.Exposure` 

Exposure to display, on which will be plotted the detections. 

results : `lsst.pipe.base.Struct` 

Results of the 'detectFootprints' method, containing positive and 

negative footprints (which contain the peak positions that we will 

plot). This is a `Struct` with ``positive`` and ``negative`` 

elements that are of type `lsst.afw.detection.FootprintSet`. 

convolvedImage : `lsst.afw.image.Image`, optional 

Convolved image used for thresholding. 

""" 

try: 

import lsstDebug 

display = lsstDebug.Info(__name__).display 

except ImportError: 

try: 

display 

except NameError: 

display = False 

if not display: 

return 

 

afwDisplay.setDefaultMaskTransparency(75) 

 

disp0 = afwDisplay.Display(frame=0) 

disp0.mtv(exposure, title="detection") 

 

def plotPeaks(fps, ctype): 

if fps is None: 

return 

with disp0.Buffering(): 

for fp in fps.getFootprints(): 

for pp in fp.getPeaks(): 

disp0.dot("+", pp.getFx(), pp.getFy(), ctype=ctype) 

plotPeaks(results.positive, "yellow") 

plotPeaks(results.negative, "red") 

 

if convolvedImage and display > 1: 

disp1 = afwDisplay.Display(frame=1) 

disp1.mtv(convolvedImage, title="PSF smoothed") 

 

def applyTempLocalBackground(self, exposure, middle, results): 

"""Apply a temporary local background subtraction 

 

This temporary local background serves to suppress noise fluctuations 

in the wings of bright objects. 

 

Peaks in the footprints will be updated. 

 

Parameters 

---------- 

exposure : `lsst.afw.image.Exposure` 

Exposure for which to fit local background. 

middle : `lsst.afw.image.MaskedImage` 

Convolved image on which detection will be performed 

(typically smaller than ``exposure`` because the 

half-kernel has been removed around the edges). 

results : `lsst.pipe.base.Struct` 

Results of the 'detectFootprints' method, containing positive and 

negative footprints (which contain the peak positions that we will 

plot). This is a `Struct` with ``positive`` and ``negative`` 

elements that are of type `lsst.afw.detection.FootprintSet`. 

""" 

# Subtract the local background from the smoothed image. Since we 

# never use the smoothed again we don't need to worry about adding 

# it back in. 

bg = self.tempLocalBackground.fitBackground(exposure.getMaskedImage()) 

bgImage = bg.getImageF() 

middle -= bgImage.Factory(bgImage, middle.getBBox()) 

thresholdPos = self.makeThreshold(middle, "positive") 

thresholdNeg = self.makeThreshold(middle, "negative") 

if self.config.thresholdPolarity != "negative": 

self.updatePeaks(results.positive, middle, thresholdPos) 

if self.config.thresholdPolarity != "positive": 

self.updatePeaks(results.negative, middle, thresholdNeg) 

 

def clearMask(self, mask): 

"""Clear the DETECTED and DETECTED_NEGATIVE mask planes 

 

Removes any previous detection mask in preparation for a new 

detection pass. 

 

Parameters 

---------- 

mask : `lsst.afw.image.Mask` 

Mask to be cleared. 

""" 

mask &= ~(mask.getPlaneBitMask("DETECTED") | mask.getPlaneBitMask("DETECTED_NEGATIVE")) 

 

def calculateKernelSize(self, sigma): 

"""Calculate size of smoothing kernel 

 

Uses the ``nSigmaForKernel`` configuration parameter. Note 

that that is the full width of the kernel bounding box 

(so a value of 7 means 3.5 sigma on either side of center). 

The value will be rounded up to the nearest odd integer. 

 

Parameters 

---------- 

sigma : `float` 

Gaussian sigma of smoothing kernel. 

 

Returns 

------- 

size : `int` 

Size of the smoothing kernel. 

""" 

return (int(sigma * self.config.nSigmaForKernel + 0.5)//2)*2 + 1 # make sure it is odd 

 

def getPsf(self, exposure, sigma=None): 

"""Retrieve the PSF for an exposure 

 

If ``sigma`` is provided, we make a ``GaussianPsf`` with that, 

otherwise use the one from the ``exposure``. 

 

Parameters 

---------- 

exposure : `lsst.afw.image.Exposure` 

Exposure from which to retrieve the PSF. 

sigma : `float`, optional 

Gaussian sigma to use if provided. 

 

Returns 

------- 

psf : `lsst.afw.detection.Psf` 

PSF to use for detection. 

""" 

if sigma is None: 

psf = exposure.getPsf() 

if psf is None: 

raise RuntimeError("Unable to determine PSF to use for detection: no sigma provided") 

sigma = psf.computeShape().getDeterminantRadius() 

size = self.calculateKernelSize(sigma) 

psf = afwDet.GaussianPsf(size, size, sigma) 

return psf 

 

def convolveImage(self, maskedImage, psf, doSmooth=True): 

"""Convolve the image with the PSF 

 

We convolve the image with a Gaussian approximation to the PSF, 

because this is separable and therefore fast. It's technically a 

correlation rather than a convolution, but since we use a symmetric 

Gaussian there's no difference. 

 

The convolution can be disabled with ``doSmooth=False``. If we do 

convolve, we mask the edges as ``EDGE`` and return the convolved image 

with the edges removed. This is because we can't convolve the edges 

because the kernel would extend off the image. 

 

Parameters 

---------- 

maskedImage : `lsst.afw.image.MaskedImage` 

Image to convolve. 

psf : `lsst.afw.detection.Psf` 

PSF to convolve with (actually with a Gaussian approximation 

to it). 

doSmooth : `bool` 

Actually do the convolution? Set to False when running on 

e.g. a pre-convolved image, or a mask plane. 

 

Return Struct contents 

---------------------- 

middle : `lsst.afw.image.MaskedImage` 

Convolved image, without the edges. 

sigma : `float` 

Gaussian sigma used for the convolution. 

""" 

self.metadata.set("doSmooth", doSmooth) 

sigma = psf.computeShape().getDeterminantRadius() 

self.metadata.set("sigma", sigma) 

 

if not doSmooth: 

middle = maskedImage.Factory(maskedImage) 

return pipeBase.Struct(middle=middle, sigma=sigma) 

 

# Smooth using a Gaussian (which is separable, hence fast) of width sigma 

# Make a SingleGaussian (separable) kernel with the 'sigma' 

kWidth = self.calculateKernelSize(sigma) 

self.metadata.set("smoothingKernelWidth", kWidth) 

gaussFunc = afwMath.GaussianFunction1D(sigma) 

gaussKernel = afwMath.SeparableKernel(kWidth, kWidth, gaussFunc, gaussFunc) 

 

convolvedImage = maskedImage.Factory(maskedImage.getBBox()) 

 

afwMath.convolve(convolvedImage, maskedImage, gaussKernel, afwMath.ConvolutionControl()) 

# 

# Only search psf-smoothed part of frame 

# 

goodBBox = gaussKernel.shrinkBBox(convolvedImage.getBBox()) 

middle = convolvedImage.Factory(convolvedImage, goodBBox, afwImage.PARENT, False) 

# 

# Mark the parts of the image outside goodBBox as EDGE 

# 

self.setEdgeBits(maskedImage, goodBBox, maskedImage.getMask().getPlaneBitMask("EDGE")) 

 

return pipeBase.Struct(middle=middle, sigma=sigma) 

 

def applyThreshold(self, middle, bbox, factor=1.0): 

"""Apply thresholds to the convolved image 

 

Identifies ``Footprint``s, both positive and negative. 

 

The threshold can be modified by the provided multiplication 

``factor``. 

 

Parameters 

---------- 

middle : `lsst.afw.image.MaskedImage` 

Convolved image to threshold. 

bbox : `lsst.geom.Box2I` 

Bounding box of unconvolved image. 

factor : `float` 

Multiplier for the configured threshold. 

 

Return Struct contents 

---------------------- 

positive : `lsst.afw.detection.FootprintSet` or `None` 

Positive detection footprints, if configured. 

negative : `lsst.afw.detection.FootprintSet` or `None` 

Negative detection footprints, if configured. 

factor : `float` 

Multiplier for the configured threshold. 

""" 

results = pipeBase.Struct(positive=None, negative=None, factor=factor) 

# Detect the Footprints (peaks may be replaced if doTempLocalBackground) 

if self.config.reEstimateBackground or self.config.thresholdPolarity != "negative": 

threshold = self.makeThreshold(middle, "positive", factor=factor) 

results.positive = afwDet.FootprintSet( 

middle, 

threshold, 

"DETECTED", 

self.config.minPixels 

) 

results.positive.setRegion(bbox) 

if self.config.reEstimateBackground or self.config.thresholdPolarity != "positive": 

threshold = self.makeThreshold(middle, "negative", factor=factor) 

results.negative = afwDet.FootprintSet( 

middle, 

threshold, 

"DETECTED_NEGATIVE", 

self.config.minPixels 

) 

results.negative.setRegion(bbox) 

 

return results 

 

def finalizeFootprints(self, mask, results, sigma, factor=1.0): 

"""Finalize the detected footprints 

 

Grows the footprints, sets the ``DETECTED`` and ``DETECTED_NEGATIVE`` 

mask planes, and logs the results. 

 

``numPos`` (number of positive footprints), ``numPosPeaks`` (number 

of positive peaks), ``numNeg`` (number of negative footprints), 

``numNegPeaks`` (number of negative peaks) entries are added to the 

detection results. 

 

Parameters 

---------- 

mask : `lsst.afw.image.Mask` 

Mask image on which to flag detected pixels. 

results : `lsst.pipe.base.Struct` 

Struct of detection results, including ``positive`` and 

``negative`` entries; modified. 

sigma : `float` 

Gaussian sigma of PSF. 

factor : `float` 

Multiplier for the configured threshold. 

""" 

for polarity, maskName in (("positive", "DETECTED"), ("negative", "DETECTED_NEGATIVE")): 

fpSet = getattr(results, polarity) 

if fpSet is None: 

continue 

if self.config.nSigmaToGrow > 0: 

nGrow = int((self.config.nSigmaToGrow * sigma) + 0.5) 

self.metadata.set("nGrow", nGrow) 

if self.config.combinedGrow: 

fpSet = afwDet.FootprintSet(fpSet, nGrow, self.config.isotropicGrow) 

else: 

stencil = (afwGeom.Stencil.CIRCLE if self.config.isotropicGrow else 

afwGeom.Stencil.MANHATTAN) 

for fp in fpSet: 

fp.dilate(nGrow, stencil) 

fpSet.setMask(mask, maskName) 

if not self.config.returnOriginalFootprints: 

setattr(results, polarity, fpSet) 

 

results.numPos = 0 

results.numPosPeaks = 0 

results.numNeg = 0 

results.numNegPeaks = 0 

positive = "" 

negative = "" 

 

if results.positive is not None: 

results.numPos = len(results.positive.getFootprints()) 

results.numPosPeaks = sum(len(fp.getPeaks()) for fp in results.positive.getFootprints()) 

positive = " %d positive peaks in %d footprints" % (results.numPosPeaks, results.numPos) 

if results.negative is not None: 

results.numNeg = len(results.negative.getFootprints()) 

results.numNegPeaks = sum(len(fp.getPeaks()) for fp in results.negative.getFootprints()) 

negative = " %d negative peaks in %d footprints" % (results.numNegPeaks, results.numNeg) 

 

self.log.info("Detected%s%s%s to %g %s" % 

(positive, " and" if positive and negative else "", negative, 

self.config.thresholdValue*self.config.includeThresholdMultiplier*factor, 

"DN" if self.config.thresholdType == "value" else "sigma")) 

 

def reEstimateBackground(self, maskedImage, backgrounds): 

"""Estimate the background after detection 

 

Parameters 

---------- 

maskedImage : `lsst.afw.image.MaskedImage` 

Image on which to estimate the background. 

backgrounds : `lsst.afw.math.BackgroundList` 

List of backgrounds; modified. 

 

Returns 

------- 

bg : `lsst.afw.math.backgroundMI` 

Empirical background model. 

""" 

bg = self.background.fitBackground(maskedImage) 

if self.config.adjustBackground: 

self.log.warn("Fiddling the background by %g", self.config.adjustBackground) 

bg += self.config.adjustBackground 

self.log.info("Resubtracting the background after object detection") 

maskedImage -= bg.getImageF() 

backgrounds.append(bg) 

return bg 

 

def clearUnwantedResults(self, mask, results): 

"""Clear unwanted results from the Struct of results 

 

If we specifically want only positive or only negative detections, 

drop the ones we don't want, and its associated mask plane. 

 

Parameters 

---------- 

mask : `lsst.afw.image.Mask` 

Mask image. 

results : `lsst.pipe.base.Struct` 

Detection results, with ``positive`` and ``negative`` elements; 

modified. 

""" 

if self.config.thresholdPolarity == "positive": 

if self.config.reEstimateBackground: 

mask &= ~mask.getPlaneBitMask("DETECTED_NEGATIVE") 

results.negative = None 

elif self.config.thresholdPolarity == "negative": 

if self.config.reEstimateBackground: 

mask &= ~mask.getPlaneBitMask("DETECTED") 

results.positive = None 

 

@pipeBase.timeMethod 

def detectFootprints(self, exposure, doSmooth=True, sigma=None, clearMask=True, expId=None): 

"""Detect footprints on an exposure. 

 

Parameters 

---------- 

exposure : `lsst.afw.image.Exposure` 

Exposure to process; DETECTED{,_NEGATIVE} mask plane will be 

set in-place. 

doSmooth : `bool`, optional 

If True, smooth the image before detection using a Gaussian 

of width ``sigma``, or the measured PSF width of ``exposure``. 

Set to False when running on e.g. a pre-convolved image, or a mask 

plane. 

sigma : `float`, optional 

Gaussian Sigma of PSF (pixels); used for smoothing and to grow 

detections; if `None` then measure the sigma of the PSF of the 

``exposure``. 

clearMask : `bool`, optional 

Clear both DETECTED and DETECTED_NEGATIVE planes before running 

detection. 

expId : `dict`, optional 

Exposure identifier; unused by this implementation, but used for 

RNG seed by subclasses. 

 

Return Struct contents 

---------------------- 

positive : `lsst.afw.detection.FootprintSet` 

Positive polarity footprints (may be `None`) 

negative : `lsst.afw.detection.FootprintSet` 

Negative polarity footprints (may be `None`) 

numPos : `int` 

Number of footprints in positive or 0 if detection polarity was 

negative. 

numNeg : `int` 

Number of footprints in negative or 0 if detection polarity was 

positive. 

background : `lsst.afw.math.BackgroundList` 

Re-estimated background. `None` if 

``reEstimateBackground==False``. 

factor : `float` 

Multiplication factor applied to the configured detection 

threshold. 

""" 

maskedImage = exposure.maskedImage 

 

if clearMask: 

self.clearMask(maskedImage.getMask()) 

 

psf = self.getPsf(exposure, sigma=sigma) 

with self.tempWideBackgroundContext(exposure): 

convolveResults = self.convolveImage(maskedImage, psf, doSmooth=doSmooth) 

middle = convolveResults.middle 

sigma = convolveResults.sigma 

 

results = self.applyThreshold(middle, maskedImage.getBBox()) 

results.background = afwMath.BackgroundList() 

if self.config.doTempLocalBackground: 

self.applyTempLocalBackground(exposure, middle, results) 

self.finalizeFootprints(maskedImage.mask, results, sigma) 

 

if self.config.reEstimateBackground: 

self.reEstimateBackground(maskedImage, results.background) 

 

self.clearUnwantedResults(maskedImage.getMask(), results) 

self.display(exposure, results, middle) 

 

return results 

 

def makeThreshold(self, image, thresholdParity, factor=1.0): 

"""Make an afw.detection.Threshold object corresponding to the task's 

configuration and the statistics of the given image. 

 

Parameters 

---------- 

image : `afw.image.MaskedImage` 

Image to measure noise statistics from if needed. 

thresholdParity: `str` 

One of "positive" or "negative", to set the kind of fluctuations 

the Threshold will detect. 

factor : `float` 

Factor by which to multiply the configured detection threshold. 

This is useful for tweaking the detection threshold slightly. 

 

Returns 

------- 

threshold : `lsst.afw.detection.Threshold` 

Detection threshold. 

""" 

parity = False if thresholdParity == "negative" else True 

thresholdValue = self.config.thresholdValue 

thresholdType = self.config.thresholdType 

if self.config.thresholdType == 'stdev': 

bad = image.getMask().getPlaneBitMask(self.config.statsMask) 

sctrl = afwMath.StatisticsControl() 

sctrl.setAndMask(bad) 

stats = afwMath.makeStatistics(image, afwMath.STDEVCLIP, sctrl) 

thresholdValue *= stats.getValue(afwMath.STDEVCLIP) 

thresholdType = 'value' 

 

threshold = afwDet.createThreshold(thresholdValue*factor, thresholdType, parity) 

threshold.setIncludeMultiplier(self.config.includeThresholdMultiplier) 

return threshold 

 

def updatePeaks(self, fpSet, image, threshold): 

"""Update the Peaks in a FootprintSet by detecting new Footprints and 

Peaks in an image and using the new Peaks instead of the old ones. 

 

Parameters 

---------- 

fpSet : `afw.detection.FootprintSet` 

Set of Footprints whose Peaks should be updated. 

image : `afw.image.MaskedImage` 

Image to detect new Footprints and Peak in. 

threshold : `afw.detection.Threshold` 

Threshold object for detection. 

 

Input Footprints with fewer Peaks than self.config.nPeaksMaxSimple 

are not modified, and if no new Peaks are detected in an input 

Footprint, the brightest original Peak in that Footprint is kept. 

""" 

for footprint in fpSet.getFootprints(): 

oldPeaks = footprint.getPeaks() 

if len(oldPeaks) <= self.config.nPeaksMaxSimple: 

continue 

# We detect a new FootprintSet within each non-simple Footprint's 

# bbox to avoid a big O(N^2) comparison between the two sets of 

# Footprints. 

sub = image.Factory(image, footprint.getBBox()) 

fpSetForPeaks = afwDet.FootprintSet( 

sub, 

threshold, 

"", # don't set a mask plane 

self.config.minPixels 

) 

newPeaks = afwDet.PeakCatalog(oldPeaks.getTable()) 

for fpForPeaks in fpSetForPeaks.getFootprints(): 

for peak in fpForPeaks.getPeaks(): 

if footprint.contains(peak.getI()): 

newPeaks.append(peak) 

if len(newPeaks) > 0: 

del oldPeaks[:] 

oldPeaks.extend(newPeaks) 

else: 

del oldPeaks[1:] 

 

@staticmethod 

def setEdgeBits(maskedImage, goodBBox, edgeBitmask): 

"""Set the edgeBitmask bits for all of maskedImage outside goodBBox 

 

Parameters 

---------- 

maskedImage : `lsst.afw.image.MaskedImage` 

Image on which to set edge bits in the mask. 

goodBBox : `lsst.geom.Box2I` 

Bounding box of good pixels, in ``LOCAL`` coordinates. 

edgeBitmask : `lsst.afw.image.MaskPixel` 

Bit mask to OR with the existing mask bits in the region 

outside ``goodBBox``. 

""" 

msk = maskedImage.getMask() 

 

mx0, my0 = maskedImage.getXY0() 

for x0, y0, w, h in ([0, 0, 

msk.getWidth(), goodBBox.getBeginY() - my0], 

[0, goodBBox.getEndY() - my0, msk.getWidth(), 

maskedImage.getHeight() - (goodBBox.getEndY() - my0)], 

[0, 0, 

goodBBox.getBeginX() - mx0, msk.getHeight()], 

[goodBBox.getEndX() - mx0, 0, 

maskedImage.getWidth() - (goodBBox.getEndX() - mx0), msk.getHeight()], 

): 

edgeMask = msk.Factory(msk, lsst.geom.BoxI(lsst.geom.PointI(x0, y0), 

lsst.geom.ExtentI(w, h)), afwImage.LOCAL) 

edgeMask |= edgeBitmask 

 

@contextmanager 

def tempWideBackgroundContext(self, exposure): 

"""Context manager for removing wide (large-scale) background 

 

Removing a wide (large-scale) background helps to suppress the 

detection of large footprints that may overwhelm the deblender. 

It does, however, set a limit on the maximum scale of objects. 

 

The background that we remove will be restored upon exit from 

the context manager. 

 

Parameters 

---------- 

exposure : `lsst.afw.image.Exposure` 

Exposure on which to remove large-scale background. 

 

Returns 

------- 

context : context manager 

Context manager that will ensure the temporary wide background 

is restored. 

""" 

doTempWideBackground = self.config.doTempWideBackground 

if doTempWideBackground: 

self.log.info("Applying temporary wide background subtraction") 

original = exposure.maskedImage.image.array[:].copy() 

self.tempWideBackground.run(exposure).background 

# Remove NO_DATA regions (e.g., edge of the field-of-view); these can cause detections after 

# subtraction because of extrapolation of the background model into areas with no constraints. 

image = exposure.maskedImage.image 

mask = exposure.maskedImage.mask 

noData = mask.array & mask.getPlaneBitMask("NO_DATA") > 0 

isGood = mask.array & mask.getPlaneBitMask(self.config.statsMask) == 0 

image.array[noData] = np.median(image.array[~noData & isGood]) 

try: 

yield 

finally: 

if doTempWideBackground: 

exposure.maskedImage.image.array[:] = original 

 

 

def addExposures(exposureList): 

"""Add a set of exposures together. 

 

Parameters 

---------- 

exposureList : `list` of `lsst.afw.image.Exposure` 

Sequence of exposures to add. 

 

Returns 

------- 

addedExposure : `lsst.afw.image.Exposure` 

An exposure of the same size as each exposure in ``exposureList``, 

with the metadata from ``exposureList[0]`` and a masked image equal 

to the sum of all the exposure's masked images. 

""" 

exposure0 = exposureList[0] 

image0 = exposure0.getMaskedImage() 

 

addedImage = image0.Factory(image0, True) 

addedImage.setXY0(image0.getXY0()) 

 

for exposure in exposureList[1:]: 

image = exposure.getMaskedImage() 

addedImage += image 

 

addedExposure = exposure0.Factory(addedImage, exposure0.getWcs()) 

return addedExposure