LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
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 
40 #include <algorithm>
41 #include <cassert>
42 #include <limits>
43 #include "boost/iterator/iterator_adaptor.hpp"
44 #include "boost/tuple/tuple.hpp"
45 #include "boost/shared_ptr.hpp"
48 
49 namespace lsst {
50 namespace afw {
51  namespace image {
52  template<typename> class Image;
53  template<typename, typename, typename> class MaskedImage;
54  }
55 namespace math {
56  template<typename> 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 };
82 Property stringToStatisticsProperty(std::string const property);
83 
84 
93  typedef enum { WEIGHTS_FALSE=0, WEIGHTS_TRUE=1, WEIGHTS_NONE } WeightsBoolean; // initial state is NONE
94 public:
95 
96  typedef boost::shared_ptr<StatisticsControl> Ptr;
97  typedef boost::shared_ptr<StatisticsControl> const ConstPtr;
98 
100  double numSigmaClip = 3.0,
101  int numIter = 3,
102  lsst::afw::image::MaskPixel andMask = 0x0,
103  bool isNanSafe = true,
104  int useWeights = WEIGHTS_NONE
105  ) :
106  _numSigmaClip(numSigmaClip),
107  _numIter(numIter),
108  _andMask(andMask),
110  _isNanSafe(isNanSafe),
111  _useWeights(useWeights == 0 ? WEIGHTS_FALSE : (useWeights == 1) ? WEIGHTS_TRUE : WEIGHTS_NONE),
113  {
114  try {
116  } catch(lsst::pex::exceptions::InvalidParameterError) {
117  ; // Mask has no EDGE plane defined
118  }
119 
120  assert(_numSigmaClip > 0);
121  assert(_numIter > 0);
122  }
123 
124  double getNumSigmaClip() const { return _numSigmaClip; }
125  int getNumIter() const { return _numIter; }
126  int getAndMask() const { return _andMask; }
127  int getNoGoodPixelsMask() const { return _noGoodPixelsMask; }
128  bool getNanSafe() const { return _isNanSafe; }
129  bool getWeighted() const { return _useWeights == WEIGHTS_TRUE ? true : false; }
130  bool getWeightedIsSet() const { return _useWeights != WEIGHTS_NONE ? true : false; }
132 
133  void setNumSigmaClip(double numSigmaClip) { assert(numSigmaClip > 0); _numSigmaClip = numSigmaClip; }
134  void setNumIter(int numIter) { assert(numIter > 0); _numIter = numIter; }
135  void setAndMask(int andMask) { _andMask = andMask; }
136  void setNoGoodPixelsMask(int noGoodPixelsMask) { _noGoodPixelsMask = noGoodPixelsMask; }
137  void setNanSafe(bool isNanSafe) { _isNanSafe = isNanSafe; }
138  void setWeighted(bool useWeights) { _useWeights = useWeights ? WEIGHTS_TRUE : WEIGHTS_FALSE; }
139  void setCalcErrorFromInputVariance(bool calcErrorFromInputVariance) {
140  _calcErrorFromInputVariance = calcErrorFromInputVariance;
141  }
142 
143 private:
144  double _numSigmaClip; // Number of standard deviations to clip at
145  int _numIter; // Number of iterations
146  int _andMask; // and-Mask to specify which mask planes to ignore
147  int _noGoodPixelsMask; // mask to set if no values are acceptable
148  bool _isNanSafe; // Check for NaNs & Infs before running (slower)
149  WeightsBoolean _useWeights; // Calculate weighted statistics (enum because of 3-valued logic)
150  bool _calcErrorFromInputVariance; // Calculate errors from the input variances, if available
151 };
152 
196 class Statistics {
197 public:
199  typedef std::pair<double, double> Value;
200 
201  template<typename ImageT, typename MaskT, typename VarianceT>
202  explicit Statistics(ImageT const &img,
203  MaskT const &msk,
204  VarianceT const &var,
205  int const flags,
206  StatisticsControl const& sctrl = StatisticsControl());
207 
208  template<typename ImageT, typename MaskT, typename VarianceT, typename WeightT>
209  explicit Statistics(ImageT const &img,
210  MaskT const &msk,
211  VarianceT const &var,
212  WeightT const &weights,
213  int const flags,
214  StatisticsControl const& sctrl = StatisticsControl());
215 
216  Value getResult(Property const prop = NOTHING) const;
217 
218  double getError(Property const prop = NOTHING) const;
219  double getValue(Property const prop = NOTHING) const;
221  return _allPixelOrMask;
222  }
223 
224 private:
225  long _flags; // The desired calculation
226 
227  int _n; // number of pixels in the image
228  Value _mean; // the image's mean
229  Value _variance; // the image's variance
230  double _min; // the image's minimum
231  double _max; // the image's maximum
232  double _sum; // the sum of all the image's pixels
233  Value _meanclip; // the image's N-sigma clipped mean
234  Value _varianceclip; // the image's N-sigma clipped variance
235  Value _median; // the image's median
236  double _iqrange; // the image's interquartile range
237  lsst::afw::image::MaskPixel _allPixelOrMask; // the 'or' of all masked pixels
238 
239  StatisticsControl _sctrl; // the control structure
240  bool _weightsAreMultiplicative; // Multiply by weights rather than dividing by them
241 
242  template<typename ImageT, typename MaskT, typename VarianceT, typename WeightT>
243  void doStatistics(ImageT const &img,
244  MaskT const &msk,
245  VarianceT const &var,
246  WeightT const &weights,
247  int const flags,
248  StatisticsControl const& sctrl);
249 };
250 
251 /************************************* The factory functions **********************************/
256 template <typename ValueT>
258  : public boost::iterator_adaptor<infinite_iterator<ValueT>,
259  const ValueT*, const ValueT,
260  boost::forward_traversal_tag> {
261 public:
262  infinite_iterator() : infinite_iterator::iterator_adaptor_(0) {}
263  explicit infinite_iterator(const ValueT* p) : infinite_iterator::iterator_adaptor_(p) {}
264 private:
266  void increment() { ; } // never actually advance the iterator
267 };
272 template<typename ValueT>
274 public:
276  explicit MaskImposter(ValueT val = 0) { _val[0] = val; }
277  x_iterator row_begin(int) const { return x_iterator(_val); }
278 private:
279  ValueT _val[1];
280 };
281 
282 
287 template<typename Pixel>
290  int const flags,
291  StatisticsControl const& sctrl = StatisticsControl()
292  ) {
294  return Statistics(img, msk, var, flags, sctrl);
295 }
296 
297 
302 template<typename ImageT, typename MaskT, typename VarianceT>
303 Statistics makeStatistics(ImageT const &img,
304  MaskT const &msk,
305  VarianceT const &var,
306  int const flags,
307  StatisticsControl const& sctrl = StatisticsControl()
308  ) {
309  return Statistics(img, msk, var, flags, sctrl);
310 }
311 
316 template<typename Pixel>
319  int const flags,
320  StatisticsControl const& sctrl = StatisticsControl()
321  )
322 {
323  if (sctrl.getWeighted() || sctrl.getCalcErrorFromInputVariance()) {
324  return Statistics(*mimg.getImage(), *mimg.getMask(), *mimg.getVariance(), flags, sctrl);
325  } else {
327  return Statistics(*mimg.getImage(), *mimg.getMask(), var, flags, sctrl);
328  }
329 }
330 
335 template<typename Pixel>
339  int const flags,
340  StatisticsControl const& sctrl = StatisticsControl()
341  )
342 {
343  if (sctrl.getWeighted() || sctrl.getCalcErrorFromInputVariance() ||
344  (!sctrl.getWeightedIsSet() && (weights.getWidth() != 0 && weights.getHeight() != 0))) {
345  return Statistics(*mimg.getImage(), *mimg.getMask(), *mimg.getVariance(), weights, flags, sctrl);
346  } else {
348  return Statistics(*mimg.getImage(), *mimg.getMask(), var, weights, flags, sctrl);
349  }
350 }
351 
358  int const flags,
359  StatisticsControl const& sctrl = StatisticsControl());
360 
361 
362 
367 template<typename Pixel>
369  lsst::afw::image::Image<Pixel> const &img,
370  int const flags,
371  StatisticsControl const& sctrl = StatisticsControl()
372 ) {
373  // make a phony mask that will be compiled out
375  MaskImposter<WeightPixel> const var;
376  return Statistics(img, msk, var, flags, sctrl);
377 }
378 
379 
384 template<typename ValueT>
386 public:
387 
388  // types we'll use in Statistics
389  typedef typename std::vector<ValueT>::const_iterator x_iterator;
390  typedef typename std::vector<ValueT>::const_iterator fast_iterator;
391  typedef ValueT Pixel;
392 
393  // constructors for std::vector<>, and copy constructor
394  // These are both shallow! ... no actual copying of values
395  explicit ImageImposter(std::vector<ValueT> const &v) : _v(v) { }
396  explicit ImageImposter(ImageImposter<ValueT> const &img) : _v(img._getVector()) {}
397 
398  // The methods we'll use in Statistics
399  x_iterator row_begin(int) const { return _v.begin(); }
400  x_iterator row_end(int) const { return _v.end(); }
401  int getWidth() const { return _v.size(); }
402  int getHeight() const { return 1; }
403 
404  bool empty() const { return _v.empty(); }
405 private:
406  std::vector<ValueT> const &_v; // a private reference to the data
407  std::vector<ValueT> const &_getVector() const { return _v; } // get the ref for the copyCon
408 };
409 
414 template<typename EntryT>
415 Statistics makeStatistics(std::vector<EntryT> const &v,
416  int const flags,
417  StatisticsControl const& sctrl = StatisticsControl()
418  ) {
419  ImageImposter<EntryT> img(v); // wrap the vector in a fake image
420  MaskImposter<lsst::afw::image::MaskPixel> msk; // instantiate a fake mask that will be compiled out.
422  return Statistics(img, msk, var, flags, sctrl);
423 }
424 
429 template<typename EntryT>
430 Statistics makeStatistics(std::vector<EntryT> const &v,
431  std::vector<WeightPixel> const &vweights,
432  int const flags,
433  StatisticsControl const& sctrl = StatisticsControl()
434  ) {
435  ImageImposter<EntryT> img(v); // wrap the vector in a fake image
436  MaskImposter<lsst::afw::image::MaskPixel> msk; // instantiate a fake mask that will be compiled out.
438 
439  ImageImposter<WeightPixel> weights(vweights);
440 
441  return Statistics(img, msk, var, weights, flags, sctrl);
442 }
443 
448 template<typename EntryT>
450  int const flags,
451  StatisticsControl const& sctrl = StatisticsControl()
452  ) {
453  if (sctrl.getWeighted() || sctrl.getCalcErrorFromInputVariance()) {
454  return Statistics(*mv.getImage(), *mv.getMask(), *mv.getVariance(), flags, sctrl);
455  } else {
457  return Statistics(*mv.getImage(), *mv.getMask(), var, flags, sctrl);
458  }
459 }
460 
465 template<typename EntryT>
467  std::vector<WeightPixel> const &vweights,
468  int const flags,
469  StatisticsControl const& sctrl = StatisticsControl()
470  ) {
471  ImageImposter<WeightPixel> weights(vweights);
472 
473  if (sctrl.getWeighted() || sctrl.getCalcErrorFromInputVariance()) {
474  return Statistics(*mv.getImage(), *mv.getMask(), *mv.getVariance(), weights, flags, sctrl);
475  } else {
477  return Statistics(*mv.getImage(), *mv.getMask(), var, weights, flags, sctrl);
478  }
479 }
480 
481 }}}
482 
483 #endif
ImageImposter(ImageImposter< ValueT > const &img)
Definition: Statistics.h:396
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
Definition: Statistics.cc:950
boost::uint16_t MaskPixel
ImagePtr getImage(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:869
Statistics makeStatistics(std::vector< EntryT > const &v, int const flags, StatisticsControl const &sctrl=StatisticsControl())
The makeStatistics() overload to handle std::vector&lt;&gt;
Definition: Statistics.h:415
boost::shared_ptr< StatisticsControl > Ptr
Definition: Statistics.h:96
lsst::afw::image::MaskedImage< EntryT >::VariancePtr getVariance() const
Definition: MaskedVector.h:89
lsst::afw::image::MaskedImage< EntryT >::MaskPtr getMask() const
Definition: MaskedVector.h:86
estimate sample mean
Definition: Statistics.h:67
std::pair< double, double > Value
The type used to report (value, error) for desired statistics.
Definition: Statistics.h:199
std::vector< ValueT > const & _getVector() const
Definition: Statistics.h:407
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition: Statistics.h:72
void setNumIter(int numIter)
Definition: Statistics.h:134
StatisticsControl(double numSigmaClip=3.0, int numIter=3, lsst::afw::image::MaskPixel andMask=0x0, bool isNanSafe=true, int useWeights=WEIGHTS_NONE)
Definition: Statistics.h:99
infinite_iterator< ValueT > x_iterator
Definition: Statistics.h:275
void setAndMask(int andMask)
Definition: Statistics.h:135
x_iterator row_begin(int) const
Definition: Statistics.h:277
MaskImposter(ValueT val=0)
Definition: Statistics.h:276
Statistics(ImageT const &img, MaskT const &msk, VarianceT const &var, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Constructor for Statistics object.
Definition: Statistics.cc:689
estimate sample standard deviation
Definition: Statistics.h:68
estimate sample variance
Definition: Statistics.h:69
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, int const flags, StatisticsControl const &sctrl=StatisticsControl())
The makeStatistics() overload to handle regular (non-masked) Images.
Definition: Statistics.h:368
find sum of pixels in the image
Definition: Statistics.h:78
Value getResult(Property const prop=NOTHING) const
Return the value and error in the specified statistic (e.g. MEAN)
Definition: Statistics.cc:828
ImageImposter(std::vector< ValueT > const &v)
Definition: Statistics.h:395
lsst::afw::image::VariancePixel WeightPixel
Definition: Statistics.h:56
boost::shared_ptr< StatisticsControl > const ConstPtr
Definition: Statistics.h:97
Statistics makeStatistics(lsst::afw::math::MaskedVector< EntryT > const &mv, std::vector< WeightPixel > const &vweights, int const flags, StatisticsControl const &sctrl=StatisticsControl())
The makeStatistics() overload to handle lsst::afw::math::MaskedVector&lt;&gt;
Definition: Statistics.h:466
estimate sample N-sigma clipped stdev (N set in StatisticsControl, default=3)
Definition: Statistics.h:73
Statistics makeStatistics(lsst::afw::math::MaskedVector< EntryT > const &mv, int const flags, StatisticsControl const &sctrl=StatisticsControl())
The makeStatistics() overload to handle lsst::afw::math::MaskedVector&lt;&gt;
Definition: Statistics.h:449
int const x0
Definition: saturated.cc:45
find mean value of square of pixel values
Definition: Statistics.h:79
lsst::afw::image::MaskedImage< EntryT >::ImagePtr getImage() const
Definition: MaskedVector.h:83
infinite_iterator(const ValueT *p)
Definition: Statistics.h:263
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
Include errors of requested quantities.
Definition: Statistics.h:65
We don&#39;t want anything.
Definition: Statistics.h:64
VariancePtr getVariance(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s variance.
Definition: MaskedImage.h:890
lsst::afw::image::MaskPixel getOrMask() const
Definition: Statistics.h:220
Statistics makeStatistics(lsst::afw::image::MaskedImage< Pixel > const &mimg, lsst::afw::image::Image< WeightPixel > const &weights, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle MaskedImages, just pass the getImage() and getMask() values right on through.
Definition: Statistics.h:336
int getWidth() const
Return the number of columns in the image.
Definition: Image.h:237
Statistics makeStatistics(ImageT const &img, MaskT const &msk, VarianceT const &var, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a straight front-end to the constructor.
Definition: Statistics.h:303
Pass parameters to a Statistics objectA class to pass parameters which control how the stats are calc...
Definition: Statistics.h:92
void doStatistics(ImageT const &img, MaskT const &msk, VarianceT const &var, WeightT const &weights, int const flags, StatisticsControl const &sctrl)
Definition: Statistics.cc:736
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:859
void setNumSigmaClip(double numSigmaClip)
Definition: Statistics.h:133
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:93
void setNanSafe(bool isNanSafe)
Definition: Statistics.h:137
std::vector< ValueT >::const_iterator fast_iterator
Definition: Statistics.h:390
A Mask wrapper to provide an infinite_iterator for Mask::row_begin(). This allows a fake Mask to be p...
Definition: Statistics.h:273
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:77
Statistics makeStatistics(std::vector< EntryT > const &v, std::vector< WeightPixel > const &vweights, int const flags, StatisticsControl const &sctrl=StatisticsControl())
The makeStatistics() overload to handle std::vector&lt;&gt;
Definition: Statistics.h:430
StatisticsControl _sctrl
Definition: Statistics.h:239
void setCalcErrorFromInputVariance(bool calcErrorFromInputVariance)
Definition: Statistics.h:139
float VariancePixel
! default type for Masks and MaskedImage Masks
estimate sample maximum
Definition: Statistics.h:77
number of sample points
Definition: Statistics.h:66
double getError(Property const prop=NOTHING) const
Return the error in the desired property (if specified in the constructor)
Definition: Statistics.cc:961
std::vector< ValueT > const & _v
Definition: Statistics.h:406
estimate sample minimum
Definition: Statistics.h:76
This iterator will never increment. It is returned by row_begin() in the MaskImposter class (below) t...
Definition: Statistics.h:257
estimate sample median
Definition: Statistics.h:70
std::vector< ValueT >::const_iterator x_iterator
Definition: Statistics.h:389
bool getCalcErrorFromInputVariance() const
Definition: Statistics.h:131
MaskPtr getMask(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage&#39;s mask.
Definition: MaskedImage.h:879
void setWeighted(bool useWeights)
Definition: Statistics.h:138
void setNoGoodPixelsMask(int noGoodPixelsMask)
Definition: Statistics.h:136
estimate sample inter-quartile range
Definition: Statistics.h:71
lsst::afw::image::MaskPixel _allPixelOrMask
Definition: Statistics.h:237
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1023
A vector wrapper to provide a vector with the necessary methods and typedefs to be processed by Stati...
Definition: Statistics.h:385
get the or-mask of all pixels used.
Definition: Statistics.h:80
Property stringToStatisticsProperty(std::string const property)
Conversion function to switch a string to a Property (see Statistics.h)
Definition: Statistics.cc:658
Implementation of the Class MaskedImage.
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:239
x_iterator row_end(int) const
Definition: Statistics.h:400
Property
control what is calculated
Definition: Statistics.h:63
Statistics makeStatistics(lsst::afw::image::MaskedImage< Pixel > const &mimg, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle MaskedImages, just pass the getImage() and getMask() values right on through.
Definition: Statistics.h:317
ImageT val
Definition: CR.cc:154
friend class boost::iterator_core_access
Definition: Statistics.h:265
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:288
x_iterator row_begin(int) const
Definition: Statistics.h:399