LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
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 <array>
25 #include <cmath>
26 
27 #include "ndarray/eigen.h"
28 
29 #include "lsst/afw/table/Source.h"
30 #include "lsst/afw/detection/Psf.h"
32 #include "lsst/afw/detection/FootprintArray.cc"
33 #include "lsst/meas/base/PsfFlux.h"
34 
35 namespace lsst { namespace meas { namespace base {
36 
37 namespace {
38 
39 std::array<FlagDefinition,PsfFluxAlgorithm::N_FLAGS> const & getFlagDefinitions() {
40  static std::array<FlagDefinition,PsfFluxAlgorithm::N_FLAGS> const flagDefs = {{
41  {"flag", "general failure flag"},
42  {"flag_noGoodPixels", "not enough non-rejected pixels in data to attempt the fit"},
43  {"flag_edge", "object was too close to the edge of the image to use the full PSF model"}
44  }};
45  return flagDefs;
46 }
47 
48 } // anonymous
49 
51  Control const & ctrl,
52  std::string const & name,
54 ) : _ctrl(ctrl),
55  _fluxResultKey(
56  FluxResultKey::addFields(schema, name, "flux derived from linear least-squares fit of PSF model")
57  ),
58  _centroidExtractor(schema, name)
59 {
60  _flagHandler = FlagHandler::addFields(schema, name,
61  getFlagDefinitions().begin(), getFlagDefinitions().end());
62 }
63 
65  afw::table::SourceRecord & measRecord,
66  afw::image::Exposure<float> const & exposure
67 ) const {
68  PTR(afw::detection::Psf const) psf = exposure.getPsf();
69  if (!psf) {
70  throw LSST_EXCEPT(
71  FatalAlgorithmError,
72  "PsfFlux algorithm requires a Psf with every exposure"
73  );
74  }
75  afw::geom::Point2D position = _centroidExtractor(measRecord, _flagHandler);
76  PTR(afw::detection::Psf::Image) psfImage = psf->computeImage(position);
77  afw::geom::Box2I fitBBox = psfImage->getBBox();
78  fitBBox.clip(exposure.getBBox());
79  if (fitBBox != psfImage->getBBox()) {
80  _flagHandler.setValue(measRecord, FAILURE, true); // if we had a suspect flag, we'd set that instead
81  _flagHandler.setValue(measRecord, EDGE, true);
82  }
83  afw::detection::Footprint fitRegion(fitBBox);
84  if (!_ctrl.badMaskPlanes.empty()) {
85  afw::image::MaskPixel badBits = 0x0;
86  for (
87  std::vector<std::string>::const_iterator i = _ctrl.badMaskPlanes.begin();
88  i != _ctrl.badMaskPlanes.end();
89  ++i
90  ) {
91  badBits |= exposure.getMaskedImage().getMask()->getPlaneBitMask(*i);
92  }
93  fitRegion.intersectMask(*exposure.getMaskedImage().getMask(), badBits);
94  }
95  if (fitRegion.getArea() == 0) {
96  throw LSST_EXCEPT(
100  );
101  }
102  typedef afw::detection::Psf::Pixel PsfPixel;
104  ndarray::EigenView<PsfPixel,1,1,Eigen::ArrayXpr> model(
106  fitRegion,
107  psfImage->getArray(),
108  psfImage->getXY0()
109  )
110  );
111  ndarray::EigenView<float,1,1,Eigen::ArrayXpr> data(
113  fitRegion,
114  exposure.getMaskedImage().getImage()->getArray(),
115  exposure.getXY0()
116  )
117  );
118  ndarray::EigenView<VarPixel,1,1,Eigen::ArrayXpr> variance(
120  fitRegion,
121  exposure.getMaskedImage().getVariance()->getArray(),
122  exposure.getXY0()
123  )
124  );
125  PsfPixel alpha = model.matrix().squaredNorm();
126  FluxResult result;
127  result.flux = model.matrix().dot(data.matrix().cast<PsfPixel>()) / alpha;
128  // If we're not using per-pixel weights to compute the flux, we'll still want to compute the
129  // variance as if we had, so we'll apply the weights to the model vector now, and update alpha.
130  result.fluxSigma = std::sqrt(model.square().matrix().dot(variance.matrix().cast<PsfPixel>()))
131  / alpha;
132  if (!std::isfinite(result.flux) || !std::isfinite(result.fluxSigma)) {
133  throw LSST_EXCEPT(PixelValueError, "Invalid pixel value detected in image.");
134  }
135  measRecord.set(_fluxResultKey, result);
136 }
137 
139  _flagHandler.handleFailure(measRecord, error);
140 }
141 
143  Control const & ctrl,
144  std::string const & name,
145  afw::table::SchemaMapper & mapper
146 ) :
147  FluxTransform{name, mapper}
148 {
149  for (auto flag = getFlagDefinitions().begin() + 1; flag < getFlagDefinitions().end(); flag++) {
150  mapper.addMapping(mapper.getInputSchema().find<afw::table::Flag>(name + "_" + flag->name).key);
151  }
152 }
153 
154 }}} // namespace lsst::meas::base
Defines the fields and offsets for a table.
Definition: Schema.h:44
std::vector< std::string > badMaskPlanes
&quot;Mask planes that indicate pixels that should be excluded from the fit&quot; ;
Definition: PsfFlux.h:50
std::uint16_t MaskPixel
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.
geom::Point2I getXY0() const
Return the Exposure&#39;s origin.
Definition: Exposure.h:195
geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
Definition: Exposure.h:197
table::Key< std::string > name
Definition: ApCorrMap.cc:71
afw::table::Schema schema
Definition: GaussianPsf.cc:41
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:19
PixelT Pixel
A pixel in this ImageBase.
Definition: Image.h:132
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=NULL) const
Handle an exception thrown by the current algorithm by setting flags in the given record...
Definition: PsfFlux.cc:138
ImagePtr getImage(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:875
std::array< FlagDefinition, BlendednessAlgorithm::N_FLAGS > const & getFlagDefinitions()
Definition: Blendedness.cc:215
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition: BaseRecord.h:145
Base for flux measurement transformations.
FlagDefinition getDefinition(std::size_t i) const
Return the FlagDefinition object that corresponds to a particular enum value.
Definition: FlagHandler.h:172
Matrix alpha
An integer coordinate rectangle.
Definition: Box.h:53
VariancePtr getVariance(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s variance.
Definition: MaskedImage.h:896
def error
Definition: log.py:103
PsfFluxTransform(Control const &ctrl, std::string const &name, afw::table::SchemaMapper &mapper)
Definition: PsfFlux.cc:142
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:153
math::Kernel::Pixel Pixel
Pixel type of Image returned by computeImage.
Definition: Psf.h:78
MaskPtr getMask(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s mask.
Definition: MaskedImage.h:885
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:50
void setValue(afw::table::BaseRecord &record, std::size_t i, bool value) const
Set the flag field corresponding to the given enum value.
Definition: FlagHandler.h:188
A FunctorKey for FluxResult.
Definition: FluxUtilities.h:56
A set of pixels in an Image.
Definition: Footprint.h:62
void intersectMask(image::Mask< MaskPixelT > const &mask, MaskPixelT bitmask=~0x0)
Intersect the Footprint with a Mask.
boost::shared_ptr< lsst::afw::detection::Psf > getPsf()
Return the Exposure&#39;s Psf object.
Definition: Exposure.h:227
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Called to measure a single child source in an image.
Definition: PsfFlux.cc:64
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
SafeCentroidExtractor _centroidExtractor
Definition: PsfFlux.h:99
#define PTR(...)
Definition: base.h:41
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=NULL) const
Handle an expected or unexpected Exception thrown by a measurement algorithm.
Definition: FlagHandler.cc:77
Record class that contains measurements made on a single exposure.
Definition: Source.h:80
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
static FlagHandler addFields(afw::table::Schema &schema, std::string const &prefix, FlagDefinition const *begin, FlagDefinition const *end)
Add Flag fields to a schema, creating a FlagHandler object to manage them.
Definition: FlagHandler.cc:28
A reusable result struct for flux measurements.
Definition: FluxUtilities.h:37
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.