35 #include <drain/Log.h>
36 #include <drain/TypeUtils.h>
45 #include "ValueScaling.h"
61 class 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));
194 ++(*this)[scaling.inv(i)];
200 void increment(T i,
int count){
201 (*this)[scaling.inv(i)] += count;
203 sampleCount += count;
208 void incrementRaw(T i){
215 void incrementRaw(T i,
int count){
217 sampleCount += count;
223 --(*this)[scaling.inv(i)];
229 void decrement(T i,
int count){
230 (*this)[scaling.inv(i)] -= count;
231 sampleCount -= count;
236 void decrementRaw(T i){
243 void decrementRaw(T i,
int count){
245 sampleCount -= count;
251 T scaleOut(size_type i)
const {
253 return static_cast<T
>(scaling.fwd(i));
266 for (size_type i = size()-1; i > 0; --i)
268 return scaleOut<T>(i);
277 for (size_type i = 0; i < size(); ++i)
279 return scaleOut<T>(i);
293 for (size_type i = 0; i < size(); i++){
294 sum += ((*this)[i] * scaleOut<T>(i));
308 return getSum<T>()/sampleCount;
316 T getMedian()
const {
321 for (size_type i = 0; i < size(); ++i){
323 if (sum >= sampleCountMedian){
325 return scaleOut<T>(i);
337 T getWeightedMedian(
float p)
const {
339 if ((p < 0.0) || (p>1.0)){
340 throw std::runtime_error(
"Histogram<T>::getMedian: median point <0 or >1.0 .");
342 const size_t limit = sampleCountMedian;
344 for (size_type i = 0; i < size(); i++){
347 return static_cast<T
>(i);
350 return static_cast<T
>(size());
367 for (size_type i = 0; i < size(); i++){
373 sumT =
static_cast<T
>(sum)/sampleCount;
374 return static_cast<T
>(sum2)/sampleCount - sumT*sumT;
380 T getStdDeviation()
const {
381 return static_cast<T
>(sqrt(getVariance<double>()));
390 const vect_t getVector()
const {
394 std::ostream & toStream(std::ostream & ostr)
const;
396 void dump(std::ostream & ostr = std::cout);
398 std::string delimiter;
402 return (this->*statisticPtr)();
412 return (this->*getStatisticPtr(c))();
417 void setValueFunc(
char c){
418 statisticPtr = getStatisticPtr(c);
426 typedef double (Histogram::*stat_ptr_t)()
const;
428 double (Histogram::*statisticPtr)()
const;
430 stat_ptr_t getStatisticPtr(
char c);
443 size_type sampleCount = 0;
446 size_type sampleCountMedian = 0;
453 bool isSmallInt(
const std::type_info & type){
454 return (type ==
typeid(
unsigned char)) || (type ==
typeid(
unsigned short int));
462 short getBitShift(
unsigned int value){
464 while ((value = (value>>1)) > 0){
479 const int size = autoSize(type);
482 mout.
error(size,
" bins, something went wrong");
487 mout.
error(size,
" bins exceeds Histogram size limit: ", 0x10000);
496 const bool SAME_SCALING = (inputScaling == this->scaling);
498 mout.
attention(
"scalings: ", this->scaling,
", ", inputScaling,
" same? ", SAME_SCALING);
499 if (inputScaling == this->scaling){
503 if (isSmallInt(type)){
504 const unsigned short histBits = getBitShift(size);
505 if (size == (1<<histBits)){
506 const signed short dataBits = (8*drain::Type::call<drain::sizeGetter>(type));
507 const short int bitShift = dataBits - histBits;
509 mout.
note(
"Small int (", dataBits,
"b) data, histogram[", size,
"] ", histBits,
" b, computing with bit shift ", bitShift);
511 mout.info(
"Physical range of the histogram: ", this->scaling.getPhysicalRange());
513 for (
typename T::const_iterator it = dst.begin(); it != dst.end(); ++it){
514 this->incrementRaw(
static_cast<unsigned short int>(*it) >> bitShift);
518 mout.advice(
"Consider histogram size of 2^N, e.g.", (1<<histBits));
526 mout.
error(
"Skipping...");
532 std::ostream & operator<<(std::ostream &ostr,
const Histogram &h){
533 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:411
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:290
T getMean() const
Unscaled mean.
Definition: Histogram.h:305
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:475
T getVariance() const
Computes variance of the values inside the window.
Definition: Histogram.h:358
T getMax() const
Statistics.
Definition: Histogram.h:265
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:308
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:412
Logger & attention(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition: Log.h:472
Logger & note(const TT &... args)
For top-level information.
Definition: Log.h:485
Utilities related to std::type_info.
Definition: Type.h:51
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