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