casacore
Loading...
Searching...
No Matches
FitToHalfStatistics.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: casa-feedback@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
26#ifndef SCIMATH_FITTOHALFSTATISTICS_H
27#define SCIMATH_FITTOHALFSTATISTICS_H
28
29#include <casacore/casa/aips.h>
30
31#include <casacore/scimath/StatsFramework/ConstrainedRangeStatistics.h>
32#include <casacore/scimath/StatsFramework/FitToHalfStatisticsData.h>
33
34namespace casacore {
35
36// Class to calculate statistics using the so-called fit to half algorithm. In
37// this algorithm, a center value is specified, and only points greater or equal
38// or less or equal this value are included. Furthermore, each of the included
39// points is reflected about the center value, and these virtual points are
40// added to the included points and the union of sets of included real points
41// and virtual points are used for computing statistics. The specified center
42// point is therefore the mean and median of the resulting distribution, and the
43// total number of points is exactly twice the number of real data points that
44// are included.
45//
46// This class uses a ConstrainedRangeQuantileComputer object for computing
47// quantile-like statistics. See class documentation for StatisticsAlgorithm for
48// details regarding QuantileComputer classes.
49
50template <class AccumType, class DataIterator, class MaskIterator = const Bool*,
51 class WeightsIterator = DataIterator>
53 public:
54 const static AccumType TWO;
55
56 // <src>value</src> is only used if <src>center</src>=CVALUE
60 AccumType value = 0);
61
62 // copy semantics
64
66
67 // copy semantics
69
70 // Clone this instance. Caller is responsible for deleting the returned
71 // pointer.
73
74 // get the algorithm that this object uses for computing stats
76
77 // The median is just the center value, so none of the parameters to this
78 // method are used.
79 AccumType getMedian(std::shared_ptr<uInt64> knownNpts = nullptr,
80 std::shared_ptr<AccumType> knownMin = nullptr,
81 std::shared_ptr<AccumType> knownMax = nullptr,
82 uInt binningThreshholdSizeBytes = 4096 * 4096,
83 Bool persistSortedArray = False, uInt nBins = 10000);
84
85 // <group>
86 // In the following group of methods, if the size of the composite dataset
87 // is smaller than <src>binningThreshholdSizeBytes</src>, the composite
88 // dataset will be (perhaps partially) sorted and persisted in memory during
89 // the call. In that case, and if <src>persistSortedArray</src> is True,
90 // this sorted array will remain in memory after the call and will be used
91 // on subsequent calls of this method when
92 // <src>binningThreshholdSizeBytes</src> is greater than the size of the
93 // composite dataset. If <src>persistSortedArray</src> is False, the sorted
94 // array will not be stored after this call completes and so any subsequent
95 // calls for which the dataset size is less than
96 // <src>binningThreshholdSizeBytes</src>, the dataset will be sorted from
97 // scratch. Values which are not included due to non-unity strides, are not
98 // included in any specified ranges, are masked, or have associated weights
99 // of zero are not considered as dataset members for quantile computations.
100 // If one has a priori information regarding the number of points (npts)
101 // and/or the minimum and maximum values of the data set, these can be
102 // supplied to improve performance. Note however, that if these values are
103 // not correct, the resulting median and/or quantile values will also not be
104 // correct (although see the following notes regarding max/min). Note that
105 // if this object has already had getStatistics() called, and the min and
106 // max were calculated, there is no need to pass these values in as they
107 // have been stored internally and will be used (although passing them
108 // explicitly shouldn't hurt anything). If provided, npts, the number of
109 // points falling in the specified ranges which are not masked and have
110 // weights > 0, should be correct. <src>min</src> can be less than the true
111 // minimum, and <src>max</src> can be greater than the True maximum, but for
112 // best performance, these should be as close to the actual min and max as
113 // possible (and ideally the actual min/max values of the data set).
114 AccumType getMedianAndQuantiles(std::map<Double, AccumType>& quantiles,
115 const std::set<Double>& fractions,
116 std::shared_ptr<uInt64> knownNpts = nullptr,
117 std::shared_ptr<AccumType> knownMin = nullptr,
118 std::shared_ptr<AccumType> knownMax = nullptr,
119 uInt binningThreshholdSizeBytes = 4096 * 4096,
120 Bool persistSortedArray = False, uInt nBins = 10000);
121
122 // get the median of the absolute deviation about the median of the data.
123 AccumType getMedianAbsDevMed(std::shared_ptr<uInt64> knownNpts = nullptr,
124 std::shared_ptr<AccumType> knownMin = nullptr,
125 std::shared_ptr<AccumType> knownMax = nullptr,
126 uInt binningThreshholdSizeBytes = 4096 * 4096,
127 Bool persistSortedArray = False, uInt nBins = 10000);
128
129 // Get the specified quantiles. <src>fractions</src> must be between 0 and
130 // 1, noninclusive.
131 std::map<Double, AccumType> getQuantiles(const std::set<Double>& fractions,
132 std::shared_ptr<uInt64> knownNpts = nullptr,
133 std::shared_ptr<AccumType> knownMin = nullptr,
134 std::shared_ptr<AccumType> knownMax = nullptr,
135 uInt binningThreshholdSizeBytes = 4096 * 4096,
136 Bool persistSortedArray = False, uInt nBins = 10000);
137 // </group>
138
139 // scan the dataset(s) that have been added, and find the min and max.
140 // This method may be called even if setStatsToCaclulate has been called and
141 // MAX and MIN has been excluded.
142 virtual void getMinMax(AccumType& mymin, AccumType& mymax);
143
144 // scan the dataset(s) that have been added, and find the number of good
145 // points. This method may be called even if setStatsToCaclulate has been
146 // called and NPTS has been excluded. If setCalculateAsAdded(True) has
147 // previously been called after this object has been (re)initialized, an
148 // exception will be thrown.
150
151 // reset object to initial state. Clears all private fields including data,
152 // accumulators, global range. It does not affect the center type, center
153 // value, or which "side" to use which were set at construction.
154 virtual void reset();
155
156 // This class does not allow statistics to be calculated as datasets are
157 // added, so an exception will be thrown if <src>c</src> is True.
159
160 // Override base class method by requiring mean to be computed in addition
161 // to what is added in stats if the requested center value is CMEAN.
162 virtual void setStatsToCalculate(std::set<StatisticsData::STATS>& stats);
163
164 protected:
166
168
170
171 inline const StatsData<AccumType>& _getStatsData() const { return _statsData; }
172
173 // <group>
174 // no weights, no mask, no ranges
176 const DataIterator& dataBegin, uInt64 nr, uInt dataStride);
177
178 // no weights, no mask
180 const DataIterator& dataBegin, uInt64 nr, uInt dataStride,
181 const DataRanges& ranges, Bool isInclude);
182
184 const DataIterator& dataBegin, uInt64 nr, uInt dataStride,
185 const MaskIterator& maskBegin, uInt maskStride);
186
188 const DataIterator& dataBegin, uInt64 nr, uInt dataStride,
189 const MaskIterator& maskBegin, uInt maskStride, const DataRanges& ranges,
190 Bool isInclude);
191 // </group>
192
194
195 // <group>
196 // has weights, but no mask, no ranges
198 const DataIterator& dataBegin, const WeightsIterator& weightsBegin, uInt64 nr,
199 uInt dataStride);
200
202 const DataIterator& dataBegin, const WeightsIterator& weightsBegin, uInt64 nr,
203 uInt dataStride, const DataRanges& ranges, Bool isInclude);
204
206 const DataIterator& dataBegin, const WeightsIterator& weightBegin, uInt64 nr,
207 uInt dataStride, const MaskIterator& maskBegin, uInt maskStride);
208
210 const DataIterator& dataBegin, const WeightsIterator& weightBegin, uInt64 nr,
211 uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
212 const DataRanges& ranges, Bool isInclude);
213 // </group>
214
215 private:
218 AccumType _centerValue;
221 // these are the max and min for the real portion of the dataset
222 std::shared_ptr<AccumType> _realMax{}, _realMin{};
224 // This is used for convenience and performance. It should always
225 // be the same as the _range value used in the base
226 // ConstrainedRangeStatistics object
227 std::shared_ptr<std::pair<AccumType, AccumType>> _range;
228
229 // get the min max of the entire (real + virtual) data set. Only
230 // used for quantile computation
231 void _getMinMax(std::shared_ptr<AccumType>& realMin, std::shared_ptr<AccumType>& realMax,
232 std::shared_ptr<AccumType> knownMin, std::shared_ptr<AccumType> knownMax);
233
234 // get the min/max of the real portion only of the dataset
235 void _getRealMinMax(AccumType& realMin, AccumType& realMax);
236
237 void _setRange();
238};
239
240} // namespace casacore
241
242#ifndef CASACORE_NO_AUTO_TEMPLATES
243#include <casacore/scimath/StatsFramework/FitToHalfStatistics.tcc>
244#endif
245
246#endif
#define DataRanges
USE_DATA
which section of data to use, greater than or less than the center value
CENTER
choice of center point based on the corresponding statistics from the entire distribution of data,...
virtual StatsData< AccumType > _getStatistics()
void _weightedStats(StatsData< AccumType > &stats, LocationType &location, const DataIterator &dataBegin, const WeightsIterator &weightBegin, uInt64 nr, uInt dataStride, const MaskIterator &maskBegin, uInt maskStride, const DataRanges &ranges, Bool isInclude)
void _updateDataProviderMaxMin(const StatsData< AccumType > &threadStats)
virtual void getMinMax(AccumType &mymin, AccumType &mymax)
scan the dataset(s) that have been added, and find the min and max.
FitToHalfStatistics< CASA_STATP > & operator=(const FitToHalfStatistics< CASA_STATP > &other)
copy semantics
FitToHalfStatistics(const FitToHalfStatistics< CASA_STATP > &other)
copy semantics
virtual StatsData< AccumType > _getInitialStats() const
const StatsData< AccumType > & _getStatsData() const
AccumType getMedianAndQuantiles(std::map< Double, AccumType > &quantiles, const std::set< Double > &fractions, std::shared_ptr< uInt64 > knownNpts=nullptr, std::shared_ptr< AccumType > knownMin=nullptr, std::shared_ptr< AccumType > knownMax=nullptr, uInt binningThreshholdSizeBytes=4096 *4096, Bool persistSortedArray=False, uInt nBins=10000)
In the following group of methods, if the size of the composite dataset is smaller than binningThresh...
StatsData< AccumType > & _getStatsData()
Retrieve stats structure.
FitToHalfStatisticsData::CENTER _centerType
virtual StatisticsAlgorithm< CASA_STATP > * clone() const
Clone this instance.
AccumType getMedianAbsDevMed(std::shared_ptr< uInt64 > knownNpts=nullptr, std::shared_ptr< AccumType > knownMin=nullptr, std::shared_ptr< AccumType > knownMax=nullptr, uInt binningThreshholdSizeBytes=4096 *4096, Bool persistSortedArray=False, uInt nBins=10000)
get the median of the absolute deviation about the median of the data.
std::shared_ptr< AccumType > _realMin
void _unweightedStats(StatsData< AccumType > &stats, uInt64 &ngood, LocationType &location, const DataIterator &dataBegin, uInt64 nr, uInt dataStride, const MaskIterator &maskBegin, uInt maskStride, const DataRanges &ranges, Bool isInclude)
void _weightedStats(StatsData< AccumType > &stats, LocationType &location, const DataIterator &dataBegin, const WeightsIterator &weightsBegin, uInt64 nr, uInt dataStride, const DataRanges &ranges, Bool isInclude)
FitToHalfStatistics(FitToHalfStatisticsData::CENTER center=FitToHalfStatisticsData::CMEAN, FitToHalfStatisticsData::USE_DATA useData=FitToHalfStatisticsData::LE_CENTER, AccumType value=0)
value is only used if center=CVALUE
void _unweightedStats(StatsData< AccumType > &stats, uInt64 &ngood, LocationType &location, const DataIterator &dataBegin, uInt64 nr, uInt dataStride, const DataRanges &ranges, Bool isInclude)
no weights, no mask
std::shared_ptr< AccumType > _realMax
these are the max and min for the real portion of the dataset
void _unweightedStats(StatsData< AccumType > &stats, uInt64 &ngood, LocationType &location, const DataIterator &dataBegin, uInt64 nr, uInt dataStride)
no weights, no mask, no ranges
virtual void setStatsToCalculate(std::set< StatisticsData::STATS > &stats)
Override base class method by requiring mean to be computed in addition to what is added in stats if ...
AccumType getMedian(std::shared_ptr< uInt64 > knownNpts=nullptr, std::shared_ptr< AccumType > knownMin=nullptr, std::shared_ptr< AccumType > knownMax=nullptr, uInt binningThreshholdSizeBytes=4096 *4096, Bool persistSortedArray=False, uInt nBins=10000)
The median is just the center value, so none of the parameters to this method are used.
std::shared_ptr< std::pair< AccumType, AccumType > > _range
This is used for convenience and performance.
StatsData< AccumType > _statsData
virtual StatisticsData::ALGORITHM algorithm() const
get the algorithm that this object uses for computing stats
void _weightedStats(StatsData< AccumType > &stats, LocationType &location, const DataIterator &dataBegin, const WeightsIterator &weightBegin, uInt64 nr, uInt dataStride, const MaskIterator &maskBegin, uInt maskStride)
void _unweightedStats(StatsData< AccumType > &stats, uInt64 &ngood, LocationType &location, const DataIterator &dataBegin, uInt64 nr, uInt dataStride, const MaskIterator &maskBegin, uInt maskStride)
uInt64 getNPts()
scan the dataset(s) that have been added, and find the number of good points.
void _getMinMax(std::shared_ptr< AccumType > &realMin, std::shared_ptr< AccumType > &realMax, std::shared_ptr< AccumType > knownMin, std::shared_ptr< AccumType > knownMax)
get the min max of the entire (real + virtual) data set.
void setCalculateAsAdded(Bool c)
This class does not allow statistics to be calculated as datasets are added, so an exception will be ...
std::map< Double, AccumType > getQuantiles(const std::set< Double > &fractions, std::shared_ptr< uInt64 > knownNpts=nullptr, std::shared_ptr< AccumType > knownMin=nullptr, std::shared_ptr< AccumType > knownMax=nullptr, uInt binningThreshholdSizeBytes=4096 *4096, Bool persistSortedArray=False, uInt nBins=10000)
Get the specified quantiles.
virtual void reset()
reset object to initial state.
void _getRealMinMax(AccumType &realMin, AccumType &realMax)
get the min/max of the real portion only of the dataset
void _weightedStats(StatsData< AccumType > &stats, LocationType &location, const DataIterator &dataBegin, const WeightsIterator &weightsBegin, uInt64 nr, uInt dataStride)
has weights, but no mask, no ranges
Base class of statistics algorithm class hierarchy.
ALGORITHM
implemented algorithms
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
std::pair< Int64, Int64 > LocationType
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360
unsigned long long uInt64
Definition aipsxtype.h:37