LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
ApertureFlux.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2014 LSST Corporation.
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 <numeric>
25 
26 #include "ndarray/eigen.h"
27 
30 #include "lsst/afw/table/Source.h"
33 
34 namespace lsst { namespace meas { namespace base {
35 
36 ApertureFluxControl::ApertureFluxControl() : radii(10), maxSincRadius(10.0), shiftKernel("lanczos5") {
37  // defaults here stolen from HSC pipeline defaults
38  static boost::array<double,10> defaultRadii = {{
39  3.0, 4.5, 6.0, 9.0, 12.0, 17.0, 25.0, 35.0, 50.0, 70.0
40  }};
41  std::copy(defaultRadii.begin(), defaultRadii.end(), radii.begin());
42 }
43 
45  Control const & ctrl,
46  std::string const & name,
48  daf::base::PropertySet & metadata
49 
50 ) : _ctrl(ctrl),
51  _centroidExtractor(schema, name),
52  _fluxKey(
53  afw::table::ArrayKey<Flux>::addFields(
54  schema,
55  name + "_flux",
56  "flux within %f pixel aperture",
57  "dn",
58  ctrl.radii
59  )
60  ),
61  _fluxSigmaKey(
62  afw::table::ArrayKey<Flux>::addFields(
63  schema,
64  name + "_fluxSigma",
65  "1-sigma uncertainty on flux within %f pixel aperture",
66  "dn",
67  ctrl.radii
68  )
69  )
70 {
71  for (std::size_t i = 0; i < ctrl.radii.size(); ++i) {
72  metadata.add(name + "_radii", ctrl.radii[i]);
73  }
74 
75  _flagKeys.reserve(ctrl.radii.size());
76  for (std::size_t i = 0; i < ctrl.radii.size(); ++i) {
77  _flagKeys.push_back(FlagKeys(name, schema, i));
78  }
79 
80  static boost::array<FlagDefinition,N_FLAGS> const flagDefs = {{
81  {"flag", "general failure flag, set if anything went wrong"},
82  }};
83  _flagHandler = FlagHandler::addFields(schema, name, flagDefs.begin(), flagDefs.end());
84 }
85 
87  std::string const & name, afw::table::Schema & schema, int index
88 ) :
89  failed(
90  schema.addField<afw::table::Flag>(
91  (boost::format("%s_flag_%d") % name % index).str(),
92  (boost::format("flag set if aperture %d failed for any reason") % index).str()
93  )
94  ),
95  apertureTruncated(
96  schema.addField<afw::table::Flag>(
97  (boost::format("%s_flag_apertureTruncated_%d") % name % index).str(),
98  (boost::format("flag set if aperture %d did not fit within the measurement image") % index).str()
99  )
100  ),
101  sincCoeffsTruncated(
102  schema.addField<afw::table::Flag>(
103  (boost::format("%s_flag_sincCoeffsTruncated_%d") % name % index).str(),
104  (boost::format("flag set if the full sinc coefficient image for aperture %d did not "
105  "fit within the measurement image") % index).str()
106  )
107  )
108 {}
109 
111  Result const & result,
113  int index
114 ) const {
115  record.set(_fluxKey[index], result.flux);
116  record.set(_fluxSigmaKey[index], result.fluxSigma);
117  if (result.getFlag(APERTURE_TRUNCATED)) {
118  record.set(_flagKeys[index].apertureTruncated, true);
119  record.set(_flagKeys[index].failed, true);
120  }
121  if (result.getFlag(SINC_COEFFS_TRUNCATED)) {
122  record.set(_flagKeys[index].sincCoeffsTruncated, true);
123  // TODO DM-464: set suspect flag
124  }
125 }
126 
127 
129  afw::table::SourceRecord & measRecord,
130  afw::image::Exposure<float> const & exposure
131 ) const {};
132 
134  _flagHandler.handleFailure(measRecord, error);
135 }
136 
137 namespace {
138 
139 // Helper function for computeSincFlux get Sinc flux coefficients, and handle cases where the coeff
140 // image needs to be clipped to fit in the measurement image
141 template <typename T>
142 CONST_PTR(afw::image::Image<T>) getSincCoeffs(
143  afw::geom::Box2I const & bbox, // measurement image bbox we need to fit inside
144  afw::geom::ellipses::Ellipse const & ellipse, // ellipse that defines the aperture
145  ApertureFluxAlgorithm::Result & result, // result object where we set flags if we do clip
146  ApertureFluxAlgorithm::Control const & ctrl // configuration
147 ) {
148  CONST_PTR(afw::image::Image<T>) cImage = SincCoeffs<T>::get(ellipse.getCore(), 0.0);
149  cImage = afw::math::offsetImage(
150  *cImage,
151  ellipse.getCenter().getX(),
152  ellipse.getCenter().getY(),
153  ctrl.shiftKernel
154  );
155  if (!bbox.contains(cImage->getBBox())) {
156  // We had to clip out at least part part of the coeff image,
157  // but since that's much larger than the aperture (and close
158  // to zero outside the aperture), it may not be a serious
159  // problem.
161  afw::geom::Box2I overlap = cImage->getBBox();
162  overlap.clip(bbox);
163  if (!overlap.contains(afw::geom::Box2I(ellipse.computeBBox()))) {
164  // The clipping was indeed serious, as we we did have to clip within
165  // the aperture; can't expect any decent answer at this point.
167  }
168  cImage = boost::make_shared< afw::image::Image<T> >(*cImage, overlap);
169  }
170  return cImage;
171 }
172 
173 } // anonymous
174 
175 template <typename T>
177  afw::image::Image<T> const & image,
178  afw::geom::ellipses::Ellipse const & ellipse,
179  Control const & ctrl
180 ) {
181  Result result;
182  CONST_PTR(afw::image::Image<T>) cImage = getSincCoeffs<T>(image.getBBox(), ellipse, result, ctrl);
183  if (result.getFlag(APERTURE_TRUNCATED)) return result;
184  afw::image::Image<T> subImage(image, cImage->getBBox());
185  result.flux = (subImage.getArray().template asEigen<Eigen::ArrayXpr>()
186  * cImage->getArray().template asEigen<Eigen::ArrayXpr>()).sum();
187  return result;
188 }
189 
190 template <typename T>
193  afw::geom::ellipses::Ellipse const & ellipse,
194  Control const & ctrl
195 ) {
196  Result result;
197  CONST_PTR(afw::image::Image<T>) cImage = getSincCoeffs<T>(image.getBBox(), ellipse, result, ctrl);
198  if (result.getFlag(APERTURE_TRUNCATED)) return result;
200  result.flux = (subImage.getImage()->getArray().template asEigen<Eigen::ArrayXpr>()
201  * cImage->getArray().template asEigen<Eigen::ArrayXpr>()).sum();
202  result.fluxSigma = std::sqrt(
203  (subImage.getVariance()->getArray().template asEigen<Eigen::ArrayXpr>().template cast<T>()
204  * cImage->getArray().template asEigen<Eigen::ArrayXpr>().square()).sum()
205  );
206  return result;
207 }
208 
209 template <typename T>
211  afw::image::Image<T> const & image,
212  afw::geom::ellipses::Ellipse const & ellipse,
213  Control const & ctrl
214 ) {
215  Result result;
216  afw::geom::ellipses::PixelRegion region(ellipse); // behaves mostly like a Footprint
217  if (!image.getBBox().contains(region.getBBox())) {
218  result.setFlag(APERTURE_TRUNCATED);
219  return result;
220  }
221  result.flux = 0;
222  for (
223  afw::geom::ellipses::PixelRegion::Iterator spanIter = region.begin(), spanEnd = region.end();
224  spanIter != spanEnd;
225  ++spanIter
226  ) {
227  typename afw::image::Image<T>::x_iterator pixIter = image.x_at(
228  spanIter->getBeginX() - image.getX0(),
229  spanIter->getY() - image.getY0()
230  );
231  result.flux += std::accumulate(pixIter, pixIter + spanIter->getWidth(), 0.0);
232  }
233  return result;
234 }
235 
236 template <typename T>
239  afw::geom::ellipses::Ellipse const & ellipse,
240  Control const & ctrl
241 ) {
242  Result result;
243  afw::geom::ellipses::PixelRegion region(ellipse); // behaves mostly like a Footprint
244  if (!image.getBBox().contains(region.getBBox())) {
245  result.setFlag(APERTURE_TRUNCATED);
246  return result;
247  }
248  result.flux = 0.0;
249  result.fluxSigma = 0.0;
250  for (
251  afw::geom::ellipses::PixelRegion::Iterator spanIter = region.begin(), spanEnd = region.end();
252  spanIter != spanEnd;
253  ++spanIter
254  ) {
255  typename afw::image::MaskedImage<T>::Image::x_iterator pixIter = image.getImage()->x_at(
256  spanIter->getBeginX() - image.getX0(),
257  spanIter->getY() - image.getY0()
258  );
259  typename afw::image::MaskedImage<T>::Variance::x_iterator varIter = image.getVariance()->x_at(
260  spanIter->getBeginX() - image.getX0(),
261  spanIter->getY() - image.getY0()
262  );
263  result.flux += std::accumulate(pixIter, pixIter + spanIter->getWidth(), 0.0);
264  // we use this to hold variance as we accumulate...
265  result.fluxSigma += std::accumulate(varIter, varIter + spanIter->getWidth(), 0.0);
266  }
267  result.fluxSigma = std::sqrt(result.fluxSigma); // ...and switch back to sigma here.
268  return result;
269 }
270 
271 template <typename T>
273  afw::image::Image<T> const & image,
274  afw::geom::ellipses::Ellipse const & ellipse,
275  Control const & ctrl
276 ) {
277  return (afw::geom::ellipses::Axes(ellipse.getCore()).getB() <= ctrl.maxSincRadius)
278  ? computeSincFlux(image, ellipse, ctrl)
279  : computeNaiveFlux(image, ellipse, ctrl);
280 }
281 
282 template <typename T>
285  afw::geom::ellipses::Ellipse const & ellipse,
286  Control const & ctrl
287 ) {
288  return (afw::geom::ellipses::Axes(ellipse.getCore()).getB() <= ctrl.maxSincRadius)
289  ? computeSincFlux(image, ellipse, ctrl)
290  : computeNaiveFlux(image, ellipse, ctrl);
291 }
292 #define INSTANTIATE(T) \
293  template \
294  ApertureFluxAlgorithm::Result ApertureFluxAlgorithm::computeFlux( \
295  afw::image::Image<T> const &, \
296  afw::geom::ellipses::Ellipse const &, \
297  Control const & \
298  ); \
299  template \
300  ApertureFluxAlgorithm::Result ApertureFluxAlgorithm::computeFlux( \
301  afw::image::MaskedImage<T> const &, \
302  afw::geom::ellipses::Ellipse const &, \
303  Control const & \
304  ); \
305  template \
306  ApertureFluxAlgorithm::Result ApertureFluxAlgorithm::computeSincFlux( \
307  afw::image::Image<T> const &, \
308  afw::geom::ellipses::Ellipse const &, \
309  Control const & \
310  ); \
311  template \
312  ApertureFluxAlgorithm::Result ApertureFluxAlgorithm::computeSincFlux( \
313  afw::image::MaskedImage<T> const &, \
314  afw::geom::ellipses::Ellipse const &, \
315  Control const & \
316  ); \
317  template \
318  ApertureFluxAlgorithm::Result ApertureFluxAlgorithm::computeNaiveFlux( \
319  afw::image::Image<T> const &, \
320  afw::geom::ellipses::Ellipse const &, \
321  Control const & \
322  ); \
323  template \
324  ApertureFluxAlgorithm::Result ApertureFluxAlgorithm::computeNaiveFlux( \
325  afw::image::MaskedImage<T> const &, \
326  afw::geom::ellipses::Ellipse const &, \
327  Control const & \
328  )
329 
330 INSTANTIATE(float);
331 INSTANTIATE(double);
332 
333 }}} // namespace lsst::meas::base
334 
Defines the fields and offsets for a table.
Definition: Schema.h:46
void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=NULL) const
FlagKeys(std::string const &name, afw::table::Schema &schema, int index)
Definition: ApertureFlux.cc:86
Eigen matrix objects that present a view into an ndarray::Array.
x_iterator x_at(int x, int y) const
Return an x_iterator to the point (x, y) in the image.
Definition: Image.h:329
bool contains(Point2I const &point) const
Return true if the box contains the point.
A class to contain the data, WCS, and other information needed to describe an image of the sky...
Definition: Exposure.h:48
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: Image.h:151
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
Definition: eigen.h:390
geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
Definition: MaskedImage.h:905
ApertureFluxAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema, daf::base::PropertySet &metadata)
Definition: ApertureFlux.cc:44
afw::table::ArrayKey< FluxErrElement > _fluxSigmaKey
Definition: ApertureFlux.h:234
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: Image.h:377
ImagePtr getImage(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:869
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
void copyResultToRecord(Result const &result, afw::table::SourceRecord &record, int index) const
int getX0() const
Definition: Image.h:247
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:890
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
afw::table::ArrayKey< Flux > _fluxKey
Definition: ApertureFlux.h:233
tbl::Schema schema
Definition: CoaddPsf.cc:324
static Result computeFlux(afw::image::Image< T > const &image, afw::geom::ellipses::Ellipse const &ellipse, Control const &ctrl=Control())
An ellipse defined by an arbitrary BaseCore and a center point.
Definition: Ellipse.h:50
double maxSincRadius
&quot;Maximum radius (in pixels) for which the sinc algorithm should be used instead of the &quot; &quot;faster naiv...
Definition: ApertureFlux.h:55
Box2I const & getBBox() const
Definition: PixelRegion.h:45
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:77
void setFlag(ApertureFluxAlgorithm::ResultFlagBits bit, bool value=true)
Set the flag value associated with the given bit.
Definition: ApertureFlux.h:243
static Result computeNaiveFlux(afw::image::Image< T > const &image, afw::geom::ellipses::Ellipse const &ellipse, Control const &ctrl=Control())
Flux flux
Measured flux in DN.
Definition: FluxUtilities.h:37
BaseCore const & getCore() const
Return the ellipse core.
Definition: Ellipse.h:75
double sum
Definition: NaiveFlux.cc:137
#define CONST_PTR(...)
Definition: base.h:47
bool getFlag(ApertureFluxAlgorithm::ResultFlagBits bit) const
Return the flag value associated with the given bit.
Definition: ApertureFlux.h:240
ImageT::Ptr offsetImage(ImageT const &image, float dx, float dy, std::string const &algorithmName="lanczos5", unsigned int buffer=0)
Return an image offset by (dx, dy) using the specified algorithm.
Definition: offsetImage.cc:55
virtual void measure(afw::table::SourceRecord &record, afw::image::Exposure< float > const &exposure) const
An ellipse core for the semimajor/semiminor axis and position angle parametrization (a...
Definition: Axes.h:45
ApertureFluxControl Control
Typedef to the control object associated with this algorithm, defined above.
Definition: ApertureFlux.h:105
#define INSTANTIATE(T)
Class for storing generic metadata.
Definition: PropertySet.h:82
static Result computeSincFlux(afw::image::Image< T > const &image, afw::geom::ellipses::Ellipse const &ellipse, Control const &ctrl=Control())
int getWidth() const
Return the number of columns in the image.
Definition: MaskedImage.h:901
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
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
std::vector< double > radii
&quot;Radius (in pixels) of apertures.&quot; ;
Definition: ApertureFlux.h:49
void add(std::string const &name, T const &value)
Definition: PropertySet.cc:619
FluxErrElement fluxSigma
1-Sigma error (sqrt of variance) on flux in DN.
Definition: FluxUtilities.h:38
int getWidth() const
Return the number of columns in the image.
Definition: Image.h:237
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:415
ApertureFluxResult Result
Result object returned by static methods.
Definition: ApertureFlux.h:108
static FlagHandler addFields(afw::table::Schema &schema, std::string const &prefix, FlagDefinition const *begin, FlagDefinition const *end)
Definition: FlagHandler.cc:28
afw::table::SourceRecord * record
int getY0() const
Definition: Image.h:255