22 #define mUndefReplacement 0 46 , needvariance_(false)
47 , needmostfreq_(false) {}
49 CalcSetup& require(
Type);
51 static int medianEvenHandling();
110 inline double getValue(
Type)
const;
111 inline int getIndex(
Type)
const;
113 inline bool hasUndefs()
const {
return nrused_ != nradded_; }
114 inline int size(
bool used=
true )
const 115 {
return used ? nrused_ : nradded_; }
117 inline bool isEmpty()
const {
return size() == 0; }
119 inline int count()
const {
return nrused_; }
120 inline double average()
const;
121 inline T mostFreq()
const;
122 inline T sum()
const;
123 inline T min(
int* index_of_min=0)
const;
124 inline T max(
int* index_of_max=0)
const;
125 inline T extreme(
int* index_of_extr=0)
const;
126 inline T median(
int* index_of_median=0)
const;
127 inline T sqSum()
const;
128 inline double rms()
const;
129 inline double stdDev()
const;
130 inline double normvariance()
const;
131 virtual inline double variance()
const;
133 inline T clipVal(
float ratio,
bool upper)
const;
140 : setup_(s) {
clear(); }
160 inline bool isZero(
const T& t )
const;
174 template <
class T>
inline 197 inline RunCalc<T>& addValue(T data,T weight=1);
198 inline RunCalc<T>& addValues(
int sz,
const T* data,
const T* weights=0);
199 inline RunCalc<T>& replaceValue(T olddata,T newdata,T wt=1);
200 inline RunCalc<T>& removeValue(T data,T weight=1);
202 inline RunCalc<T>& operator +=( T t ) {
return addValue(t); }
208 inline void setMostFrequent(T val,T wt);
243 , wts_(calc_.isWeighted() ? new T [sz] : 0)
244 , vals_(new T [sz]) {
clear(); }
246 {
delete [] vals_;
delete [] wts_; }
249 inline WindowedCalc& addValue(T data,T weight=1);
250 inline WindowedCalc& operator +=( T t ) {
return addValue(t); }
252 inline int getIndex(
Type)
const;
254 inline double getValue(
Type)
const;
256 inline T
count()
const {
return full_ ? sz_ : posidx_; }
258 # define mRunCalc_DefEqFn(ret,fn) \ 259 inline ret fn() const { return calc_.fn(); } 268 # undef mRunCalc_DefEqFn 270 inline T min(
int* i=0)
const;
271 inline T max(
int* i=0)
const;
272 inline T extreme(
int* i=0)
const;
273 inline T median(
int* i=0)
const;
291 template <
class T>
inline 294 sum_x_ = sum_w_ = sum_xx_ = sum_wx_ = sum_wxx_ = 0;
295 nradded_ = nrused_ = minidx_ = maxidx_ = 0;
296 minval_ = maxval_ =
mUdf(T);
297 clss_.erase(); clsswt_.erase(); medvals_.erase(); medwts_.erase();
301 template <
class T>
inline 306 case Count:
return count();
307 case Average:
return average();
308 case Median:
return median();
309 case RMS:
return rms();
310 case StdDev:
return stdDev();
313 case Min:
return min();
314 case Max:
return max();
315 case Extreme:
return extreme();
316 case Sum:
return sum();
317 case SqSum:
return sqSum();
325 template <
class T>
inline 331 case Min: (void)min( &ret );
break;
332 case Max: (void)max( &ret );
break;
333 case Extreme: (void)extreme( &ret );
break;
334 case Median: (void)median( &ret );
break;
335 default: ret = 0;
break;
342 #undef mBaseCalc_ChkEmpty 343 #define mBaseCalc_ChkEmpty(typ) \ 344 if ( nrused_ < 1 ) return mUdf(typ); 361 if ( !setup_.weighted_ )
362 return ((
double)sum_x_) / nrused_;
364 return this->isZero(sum_w_) ?
mUdf(
double) : ((double)sum_wx_) / sum_w_;
389 if ( !setup_.weighted_ )
390 return Math::Sqrt( ((
double)sum_xx_) / nrused_ );
392 return this->isZero(sum_w_) ?
mUdf(
double)
400 if ( nrused_ < 2 )
return 0;
402 if ( !setup_.weighted_ )
403 return ( sum_xx_ - (sum_x_ * ((
double)sum_x_) / nrused_) )
406 return (sum_wxx_ - (sum_wx_ * ((
double)sum_wx_) / sum_w_) )
407 / ( (nrused_-1) * ((double)sum_w_) / nrused_ );
414 pErrMsg(
"Undefined operation for float_complex in template");
422 if ( nrused_ < 2 )
return 0;
425 double avg = average();
427 const double divisor = avg*avg + fact*var;
431 return var / divisor;
438 if ( index_of_min ) *index_of_min = minidx_;
447 if ( index_of_max ) *index_of_max = maxidx_;
456 if ( index_of_extr ) *index_of_extr = 0;
459 const T maxcmp = maxval_ < 0 ? -maxval_ : maxval_;
460 const T mincmp = minval_ < 0 ? -minval_ : minval_;
461 if ( maxcmp < mincmp )
463 if ( index_of_extr ) *index_of_extr = minidx_;
468 if ( index_of_extr ) *index_of_extr = maxidx_;
479 const int lastidx = medvals_.size();
480 const float fidx = ratio * lastidx;
481 const int idx = fidx <= 0 ? 0 : (fidx > lastidx ? lastidx : (int)fidx);
482 return medvals_[upper ? lastidx - idx : idx];
489 if ( clss_.isEmpty() )
492 T maxwt = clsswt_[0];
int ret = clss_[0];
493 for (
int idx=1; idx<clss_.size(); idx++ )
495 if ( clsswt_[idx] > maxwt )
496 { maxwt = clsswt_[idx]; ret = clss_[idx]; }
508 template <
class T>
inline 511 if ( idx_of_med ) *idx_of_med = 0;
513 return sz < 1 ?
mUdf(T) : data[0];
516 T* valarr =
const_cast<T*
>( data );
529 *idx_of_med = idxs[ mididx ];
536 return (data[mididx] + data[mididx-1]) / 2;
541 return data[ mididx ];
545 template <
class T>
inline 549 if ( idx_of_med ) *idx_of_med = 0;
551 return sz < 1 ?
mUdf(T) : data[0];
553 T* valarr =
const_cast<T*
>( data );
556 T* wtcopy =
new T[sz];
557 OD::memCopy( wtcopy, wts, sz*
sizeof(T) );
559 for (
int idx=0; idx<sz; idx++ )
561 const_cast<T&
>(wts[idx]) = wtcopy[ idxs[idx] ];
562 wsum += (float) wts[idx];
566 const float hwsum = wsum * 0.5f;
569 for (
int idx=0; idx<sz; idx++ )
571 wsum += (float) wts[idx];
573 { medidx = idx;
break; }
575 if ( idx_of_med ) *idx_of_med = medidx;
576 return valarr[ medidx ];
583 const int policy = setup_.medianEvenHandling();
584 const int sz = medvals_.size();
585 const T* vals = medvals_.arr();
586 return setup_.weighted_ ?
594 template <
class T>
inline 600 if ( setup_.weighted_ && (
mIsUdf(wt) || this->isZero(wt)) )
603 if ( setup_.needmed_ )
606 if ( setup_.weighted_ )
610 if ( setup_.needextreme_ )
613 minval_ = maxval_ = val;
616 if ( val < minval_ ) { minval_ = val; minidx_ = nradded_ - 1; }
617 if ( val > maxval_ ) { maxval_ = val; maxidx_ = nradded_ - 1; }
621 if ( setup_.needmostfreq_ )
622 setMostFrequent( val, wt );
624 if ( setup_.needsums_ )
627 sum_xx_ += val * val;
628 if ( setup_.weighted_ )
632 sum_wxx_ += wt * val * val;
641 template <
class T>
inline 645 int setidx = clss_.indexOf( ival );
648 { clss_ += ival; clsswt_ += wt; }
650 clsswt_[setidx] += wt;
657 {
pErrMsg(
"Undefined operation for float_complex in template"); }
660 template <
class T>
inline 666 if ( setup_.weighted_ && (
mIsUdf(wt) || this->isZero(wt)) )
669 if ( setup_.needmed_ )
671 int idx = medvals_.size();
674 idx = medvals_.indexOf( val,
false, idx-1 );
675 if ( idx < 0 )
break;
676 if ( setup_.weighted_ && medwts_[idx] == wt )
678 medvals_.removeSingle( idx );
679 medwts_.removeSingle( idx );
685 if ( setup_.needmostfreq_ )
688 int setidx = clss_.indexOf( ival );
690 if ( setup_.weighted_ )
695 clsswt_[setidx] -= wt;
696 if ( clsswt_[setidx] <= 0 )
698 clss_.removeSingle( setidx );
699 clsswt_.removeSingle( setidx );
704 if ( setup_.needsums_ )
707 sum_xx_ -= val * val;
708 if ( setup_.weighted_ )
712 sum_wxx_ -= wt * val * val;
721 template <
class T>
inline 724 for (
int idx=0; idx<sz; idx++ )
725 addValue( data[idx], weights ? weights[idx] : 1 );
730 template <
class T>
inline 733 removeValue( oldval, wt );
734 return addValue( newval, wt );
740 template <
class T>
inline 743 posidx_ = 0; empty_ =
true; full_ =
false;
744 needcalc_ = calc_.setup().needSums() || calc_.setup().needMostFreq();
749 template <
class T>
inline 752 if ( empty_ )
return;
754 const int stopidx = full_ ? sz_ : posidx_;
755 for (
int idx=posidx_; idx<stopidx; idx++ )
756 calc.
addValue( vals_[idx], wts_ ? wts_[idx] : 1 );
757 for (
int idx=0; idx<posidx_; idx++ )
758 calc.
addValue( vals_[idx], wts_ ? wts_[idx] : 1 );
762 template <
class T>
inline 767 case Min:
return min();
768 case Max:
return max();
769 case Extreme:
return extreme();
770 case Median:
return median();
773 return calc_.getValue( t );
777 template <
class T>
inline 783 case Min: (void)min( &ret );
break;
784 case Max: (void)max( &ret );
break;
785 case Extreme: (void)extreme( &ret );
break;
786 case Median: (void)median( &ret );
break;
787 default: ret = 0;
break;
793 template <
class T>
inline 798 return calc.
min( index_of_min );
802 template <
class T>
inline 807 return calc.
max( index_of_max );
811 template <
class T>
inline 816 return calc.
extreme( index_of_extr );
820 template <
class T>
inline 823 CalcSetup rcs( calc_.setup().weighted_ );
826 return calc.median( index_of_med );
839 if ( !wts_ || wt == wts_[posidx_] )
840 calc_.replaceValue( vals_[posidx_], val, wt );
843 calc_.removeValue( vals_[posidx_], wts_[posidx_] );
849 vals_[posidx_] = val;
850 if ( wts_ ) wts_[posidx_] = wt;
853 if ( posidx_ >= sz_ )
854 { full_ =
true; posidx_ = 0; }
860 #undef mRunCalc_ChkEmpty bool needextreme_
Definition: statruncalc.h:72
Definition: stattype.h:27
#define mExpClass(module)
Definition: commondefs.h:160
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:287
T maxval_
Definition: statruncalc.h:149
#define mGetIdxArr(tp, var, sz)
Creates new array of an integer type filled with index.
Definition: commondefs.h:253
BaseCalc(const CalcSetup &s)
Definition: statruncalc.h:139
T median(int *i=0) const
Definition: statruncalc.h:821
bool needMedian() const
Definition: statruncalc.h:56
T count() const
Definition: statruncalc.h:256
#define mBaseCalc_ChkEmpty(typ)
Definition: statruncalc.h:343
bool is8BitesData(const T *vals, od_int64 sz, const unsigned int samplesz=100)
Definition: simpnumer.h:420
int maxidx_
Definition: statruncalc.h:147
Setup for the Stats::RunCalc and Stats::ParallelCalc objects.
Definition: statruncalc.h:38
const CalcSetup & setup() const
Definition: statruncalc.h:107
Definition: stattype.h:27
int getIndex(Type) const
only for Median, Min and Max
Definition: statruncalc.h:326
bool needvariance_
Definition: statruncalc.h:76
T minval_
Definition: statruncalc.h:148
Definition: stattype.h:25
T sum() const
Definition: statruncalc.h:369
#define mIsZero(x, eps)
Definition: commondefs.h:53
double normvariance() const
Definition: statruncalc.h:420
RunCalc< T > & replaceValue(T olddata, T newdata, T wt=1)
Definition: statruncalc.h:731
void clear()
Definition: statruncalc.h:292
T * vals_
Definition: statruncalc.h:280
double getValue(Type) const
Definition: statruncalc.h:763
RunCalc< T > & removeValue(T data, T weight=1)
Definition: statruncalc.h:661
RunCalc< T > & addValues(int sz, const T *data, const T *weights=0)
Definition: statruncalc.h:722
int minidx_
Definition: statruncalc.h:146
bool isWeighted() const
Definition: statruncalc.h:53
RunCalc(const CalcSetup &s)
Definition: statruncalc.h:194
void clear()
Definition: statruncalc.h:741
int posidx_
Definition: statruncalc.h:281
void set(T &_to, const F &fr)
template based type conversion
Definition: convert.h:29
double getValue(Type) const
Definition: statruncalc.h:302
T computeWeightedMedian(const T *data, const T *wts, int sz, int *idx_of_med)
Definition: statruncalc.h:546
TypeSet< int > clss_
Definition: statruncalc.h:156
T max(int *index_of_max=0) const
Definition: statruncalc.h:445
RunCalc< T > & addValue(T data, T weight=1)
Definition: statruncalc.h:595
Definition: stattype.h:26
Stats computation running in parallel.
Definition: statparallelcalc.h:33
T median(int *index_of_median=0) const
Definition: statruncalc.h:581
bool needVariance() const
Definition: statruncalc.h:58
int nrused_
Definition: statruncalc.h:145
float variance(const X &x, int sz)
Definition: simpnumer.h:288
virtual double variance() const
Definition: statruncalc.h:398
Type
Definition: stattype.h:21
bool needExtreme() const
Definition: statruncalc.h:54
void quickSort(T *arr, I sz)
Definition: sorting.h:259
bool needSums() const
Definition: statruncalc.h:55
int getIndex(Type) const
Only use for Min, Max or Median.
Definition: statruncalc.h:778
bool duplicate_sort(T *arr, I sz, int maxnrvals)
Definition: sorting.h:72
int count() const
Definition: statruncalc.h:119
bool needcalc_
Definition: statruncalc.h:284
TypeSet< T > clsswt_
Definition: statruncalc.h:157
void setMostFrequent(T val, T wt)
Definition: statruncalc.h:642
bool needmed_
Definition: statruncalc.h:74
bool isEmpty() const
Definition: statruncalc.h:117
Set of (small) copyable elements.
Definition: commontypes.h:30
Definition: stattype.h:26
bool needMostFreq() const
Definition: statruncalc.h:57
T sum_xx_
Definition: statruncalc.h:151
int nradded_
Definition: statruncalc.h:144
T computeMedian(const T *data, int sz, int pol, int *idx_of_med)
Definition: statruncalc.h:509
T * wts_
Definition: statruncalc.h:279
int size(bool used=true) const
Definition: statruncalc.h:114
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:272
T mostFreq() const
Definition: statruncalc.h:487
WindowedCalc(const CalcSetup &rcs, int sz)
Definition: statruncalc.h:240
T min(int *i=0) const
Definition: statruncalc.h:794
Definition: stattype.h:24
Definition: stattype.h:28
std::complex< float > float_complex
Definition: odcomplex.h:18
TypeSet< T > medvals_
Definition: statruncalc.h:136
RunCalc manager which buffers a part of the data.
Definition: statruncalc.h:237
T extreme(int *i=0) const
Definition: statruncalc.h:812
Definition: stattype.h:24
bool empty_
Definition: statruncalc.h:282
T extreme(int *index_of_extr=0) const
Definition: statruncalc.h:454
#define mDefEps
Definition: commondefs.h:58
Definition: stattype.h:25
~WindowedCalc()
Definition: statruncalc.h:245
double rms() const
Definition: statruncalc.h:385
bool weighted_
Definition: statruncalc.h:71
double stdDev() const
Definition: statruncalc.h:347
bool hasUndefs() const
Definition: statruncalc.h:113
CalcSetup(bool weighted=false)
Definition: statruncalc.h:41
Calculates mean, min, max etc., as running values.
Definition: statruncalc.h:191
void fillCalc(RunCalc< T > &) const
Definition: statruncalc.h:750
T clipVal(float ratio, bool upper) const
require median; 0 <= ratio <= 1
Definition: statruncalc.h:475
T sum_wx_
Definition: statruncalc.h:153
Definition: stattype.h:25
Base class to calculate mean, min, max, etc.. can be used either as running values (Stats::RunCalc) o...
Definition: statruncalc.h:100
virtual ~BaseCalc()
Definition: statruncalc.h:104
bool full_
Definition: statruncalc.h:283
Statistics.
Definition: array2dinterpol.h:28
T min(int *index_of_min=0) const
Definition: statruncalc.h:436
#define mRunCalc_DefEqFn(ret, fn)
Definition: statruncalc.h:258
Definition: stattype.h:23
const int sz_
Definition: statruncalc.h:278
T sum_x_
Definition: statruncalc.h:150
T sum_wxx_
Definition: statruncalc.h:154
T sum_w_
Definition: statruncalc.h:152
RunCalc< T > calc_
Definition: statruncalc.h:277
T sqSum() const
Definition: statruncalc.h:377
Definition: stattype.h:24
bool needsums_
Definition: statruncalc.h:73
CalcSetup setup_
Definition: statruncalc.h:142
double average() const
Definition: statruncalc.h:357
#define mClass(module)
Definition: commondefs.h:164
Definition: stattype.h:26
#define pErrMsg(msg)
Definition: errmsg.h:60
bool isZero(const T &t) const
Definition: statruncalc.h:175
T max(int *i=0) const
Definition: statruncalc.h:803
bool isWeighted() const
Definition: statruncalc.h:108
bool needmostfreq_
Definition: statruncalc.h:75
TypeSet< T > medwts_
Definition: statruncalc.h:158
WindowedCalc & addValue(T data, T weight=1)
Definition: statruncalc.h:831