LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
PsfFlux.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2015 AURA/LSST.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #include "boost/array.hpp"
25 
26 #include "ndarray/eigen.h"
27 
28 #include "lsst/afw/table/Source.h"
29 #include "lsst/afw/detection/Psf.h"
31 #include "lsst/afw/detection/FootprintArray.cc"
32 #include "lsst/meas/base/PsfFlux.h"
33 
34 namespace lsst { namespace meas { namespace base {
35 
36 namespace {
37 
38 boost::array<FlagDefinition,PsfFluxAlgorithm::N_FLAGS> const & getFlagDefinitions() {
39  static boost::array<FlagDefinition,PsfFluxAlgorithm::N_FLAGS> const flagDefs = {{
40  {"flag", "general failure flag"},
41  {"flag_noGoodPixels", "not enough non-rejected pixels in data to attempt the fit"},
42  {"flag_edge", "object was too close to the edge of the image to use the full PSF model"}
43  }};
44  return flagDefs;
45 }
46 
47 } // anonymous
48 
50  Control const & ctrl,
51  std::string const & name,
53 ) : _ctrl(ctrl),
54  _fluxResultKey(
55  FluxResultKey::addFields(schema, name, "flux derived from linear least-squares fit of PSF model")
56  ),
57  _centroidExtractor(schema, name)
58 {
59  _flagHandler = FlagHandler::addFields(schema, name,
60  getFlagDefinitions().begin(), getFlagDefinitions().end());
61 }
62 
64  afw::table::SourceRecord & measRecord,
65  afw::image::Exposure<float> const & exposure
66 ) const {
67  PTR(afw::detection::Psf const) psf = exposure.getPsf();
68  if (!psf) {
69  throw LSST_EXCEPT(
70  FatalAlgorithmError,
71  "PsfFlux algorithm requires a Psf with every exposure"
72  );
73  }
74  afw::geom::Point2D position = _centroidExtractor(measRecord, _flagHandler);
75  PTR(afw::detection::Psf::Image) psfImage = psf->computeImage(position);
76  afw::geom::Box2I fitBBox = psfImage->getBBox();
77  fitBBox.clip(exposure.getBBox());
78  if (fitBBox != psfImage->getBBox()) {
79  _flagHandler.setValue(measRecord, FAILURE, true); // if we had a suspect flag, we'd set that instead
80  _flagHandler.setValue(measRecord, EDGE, true);
81  }
82  afw::detection::Footprint fitRegion(fitBBox);
83  if (!_ctrl.badMaskPlanes.empty()) {
84  afw::image::MaskPixel badBits = 0x0;
85  for (
86  std::vector<std::string>::const_iterator i = _ctrl.badMaskPlanes.begin();
87  i != _ctrl.badMaskPlanes.end();
88  ++i
89  ) {
90  badBits |= exposure.getMaskedImage().getMask()->getPlaneBitMask(*i);
91  }
92  fitRegion.intersectMask(*exposure.getMaskedImage().getMask(), badBits);
93  }
94  if (fitRegion.getArea() == 0) {
95  throw LSST_EXCEPT(
99  );
100  }
101  typedef afw::detection::Psf::Pixel PsfPixel;
105  fitRegion,
106  psfImage->getArray(),
107  psfImage->getXY0()
108  )
109  );
112  fitRegion,
113  exposure.getMaskedImage().getImage()->getArray(),
114  exposure.getXY0()
115  )
116  );
119  fitRegion,
120  exposure.getMaskedImage().getVariance()->getArray(),
121  exposure.getXY0()
122  )
123  );
124  PsfPixel alpha = model.matrix().squaredNorm();
125  FluxResult result;
126  result.flux = model.matrix().dot(data.matrix().cast<PsfPixel>()) / alpha;
127  // If we're not using per-pixel weights to compute the flux, we'll still want to compute the
128  // variance as if we had, so we'll apply the weights to the model vector now, and update alpha.
129  result.fluxSigma = std::sqrt(model.square().matrix().dot(variance.matrix().cast<PsfPixel>()))
130  / alpha;
131  if (!utils::isfinite(result.flux) || !utils::isfinite(result.fluxSigma)) {
132  throw LSST_EXCEPT(PixelValueError, "Invalid pixel value detected in image.");
133  }
134  measRecord.set(_fluxResultKey, result);
135 }
136 
138  _flagHandler.handleFailure(measRecord, error);
139 }
140 
142  Control const & ctrl,
143  std::string const & name,
144  afw::table::SchemaMapper & mapper
145 ) :
146  FluxTransform{name, mapper}
147 {
148  for (auto flag = getFlagDefinitions().begin() + 1; flag < getFlagDefinitions().end(); flag++) {
149  mapper.addMapping(mapper.getInputSchema().find<afw::table::Flag>(name + "_" + flag->name).key);
150  }
151 }
152 
153 }}} // namespace lsst::meas::base
Defines the fields and offsets for a table.
Definition: Schema.h:46
std::vector< std::string > badMaskPlanes
&quot;Mask planes that indicate pixels that should be excluded from the fit&quot; ;
Definition: PsfFlux.h:50
void flattenArray(Footprint const &fp, ndarray::Array< T, N, C > const &src, ndarray::Array< U, N-1, D > const &dest, lsst::afw::geom::Point2I const &xy0=lsst::afw::geom::Point2I())
Flatten the first two dimensions of an array.
boost::uint16_t MaskPixel
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:150
ImagePtr getImage(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:869
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
#define PTR(...)
Definition: base.h:41
table::Key< std::string > name
Definition: ApCorrMap.cc:71
Eigen matrix objects that present a view into an ndarray::Array.
Eigen3 view into an ndarray::Array.
Definition: eigen.h:232
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:19
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=NULL) const
Definition: PsfFlux.cc:137
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
lsst::afw::image::VariancePixel VarPixel
Definition: convCUDA.h:44
PixelT Pixel
A pixel in this ImageBase.
Definition: Image.h:133
Matrix alpha
An integer coordinate rectangle.
Definition: Box.h:53
Key< T > addMapping(Key< T > const &inputKey, bool doReplace=false)
Add a new field to the output Schema that is a copy of a field in the input Schema.
VariancePtr getVariance(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s variance.
Definition: MaskedImage.h:890
def error
Definition: log.py:108
PsfFluxTransform(Control const &ctrl, std::string const &name, afw::table::SchemaMapper &mapper)
Definition: PsfFlux.cc:141
FlagDefinition getDefinition(int i) const
Definition: FlagHandler.h:66
Flux flux
Measured flux in DN.
Definition: FluxUtilities.h:38
PsfFluxAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
Definition: PsfFlux.cc:49
void setValue(afw::table::BaseRecord &record, int i, bool value) const
Definition: FlagHandler.h:72
void intersectMask(image::Mask< MaskPixelT > const &mask, MaskPixelT bitmask=~0x0)
Intersect the Footprint with a Mask.
math::Kernel::Pixel Pixel
Pixel type of Image returned by computeImage.
Definition: Psf.h:78
tbl::Schema schema
A FunctorKey for FluxResult.
Definition: FluxUtilities.h:56
A set of pixels in an Image.
Definition: Footprint.h:62
int isfinite(T t)
Definition: ieee.h:100
boost::shared_ptr< lsst::afw::detection::Psf > getPsf()
Return the Exposure&#39;s Psf object.
Definition: Exposure.h:224
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Definition: PsfFlux.cc:63
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
SafeCentroidExtractor _centroidExtractor
Definition: PsfFlux.h:99
MaskPtr getMask(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s mask.
Definition: MaskedImage.h:879
geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
Definition: Exposure.h:194
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition: BaseRecord.h:136
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=NULL) const
Definition: FlagHandler.cc:59
Record class that contains measurements made on a single exposure.
Definition: Source.h:81
A polymorphic base class for representing an image&#39;s Point Spread Function.
Definition: Psf.h:68
FluxErrElement fluxSigma
1-Sigma error (sqrt of variance) on flux in DN.
Definition: FluxUtilities.h:39
A C++ control class to handle PsfFluxAlgorithm&#39;s configuration.
Definition: PsfFlux.h:46
geom::Point2I getXY0() const
Definition: Exposure.h:192
static FlagHandler addFields(afw::table::Schema &schema, std::string const &prefix, FlagDefinition const *begin, FlagDefinition const *end)
Definition: FlagHandler.cc:28
A reusable result struct for flux measurements.
Definition: FluxUtilities.h:37