 |
OpendTect
6.6
|
Go to the documentation of this file.
21 #define mUndefReplacement 0
50 , needvariance_(false)
51 , needmostfreq_(false) {}
123 inline bool hasUndefs()
const {
return nrused_ != nradded_; }
124 inline size_type
size(
bool used=
true )
const
125 {
return used ? (size_type)nrused_ : nradded_; }
127 inline bool isEmpty()
const {
return size() == 0; }
129 inline size_type
count()
const {
return nrused_; }
133 inline T
min(idx_type* index_of_min=0)
const;
134 inline T
max(idx_type* index_of_max=0)
const;
135 inline T
extreme(idx_type* index_of_extr=0)
const;
136 inline T
median(idx_type* index_of_median=0)
const;
138 inline double rms()
const;
143 inline T
clipVal(
float ratio,
bool upper)
const;
150 : setup_(s) {
clear(); }
165 bool issorted_ =
false;
174 virtual const T*
sort(idx_type* index_of_median =
nullptr);
177 idx_type* index_of_median =
nullptr)
const;
191 template <
class T>
inline
224 inline RunCalc<T>& operator +=( T t ) {
return addValue(t); }
268 , wts_(calc_.isWeighted() ? new T [sz] : 0)
269 , vals_(new T [sz]) {
clear(); }
271 {
delete [] vals_;
delete [] wts_; }
277 inline idx_type getIndex(
Type)
const;
279 inline double getValue(
Type)
const;
281 inline size_type
count()
const {
return full_ ? sz_ : posidx_; }
283 # define mRunCalc_DefEqFn(ret,fn) \
284 inline ret fn() const { return calc_.fn(); }
293 # undef mRunCalc_DefEqFn
295 inline T min(idx_type* i=0)
const;
296 inline T max(idx_type* i=0)
const;
297 inline T extreme(idx_type* i=0)
const;
298 inline T median(idx_type* i=0)
const;
316 template <
class T>
inline
319 sum_x_ = sum_w_ = sum_xx_ = sum_wx_ = sum_wxx_ = 0;
320 nradded_ = nrused_ = minidx_ = maxidx_ = 0;
321 minval_ = maxval_ =
mUdf(T);
322 clss_.setEmpty(); clsswt_.setEmpty();
323 medvals_.setEmpty(); medwts_.setEmpty(); medidxs_.setEmpty();
328 template <
class T>
inline
333 case Count:
return count();
334 case Average:
return average();
335 case Median:
return median();
336 case RMS:
return rms();
337 case StdDev:
return stdDev();
340 case Min:
return min();
341 case Max:
return max();
342 case Extreme:
return extreme();
343 case Sum:
return sum();
344 case SqSum:
return sqSum();
352 template <
class T>
inline
358 case Min: (void)min( &ret );
break;
359 case Max: (void)max( &ret );
break;
360 case Extreme: (void)extreme( &ret );
break;
361 case Median: (void)median( &ret );
break;
362 default: ret = 0;
break;
369 #undef mBaseCalc_ChkEmpty
370 #define mBaseCalc_ChkEmpty(typ) \
371 if ( nrused_ < 1 ) return mUdf(typ);
388 if ( !setup_.weighted_ )
389 return ((
double)sum_x_) / nrused_;
391 return this->isZero(sum_w_) ?
mUdf(
double) : ((double)sum_wx_) / sum_w_;
416 if ( !setup_.weighted_ )
417 return Math::Sqrt( ((
double)sum_xx_) / nrused_ );
419 return this->isZero(sum_w_) ?
mUdf(
double)
427 if ( nrused_ < 2 )
return 0;
429 if ( !setup_.weighted_ )
430 return ( sum_xx_ - (sum_x_ * ((
double)sum_x_) / nrused_) )
433 return (sum_wxx_ - (sum_wx_ * ((
double)sum_wx_) / sum_w_) )
434 / ( (nrused_-1) * ((double)sum_w_) / nrused_ );
441 pErrMsg(
"Undefined operation for float_complex in template");
449 if ( nrused_ < 2 )
return 0;
452 double avg = average();
454 const double divisor = avg*avg + fact*var;
458 return var / divisor;
465 if ( index_of_min ) *index_of_min = minidx_;
474 if ( index_of_max ) *index_of_max = maxidx_;
483 if ( index_of_extr ) *index_of_extr = 0;
486 const T maxcmp = maxval_ < 0 ? -maxval_ : maxval_;
487 const T mincmp = minval_ < 0 ? -minval_ : minval_;
488 if ( maxcmp < mincmp )
490 if ( index_of_extr ) *index_of_extr = minidx_;
495 if ( index_of_extr ) *index_of_extr = maxidx_;
501 template <
class T>
inline
504 T* valarr = medvals_.arr();
505 const bool withidxs = idx_of_med || setup_.weighted_;
507 ( (withidxs && medidxs_.size()==nrused_) || (!withidxs) ) )
512 if ( medidxs_.size() != nrused_ )
514 medidxs_.setSize( nrused_, 0 );
515 for ( idx_type idx=0; idx<nrused_; idx++ )
519 quickSort( valarr, medidxs_.arr(), nrused_ );
521 else if ( nrused_<=255 || !
is8BitesData(valarr,nrused_,100) ||
532 pErrMsg(
"Undefined operation for float_complex in template");
540 const T* valarr = medvals_.arr();
541 const T* wts = medwts_.arr();
542 const size_type sz = nrused_;
543 T* wtcopy =
new T[sz];
544 OD::memCopy( wtcopy, wts, sz*
sizeof(T) );
546 const idx_type* idxs = medidxs_.arr();
547 for ( idx_type idx=0; idx<sz; idx++ )
549 const_cast<T&
>(wts[idx]) = wtcopy[ idxs[idx] ];
550 wsum += (double) wts[idx];
555 const double hwsum = wsum * 0.5;
558 for ( size_type idx=0; idx<sz; idx++ )
560 wsum += (double) wts[idx];
562 { medidx = idx;
break; }
565 if ( idx_of_med ) *idx_of_med = medidx;
574 const T* valarr = medvals_.arr();
575 const size_type sz = nrused_;
576 idx_type mididx = sz/2;
579 const idx_type* idxs = medidxs_.arr();
580 *idx_of_med = idxs[ mididx ];
586 const int policy = setup_.medianEvenHandling();
588 return (valarr[mididx] + valarr[mididx-1]) / 2;
589 else if ( policy == 1 )
593 return valarr[mididx];
601 const size_type sz = nrused_;
604 if ( index_of_median ) *index_of_median = 0;
609 return setup_.weighted_ ? computeWeightedMedian( index_of_median )
610 : computeMedian( index_of_median );
620 const size_type lastidx = nrused_;
621 const float fidx = ratio * lastidx;
622 const idx_type idx = fidx <= 0 ? 0
623 : (fidx > lastidx ? lastidx
625 return vararr[upper ? lastidx - idx : idx];
632 if ( clss_.isEmpty() )
635 T maxwt = clsswt_[0]; idx_type ret = clss_[0];
636 for ( idx_type idx=1; idx<clss_.size(); idx++ )
638 if ( clsswt_[idx] > maxwt )
639 { maxwt = clsswt_[idx]; ret = clss_[idx]; }
658 if ( setup_.weighted_ && (
mIsUdf(wt) || this->isZero(wt)) )
661 if ( setup_.needmed_ || setup_.needsorted_ )
665 if ( setup_.weighted_ )
669 if ( setup_.needextreme_ )
672 minval_ = maxval_ = val;
675 if ( val < minval_ ) { minval_ = val; minidx_ = nradded_ - 1; }
676 if ( val > maxval_ ) { maxval_ = val; maxidx_ = nradded_ - 1; }
680 if ( setup_.needmostfreq_ )
681 setMostFrequent( val, wt );
683 if ( setup_.needsums_ )
686 sum_xx_ += val * val;
687 if ( setup_.weighted_ )
691 sum_wxx_ += wt * val * val;
700 template <
class T>
inline
704 idx_type setidx = clss_.indexOf( ival );
707 { clss_ += ival; clsswt_ += wt; }
709 clsswt_[setidx] += wt;
716 {
pErrMsg(
"Undefined operation for float_complex in template"); }
719 template <
class T>
inline
725 if ( setup_.weighted_ && (
mIsUdf(wt) || this->isZero(wt)) )
728 if ( setup_.needmed_ || setup_.needsorted_ )
730 size_type idx = medvals_.
size();
733 idx = medvals_.indexOf( val,
false, idx-1 );
734 if ( idx < 0 )
break;
735 if ( setup_.weighted_ && medwts_[idx] == wt )
737 medvals_.removeSingle( idx );
738 medwts_.removeSingle( idx );
744 if ( setup_.needmostfreq_ )
747 idx_type setidx = clss_.indexOf( ival );
749 if ( setup_.weighted_ )
754 clsswt_[setidx] -= wt;
755 if ( clsswt_[setidx] <= 0 )
757 clss_.removeSingle( setidx );
758 clsswt_.removeSingle( setidx );
763 if ( setup_.needsums_ )
766 sum_xx_ -= val * val;
767 if ( setup_.weighted_ )
771 sum_wxx_ -= wt * val * val;
780 template <
class T>
inline
784 for ( idx_type idx=0; idx<sz; idx++ )
785 addValue( data[idx], weights ? weights[idx] : 1 );
790 template <
class T>
inline
793 removeValue( oldval, wt );
794 return addValue( newval, wt );
800 template <
class T>
inline
803 posidx_ = 0; empty_ =
true; full_ =
false;
804 needcalc_ = calc_.setup().needSums() || calc_.setup().needMostFreq();
809 template <
class T>
inline
812 if ( empty_ )
return;
814 const idx_type stopidx = full_ ? sz_ : posidx_;
815 for ( idx_type idx=posidx_; idx<stopidx; idx++ )
816 calc.
addValue( vals_[idx], wts_ ? wts_[idx] : 1 );
817 for ( idx_type idx=0; idx<posidx_; idx++ )
818 calc.
addValue( vals_[idx], wts_ ? wts_[idx] : 1 );
822 template <
class T>
inline
827 case Min:
return min();
828 case Max:
return max();
829 case Extreme:
return extreme();
830 case Median:
return median();
833 return calc_.getValue( t );
837 template <
class T>
inline
843 case Min: (void)min( &ret );
break;
844 case Max: (void)max( &ret );
break;
845 case Extreme: (void)extreme( &ret );
break;
846 case Median: (void)median( &ret );
break;
847 default: ret = 0;
break;
853 template <
class T>
inline
858 return calc.
min( index_of_min );
862 template <
class T>
inline
867 return calc.
max( index_of_max );
871 template <
class T>
inline
876 return calc.
extreme( index_of_extr );
880 template <
class T>
inline
883 CalcSetup rcs( calc_.setup().weighted_ );
886 return calc.
median( index_of_med );
899 if ( !wts_ || wt == wts_[posidx_] )
900 calc_.replaceValue( vals_[posidx_], val, wt );
903 calc_.removeValue( vals_[posidx_], wts_[posidx_] );
909 vals_[posidx_] = val;
910 if ( wts_ ) wts_[posidx_] = wt;
913 if ( posidx_ >= sz_ )
914 { full_ =
true; posidx_ = 0; }
920 #undef mRunCalc_ChkEmpty
mUseType(CalcSetup, idx_type)
bool hasUndefs() const
Definition: statruncalc.h:123
@ Sum
Definition: stattype.h:26
idx_type posidx_
Definition: statruncalc.h:306
const T * medValsArr() const
Definition: statruncalc.h:146
bool needvariance_
Definition: statruncalc.h:83
size_type count() const
Definition: statruncalc.h:129
void setNeedSorted(bool yn=true)
Definition: statruncalc.h:54
Threads::Atomic< size_type > nrused_
Definition: statruncalc.h:155
const size_type sz_
Definition: statruncalc.h:303
@ Median
Definition: stattype.h:23
double average() const
Definition: statruncalc.h:384
@ Average
Definition: stattype.h:23
@ Count
Definition: stattype.h:22
bool isWeighted() const
Definition: statruncalc.h:118
T maxval_
Definition: statruncalc.h:159
std::complex< float > float_complex
Definition: odcomplex.h:17
bool needMedian() const
Definition: statruncalc.h:61
size_type nradded_
Definition: statruncalc.h:154
idx_type minidx_
Definition: statruncalc.h:156
@ MostFreq
Definition: stattype.h:27
bool needmed_
Definition: statruncalc.h:80
T max(idx_type *index_of_max=0) const
Definition: statruncalc.h:472
WindowedCalc & addValue(T data, T weight=1)
Definition: statruncalc.h:891
LargeValVec< T > clsswt_
Definition: statruncalc.h:168
LargeValVec< T > medvals_
Definition: statruncalc.h:169
bool needmostfreq_
Definition: statruncalc.h:82
RunCalc< T > & addValues(size_type sz, const T *data, const T *weights=0)
Definition: statruncalc.h:781
T computeWeightedMedian(idx_type *index_of_median=nullptr) const
Definition: statruncalc.h:538
bool isEmpty() const
Definition: statruncalc.h:127
bool needSorted() const
Definition: statruncalc.h:62
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:289
static int medianEvenHandling()
#define od_int64
Definition: plftypes.h:35
T * vals_
Definition: statruncalc.h:305
double rms() const
Definition: statruncalc.h:412
T extreme(idx_type *index_of_extr=0) const
Definition: statruncalc.h:481
void clear()
Definition: statruncalc.h:317
#define mExpClass(module)
Definition: commondefs.h:177
T * wts_
Definition: statruncalc.h:304
LargeValVec< T > medwts_
Definition: statruncalc.h:170
Base class to calculate mean, min, max, etc.. can be used either as running values (Stats::RunCalc) o...
Definition: statruncalc.h:108
RunCalc< T > & replaceValue(T olddata, T newdata, T wt=1)
Definition: statruncalc.h:791
#define mDefEps
Definition: commondefs.h:71
@ StdDev
Definition: stattype.h:24
virtual const T * sort(idx_type *index_of_median=nullptr)
Definition: statruncalc.h:502
bool needVariance() const
Definition: statruncalc.h:64
RunCalc< T > & removeValue(T data, T weight=1)
Definition: statruncalc.h:720
bool needsorted_
Definition: statruncalc.h:81
double normvariance() const
Definition: statruncalc.h:447
T computeMedian(idx_type *index_of_median=nullptr) const
Definition: statruncalc.h:572
T minval_
Definition: statruncalc.h:158
bool is8BitesData(const T *vals, od_int64 sz, const unsigned int samplesz=100)
Definition: simpnumer.h:419
T sum_xx_
Definition: statruncalc.h:161
Type
Definition: stattype.h:21
bool isZero(const T &) const
Definition: statruncalc.h:192
T mostFreq() const
Definition: statruncalc.h:630
@ NormVariance
Definition: stattype.h:24
T clipVal(float ratio, bool upper) const
requires sort; 0 <= ratio <= 1
Definition: statruncalc.h:615
RunCalc< T > & addValue(T data, T weight=1)
Definition: statruncalc.h:653
idx_type size_type
Definition: statruncalc.h:42
RunCalc< T > calc_
Definition: statruncalc.h:302
const CalcSetup & setup() const
Definition: statruncalc.h:117
RunCalc(const CalcSetup &s)
Definition: statruncalc.h:215
Calculates mean, min, max etc., as running values.
Definition: statruncalc.h:209
@ RMS
Definition: stattype.h:23
~WindowedCalc()
Definition: statruncalc.h:270
mUseType(CalcSetup, size_type)
Stats computation running in parallel.
Definition: statparallelcalc.h:35
@ Variance
Definition: stattype.h:24
#define mIsZero(x, eps)
Definition: commondefs.h:66
void quickSort(T *arr, I sz)
Definition: sorting.h:263
#define mClass(module)
Definition: commondefs.h:181
void sort(BufferStringSet &)
double getValue(Type) const
Definition: statruncalc.h:329
od_int64 idx_type
Definition: statruncalc.h:41
RunCalc manager which buffers a part of the data.
Definition: statruncalc.h:259
mUseType(CalcSetup, size_type)
bool duplicate_sort(T *arr, I sz, int maxnrvals)
Definition: sorting.h:71
bool needExtreme() const
Definition: statruncalc.h:59
void setMostFrequent(T val, T wt)
Definition: statruncalc.h:701
bool weighted_
Definition: statruncalc.h:77
bool needsums_
Definition: statruncalc.h:79
LargeValVec< idx_type > clss_
Definition: statruncalc.h:167
#define pErrMsg(msg)
Usual access point for programmer error messages.
Definition: errmsg.h:37
size_type size(bool used=true) const
Definition: statruncalc.h:124
void set(T &_to, const F &fr)
template based type conversion
Definition: convert.h:27
bool empty_
Definition: statruncalc.h:307
bool needcalc_
Definition: statruncalc.h:309
mUseType(CalcSetup, size_type)
T sum_wx_
Definition: statruncalc.h:163
T sum_w_
Definition: statruncalc.h:162
T sum_wxx_
Definition: statruncalc.h:164
@ SqSum
Definition: stattype.h:26
virtual double variance() const
Definition: statruncalc.h:425
bool needMostFreq() const
Definition: statruncalc.h:63
T sum() const
Definition: statruncalc.h:396
@ Min
Definition: stattype.h:25
T min(idx_type *index_of_min=0) const
Definition: statruncalc.h:463
CalcSetup & require(Type)
float variance(const X &x, int sz)
Definition: simpnumer.h:287
mUseType(CalcSetup, idx_type)
bool full_
Definition: statruncalc.h:308
LargeValVec< idx_type > medidxs_
Definition: statruncalc.h:171
Setup for the Stats::RunCalc and Stats::ParallelCalc objects.
Definition: statruncalc.h:38
T sum_x_
Definition: statruncalc.h:160
bool isWeighted() const
Definition: statruncalc.h:58
bool needextreme_
Definition: statruncalc.h:78
CalcSetup setup_
Definition: statruncalc.h:152
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:274
mUseType(CalcSetup, idx_type)
idx_type maxidx_
Definition: statruncalc.h:157
bool needSums() const
Definition: statruncalc.h:60
#define mRunCalc_DefEqFn(ret, fn)
Definition: statruncalc.h:283
Statistics.
Definition: array2dinterpol.h:27
@ Max
Definition: stattype.h:25
double stdDev() const
Definition: statruncalc.h:374
T sqSum() const
Definition: statruncalc.h:404
size_type count() const
Definition: statruncalc.h:281
BaseCalc(const CalcSetup &s)
Definition: statruncalc.h:149
virtual ~BaseCalc()
Definition: statruncalc.h:114
idx_type getIndex(Type) const
only for Median, Min and Max
Definition: statruncalc.h:353
WindowedCalc(const CalcSetup &rcs, size_type sz)
Definition: statruncalc.h:265
CalcSetup(bool weighted=false)
Definition: statruncalc.h:44
#define mBaseCalc_ChkEmpty(typ)
Definition: statruncalc.h:370
T median(idx_type *index_of_median=0) const
Definition: statruncalc.h:598
@ Extreme
Definition: stattype.h:25
Generated at
for the OpendTect
seismic interpretation project.
Copyright (C): dGB Beheer B.V. 1995-2021