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_CONSTRAINEDRANGESTATISTICS_H
00028 #define SCIMATH_CONSTRAINEDRANGESTATISTICS_H
00029 
00030 #include <casacore/casa/aips.h>
00031 
00032 #include <casacore/scimath/Mathematics/ClassicalStatistics.h>
00033 
00034 #include <set>
00035 #include <vector>
00036 #include <utility>
00037 
00038 namespace casacore {
00039 
00040 
00041 
00042 
00043 template <class AccumType, class DataIterator, class MaskIterator=const Bool*, class WeightsIterator=DataIterator>
00044 class ConstrainedRangeStatistics
00045     : public ClassicalStatistics<CASA_STATP> {
00046 public:
00047 
00048     virtual ~ConstrainedRangeStatistics();
00049 
00050     
00051     ConstrainedRangeStatistics<CASA_STATP>& operator=(
00052         const ConstrainedRangeStatistics<CASA_STATP>& other
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     AccumType getMedian(
00096         CountedPtr<uInt64> knownNpts=NULL, CountedPtr<AccumType> knownMin=NULL,
00097         CountedPtr<AccumType> knownMax=NULL, uInt binningThreshholdSizeBytes=4096*4096,
00098         Bool persistSortedArray=False, uInt64 nBins=10000
00099     );
00100 
00101     
00102     AccumType getMedianAbsDevMed(
00103         CountedPtr<uInt64> knownNpts=NULL,
00104         CountedPtr<AccumType> knownMin=NULL, CountedPtr<AccumType> knownMax=NULL,
00105         uInt binningThreshholdSizeBytes=4096*4096, Bool persistSortedArray=False,
00106         uInt64 nBins=10000
00107     );
00108 
00109     
00110     
00111     
00112     
00113     AccumType getMedianAndQuantiles(
00114         std::map<Double, AccumType>& quantileToValue, const std::set<Double>& quantiles,
00115         CountedPtr<uInt64> knownNpts=NULL, CountedPtr<AccumType> knownMin=NULL,
00116         CountedPtr<AccumType> knownMax=NULL,
00117         uInt binningThreshholdSizeBytes=4096*4096, Bool persistSortedArray=False,
00118         uInt64 nBins=10000
00119     );
00120 
00121     
00122     
00123     std::map<Double, AccumType> getQuantiles(
00124         const std::set<Double>& quantiles, CountedPtr<uInt64> knownNpts=NULL,
00125         CountedPtr<AccumType> knownMin=NULL, CountedPtr<AccumType> knownMax=NULL,
00126         uInt binningThreshholdSizeBytes=4096*4096, Bool persistSortedArray=False,
00127         uInt64 nBins=10000
00128     );
00129     
00130 
00131     
00132     virtual void getMinMax(AccumType& mymin, AccumType& mymax);
00133 
00134     
00135     
00136     
00137     
00138     virtual uInt64 getNPts();
00139 
00140     
00141     std::pair<Int64, Int64> getStatisticIndex(StatisticsData::STATS stat);
00142 
00143     
00144     
00145     
00146     virtual void reset();
00147 
00148 protected:
00149 
00150     ConstrainedRangeStatistics();
00151 
00152     
00153     
00154     
00155     
00156     
00157     inline void _accumNpts(
00158         uInt64& npts,
00159         const DataIterator& dataStart, Int64 nr, uInt dataStride
00160     ) const;
00161 
00162     void _accumNpts(
00163         uInt64& npts,
00164         const DataIterator& dataStart, Int64 nr, uInt dataStride,
00165         const DataRanges& ranges, Bool isInclude
00166     ) const;
00167 
00168     void _accumNpts(
00169         uInt64& npts,
00170         const DataIterator& dataBegin, Int64 nr, uInt dataStride,
00171         const MaskIterator& maskBegin, uInt maskStride
00172     ) const;
00173 
00174     void _accumNpts(
00175         uInt64& npts,
00176         const DataIterator& dataBegin, Int64 nr, uInt dataStride,
00177         const MaskIterator& maskBegin, uInt maskStride, const DataRanges& ranges,
00178         Bool isInclude
00179     ) const;
00180 
00181     void _accumNpts(
00182         uInt64& npts,
00183         const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
00184         Int64 nr, uInt dataStride
00185     ) const;
00186 
00187     void _accumNpts(
00188         uInt64& npts,
00189         const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
00190         Int64 nr, uInt dataStride, const DataRanges& ranges, Bool isInclude
00191     ) const;
00192 
00193     void _accumNpts(
00194         uInt64& npts,
00195         const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
00196         Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
00197         const DataRanges& ranges, Bool isInclude
00198     ) const;
00199 
00200     void _accumNpts(
00201         uInt64& npts,
00202         const DataIterator& dataBegin, const WeightsIterator& weightBegin,
00203         Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride
00204     ) const;
00205     
00206 
00207     virtual void _findBins(
00208         vector<vector<uInt64> >& binCounts,
00209         vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
00210         const DataIterator& dataBegin, Int64 nr, uInt dataStride,
00211         const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc,
00212         const vector<AccumType>& maxLimit
00213     ) const;
00214 
00215     virtual void _findBins(
00216         vector<vector<uInt64> >& binCounts,
00217         vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
00218         const DataIterator& dataBegin, Int64 nr, uInt dataStride,
00219         const DataRanges& ranges, Bool isInclude,
00220         const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
00221     ) const;
00222 
00223     virtual void _findBins(
00224         vector<vector<uInt64> >& binCounts,
00225         vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
00226         const DataIterator& dataBegin, Int64 nr, uInt dataStride,
00227         const MaskIterator& maskBegin, uInt maskStride,
00228         const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
00229     ) const;
00230 
00231     virtual void _findBins(
00232         vector<vector<uInt64> >& binCounts,
00233         vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
00234         const DataIterator& dataBegin, Int64 nr, uInt dataStride,
00235         const MaskIterator& maskBegin, uInt maskStride, const DataRanges& ranges,
00236         Bool isInclude,
00237         const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
00238     ) const;
00239 
00240     virtual void _findBins(
00241         vector<vector<uInt64> >& binCounts,
00242         vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
00243         const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
00244         Int64 nr, uInt dataStride,
00245         const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
00246     ) const ;
00247 
00248     virtual void _findBins(
00249         vector<vector<uInt64> >& binCounts,
00250         vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
00251         const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
00252         Int64 nr, uInt dataStride, const DataRanges& ranges, Bool isInclude,
00253         const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
00254     ) const;
00255 
00256     virtual void _findBins(
00257         vector<vector<uInt64> >& binCounts,
00258         vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
00259         const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
00260         Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
00261         const DataRanges& ranges, Bool isInclude,
00262         const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
00263     ) const;
00264 
00265     virtual void _findBins(
00266         vector<vector<uInt64> >& binCounts,
00267         vector<CountedPtr<AccumType> >& sameVal, vector<Bool>& allSame,
00268         const DataIterator& dataBegin, const WeightsIterator& weightBegin,
00269         Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
00270         const vector<typename StatisticsUtilities<AccumType>::BinDesc>& binDesc, const vector<AccumType>& maxLimit
00271     ) const;
00272     
00273 
00274     AccumType _getStatistic(StatisticsData::STATS stat);
00275 
00276     StatsData<AccumType> _getStatistics();
00277 
00278     inline Bool _isInRange(const AccumType& datum) const;
00279 
00280     
00281     virtual void _minMax(
00282         CountedPtr<AccumType>& mymin, CountedPtr<AccumType>& mymax,
00283         const DataIterator& dataBegin, Int64 nr, uInt dataStride
00284     ) const;
00285 
00286     virtual void _minMax(
00287         CountedPtr<AccumType>& mymin, CountedPtr<AccumType>& mymax,
00288         const DataIterator& dataBegin, Int64 nr, uInt dataStride,
00289         const DataRanges& ranges, Bool isInclude
00290     ) const;
00291 
00292     virtual void _minMax(
00293         CountedPtr<AccumType>& mymin, CountedPtr<AccumType>& mymax,
00294         const DataIterator& dataBegin, Int64 nr, uInt dataStride,
00295         const MaskIterator& maskBegin, uInt maskStride
00296     ) const;
00297 
00298     virtual void _minMax(
00299         CountedPtr<AccumType>& mymin, CountedPtr<AccumType>& mymax,
00300         const DataIterator& dataBegin, Int64 nr, uInt dataStride,
00301         const MaskIterator& maskBegin, uInt maskStride, const DataRanges& ranges,
00302         Bool isInclude
00303     ) const;
00304 
00305     virtual void _minMax(
00306         CountedPtr<AccumType>& mymin, CountedPtr<AccumType>& mymax,
00307         const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
00308         Int64 nr, uInt dataStride
00309     ) const;
00310 
00311     virtual void _minMax(
00312         CountedPtr<AccumType>& mymin, CountedPtr<AccumType>& mymax,
00313         const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
00314         Int64 nr, uInt dataStride, const DataRanges& ranges, Bool isInclude
00315     ) const;
00316 
00317     virtual void _minMax(
00318         CountedPtr<AccumType>& mymin, CountedPtr<AccumType>& mymax,
00319         const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
00320         Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
00321         const DataRanges& ranges, Bool isInclude
00322     ) const;
00323 
00324     virtual void _minMax(
00325         CountedPtr<AccumType>& mymin, CountedPtr<AccumType>& mymax,
00326         const DataIterator& dataBegin, const WeightsIterator& weightBegin,
00327         Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride
00328     ) const;
00329     
00330 
00331     
00332     
00333     
00334     
00335     
00336     
00337 
00338     
00339     void _populateArray(
00340         vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr, uInt dataStride
00341     ) const;
00342 
00343     
00344     void _populateArray(
00345         vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr,
00346         uInt dataStride, const DataRanges& ranges, Bool isInclude
00347     ) const;
00348 
00349     void _populateArray(
00350         vector<AccumType>& ary, const DataIterator& dataBegin,
00351         Int64 nr, uInt dataStride, const MaskIterator& maskBegin,
00352         uInt maskStride
00353     ) const;
00354 
00355     
00356     void _populateArray(
00357         vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr,
00358         uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
00359         const DataRanges& ranges, Bool isInclude
00360     ) const;
00361 
00362     
00363     void _populateArray(
00364         vector<AccumType>& ary, const DataIterator& dataBegin,
00365         const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride
00366     ) const;
00367 
00368     
00369     void _populateArray(
00370         vector<AccumType>& ary, const DataIterator& dataBegin,
00371         const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride,
00372         const DataRanges& ranges, Bool isInclude
00373     ) const;
00374 
00375     
00376     void _populateArray(
00377         vector<AccumType>& ary, const DataIterator& dataBegin,
00378         const WeightsIterator& weightBegin, Int64 nr, uInt dataStride,
00379         const MaskIterator& maskBegin, uInt maskStride
00380     ) const;
00381 
00382     
00383     void _populateArray(
00384         vector<AccumType>& ary, const DataIterator& dataBegin, const WeightsIterator& weightBegin,
00385         Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
00386         const DataRanges& ranges, Bool isInclude
00387     ) const;
00388 
00389     
00390     virtual void _populateArrays(
00391         vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin, Int64 nr, uInt dataStride,
00392         const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
00393     ) const;
00394 
00395     
00396     virtual void _populateArrays(
00397         vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin, Int64 nr,
00398         uInt dataStride, const DataRanges& ranges, Bool isInclude,
00399         const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
00400     ) const;
00401 
00402     virtual void _populateArrays(
00403         vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin,
00404         Int64 nr, uInt dataStride, const MaskIterator& maskBegin,
00405         uInt maskStride,
00406         const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
00407     ) const;
00408 
00409     
00410     virtual void _populateArrays(
00411         vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin, Int64 nr,
00412         uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
00413         const DataRanges& ranges, Bool isInclude,
00414         const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
00415     ) const;
00416 
00417     
00418     virtual void _populateArrays(
00419         vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin,
00420         const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride,
00421         const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
00422     ) const;
00423 
00424     
00425     virtual void _populateArrays(
00426         vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin,
00427         const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride,
00428         const DataRanges& ranges, Bool isInclude,
00429         const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
00430     ) const;
00431 
00432     
00433     virtual void _populateArrays(
00434         vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin,
00435         const WeightsIterator& weightBegin, Int64 nr, uInt dataStride,
00436         const MaskIterator& maskBegin, uInt maskStride,
00437         const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
00438     ) const;
00439 
00440     
00441     virtual void _populateArrays(
00442         vector<vector<AccumType> >& arys, uInt64& currentCount, const DataIterator& dataBegin, const WeightsIterator& weightBegin,
00443         Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
00444         const DataRanges& ranges, Bool isInclude,
00445         const vector<std::pair<AccumType, AccumType> > &includeLimits, uInt64 maxCount
00446     ) const;
00447     
00448 
00449     
00450     
00451     Bool _populateTestArray(
00452         vector<AccumType>& ary, const DataIterator& dataBegin,
00453         Int64 nr, uInt dataStride, uInt maxElements
00454     ) const;
00455 
00456     
00457     Bool _populateTestArray(
00458         vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr,
00459         uInt dataStride, const DataRanges& ranges, Bool isInclude,
00460         uInt maxElements
00461     ) const;
00462 
00463     
00464     Bool _populateTestArray(
00465         vector<AccumType>& ary, const DataIterator& dataBegin,
00466         Int64 nr, uInt dataStride, const MaskIterator& maskBegin,
00467         uInt maskStride, uInt maxElements
00468     ) const;
00469 
00470     
00471     Bool _populateTestArray(
00472         vector<AccumType>& ary, const DataIterator& dataBegin, Int64 nr,
00473         uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
00474         const DataRanges& ranges, Bool isInclude, uInt maxElements
00475     ) const;
00476 
00477     
00478     Bool _populateTestArray(
00479         vector<AccumType>& ary, const DataIterator& dataBegin,
00480         const WeightsIterator& weightBegin, Int64 nr, uInt dataStride,
00481         uInt maxElements
00482     ) const;
00483 
00484     
00485     Bool _populateTestArray(
00486         vector<AccumType>& ary, const DataIterator& dataBegin,
00487         const WeightsIterator& weightsBegin, Int64 nr, uInt dataStride,
00488         const DataRanges& ranges, Bool isInclude, uInt maxElements
00489     ) const;
00490 
00491     
00492     Bool _populateTestArray(
00493         vector<AccumType>& ary, const DataIterator& dataBegin,
00494         const WeightsIterator& weightBegin, Int64 nr,
00495         uInt dataStride, const MaskIterator& maskBegin,
00496         uInt maskStride, uInt maxElements
00497     ) const;
00498 
00499     
00500     Bool _populateTestArray(
00501         vector<AccumType>& ary, const DataIterator& dataBegin, const WeightsIterator& weightBegin,
00502         Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
00503         const DataRanges& ranges, Bool isInclude,
00504         uInt maxElements
00505     ) const;
00506     
00507 
00508     inline void _setRange(CountedPtr<std::pair<AccumType, AccumType> > r) { this->_clearStats(); _range = r; }
00509 
00510     
00511     virtual void _setRange() = 0;
00512 
00513 
00514 
00515 
00516 
00517 
00518 
00519 
00520 
00521 
00522 
00523 
00524 
00525 
00526 
00527 
00528 
00529 
00530 
00531 
00532 
00533 
00534 
00535 
00536 
00537 
00538 
00539 
00540 
00541 
00542 
00543 
00544 
00545 
00546 
00547 
00548 
00549 
00550 
00551 
00552 
00553 
00554 
00555 
00556 
00557 
00558 
00559 
00560 
00561 
00562 
00563 
00564 
00565 
00566 
00567 
00568 
00569 
00570 
00571 
00572 
00573 
00574 
00575 
00576 
00577 
00578     
00579     
00580     void _unweightedStats(
00581         StatsData<AccumType>& stats, uInt64& ngood, 
00582 
00583         LocationType& location, const DataIterator& dataBegin, Int64 nr,
00584         uInt dataStride
00585     );
00586 
00587     
00588     void _unweightedStats(
00589         StatsData<AccumType>& stats, uInt64& ngood, 
00590 
00591         LocationType& location, const DataIterator& dataBegin, Int64 nr,
00592         uInt dataStride, const DataRanges& ranges, Bool isInclude
00593     );
00594 
00595     void _unweightedStats(
00596         StatsData<AccumType>& stats, uInt64& ngood, 
00597 
00598         LocationType& location, const DataIterator& dataBegin, Int64 nr,
00599         uInt dataStride, const MaskIterator& maskBegin, uInt maskStride
00600     );
00601 
00602     void _unweightedStats(
00603         StatsData<AccumType>& stats, uInt64& ngood, 
00604 
00605         LocationType& location, const DataIterator& dataBegin, Int64 nr,
00606         uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
00607         const DataRanges& ranges, Bool isInclude
00608     );
00609     
00610 
00611     
00612     
00613     void _weightedStats(
00614         StatsData<AccumType>& stats, 
00615 LocationType& location,
00616         const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
00617         Int64 nr, uInt dataStride
00618     );
00619 
00620     void _weightedStats(
00621         StatsData<AccumType>& stats, 
00622  LocationType& location,
00623         const DataIterator& dataBegin, const WeightsIterator& weightsBegin,
00624         Int64 nr, uInt dataStride, const DataRanges& ranges, Bool isInclude
00625     );
00626 
00627     void _weightedStats(
00628         StatsData<AccumType>& stats, 
00629  LocationType& location,
00630         const DataIterator& dataBegin, const WeightsIterator& weightBegin,
00631         Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride
00632     );
00633 
00634     void _weightedStats(
00635         StatsData<AccumType>& stats, 
00636  LocationType& location,
00637         const DataIterator& dataBegin, const WeightsIterator& weightBegin,
00638         Int64 nr, uInt dataStride, const MaskIterator& maskBegin, uInt maskStride,
00639         const DataRanges& ranges, Bool isInclude
00640     );
00641     
00642 
00643 private:
00644     CountedPtr<std::pair<AccumType, AccumType> > _range;
00645     Bool _doMedAbsDevMed;
00646 
00647 };
00648 
00649 }
00650 
00651 #ifndef CASACORE_NO_AUTO_TEMPLATES
00652 #include <casacore/scimath/Mathematics/ConstrainedRangeStatistics.tcc>
00653 #endif
00654 
00655 #endif