LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
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::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
Definition: Exposure.h:190
geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
Definition: Exposure.h:192
FlagDefinition getDefinition(std::size_t i) const
Definition: FlagHandler.h:172
table::Key< std::string > name
Definition: ApCorrMap.cc:71
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.
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=NULL) const
Definition: PsfFlux.cc:138
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:133
SafeCentroidExtractor _centroidExtractor
Definition: PsfFlux.h:99
Flux flux
Measured flux in DN.
Definition: FluxUtilities.h:38
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 handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=NULL) const
Definition: FlagHandler.cc:77
Matrix alpha
An integer coordinate rectangle.
Definition: Box.h:53
FluxErrElement fluxSigma
1-Sigma error (sqrt of variance) on flux in DN.
Definition: FluxUtilities.h:39
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
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:148
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
A FunctorKey for FluxResult.
Definition: FluxUtilities.h:56
A set of pixels in an Image.
Definition: Footprint.h:62
std::vector< std::string > badMaskPlanes
&quot;Mask planes that indicate pixels that should be excluded from the fit&quot; ;
Definition: PsfFlux.h:50
PsfFluxTransform(Control const &ctrl, std::string const &name, afw::table::SchemaMapper &mapper)
Definition: PsfFlux.cc:142
PsfFluxAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
Definition: PsfFlux.cc:50
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:222
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
static FlagHandler addFields(afw::table::Schema &schema, std::string const &prefix, FlagDefinition const *begin, FlagDefinition const *end)
Definition: FlagHandler.cc:28
#define PTR(...)
Definition: base.h:41
lsst::afw::image::VariancePixel VarPixel
Definition: convCUDA.h:44
void setValue(afw::table::BaseRecord &record, std::size_t i, bool value) const
Definition: FlagHandler.h:188
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition: BaseRecord.h:145
Record class that contains measurements made on a single exposure.
Definition: Source.h:80
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
A polymorphic base class for representing an image&#39;s Point Spread Function.
Definition: Psf.h:68
A C++ control class to handle PsfFluxAlgorithm&#39;s configuration.
Definition: PsfFlux.h:46
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Definition: PsfFlux.cc:64
A reusable result struct for flux measurements.
Definition: FluxUtilities.h:37