00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef SCIMATH_STATSALGORITHM_H
00028 #define SCIMATH_STATSALGORITHM_H
00029
00030 #include <casacore/casa/aips.h>
00031 #include <casacore/casa/Exceptions/Error.h>
00032 #include <casacore/casa/Utilities/CountedPtr.h>
00033 #include <casacore/scimath/Mathematics/StatsDataProvider.h>
00034 #include <casacore/scimath/Mathematics/StatisticsData.h>
00035 #include <casacore/scimath/Mathematics/StatisticsTypes.h>
00036
00037 #include <map>
00038 #include <set>
00039 #include <vector>
00040
00041
00042 #define CASA_STATD template <class AccumType, class DataIterator, class MaskIterator, class WeightsIterator>
00043 #define CASA_STATP AccumType, DataIterator, MaskIterator, WeightsIterator
00044
00045 namespace casacore {
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 template <class AccumType, class DataIterator, class MaskIterator=const Bool *, class WeightsIterator=DataIterator>
00105 class StatisticsAlgorithm {
00106
00107 public:
00108
00109 virtual ~StatisticsAlgorithm();
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 virtual void addData(
00127 const DataIterator& first, uInt nr, uInt dataStride=1,
00128 Bool nrAccountsForStride=False
00129 );
00130
00131 virtual void addData(
00132 const DataIterator& first, uInt nr,
00133 const DataRanges& dataRanges, Bool isInclude=True, uInt dataStride=1,
00134 Bool nrAccountsForStride=False
00135 );
00136
00137 virtual void addData(
00138 const DataIterator& first, const MaskIterator& maskFirst,
00139 uInt nr, uInt dataStride=1, Bool nrAccountsForStride=False, uInt maskStride=1
00140 );
00141
00142 virtual void addData(
00143 const DataIterator& first, const MaskIterator& maskFirst,
00144 uInt nr, const DataRanges& dataRanges,
00145 Bool isInclude=True, uInt dataStride=1, Bool nrAccountsForStride=False,
00146 uInt maskStride=1
00147 );
00148
00149 virtual void addData(
00150 const DataIterator& first, const WeightsIterator& weightFirst,
00151 uInt nr, uInt dataStride=1, Bool nrAccountsForStride=False
00152 );
00153
00154 virtual void addData(
00155 const DataIterator& first, const WeightsIterator& weightFirst,
00156 uInt nr, const DataRanges& dataRanges,
00157 Bool isInclude=True, uInt dataStride=1, Bool nrAccountsForStride=False
00158 );
00159
00160 virtual void addData(
00161 const DataIterator& first, const WeightsIterator& weightFirst,
00162 const MaskIterator& maskFirst, uInt nr, uInt dataStride=1,
00163 Bool nrAccountsForStride=False,
00164 uInt maskStride=1
00165 );
00166
00167 virtual void addData(
00168 const DataIterator& first, const WeightsIterator& weightFirst,
00169 const MaskIterator& maskFirst, uInt nr, const DataRanges& dataRanges,
00170 Bool isInclude=True, uInt dataStride=1, Bool nrAccountsForStride=False,
00171 uInt maskStride=1
00172 );
00173
00174
00175
00176 virtual StatisticsData::ALGORITHM algorithm() const = 0;
00177
00178
00179 void deleteSortedArray();
00180
00181 virtual AccumType getMedian(
00182 CountedPtr<uInt64> knownNpts=NULL, CountedPtr<AccumType> knownMin=NULL,
00183 CountedPtr<AccumType> knownMax=NULL, uInt binningThreshholdSizeBytes=4096*4096,
00184 Bool persistSortedArray=False, uInt64 nBins=10000
00185 ) = 0;
00186
00187
00188 virtual AccumType getMedianAndQuantiles(
00189 std::map<Double, AccumType>& quantileToValue, const std::set<Double>& quantiles,
00190 CountedPtr<uInt64> knownNpts=NULL, CountedPtr<AccumType> knownMin=NULL,
00191 CountedPtr<AccumType> knownMax=NULL,
00192 uInt binningThreshholdSizeBytes=4096*4096, Bool persistSortedArray=False,
00193 uInt64 nBins=10000
00194 ) = 0;
00195
00196
00197 virtual AccumType getMedianAbsDevMed(
00198 CountedPtr<uInt64> knownNpts=NULL,
00199 CountedPtr<AccumType> knownMin=NULL, CountedPtr<AccumType> knownMax=NULL,
00200 uInt binningThreshholdSizeBytes=4096*4096, Bool persistSortedArray=False,
00201 uInt64 nBins=10000
00202 ) = 0;
00203
00204 AccumType getQuantile(
00205 Double quantile, CountedPtr<uInt64> knownNpts=NULL,
00206 CountedPtr<AccumType> knownMin=NULL, CountedPtr<AccumType> knownMax=NULL,
00207 uInt binningThreshholdSizeBytes=4096*4096,
00208 Bool persistSortedArray=False, uInt64 nBins=10000
00209 );
00210
00211
00212 virtual std::map<Double, AccumType> getQuantiles(
00213 const std::set<Double>& quantiles, CountedPtr<uInt64> npts=NULL,
00214 CountedPtr<AccumType> min=NULL, CountedPtr<AccumType> max=NULL,
00215 uInt binningThreshholdSizeBytes=4096*4096, Bool persistSortedArray=False,
00216 uInt64 nBins=10000
00217 ) = 0;
00218
00219
00220 virtual AccumType getStatistic(StatisticsData::STATS stat);
00221
00222
00223
00224
00225
00226
00227
00228 virtual std::pair<Int64, Int64> getStatisticIndex(StatisticsData::STATS stat) = 0;
00229
00230 virtual StatsData<AccumType> getStatistics();
00231
00232
00233
00234
00235
00236 virtual void setData(const DataIterator& first, uInt nr, uInt dataStride=1, Bool nrAccountsForStride=False);
00237
00238 virtual void setData(
00239 const DataIterator& first, uInt nr,
00240 const DataRanges& dataRanges, Bool isInclude=True, uInt dataStride=1,
00241 Bool nrAccountsForStride=False
00242 );
00243
00244 virtual void setData(
00245 const DataIterator& first, const MaskIterator& maskFirst,
00246 uInt nr, uInt dataStride=1, Bool nrAccountsForStride=False,
00247 uInt maskStride=1
00248 );
00249
00250 virtual void setData(
00251 const DataIterator& first, const MaskIterator& maskFirst,
00252 uInt nr, const DataRanges& dataRanges,
00253 Bool isInclude=True, uInt dataStride=1, Bool nrAccountsForStride=False,
00254 uInt maskStride=1
00255 );
00256
00257 virtual void setData(
00258 const DataIterator& first, const WeightsIterator& weightFirst,
00259 uInt nr, uInt dataStride=1,
00260 Bool nrAccountsForStride=False
00261 );
00262
00263 virtual void setData(
00264 const DataIterator& first, const WeightsIterator& weightFirst,
00265 uInt nr, const DataRanges& dataRanges,
00266 Bool isInclude=True, uInt dataStride=1,
00267 Bool nrAccountsForStride=False
00268 );
00269
00270 virtual void setData(
00271 const DataIterator& first, const WeightsIterator& weightFirst,
00272 const MaskIterator& maskFirst, uInt nr, uInt dataStride=1,
00273 Bool nrAccountsForStride=False,
00274 uInt maskStride=1
00275 );
00276
00277 virtual void setData(
00278 const DataIterator& first, const WeightsIterator& weightFirst,
00279 const MaskIterator& maskFirst, uInt nr, const DataRanges& dataRanges,
00280 Bool isInclude=True, uInt dataStride=1, Bool nrAccountsForStride=False,
00281 uInt maskStride=1
00282 );
00283
00284
00285
00286
00287
00288 virtual void setDataProvider(StatsDataProvider<CASA_STATP> *dataProvider) {
00289 ThrowIf(! dataProvider, "Logic Error: data provider cannot be NULL");
00290 _clearData();
00291 _dataProvider = dataProvider;
00292 }
00293
00294
00295
00296 virtual void setStatsToCalculate(std::set<StatisticsData::STATS>& stats);
00297
00298 protected:
00299 StatisticsAlgorithm();
00300
00301
00302 StatisticsAlgorithm<CASA_STATP>& operator=(
00303 const StatisticsAlgorithm<CASA_STATP>& other
00304 );
00305
00306
00307
00308 virtual void _addData() {}
00309
00310 virtual void _clearData();
00311
00312 const vector<Int64>& _getCounts() const { return _counts; }
00313
00314 const vector<DataIterator>& _getData() const { return _data; }
00315
00316 StatsDataProvider<CASA_STATP>* _getDataProvider() {
00317 return _dataProvider;
00318 }
00319
00320 const vector<uInt>& _getDataStrides() const { return _dataStrides; }
00321
00322 const std::map<uInt, Bool>& _getIsIncludeRanges() const { return _isIncludeRanges; }
00323
00324 const std::map<uInt, MaskIterator> _getMasks() const { return _masks; }
00325
00326 const std::map<uInt, uInt>& _getMaskStrides() const { return _maskStrides; }
00327
00328 const std::map<uInt, DataRanges>& _getRanges() const { return _dataRanges; }
00329
00330 virtual AccumType _getStatistic(StatisticsData::STATS stat) = 0;
00331
00332 virtual StatsData<AccumType> _getStatistics() = 0;
00333
00334 const std::set<StatisticsData::STATS> _getStatsToCalculate() const {
00335 return _statsToCalculate;
00336 }
00337
00338 std::vector<AccumType>& _getSortedArray() { return _sortedArray; }
00339
00340 virtual const std::set<StatisticsData::STATS>& _getUnsupportedStatistics() const {
00341 return _unsupportedStats;
00342 }
00343
00344 const std::map<uInt, WeightsIterator>& _getWeights() const {
00345 return _weights;
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358 static std::map<uInt64, AccumType> _valuesFromArray(
00359 vector<AccumType>& myArray, const std::set<uInt64>& indices
00360 );
00361
00362 void _setSortedArray(const vector<AccumType>& v) { _sortedArray = v; }
00363
00364 private:
00365 vector<DataIterator> _data;
00366
00367 std::map<uInt, WeightsIterator> _weights;
00368
00369 std::map<uInt, MaskIterator> _masks;
00370 vector<Int64> _counts;
00371 vector<uInt> _dataStrides;
00372 std::map<uInt, uInt> _maskStrides;
00373 std::map<uInt, Bool> _isIncludeRanges;
00374 std::map<uInt, DataRanges> _dataRanges;
00375 vector<AccumType> _sortedArray;
00376 std::set<StatisticsData::STATS> _statsToCalculate, _unsupportedStats;
00377 StatsDataProvider<CASA_STATP> *_dataProvider;
00378
00379 void _throwIfDataProviderDefined() const;
00380 };
00381
00382 }
00383
00384 #ifndef CASACORE_NO_AUTO_TEMPLATES
00385 #include <casacore/scimath/Mathematics/StatisticsAlgorithm.tcc>
00386 #endif
00387
00388 #endif