contrib/mul/clsfy/clsfy_binary_threshold_1d.cxx

Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_binary_threshold_1d.cxx
00002 #include "clsfy_binary_threshold_1d.h"
00003 //:
00004 // \file
00005 // \brief Simplest possible 1D classifier: A single thresholding function
00006 // \author Tim Cootes
00007 
00008 //=======================================================================
00009 
00010 #include <vcl_string.h>
00011 #include <vcl_iostream.h>
00012 #include <vcl_vector.h>
00013 #include <vcl_cassert.h>
00014 #include <vcl_cmath.h>
00015 #include <vsl/vsl_binary_io.h>
00016 #include <vsl/vsl_indent.h>
00017 #include <vnl/vnl_double_2.h>
00018 #include <vnl/io/vnl_io_vector.h>
00019 #include <vnl/io/vnl_io_matrix.h>
00020 
00021 //=======================================================================
00022 
00023 //: Find the posterior probability of the input being in the positive class.
00024 // The result is outputs(0)
00025 void clsfy_binary_threshold_1d::class_probabilities(vcl_vector<double> &outputs,
00026                                                     double input) const
00027 {
00028   outputs.resize(1);
00029   outputs[0] = 1.0 / (1.0 + vcl_exp(-log_l(input)));
00030 }
00031 
00032 
00033 //=======================================================================
00034 
00035 //: Log likelihood of being in the positive class.
00036 // Class probability = 1 / (1+exp(-log_l))
00037 double clsfy_binary_threshold_1d::log_l(double input) const
00038 {
00039   return s_*input - threshold_;  //ie distance above threshold
00040 }
00041 
00042 //: Return parameters defining classifier in a vector (format depends on classifier)
00043 vnl_vector<double> clsfy_binary_threshold_1d::params() const
00044 {
00045   return vnl_double_2(s_,threshold_).as_vector();
00046 }
00047 
00048 //: Set parameters defining classifier with a vector (format depends on classifier)
00049 void clsfy_binary_threshold_1d::set_params(const vnl_vector<double>& p)
00050 {
00051   assert(p.size()==2);
00052   s_=p[0];
00053   threshold_=p[1];
00054 }
00055 
00056 //=======================================================================
00057 
00058 //: Equality operator for 1d classifiers
00059 bool clsfy_binary_threshold_1d::operator==(const clsfy_classifier_1d& x) const
00060 {
00061   assert( x.is_class("clsfy_binary_threshold_1d"));
00062   clsfy_binary_threshold_1d& x2= (clsfy_binary_threshold_1d&) x;
00063   return x2.s_ == s_ &&
00064          x2.threshold_ == threshold_;
00065 }
00066 
00067 
00068 vcl_string clsfy_binary_threshold_1d::is_a() const
00069 {
00070   return vcl_string("clsfy_binary_threshold_1d");
00071 }
00072 
00073 bool clsfy_binary_threshold_1d::is_class(vcl_string const& s) const
00074 {
00075   return s == clsfy_binary_threshold_1d::is_a() || clsfy_classifier_1d::is_class(s);
00076 }
00077 
00078 //=======================================================================
00079 
00080 // required if data is present in this class
00081 void clsfy_binary_threshold_1d::print_summary(vcl_ostream& os) const
00082 {
00083   os << "s: " << s_ << "   threshold: "<<threshold_<<'\n';
00084 }
00085 
00086 //=======================================================================
00087 
00088 short clsfy_binary_threshold_1d::version_no() const
00089 {
00090   return 1;
00091 }
00092 
00093 //=======================================================================
00094 
00095 void clsfy_binary_threshold_1d::b_write(vsl_b_ostream& bfs) const
00096 {
00097   vsl_b_write(bfs,version_no());
00098   vsl_b_write(bfs,s_);
00099   vsl_b_write(bfs,threshold_);
00100 }
00101 
00102 //=======================================================================
00103 
00104 void clsfy_binary_threshold_1d::b_read(vsl_b_istream& bfs)
00105 {
00106   if (!bfs) return;
00107 
00108   short version;
00109   vsl_b_read(bfs,version);
00110   switch (version)
00111   {
00112     case (1):
00113       vsl_b_read(bfs,s_);
00114       vsl_b_read(bfs,threshold_);
00115       break;
00116     default:
00117       vcl_cerr << "I/O ERROR: clsfy_binary_threshold_1d::b_read(vsl_b_istream&)\n"
00118                << "           Unknown version number "<< version << '\n';
00119       bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00120   }
00121 }
00122 
00123 //=======================================================================
00124 
00125 void vsl_b_write(vsl_b_ostream& bfs, const clsfy_binary_threshold_1d& b)
00126 {
00127   b.b_write(bfs);
00128 }
00129 
00130 //=======================================================================
00131 
00132 void vsl_b_read(vsl_b_istream& bfs, clsfy_binary_threshold_1d& b)
00133 {
00134   b.b_read(bfs);
00135 }
00136 
00137 //=======================================================================
00138 
00139 void vsl_print_summary(vcl_ostream& os,const clsfy_binary_threshold_1d& b)
00140 {
00141   os << b.is_a() << ": ";
00142   vsl_indent_inc(os);
00143   b.print_summary(os);
00144   vsl_indent_dec(os);
00145 }
00146 
00147 //=======================================================================
00148 
00149 vcl_ostream& operator<<(vcl_ostream& os,const clsfy_binary_threshold_1d& b)
00150 {
00151   vsl_print_summary(os,b);
00152   return os;
00153 }

Generated on Thu Jan 8 05:11:33 2009 for contrib/mul/clsfy by  doxygen 1.5.1