contrib/mul/clsfy/clsfy_parzen_builder.cxx
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_parzen_builder.cxx
00002 // Copyright (c) 2001: British Telecommunications plc
00003 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00004 #pragma implementation
00005 #endif
00006 //:
00007 // \file
00008 // \brief Implement a Parzen window classifier builder
00009 // \author Ian Scott
00010 // \date 2001-10-07
00011 
00012 #include "clsfy_parzen_builder.h"
00013 
00014 #include <vcl_iostream.h>
00015 #include <vcl_sstream.h>
00016 #include <vcl_string.h>
00017 #include <vcl_cassert.h>
00018 #include <vsl/vsl_binary_loader.h>
00019 #include <vul/vul_string.h>
00020 #include <mbl/mbl_parse_block.h>
00021 #include <mbl/mbl_read_props.h>
00022 #include <clsfy/clsfy_rbf_parzen.h>
00023 
00024 //=======================================================================
00025 
00026 clsfy_parzen_builder::clsfy_parzen_builder():
00027 sigma_(1.0), power_(2.0)
00028 {
00029 }
00030 
00031 
00032 //=======================================================================
00033 
00034 short clsfy_parzen_builder::version_no() const
00035 {
00036   return 1;
00037 }
00038 
00039 //=======================================================================
00040 
00041 vcl_string clsfy_parzen_builder::is_a() const
00042 {
00043   return vcl_string("clsfy_parzen_builder");
00044 }
00045 
00046 //=======================================================================
00047 
00048 bool clsfy_parzen_builder::is_class(vcl_string const& s) const
00049 {
00050   return s == clsfy_parzen_builder::is_a() || clsfy_builder_base::is_class(s);
00051 }
00052 
00053 //=======================================================================
00054 
00055 clsfy_builder_base* clsfy_parzen_builder::clone() const
00056 {
00057   return new clsfy_parzen_builder(*this);
00058 }
00059 
00060 //=======================================================================
00061 
00062 void clsfy_parzen_builder::print_summary(vcl_ostream& os) const
00063 {
00064   os << "rbf width = " << sigma_ << ", power = "<< power_;
00065 }
00066 
00067 //=======================================================================
00068 
00069 void clsfy_parzen_builder::b_write(vsl_b_ostream& bfs) const
00070 {
00071   vsl_b_write(bfs, version_no());
00072   vsl_b_write(bfs, sigma_);
00073   vsl_b_write(bfs, power_);
00074 }
00075 
00076 //=======================================================================
00077 
00078 void clsfy_parzen_builder::b_read(vsl_b_istream& bfs)
00079 {
00080   if (!bfs) return;
00081 
00082   short version;
00083   vsl_b_read(bfs,version);
00084   switch (version)
00085   {
00086   case (1):
00087     vsl_b_read(bfs, sigma_);
00088     vsl_b_read(bfs, power_);
00089     break;
00090   default:
00091     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, clsfy_parzen_builder&)\n"
00092              << "           Unknown version number "<< version << '\n';
00093     bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00094   }
00095 }
00096 
00097 //=======================================================================
00098 
00099 //: Build model from data
00100 // return the mean error over the training set.
00101 // For many classifiers, you may use nClasses==1 to
00102 // indicate a binary classifier
00103 double clsfy_parzen_builder::build(clsfy_classifier_base& model,
00104                                    mbl_data_wrapper<vnl_vector<double> >& inputs,
00105                                    unsigned /* nClasses */,
00106                                    const vcl_vector<unsigned> &outputs) const
00107 {
00108   assert(model.is_class("clsfy_rbf_parzen")); // equiv to dynamic_cast<> != 0
00109   assert(inputs.size()==outputs.size());
00110 
00111   clsfy_rbf_parzen &parzen = (clsfy_rbf_parzen&) model;
00112 
00113   vcl_vector<vnl_vector<double> > vin(inputs.size());
00114 
00115   inputs.reset();
00116   unsigned i=0;
00117   do
00118   {
00119     vin[i++] = inputs.current();
00120   } while (inputs.next());
00121 
00122   assert(i==inputs.size());
00123 
00124   parzen.set(vin, outputs);
00125   parzen.set_power(power_);
00126   parzen.set_rbf_width(sigma_);
00127   return clsfy_test_error(model, inputs, outputs);
00128 }
00129 
00130 //=======================================================================
00131 
00132 
00133 //: Set the 1st standard deviation width of the RBF window.
00134 // The default value is 1. Really this could be better named as the RBF radius.
00135 void clsfy_parzen_builder::set_rbf_width(double sigma)
00136 {
00137   assert(sigma > 0.0);
00138   sigma_=sigma;
00139 }
00140 
00141 //=======================================================================
00142 
00143 //: The value p in the window function $exp(-1/(2*sigma^p) * |x-y|^p)$.
00144 // The value p affects the kurtosis, or peakyness of the window.
00145 // Towards 0 gives a more peaked central spike, and longer tail.
00146 // Toward +inf gives a broader peak, and shorter tail.
00147 // The default value is 2, giving a Gaussian distribution.
00148 void clsfy_parzen_builder::set_power(double p)
00149 {
00150   assert(p != 0.0);
00151   power_ = p;
00152 }
00153 
00154 //=======================================================================
00155 
00156 //: Create empty classifier
00157 // Caller is responsible for deletion
00158 clsfy_classifier_base* clsfy_parzen_builder::new_classifier() const
00159 {
00160   return new clsfy_rbf_parzen();
00161 }
00162 
00163 //=======================================================================
00164 //: Initialise the parameters from a text stream.
00165 // The next non-ws character in the stream should be a '{'
00166 // \verbatim
00167 // {
00168 //   sigma: 1.0  (optional, width of RBF)
00169 //   power: 2.0  (optional, exponent of RBF)
00170 // }
00171 // \endverbatim
00172 // \throw mbl_exception_parse_error if the parse fails.
00173 
00174 void clsfy_parzen_builder::config(vcl_istream &as)
00175 {
00176  vcl_string s = mbl_parse_block(as);
00177 
00178   vcl_istringstream ss(s);
00179   mbl_read_props_type props = mbl_read_props_ws(ss);
00180 
00181   {
00182     sigma_= vul_string_atof(props.get_optional_property("sigma", "1.0"));
00183     power_= vul_string_atof(props.get_optional_property("k", "2.0"));
00184   }
00185 
00186   // Check for unused props
00187   mbl_read_props_look_for_unused_props(
00188     "clsfy_parzen_builder::config", props, mbl_read_props_type());
00189 }