lsst.meas.base  14.0-22-gcfb0d17
LocalBackground.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2018 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 
28 #include "lsst/afw/table/Source.h"
29 #include "lsst/log/Log.h"
30 #include "lsst/afw/geom/SpanSet.h"
31 #include "lsst/afw/detection/Psf.h"
33 
34 namespace lsst { namespace meas { namespace base {
35 namespace {
36 FlagDefinitionList flagDefinitions;
37 } // end anonymous
38 
39 FlagDefinition const LocalBackgroundAlgorithm::FAILURE = flagDefinitions.addFailureFlag();
40 FlagDefinition const LocalBackgroundAlgorithm::NO_GOOD_PIXELS = flagDefinitions.add("flag_noGoodPixels",
41  "no good pixels in the annulus");
42 FlagDefinition const LocalBackgroundAlgorithm::NO_PSF = flagDefinitions.add("flag_noPsf",
43  "no PSF provided");
44 
46  return flagDefinitions;
47 }
48 
49 
51  Control const & ctrl,
52  std::string const & name,
53  afw::table::Schema & schema,
54  std::string const & logName
55 ) : _ctrl(ctrl),
56  _resultKey(FluxResultKey::addFields(schema, name, "background in annulus around source")),
57  _flagHandler(FlagHandler::addFields(schema, name, getFlagDefinitions())),
58  _centroidExtractor(schema, name),
59  _stats(ctrl.bgRej, ctrl.bgIter)
60 {
61  _logName = logName.size() ? logName : name;
62 }
63 
64 
65 void
67  afw::table::SourceRecord & measRecord,
68  afw::image::Exposure<float> const & exposure
69 ) const {
70  afw::geom::Point2D const center = _centroidExtractor(measRecord, _flagHandler);
73  afw::image::MaskPixel const badMask = mask.getPlaneBitMask(_ctrl.badMaskPlanes);
74 
75  // Define pixels in annulus
76  auto const psf = exposure.getPsf();
77  if (!psf) {
79  }
80  float const psfSigma = psf->computeShape().getDeterminantRadius();
81 
82  float const innerRadius = _ctrl.annulusInner*psfSigma;
83  afw::geom::ellipses::Axes const innerCircle{innerRadius, innerRadius};
84  auto const& inner = afw::geom::SpanSet::fromShape(afw::geom::ellipses::Ellipse(innerCircle, center));
85 
86  float const outerRadius = _ctrl.annulusOuter*psfSigma;
87  afw::geom::ellipses::Axes const outerCircle{outerRadius, outerRadius};
88  auto const& outer = afw::geom::SpanSet::fromShape(afw::geom::ellipses::Ellipse(outerCircle, center));
89 
90  auto const& annulus = outer->clippedTo(image.getBBox())->intersectNot(*inner);
91  auto const& imageValues = annulus->flatten(image.getImage()->getArray(), image.getXY0());
92  auto const& maskValues = annulus->flatten(image.getMask()->getArray(), image.getXY0());
93 
94  // Extract from ndarray::Array into std::vector because of limitations in afw::math::makeStatistics
95  std::vector<float> values;
96  values.reserve(imageValues.getNumElements());
97  assert(imageValues.getNumElements() == maskValues.getNumElements()); // constructed from the same spans
98  auto maskIter = maskValues.begin();
99  for (auto imageIter = imageValues.begin(); imageIter != imageValues.end(); ++imageIter, ++maskIter) {
100  if ((*maskIter & badMask) == 0) {
101  values.push_back(*imageIter);
102  }
103  }
104 
105  if (values.size() == 0) {
107  }
108 
109  // Measure the background
110  auto const stats = afw::math::makeStatistics(values, afw::math::MEANCLIP | afw::math::STDEVCLIP, _stats);
111  FluxResult const result(stats.getValue(afw::math::MEANCLIP), stats.getValue(afw::math::STDEVCLIP));
112  measRecord.set(_resultKey, result);
113 }
114 
115 
117  _flagHandler.handleFailure(measRecord, error);
118 }
119 
120 
122  Control const & ctrl,
123  std::string const & name,
124  afw::table::SchemaMapper & mapper
125 ) :
126  FluxTransform{name, mapper}
127 {
130  if (flag == LocalBackgroundAlgorithm::FAILURE) continue;
131  if (mapper.getInputSchema().getNames().count(
132  mapper.getInputSchema().join(name, flag.name)) == 0) continue;
133  afw::table::Key<afw::table::Flag> key = mapper.getInputSchema().find<afw::table::Flag>(
134  name + "_" + flag.name).key;
135  mapper.addMapping(key);
136  }
137 }
138 
139 
140 }}} // namespace lsst::meas::base
std::size_t size() const
return the current size (number of defined elements) of the collection
Definition: FlagHandler.h:133
Configuration of LocalBackgroundAlgorithm.
afw::table::Key< afw::table::Array< ImagePixelT > > image
static std::shared_ptr< geom::SpanSet > fromShape(int r, Stencil s=Stencil::CIRCLE, Point2I offset=Point2I())
afw::table::Key< afw::table::Array< MaskPixelT > > mask
Simple class used to define and document flags The name and doc constitute the identity of the FlagDe...
Definition: FlagHandler.h:38
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Called to measure a single child source in an image.
float annulusInner
"Inner radius for background annulus as a multiple of the PSF sigma" ;
std::vector< std::string > badMaskPlanes
"Mask planes that indicate pixels that should be excluded from the measurement" ; ...
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
Base for flux measurement transformations.
string name
STL class.
Utility class for handling flag fields that indicate the failure modes of an algorithm.
Definition: FlagHandler.h:156
T push_back(T... args)
float annulusOuter
"Outer radius for background annulus as a multiple of the PSF sigma" ;
MaskedImageT getMaskedImage()
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
static MaskPixelT getPlaneBitMask(const std::vector< std::string > &names)
static FlagDefinition const NO_GOOD_PIXELS
A FunctorKey for FluxResult.
Definition: FluxUtilities.h:58
LocalBackgroundTransform(Control const &ctrl, std::string const &name, afw::table::SchemaMapper &mapper)
static FlagDefinitionList const & getFlagDefinitions()
T size(T... args)
#define LSST_EXCEPT(type,...)
STL class.
LocalBackgroundAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema, std::string const &logName="")
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=nullptr) const
Handle an exception thrown by the current algorithm by setting flags in the given record...
std::shared_ptr< lsst::afw::detection::Psf > getPsf()
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=nullptr) const
Handle an expected or unexpected Exception thrown by a measurement algorithm.
Definition: FlagHandler.cc:93
void set(Key< T > const &key, U const &value)
vector-type utility class to build a collection of FlagDefinitions
Definition: FlagHandler.h:63
A reusable result struct for flux measurements.
Definition: FluxUtilities.h:39
T reserve(T... args)
geom::Point2I getXY0() const