KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
learned_discrete_prior.h
Go to the documentation of this file.
1 /* $Id$ */
35 /*=========================================================================== *
36  |
37  | Copyright (c) 1994-2010 by Kobus Barnard (author)
38  |
39  | Personal and educational use of this code is granted, provided that this
40  | header is kept intact, and that the authorship is not misrepresented, that
41  | its use is acknowledged in publications, and relevant papers are cited.
42  |
43  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
44  |
45  | Please note that the code in this file has not necessarily been adequately
46  | tested. Naturally, there is no guarantee of performance, support, or fitness
47  | for any particular task. Nonetheless, I am interested in hearing about
48  | problems that you encounter.
49  |
50  | Author: Emily Hartley
51  *=========================================================================== */
52 
53 #ifndef KJB_LEARNED_DISCRETE_PRIOR_H_
54 #define KJB_LEARNED_DISCRETE_PRIOR_H_
55 
56 #include "m_cpp/m_vector.h"
57 #include "l_cpp/l_readable.h"
58 #include "l_cpp/l_writeable.h"
59 #include <cmath>
60 
61 namespace kjb {
62 
72 {
73 private:
74  int num_bins;
75  double histo_max;
76  double histo_min;
77  Vector histo_bins;
78 
79 protected:
84  void find_bin
85  (
86  double& bin,
87  double element,
88  int numBins,
89  double min,
90  double max
91  ) const;
92 
93 public:
96  (
97  int numBins,
98  double max,
99  double min,
100  const char* fname,
101  int softBin=1
102  );
103 
105  {
106  num_bins = 1;
107  histo_max = 1.0;
108  histo_min = 0.0;
109  }
110 
113 
117  {
118  read(fname);
119  }
120 
124  {
125  read(in);
126  }
127 
130 
133 
135  virtual void read(std::istream& in) throw (kjb::Illegal_argument,
136  kjb::IO_error);
137 
139  virtual void read(const char * fname) throw (kjb::IO_error, kjb::Illegal_argument)
140  {
141  Readable::read(fname);
142  }
143 
145  virtual void write(std::ostream& out) const
146  throw (kjb::IO_error);
147 
149  virtual void write(const char * fname) const
150  throw (kjb::IO_error)
151  {
152  Writeable::write(fname);
153  }
154 
156  int get_num_bins() const;
157 
159  double get_histo_max() const;
160 
162  double get_histo_min() const;
163 
165  const Vector& get_histo_bins() const;
166 
171  int plot_histogram(const char* name=NULL) const;
172 
173  static void find_min_max(double & hmax, double & hmin, const char* fname);
174 
175  inline double evaluate_prior(double value) const
176  {
177  double dbin = 0.0;
178  find_bin(dbin, value, num_bins, histo_min, histo_max);
179  int bin = std::floor(dbin);
180  return histo_bins(bin);
181  }
182 
183 };
184 
185 } // namespace kjb
186 
187 #endif /* KJB_LEARNED_DISCRETE_PRIOR_H_ */
188 
const Vector & get_histo_bins() const
Returns the Vector containing the count of points in each bin.
Definition: learned_discrete_prior.cpp:350
Abstract class to write this object to an output stream.
Definition: l_writeable.h:41
void find_bin(double &bin, double element, int numBins, double min, double max) const
Determines which bin in the histogram the given element should be put in.
Definition: learned_discrete_prior.cpp:408
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
Abstract class to read this object from an input stream.
Definition: l_readable.h:39
virtual void read(std::istream &in)
Reads this parametric_parapiped from an input stream.
Definition: learned_discrete_prior.cpp:228
Learned_discrete_prior & operator=(const Learned_discrete_prior &dp)
Assignment operator.
Definition: learned_discrete_prior.cpp:214
virtual void write(const char *fname) const
Writes this parametric_parapiped to an output stream.
Definition: learned_discrete_prior.h:149
virtual void write(std::ostream &out) const
Writes this parametric_parapiped to a file.
Definition: learned_discrete_prior.cpp:308
~Learned_discrete_prior()
Destructor.
Definition: learned_discrete_prior.cpp:203
Learned_discrete_prior(const char *fname)
Constructs a parametric_parapiped from an input file.
Definition: learned_discrete_prior.h:115
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
virtual void write(std::ostream &out) const =0
Writes this Writeable to an output stream.
double get_histo_min() const
Returns the minimum value of the histogram.
Definition: learned_discrete_prior.cpp:342
int plot_histogram(const char *name=NULL) const
Creates a plot of the histogram and saves it in ps format in a file with the provided name or plot...
Definition: learned_discrete_prior.cpp:362
Int_matrix floor(const Matrix &m)
Definition: m_matrix.cpp:2026
virtual void read(const char *fname)
Reads this parametric_parapiped from a file.
Definition: learned_discrete_prior.h:139
Learned_discrete_prior()
Definition: learned_discrete_prior.h:104
Int_matrix::Value_type min(const Int_matrix &mat)
Return the minimum value in this matrix.
Definition: l_int_matrix.h:1385
Learned_discrete_prior(std::istream &in)
Constructs a parametric_parapiped from an input stream.
Definition: learned_discrete_prior.h:122
This class creates a histogram of a list of points and stores the number of bins in num_bins...
Definition: learned_discrete_prior.h:71
int get_num_bins() const
Returns the number of bins.
Definition: learned_discrete_prior.cpp:326
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
static void find_min_max(double &hmax, double &hmin, const char *fname)
Definition: learned_discrete_prior.cpp:431
virtual void read(std::istream &in)=0
Reads this Readable from an input stream.
Object thrown when input or output fails.
Definition: l_exception.h:496
double get_histo_max() const
Returns the maximum value of the histogram.
Definition: learned_discrete_prior.cpp:334
double evaluate_prior(double value) const
Definition: learned_discrete_prior.h:175
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...