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 //:
00081 // required if data is present in this class
00082 void clsfy_binary_threshold_1d::print_summary(vcl_ostream& os) const
00083 {
00084   os << "s: " << s_ << "   threshold: "<<threshold_<<'\n';
00085 }
00086 
00087 //=======================================================================
00088 
00089 short clsfy_binary_threshold_1d::version_no() const
00090 {
00091   return 1;
00092 }
00093 
00094 //=======================================================================
00095 
00096 void clsfy_binary_threshold_1d::b_write(vsl_b_ostream& bfs) const
00097 {
00098   vsl_b_write(bfs,version_no());
00099   vsl_b_write(bfs,s_);
00100   vsl_b_write(bfs,threshold_);
00101 }
00102 
00103 //=======================================================================
00104 
00105 void clsfy_binary_threshold_1d::b_read(vsl_b_istream& bfs)
00106 {
00107   if (!bfs) return;
00108 
00109   short version;
00110   vsl_b_read(bfs,version);
00111   switch (version)
00112   {
00113     case (1):
00114       vsl_b_read(bfs,s_);
00115       vsl_b_read(bfs,threshold_);
00116       break;
00117     default:
00118       vcl_cerr << "I/O ERROR: clsfy_binary_threshold_1d::b_read(vsl_b_istream&)\n"
00119                << "           Unknown version number "<< version << '\n';
00120       bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00121   }
00122 }
00123 
00124 //=======================================================================
00125 
00126 void vsl_b_write(vsl_b_ostream& bfs, const clsfy_binary_threshold_1d& b)
00127 {
00128   b.b_write(bfs);
00129 }
00130 
00131 //=======================================================================
00132 
00133 void vsl_b_read(vsl_b_istream& bfs, clsfy_binary_threshold_1d& b)
00134 {
00135   b.b_read(bfs);
00136 }
00137 
00138 //=======================================================================
00139 
00140 void vsl_print_summary(vcl_ostream& os,const clsfy_binary_threshold_1d& b)
00141 {
00142   os << b.is_a() << ": ";
00143   vsl_indent_inc(os);
00144   b.print_summary(os);
00145   vsl_indent_dec(os);
00146 }
00147 
00148 //=======================================================================
00149 
00150 vcl_ostream& operator<<(vcl_ostream& os,const clsfy_binary_threshold_1d& b)
00151 {
00152   vsl_print_summary(os,b);
00153   return os;
00154 }