casacore
HingesFencesStatistics.h
Go to the documentation of this file.
1 //# Copyright (C) 2000,2001
2 //# Associated Universities, Inc. Washington DC, USA.
3 //#
4 //# This library is free software; you can redistribute it and/or modify it
5 //# under the terms of the GNU Library General Public License as published by
6 //# the Free Software Foundation; either version 2 of the License, or (at your
7 //# option) any later version.
8 //#
9 //# This library is distributed in the hope that it will be useful, but WITHOUT
10 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 //# License for more details.
13 //#
14 //# You should have received a copy of the GNU Library General Public License
15 //# along with this library; if not, write to the Free Software Foundation,
16 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
17 //#
18 //# Correspondence concerning AIPS++ should be addressed as follows:
19 //# Internet email: aips2-request@nrao.edu.
20 //# Postal address: AIPS++ Project Office
21 //# National Radio Astronomy Observatory
22 //# 520 Edgemont Road
23 //# Charlottesville, VA 22903-2475 USA
24 //#
25 //# $Id: Array.h 21545 2015-01-22 19:36:35Z gervandiepen $
26 
27 #ifndef SCIMATH_HINGESFENCESSTATISTICS_H
28 #define SCIMATH_HINGESFENCESSTATISTICS_H
29 
30 #include <casacore/casa/aips.h>
31 
32 #include <casacore/scimath/Mathematics/ConstrainedRangeStatistics.h>
33 
34 #include <set>
35 #include <vector>
36 #include <utility>
37 
38 namespace casacore {
39 
40 // Class to calculate statistics using the so-called hinges and fences algorithm. In this
41 // algorithm, the data on which the statistics are computed from is limited to the range
42 // of values between Q1 - f*D and Q3 + f*D, inclusive, where D = Q3 - Q1 and Q1 and Q3 are
43 // the first and third quartiles, respectively.
44 
45 template <class AccumType, class DataIterator, class MaskIterator=const Bool *, class WeightsIterator=DataIterator>
47  : public ConstrainedRangeStatistics<CASA_STATP> {
48 public:
49 
50  // If <src>f</src> is negative, the full dataset is used; ie the object has the same
51  // behavior as a ClassicalStatistics object
53 
54  virtual ~HingesFencesStatistics();
55 
56  // copy semantics
59  );
60 
61  // get the algorithm that this object uses for computing stats
64  };
65 
66  // reset object to initial state. Clears all private fields including data,
67  // accumulators, global range. It does not affect the fence factor (_f), which was
68  // set at object construction.
69  virtual void reset();
70 
71  // This class does not allow statistics to be calculated as datasets are added, so
72  // an exception will be thrown if <src>c</src> is True.
74 
75 protected:
76  // <group>
77  // scan through the data set to determine the number of good (unmasked, weight > 0,
78  // within range) points. The first with no mask, no
79  // ranges, and no weights is trivial with npts = nr in this class, but is implemented here
80  // so that derived classes may override it.
81  inline void _accumNpts(
82  uInt64& npts,
83  const DataIterator& dataStart, Int64 nr, uInt dataStride
84  ) const;
85 
86  void _accumNpts(
87  uInt64& npts,
88  const DataIterator& dataStart, Int64 nr, uInt dataStride,
89  const DataRanges& ranges, Bool isInclude
90  ) const;
91 
92  void _accumNpts(
93  uInt64& npts,
94  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
95  const MaskIterator& maskBegin, uInt maskStride
96  ) const;
97 
98  void _accumNpts(
99  uInt64& npts,
100  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
101  const MaskIterator& maskBegin, uInt maskStride, const DataRanges& ranges,
102  Bool isInclude
103  ) const;
104 
105  void _accumNpts(
106  uInt64& npts,
107  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
108  Int64 nr, uInt dataStride
109  ) const;
110 
111  void _accumNpts(
112  uInt64& npts,
113  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
114  Int64 nr, uInt dataStride, const DataRanges& ranges, Bool isInclude
115  ) const;
116 
117  void _accumNpts(
118  uInt64& npts,
119  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
120  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
121  const DataRanges& ranges, Bool isInclude
122  ) const;
123 
124  void _accumNpts(
125  uInt64& npts,
126  const DataIterator& dataBegin, const WeightsIterator& weightBegin,
127  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride
128  ) const;
129  // </group>
130 
131  // <group>
132  virtual void _findBins(
133  vector<vector<uInt64> >& binCounts,
134  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
135  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
136  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc,
137  const vector<AccumType>& maxLimit
138  ) const;
139 
140  virtual void _findBins(
141  vector<vector<uInt64> >& binCounts,
142  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
143  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
144  const DataRanges& ranges, Bool isInclude,
145  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
146  ) const;
147 
148  virtual void _findBins(
149  vector<vector<uInt64> >& binCounts,
150  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
151  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
152  const MaskIterator& maskBegin, uInt maskStride,
153  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
154  ) const;
155 
156  virtual void _findBins(
157  vector<vector<uInt64> >& binCounts,
158  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
159  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
160  const MaskIterator& maskBegin, uInt maskStride, const DataRanges& ranges,
161  Bool isInclude,
162  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
163  ) const;
164 
165  virtual void _findBins(
166  vector<vector<uInt64> >& binCounts,
167  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
168  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
169  Int64 nr, uInt dataStride,
170  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
171  ) const ;
172 
173  virtual void _findBins(
174  vector<vector<uInt64> >& binCounts,
175  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
176  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
177  Int64 nr, uInt dataStride, const DataRanges& ranges, Bool isInclude,
178  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
179  ) const;
180 
181  virtual void _findBins(
182  vector<vector<uInt64> >& binCounts,
183  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
184  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
185  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
186  const DataRanges& ranges, Bool isInclude,
187  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
188  ) const;
189 
190  virtual void _findBins(
191  vector<vector<uInt64> >& binCounts,
192  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
193  const DataIterator& dataBegin, const WeightsIterator& weightBegin,
194  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
195  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
196  ) const;
197  // </group>
198 
199  // <group>
200  virtual void _minMax(
202  const DataIterator& dataBegin, Int64 nr, uInt dataStride
203  ) const;
204 
205  virtual void _minMax(
207  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
208  const DataRanges& ranges, Bool isInclude
209  ) const;
210 
211  virtual void _minMax(
213  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
214  const MaskIterator& maskBegin, uInt maskStride
215  ) const;
216 
217  virtual void _minMax(
219  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
220  const MaskIterator& maskBegin, uInt maskStride, const DataRanges& ranges,
221  Bool isInclude
222  ) const;
223 
224  virtual void _minMax(
226  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
227  Int64 nr, uInt dataStride
228  ) const;
229 
230  virtual void _minMax(
232  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
233  Int64 nr, uInt dataStride, const DataRanges& ranges, Bool isInclude
234  ) const;
235 
236  virtual void _minMax(
238  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
239  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
240  const DataRanges& ranges, Bool isInclude
241  ) const;
242 
243  virtual void _minMax(
245  const DataIterator& dataBegin, const WeightsIterator& weightBegin,
246  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride
247  ) const;
248  // </group>
249 
250  //<group>
251  // populate an unsorted array with valid data. If <src>includeLimits</src> is defined,
252  // then restrict values that are entered in the array to those limits (inclusive of the
253  // minimum, exclusive of the maximum). <src>maxCount</src> and <src>currentCount</src> are
254  // used only if <src>includeLimits</src> is defined. In this case, the method will return
255  // when currentCount == maxCount, thus avoiding scanning remaining data unnecessarily.
256 
257  // no weights, no mask, no ranges
258  void _populateArray(
259  vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr, uInt dataStride
260  ) const;
261 
262  // ranges
263  void _populateArray(
264  vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr,
265  uInt dataStride, const DataRanges& ranges, Bool isInclude
266  ) const;
267 
268  void _populateArray(
269  vector<AccumType>& ary, const DataIterator& dataBegin,
270  Int64 nr, uInt dataStride, const MaskIterator& maskBegin,
271  uInt maskStride
272  ) const;
273 
274  // mask and ranges
275  void _populateArray(
276  vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr,
277  uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
278  const DataRanges& ranges, Bool isInclude
279  ) const;
280 
281  // weights
282  void _populateArray(
283  vector<AccumType>& ary, const DataIterator& dataBegin,
284  const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride
285  ) const;
286 
287  // weights and ranges
288  void _populateArray(
289  vector<AccumType>& ary, const DataIterator& dataBegin,
290  const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride,
291  const DataRanges& ranges, Bool isInclude
292  ) const;
293 
294  // weights and mask
295  void _populateArray(
296  vector<AccumType>& ary, const DataIterator& dataBegin,
297  const WeightsIterator& weightBegin, Int64 nr, uInt dataStride,
298  const MaskIterator& maskBegin, uInt maskStride
299  ) const;
300 
301  // weights, mask, ranges
302  void _populateArray(
303  vector<AccumType>& ary, const DataIterator& dataBegin, const WeightsIterator& weightBegin,
304  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
305  const DataRanges& ranges, Bool isInclude
306  ) const;
307 
308  // no weights, no mask, no ranges
309  virtual void _populateArrays(
310  vector<vector<AccumType> >& arys, uInt& currentCount, const DataIterator& dataBegin, Int64 nr, uInt dataStride,
311  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt maxCount
312  ) const;
313 
314  // ranges
315  virtual void _populateArrays(
316  vector<vector<AccumType> >& arys, uInt& currentCount, const DataIterator& dataBegin, Int64 nr,
317  uInt dataStride, const DataRanges& ranges, Bool isInclude,
318  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt maxCount
319  ) const;
320 
321  virtual void _populateArrays(
322  vector<vector<AccumType> >& arys, uInt& currentCount, const DataIterator& dataBegin,
323  Int64 nr, uInt dataStride, const MaskIterator& maskBegin,
324  uInt maskStride,
325  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt maxCount
326  ) const;
327 
328  // mask and ranges
329  virtual void _populateArrays(
330  vector<vector<AccumType> >& arys, uInt& currentCount, const DataIterator& dataBegin, Int64 nr,
331  uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
332  const DataRanges& ranges, Bool isInclude,
333  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt maxCount
334  ) const;
335 
336  // weights
337  virtual void _populateArrays(
338  vector<vector<AccumType> >& arys, uInt& currentCount, const DataIterator& dataBegin,
339  const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride,
340  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt maxCount
341  ) const;
342 
343  // weights and ranges
344  virtual void _populateArrays(
345  vector<vector<AccumType> >& arys, uInt& currentCount, const DataIterator& dataBegin,
346  const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride,
347  const DataRanges& ranges, Bool isInclude,
348  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt maxCount
349  ) const;
350 
351  // weights and mask
352  virtual void _populateArrays(
353  vector<vector<AccumType> >& arys, uInt& currentCount, const DataIterator& dataBegin,
354  const WeightsIterator& weightBegin, Int64 nr, uInt dataStride,
355  const MaskIterator& maskBegin, uInt maskStride,
356  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt maxCount
357  ) const;
358 
359  // weights, mask, ranges
360  virtual void _populateArrays(
361  vector<vector<AccumType> >& arys, uInt& currentCount, const DataIterator& dataBegin, const WeightsIterator& weightBegin,
362  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
363  const DataRanges& ranges, Bool isInclude,
364  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt maxCount
365  ) const;
366  // </group>
367 
368  // <group>
369  // no weights, no mask, no ranges
371  vector<AccumType>& ary, const DataIterator& dataBegin,
372  Int64 nr, uInt dataStride, uInt maxElements
373  ) const;
374 
375  // ranges
377  vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr,
378  uInt dataStride, const DataRanges& ranges, Bool isInclude,
379  uInt maxElements
380  ) const;
381 
382  // mask
384  vector<AccumType>& ary, const DataIterator& dataBegin,
385  Int64 nr, uInt dataStride, const MaskIterator& maskBegin,
386  uInt maskStride, uInt maxElements
387  ) const;
388 
389  // mask and ranges
391  vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr,
392  uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
393  const DataRanges& ranges, Bool isInclude, uInt maxElements
394  ) const;
395 
396  // weights
398  vector<AccumType>& ary, const DataIterator& dataBegin,
399  const WeightsIterator& weightBegin, Int64 nr, uInt dataStride,
400  uInt maxElements
401  ) const;
402 
403  // weights and ranges
405  vector<AccumType>& ary, const DataIterator& dataBegin,
406  const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride,
407  const DataRanges& ranges, Bool isInclude, uInt maxElements
408  ) const;
409 
410  // weights and mask
412  vector<AccumType>& ary, const DataIterator& dataBegin,
413  const WeightsIterator& weightBegin, Int64 nr,
414  uInt dataStride, const MaskIterator& maskBegin,
415  uInt maskStride, uInt maxElements
416  ) const;
417 
418  // weights, mask, ranges
420  vector<AccumType>& ary, const DataIterator& dataBegin, const WeightsIterator& weightBegin,
421  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
422  const DataRanges& ranges, Bool isInclude,
423  uInt maxElements
424  ) const;
425  // </group>
426 
427  // <group>
428  // no weights, no mask, no ranges
429  void _unweightedStats(
430  uInt64& ngood, AccumType& mymin, AccumType& mymax,
431  Int64& minpos, Int64& maxpos,
432  const DataIterator& dataBegin, Int64 nr, uInt dataStride
433  );
434 
435  // no weights, no mask
436  void _unweightedStats(
437  uInt64& ngood, AccumType& mymin, AccumType& mymax,
438  Int64& minpos, Int64& maxpos,
439  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
440  const DataRanges& ranges, Bool isInclude
441  );
442 
443  void _unweightedStats(
444  uInt64& ngood, AccumType& mymin, AccumType& mymax,
445  Int64& minpos, Int64& maxpos,
446  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
447  const MaskIterator& maskBegin, uInt maskStride
448  );
449 
450  void _unweightedStats(
451  uInt64& ngood, AccumType& mymin, AccumType& mymax,
452  Int64& minpos, Int64& maxpos,
453  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
454  const MaskIterator& maskBegin, uInt maskStride,
455  const DataRanges& ranges, Bool isInclude
456  );
457  // </group>
458 
459  // <group>
460  // has weights, but no mask, no ranges
461  void _weightedStats(
462  AccumType& mymin, AccumType& mymax,
463  Int64& minpos, Int64& maxpos,
464  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
465  Int64 nr, uInt dataStride
466  );
467 
468  void _weightedStats(
469  AccumType& mymin, AccumType& mymax,
470  Int64& minpos, Int64& maxpos,
471  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
472  Int64 nr, uInt dataStride, const DataRanges& ranges, Bool isInclude
473  );
474 
475  void _weightedStats(
476  AccumType& mymin, AccumType& mymax,
477  Int64& minpos, Int64& maxpos,
478  const DataIterator& dataBegin, const WeightsIterator& weightBegin,
479  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride
480  );
481 
482  void _weightedStats(
483  AccumType& mymin, AccumType& mymax,
484  Int64& minpos, Int64& maxpos,
485  const DataIterator& dataBegin, const WeightsIterator& weightBegin,
486  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
487  const DataRanges& ranges, Bool isInclude
488  );
489  // </group>
490 
491 private:
492 
493  // _f defined in inclusion range between Q1 - _f*D and Q3 + _f*D, where D = Q3 - Q1 and
494  // Q1 and Q3 are the first and third quartiles, respectively
497 
498  void _setRange();
499 
500 };
501 
502 }
503 
504 #ifndef CASACORE_NO_AUTO_TEMPLATES
505 #include <casacore/scimath/Mathematics/HingesFencesStatistics.tcc>
506 #endif //# CASACORE_NO_AUTO_TEMPLATES
507 
508 #endif
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
virtual void _minMax(CountedPtr< AccumType > &mymin, CountedPtr< AccumType > &mymax, const DataIterator &dataBegin, Int64 nr, uInt dataStride) const
Class to calculate statistics using the so-called hinges and fences algorithm.
void _unweightedStats(uInt64 &ngood, AccumType &mymin, AccumType &mymax, Int64 &minpos, Int64 &maxpos, const DataIterator &dataBegin, Int64 nr, uInt dataStride)
no weights, no mask, no ranges
unsigned long long uInt64
Definition: aipsxtype.h:39
HingesFencesStatistics(Double f=-1.0)
If f is negative, the full dataset is used; ie the object has the same behavior as a ClassicalStatist...
virtual void reset()
reset object to initial state.
ALGORITHM
implemented algorithms
HingesFencesStatistics< CASA_STATP > & operator=(const HingesFencesStatistics< CASA_STATP > &other)
copy semantics
void _weightedStats(AccumType &mymin, AccumType &mymax, Int64 &minpos, Int64 &maxpos, const DataIterator &dataBegin, const WeightsIterator &weightsBegin, Int64 nr, uInt dataStride)
has weights, but no mask, no ranges
double Double
Definition: aipstype.h:52
#define DataRanges
Commonly used types in statistics framework.
void _populateArray(vector< AccumType > &ary, const DataIterator &dataBegin, Int64 nr, uInt dataStride) const
populate an unsorted array with valid data.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:39
virtual void _findBins(vector< vector< uInt64 > > &binCounts, vector< CountedPtr< AccumType > > &sameVal, vector< Bool > &allSame, const DataIterator &dataBegin, Int64 nr, uInt dataStride, const vector< typename StatisticsUtilities< AccumType >::BinDesc > &binDesc, const vector< AccumType > &maxLimit) const
Abstract base class for statistics algorithms which are characterized by a range of good values...
Double _f
_f defined in inclusion range between Q1 - _f*D and Q3 + _f*D, where D = Q3 - Q1 and Q1 and Q3 are t...
void _accumNpts(uInt64 &npts, const DataIterator &dataStart, Int64 nr, uInt dataStride) const
scan through the data set to determine the number of good (unmasked, weight > 0, within range) points...
void setCalculateAsAdded(Bool c)
This class does not allow statistics to be calculated as datasets are added, so an exception will be ...
const Double c
Fundamental physical constants (SI units):
virtual void _populateArrays(vector< vector< AccumType > > &arys, uInt &currentCount, const DataIterator &dataBegin, Int64 nr, uInt dataStride, const vector< std::pair< AccumType, AccumType > > &includeLimits, uInt maxCount) const
no weights, no mask, no ranges
Bool _populateTestArray(vector< AccumType > &ary, const DataIterator &dataBegin, Int64 nr, uInt dataStride, uInt maxElements) const
no weights, no mask, no ranges
virtual StatisticsData::ALGORITHM algorithm() const
get the algorithm that this object uses for computing stats
this file contains all the compiler specific defines
Definition: mainpage.dox:28
unsigned int uInt
Definition: aipstype.h:48
description of a regularly spaced bins with the first bin having lower limit of minLimit and having n...