33 # include "Minuit2/FCNBase.h"
34 # include "Minuit2/FunctionMinimum.h"
35 # include "Minuit2/MnMigrad.h"
36 # include "Minuit2/MnMinos.h"
37 # include "Minuit2/MnPrint.h"
44 #include "lsst/afw/detection/FootprintArray.cc"
49 #include "ndarray/eigen.h"
51 namespace pexExceptions = lsst::pex::exceptions;
52 namespace afwDet = lsst::afw::detection;
54 namespace afwMath = lsst::afw::math;
55 namespace afwGeom = lsst::afw::geom;
57 namespace lsst {
namespace ip {
namespace diffim {
70 afw::table::SourceRecord & source,
71 afw::image::Exposure<float>
const& exposure,
73 meas::base::CentroidResultKey
const & keys
76 typedef afw::image::Image<float> ImageT;
77 ImageT
const&
image = *exposure.getMaskedImage().getImage();
79 source.set(keys.getX(), center.getX());
80 source.set(keys.getY(), center.getY());
82 int x = center.getX() - image.getX0();
83 int y = center.getY() - image.getY0();
85 if (x < 1 || x >= image.getWidth() - 1 || y < 1 || y >= image.getHeight() - 1) {
91 ImageT::xy_locator im = image.xy_at(x, y);
94 (im(-1, 1) + im( 0, 1) + im( 1, 1) +
95 im(-1, 0) + im( 0, 0) + im( 1, 0) +
96 im(-1, -1) + im( 0, -1) + im( 1, -1));
106 -im(-1, 1) + im( 1, 1) +
107 -im(-1, 0) + im( 1, 0) +
108 -im(-1, -1) + im( 1, -1);
110 (im(-1, 1) + im( 0, 1) + im( 1, 1)) -
111 (im(-1, -1) + im( 0, -1) + im( 1, -1));
115 source.set(keys.getX(), xx);
116 source.set(keys.getY(), yy);
123 Control
const & ctrl,
124 std::string
const &
name,
140 double posValue = peaks[posInd].getPeakValue(), negValue = 0;
142 posInd = peaks.
size() - 1;
143 posValue = peaks[posInd].getPeakValue();
145 naiveCentroid(source, exposure, peaks[posInd].getI(),
148 if (posValue > 0. && posInd == 0 && peaks.
size() > 1) {
149 int negInd = peaks.
size() - 1;
150 negValue = peaks[negInd].getPeakValue();
151 if (posValue > 0. && negValue < 0.) {
152 naiveCentroid(source, exposure, peaks[negInd].getI(),
162 double posValue,
double negValue)
const {
164 double pos_x, pos_y, pos_f;
165 double neg_x, neg_y, neg_f;
175 if(std::isfinite(pos_x) && std::isfinite(pos_y) &&
176 std::isfinite(neg_x) && std::isfinite(neg_y)) {
177 source.
set(
getCenterKeys().getX(), (pos_x * pos_f + neg_x * neg_f) / (pos_f + neg_f));
178 source.
set(
getCenterKeys().getY(), (pos_y * pos_f + neg_y * neg_f) / (pos_f + neg_f));
179 }
else if (std::isfinite(pos_x) && std::isfinite(pos_y)) {
199 ) : afw::detection::FootprintFunctor< afw::image::MaskedImage<float> >(mimage),
210 void operator()( afw::image::MaskedImage<float>::xy_locator loc,
295 virtual double operator()(std::vector<double>
const & params)
const {
304 if ((negFlux > 0.0) || (posFlux < 0.0)) {
309 posCenterX, posCenterY, posFlux);
310 double chi2 = fit.first;
311 int nPix = fit.second;
334 double negCenterX,
double negCenterY,
double negFlux,
335 double posCenterX,
double posCenterY,
double posFlux
354 footprint->getBBox());
362 int negXmin = std::max(negPsfBBox.getMinX(), negModelBBox.
getMinX());
363 int negYmin = std::max(negPsfBBox.getMinY(), negModelBBox.
getMinY());
364 int negXmax = std::min(negPsfBBox.getMaxX(), negModelBBox.
getMaxX());
365 int negYmax = std::min(negPsfBBox.getMaxY(), negModelBBox.
getMaxY());
370 negModelSubim += negSubim;
373 int posXmin = std::max(posPsfBBox.
getMinX(), posModelBBox.
getMinX());
374 int posYmin = std::max(posPsfBBox.
getMinY(), posModelBBox.
getMinY());
375 int posXmax = std::min(posPsfBBox.
getMaxX(), posModelBBox.
getMaxX());
376 int posYmax = std::min(posPsfBBox.
getMaxY(), posModelBBox.
getMaxY());
381 posModelSubim += posSubim;
386 residuals += posModel;
388 residuals *= residuals;
393 return std::pair<double,int>(
chi2, nPix);
411 if (peakCatalog.
size() == 0) {
415 else if (peakCatalog.
size() == 1) {
427 ROOT::Minuit2::MnUserParameters fitPar;
444 ROOT::Minuit2::MnMigrad migrad(minimizerFunc, fitPar);
451 float minChi2 = min.Fval();
452 bool const isValid = min.IsValid() && std::isfinite(minChi2);
454 if (
true || isValid) {
461 std::pair<double,int> fit =
chi2(source, exposure,
468 double evalChi2 = fit.first;
469 int nPix = fit.second;
An include file to include the public header files for lsst::afw::math.
Defines the fields and offsets for a table.
afw::table::Key< float > _chi2dofKey
float stepSizeFlux
"Default initial step size for flux in non-linear fitter" ;
An include file to include the header files for lsst::afw::geom.
meas::base::CentroidResultKey _avgCentroid
Implementation of Psf dipole flux.
double indexToPosition(double ind)
Convert image index to image position.
table::Key< std::string > name
void mergeCentroids(afw::table::SourceRecord &source, double posValue, double negValue) const
afw::table::Key< int > _numNegativeKey
ResultKey const & getCenterKeys() const
Return the standard centroid keys registered by this algorithm.
afw::table::Key< CentroidElement > getY() const
Return a Key for the y coordinate.
afw::table::Schema schema
Include files required for standard LSST Exception handling.
PixelT Pixel
A pixel in this ImageBase.
reference back() const
Return the last record.
MinimizeDipoleChi2(PsfDipoleFlux const &psfDipoleFlux, afw::table::SourceRecord &source, afw::image::Exposure< float > const &exposure)
ImagePtr getImage(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's image.
size_type size() const
Return the number of elements in the catalog.
void fail(afw::table::SourceRecord &measRecord, meas::base::MeasurementError *error=NULL) const
Handle an exception thrown by the current algorithm by setting flags in the given record...
meas::base::CentroidResultKey _negCentroid
PsfDipoleFlux const & _psfDipoleFlux
Exception to be thrown when a measurement algorithm experiences a known failure mode.
ResultKey const & getPositiveKeys() const
Return the standard flux keys registered by this algorithm.
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
ResultKey const & getNegativeKeys() const
A class to evaluate image statistics.
An integer coordinate rectangle.
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...
VariancePtr getVariance(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's variance.
table::Key< table::Array< Kernel::Pixel > > image
afw::image::Exposure< float > const & _exposure
meas::base::CentroidResultKey _posCentroid
void setErrorDef(double def)
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
An include file to include the header files for lsst::afw::image.
void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Called to measure a single child source in an image.
afw::table::CatalogT< PeakRecord > PeakCatalog
MaskedImageT getMaskedImage()
Return the MaskedImage.
Class to minimize PsfDipoleFlux; this is the object that Minuit minimizes.
An include file to include the header files for lsst::afw::detection.
afw::table::Key< CentroidElement > getX() const
Return a Key for the x coordinate.
meas::base::FlagHandler _flagHandler
ResultKey const & getNegativeKeys() const
A class to manipulate images, masks, and variance as a single object.
virtual double operator()(std::vector< double > const ¶ms) const
NaiveDipoleCentroid(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
double errorDef
"How many sigma the error bars of the non-linear fitter represent" ;
void setMaxPix(int maxPix)
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
void fail(afw::table::SourceRecord &measRecord, meas::base::MeasurementError *error=NULL) const
Handle an exception thrown by the current algorithm by setting flags in the given record...
Intermediate base class for algorithms that compute a centroid.
int maxFnCalls
"Maximum function calls for non-linear fitter; 0 = unlimited" ;
void fail(afw::table::SourceRecord &measRecord, meas::base::MeasurementError *error=NULL) const
Handle an exception thrown by the current algorithm by setting flags in the given record...
RecordId getId() const
Convenience accessors for the keys in the minimal reference schema.
boost::shared_ptr< lsst::afw::detection::Psf > getPsf()
Return the Exposure's Psf object.
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
meas::base::FlagHandler _flagHandler
float getFy() const
Convenience accessors for the keys in the minimal schema.
float stepSizeCoord
"Default initial step size for coordinates in non-linear fitter" ;
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
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
ResultKey const & getPositiveKeys() const
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=NULL) const
Handle an expected or unexpected Exception thrown by a measurement algorithm.
float getPeakValue() const
Convenience accessors for the keys in the minimal schema.
Record class that contains measurements made on a single exposure.
float getFx() const
Convenience accessors for the keys in the minimal schema.
afw::table::SourceRecord & _source
afw::table::Key< int > _numPositiveKey
find sum of pixels in the image
reference front() const
Return the first record.
#define CONST_PTR(...)
A shared pointer to a const object.
A polymorphic base class for representing an image's Point Spread Function.
Record class that represents a peak in a Footprint.
boost::shared_ptr< Footprint > getFootprint() const
A class to represent a 2-dimensional array of pixels.
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...