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