lsst.afw  13.0-98-g5a654715e+2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Statistics.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #if !defined(LSST_AFW_MATH_STATISTICS_H)
26 #define LSST_AFW_MATH_STATISTICS_H
27 
37 #include <algorithm>
38 #include <cassert>
39 #include <limits>
40 #include "boost/iterator/iterator_adaptor.hpp"
41 #include "boost/tuple/tuple.hpp"
42 #include <memory>
45 
46 namespace lsst {
47 namespace afw {
48 namespace image {
49 template <typename>
50 class Image;
51 template <typename, typename, typename>
52 class MaskedImage;
53 }
54 namespace math {
55 template <typename>
56 class MaskedVector; // forward declaration
57 
58 typedef lsst::afw::image::VariancePixel WeightPixel; // Type used for weights
59 
63 enum Property {
64  NOTHING = 0x0,
65  ERRORS = 0x1,
66  NPOINT = 0x2,
67  MEAN = 0x4,
68  STDEV = 0x8,
69  VARIANCE = 0x10,
70  MEDIAN = 0x20,
71  IQRANGE = 0x40,
72  MEANCLIP = 0x80,
73  STDEVCLIP = 0x100,
74  VARIANCECLIP = 0x200,
75  MIN = 0x400,
77  MAX = 0x800,
78  SUM = 0x1000,
79  MEANSQUARE = 0x2000,
80  ORMASK = 0x4000
81 };
83 Property stringToStatisticsProperty(std::string const property);
84 
92 public:
93  enum WeightsBoolean { WEIGHTS_FALSE = 0, WEIGHTS_TRUE = 1, WEIGHTS_NONE }; // initial state is NONE
94 
95  StatisticsControl(double numSigmaClip = 3.0,
96  int numIter = 3,
98  0x0,
99  bool isNanSafe = true,
100  WeightsBoolean useWeights =
101  WEIGHTS_NONE
102  )
103  : _numSigmaClip(numSigmaClip),
104  _numIter(numIter),
105  _andMask(andMask),
106  _noGoodPixelsMask(0x0),
107  _isNanSafe(isNanSafe),
108  _useWeights(useWeights),
109  _calcErrorFromInputVariance(false),
110  _maskPropagationThresholds() {
111  try {
112  _noGoodPixelsMask = lsst::afw::image::Mask<>::getPlaneBitMask("NO_DATA");
113  } catch (lsst::pex::exceptions::InvalidParameterError) {
114  ; // Mask has no NO_DATA plane defined
115  }
116 
117  assert(_numSigmaClip > 0);
118  assert(_numIter > 0);
119  }
120 
122 
127  double getMaskPropagationThreshold(int bit) const;
128  void setMaskPropagationThreshold(int bit, double threshold);
130 
131  double getNumSigmaClip() const { return _numSigmaClip; }
132  int getNumIter() const { return _numIter; }
133  int getAndMask() const { return _andMask; }
134  int getNoGoodPixelsMask() const { return _noGoodPixelsMask; }
135  bool getNanSafe() const { return _isNanSafe; }
136  bool getWeighted() const { return _useWeights == WEIGHTS_TRUE ? true : false; }
137  bool getWeightedIsSet() const { return _useWeights != WEIGHTS_NONE ? true : false; }
138  bool getCalcErrorFromInputVariance() const { return _calcErrorFromInputVariance; }
139 
140  void setNumSigmaClip(double numSigmaClip) {
141  assert(numSigmaClip > 0);
142  _numSigmaClip = numSigmaClip;
143  }
144  void setNumIter(int numIter) {
145  assert(numIter > 0);
146  _numIter = numIter;
147  }
148  void setAndMask(int andMask) { _andMask = andMask; }
149  void setNoGoodPixelsMask(int noGoodPixelsMask) { _noGoodPixelsMask = noGoodPixelsMask; }
150  void setNanSafe(bool isNanSafe) { _isNanSafe = isNanSafe; }
151  void setWeighted(bool useWeights) { _useWeights = useWeights ? WEIGHTS_TRUE : WEIGHTS_FALSE; }
152  void setCalcErrorFromInputVariance(bool calcErrorFromInputVariance) {
153  _calcErrorFromInputVariance = calcErrorFromInputVariance;
154  }
155 
156 private:
157  friend class Statistics;
158 
159  double _numSigmaClip; // Number of standard deviations to clip at
160  int _numIter; // Number of iterations
161  int _andMask; // and-Mask to specify which mask planes to ignore
162  int _noGoodPixelsMask; // mask to set if no values are acceptable
163  bool _isNanSafe; // Check for NaNs & Infs before running (slower)
164  WeightsBoolean _useWeights; // Calculate weighted statistics (enum because of 3-valued logic)
165  bool _calcErrorFromInputVariance; // Calculate errors from the input variances, if available
166  std::vector<double> _maskPropagationThresholds; // Thresholds for when to propagate mask bits,
167  // treated like a dict (unset bits are set to 1.0)
168 };
169 
213 class Statistics {
214 public:
216  typedef std::pair<double, double> Value;
217 
230  template <typename ImageT, typename MaskT, typename VarianceT>
231  explicit Statistics(ImageT const &img, MaskT const &msk, VarianceT const &var, int const flags,
232  StatisticsControl const &sctrl = StatisticsControl());
233 
242  template <typename ImageT, typename MaskT, typename VarianceT, typename WeightT>
243  explicit Statistics(ImageT const &img, MaskT const &msk, VarianceT const &var, WeightT const &weights,
244  int const flags, StatisticsControl const &sctrl = StatisticsControl());
245 
259  Value getResult(Property const prop = NOTHING) const;
260 
268  double getError(Property const prop = NOTHING) const;
274  double getValue(Property const prop = NOTHING) const;
275  lsst::afw::image::MaskPixel getOrMask() const { return _allPixelOrMask; }
276 
277 private:
278  long _flags; // The desired calculation
279 
280  int _n; // number of pixels in the image
281  Value _mean; // the image's mean
282  Value _variance; // the image's variance
283  double _min; // the image's minimum
284  double _max; // the image's maximum
285  double _sum; // the sum of all the image's pixels
286  Value _meanclip; // the image's N-sigma clipped mean
287  Value _varianceclip; // the image's N-sigma clipped variance
288  Value _median; // the image's median
289  double _iqrange; // the image's interquartile range
290  lsst::afw::image::MaskPixel _allPixelOrMask; // the 'or' of all masked pixels
291 
292  StatisticsControl _sctrl; // the control structure
293  bool _weightsAreMultiplicative; // Multiply by weights rather than dividing by them
294 
303  template <typename ImageT, typename MaskT, typename VarianceT, typename WeightT>
304  void doStatistics(ImageT const &img, MaskT const &msk, VarianceT const &var, WeightT const &weights,
305  int const flags, StatisticsControl const &sctrl);
306 };
307 
308 /* ************************************ The factory functions ********************************* */
313 template <typename ValueT>
314 class infinite_iterator : public boost::iterator_adaptor<infinite_iterator<ValueT>, const ValueT *,
315  const ValueT, boost::forward_traversal_tag> {
316 public:
317  infinite_iterator() : infinite_iterator::iterator_adaptor_(0) {}
318  explicit infinite_iterator(const ValueT *p) : infinite_iterator::iterator_adaptor_(p) {}
319 
320 private:
322  void increment() { ; } // never actually advance the iterator
323 };
328 template <typename ValueT>
330 public:
332  explicit MaskImposter(ValueT val = 0) { _val[0] = val; }
333  x_iterator row_begin(int) const { return x_iterator(_val); }
334 
335 private:
336  ValueT _val[1];
337 };
338 
343 template <typename Pixel>
345  lsst::afw::image::Mask<image::MaskPixel> const &msk, int const flags,
346  StatisticsControl const &sctrl = StatisticsControl()) {
348  return Statistics(img, msk, var, flags, sctrl);
349 }
350 
355 template <typename ImageT, typename MaskT, typename VarianceT>
356 Statistics makeStatistics(ImageT const &img, MaskT const &msk, VarianceT const &var, int const flags,
357  StatisticsControl const &sctrl = StatisticsControl()) {
358  return Statistics(img, msk, var, flags, sctrl);
359 }
360 
365 template <typename Pixel>
367  StatisticsControl const &sctrl = StatisticsControl()) {
368  if (sctrl.getWeighted() || sctrl.getCalcErrorFromInputVariance()) {
369  return Statistics(*mimg.getImage(), *mimg.getMask(), *mimg.getVariance(), flags, sctrl);
370  } else {
372  return Statistics(*mimg.getImage(), *mimg.getMask(), var, flags, sctrl);
373  }
374 }
375 
380 template <typename Pixel>
382  lsst::afw::image::Image<WeightPixel> const &weights, int const flags,
383  StatisticsControl const &sctrl = StatisticsControl()) {
384  if (sctrl.getWeighted() || sctrl.getCalcErrorFromInputVariance() ||
385  (!sctrl.getWeightedIsSet() && (weights.getWidth() != 0 && weights.getHeight() != 0))) {
386  return Statistics(*mimg.getImage(), *mimg.getMask(), *mimg.getVariance(), weights, flags, sctrl);
387  } else {
389  return Statistics(*mimg.getImage(), *mimg.getMask(), var, weights, flags, sctrl);
390  }
391 }
392 
402 Statistics makeStatistics(lsst::afw::image::Mask<lsst::afw::image::MaskPixel> const &msk, int const flags,
403  StatisticsControl const &sctrl = StatisticsControl());
404 
409 template <typename Pixel>
411  lsst::afw::image::Image<Pixel> const &img,
412  int const flags,
413  StatisticsControl const &sctrl = StatisticsControl()
414  ) {
415  // make a phony mask that will be compiled out
417  MaskImposter<WeightPixel> const var;
418  return Statistics(img, msk, var, flags, sctrl);
419 }
420 
425 template <typename ValueT>
427 public:
428  // types we'll use in Statistics
429  typedef typename std::vector<ValueT>::const_iterator x_iterator;
430  typedef typename std::vector<ValueT>::const_iterator fast_iterator;
431  typedef ValueT Pixel;
432 
433  // constructors for std::vector<>, and copy constructor
434  // These are both shallow! ... no actual copying of values
435  explicit ImageImposter(std::vector<ValueT> const &v) : _v(v) {}
436  explicit ImageImposter(ImageImposter<ValueT> const &img) : _v(img._getVector()) {}
437 
438  // The methods we'll use in Statistics
439  x_iterator row_begin(int) const { return _v.begin(); }
440  x_iterator row_end(int) const { return _v.end(); }
441  int getWidth() const { return _v.size(); }
442  int getHeight() const { return 1; }
444 
445  bool empty() const { return _v.empty(); }
446 
447 private:
448  std::vector<ValueT> const &_v; // a private reference to the data
449  std::vector<ValueT> const &_getVector() const { return _v; } // get the ref for the copyCon
450 };
451 
456 template <typename EntryT>
457 Statistics makeStatistics(std::vector<EntryT> const &v,
458  int const flags,
459  StatisticsControl const &sctrl = StatisticsControl()
460  ) {
461  ImageImposter<EntryT> img(v); // wrap the vector in a fake image
462  MaskImposter<lsst::afw::image::MaskPixel> msk; // instantiate a fake mask that will be compiled out.
464  return Statistics(img, msk, var, flags, sctrl);
465 }
466 
471 template <typename EntryT>
472 Statistics makeStatistics(std::vector<EntryT> const &v,
473  std::vector<WeightPixel> const &vweights,
474  int const flags,
475  StatisticsControl const &sctrl = StatisticsControl()
476  ) {
477  ImageImposter<EntryT> img(v); // wrap the vector in a fake image
478  MaskImposter<lsst::afw::image::MaskPixel> msk; // instantiate a fake mask that will be compiled out.
480 
481  ImageImposter<WeightPixel> weights(vweights);
482 
483  return Statistics(img, msk, var, weights, flags, sctrl);
484 }
485 
490 template <typename EntryT>
492  int const flags,
493  StatisticsControl const &sctrl = StatisticsControl()
494  ) {
495  if (sctrl.getWeighted() || sctrl.getCalcErrorFromInputVariance()) {
496  return Statistics(*mv.getImage(), *mv.getMask(), *mv.getVariance(), flags, sctrl);
497  } else {
499  return Statistics(*mv.getImage(), *mv.getMask(), var, flags, sctrl);
500  }
501 }
502 
507 template <typename EntryT>
509  std::vector<WeightPixel> const &vweights,
510  int const flags,
511  StatisticsControl const &sctrl = StatisticsControl()
512  ) {
513  ImageImposter<WeightPixel> weights(vweights);
514 
515  if (sctrl.getWeighted() || sctrl.getCalcErrorFromInputVariance()) {
516  return Statistics(*mv.getImage(), *mv.getMask(), *mv.getVariance(), weights, flags, sctrl);
517  } else {
519  return Statistics(*mv.getImage(), *mv.getMask(), var, weights, flags, sctrl);
520  }
521 }
522 }
523 }
524 }
525 
526 #endif
ImageImposter(ImageImposter< ValueT > const &img)
Definition: Statistics.h:436
lsst::afw::image::MaskPixel getOrMask() const
Definition: Statistics.h:275
std::vector< ValueT >::const_iterator x_iterator
Definition: Statistics.h:429
lsst::afw::image::VariancePixel WeightPixel
Definition: Statistics.h:56
get the or-mask of all pixels used.
Definition: Statistics.h:80
void setNumSigmaClip(double numSigmaClip)
Definition: Statistics.h:140
estimate sample minimum
Definition: Statistics.h:76
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage&#39;s variance.
Definition: MaskedImage.h:1074
afw::geom::Extent2I getDimensions() const
Definition: Statistics.h:443
x_iterator row_begin(int) const
Definition: Statistics.h:439
estimate sample standard deviation
Definition: Statistics.h:68
find mean value of square of pixel values
Definition: Statistics.h:79
estimate sample maximum
Definition: Statistics.h:77
Extent< int, 2 > Extent2I
Definition: Extent.h:341
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:1041
void setMaskPropagationThreshold(int bit, double threshold)
When pixels with the given bit are rejected, we count what fraction the rejected pixels would have co...
Definition: Statistics.cc:607
void setNoGoodPixelsMask(int noGoodPixelsMask)
Definition: Statistics.h:149
We don&#39;t want anything.
Definition: Statistics.h:64
bool getCalcErrorFromInputVariance() const
Definition: Statistics.h:138
Include errors of requested quantities.
Definition: Statistics.h:65
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage&#39;s mask.
Definition: MaskedImage.h:1053
A class to evaluate image statistics.
Definition: Statistics.h:213
Statistics(ImageT const &img, MaskT const &msk, VarianceT const &var, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Constructor for Statistics object.
Definition: Statistics.cc:643
x_iterator row_end(int) const
Definition: Statistics.h:440
std::vector< ValueT >::const_iterator fast_iterator
Definition: Statistics.h:430
lsst::afw::image::MaskedImage< EntryT >::VariancePtr getVariance() const
Definition: MaskedVector.h:88
void setCalcErrorFromInputVariance(bool calcErrorFromInputVariance)
Definition: Statistics.h:152
estimate sample N-sigma clipped stdev (N set in StatisticsControl, default=3)
Definition: Statistics.h:73
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:902
std::pair< double, double > Value
The type used to report (value, error) for desired statistics.
Definition: Statistics.h:216
Property stringToStatisticsProperty(std::string const property)
Conversion function to switch a string to a Property (see Statistics.h)
Definition: Statistics.cc:619
infinite_iterator< ValueT > x_iterator
Definition: Statistics.h:331
StatisticsControl(double numSigmaClip=3.0, int numIter=3, lsst::afw::image::MaskPixel andMask=0x0, bool isNanSafe=true, WeightsBoolean useWeights=WEIGHTS_NONE)
Definition: Statistics.h:95
estimate sample median
Definition: Statistics.h:70
Pass parameters to a Statistics object.
Definition: Statistics.h:91
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition: Statistics.h:72
x_iterator row_begin(int) const
Definition: Statistics.h:333
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:83
estimate sample N-sigma clipped variance (N set in StatisticsControl, default=3)
Definition: Statistics.h:74
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
Definition: Statistics.h:344
A Mask wrapper to provide an infinite_iterator for Mask::row_begin().
Definition: Statistics.h:329
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:81
number of sample points
Definition: Statistics.h:66
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:299
static MaskPixelT getPlaneBitMask(const std::vector< std::string > &names)
Return the bitmask corresponding to a vector of plane names OR&#39;d together.
Definition: Mask.cc:699
std::int32_t MaskPixel
default type for Masks and MaskedImage Masks
void setWeighted(bool useWeights)
Definition: Statistics.h:151
This iterator will never increment.
Definition: Statistics.h:314
lsst::afw::image::MaskedImage< EntryT >::MaskPtr getMask() const
Definition: MaskedVector.h:85
infinite_iterator(const ValueT *p)
Definition: Statistics.h:318
Value getResult(Property const prop=NOTHING) const
Return the value and error in the specified statistic (e.g.
Definition: Statistics.cc:791
estimate sample inter-quartile range
Definition: Statistics.h:71
void setNanSafe(bool isNanSafe)
Definition: Statistics.h:150
double getError(Property const prop=NOTHING) const
Return the error in the desired property (if specified in the constructor)
Definition: Statistics.cc:904
estimate sample mean
Definition: Statistics.h:67
afw::table::Key< afw::table::Array< ImagePixelT > > image
double getMaskPropagationThreshold(int bit) const
When pixels with the given bit are rejected, we count what fraction the rejected pixels would have co...
Definition: Statistics.cc:599
estimate sample variance
Definition: Statistics.h:69
A vector wrapper to provide a vector with the necessary methods and typedefs to be processed by Stati...
Definition: Statistics.h:426
find sum of pixels in the image
Definition: Statistics.h:78
lsst::afw::image::MaskedImage< EntryT >::ImagePtr getImage() const
Definition: MaskedVector.h:82
float VariancePixel
default type for MaskedImage variance images
Property
control what is calculated
Definition: Statistics.h:63
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:297
friend class boost::iterator_core_access
Definition: Statistics.h:321
ImageImposter(std::vector< ValueT > const &v)
Definition: Statistics.h:435