lsst.afw  14.0-25-g0e8fc950e+6
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  NCLIPPED = 0x8000,
82  NMASKED = 0x10000
83 };
86 
94 public:
95  enum WeightsBoolean { WEIGHTS_FALSE = 0, WEIGHTS_TRUE = 1, WEIGHTS_NONE }; // initial state is NONE
96 
97  StatisticsControl(double numSigmaClip = 3.0,
98  int numIter = 3,
100  0x0,
101  bool isNanSafe = true,
102  WeightsBoolean useWeights =
103  WEIGHTS_NONE
104  )
105  : _numSigmaClip(numSigmaClip),
106  _numIter(numIter),
107  _andMask(andMask),
108  _noGoodPixelsMask(0x0),
109  _isNanSafe(isNanSafe),
110  _useWeights(useWeights),
111  _calcErrorFromInputVariance(false),
112  _maskPropagationThresholds() {
113  try {
114  _noGoodPixelsMask = lsst::afw::image::Mask<>::getPlaneBitMask("NO_DATA");
116  ; // Mask has no NO_DATA plane defined
117  }
118 
119  assert(_numSigmaClip > 0);
120  assert(_numIter > 0);
121  }
122 
124 
129  double getMaskPropagationThreshold(int bit) const;
130  void setMaskPropagationThreshold(int bit, double threshold);
132 
133  double getNumSigmaClip() const { return _numSigmaClip; }
134  int getNumIter() const { return _numIter; }
135  int getAndMask() const { return _andMask; }
136  int getNoGoodPixelsMask() const { return _noGoodPixelsMask; }
137  bool getNanSafe() const { return _isNanSafe; }
138  bool getWeighted() const { return _useWeights == WEIGHTS_TRUE ? true : false; }
139  bool getWeightedIsSet() const { return _useWeights != WEIGHTS_NONE ? true : false; }
140  bool getCalcErrorFromInputVariance() const { return _calcErrorFromInputVariance; }
141 
142  void setNumSigmaClip(double numSigmaClip) {
143  assert(numSigmaClip > 0);
144  _numSigmaClip = numSigmaClip;
145  }
146  void setNumIter(int numIter) {
147  assert(numIter > 0);
148  _numIter = numIter;
149  }
150  void setAndMask(int andMask) { _andMask = andMask; }
151  void setNoGoodPixelsMask(int noGoodPixelsMask) { _noGoodPixelsMask = noGoodPixelsMask; }
152  void setNanSafe(bool isNanSafe) { _isNanSafe = isNanSafe; }
153  void setWeighted(bool useWeights) { _useWeights = useWeights ? WEIGHTS_TRUE : WEIGHTS_FALSE; }
154  void setCalcErrorFromInputVariance(bool calcErrorFromInputVariance) {
155  _calcErrorFromInputVariance = calcErrorFromInputVariance;
156  }
157 
158 private:
159  friend class Statistics;
160 
161  double _numSigmaClip; // Number of standard deviations to clip at
162  int _numIter; // Number of iterations
163  int _andMask; // and-Mask to specify which mask planes to ignore
164  int _noGoodPixelsMask; // mask to set if no values are acceptable
165  bool _isNanSafe; // Check for NaNs & Infs before running (slower)
166  WeightsBoolean _useWeights; // Calculate weighted statistics (enum because of 3-valued logic)
167  bool _calcErrorFromInputVariance; // Calculate errors from the input variances, if available
168  std::vector<double> _maskPropagationThresholds; // Thresholds for when to propagate mask bits,
169  // treated like a dict (unset bits are set to 1.0)
170 };
171 
215 class Statistics {
216 public:
219 
232  template <typename ImageT, typename MaskT, typename VarianceT>
233  explicit Statistics(ImageT const &img, MaskT const &msk, VarianceT const &var, int const flags,
234  StatisticsControl const &sctrl = StatisticsControl());
235 
244  template <typename ImageT, typename MaskT, typename VarianceT, typename WeightT>
245  explicit Statistics(ImageT const &img, MaskT const &msk, VarianceT const &var, WeightT const &weights,
246  int const flags, StatisticsControl const &sctrl = StatisticsControl());
247 
261  Value getResult(Property const prop = NOTHING) const;
262 
270  double getError(Property const prop = NOTHING) const;
276  double getValue(Property const prop = NOTHING) const;
277  lsst::afw::image::MaskPixel getOrMask() const { return _allPixelOrMask; }
278 
279 private:
280  long _flags; // The desired calculation
281 
282  int _n; // number of pixels in the image
283  Value _mean; // the image's mean
284  Value _variance; // the image's variance
285  double _min; // the image's minimum
286  double _max; // the image's maximum
287  double _sum; // the sum of all the image's pixels
288  Value _meanclip; // the image's N-sigma clipped mean
289  Value _varianceclip; // the image's N-sigma clipped variance
290  Value _median; // the image's median
291  int _nClipped; // number of pixels clipped
292  int _nMasked; // number of pixels masked
293  double _iqrange; // the image's interquartile range
294  lsst::afw::image::MaskPixel _allPixelOrMask; // the 'or' of all masked pixels
295 
296  StatisticsControl _sctrl; // the control structure
297  bool _weightsAreMultiplicative; // Multiply by weights rather than dividing by them
298 
307  template <typename ImageT, typename MaskT, typename VarianceT, typename WeightT>
308  void doStatistics(ImageT const &img, MaskT const &msk, VarianceT const &var, WeightT const &weights,
309  int const flags, StatisticsControl const &sctrl);
310 };
311 
312 /* ************************************ The factory functions ********************************* */
317 template <typename ValueT>
318 class infinite_iterator : public boost::iterator_adaptor<infinite_iterator<ValueT>, const ValueT *,
319  const ValueT, boost::forward_traversal_tag> {
320 public:
321  infinite_iterator() : infinite_iterator::iterator_adaptor_(0) {}
322  explicit infinite_iterator(const ValueT *p) : infinite_iterator::iterator_adaptor_(p) {}
323 
324 private:
325  friend class boost::iterator_core_access;
326  void increment() { ; } // never actually advance the iterator
327 };
332 template <typename ValueT>
334 public:
336  explicit MaskImposter(ValueT val = 0) { _val[0] = val; }
337  x_iterator row_begin(int) const { return x_iterator(_val); }
338 
339 private:
340  ValueT _val[1];
341 };
342 
347 template <typename Pixel>
349  lsst::afw::image::Mask<image::MaskPixel> const &msk, int const flags,
350  StatisticsControl const &sctrl = StatisticsControl()) {
352  return Statistics(img, msk, var, flags, sctrl);
353 }
354 
359 template <typename ImageT, typename MaskT, typename VarianceT>
360 Statistics makeStatistics(ImageT const &img, MaskT const &msk, VarianceT const &var, int const flags,
361  StatisticsControl const &sctrl = StatisticsControl()) {
362  return Statistics(img, msk, var, flags, sctrl);
363 }
364 
369 template <typename Pixel>
371  StatisticsControl const &sctrl = StatisticsControl()) {
372  if (sctrl.getWeighted() || sctrl.getCalcErrorFromInputVariance()) {
373  return Statistics(*mimg.getImage(), *mimg.getMask(), *mimg.getVariance(), flags, sctrl);
374  } else {
376  return Statistics(*mimg.getImage(), *mimg.getMask(), var, flags, sctrl);
377  }
378 }
379 
384 template <typename Pixel>
386  lsst::afw::image::Image<WeightPixel> const &weights, int const flags,
387  StatisticsControl const &sctrl = StatisticsControl()) {
388  if (sctrl.getWeighted() || sctrl.getCalcErrorFromInputVariance() ||
389  (!sctrl.getWeightedIsSet() && (weights.getWidth() != 0 && weights.getHeight() != 0))) {
390  return Statistics(*mimg.getImage(), *mimg.getMask(), *mimg.getVariance(), weights, flags, sctrl);
391  } else {
393  return Statistics(*mimg.getImage(), *mimg.getMask(), var, weights, flags, sctrl);
394  }
395 }
396 
407  StatisticsControl const &sctrl = StatisticsControl());
408 
413 template <typename Pixel>
415  lsst::afw::image::Image<Pixel> const &img,
416  int const flags,
417  StatisticsControl const &sctrl = StatisticsControl()
418  ) {
419  // make a phony mask that will be compiled out
421  MaskImposter<WeightPixel> const var;
422  return Statistics(img, msk, var, flags, sctrl);
423 }
424 
429 template <typename ValueT>
431 public:
432  // types we'll use in Statistics
435  typedef ValueT Pixel;
436 
437  // constructors for std::vector<>, and copy constructor
438  // These are both shallow! ... no actual copying of values
439  explicit ImageImposter(std::vector<ValueT> const &v) : _v(v) {}
440  explicit ImageImposter(ImageImposter<ValueT> const &img) : _v(img._getVector()) {}
441 
442  // The methods we'll use in Statistics
443  x_iterator row_begin(int) const { return _v.begin(); }
444  x_iterator row_end(int) const { return _v.end(); }
445  int getWidth() const { return _v.size(); }
446  int getHeight() const { return 1; }
447  afw::geom::Extent2I getDimensions() const { return afw::geom::Extent2I(getWidth(), getHeight()); }
448 
449  bool empty() const { return _v.empty(); }
450 
451 private:
452  std::vector<ValueT> const &_v; // a private reference to the data
453  std::vector<ValueT> const &_getVector() const { return _v; } // get the ref for the copyCon
454 };
455 
460 template <typename EntryT>
462  int const flags,
463  StatisticsControl const &sctrl = StatisticsControl()
464  ) {
465  ImageImposter<EntryT> img(v); // wrap the vector in a fake image
466  MaskImposter<lsst::afw::image::MaskPixel> msk; // instantiate a fake mask that will be compiled out.
468  return Statistics(img, msk, var, flags, sctrl);
469 }
470 
475 template <typename EntryT>
477  std::vector<WeightPixel> const &vweights,
478  int const flags,
479  StatisticsControl const &sctrl = StatisticsControl()
480  ) {
481  ImageImposter<EntryT> img(v); // wrap the vector in a fake image
482  MaskImposter<lsst::afw::image::MaskPixel> msk; // instantiate a fake mask that will be compiled out.
484 
485  ImageImposter<WeightPixel> weights(vweights);
486 
487  return Statistics(img, msk, var, weights, flags, sctrl);
488 }
489 
494 template <typename EntryT>
496  int const flags,
497  StatisticsControl const &sctrl = StatisticsControl()
498  ) {
499  if (sctrl.getWeighted() || sctrl.getCalcErrorFromInputVariance()) {
500  return Statistics(*mv.getImage(), *mv.getMask(), *mv.getVariance(), flags, sctrl);
501  } else {
503  return Statistics(*mv.getImage(), *mv.getMask(), var, flags, sctrl);
504  }
505 }
506 
511 template <typename EntryT>
513  std::vector<WeightPixel> const &vweights,
514  int const flags,
515  StatisticsControl const &sctrl = StatisticsControl()
516  ) {
517  ImageImposter<WeightPixel> weights(vweights);
518 
519  if (sctrl.getWeighted() || sctrl.getCalcErrorFromInputVariance()) {
520  return Statistics(*mv.getImage(), *mv.getMask(), *mv.getVariance(), weights, flags, sctrl);
521  } else {
523  return Statistics(*mv.getImage(), *mv.getMask(), var, weights, flags, sctrl);
524  }
525 }
526 }
527 }
528 }
529 
530 #endif
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage&#39;s variance.
Definition: MaskedImage.h:1074
ImageImposter(ImageImposter< ValueT > const &img)
Definition: Statistics.h:440
std::vector< ValueT >::const_iterator x_iterator
Definition: Statistics.h:433
x_iterator row_end(int) const
Definition: Statistics.h:444
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:142
estimate sample minimum
Definition: Statistics.h:76
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:299
number of clipped points
Definition: Statistics.h:81
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
void setNoGoodPixelsMask(int noGoodPixelsMask)
Definition: Statistics.h:151
We don&#39;t want anything.
Definition: Statistics.h:64
bool getCalcErrorFromInputVariance() const
Definition: Statistics.h:140
lsst::afw::image::MaskedImage< EntryT >::ImagePtr getImage() const
Definition: MaskedVector.h:82
Include errors of requested quantities.
Definition: Statistics.h:65
A class to evaluate image statistics.
Definition: Statistics.h:215
number of masked points
Definition: Statistics.h:82
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:1041
STL class.
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<>
Definition: Statistics.h:512
std::vector< ValueT >::const_iterator fast_iterator
Definition: Statistics.h:434
void setCalcErrorFromInputVariance(bool calcErrorFromInputVariance)
Definition: Statistics.h:154
estimate sample N-sigma clipped stdev (N set in StatisticsControl, default=3)
Definition: Statistics.h:73
std::pair< double, double > Value
The type used to report (value, error) for desired statistics.
Definition: Statistics.h:218
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:335
StatisticsControl(double numSigmaClip=3.0, int numIter=3, lsst::afw::image::MaskPixel andMask=0x0, bool isNanSafe=true, WeightsBoolean useWeights=WEIGHTS_NONE)
Definition: Statistics.h:97
estimate sample median
Definition: Statistics.h:70
Pass parameters to a Statistics object.
Definition: Statistics.h:93
estimate sample N-sigma clipped mean (N set in StatisticsControl, default=3)
Definition: Statistics.h:72
A base class for image defects.
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
A Mask wrapper to provide an infinite_iterator for Mask::row_begin().
Definition: Statistics.h:333
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:81
number of sample points
Definition: Statistics.h:66
x_iterator row_begin(int) const
Definition: Statistics.h:443
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
void setWeighted(bool useWeights)
Definition: Statistics.h:153
lsst::afw::image::MaskedImage< EntryT >::VariancePtr getVariance() const
Definition: MaskedVector.h:88
This iterator will never increment.
Definition: Statistics.h:318
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage&#39;s mask.
Definition: MaskedImage.h:1053
lsst::afw::image::MaskedImage< EntryT >::MaskPtr getMask() const
Definition: MaskedVector.h:85
infinite_iterator(const ValueT *p)
Definition: Statistics.h:322
estimate sample inter-quartile range
Definition: Statistics.h:71
afw::geom::Extent2I getDimensions() const
Definition: Statistics.h:447
void setNanSafe(bool isNanSafe)
Definition: Statistics.h:152
estimate sample mean
Definition: Statistics.h:67
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:297
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:430
lsst::afw::image::MaskPixel getOrMask() const
Definition: Statistics.h:277
find sum of pixels in the image
Definition: Statistics.h:78
float VariancePixel
default type for MaskedImage variance images
Property
control what is calculated
Definition: Statistics.h:63
x_iterator row_begin(int) const
Definition: Statistics.h:337
ImageImposter(std::vector< ValueT > const &v)
Definition: Statistics.h:439