IMP  2.1.0
The Integrative Modeling Platform
HistogramD.h
Go to the documentation of this file.
1 /**
2  * \file IMP/statistics/HistogramD.h \brief Holds a histogram
3  *
4  * Copyright 2007-2013 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPSTATISTICS_HISTOGRAM_D_H
9 #define IMPSTATISTICS_HISTOGRAM_D_H
10 #include <IMP/statistics/statistics_config.h>
11 #include "internal/histogram.h"
13 #include <IMP/algebra/VectorD.h>
15 #include <IMP/algebra/GridD.h>
17 #include <IMP/base_types.h>
18 #include <limits>
19 #include <vector>
20 IMPSTATISTICS_BEGIN_NAMESPACE
21 /** Dynamically build a histogram embedded in D-dimensional space. */
22 template <int D, class Grid = algebra::GridD<
23  D, typename algebra::DenseGridStorageD<D, double>, double,
24  typename algebra::DefaultEmbeddingD<D> > >
26  public:
27 
28  typedef Grid CountGrid;
29  HistogramD() : count_(std::numeric_limits<double>::max()) {}
30  HistogramD(double voxel_size, const algebra::BoundingBoxD<D> &bb)
31  : grid_(voxel_size, bb, 0), count_(0) {}
32  /** Increase the count for the bin that holds a
33  value that is in range for this histogram.*/
34  void add(const algebra::VectorInputD<D> &x, double weight = 1) {
35  IMP_USAGE_CHECK(count_ != std::numeric_limits<double>::max(),
36  "Using uninitialized histogram");
37  typename CountGrid::ExtendedIndex ei = grid_.get_nearest_extended_index(x);
38  if (grid_.get_has_index(ei)) {
39  grid_[grid_.get_index(ei)] += weight;
40  }
41  count_ += weight;
42  }
43  //! Get the sum of all counts in the histogram.
44  double get_total_count() const { return count_; }
45  HistogramD<D> get_frequencies() const {
46  CountGrid grid(grid_.get_unit_cell()[0], algebra::get_bounding_box(grid_),
47  0);
48  grid_.apply(internal::Frequency<D, Grid>(grid, 1.0 / count_));
49  return HistogramD<D>(grid);
50  }
51  /** Get a histogram that has the pdf value as the value for the bin.*/
53  CountGrid grid(grid_.get_unit_cell()[0], algebra::get_bounding_box(grid_),
54  0);
55  double volume =
56  algebra::get_volume(grid_.get_bounding_box(*grid_.all_indexes_begin()));
57  grid_.apply(internal::Frequency<D, Grid>(grid, 1.0 / (count_ * volume)));
58  return HistogramD<D>(grid);
59  }
60  const CountGrid &get_counts() const { return grid_; }
62  algebra::VectorD<D> zeros(grid_.get_bounding_box().get_corner(0));
63  std::fill(zeros.coordinates_begin(), zeros.coordinates_end(), 0.0);
64  return grid_.apply(internal::Mean<D>(zeros)).mn / count_;
65  }
66  unsigned int get_dimension() const { return grid_.get_dimension(); }
67  algebra::VectorD<D> get_standard_deviation(
68  const algebra::VectorInputD<D> &mean) const {
69  algebra::VectorD<D> zeros(grid_.get_bounding_box().get_corner(0));
70  std::fill(zeros.coordinates_begin(), zeros.coordinates_end(), 0.0);
71  algebra::VectorD<D> s2 =
72  grid_.apply(internal::Sigma2<D>(mean, zeros)).sigma2;
73  s2 /= count_;
74  for (unsigned int i = 0; i < get_dimension(); ++i) {
75  s2[i] = std::sqrt(s2[i]);
76  }
77  return s2;
78  }
79  algebra::BoundingBoxD<D> get_bounding_box() const {
80  return IMP::algebra::get_bounding_box(grid_);
81  }
82  FloatPair get_minimum_and_maximum() const {
83  return grid_.apply(internal::MinMax<D>()).minmax;
84  }
85 
86  IMP_SHOWABLE_INLINE(HistogramD, out << "count: " << count_);
87 
88  private:
89  HistogramD(const CountGrid &g) : grid_(g), count_(1) {}
90  CountGrid grid_;
91  double count_;
92 };
93 
94 #ifndef IMP_DOXYGEN
95 typedef HistogramD<1> Histogram1D;
96 IMP_VALUES(Histogram1D, Histogram1Ds);
97 typedef HistogramD<2> Histogram2D;
98 IMP_VALUES(Histogram2D, Histogram2Ds);
99 typedef HistogramD<3> Histogram3D;
100 IMP_VALUES(Histogram3D, Histogram3Ds);
101 typedef HistogramD<4> Histogram4D;
102 IMP_VALUES(Histogram4D, Histogram4Ds);
103 typedef HistogramD<5> Histogram5D;
104 IMP_VALUES(Histogram5D, Histogram5Ds);
105 typedef HistogramD<6> Histogram6D;
106 IMP_VALUES(Histogram6D, Histogram6Ds);
107 typedef HistogramD<-1> HistogramKD;
108 IMP_VALUES(HistogramKD, HistogramKDs);
109 #endif
110 
111 /** Return the midpoint of the bin that best approximates the
112  specified quantile (passed as a fraction). That is,
113 passing .5 returns the median. And passing .9*/
114 IMPSTATISTICSEXPORT double get_quantile(const Histogram1D &h, double fraction);
115 
116 IMPSTATISTICS_END_NAMESPACE
117 #endif /* IMPSTATISTICS_HISTOGRAM_D_H */
Basic types used by IMP.
double get_mean(const cv::Mat &mat, const cvIntMat &mask)
Import IMP/kernel/base_types.h in the namespace.
A class to represent a voxel grid.
std::pair< double, double > FloatPair
A generic pair of floats.
Definition: base/types.h:27
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
double get_quantile(const Histogram1D &h, double fraction)
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
double get_volume(const BoundingBoxD< D > &bb)
Definition: BoundingBoxD.h:147
A Cartesian vector in D-dimensions.
Definition: VectorD.h:48
A bounding box in D dimensions.
Simple D vector class.
An axis-aligned bounding box.
Definition: BoundingBoxD.h:26
HistogramD< D > get_probability_distribution_function() const
Definition: HistogramD.h:52
A class to represent a voxel grid.
double get_total_count() const
Get the sum of all counts in the histogram.
Definition: HistogramD.h:44
BoundingBoxD< D > get_bounding_box(const BoundingBoxD< D > &g)
Definition: BoundingBoxD.h:160
void add(const algebra::VectorInputD< D > &x, double weight=1)
Definition: HistogramD.h:34