Xmipp  v3.23.11-Nereus
Classes
Collaboration diagram for Histograms:

Classes

class  Histogram1D
 
class  CDF
 
class  IrregularHistogram1D
 
class  Histogram2D
 

Functions related to histograms 1D

template<typename T >
void compute_hist (const MultidimArray< T > &array, Histogram1D &hist, int no_steps)
 
void compute_hist (const MultidimArrayGeneric &array, Histogram1D &hist, int no_steps)
 
template<typename T >
void compute_hist (const std::vector< T > &v, Histogram1D &hist, int no_steps=100)
 
template<typename T >
void compute_hist (const MultidimArray< T > &v, Histogram1D &hist, double min, double max, int no_steps)
 
void compute_hist (const MultidimArrayGeneric &v, Histogram1D &hist, double min, double max, int no_steps)
 
template<typename T >
void compute_hist (const MultidimArray< T > &v, Histogram1D &hist, const Matrix1D< int > &corner1, const Matrix1D< int > &corner2, int no_steps=100)
 
double detectability_error (const Histogram1D &h1, const Histogram1D &h2)
 
double KLDistance (const Histogram1D &h1, const Histogram1D &h2)
 
template<typename T >
double effective_range (const T &v, double percentil_out=0.25)
 
template<typename T >
void reject_outliers (T &v, double percentil_out=0.25)
 
template<typename T >
void histogram_equalization (MultidimArray< T > &v, int bins=8)
 

Functions related to histograms 2D

The vectors can be of any numerical type (short int, int, double, ...), but both of the same type. Vectors must be of the same shape, the first element of v1 and the first of v2 define the position were the first point will be inserted in the histogram, then the second of v1 and of v2, ... That is, the elements of v1 and v2 serve as coordinates within the histogram. The number of steps must always be given.

template<typename T >
void compute_hist (const T &v1, const T &v2, Histogram2D &hist, int no_steps1, int no_steps2)
 
template<typename T >
void compute_hist (const MultidimArray< T > &v1, const MultidimArray< T > &v2, Histogram2D &hist, double m1, double M1, double m2, double M2, int no_steps1, int no_steps2)
 

Detailed Description

Function Documentation

◆ compute_hist() [1/8]

template<typename T >
void compute_hist ( const MultidimArray< T > &  array,
Histogram1D hist,
int  no_steps 
)

Compute histogram of a vector within its minimum and maximum value

Given an array as input, this function returns its histogram within the minimum and maximum of the array, in this way all the values in the array are counted. The array can be of any numerical type (short int, int, double, ...) and dimension. The number of steps must always be given.

compute_hist(v, hist, 100);

Definition at line 430 of file histogram.h.

432 {
433  double min=0, max=0;
434  array.computeDoubleMinMax(min, max);
435  compute_hist(array, hist, min, max, no_steps);
436 }
void min(Image< double > &op1, const Image< double > &op2)
void computeDoubleMinMax(double &minval, double &maxval) const
void max(Image< double > &op1, const Image< double > &op2)
void compute_hist(const MultidimArray< T > &array, Histogram1D &hist, int no_steps)
Definition: histogram.h:430

◆ compute_hist() [2/8]

void compute_hist ( const MultidimArrayGeneric array,
Histogram1D hist,
int  no_steps 
)

Compute histogram of a MultidimArrayGeneric within its minimum and maximum value

Definition at line 572 of file histogram.cpp.

574 {
575  double min=0., max=0.;
576  array.computeDoubleMinMax(min, max);
577  compute_hist(array, hist, min, max, no_steps);
578 }
void min(Image< double > &op1, const Image< double > &op2)
void computeDoubleMinMax(double &minval, double &maxval) const
void compute_hist(const MultidimArrayGeneric &array, Histogram1D &hist, int no_steps)
Definition: histogram.cpp:572
void max(Image< double > &op1, const Image< double > &op2)

◆ compute_hist() [3/8]

template<typename T >
void compute_hist ( const std::vector< T > &  v,
Histogram1D hist,
int  no_steps = 100 
)

Compute histogram of a vector

Definition at line 445 of file histogram.h.

448 {
449  hist.clear();
450  int imax=v.size();
451  if (imax==0)
452  return;
453 
454  // Compute minimum and maximum
455  double min, max;
456  min=max=v[0];
457  for (int i=1; i<imax; i++)
458  {
459  double val=v[i];
460  min=XMIPP_MIN(min,val);
461  max=XMIPP_MAX(max,val);
462  }
463  hist.init(min, max, no_steps);
464 
465  for (int i=1; i<imax; i++)
466  {
467  double value=v[i];
468  INSERT_VALUE(hist,value);
469  }
470 }
void clear()
Definition: histogram.cpp:40
void min(Image< double > &op1, const Image< double > &op2)
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
#define INSERT_VALUE(histogram, value)
Definition: histogram.h:205
void init(double min_val, double max_val, int n_steps)
Definition: histogram.cpp:71
#define i
void max(Image< double > &op1, const Image< double > &op2)
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181

◆ compute_hist() [4/8]

template<typename T >
void compute_hist ( const MultidimArray< T > &  v,
Histogram1D hist,
double  min,
double  max,
int  no_steps 
)

Compute histogram of the array within two values

Given a array as input, this function returns its histogram within two values, the array values outside this range are not counted. This can be used to avoid the effect of outliers which causes a "compression" in the histogram. The array can be of any numerical type (short int, int, double, ...). The number of steps must always be given.

compute_hist(v, hist, 0, 1, 100);

Definition at line 486 of file histogram.h.

488 {
489  hist.init(min, max, no_steps);
490  T* ptr=&DIRECT_MULTIDIM_ELEM(v,0);
491  size_t nmax=(MULTIDIM_SIZE(v)/4)*4;
492 
493  double value;
494  for (size_t n=0; n<nmax; n+=4, ptr+=4)
495  {
496  value=*ptr;
497  INSERT_VALUE(hist,value);
498  value=*(ptr+1);
499  INSERT_VALUE(hist,value);
500  value=*(ptr+2);
501  INSERT_VALUE(hist,value);
502  value=*(ptr+3);
503  INSERT_VALUE(hist,value);
504  }
505  for (size_t n=nmax; n<MULTIDIM_SIZE(v); ++n, ptr+=1)
506  {
507  value=*ptr;
508  INSERT_VALUE(hist,value);
509  }
510 }
int * nmax
void min(Image< double > &op1, const Image< double > &op2)
#define INSERT_VALUE(histogram, value)
Definition: histogram.h:205
#define MULTIDIM_SIZE(v)
void init(double min_val, double max_val, int n_steps)
Definition: histogram.cpp:71
void max(Image< double > &op1, const Image< double > &op2)
#define DIRECT_MULTIDIM_ELEM(v, n)
int * n

◆ compute_hist() [5/8]

void compute_hist ( const MultidimArrayGeneric v,
Histogram1D hist,
double  min,
double  max,
int  no_steps 
)

Compute histogram of the MultidimArrayGeneric within two values

Definition at line 580 of file histogram.cpp.

582 {
583  hist.init(min, max, no_steps);
584 #define COMPUTEHIST(type) compute_hist(MULTIDIM_ARRAY_TYPE(v,type),hist,min,max,no_steps);
585 
587 #undef COMPUTEHIST
588 }
void min(Image< double > &op1, const Image< double > &op2)
void init(double min_val, double max_val, int n_steps)
Definition: histogram.cpp:71
void max(Image< double > &op1, const Image< double > &op2)
#define COMPUTEHIST(type)
#define SWITCHDATATYPE(datatype, OP)

◆ compute_hist() [6/8]

template<typename T >
void compute_hist ( const MultidimArray< T > &  v,
Histogram1D hist,
const Matrix1D< int > &  corner1,
const Matrix1D< int > &  corner2,
int  no_steps = 100 
)

Compute histogram within a region (2D or 3D)

The region is defined by its corners

Definition at line 522 of file histogram.h.

527 {
528  double min, max;
529  v.computeDoubleMinMax(min, max, corner1, corner2);
530  hist.init(min, max, no_steps);
531 
532  Matrix1D< int > r(2);
533  FOR_ALL_ELEMENTS_IN_ARRAY2D_BETWEEN(corner1, corner2)
534  {
535  double value=v(r);
536  INSERT_VALUE(hist,value);
537  }
538 }
void min(Image< double > &op1, const Image< double > &op2)
#define INSERT_VALUE(histogram, value)
Definition: histogram.h:205
void init(double min_val, double max_val, int n_steps)
Definition: histogram.cpp:71
void computeDoubleMinMax(double &minval, double &maxval) const
void max(Image< double > &op1, const Image< double > &op2)
#define FOR_ALL_ELEMENTS_IN_ARRAY2D_BETWEEN(corner1, corner2)

◆ compute_hist() [7/8]

template<typename T >
void compute_hist ( const T &  v1,
const T &  v2,
Histogram2D hist,
int  no_steps1,
int  no_steps2 
)

Compute histogram of two arrays within their minimum and maximum values

Given two arrays as input, this function returns their joint histogram within the minimum and maximum of the arrays, in this way all the values in the arrays are counted. Both arrays must have the same shape

Definition at line 966 of file histogram.h.

968 {
969  double min1, max1;
970  v1.computeDoubleMinMax(min1, max1)
971  ;
972 
973  double min2, max2;
974  v2.computeDoubleMinMax(min2, max2);
975 
976  compute_hist(v1, v2, hist, min1, max1, min2, max2, no_steps1, no_steps2);
977 }
double v1
void compute_hist(const MultidimArray< T > &array, Histogram1D &hist, int no_steps)
Definition: histogram.h:430

◆ compute_hist() [8/8]

template<typename T >
void compute_hist ( const MultidimArray< T > &  v1,
const MultidimArray< T > &  v2,
Histogram2D hist,
double  m1,
double  M1,
double  m2,
double  M2,
int  no_steps1,
int  no_steps2 
)

Compute histogram of two arrays within given values

Given two arrays as input, this function returns their joint histogram within the specified values, all the values lying outside are not counted

Definition at line 985 of file histogram.h.

990 {
991  if (!v1.sameShape(v2))
992  REPORT_ERROR(ERR_MULTIDIM_SIZE, "compute_hist: v1 and v2 are of different shape");
993 
994  hist.init(m1, M1, no_steps1, m2, M2, no_steps2);
995 
998  DIRECT_MULTIDIM_ELEM(v2, n));
999 }
void insert_value(double v, double u)
Definition: histogram.cpp:530
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void init(double imin_val, double imax_val, int in_steps, double jmin_val, double jmax_val, int jn_steps)
Definition: histogram.cpp:512
Incorrect MultidimArray size.
Definition: xmipp_error.h:174
#define M2
#define M1
bool sameShape(const MultidimArrayBase &op) const
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
#define DIRECT_MULTIDIM_ELEM(v, n)
int * n

◆ detectability_error()

double detectability_error ( const Histogram1D h1,
const Histogram1D h2 
)

Compute the detectability error between two pdf's

The input histograms are expressed as probability density functions representing two different objects with the same parameter of variation, for instance, the protein and background both defined by a grey-level. The first histogram would be the histogram of the grey-levels associated to voxels which we know to belong to the protein area, while the second histogram is for the grey-level of voxels which we know to belong to the background. Then the intersection area between the both associated pdf's represents the probability of committing an error when classifying a voxel, ie, the probability of saying that a voxel is protein when it really is background and viceversa. This function allows you to compute this probability error when the two histograms are provided. Be careful that this probability usually has got a very low value.

detect_error = detectability_error(hist1, hist2);

Definition at line 324 of file histogram.cpp.

325 {
326  double hmin, hmax;
327  double step;
328  double v;
329  double error = 0;
330  int ih1, ih2; // Indexes within the histograms
331  double p1, p2; // Probability associated
332 
333  // Find global range
334  hmin = XMIPP_MAX(h1.hmin, h2.hmin);
335  hmax = XMIPP_MIN(h1.hmax, h2.hmax);
336  step = XMIPP_MIN(h1.step_size, h2.step_size) / 2;
337 
338  // Go over the range computing the errors
339  v = hmin;
340  int N = 0;
341  while (v <= hmax)
342  {
343  h1.val2index(v, ih1);
344  p1 = A1D_ELEM(h1, ih1) / h1.no_samples;
345  h2.val2index(v, ih2);
346  p2 = A1D_ELEM(h2, ih2) / h2.no_samples;
347  //#define DEBUG
348 #ifdef DEBUG
349 
350  std::cout << "Comparing at " << v << " (" << ih1 << ") p1=" << p1 << " p2= " << p2 << std::endl;
351  std::cout << " hmin " << hmin << " hmax " << hmax << " stepsize " << h1.step_size << std::endl;
352 #endif//;
353 
354  if (p1 != 0 && p2 != 0)
355  {
356  if (p1 > p2)
357  error += p2;
358  else
359  error += p1;
360  }
361  v += step;
362  N++;
363  }
364 
365  // Normalise such that the result is the area of a probability function
366  error *= step / (hmax - hmin);
367  error /= N;
368 #ifdef DEBUG
369 
370  std::cout << "Total error = " << error << std::endl;
371 #endif
372 
373  return error;
374 }
void val2index(double v, int &i) const
Definition: histogram.h:271
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
#define A1D_ELEM(v, i)
double hmin
Definition: histogram.h:125
double hmax
Definition: histogram.h:126
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181
void error(char *s)
Definition: tools.cpp:107
int no_samples
Definition: histogram.h:129
double step_size
Definition: histogram.h:127

◆ effective_range()

template<typename T >
double effective_range ( const T &  v,
double  percentil_out = 0.25 
)

Returns the effective range of a multidimensional array

The effective range is defined as the difference of those two values comprising a given central percentage of the array histogram. This function is used to compute the range removing outliers. The default central percentage is 99.75%, although this value should be increased as the number of values in the array decreases. For the default, for instance, the 0.125% of the smaller values are left out as well as the 0.125% of the higher values. The range is given always as a double number.

double range = v.effective_range();
// range for the 99.75% of the mass
double range = v.effective_range(1);
// range for the 99% of the mass

Definition at line 590 of file histogram.h.

591 {
592  Histogram1D hist;
593  compute_hist(v, hist, 200)
594  ;
595  double min_val = hist.percentil(percentil_out / 2);
596  double max_val = hist.percentil(100 - percentil_out / 2);
597  return max_val - min_val;
598 }
double percentil(double percent_mass)
Definition: histogram.cpp:160
void compute_hist(const MultidimArray< T > &array, Histogram1D &hist, int no_steps)
Definition: histogram.h:430

◆ histogram_equalization()

template<typename T >
void histogram_equalization ( MultidimArray< T > &  v,
int  bins = 8 
)

Histogram equalization and re-quantization

This function equalizes the histogram of the input multidimensional array, and re-quantize the input array to a specified number of bins. The output array is defined between 0 and bins-1.

Definition at line 633 of file histogram.h.

635 {
636  const int hist_steps = 200;
637  Histogram1D hist;
638  compute_hist(v, hist, hist_steps);
639 
640  // Compute the distribution function of the pdf
641  MultidimArray<double> norm_sum(hist_steps);
642  DIRECT_A1D_ELEM(norm_sum,0) = DIRECT_A1D_ELEM(hist,0);
643 
644  for (int i = 1; i < hist_steps; i++)
645  DIRECT_A1D_ELEM(norm_sum,i) = DIRECT_A1D_ELEM(norm_sum,i - 1) + DIRECT_A1D_ELEM(hist,i);
646  norm_sum /= MULTIDIM_SIZE(v);
647 
648  // array to store the boundary pixels of bins
649  MultidimArray< double > div(bins - 1);
650  int index = 0;
651 
652  for (int current_bin = 1; current_bin < bins; current_bin++)
653  {
654  double current_value = (double) current_bin / bins;
655  while (DIRECT_A1D_ELEM(norm_sum,index) < current_value && index < FINISHINGX(norm_sum))
656  index++;
657  hist.index2val((double) index, DIRECT_A1D_ELEM(div,current_bin - 1));
658  }
659 
660  // requantize and equalize histogram
661  T* ptr=NULL;
662  unsigned long int n;
664  {
665  T vi=*ptr;
666  if (vi < DIRECT_A1D_ELEM(div,0))
667  *ptr = 0;
668  else if (vi > DIRECT_A1D_ELEM(div,bins - 2))
669  *ptr = bins - 1;
670  else
671  {
672  index = 0;
673  while (vi > DIRECT_A1D_ELEM(div,index))
674  index++;
675  *ptr = index;
676  }
677  }
678 }
#define FINISHINGX(v)
#define MULTIDIM_SIZE(v)
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY_ptr(v, n, ptr)
#define i
#define DIRECT_A1D_ELEM(v, i)
void index2val(double i, double &v) const
Definition: histogram.h:295
viol index
void compute_hist(const MultidimArray< T > &array, Histogram1D &hist, int no_steps)
Definition: histogram.h:430
int current_value
Definition: svm-toy.cpp:54
int * n

◆ KLDistance()

double KLDistance ( const Histogram1D h1,
const Histogram1D h2 
)

Compute the Kullback-Leibler distance between two pdf's

The input histograms are expressed as probability density functions.

distance = KLDistance(hist1, hist2);

Definition at line 377 of file histogram.cpp.

378 {
379  if (XSIZE(h1) != XSIZE(h2))
380  REPORT_ERROR(ERR_MULTIDIM_SIZE,"KLDistance: Histograms of different sizes");
381 
382  double retval = 0;
384  if (A1D_ELEM(h2,i) >1e-180 && A1D_ELEM(h1,i) >1e-180)
385  retval += A1D_ELEM(h1,i) * log10(A1D_ELEM(h1,i) / A1D_ELEM(h2,i));
386  return retval;
387 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define A1D_ELEM(v, i)
Incorrect MultidimArray size.
Definition: xmipp_error.h:174
#define i
#define XSIZE(v)
void log10(Image< double > &op)
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v)

◆ reject_outliers()

template<typename T >
void reject_outliers ( T &  v,
double  percentil_out = 0.25 
)

Clips the array values within the effective range

Look at the documentation of effective_rage

Definition at line 605 of file histogram.h.

606 {
607  Histogram1D hist;
608  compute_hist(v, hist, 400);
609  double eff0 = hist.percentil(percentil_out / 2);
610  double effF = hist.percentil(100 - percentil_out / 2);
611  int i0, iF;
612  hist.val2index(eff0, i0);
613  hist.val2index(effF, iF);
614  if (iF == i0) {
615  hist.index2val(i0, eff0);
616  hist.index2val(i0 + 1, effF);
617  }
618 
620  if (DIRECT_MULTIDIM_ELEM(v,n) < eff0)
621  DIRECT_MULTIDIM_ELEM(v,n) = eff0;
622  else if (DIRECT_MULTIDIM_ELEM(v,n) > effF)
623  DIRECT_MULTIDIM_ELEM(v,n) = effF;
624 }
void val2index(double v, int &i) const
Definition: histogram.h:271
double percentil(double percent_mass)
Definition: histogram.cpp:160
void index2val(double i, double &v) const
Definition: histogram.h:295
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
#define DIRECT_MULTIDIM_ELEM(v, n)
void compute_hist(const MultidimArray< T > &array, Histogram1D &hist, int no_steps)
Definition: histogram.h:430
int * n