32 # include "Minuit2/FCNBase.h"
33 # include "Minuit2/FunctionMinimum.h"
34 # include "Minuit2/MnMigrad.h"
35 # include "Minuit2/MnMinos.h"
36 # include "Minuit2/MnPrint.h"
47 #include "ndarray/eigen.h"
55 namespace lsst {
namespace ip {
namespace diffim {
58 meas::base::FlagDefinitionList dipoleFluxFlagDefinitions;
61 meas::base::FlagDefinition
const DipoleFluxAlgorithm::FAILURE = dipoleFluxFlagDefinitions.addFailureFlag(
"general failure flag, set if anything went wrong");
62 meas::base::FlagDefinition
const DipoleFluxAlgorithm::POS_FLAG = dipoleFluxFlagDefinitions.add(
"pos_flag",
"failure flag for positive, set if anything went wrong");
63 meas::base::FlagDefinition
const DipoleFluxAlgorithm::NEG_FLAG = dipoleFluxFlagDefinitions.add(
"neg_flag",
"failure flag for negative, set if anything went wrong");
66 return dipoleFluxFlagDefinitions;
78 return dipoleCentroidFlagDefinitions;
104 int x = center.getX() -
image.getX0();
105 int y = center.getY() -
image.getY0();
107 if (x < 1 || x >=
image.getWidth() - 1 || y < 1 || y >=
image.getHeight() - 1) {
109 (boost::format(
"Object at (%d, %d) is too close to the edge")
113 ImageT::xy_locator im =
image.xy_at(x, y);
116 (im(-1, 1) + im( 0, 1) + im( 1, 1) +
117 im(-1, 0) + im( 0, 0) + im( 1, 0) +
118 im(-1, -1) + im( 0, -1) + im( 1, -1));
123 (boost::format(
"Object at (%d, %d) has no counts") %
128 -im(-1, 1) + im( 1, 1) +
129 -im(-1, 0) + im( 1, 0) +
130 -im(-1, -1) + im( 1, -1);
132 (im(-1, 1) + im( 0, 1) + im( 1, 1)) -
133 (im(-1, -1) + im( 0, -1) + im( 1, -1));
162 double posValue = peaks[posInd].getPeakValue(), negValue = 0;
164 posInd = peaks.
size() - 1;
165 posValue = peaks[posInd].getPeakValue();
167 naiveCentroid(
source, exposure, peaks[posInd].getI(),
170 if (posValue > 0. && posInd == 0 && peaks.
size() > 1) {
171 int negInd = peaks.
size() - 1;
172 negValue = peaks[negInd].getPeakValue();
173 if (posValue > 0. && negValue < 0.) {
174 naiveCentroid(
source, exposure, peaks[negInd].getI(),
184 double posValue,
double negValue)
const {
186 double pos_x, pos_y, pos_f;
187 double neg_x, neg_y, neg_f;
218 class NaiveDipoleFootprinter {
220 explicit NaiveDipoleFootprinter() : _sumPositive(0.0), _sumNegative(0.0), _numPositive(0),
225 _sumPositive = _sumNegative = 0.0;
226 _numPositive = _numNegative = 0;
235 _sumPositive += ival;
236 _varPositive += vval;
239 _sumNegative += ival;
240 _varPositive += vval;
245 double getSumPositive()
const {
return _sumPositive; }
246 double getSumNegative()
const {
return _sumNegative; }
247 double getVarPositive()
const {
return _sumPositive; }
248 double getVarNegative()
const {
return _sumNegative; }
249 int getNumPositive()
const {
return _numPositive; }
250 int getNumNegative()
const {
return _numNegative; }
273 NaiveDipoleFootprinter functor;
279 source.set(_numPositiveKey, functor.getNumPositive());
283 source.set(_numNegativeKey, functor.getNumNegative());
304 _psfDipoleFlux(psfDipoleFlux),
308 double Up()
const {
return _errorDef; }
324 if ((negFlux > 0.0) || (posFlux < 0.0)) {
329 posCenterX, posCenterY, posFlux);
330 double chi2 = fit.first;
331 int nPix = fit.second;
332 if (nPix > _maxPix) {
354 double negCenterX,
double negCenterY,
double negFlux,
355 double posCenterX,
double posCenterY,
double posFlux
374 footprint->getBBox());
390 negModelSubim += negSubim;
401 posModelSubim += posSubim;
406 residuals += posModel;
408 residuals *= residuals;
410 afwMath::Statistics stats = afwMath::makeStatistics(residuals, afwMath::SUM | afwMath::NPOINT);
412 int nPix = stats.
getValue(afwMath::NPOINT);
426 (boost::format(
"No footprint for source %d") %
source.getId()).str());
431 if (peakCatalog.
size() == 0) {
433 (boost::format(
"No peak for source %d") %
source.getId()).str());
435 else if (peakCatalog.
size() == 1) {
447 ROOT::Minuit2::MnUserParameters fitPar;
464 ROOT::Minuit2::MnMigrad migrad(minimizerFunc, fitPar);
469 ROOT::Minuit2::FunctionMinimum
min = migrad(_ctrl.
maxFnCalls);
471 float minChi2 =
min.Fval();
488 double evalChi2 = fit.first;
489 int nPix = fit.second;
501 source.set(_chi2dofKey, evalChi2 / (nPix - minimizerFunc.
getNpar()));
502 source.set(_negCentroid.
getX(), minNegCentroid->getX());
503 source.set(_negCentroid.
getY(), minNegCentroid->getY());
504 source.set(_posCentroid.
getX(), minPosCentroid->getX());
505 source.set(_posCentroid.
getY(), minPosCentroid->getY());
506 source.set(_avgCentroid.
getX(), 0.5*(minNegCentroid->getX() + minPosCentroid->getX()));
507 source.set(_avgCentroid.
getY(), 0.5*(minNegCentroid->getY() + minPosCentroid->getY()));
table::Key< std::string > name
#define LSST_EXCEPT(type,...)
float getPeakValue() const
MaskedImageT getMaskedImage()
std::shared_ptr< lsst::afw::detection::Psf const > getPsf() const
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
VariancePtr getVariance() const
ImagePtr getImage() const
double getValue(Property const prop=NOTHING) const
int getMinY() const noexcept
int getMinX() const noexcept
int getMaxX() const noexcept
int getMaxY() const noexcept
Intermediate base class for algorithms that compute a centroid.
static meas::base::FlagDefinition const FAILURE
static meas::base::FlagDefinition const POS_FLAG
static meas::base::FlagDefinition const NEG_FLAG
static meas::base::FlagDefinitionList const & getFlagDefinitions()
static meas::base::FlagDefinitionList const & getFlagDefinitions()
static meas::base::FlagDefinition const POS_FLAG
static meas::base::FlagDefinition const NEG_FLAG
ResultKey const & getNegativeKeys() const
meas::base::FlagHandler _flagHandler
static meas::base::FlagDefinition const FAILURE
ResultKey const & getPositiveKeys() const
Return the standard flux keys registered by this algorithm.
Class to minimize PsfDipoleFlux; this is the object that Minuit minimizes.
void setMaxPix(int maxPix)
void setErrorDef(double def)
virtual double operator()(std::vector< double > const ¶ms) const
MinimizeDipoleChi2(PsfDipoleFlux const &psfDipoleFlux, afw::table::SourceRecord &source, afw::image::Exposure< float > const &exposure)
ResultKey const & getNegativeKeys() const
NaiveDipoleCentroid(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
ResultKey const & getCenterKeys() const
Return the standard centroid keys registered by this algorithm.
ResultKey const & getPositiveKeys() const
void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Given an image and a pixel position, return a Centroid using a naive 3x3 weighted moment.
void mergeCentroids(afw::table::SourceRecord &source, double posValue, double negValue) const
void fail(afw::table::SourceRecord &measRecord, meas::base::MeasurementError *error=NULL) const
void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Given an image and a pixel position, return a Centroid using a naive 3x3 weighted moment.
void fail(afw::table::SourceRecord &measRecord, meas::base::MeasurementError *error=NULL) const
float stepSizeCoord
"Default initial step size for coordinates in non-linear fitter" ;
double errorDef
"How many sigma the error bars of the non-linear fitter represent" ;
float stepSizeFlux
"Default initial step size for flux in non-linear fitter" ;
int maxFnCalls
"Maximum function calls for non-linear fitter; 0 = unlimited" ;
Implementation of Psf dipole flux.
std::pair< double, int > chi2(afw::table::SourceRecord &source, afw::image::Exposure< float > const &exposure, double negCenterX, double negCenterY, double negFlux, double posCenterX, double poCenterY, double posFlux) const
void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
void fail(afw::table::SourceRecord &measRecord, meas::base::MeasurementError *error=NULL) const
afw::table::Key< CentroidElement > getY() const
afw::table::Key< CentroidElement > getX() const
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=nullptr) const
afw::table::CatalogT< PeakRecord > PeakCatalog
double indexToPosition(double ind)