Coverage for python/lsst/ip/diffim/dipoleFitTask.py: 70%

473 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-11 01:44 -0800

1# 

2# LSST Data Management System 

3# Copyright 2008-2016 AURA/LSST. 

4# 

5# This product includes software developed by the 

6# LSST Project (http://www.lsst.org/). 

7# 

8# This program is free software: you can redistribute it and/or modify 

9# it under the terms of the GNU General Public License as published by 

10# the Free Software Foundation, either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# This program is distributed in the hope that it will be useful, 

14# but WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the LSST License Statement and 

19# the GNU General Public License along with this program. If not, 

20# see <https://www.lsstcorp.org/LegalNotices/>. 

21# 

22 

23import math 

24import logging 

25import numpy as np 

26import warnings 

27 

28import lsst.afw.image as afwImage 

29import lsst.meas.base as measBase 

30import lsst.afw.detection as afwDet 

31import lsst.geom as geom 

32import lsst.pex.exceptions as pexExcept 

33import lsst.pex.config as pexConfig 

34from lsst.pipe.base import Struct 

35from lsst.utils.timer import timeMethod 

36 

37__all__ = ("DipoleFitTask", "DipoleFitPlugin", "DipoleFitTaskConfig", "DipoleFitPluginConfig", 

38 "DipoleFitAlgorithm") 

39 

40 

41# Create a new measurement task (`DipoleFitTask`) that can handle all other SFM tasks but can 

42# pass a separate pos- and neg- exposure/image to the `DipoleFitPlugin`s `run()` method. 

43 

44 

45class DipoleFitPluginConfig(measBase.SingleFramePluginConfig): 

46 """Configuration for DipoleFitPlugin 

47 """ 

48 

49 fitAllDiaSources = pexConfig.Field( 

50 dtype=bool, default=False, 

51 doc="""Attempte dipole fit of all diaSources (otherwise just the ones consisting of overlapping 

52 positive and negative footprints)""") 

53 

54 maxSeparation = pexConfig.Field( 

55 dtype=float, default=5., 

56 doc="Assume dipole is not separated by more than maxSeparation * psfSigma") 

57 

58 relWeight = pexConfig.Field( 

59 dtype=float, default=0.5, 

60 doc="""Relative weighting of pre-subtraction images (higher -> greater influence of pre-sub. 

61 images on fit)""") 

62 

63 tolerance = pexConfig.Field( 

64 dtype=float, default=1e-7, 

65 doc="Fit tolerance") 

66 

67 fitBackground = pexConfig.Field( 

68 dtype=int, default=1, 

69 doc="Set whether and how to fit for linear gradient in pre-sub. images. Possible values:" 

70 "0: do not fit background at all" 

71 "1 (default): pre-fit the background using linear least squares and then do not fit it as part" 

72 "of the dipole fitting optimization" 

73 "2: pre-fit the background using linear least squares (as in 1), and use the parameter" 

74 "estimates from that fit as starting parameters for an integrated re-fit of the background") 

75 

76 fitSeparateNegParams = pexConfig.Field( 

77 dtype=bool, default=False, 

78 doc="Include parameters to fit for negative values (flux, gradient) separately from pos.") 

79 

80 # Config params for classification of detected diaSources as dipole or not 

81 minSn = pexConfig.Field( 

82 dtype=float, default=math.sqrt(2) * 5.0, 

83 doc="Minimum quadrature sum of positive+negative lobe S/N to be considered a dipole") 

84 

85 maxFluxRatio = pexConfig.Field( 

86 dtype=float, default=0.65, 

87 doc="Maximum flux ratio in either lobe to be considered a dipole") 

88 

89 maxChi2DoF = pexConfig.Field( 

90 dtype=float, default=0.05, 

91 doc="""Maximum Chi2/DoF significance of fit to be considered a dipole. 

92 Default value means \"Choose a chi2DoF corresponding to a significance level of at most 0.05\" 

93 (note this is actually a significance, not a chi2 value).""") 

94 

95 maxFootprintArea = pexConfig.Field( 

96 dtype=int, default=1_200, 

97 doc=("Maximum area for footprints before they are ignored as large; " 

98 "non-positive means no threshold applied" 

99 "Threshold chosen for HSC and DECam data, see DM-38741 for details.")) 

100 

101 

102class DipoleFitTaskConfig(measBase.SingleFrameMeasurementConfig): 

103 

104 def setDefaults(self): 

105 measBase.SingleFrameMeasurementConfig.setDefaults(self) 

106 

107 self.plugins.names = ["base_SdssCentroid", 

108 "ip_diffim_DipoleFit", 

109 "base_CircularApertureFlux", 

110 "base_PixelFlags", 

111 "base_SkyCoord", 

112 "base_PsfFlux", 

113 "base_SdssShape", 

114 ] 

115 # Only measure the apertures we need to report in the alert stream. 

116 self.plugins["base_CircularApertureFlux"].radii = [12.0] 

117 

118 self.slots.calibFlux = None 

119 self.slots.modelFlux = None 

120 self.slots.gaussianFlux = None 

121 self.slots.shape = "base_SdssShape" 

122 # This will be switched to "ip_diffim_DipoleFit" as this task runs. 

123 self.slots.centroid = "base_SdssCentroid" 

124 self.doReplaceWithNoise = False 

125 

126 

127class DipoleFitTask(measBase.SingleFrameMeasurementTask): 

128 """A task that fits a dipole to a difference image, with an optional 

129 separate detection image. 

130 

131 Because it subclasses SingleFrameMeasurementTask, and calls 

132 SingleFrameMeasurementTask.run() from its run() method, it still 

133 can be used identically to a standard SingleFrameMeasurementTask. 

134 """ 

135 

136 ConfigClass = DipoleFitTaskConfig 

137 _DefaultName = "dipoleFit" 

138 

139 def __init__(self, schema, algMetadata=None, **kwargs): 

140 super().__init__(schema, algMetadata, **kwargs) 

141 

142 # Enforce a specific plugin order, so that DipoleFit can fall back on 

143 # SdssCentroid for non-dipoles 

144 self.plugins_pre = self.plugins.copy() 

145 self.plugins_post = self.plugins.copy() 

146 self.plugins_pre.clear() 

147 self.plugins_pre["base_SdssCentroid"] = self.plugins["base_SdssCentroid"] 

148 self.plugins_post.pop("base_SdssCentroid") 

149 self.dipoleFit = self.plugins_post.pop("ip_diffim_DipoleFit") 

150 del self.plugins 

151 

152 @timeMethod 

153 def run(self, sources, exposure, posExp=None, negExp=None, **kwargs): 

154 """Run dipole measurement and classification. 

155 

156 Run SdssCentroid first, then switch the centroid slot, then DipoleFit 

157 then the rest; DipoleFit will fall back on SdssCentroid for sources 

158 not containing positive+negative peaks. 

159 

160 Parameters 

161 ---------- 

162 sources : `lsst.afw.table.SourceCatalog` 

163 ``diaSources`` that will be measured using dipole measurement. 

164 exposure : `lsst.afw.image.Exposure` 

165 The difference exposure on which the ``sources`` were detected. 

166 If neither ``posExp`` nor ``negExp`` are set, then the dipole is also 

167 fitted directly to this difference image. 

168 posExp : `lsst.afw.image.Exposure`, optional 

169 "Positive" exposure, typically a science exposure, or None if unavailable 

170 When `posExp` is `None`, will compute `posImage = exposure + negExp`. 

171 negExp : `lsst.afw.image.Exposure`, optional 

172 "Negative" exposure, typically a template exposure, or None if unavailable 

173 When `negExp` is `None`, will compute `negImage = posExp - exposure`. 

174 **kwargs 

175 Additional keyword arguments for `lsst.meas.base.sfm.SingleFrameMeasurementTask`. 

176 """ 

177 # Run plugins in a very specific order, so DipoleFitPlugin has a 

178 # centroid to fall back on. 

179 self.plugins = self.plugins_pre 

180 super().run(sources, exposure, **kwargs) 

181 

182 for source in sources: 

183 self.dipoleFit.measureDipoles(source, exposure, posExp, negExp) 

184 # Use the new DipoleFit outputs for subsequent measurements, now that 

185 # non-dipoles have been filled in with the earlier centroid values. 

186 sources.schema.getAliasMap().set("slot_Centroid", "ip_diffim_DipoleFit") 

187 

188 self.plugins = self.plugins_post 

189 super().run(sources, exposure, **kwargs) 

190 

191 

192class DipoleModel: 

193 """Lightweight class containing methods for generating a dipole model for fitting 

194 to sources in diffims, used by DipoleFitAlgorithm. 

195 

196 See also: 

197 `DMTN-007: Dipole characterization for image differencing <https://dmtn-007.lsst.io>`_. 

198 """ 

199 

200 def __init__(self): 

201 import lsstDebug 

202 self.debug = lsstDebug.Info(__name__).debug 

203 self.log = logging.getLogger(__name__) 

204 

205 def makeBackgroundModel(self, in_x, pars=None): 

206 """Generate gradient model (2-d array) with up to 2nd-order polynomial 

207 

208 Parameters 

209 ---------- 

210 in_x : `numpy.array` 

211 (2, w, h)-dimensional `numpy.array`, containing the 

212 input x,y meshgrid providing the coordinates upon which to 

213 compute the gradient. This will typically be generated via 

214 `_generateXYGrid()`. `w` and `h` correspond to the width and 

215 height of the desired grid. 

216 pars : `list` of `float`, optional 

217 Up to 6 floats for up 

218 to 6 2nd-order 2-d polynomial gradient parameters, in the 

219 following order: (intercept, x, y, xy, x**2, y**2). If `pars` 

220 is emtpy or `None`, do nothing and return `None` (for speed). 

221 

222 Returns 

223 ------- 

224 result : `None` or `numpy.array` 

225 return None, or 2-d numpy.array of width/height matching 

226 input bbox, containing computed gradient values. 

227 """ 

228 

229 # Don't fit for other gradient parameters if the intercept is not included. 

230 if (pars is None) or (len(pars) <= 0) or (pars[0] is None): 230 ↛ 231line 230 didn't jump to line 231 because the condition on line 230 was never true

231 return 

232 

233 y, x = in_x[0, :], in_x[1, :] 

234 gradient = np.full_like(x, pars[0], dtype='float64') 

235 if len(pars) > 1 and pars[1] is not None: 235 ↛ 237line 235 didn't jump to line 237 because the condition on line 235 was always true

236 gradient += pars[1] * x 

237 if len(pars) > 2 and pars[2] is not None: 237 ↛ 239line 237 didn't jump to line 239 because the condition on line 237 was always true

238 gradient += pars[2] * y 

239 if len(pars) > 3 and pars[3] is not None: 239 ↛ 240line 239 didn't jump to line 240 because the condition on line 239 was never true

240 gradient += pars[3] * (x * y) 

241 if len(pars) > 4 and pars[4] is not None: 241 ↛ 242line 241 didn't jump to line 242 because the condition on line 241 was never true

242 gradient += pars[4] * (x * x) 

243 if len(pars) > 5 and pars[5] is not None: 243 ↛ 244line 243 didn't jump to line 244 because the condition on line 243 was never true

244 gradient += pars[5] * (y * y) 

245 

246 return gradient 

247 

248 def _generateXYGrid(self, bbox): 

249 """Generate a meshgrid covering the x,y coordinates bounded by bbox 

250 

251 Parameters 

252 ---------- 

253 bbox : `lsst.geom.Box2I` 

254 input Bounding Box defining the coordinate limits 

255 

256 Returns 

257 ------- 

258 in_x : `numpy.array` 

259 (2, w, h)-dimensional numpy array containing the grid indexing over x- and 

260 y- coordinates 

261 """ 

262 

263 x, y = np.mgrid[bbox.getBeginY():bbox.getEndY(), bbox.getBeginX():bbox.getEndX()] 

264 in_x = np.array([y, x]).astype(np.float64) 

265 in_x[0, :] -= np.mean(in_x[0, :]) 

266 in_x[1, :] -= np.mean(in_x[1, :]) 

267 return in_x 

268 

269 def _getHeavyFootprintSubimage(self, fp, badfill=np.nan, grow=0): 

270 """Extract the image from a ``~lsst.afw.detection.HeavyFootprint`` 

271 as an `lsst.afw.image.ImageF`. 

272 

273 Parameters 

274 ---------- 

275 fp : `lsst.afw.detection.HeavyFootprint` 

276 HeavyFootprint to use to generate the subimage 

277 badfill : `float`, optional 

278 Value to fill in pixels in extracted image that are outside the footprint 

279 grow : `int` 

280 Optionally grow the footprint by this amount before extraction 

281 

282 Returns 

283 ------- 

284 subim2 : `lsst.afw.image.ImageF` 

285 An `~lsst.afw.image.ImageF` containing the subimage. 

286 """ 

287 bbox = fp.getBBox() 

288 if grow > 0: 288 ↛ 291line 288 didn't jump to line 291 because the condition on line 288 was always true

289 bbox.grow(grow) 

290 

291 subim2 = afwImage.ImageF(bbox, badfill) 

292 fp.getSpans().unflatten(subim2.array, fp.getImageArray(), bbox.getCorners()[0]) 

293 return subim2 

294 

295 def fitFootprintBackground(self, source, posImage, order=1): 

296 """Fit a linear (polynomial) model of given order (max 2) to the background of a footprint. 

297 

298 Only fit the pixels OUTSIDE of the footprint, but within its bounding box. 

299 

300 Parameters 

301 ---------- 

302 source : `lsst.afw.table.SourceRecord` 

303 SourceRecord, the footprint of which is to be fit 

304 posImage : `lsst.afw.image.Exposure` 

305 The exposure from which to extract the footprint subimage 

306 order : `int` 

307 Polynomial order of background gradient to fit. 

308 

309 Returns 

310 ------- 

311 pars : `tuple` of `float` 

312 `tuple` of length (1 if order==0; 3 if order==1; 6 if order == 2), 

313 containing the resulting fit parameters 

314 """ 

315 

316 # TODO look into whether to use afwMath background methods -- see 

317 # http://lsst-web.ncsa.illinois.edu/doxygen/x_masterDoxyDoc/_background_example.html 

318 fp = source.getFootprint() 

319 bbox = fp.getBBox() 

320 bbox.grow(3) 

321 posImg = afwImage.ImageF(posImage.image, bbox, afwImage.PARENT) 

322 

323 # This code constructs the footprint image so that we can identify the pixels that are 

324 # outside the footprint (but within the bounding box). These are the pixels used for 

325 # fitting the background. 

326 posHfp = afwDet.HeavyFootprintF(fp, posImage.getMaskedImage()) 

327 posFpImg = self._getHeavyFootprintSubimage(posHfp, grow=3) 

328 

329 isBg = np.isnan(posFpImg.array).ravel() 

330 

331 data = posImg.array.ravel() 

332 data = data[isBg] 

333 B = data 

334 

335 x, y = np.mgrid[bbox.getBeginY():bbox.getEndY(), bbox.getBeginX():bbox.getEndX()] 

336 x = x.astype(np.float64).ravel() 

337 x -= np.mean(x) 

338 x = x[isBg] 

339 y = y.astype(np.float64).ravel() 

340 y -= np.mean(y) 

341 y = y[isBg] 

342 b = np.ones_like(x, dtype=np.float64) 

343 

344 M = np.vstack([b]).T # order = 0 

345 if order == 1: 345 ↛ 347line 345 didn't jump to line 347 because the condition on line 345 was always true

346 M = np.vstack([b, x, y]).T 

347 elif order == 2: 

348 M = np.vstack([b, x, y, x**2., y**2., x*y]).T 

349 

350 pars = np.linalg.lstsq(M, B, rcond=-1)[0] 

351 return pars 

352 

353 def makeStarModel(self, bbox, psf, xcen, ycen, flux): 

354 """Generate a 2D image model of a single PDF centered at the given coordinates. 

355 

356 Parameters 

357 ---------- 

358 bbox : `lsst.geom.Box` 

359 Bounding box marking pixel coordinates for generated model 

360 psf : TODO: DM-17458 

361 Psf model used to generate the 'star' 

362 xcen : `float` 

363 Desired x-centroid of the 'star' 

364 ycen : `float` 

365 Desired y-centroid of the 'star' 

366 flux : `float` 

367 Desired flux of the 'star' 

368 

369 Returns 

370 ------- 

371 p_Im : `lsst.afw.image.Image` 

372 2-d stellar image of width/height matching input ``bbox``, 

373 containing PSF with given centroid and flux 

374 """ 

375 

376 # Generate the psf image, normalize to flux 

377 psf_img = psf.computeImage(geom.Point2D(xcen, ycen)).convertF() 

378 psf_img_sum = np.nansum(psf_img.array) 

379 psf_img *= (flux/psf_img_sum) 

380 

381 # Clip the PSF image bounding box to fall within the footprint bounding box 

382 psf_box = psf_img.getBBox() 

383 psf_box.clip(bbox) 

384 psf_img = afwImage.ImageF(psf_img, psf_box, afwImage.PARENT) 

385 

386 # Then actually crop the psf image. 

387 # Usually not necessary, but if the dipole is near the edge of the image... 

388 # Would be nice if we could compare original pos_box with clipped pos_box and 

389 # see if it actually was clipped. 

390 p_Im = afwImage.ImageF(bbox) 

391 tmpSubim = afwImage.ImageF(p_Im, psf_box, afwImage.PARENT) 

392 tmpSubim += psf_img 

393 

394 return p_Im 

395 

396 def makeModel(self, x, flux, xcenPos, ycenPos, xcenNeg, ycenNeg, fluxNeg=None, 

397 b=None, x1=None, y1=None, xy=None, x2=None, y2=None, 

398 bNeg=None, x1Neg=None, y1Neg=None, xyNeg=None, x2Neg=None, y2Neg=None, 

399 **kwargs): 

400 """Generate dipole model with given parameters. 

401 

402 This is the function whose sum-of-squared difference from data 

403 is minimized by `lmfit`. 

404 

405 x : TODO: DM-17458 

406 Input independent variable. Used here as the grid on 

407 which to compute the background gradient model. 

408 flux : `float` 

409 Desired flux of the positive lobe of the dipole 

410 xcenPos, ycenPos : `float` 

411 Desired x,y-centroid of the positive lobe of the dipole 

412 xcenNeg, ycenNeg : `float` 

413 Desired x,y-centroid of the negative lobe of the dipole 

414 fluxNeg : `float`, optional 

415 Desired flux of the negative lobe of the dipole, set to 'flux' if None 

416 b, x1, y1, xy, x2, y2 : `float` 

417 Gradient parameters for positive lobe. 

418 bNeg, x1Neg, y1Neg, xyNeg, x2Neg, y2Neg : `float`, optional 

419 Gradient parameters for negative lobe. 

420 They are set to the corresponding positive values if None. 

421 

422 **kwargs : `dict` [`str`] 

423 Keyword arguments passed through ``lmfit`` and 

424 used by this function. These must include: 

425 

426 - ``psf`` Psf model used to generate the 'star' 

427 - ``rel_weight`` Used to signify least-squares weighting of posImage/negImage 

428 relative to diffim. If ``rel_weight == 0`` then posImage/negImage are ignored. 

429 - ``bbox`` Bounding box containing region to be modelled 

430 

431 Returns 

432 ------- 

433 zout : `numpy.array` 

434 Has width and height matching the input bbox, and 

435 contains the dipole model with given centroids and flux(es). If 

436 ``rel_weight`` = 0, this is a 2-d array with dimensions matching 

437 those of bbox; otherwise a stack of three such arrays, 

438 representing the dipole (diffim), positive, and negative images 

439 respectively. 

440 """ 

441 

442 psf = kwargs.get('psf') 

443 rel_weight = kwargs.get('rel_weight') # if > 0, we're including pre-sub. images 

444 fp = kwargs.get('footprint') 

445 bbox = fp.getBBox() 

446 

447 if fluxNeg is None: 447 ↛ 450line 447 didn't jump to line 450 because the condition on line 447 was always true

448 fluxNeg = flux 

449 

450 self.log.debug('flux: %.2f fluxNeg: %.2f x+: %.2f x-: %.2f y+: %.2f y-: %.2f ', 

451 flux, fluxNeg, xcenPos, xcenNeg, ycenPos, ycenNeg) 

452 if x1 is not None: 452 ↛ 453line 452 didn't jump to line 453 because the condition on line 452 was never true

453 self.log.debug(' b: %.2f x1: %.2f y1: %.2f', b, x1, y1) 

454 if xy is not None: 454 ↛ 455line 454 didn't jump to line 455 because the condition on line 454 was never true

455 self.log.debug(' xy: %.2f x2: %.2f y2: %.2f', xy, x2, y2) 

456 

457 posIm = self.makeStarModel(bbox, psf, xcenPos, ycenPos, flux) 

458 negIm = self.makeStarModel(bbox, psf, xcenNeg, ycenNeg, fluxNeg) 

459 

460 in_x = x 

461 if in_x is None: # use the footprint to generate the input grid 461 ↛ 462line 461 didn't jump to line 462 because the condition on line 461 was never true

462 y, x = np.mgrid[bbox.getBeginY():bbox.getEndY(), bbox.getBeginX():bbox.getEndX()] 

463 in_x = np.array([x, y]) * 1. 

464 in_x[0, :] -= in_x[0, :].mean() # center it! 

465 in_x[1, :] -= in_x[1, :].mean() 

466 

467 if b is not None: 467 ↛ 468line 467 didn't jump to line 468 because the condition on line 467 was never true

468 gradient = self.makeBackgroundModel(in_x, (b, x1, y1, xy, x2, y2)) 

469 

470 # If bNeg is None, then don't fit the negative background separately 

471 if bNeg is not None: 

472 gradientNeg = self.makeBackgroundModel(in_x, (bNeg, x1Neg, y1Neg, xyNeg, x2Neg, y2Neg)) 

473 else: 

474 gradientNeg = gradient 

475 

476 posIm.array[:, :] += gradient 

477 negIm.array[:, :] += gradientNeg 

478 

479 # Generate the diffIm model 

480 diffIm = afwImage.ImageF(bbox) 

481 diffIm += posIm 

482 diffIm -= negIm 

483 

484 zout = diffIm.array 

485 if rel_weight > 0.: 

486 zout = np.append([zout], [posIm.array, negIm.array], axis=0) 

487 

488 return zout 

489 

490 

491class DipoleFitAlgorithm: 

492 """Fit a dipole model using an image difference. 

493 

494 See also: 

495 `DMTN-007: Dipole characterization for image differencing <https://dmtn-007.lsst.io>`_. 

496 """ 

497 

498 # This is just a private version number to sync with the ipython notebooks that I have been 

499 # using for algorithm development. 

500 _private_version_ = '0.0.5' 

501 

502 # Below is a (somewhat incomplete) list of improvements 

503 # that would be worth investigating, given the time: 

504 

505 # todo 1. evaluate necessity for separate parameters for pos- and neg- images 

506 # todo 2. only fit background OUTSIDE footprint (DONE) and dipole params INSIDE footprint (NOT DONE)? 

507 # todo 3. correct normalization of least-squares weights based on variance planes 

508 # todo 4. account for PSFs that vary across the exposures (should be happening by default?) 

509 # todo 5. correctly account for NA/masks (i.e., ignore!) 

510 # todo 6. better exception handling in the plugin 

511 # todo 7. better classification of dipoles (e.g. by comparing chi2 fit vs. monopole?) 

512 # todo 8. (DONE) Initial fast estimate of background gradient(s) params -- perhaps using numpy.lstsq 

513 # todo 9. (NOT NEEDED - see (2)) Initial fast test whether a background gradient needs to be fit 

514 # todo 10. (DONE) better initial estimate for flux when there's a strong gradient 

515 # todo 11. (DONE) requires a new package `lmfit` -- investiate others? (astropy/scipy/iminuit?) 

516 

517 def __init__(self, diffim, posImage=None, negImage=None): 

518 """Algorithm to run dipole measurement on a diaSource 

519 

520 Parameters 

521 ---------- 

522 diffim : `lsst.afw.image.Exposure` 

523 Exposure on which the diaSources were detected 

524 posImage : `lsst.afw.image.Exposure` 

525 "Positive" exposure from which the template was subtracted 

526 negImage : `lsst.afw.image.Exposure` 

527 "Negative" exposure which was subtracted from the posImage 

528 """ 

529 

530 self.diffim = diffim 

531 self.posImage = posImage 

532 self.negImage = negImage 

533 self.psfSigma = None 

534 if diffim is not None: 534 ↛ 539line 534 didn't jump to line 539 because the condition on line 534 was always true

535 diffimPsf = diffim.getPsf() 

536 diffimAvgPos = diffimPsf.getAveragePosition() 

537 self.psfSigma = diffimPsf.computeShape(diffimAvgPos).getDeterminantRadius() 

538 

539 self.log = logging.getLogger(__name__) 

540 

541 import lsstDebug 

542 self.debug = lsstDebug.Info(__name__).debug 

543 

544 def fitDipoleImpl(self, source, tol=1e-7, rel_weight=0.5, 

545 fitBackground=1, bgGradientOrder=1, maxSepInSigma=5., 

546 separateNegParams=True, verbose=False): 

547 """Fit a dipole model to an input difference image. 

548 

549 Actually, fits the subimage bounded by the input source's 

550 footprint) and optionally constrain the fit using the 

551 pre-subtraction images posImage and negImage. 

552 

553 Parameters 

554 ---------- 

555 source : TODO: DM-17458 

556 TODO: DM-17458 

557 tol : float, optional 

558 TODO: DM-17458 

559 rel_weight : `float`, optional 

560 TODO: DM-17458 

561 fitBackground : `int`, optional 

562 TODO: DM-17458 

563 bgGradientOrder : `int`, optional 

564 TODO: DM-17458 

565 maxSepInSigma : `float`, optional 

566 TODO: DM-17458 

567 separateNegParams : `bool`, optional 

568 TODO: DM-17458 

569 verbose : `bool`, optional 

570 TODO: DM-17458 

571 

572 Returns 

573 ------- 

574 result : `lmfit.MinimizerResult` 

575 return `lmfit.MinimizerResult` object containing the fit 

576 parameters and other information. 

577 """ 

578 

579 # Only import lmfit if someone wants to use the new DipoleFitAlgorithm. 

580 import lmfit 

581 

582 fp = source.getFootprint() 

583 bbox = fp.getBBox() 

584 subim = afwImage.MaskedImageF(self.diffim.getMaskedImage(), bbox=bbox, origin=afwImage.PARENT) 

585 

586 z = diArr = subim.image.array 

587 # Make sure we don't overwrite buffers. 

588 z = z.copy() 

589 weights = 1. / subim.variance.array # get the weights (=1/variance) 

590 

591 if rel_weight > 0. and ((self.posImage is not None) or (self.negImage is not None)): 

592 if self.negImage is not None: 

593 negSubim = afwImage.MaskedImageF(self.negImage.getMaskedImage(), bbox, origin=afwImage.PARENT) 

594 if self.posImage is not None: 

595 posSubim = afwImage.MaskedImageF(self.posImage.getMaskedImage(), bbox, origin=afwImage.PARENT) 

596 if self.posImage is None: # no science image provided; generate it from diffim + negImage 

597 posSubim = subim.clone() 

598 posSubim += negSubim 

599 if self.negImage is None: # no template provided; generate it from the posImage - diffim 

600 negSubim = posSubim.clone() 

601 negSubim -= subim 

602 

603 z = np.append([z], [posSubim.image.array, 

604 negSubim.image.array], axis=0) 

605 # Weight the pos/neg images by rel_weight relative to the diffim 

606 weights = np.append([weights], [1. / posSubim.variance.array * rel_weight, 

607 1. / negSubim.variance.array * rel_weight], axis=0) 

608 else: 

609 rel_weight = 0. # a short-cut for "don't include the pre-subtraction data" 

610 

611 # It seems that `lmfit` requires a static functor as its optimized method, which eliminates 

612 # the ability to pass a bound method or other class method. Here we write a wrapper which 

613 # makes this possible. 

614 def dipoleModelFunctor(x, flux, xcenPos, ycenPos, xcenNeg, ycenNeg, fluxNeg=None, 

615 b=None, x1=None, y1=None, xy=None, x2=None, y2=None, 

616 bNeg=None, x1Neg=None, y1Neg=None, xyNeg=None, x2Neg=None, y2Neg=None, 

617 **kwargs): 

618 """Generate dipole model with given parameters. 

619 

620 It simply defers to `modelObj.makeModel()`, where `modelObj` comes 

621 out of `kwargs['modelObj']`. 

622 """ 

623 modelObj = kwargs.pop('modelObj') 

624 return modelObj.makeModel(x, flux, xcenPos, ycenPos, xcenNeg, ycenNeg, fluxNeg=fluxNeg, 

625 b=b, x1=x1, y1=y1, xy=xy, x2=x2, y2=y2, 

626 bNeg=bNeg, x1Neg=x1Neg, y1Neg=y1Neg, xyNeg=xyNeg, 

627 x2Neg=x2Neg, y2Neg=y2Neg, **kwargs) 

628 

629 dipoleModel = DipoleModel() 

630 

631 modelFunctor = dipoleModelFunctor # dipoleModel.makeModel does not work for now. 

632 # Create the lmfit model (lmfit uses scipy 'leastsq' option by default - Levenberg-Marquardt) 

633 # We have to (later) filter out the nans by hand in our input to gmod.fit(). 

634 # The only independent variable in the model is "x"; lmfit tries to 

635 # introspect variables and parameters from the function signature, but 

636 # gets it wrong for the model signature above. 

637 gmod = lmfit.Model(modelFunctor, independent_vars=["x"], verbose=verbose) 

638 

639 # Add the constraints for centroids, fluxes. 

640 # starting constraint - near centroid of footprint 

641 fpCentroid = np.array([fp.getCentroid().getX(), fp.getCentroid().getY()]) 

642 cenNeg = cenPos = fpCentroid 

643 

644 pks = fp.getPeaks() 

645 

646 # If individual (merged) peaks were detected, start the lobes at the 

647 # highest and the lowest peak. The peaks may be in any order. 

648 if len(pks) >= 1: 648 ↛ 650line 648 didn't jump to line 650 because the condition on line 648 was always true

649 cenPos = max(pks, key=lambda peak: peak.getPeakValue()).getF() 

650 if len(pks) >= 2: 650 ↛ 655line 650 didn't jump to line 655 because the condition on line 650 was always true

651 cenNeg = min(pks, key=lambda peak: peak.getPeakValue()).getF() 

652 

653 # For close/faint dipoles the starting locs (min/max) might be way off, let's help them a bit. 

654 # First assume dipole is not separated by more than 5*psfSigma. 

655 maxSep = self.psfSigma * maxSepInSigma 

656 

657 # As an initial guess -- assume the dipole is close to the center of the footprint. 

658 if np.sum(np.sqrt((np.array(cenPos) - fpCentroid)**2.)) > maxSep: 

659 cenPos = fpCentroid 

660 if np.sum(np.sqrt((np.array(cenNeg) - fpCentroid)**2.)) > maxSep: 

661 cenNeg = fpCentroid 

662 

663 # parameter hints/constraints: https://lmfit.github.io/lmfit-py/model.html#model-param-hints-section 

664 # might make sense to not use bounds -- see http://lmfit.github.io/lmfit-py/bounds.html 

665 # also see this discussion -- https://github.com/scipy/scipy/issues/3129 

666 gmod.set_param_hint('xcenPos', value=cenPos[0], 

667 min=cenPos[0]-maxSep, max=cenPos[0]+maxSep) 

668 gmod.set_param_hint('ycenPos', value=cenPos[1], 

669 min=cenPos[1]-maxSep, max=cenPos[1]+maxSep) 

670 gmod.set_param_hint('xcenNeg', value=cenNeg[0], 

671 min=cenNeg[0]-maxSep, max=cenNeg[0]+maxSep) 

672 gmod.set_param_hint('ycenNeg', value=cenNeg[1], 

673 min=cenNeg[1]-maxSep, max=cenNeg[1]+maxSep) 

674 

675 # Use the (flux under the dipole)*5 for an estimate. 

676 # Lots of testing showed that having startingFlux be too high was better than too low. 

677 startingFlux = np.nansum(np.abs(diArr) - np.nanmedian(np.abs(diArr))) * 5. 

678 posFlux = negFlux = startingFlux 

679 

680 # TBD: set max. flux limit? 

681 gmod.set_param_hint('flux', value=posFlux, min=0.1) 

682 

683 if separateNegParams: 683 ↛ 685line 683 didn't jump to line 685 because the condition on line 683 was never true

684 # TBD: set max negative lobe flux limit? 

685 gmod.set_param_hint('fluxNeg', value=np.abs(negFlux), min=0.1) 

686 

687 # Fixed parameters (don't fit for them if there are no pre-sub images or no gradient fit requested): 

688 # Right now (fitBackground == 1), we fit a linear model to the background and then subtract 

689 # it from the data and then don't fit the background again (this is faster). 

690 # A slower alternative (fitBackground == 2) is to use the estimated background parameters as 

691 # starting points in the integrated model fit. That is currently not performed by default, 

692 # but might be desirable in some cases. 

693 bgParsPos = bgParsNeg = (0., 0., 0.) 

694 if ((rel_weight > 0.) and (fitBackground != 0) and (bgGradientOrder >= 0)): 

695 pbg = 0. 

696 bgFitImage = self.posImage if self.posImage is not None else self.negImage 

697 # Fit the gradient to the background (linear model) 

698 bgParsPos = bgParsNeg = dipoleModel.fitFootprintBackground(source, bgFitImage, 

699 order=bgGradientOrder) 

700 

701 # Generate the gradient and subtract it from the pre-subtraction image data 

702 if fitBackground == 1: 702 ↛ 721line 702 didn't jump to line 721 because the condition on line 702 was always true

703 in_x = dipoleModel._generateXYGrid(bbox) 

704 pbg = dipoleModel.makeBackgroundModel(in_x, tuple(bgParsPos)) 

705 z[1, :] -= pbg 

706 z[1, :] -= np.nanmedian(z[1, :]) 

707 posFlux = np.nansum(z[1, :]) 

708 gmod.set_param_hint('flux', value=posFlux*1.5, min=0.1) 

709 

710 if separateNegParams and self.negImage is not None: 710 ↛ 711line 710 didn't jump to line 711 because the condition on line 710 was never true

711 bgParsNeg = dipoleModel.fitFootprintBackground(source, self.negImage, 

712 order=bgGradientOrder) 

713 pbg = dipoleModel.makeBackgroundModel(in_x, tuple(bgParsNeg)) 

714 z[2, :] -= pbg 

715 z[2, :] -= np.nanmedian(z[2, :]) 

716 if separateNegParams: 716 ↛ 717line 716 didn't jump to line 717 because the condition on line 716 was never true

717 negFlux = np.nansum(z[2, :]) 

718 gmod.set_param_hint('fluxNeg', value=negFlux*1.5, min=0.1) 

719 

720 # Do not subtract the background from the images but include the background parameters in the fit 

721 if fitBackground == 2: 721 ↛ 722line 721 didn't jump to line 722 because the condition on line 721 was never true

722 if bgGradientOrder >= 0: 

723 gmod.set_param_hint('b', value=bgParsPos[0]) 

724 if separateNegParams: 

725 gmod.set_param_hint('bNeg', value=bgParsNeg[0]) 

726 if bgGradientOrder >= 1: 

727 gmod.set_param_hint('x1', value=bgParsPos[1]) 

728 gmod.set_param_hint('y1', value=bgParsPos[2]) 

729 if separateNegParams: 

730 gmod.set_param_hint('x1Neg', value=bgParsNeg[1]) 

731 gmod.set_param_hint('y1Neg', value=bgParsNeg[2]) 

732 if bgGradientOrder >= 2: 

733 gmod.set_param_hint('xy', value=bgParsPos[3]) 

734 gmod.set_param_hint('x2', value=bgParsPos[4]) 

735 gmod.set_param_hint('y2', value=bgParsPos[5]) 

736 if separateNegParams: 

737 gmod.set_param_hint('xyNeg', value=bgParsNeg[3]) 

738 gmod.set_param_hint('x2Neg', value=bgParsNeg[4]) 

739 gmod.set_param_hint('y2Neg', value=bgParsNeg[5]) 

740 

741 y, x = np.mgrid[bbox.getBeginY():bbox.getEndY(), bbox.getBeginX():bbox.getEndX()] 

742 in_x = np.array([x, y]).astype(np.float64) 

743 in_x[0, :] -= in_x[0, :].mean() # center it! 

744 in_x[1, :] -= in_x[1, :].mean() 

745 

746 # Instead of explicitly using a mask to ignore flagged pixels, just set the ignored pixels' 

747 # weights to 0 in the fit. TBD: need to inspect mask planes to set this mask. 

748 mask = np.ones_like(z, dtype=bool) # TBD: set mask values to False if the pixels are to be ignored 

749 

750 # I'm not sure about the variance planes in the diffim (or convolved pre-sub. images 

751 # for that matter) so for now, let's just do an un-weighted least-squares fit 

752 # (override weights computed above). 

753 weights = mask.astype(np.float64) 

754 if self.posImage is not None and rel_weight > 0.: 

755 weights = np.array([np.ones_like(diArr), np.ones_like(diArr)*rel_weight, 

756 np.ones_like(diArr)*rel_weight]) 

757 

758 # Set the weights to zero if mask is False 

759 if np.any(~mask): 759 ↛ 760line 759 didn't jump to line 760 because the condition on line 759 was never true

760 weights[~mask] = 0. 

761 

762 # Filter out any nans, and make the weights 0. 

763 nans = (np.isnan(z) | np.isnan(weights)) 

764 nNans = nans.sum() 

765 if nNans > 0: 765 ↛ 766line 765 didn't jump to line 766 because the condition on line 765 was never true

766 if nNans < len(z): 

767 z[nans] = np.nanmedian(z) 

768 else: 

769 z[nans] = 0 

770 weights[nans] = 0 

771 

772 # Note that although we can, we're not required to set initial values for params here, 

773 # since we set their param_hint's above. 

774 # Can add "method" param to not use 'leastsq' (==levenberg-marquardt), e.g. "method='nelder'" 

775 with warnings.catch_warnings(): 

776 # Ignore lmfit unknown argument warnings: 

777 # "psf, rel_weight, footprint, modelObj" all become pass-through kwargs for makeModel. 

778 warnings.filterwarnings("ignore", "The keyword argument .* does not match", UserWarning) 

779 result = gmod.fit(z, weights=weights, x=in_x, max_nfev=250, 

780 method="leastsq", # TODO: try using `least_squares` here for speed/robustness 

781 verbose=verbose, 

782 # see scipy docs for the meaning of these keywords 

783 fit_kws={'ftol': tol, 'xtol': tol, 'gtol': tol, 

784 # Our model is float32 internally, so we need a larger epsfcn. 

785 'epsfcn': 1e-8}, 

786 psf=self.diffim.getPsf(), # hereon: kwargs that get passed to makeModel() 

787 rel_weight=rel_weight, 

788 footprint=fp, 

789 modelObj=dipoleModel) 

790 

791 if verbose: # the ci_report() seems to fail if neg params are constrained -- TBD why. 791 ↛ 796line 791 didn't jump to line 796 because the condition on line 791 was never true

792 # Never wanted in production - this takes a long time (longer than the fit!) 

793 # This is how to get confidence intervals out: 

794 # https://lmfit.github.io/lmfit-py/confidence.html and 

795 # http://cars9.uchicago.edu/software/python/lmfit/model.html 

796 print(result.fit_report(show_correl=False)) 

797 if separateNegParams: 

798 print(result.ci_report()) 

799 

800 return result 

801 

802 def fitDipole(self, source, tol=1e-7, rel_weight=0.1, 

803 fitBackground=1, maxSepInSigma=5., separateNegParams=True, 

804 bgGradientOrder=1, verbose=False, display=False): 

805 """Fit a dipole model to an input ``diaSource`` (wraps `fitDipoleImpl`). 

806 

807 Actually, fits the subimage bounded by the input source's 

808 footprint) and optionally constrain the fit using the 

809 pre-subtraction images self.posImage (science) and 

810 self.negImage (template). Wraps the output into a 

811 `pipeBase.Struct` named tuple after computing additional 

812 statistics such as orientation and SNR. 

813 

814 Parameters 

815 ---------- 

816 source : `lsst.afw.table.SourceRecord` 

817 Record containing the (merged) dipole source footprint detected on the diffim 

818 tol : `float`, optional 

819 Tolerance parameter for scipy.leastsq() optimization 

820 rel_weight : `float`, optional 

821 Weighting of posImage/negImage relative to the diffim in the fit 

822 fitBackground : `int`, {0, 1, 2}, optional 

823 How to fit linear background gradient in posImage/negImage 

824 

825 - 0: do not fit background at all 

826 - 1 (default): pre-fit the background using linear least squares and then do not fit it 

827 as part of the dipole fitting optimization 

828 - 2: pre-fit the background using linear least squares (as in 1), and use the parameter 

829 estimates from that fit as starting parameters for an integrated "re-fit" of the 

830 background as part of the overall dipole fitting optimization. 

831 maxSepInSigma : `float`, optional 

832 Allowed window of centroid parameters relative to peak in input source footprint 

833 separateNegParams : `bool`, optional 

834 Fit separate parameters to the flux and background gradient in 

835 bgGradientOrder : `int`, {0, 1, 2}, optional 

836 Desired polynomial order of background gradient 

837 verbose: `bool`, optional 

838 Be verbose 

839 display 

840 Display input data, best fit model(s) and residuals in a matplotlib window. 

841 

842 Returns 

843 ------- 

844 result : `struct` 

845 `pipeBase.Struct` object containing the fit parameters and other information. 

846 

847 result : `callable` 

848 `lmfit.MinimizerResult` object for debugging and error estimation, etc. 

849 

850 Notes 

851 ----- 

852 Parameter `fitBackground` has three options, thus it is an integer: 

853 

854 """ 

855 

856 fitResult = self.fitDipoleImpl( 

857 source, tol=tol, rel_weight=rel_weight, fitBackground=fitBackground, 

858 maxSepInSigma=maxSepInSigma, separateNegParams=separateNegParams, 

859 bgGradientOrder=bgGradientOrder, verbose=verbose) 

860 

861 # Display images, model fits and residuals (currently uses matplotlib display functions) 

862 if display: 862 ↛ 863line 862 didn't jump to line 863 because the condition on line 862 was never true

863 fp = source.getFootprint() 

864 self.displayFitResults(fp, fitResult) 

865 

866 # usually around 0.1 -- the minimum flux allowed -- i.e. bad fit. 

867 if fitResult.params['flux'].value <= 1.: 867 ↛ 868line 867 didn't jump to line 868 because the condition on line 867 was never true

868 self.log.debug("Fitted flux too small for id=%d; ModelResult.message='%s'", 

869 source["id"], fitResult.message) 

870 return None, fitResult 

871 if not fitResult.result.errorbars: 871 ↛ 872line 871 didn't jump to line 872 because the condition on line 871 was never true

872 self.log.debug("Could not estimate error bars for id=%d; ModelResult.message='%s'", 

873 source["id"], fitResult.message) 

874 return None, fitResult 

875 

876 # TODO: We could include covariances, which could be derived from 

877 # `fitResult.params[name].correl`, but those are correlations. 

878 posCentroid = measBase.CentroidResult(fitResult.params['xcenPos'].value, 

879 fitResult.params['ycenPos'].value, 

880 fitResult.params['xcenPos'].stderr, 

881 fitResult.params['ycenPos'].stderr) 

882 negCentroid = measBase.CentroidResult(fitResult.params['xcenNeg'].value, 

883 fitResult.params['ycenNeg'].value, 

884 fitResult.params['xcenNeg'].stderr, 

885 fitResult.params['ycenNeg'].stderr) 

886 xposIdx = fitResult.var_names.index("xcenPos") 

887 yposIdx = fitResult.var_names.index("ycenPos") 

888 xnegIdx = fitResult.var_names.index("xcenNeg") 

889 ynegIdx = fitResult.var_names.index("ycenNeg") 

890 centroid = measBase.CentroidResult((fitResult.params['xcenPos'] + fitResult.params['xcenNeg']) / 2, 

891 (fitResult.params['ycenPos'] + fitResult.params['ycenNeg']) / 2., 

892 math.sqrt(posCentroid.xErr**2 + negCentroid.xErr**2 

893 + 2*fitResult.covar[xposIdx, xnegIdx]) / 2., 

894 math.sqrt(posCentroid.yErr**2 + negCentroid.yErr**2 

895 + 2*fitResult.covar[yposIdx, ynegIdx]) / 2.) 

896 dx = fitResult.params['xcenPos'].value - fitResult.params['xcenNeg'].value 

897 dy = fitResult.params['ycenPos'].value - fitResult.params['ycenNeg'].value 

898 angle = np.arctan2(dy, dx) 

899 

900 # Extract flux value, compute signalToNoise from flux/variance_within_footprint 

901 # Also extract the stderr of flux estimate. 

902 # TODO: should this instead use the lmfit-computed uncertainty from 

903 # `lmfitResult.result.uvars['flux'].std_dev`? 

904 def computeSumVariance(exposure, footprint): 

905 return math.sqrt(np.nansum(exposure[footprint.getBBox(), afwImage.PARENT].variance.array)) 

906 

907 # NOTE: These will all be the same unless separateNegParams=True! 

908 flux = measBase.FluxResult(fitResult.params["flux"].value, fitResult.params["flux"].stderr) 

909 posFlux = measBase.FluxResult(fitResult.params["flux"].value, fitResult.params["flux"].stderr) 

910 negFlux = measBase.FluxResult(fitResult.params["flux"].value, fitResult.params["flux"].stderr) 

911 if self.posImage is not None: 

912 fluxVar = computeSumVariance(self.posImage, source.getFootprint()) 

913 else: 

914 fluxVar = computeSumVariance(self.diffim, source.getFootprint()) 

915 fluxVarNeg = fluxVar 

916 

917 if separateNegParams: 917 ↛ 918line 917 didn't jump to line 918 because the condition on line 917 was never true

918 negFlux.instFlux = fitResult.params['fluxNeg'].value 

919 negFlux.instFluxErr = fitResult.params['fluxNeg'].stderr 

920 if self.negImage is not None: 

921 fluxVarNeg = computeSumVariance(self.negImage, source.getFootprint()) 

922 

923 try: 

924 signalToNoise = math.sqrt((posFlux.instFlux/fluxVar)**2 + (negFlux.instFlux/fluxVarNeg)**2) 

925 except ZeroDivisionError: # catch divide by zero - should never happen. 

926 signalToNoise = np.nan 

927 

928 out = Struct(posCentroid=posCentroid, negCentroid=negCentroid, centroid=centroid, 

929 posFlux=posFlux, negFlux=negFlux, flux=flux, orientation=angle, 

930 signalToNoise=signalToNoise, chi2=fitResult.chisqr, redChi2=fitResult.redchi, 

931 nData=fitResult.ndata) 

932 

933 # fitResult may be returned for debugging 

934 return out, fitResult 

935 

936 def displayFitResults(self, footprint, result): 

937 """Display data, model fits and residuals (currently uses matplotlib display functions). 

938 

939 Parameters 

940 ---------- 

941 footprint : `lsst.afw.detection.Footprint` 

942 Footprint containing the dipole that was fit 

943 result : `lmfit.MinimizerResult` 

944 `lmfit.MinimizerResult` object returned by `lmfit` optimizer 

945 """ 

946 try: 

947 import matplotlib.pyplot as plt 

948 except ImportError as err: 

949 self.log.warning('Unable to import matplotlib: %s', err) 

950 raise err 

951 

952 def display2dArray(ax, arr, x, y, xErr, yErr, title, extent=None): 

953 """Use `matplotlib.pyplot.imshow` to display a 2-D array with a given coordinate range. 

954 """ 

955 fig = ax.imshow(arr, origin='lower', interpolation='none', cmap='gray', extent=extent) 

956 ax.set_title(title) 

957 ax.errorbar(x["total"], y["total"], xErr["total"], yErr["total"], c="cyan") 

958 ax.errorbar(x["Pos"], y["Pos"], xErr["Pos"], yErr["Pos"], c="green") 

959 ax.errorbar(x["Neg"], y["Neg"], xErr["Neg"], yErr["Neg"], c="red") 

960 return fig 

961 

962 z = result.data 

963 fit = result.best_fit 

964 bbox = footprint.getBBox() 

965 extent = (bbox.getBeginX(), bbox.getEndX(), bbox.getBeginY(), bbox.getEndY()) 

966 

967 if z.shape[0] == 3: 

968 x, y, xErr, yErr = {}, {}, {}, {} 

969 for name in ("Pos", "Neg"): 

970 x[name] = result.best_values[f"xcen{name}"] 

971 y[name] = result.best_values[f"ycen{name}"] 

972 xErr[name] = result.params[f"xcen{name}"].stderr 

973 yErr[name] = result.params[f"ycen{name}"].stderr 

974 x["total"] = (result.best_values["xcenPos"] + result.best_values["xcenNeg"])/2 

975 y["total"] = (result.best_values["ycenPos"] + result.best_values["ycenNeg"])/2 

976 xErr["total"] = math.sqrt(xErr["Pos"]**2 + xErr["Neg"]**2) 

977 yErr["total"] = math.sqrt(yErr["Pos"]**2 + yErr["Neg"]**2) 

978 

979 fig, axes = plt.subplots(nrows=3, ncols=3, sharex=True, sharey=True, figsize=(8, 8)) 

980 for i, label in enumerate(("total", "Pos", "Neg")): 

981 display2dArray(axes[i][0], z[i, :], x, y, xErr, yErr, 

982 f'Data {label}', extent=extent) 

983 display2dArray(axes[i][1], fit[i, :], x, y, xErr, yErr, 

984 f'Model {label}', extent=extent) 

985 display2dArray(axes[i][2], z[i, :] - fit[i, :], x, y, xErr, yErr, 

986 f'Residual {label}', extent=extent) 

987 

988 plt.setp(axes[i][1].get_yticklabels(), visible=False) 

989 plt.setp(axes[i][2].get_yticklabels(), visible=False) 

990 if i != 2: # remove top two row x-axis labels 

991 plt.setp(axes[i][0].get_xticklabels(), visible=False) 

992 plt.setp(axes[i][1].get_xticklabels(), visible=False) 

993 plt.setp(axes[i][2].get_xticklabels(), visible=False) 

994 else: 

995 fig, axes = plt.subplots(nrows=3, ncols=3, sharex=True, sharey=True, figsize=(10, 2.5)) 

996 display2dArray(axes[0], z, 'Data', extent=extent) 

997 display2dArray(axes[1], 'Model', extent=extent) 

998 display2dArray(axes[2], z - fit, 'Residual', extent=extent) 

999 

1000 fig.tight_layout(pad=0, w_pad=0, h_pad=0) 

1001 plt.show() 

1002 

1003 

1004@measBase.register("ip_diffim_DipoleFit") 

1005class DipoleFitPlugin(measBase.SingleFramePlugin): 

1006 """A single frame measurement plugin that fits dipoles to all merged (two-peak) ``diaSources``. 

1007 

1008 This measurement plugin accepts up to three input images in 

1009 its `measure` method. If these are provided, it includes data 

1010 from the pre-subtraction posImage (science image) and optionally 

1011 negImage (template image) to constrain the fit. The meat of the 

1012 fitting routines are in the class `~lsst.module.name.DipoleFitAlgorithm`. 

1013 

1014 Notes 

1015 ----- 

1016 The motivation behind this plugin and the necessity for including more than 

1017 one exposure are documented in DMTN-007 (http://dmtn-007.lsst.io). 

1018 

1019 This class is named `ip_diffim_DipoleFit` so that it may be used alongside 

1020 the existing `ip_diffim_DipoleMeasurement` classes until such a time as those 

1021 are deemed to be replaceable by this. 

1022 """ 

1023 

1024 ConfigClass = DipoleFitPluginConfig 

1025 DipoleFitAlgorithmClass = DipoleFitAlgorithm # Pointer to the class that performs the fit 

1026 

1027 FAILURE_EDGE = 1 # too close to the edge 

1028 FAILURE_FIT = 2 # failure in the fitting 

1029 FAILURE_NOT_DIPOLE = 4 # input source is not a putative dipole to begin with 

1030 FAILURE_TOO_LARGE = 8 # input source is too large to be fit 

1031 

1032 @classmethod 

1033 def getExecutionOrder(cls): 

1034 """This algorithm simultaneously fits the centroid and flux, and does 

1035 not require any previous centroid fit. 

1036 """ 

1037 return cls.CENTROID_ORDER 

1038 

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

1040 if logName is None: 1040 ↛ 1042line 1040 didn't jump to line 1042 because the condition on line 1040 was always true

1041 logName = name 

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

1043 

1044 self.log = logging.getLogger(logName) 

1045 

1046 self._setupSchema(config, name, schema, metadata) 

1047 

1048 def _setupSchema(self, config, name, schema, metadata): 

1049 """Add fields for the outputs, and save the keys for fast assignment. 

1050 """ 

1051 self.posFluxKey = measBase.FluxResultKey.addFields(schema, 

1052 schema.join(name, "pos"), 

1053 "Dipole positive lobe instrumental flux.") 

1054 self.negFluxKey = measBase.FluxResultKey.addFields(schema, 

1055 schema.join(name, "neg"), 

1056 "Dipole negative lobe instrumental flux.") 

1057 doc = "Dipole overall instrumental flux (mean of absolute value of positive and negative lobes)." 

1058 self.fluxKey = measBase.FluxResultKey.addFields(schema, name, doc) 

1059 

1060 self.posCentroidKey = measBase.CentroidResultKey.addFields(schema, 

1061 schema.join(name, "pos"), 

1062 "Dipole positive lobe centroid position.", 

1063 measBase.UncertaintyEnum.SIGMA_ONLY) 

1064 self.negCentroidKey = measBase.CentroidResultKey.addFields(schema, 

1065 schema.join(name, "neg"), 

1066 "Dipole negative lobe centroid position.", 

1067 measBase.UncertaintyEnum.SIGMA_ONLY) 

1068 self.centroidKey = measBase.CentroidResultKey.addFields(schema, 

1069 name, 

1070 "Dipole centroid position.", 

1071 measBase.UncertaintyEnum.SIGMA_ONLY) 

1072 

1073 self.orientationKey = schema.addField( 

1074 schema.join(name, "orientation"), type=float, units="rad", 

1075 doc="Dipole orientation. Convention is CCW from +x on image.") 

1076 

1077 self.separationKey = schema.addField( 

1078 schema.join(name, "separation"), type=float, units="pixel", 

1079 doc="Pixel separation between positive and negative lobes of dipole") 

1080 

1081 self.chi2dofKey = schema.addField( 

1082 schema.join(name, "chi2dof"), type=float, 

1083 doc="Chi2 per degree of freedom (chi2/(nData-nVariables)) of dipole fit") 

1084 

1085 self.nDataKey = schema.addField( 

1086 schema.join(name, "nData"), type=np.int64, 

1087 doc="Number of data points in the dipole fit") 

1088 

1089 self.signalToNoiseKey = schema.addField( 

1090 schema.join(name, "signalToNoise"), type=float, 

1091 doc="Estimated signal-to-noise of dipole fit") 

1092 

1093 self.classificationFlagKey = schema.addField( 

1094 schema.join(name, "classification"), type="Flag", 

1095 doc="Flag indicating diaSource is classified as a dipole") 

1096 

1097 self.classificationAttemptedFlagKey = schema.addField( 

1098 schema.join(name, "classificationAttempted"), type="Flag", 

1099 doc="Flag indicating diaSource was attempted to be classified as a dipole") 

1100 

1101 self.flagKey = schema.addField( 

1102 schema.join(name, "flag"), type="Flag", 

1103 doc="General failure flag for dipole fit") 

1104 

1105 self.edgeFlagKey = schema.addField( 

1106 schema.join(name, "flag", "edge"), type="Flag", 

1107 doc="Flag set when dipole is too close to edge of image") 

1108 

1109 def measureDipoles(self, measRecord, exposure, posExp=None, negExp=None): 

1110 """Perform the non-linear least squares minimization on the putative dipole source. 

1111 

1112 Parameters 

1113 ---------- 

1114 measRecord : `lsst.afw.table.SourceRecord` 

1115 diaSources that will be measured using dipole measurement 

1116 exposure : `lsst.afw.image.Exposure` 

1117 Difference exposure on which the diaSources were detected; `exposure = posExp-negExp` 

1118 If both `posExp` and `negExp` are `None`, will attempt to fit the 

1119 dipole to just the `exposure` with no constraint. 

1120 posExp : `lsst.afw.image.Exposure`, optional 

1121 "Positive" exposure, typically a science exposure, or None if unavailable 

1122 When `posExp` is `None`, will compute `posImage = exposure + negExp`. 

1123 negExp : `lsst.afw.image.Exposure`, optional 

1124 "Negative" exposure, typically a template exposure, or None if unavailable 

1125 When `negExp` is `None`, will compute `negImage = posExp - exposure`. 

1126 

1127 Notes 

1128 ----- 

1129 The main functionality of this routine was placed outside of 

1130 this plugin (into `DipoleFitAlgorithm.fitDipole()`) so that 

1131 `DipoleFitAlgorithm.fitDipole()` can be called separately for 

1132 testing (@see `tests/testDipoleFitter.py`) 

1133 """ 

1134 result = None 

1135 peakValues = [peak.getPeakValue() for peak in measRecord.getFootprint().getPeaks()] 

1136 

1137 # Check if the footprint consists of a putative dipole - else don't fit it. 

1138 if ( 

1139 # One peak in the footprint (not a dipole). 

1140 len(peakValues) <= 1 

1141 # Peaks are the same sign (not a dipole). 

1142 or np.sign(max(peakValues)) == np.sign(min(peakValues)) 

1143 ): 

1144 if not self.config.fitAllDiaSources: 1144 ↛ 1154line 1144 didn't jump to line 1154 because the condition on line 1144 was always true

1145 # Non-dipoles fall back on the centroid slot for positions, 

1146 # errors, and the failure flag, if we're not fitting them. 

1147 measRecord[self.centroidKey.getX()] = measRecord.getX() 

1148 measRecord[self.centroidKey.getY()] = measRecord.getY() 

1149 self.centroidKey.getCentroidErr().set(measRecord, measRecord.getCentroidErr()) 

1150 measRecord[self.flagKey] = measRecord.getCentroidFlag() 

1151 return 

1152 

1153 # Footprint is too large (not a dipole). 

1154 if ((area := measRecord.getFootprint().getArea()) > self.config.maxFootprintArea): 

1155 self.fail(measRecord, measBase.MeasurementError(f"{area} > {self.config.maxFootprintArea}", 

1156 self.FAILURE_TOO_LARGE)) 

1157 return 

1158 

1159 try: 

1160 alg = self.DipoleFitAlgorithmClass(exposure, posImage=posExp, negImage=negExp) 

1161 result, _ = alg.fitDipole( 

1162 measRecord, rel_weight=self.config.relWeight, 

1163 tol=self.config.tolerance, 

1164 maxSepInSigma=self.config.maxSeparation, 

1165 fitBackground=self.config.fitBackground, 

1166 separateNegParams=self.config.fitSeparateNegParams, 

1167 verbose=False, display=False) 

1168 except pexExcept.LengthError: 

1169 self.fail(measRecord, measBase.MeasurementError('edge failure', self.FAILURE_EDGE)) 

1170 except Exception as e: 

1171 errorMessage = f"Exception in dipole fit. {e.__class__.__name__}: {e}" 

1172 self.fail(measRecord, measBase.MeasurementError(errorMessage, self.FAILURE_FIT)) 

1173 

1174 self.log.debug("Dipole fit result: %d %s", measRecord.getId(), str(result)) 

1175 

1176 if result is None: 1176 ↛ 1177line 1176 didn't jump to line 1177 because the condition on line 1176 was never true

1177 self.fail(measRecord, measBase.MeasurementError("bad dipole fit", self.FAILURE_FIT)) 

1178 return 

1179 

1180 # TODO: add chi2, dipole classification 

1181 self.posFluxKey.set(measRecord, result.posFlux) 

1182 self.posCentroidKey.set(measRecord, result.posCentroid) 

1183 

1184 self.negFluxKey.set(measRecord, result.negFlux) 

1185 self.negCentroidKey.set(measRecord, result.negCentroid) 

1186 

1187 self.fluxKey.set(measRecord, result.flux) 

1188 self.centroidKey.set(measRecord, result.centroid) 

1189 

1190 measRecord[self.orientationKey] = result.orientation 

1191 measRecord[self.separationKey] = math.sqrt((result.posCentroid.x - result.negCentroid.x)**2 

1192 + (result.posCentroid.y - result.negCentroid.y)**2) 

1193 

1194 measRecord[self.signalToNoiseKey] = result.signalToNoise 

1195 measRecord[self.chi2dofKey] = result.redChi2 

1196 

1197 if result.nData >= 1: 1197 ↛ 1200line 1197 didn't jump to line 1200 because the condition on line 1197 was always true

1198 measRecord[self.nDataKey] = result.nData 

1199 else: 

1200 measRecord[self.nDataKey] = 0 

1201 

1202 self.doClassify(measRecord, result.chi2) 

1203 

1204 def doClassify(self, measRecord, chi2val): 

1205 """Classify a source as a dipole. 

1206 

1207 Parameters 

1208 ---------- 

1209 measRecord : TODO: DM-17458 

1210 TODO: DM-17458 

1211 chi2val : TODO: DM-17458 

1212 TODO: DM-17458 

1213 

1214 Notes 

1215 ----- 

1216 Sources are classified as dipoles, or not, according to three criteria: 

1217 

1218 1. Does the total signal-to-noise surpass the ``minSn``? 

1219 2. Are the pos/neg fluxes greater than 1.0 and no more than 0.65 (``maxFluxRatio``) 

1220 of the total flux? By default this will never happen since ``posFlux == negFlux``. 

1221 3. Is it a good fit (``chi2dof`` < 1)? (Currently not used.) 

1222 """ 

1223 

1224 # First, does the total signal-to-noise surpass the minSn? 

1225 passesSn = measRecord[self.signalToNoiseKey] > self.config.minSn 

1226 

1227 # Second, are the pos/neg fluxes greater than 1.0 and no more than 0.65 (param maxFluxRatio) 

1228 # of the total flux? By default this will never happen since posFlux = negFlux. 

1229 passesFluxPos = (abs(measRecord[self.posFluxKey.getInstFlux()]) 

1230 / (measRecord[self.fluxKey.getInstFlux()]*2.)) < self.config.maxFluxRatio 

1231 passesFluxPos &= (abs(measRecord[self.posFluxKey.getInstFlux()]) >= 1.0) 

1232 passesFluxNeg = (abs(measRecord[self.negFluxKey.getInstFlux()]) 

1233 / (measRecord[self.fluxKey.getInstFlux()]*2.)) < self.config.maxFluxRatio 

1234 passesFluxNeg &= (abs(measRecord[self.negFluxKey.getInstFlux()]) >= 1.0) 

1235 allPass = (passesSn and passesFluxPos and passesFluxNeg) # and passesChi2) 

1236 

1237 # Third, is it a good fit (chi2dof < 1)? 

1238 # Use scipy's chi2 cumulative distrib to estimate significance 

1239 # This doesn't really work since I don't trust the values in the variance plane (which 

1240 # affects the least-sq weights, which affects the resulting chi2). 

1241 # But I'm going to keep this here for future use. 

1242 if False: 

1243 from scipy.stats import chi2 

1244 ndof = chi2val / measRecord[self.chi2dofKey] 

1245 significance = chi2.cdf(chi2val, ndof) 

1246 passesChi2 = significance < self.config.maxChi2DoF 

1247 allPass = allPass and passesChi2 

1248 

1249 measRecord.set(self.classificationAttemptedFlagKey, True) 

1250 

1251 if allPass: # Note cannot pass `allPass` into the `measRecord.set()` call below...? 1251 ↛ 1254line 1251 didn't jump to line 1254 because the condition on line 1251 was always true

1252 measRecord.set(self.classificationFlagKey, True) 

1253 else: 

1254 measRecord.set(self.classificationFlagKey, False) 

1255 

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

1257 """Catch failures and set the correct flags. 

1258 

1259 Fallback on the current slot centroid positions, but set the dipole 

1260 failure flag, since we attempted to fit the source. 

1261 """ 

1262 measRecord[self.centroidKey.getX()] = measRecord.getX() 

1263 measRecord[self.centroidKey.getY()] = measRecord.getY() 

1264 self.centroidKey.getCentroidErr().set(measRecord, measRecord.getCentroidErr()) 

1265 

1266 measRecord.set(self.flagKey, True) 

1267 if error is not None: 1267 ↛ 1277line 1267 didn't jump to line 1277 because the condition on line 1267 was always true

1268 if error.getFlagBit() == self.FAILURE_EDGE: 1268 ↛ 1269line 1268 didn't jump to line 1269 because the condition on line 1268 was never true

1269 self.log.debug('DipoleFitPlugin not run on record %d: %s', measRecord.getId(), str(error)) 

1270 measRecord.set(self.edgeFlagKey, True) 

1271 if error.getFlagBit() == self.FAILURE_FIT: 1271 ↛ 1272line 1271 didn't jump to line 1272 because the condition on line 1271 was never true

1272 self.log.warning('DipoleFitPlugin failed on record %d: %s', measRecord.getId(), str(error)) 

1273 if error.getFlagBit() == self.FAILURE_TOO_LARGE: 1273 ↛ exitline 1273 didn't return from function 'fail' because the condition on line 1273 was always true

1274 self.log.debug('DipoleFitPlugin not run on record with too large footprint %d: %s', 

1275 measRecord.getId(), str(error)) 

1276 else: 

1277 self.log.warning('DipoleFitPlugin failed on record %d', measRecord.getId())