36#include <drain/TypeUtils.h> 
   45#include "ValueScaling.h" 
   61class Histogram : 
protected std::vector<unsigned long> {
 
   64    typedef unsigned long count_t;
 
   65    typedef std::vector<count_t> vect_t;
 
   79    std::size_t recommendSizeByType(
const std::type_info & type, std::size_t defaultValue = 256);
 
   82    int getSize()
 const { 
return size(); };
 
   85    int autoSize(
const std::type_info & type){
 
   87            resize(recommendSizeByType(type, 256));
 
   95        std::fill(begin(), end(), 0);
 
 
  106    void compute(
const T & src, 
const std::type_info & type = 
typeid(
double), 
const UniTuple<double,2>  & scaling = {1.0, 0.0});
 
  116        sampleCountMedian = 
static_cast<size_t>(weight * 
static_cast<double>(n));
 
 
  119    size_t getSampleCount()
 const {
 
  125    void setRange(
double dataMin, 
double dataMax);
 
  135        setRange(s.
fwd(0.0), s.
fwd(1 << (8*drain::Type::call<drain::sizeGetter>(type))));
 
  139    void setScale(
const ValueScaling & scaling){
 
  141        mout.discouraged(
"use setRange instead (scaling=", scaling, 
")");
 
  142        mout.advice(
"range perhaps: [", this->scaling.getPhysicalRange());
 
  143        this->scaling.assignSequence(scaling);
 
  164    int getOutMin()
 const { 
return scaling.fwd(0); };
 
  179        sampleCountMedian = 
static_cast<size_t>(weight * 
static_cast<double>(sampleCount));
 
 
  199        ++(*this)[scaling.inv(i)];
 
  205    void increment(T i, 
int count){
 
  213        (*this)[scaling.inv(i)] += count;
 
  215        sampleCount += count;
 
  220    void incrementRaw(T i){
 
  232    void incrementRaw(T i, 
int count){
 
  233        if (i > this->size()){
 
  234            std::cerr << 
"failRaw: " << i << 
'\t';
 
  238        sampleCount += count;
 
  244        --(*this)[scaling.inv(i)]; 
 
  250    void decrement(T i, 
int count){
 
  251        (*this)[scaling.inv(i)] -= count; 
 
  252        sampleCount -= count;
 
  257    void decrementRaw(T i){
 
  264    void decrementRaw(T i, 
int count){
 
  266        sampleCount -= count;
 
  272    T scaleOut(size_type i)
 const {
 
  274        return static_cast<T
>(scaling.fwd(i));
 
  287        for (size_type i = size()-1; i > 0; --i)
 
  289                return scaleOut<T>(i);
 
 
  298        for (size_type i = 0; i < size(); ++i)
 
  300                return scaleOut<T>(i);
 
  314        for (size_type i = 0; i < size(); i++){
 
  315            sum += ((*this)[i] * scaleOut<T>(i)); 
 
 
  329          return getSum<T>()/sampleCount;
 
 
  337    T getMedian()
 const {
 
  342        for (size_type i = 0; i < size(); ++i){
 
  344            if (sum >= sampleCountMedian){ 
 
  346                return scaleOut<T>(i);
 
  358    T getWeightedMedian(
float p)
 const {
 
  360        if ((p < 0.0) || (p>1.0)){
 
  361            throw std::runtime_error(
"Histogram<T>::getMedian: median point <0 or >1.0 .");
 
  363        const size_t limit =  sampleCountMedian; 
 
  365        for (size_type i = 0; i < size(); i++){
 
  368                return static_cast<T
>(i);
 
  371        return static_cast<T
>(size());
 
  388        for (size_type i = 0; i < size(); i++){
 
  394        sumT = 
static_cast<T
>(sum)/sampleCount;
 
  395        return  static_cast<T
>(sum2)/sampleCount - sumT*sumT;
 
 
  401    T getStdDeviation()
 const {
 
  402        return  static_cast<T
>(sqrt(getVariance<double>()));
 
  411    const vect_t getVector()
 const {
 
  415    std::ostream & toStream(std::ostream & ostr) 
const;
 
  417    void dump(std::ostream & ostr = std::cout);
 
  419    std::string delimiter;
 
  423        return (this->*statisticPtr)();
 
  433        return (this->*getStatisticPtr(c))();
 
 
  438    void setValueFunc(
char c){
 
  439        statisticPtr = getStatisticPtr(c);
 
  447    typedef double (Histogram::*stat_ptr_t)() const;
 
  449    double (Histogram::*statisticPtr)() const;
 
  451    stat_ptr_t getStatisticPtr(
char c);
 
  464    size_type sampleCount = 0;
 
  467    size_type sampleCountMedian = 0;
 
  474    bool isSmallInt(
const std::type_info & type){
 
  475        return (type == 
typeid(
unsigned char)) || (type == 
typeid(
unsigned short int));
 
  483    short getBitShift(
unsigned int value){
 
  485        while ((value = (value>>1)) > 0){
 
 
  500    const int size = autoSize(type);
 
  503        mout.
error(size, 
" bins, something went wrong");
 
  508        mout.
error(size, 
" bins exceeds Histogram size limit: ", 0x10000);
 
  517    const bool SAME_SCALING = (inputScaling == this->scaling);
 
  519    mout.
attention(
"scalings: ", this->scaling, 
", ", inputScaling, 
" same? ", SAME_SCALING);
 
  520    if (inputScaling == this->scaling){
 
  524    if (isSmallInt(type)){
 
  525        const unsigned short histBits = getBitShift(size);
 
  526        if (size == (1<<histBits)){ 
 
  527            const signed short dataBits = (8*drain::Type::call<drain::sizeGetter>(type));
 
  528            const short int bitShift = dataBits - histBits;
 
  530            mout.
note(
"Small int (", dataBits, 
"b) data, histogram[", size,
"] ", histBits, 
" b, computing with bit shift ", bitShift);
 
  532            mout.info(
"Physical range of the histogram: ", this->scaling.getPhysicalRange());
 
  534            for (
typename T::const_iterator it = dst.begin(); it != dst.end(); ++it){
 
  535                this->incrementRaw(
static_cast<unsigned short int>(*it) >> bitShift);
 
  539            mout.advice(
"Consider histogram size of 2^N, e.g.", (1<<histBits));
 
  547    mout.
error(
"Skipping...");
 
 
  553std::ostream & operator<<(std::ostream &ostr,
const Histogram &h){
 
  554    return h.toStream(ostr);
 
Class for computing a histogram and some statistics: average, min, max, mean, std....
Definition Histogram.h:61
void clearBins()
Does not change the size of the histogram.
Definition Histogram.h:94
double getValue(char c)
Return requested statistic.
Definition Histogram.h:432
void setRange(double dataMin, double dataMax)
Set range of original (physical) values to be mapped on the limited number of bins....
Definition Histogram.cpp:76
void setMedianPosition(double pos)
Set location of the median, if not in the middle (50%).
Definition Histogram.h:177
int getUpperBoundIn() const
Returns the upperLimit (exclusive)
Definition Histogram.h:160
int getInMin() const
Set range of original (physical) values to be mapped on the limited number of bins....
Definition Histogram.h:156
T getSum() const
Sum of the samples.
Definition Histogram.h:311
T getMean() const
Unscaled mean.
Definition Histogram.h:326
void setSampleCount(long int n)
Does not change the size of the histogram.
Definition Histogram.h:113
int getUpperBoundOut() const
Returns the upperLimit (exclusive)
Definition Histogram.h:168
void compute(const T &src, const std::type_info &type=typeid(double), const UniTuple< double, 2 > &scaling={1.0, 0.0})
Collect distribution.
Definition Histogram.h:496
T getVariance() const
Computes variance of the values inside the window.
Definition Histogram.h:379
T getMax() const
Statistics.
Definition Histogram.h:286
void setRange(const Range< double > &range)
Set range of original (physical) values to be mapped on the limited number of bins....
Definition Histogram.h:129
void setSize(size_t s)
Sets the number of bins; the resolution of the histogram.
Definition Histogram.cpp:62
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & note(const TT &... args)
For top-level information.
Definition Log.h:489
Logger & attention(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition Log.h:476
Logger & error(const TT &... args)
Echoes.
Definition Log.h:416
Utilities related to std::type_info.
Definition Type.h:50
Tuple of N elements of type T.
Definition UniTuple.h:65
Linear scaling and physical range for image intensities.
Definition ValueScaling.h:64
double fwd(double x) const
Forward scaling: given encoded value x, returns corresponding value (possibly physically meaningful).
Definition ValueScaling.h:295
Definition DataSelector.cpp:1277