543 fitBackground=1, bgGradientOrder=1, maxSepInSigma=5.,
544 separateNegParams=True, verbose=False):
545 """Fit a dipole model to an input difference image.
547 Actually, fits the subimage bounded by the input source's
548 footprint) and optionally constrain the fit using the
549 pre-subtraction images posImage and negImage.
553 source : TODO: DM-17458
555 tol : float, optional
557 rel_weight : `float`, optional
559 fitBackground : `int`, optional
561 bgGradientOrder : `int`, optional
563 maxSepInSigma : `float`, optional
565 separateNegParams : `bool`, optional
567 verbose : `bool`, optional
572 result : `lmfit.MinimizerResult`
573 return `lmfit.MinimizerResult` object containing the fit
574 parameters and other information.
580 fp = source.getFootprint()
582 subim = afwImage.MaskedImageF(self.
diffim.getMaskedImage(), bbox=bbox, origin=afwImage.PARENT)
584 z = diArr = subim.image.array
587 weights = 1. / subim.variance.array
589 if rel_weight > 0.
and ((self.
posImage is not None)
or (self.
negImage is not None)):
591 negSubim = afwImage.MaskedImageF(self.
negImage.getMaskedImage(), bbox, origin=afwImage.PARENT)
593 posSubim = afwImage.MaskedImageF(self.
posImage.getMaskedImage(), bbox, origin=afwImage.PARENT)
595 posSubim = subim.clone()
598 negSubim = posSubim.clone()
601 z = np.append([z], [posSubim.image.array,
602 negSubim.image.array], axis=0)
604 weights = np.append([weights], [1. / posSubim.variance.array * rel_weight,
605 1. / negSubim.variance.array * rel_weight], axis=0)
612 def dipoleModelFunctor(x, flux, xcenPos, ycenPos, xcenNeg, ycenNeg, fluxNeg=None,
613 b=None, x1=None, y1=None, xy=None, x2=None, y2=None,
614 bNeg=None, x1Neg=None, y1Neg=None, xyNeg=None, x2Neg=None, y2Neg=None,
616 """Generate dipole model with given parameters.
618 It simply defers to `modelObj.makeModel()`, where `modelObj` comes
619 out of `kwargs['modelObj']`.
621 modelObj = kwargs.pop(
'modelObj')
622 return modelObj.makeModel(x, flux, xcenPos, ycenPos, xcenNeg, ycenNeg, fluxNeg=fluxNeg,
623 b=b, x1=x1, y1=y1, xy=xy, x2=x2, y2=y2,
624 bNeg=bNeg, x1Neg=x1Neg, y1Neg=y1Neg, xyNeg=xyNeg,
625 x2Neg=x2Neg, y2Neg=y2Neg, **kwargs)
629 modelFunctor = dipoleModelFunctor
635 gmod = lmfit.Model(modelFunctor, independent_vars=[
"x"], verbose=verbose)
639 fpCentroid = np.array([fp.getCentroid().getX(), fp.getCentroid().getY()])
640 cenNeg = cenPos = fpCentroid
645 cenPos = pks[0].getF()
647 cenNeg = pks[-1].getF()
651 maxSep = self.
psfSigma * maxSepInSigma
654 if np.sum(np.sqrt((np.array(cenPos) - fpCentroid)**2.)) > maxSep:
656 if np.sum(np.sqrt((np.array(cenNeg) - fpCentroid)**2.)) > maxSep:
662 gmod.set_param_hint(
'xcenPos', value=cenPos[0],
663 min=cenPos[0]-maxSep, max=cenPos[0]+maxSep)
664 gmod.set_param_hint(
'ycenPos', value=cenPos[1],
665 min=cenPos[1]-maxSep, max=cenPos[1]+maxSep)
666 gmod.set_param_hint(
'xcenNeg', value=cenNeg[0],
667 min=cenNeg[0]-maxSep, max=cenNeg[0]+maxSep)
668 gmod.set_param_hint(
'ycenNeg', value=cenNeg[1],
669 min=cenNeg[1]-maxSep, max=cenNeg[1]+maxSep)
673 startingFlux = np.nansum(np.abs(diArr) - np.nanmedian(np.abs(diArr))) * 5.
674 posFlux = negFlux = startingFlux
677 gmod.set_param_hint(
'flux', value=posFlux, min=0.1)
679 if separateNegParams:
681 gmod.set_param_hint(
'fluxNeg', value=np.abs(negFlux), min=0.1)
689 bgParsPos = bgParsNeg = (0., 0., 0.)
690 if ((rel_weight > 0.)
and (fitBackground != 0)
and (bgGradientOrder >= 0)):
694 bgParsPos = bgParsNeg = dipoleModel.fitFootprintBackground(source, bgFitImage,
695 order=bgGradientOrder)
698 if fitBackground == 1:
699 in_x = dipoleModel._generateXYGrid(bbox)
700 pbg = dipoleModel.makeBackgroundModel(in_x, tuple(bgParsPos))
702 z[1, :] -= np.nanmedian(z[1, :])
703 posFlux = np.nansum(z[1, :])
704 gmod.set_param_hint(
'flux', value=posFlux*1.5, min=0.1)
706 if separateNegParams
and self.
negImage is not None:
707 bgParsNeg = dipoleModel.fitFootprintBackground(source, self.
negImage,
708 order=bgGradientOrder)
709 pbg = dipoleModel.makeBackgroundModel(in_x, tuple(bgParsNeg))
711 z[2, :] -= np.nanmedian(z[2, :])
712 if separateNegParams:
713 negFlux = np.nansum(z[2, :])
714 gmod.set_param_hint(
'fluxNeg', value=negFlux*1.5, min=0.1)
717 if fitBackground == 2:
718 if bgGradientOrder >= 0:
719 gmod.set_param_hint(
'b', value=bgParsPos[0])
720 if separateNegParams:
721 gmod.set_param_hint(
'bNeg', value=bgParsNeg[0])
722 if bgGradientOrder >= 1:
723 gmod.set_param_hint(
'x1', value=bgParsPos[1])
724 gmod.set_param_hint(
'y1', value=bgParsPos[2])
725 if separateNegParams:
726 gmod.set_param_hint(
'x1Neg', value=bgParsNeg[1])
727 gmod.set_param_hint(
'y1Neg', value=bgParsNeg[2])
728 if bgGradientOrder >= 2:
729 gmod.set_param_hint(
'xy', value=bgParsPos[3])
730 gmod.set_param_hint(
'x2', value=bgParsPos[4])
731 gmod.set_param_hint(
'y2', value=bgParsPos[5])
732 if separateNegParams:
733 gmod.set_param_hint(
'xyNeg', value=bgParsNeg[3])
734 gmod.set_param_hint(
'x2Neg', value=bgParsNeg[4])
735 gmod.set_param_hint(
'y2Neg', value=bgParsNeg[5])
737 y, x = np.mgrid[bbox.getBeginY():bbox.getEndY(), bbox.getBeginX():bbox.getEndX()]
738 in_x = np.array([x, y]).astype(np.float64)
739 in_x[0, :] -= in_x[0, :].mean()
740 in_x[1, :] -= in_x[1, :].mean()
744 mask = np.ones_like(z, dtype=bool)
749 weights = mask.astype(np.float64)
750 if self.
posImage is not None and rel_weight > 0.:
751 weights = np.array([np.ones_like(diArr), np.ones_like(diArr)*rel_weight,
752 np.ones_like(diArr)*rel_weight])
759 nans = (np.isnan(z) | np.isnan(weights))
763 z[nans] = np.nanmedian(z)
771 with warnings.catch_warnings():
774 warnings.filterwarnings(
"ignore",
"The keyword argument .* does not match", UserWarning)
775 result = gmod.fit(z, weights=weights, x=in_x, max_nfev=250,
779 fit_kws={
'ftol': tol,
'xtol': tol,
'gtol': tol,
783 rel_weight=rel_weight,
785 modelObj=dipoleModel)
792 print(result.fit_report(show_correl=
False))
793 if separateNegParams:
794 print(result.ci_report())
799 fitBackground=1, maxSepInSigma=5., separateNegParams=True,
800 bgGradientOrder=1, verbose=False, display=False):
801 """Fit a dipole model to an input ``diaSource`` (wraps `fitDipoleImpl`).
803 Actually, fits the subimage bounded by the input source's
804 footprint) and optionally constrain the fit using the
805 pre-subtraction images self.posImage (science) and
806 self.negImage (template). Wraps the output into a
807 `pipeBase.Struct` named tuple after computing additional
808 statistics such as orientation and SNR.
812 source : `lsst.afw.table.SourceRecord`
813 Record containing the (merged) dipole source footprint detected on the diffim
814 tol : `float`, optional
815 Tolerance parameter for scipy.leastsq() optimization
816 rel_weight : `float`, optional
817 Weighting of posImage/negImage relative to the diffim in the fit
818 fitBackground : `int`, {0, 1, 2}, optional
819 How to fit linear background gradient in posImage/negImage
821 - 0: do not fit background at all
822 - 1 (default): pre-fit the background using linear least squares and then do not fit it
823 as part of the dipole fitting optimization
824 - 2: pre-fit the background using linear least squares (as in 1), and use the parameter
825 estimates from that fit as starting parameters for an integrated "re-fit" of the
826 background as part of the overall dipole fitting optimization.
827 maxSepInSigma : `float`, optional
828 Allowed window of centroid parameters relative to peak in input source footprint
829 separateNegParams : `bool`, optional
830 Fit separate parameters to the flux and background gradient in
831 bgGradientOrder : `int`, {0, 1, 2}, optional
832 Desired polynomial order of background gradient
833 verbose: `bool`, optional
836 Display input data, best fit model(s) and residuals in a matplotlib window.
841 `pipeBase.Struct` object containing the fit parameters and other information.
844 `lmfit.MinimizerResult` object for debugging and error estimation, etc.
848 Parameter `fitBackground` has three options, thus it is an integer:
853 source, tol=tol, rel_weight=rel_weight, fitBackground=fitBackground,
854 maxSepInSigma=maxSepInSigma, separateNegParams=separateNegParams,
855 bgGradientOrder=bgGradientOrder, verbose=verbose)
859 fp = source.getFootprint()
862 fitParams = fitResult.best_values
863 if fitParams[
'flux'] <= 1.:
864 return None, fitResult
866 centroid = ((fitParams[
'xcenPos'] + fitParams[
'xcenNeg']) / 2.,
867 (fitParams[
'ycenPos'] + fitParams[
'ycenNeg']) / 2.)
868 dx, dy = fitParams[
'xcenPos'] - fitParams[
'xcenNeg'], fitParams[
'ycenPos'] - fitParams[
'ycenNeg']
869 angle = np.arctan2(dy, dx)
873 def computeSumVariance(exposure, footprint):
874 return np.sqrt(np.nansum(exposure[footprint.getBBox(), afwImage.PARENT].variance.array))
876 fluxVal = fluxVar = fitParams[
'flux']
877 fluxErr = fluxErrNeg = fitResult.params[
'flux'].stderr
879 fluxVar = computeSumVariance(self.
posImage, source.getFootprint())
881 fluxVar = computeSumVariance(self.
diffim, source.getFootprint())
883 fluxValNeg, fluxVarNeg = fluxVal, fluxVar
884 if separateNegParams:
885 fluxValNeg = fitParams[
'fluxNeg']
886 fluxErrNeg = fitResult.params[
'fluxNeg'].stderr
888 fluxVarNeg = computeSumVariance(self.
negImage, source.getFootprint())
891 signalToNoise = np.sqrt((fluxVal/fluxVar)**2 + (fluxValNeg/fluxVarNeg)**2)
892 except ZeroDivisionError:
893 signalToNoise = np.nan
895 out = Struct(posCentroidX=fitParams[
'xcenPos'], posCentroidY=fitParams[
'ycenPos'],
896 negCentroidX=fitParams[
'xcenNeg'], negCentroidY=fitParams[
'ycenNeg'],
897 posFlux=fluxVal, negFlux=-fluxValNeg, posFluxErr=fluxErr, negFluxErr=fluxErrNeg,
898 centroidX=centroid[0], centroidY=centroid[1], orientation=angle,
899 signalToNoise=signalToNoise, chi2=fitResult.chisqr, redChi2=fitResult.redchi,
900 nData=fitResult.ndata)
903 return out, fitResult