36#include <drain/TypeUtils.h>
44#include "ValueScaling.h"
60class Histogram :
protected std::vector<unsigned long> {
63 typedef unsigned long count_t;
64 typedef std::vector<count_t> vect_t;
78 std::size_t recommendSizeByType(
const std::type_info & type, std::size_t defaultValue = 256);
81 int getSize()
const {
return size(); };
84 int autoSize(
const std::type_info & type){
86 resize(recommendSizeByType(type, 256));
94 std::fill(begin(), end(), 0);
105 void compute(
const T & src,
const std::type_info & type =
typeid(
double),
const UniTuple<double,2> & scaling = {1.0, 0.0});
115 sampleCountMedian =
static_cast<size_t>(weight *
static_cast<double>(n));
118 size_t getSampleCount()
const {
124 void setRange(
double dataMin,
double dataMax);
134 setRange(s.
fwd(0.0), s.
fwd(1 << (8*drain::Type::call<drain::sizeGetter>(type))));
138 void setScale(
const ValueScaling & scaling){
140 mout.discouraged(
"use setRange instead (scaling=", scaling,
")");
141 mout.advice(
"range perhaps: [", this->scaling.getPhysicalRange());
142 this->scaling.assignSequence(scaling);
163 int getOutMin()
const {
return scaling.fwd(0); };
178 sampleCountMedian =
static_cast<size_t>(weight *
static_cast<double>(sampleCount));
198 ++(*this)[scaling.inv(i)];
204 void increment(T i,
int count){
212 (*this)[scaling.inv(i)] += count;
214 sampleCount += count;
219 void incrementRaw(T i){
231 void incrementRaw(T i,
int count){
232 if (i > this->size()){
233 std::cerr <<
"failRaw: " << i <<
'\t';
237 sampleCount += count;
243 --(*this)[scaling.inv(i)];
249 void decrement(T i,
int count){
250 (*this)[scaling.inv(i)] -= count;
251 sampleCount -= count;
256 void decrementRaw(T i){
263 void decrementRaw(T i,
int count){
265 sampleCount -= count;
271 T scaleOut(size_type i)
const {
273 return static_cast<T
>(scaling.fwd(i));
286 for (size_type i = size()-1; i > 0; --i)
288 return scaleOut<T>(i);
297 for (size_type i = 0; i < size(); ++i)
299 return scaleOut<T>(i);
313 for (size_type i = 0; i < size(); i++){
314 sum += ((*this)[i] * scaleOut<T>(i));
328 return getSum<T>()/sampleCount;
336 T getMedian()
const {
341 for (size_type i = 0; i < size(); ++i){
343 if (sum >= sampleCountMedian){
345 return scaleOut<T>(i);
357 T getWeightedMedian(
float p)
const {
359 if ((p < 0.0) || (p>1.0)){
360 throw std::runtime_error(
"Histogram<T>::getMedian: median point <0 or >1.0 .");
362 const size_t limit = sampleCountMedian;
364 for (size_type i = 0; i < size(); i++){
367 return static_cast<T
>(i);
370 return static_cast<T
>(size());
387 for (size_type i = 0; i < size(); i++){
393 sumT =
static_cast<T
>(sum)/sampleCount;
394 return static_cast<T
>(sum2)/sampleCount - sumT*sumT;
400 T getStdDeviation()
const {
401 return static_cast<T
>(sqrt(getVariance<double>()));
410 const vect_t getVector()
const {
414 std::ostream & toStream(std::ostream & ostr)
const;
416 void dump(std::ostream & ostr = std::cout);
418 std::string delimiter;
422 return (this->*statisticPtr)();
432 return (this->*getStatisticPtr(c))();
437 void setValueFunc(
char c){
438 statisticPtr = getStatisticPtr(c);
446 typedef double (Histogram::*stat_ptr_t)() const;
448 double (Histogram::*statisticPtr)() const;
450 stat_ptr_t getStatisticPtr(
char c);
463 size_type sampleCount = 0;
466 size_type sampleCountMedian = 0;
473 bool isSmallInt(
const std::type_info & type){
474 return (type ==
typeid(
unsigned char)) || (type ==
typeid(
unsigned short int));
482 short getBitShift(
unsigned int value){
484 while ((value = (value>>1)) > 0){
499 const int size = autoSize(type);
502 mout.
error(size,
" bins, something went wrong");
507 mout.
error(size,
" bins exceeds Histogram size limit: ", 0x10000);
516 const bool SAME_SCALING = (inputScaling == this->scaling);
518 mout.
attention(
"scalings: ", this->scaling,
", ", inputScaling,
" same? ", SAME_SCALING);
519 if (inputScaling == this->scaling){
523 mout.info(
"Physical range of the histogram: ", this->scaling.getPhysicalRange());
525 if (isSmallInt(type)){
526 const unsigned short histBits = getBitShift(size);
527 if (size == (1<<histBits)){
528 const signed short dataBits = (8*drain::Type::call<drain::sizeGetter>(type));
529 const short int bitShift = dataBits - histBits;
531 mout.
note(
"Small int (", dataBits,
"b) data, histogram[", size,
"] ", histBits,
" b, computing with bit shift ", bitShift);
533 mout.info(
"Physical range of the histogram: ", this->scaling.getPhysicalRange());
535 for (
typename T::const_iterator it = dst.begin(); it != dst.end(); ++it){
536 this->incrementRaw(
static_cast<unsigned short int>(*it) >> bitShift);
540 mout.advice(
"Consider histogram size of 2^N, e.g.", (1<<histBits));
546 mout.
warn(
"Handling as int ");
547 for (
typename T::const_iterator it = dst.begin(); it != dst.end(); ++it){
548 this->incrementRaw(
static_cast<int>(*it));
556 mout.
error(
"Skipping...");
562std::ostream & operator<<(std::ostream &ostr,
const Histogram &h){
563 return h.toStream(ostr);
Class for computing a histogram and some statistics: average, min, max, mean, std....
Definition Histogram.h:60
void clearBins()
Does not change the size of the histogram.
Definition Histogram.h:93
double getValue(char c)
Return requested statistic.
Definition Histogram.h:431
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:176
int getUpperBoundIn() const
Returns the upperLimit (exclusive)
Definition Histogram.h:159
int getInMin() const
Set range of original (physical) values to be mapped on the limited number of bins....
Definition Histogram.h:155
T getSum() const
Sum of the samples.
Definition Histogram.h:310
T getMean() const
Unscaled mean.
Definition Histogram.h:325
void setSampleCount(long int n)
Does not change the size of the histogram.
Definition Histogram.h:112
int getUpperBoundOut() const
Returns the upperLimit (exclusive)
Definition Histogram.h:167
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:495
T getVariance() const
Computes variance of the values inside the window.
Definition Histogram.h:378
T getMax() const
Statistics.
Definition Histogram.h:285
void setRange(const Range< double > &range)
Set range of original (physical) values to be mapped on the limited number of bins....
Definition Histogram.h:128
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:313
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:431
Logger & note(const TT &... args)
For top-level information.
Definition Log.h:490
Logger & attention(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition Log.h:477
Logger & error(const TT &... args)
Echoes.
Definition Log.h:417
Utilities related to std::type_info.
Definition Type.h:48
Tuple of N elements of type T.
Definition UniTuple.h:65
Linear scaling and physical range for image intensities.
Definition ValueScaling.h:63
double fwd(double x) const
Forward scaling: given encoded value x, returns corresponding value (possibly physically meaningful).
Definition ValueScaling.h:294
Definition DataSelector.cpp:1277
Default implementation.
Definition TypeName.h:54