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  // Clone this instance. Caller is responsible for deleting the returned pointer.
62  virtual StatisticsAlgorithm<CASA_STATP>* clone() const;
63 
64  // get the algorithm that this object uses for computing stats
67  };
68 
69  // reset object to initial state. Clears all private fields including data,
70  // accumulators, global range. It does not affect the fence factor (_f), which was
71  // set at object construction.
72  virtual void reset();
73 
74  // This class does not allow statistics to be calculated as datasets are added, so
75  // an exception will be thrown if <src>c</src> is True.
77 
78 protected:
79  // <group>
80  // scan through the data set to determine the number of good (unmasked, weight > 0,
81  // within range) points. The first with no mask, no
82  // ranges, and no weights is trivial with npts = nr in this class, but is implemented here
83  // so that derived classes may override it.
84  inline void _accumNpts(
85  uInt64& npts,
86  const DataIterator& dataStart, Int64 nr, uInt dataStride
87  ) const;
88 
89  void _accumNpts(
90  uInt64& npts,
91  const DataIterator& dataStart, Int64 nr, uInt dataStride,
92  const DataRanges& ranges, Bool isInclude
93  ) const;
94 
95  void _accumNpts(
96  uInt64& npts,
97  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
98  const MaskIterator& maskBegin, uInt maskStride
99  ) const;
100 
101  void _accumNpts(
102  uInt64& npts,
103  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
104  const MaskIterator& maskBegin, uInt maskStride, const DataRanges& ranges,
105  Bool isInclude
106  ) const;
107 
108  void _accumNpts(
109  uInt64& npts,
110  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
111  Int64 nr, uInt dataStride
112  ) const;
113 
114  void _accumNpts(
115  uInt64& npts,
116  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
117  Int64 nr, uInt dataStride, const DataRanges& ranges, Bool isInclude
118  ) const;
119 
120  void _accumNpts(
121  uInt64& npts,
122  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
123  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
124  const DataRanges& ranges, Bool isInclude
125  ) const;
126 
127  void _accumNpts(
128  uInt64& npts,
129  const DataIterator& dataBegin, const WeightsIterator& weightBegin,
130  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride
131  ) const;
132  // </group>
133 
134  // <group>
135  virtual void _findBins(
136  vector<vector<uInt64> >& binCounts,
137  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
138  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
139  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc,
140  const vector<AccumType>& maxLimit
141  ) const;
142 
143  virtual void _findBins(
144  vector<vector<uInt64> >& binCounts,
145  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
146  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
147  const DataRanges& ranges, Bool isInclude,
148  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
149  ) const;
150 
151  virtual void _findBins(
152  vector<vector<uInt64> >& binCounts,
153  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
154  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
155  const MaskIterator& maskBegin, uInt maskStride,
156  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
157  ) const;
158 
159  virtual void _findBins(
160  vector<vector<uInt64> >& binCounts,
161  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
162  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
163  const MaskIterator& maskBegin, uInt maskStride, const DataRanges& ranges,
164  Bool isInclude,
165  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
166  ) const;
167 
168  virtual void _findBins(
169  vector<vector<uInt64> >& binCounts,
170  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
171  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
172  Int64 nr, uInt dataStride,
173  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
174  ) const ;
175 
176  virtual void _findBins(
177  vector<vector<uInt64> >& binCounts,
178  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
179  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
180  Int64 nr, uInt dataStride, const DataRanges& ranges, Bool isInclude,
181  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
182  ) const;
183 
184  virtual void _findBins(
185  vector<vector<uInt64> >& binCounts,
186  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
187  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
188  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
189  const DataRanges& ranges, Bool isInclude,
190  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
191  ) const;
192 
193  virtual void _findBins(
194  vector<vector<uInt64> >& binCounts,
195  vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
196  const DataIterator& dataBegin, const WeightsIterator& weightBegin,
197  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
198  const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
199  ) const;
200  // </group>
201 
202  // <group>
203  virtual void _minMax(
205  const DataIterator& dataBegin, Int64 nr, uInt dataStride
206  ) const;
207 
208  virtual void _minMax(
210  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
211  const DataRanges& ranges, Bool isInclude
212  ) const;
213 
214  virtual void _minMax(
216  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
217  const MaskIterator& maskBegin, uInt maskStride
218  ) const;
219 
220  virtual void _minMax(
222  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
223  const MaskIterator& maskBegin, uInt maskStride, const DataRanges& ranges,
224  Bool isInclude
225  ) const;
226 
227  virtual void _minMax(
229  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
230  Int64 nr, uInt dataStride
231  ) const;
232 
233  virtual void _minMax(
235  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
236  Int64 nr, uInt dataStride, const DataRanges& ranges, Bool isInclude
237  ) const;
238 
239  virtual void _minMax(
241  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
242  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
243  const DataRanges& ranges, Bool isInclude
244  ) const;
245 
246  virtual void _minMax(
248  const DataIterator& dataBegin, const WeightsIterator& weightBegin,
249  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride
250  ) const;
251  // </group>
252 
253  //<group>
254  // populate an unsorted array with valid data. If <src>includeLimits</src> is defined,
255  // then restrict values that are entered in the array to those limits (inclusive of the
256  // minimum, exclusive of the maximum). <src>maxCount</src> and <src>currentCount</src> are
257  // used only if <src>includeLimits</src> is defined. In this case, the method will return
258  // when currentCount == maxCount, thus avoiding scanning remaining data unnecessarily.
259 
260  // no weights, no mask, no ranges
261  void _populateArray(
262  vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr, uInt dataStride
263  ) const;
264 
265  // ranges
266  void _populateArray(
267  vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr,
268  uInt dataStride, const DataRanges& ranges, Bool isInclude
269  ) const;
270 
271  void _populateArray(
272  vector<AccumType>& ary, const DataIterator& dataBegin,
273  Int64 nr, uInt dataStride, const MaskIterator& maskBegin,
274  uInt maskStride
275  ) const;
276 
277  // mask and ranges
278  void _populateArray(
279  vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr,
280  uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
281  const DataRanges& ranges, Bool isInclude
282  ) const;
283 
284  // weights
285  void _populateArray(
286  vector<AccumType>& ary, const DataIterator& dataBegin,
287  const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride
288  ) const;
289 
290  // weights and ranges
291  void _populateArray(
292  vector<AccumType>& ary, const DataIterator& dataBegin,
293  const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride,
294  const DataRanges& ranges, Bool isInclude
295  ) const;
296 
297  // weights and mask
298  void _populateArray(
299  vector<AccumType>& ary, const DataIterator& dataBegin,
300  const WeightsIterator& weightBegin, Int64 nr, uInt dataStride,
301  const MaskIterator& maskBegin, uInt maskStride
302  ) const;
303 
304  // weights, mask, ranges
305  void _populateArray(
306  vector<AccumType>& ary, const DataIterator& dataBegin, const WeightsIterator& weightBegin,
307  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
308  const DataRanges& ranges, Bool isInclude
309  ) const;
310 
311  // no weights, no mask, no ranges
312  virtual void _populateArrays(
313  vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin, Int64 nr, uInt dataStride,
314  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
315  ) const;
316 
317  // ranges
318  virtual void _populateArrays(
319  vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin, Int64 nr,
320  uInt dataStride, const DataRanges& ranges, Bool isInclude,
321  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
322  ) const;
323 
324  virtual void _populateArrays(
325  vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin,
326  Int64 nr, uInt dataStride, const MaskIterator& maskBegin,
327  uInt maskStride,
328  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
329  ) const;
330 
331  // mask and ranges
332  virtual void _populateArrays(
333  vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin, Int64 nr,
334  uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
335  const DataRanges& ranges, Bool isInclude,
336  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
337  ) const;
338 
339  // weights
340  virtual void _populateArrays(
341  vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin,
342  const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride,
343  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
344  ) const;
345 
346  // weights and ranges
347  virtual void _populateArrays(
348  vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin,
349  const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride,
350  const DataRanges& ranges, Bool isInclude,
351  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
352  ) const;
353 
354  // weights and mask
355  virtual void _populateArrays(
356  vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin,
357  const WeightsIterator& weightBegin, Int64 nr, uInt dataStride,
358  const MaskIterator& maskBegin, uInt maskStride,
359  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
360  ) const;
361 
362  // weights, mask, ranges
363  virtual void _populateArrays(
364  vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin, const WeightsIterator& weightBegin,
365  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
366  const DataRanges& ranges, Bool isInclude,
367  const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
368  ) const;
369  // </group>
370 
371  // <group>
372  // no weights, no mask, no ranges
374  vector<AccumType>& ary, const DataIterator& dataBegin,
375  Int64 nr, uInt dataStride, uInt maxElements
376  ) const;
377 
378  // ranges
380  vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr,
381  uInt dataStride, const DataRanges& ranges, Bool isInclude,
382  uInt maxElements
383  ) const;
384 
385  // mask
387  vector<AccumType>& ary, const DataIterator& dataBegin,
388  Int64 nr, uInt dataStride, const MaskIterator& maskBegin,
389  uInt maskStride, uInt maxElements
390  ) const;
391 
392  // mask and ranges
394  vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr,
395  uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
396  const DataRanges& ranges, Bool isInclude, uInt maxElements
397  ) const;
398 
399  // weights
401  vector<AccumType>& ary, const DataIterator& dataBegin,
402  const WeightsIterator& weightBegin, Int64 nr, uInt dataStride,
403  uInt maxElements
404  ) const;
405 
406  // weights and ranges
408  vector<AccumType>& ary, const DataIterator& dataBegin,
409  const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride,
410  const DataRanges& ranges, Bool isInclude, uInt maxElements
411  ) const;
412 
413  // weights and mask
415  vector<AccumType>& ary, const DataIterator& dataBegin,
416  const WeightsIterator& weightBegin, Int64 nr,
417  uInt dataStride, const MaskIterator& maskBegin,
418  uInt maskStride, uInt maxElements
419  ) const;
420 
421  // weights, mask, ranges
423  vector<AccumType>& ary, const DataIterator& dataBegin, const WeightsIterator& weightBegin,
424  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
425  const DataRanges& ranges, Bool isInclude,
426  uInt maxElements
427  ) const;
428  // </group>
429 
430  // <group>
431  // no weights, no mask, no ranges
432  void _unweightedStats(
433  StatsData<AccumType>& stats, uInt64& ngood, LocationType& location,
434  const DataIterator& dataBegin, Int64 nr, uInt dataStride
435  );
436 
437  // no weights, no mask
438  void _unweightedStats(
439  StatsData<AccumType>& stats, uInt64& ngood, LocationType& location,
440  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
441  const DataRanges& ranges, Bool isInclude
442  );
443 
444  void _unweightedStats(
445  StatsData<AccumType>& stats, uInt64& ngood, LocationType& location,
446  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
447  const MaskIterator& maskBegin, uInt maskStride
448  );
449 
450  void _unweightedStats(
451  StatsData<AccumType>& stats, uInt64& ngood, LocationType& location,
452  const DataIterator& dataBegin, Int64 nr, uInt dataStride,
453  const MaskIterator& maskBegin, uInt maskStride,
454  const DataRanges& ranges, Bool isInclude
455  );
456  // </group>
457 
458  // <group>
459  // has weights, but no mask, no ranges
460  void _weightedStats(
461  StatsData<AccumType>& stats, LocationType& location,
462  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
463  Int64 nr, uInt dataStride
464  );
465 
466  void _weightedStats(
467  StatsData<AccumType>& stats, LocationType& location,
468  const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
469  Int64 nr, uInt dataStride, const DataRanges& ranges, Bool isInclude
470  );
471 
472  void _weightedStats(
473  StatsData<AccumType>& stats, LocationType& location,
474  const DataIterator& dataBegin, const WeightsIterator& weightBegin,
475  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride
476  );
477 
478  void _weightedStats(
479  StatsData<AccumType>& stats, LocationType& location,
480  const DataIterator& dataBegin, const WeightsIterator& weightBegin,
481  Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
482  const DataRanges& ranges, Bool isInclude
483  );
484  // </group>
485 
486 private:
487 
488  // _f defined in inclusion range between Q1 - _f*D and Q3 + _f*D, where D = Q3 - Q1 and
489  // Q1 and Q3 are the first and third quartiles, respectively
492 
493  void _setRange();
494 
495 };
496 
497 }
498 
499 #ifndef CASACORE_NO_AUTO_TEMPLATES
500 #include <casacore/scimath/Mathematics/HingesFencesStatistics.tcc>
501 #endif //# CASACORE_NO_AUTO_TEMPLATES
502 
503 #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 _weightedStats(StatsData< AccumType > &stats, LocationType &location, const DataIterator &dataBegin, const WeightsIterator &weightsBegin, Int64 nr, uInt dataStride)
has weights, but no mask, no ranges
unsigned long long uInt64
Definition: aipsxtype.h:39
std::pair< Int64, Int64 > LocationType
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
double Double
Definition: aipstype.h:55
virtual StatisticsAlgorithm< CASA_STATP > * clone() const
Clone this instance.
#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:42
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...
virtual void _populateArrays(vector< vector< AccumType > > &arys, uInt64 &currentCount, const DataIterator &dataBegin, Int64 nr, uInt dataStride, const vector< std::pair< AccumType, AccumType > > &includeLimits, uInt64 maxCount) const
no weights, no mask, no ranges
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 ...
void _unweightedStats(StatsData< AccumType > &stats, uInt64 &ngood, LocationType &location, const DataIterator &dataBegin, Int64 nr, uInt dataStride)
no weights, no mask, no ranges
const Double c
Fundamental physical constants (SI units):
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:51
description of a regularly spaced bins with the first bin having lower limit of minLimit and having n...