1 #ifndef mbl_histogram_h_ 2 #define mbl_histogram_h_ 3 4 //: 5 // \file 6 // \brief Simple object to build histogram from supplied data. 7 // \author Tim Cootes 8 9 #include <iostream> 10 #include <iosfwd> 11 #include <vector> 12 #ifdef _MSC_VER 13 # include <vcl_msvc_warnings.h> 14 #endif 15 #include <vsl/vsl_binary_io.h> 16 17 //: Simple object to build histogram from supplied data. 18 // Assumes all 1D data can be cast to doubles 19 // One could perhaps re-write this, templated by object type, if really necessary. 20 class mbl_histogram 21 { 22 //: Position of limits of each bin. 23 // Bin i is [bins_[i],bins_[i+1]) 24 std::vector<double> bins_; 25 26 //: Number in each bin 27 std::vector<int> freq_; 28 29 //: Number below lowest bin 30 int n_below_; 31 32 //: Number above highest bin 33 int n_above_; 34 35 //: Total number of examples supplied 36 int n_obs_; 37 public: 38 //: Construct with no bins 39 mbl_histogram(); 40 41 //: Construct with given number of bins over given range 42 mbl_histogram(double x_lo, double x_hi, int n_bins); 43 44 //: Define number and size of bins 45 void set_bins(double x_lo, double x_hi, int n_bins); 46 47 //: Remove all data 48 void clear(); 49 50 //: Add given observation 51 void obs(double v); 52 53 //: Number of bins n_bins()54 int n_bins() const { return freq_.size(); } 55 56 //: Position of limits of each bin. 57 // Bin i is [bins_[i],bins_[i+1]) bins()58 const std::vector<double>& bins() const { return bins_; } 59 60 //: Number of observations n_obs()61 int n_obs() const { return n_obs_;} 62 63 //: Number in each bin frequency()64 const std::vector<int>& frequency() const { return freq_; } 65 66 //: Number below lowest bin n_below()67 int n_below() const { return n_below_; } 68 69 //: Number above highest bin n_above()70 int n_above() const { return n_above_; } 71 72 //: Write out probabilities (freq/n_obs) to a named file 73 // Can then be plotted by your favorite tool 74 // 75 // Format: (bin-centre) prob (one per line) 76 // \return true if successful 77 bool write_probabilities(const char* path); 78 79 //: Write out cumulative probability distribution to a named file 80 // Format: (bin-centre) sum_prob (one per line) 81 // \return true if successful 82 bool write_cdf(const char* path); 83 84 void print_summary(std::ostream& os) const; 85 //: Version number for I/O 86 short version_no() const; 87 void b_write(vsl_b_ostream& bfs) const; 88 void b_read(vsl_b_istream& bfs); 89 90 //: Test for equality 91 bool operator==(const mbl_histogram& s) const; 92 }; 93 94 //: Binary file stream output operator for class reference 95 void vsl_b_write(vsl_b_ostream& bfs, const mbl_histogram& histo); 96 97 //: Binary file stream input operator for class reference 98 void vsl_b_read(vsl_b_istream& bfs, mbl_histogram& histo); 99 100 //: Stream output operator for class reference 101 std::ostream& operator<<(std::ostream& os, const mbl_histogram& histo); 102 103 //: Stream output operator for class reference 104 void vsl_print_summary(std::ostream& os, const mbl_histogram& histo); 105 106 #endif 107