contrib/mul/clsfy/clsfy_binary_1d_wrapper_builder.cxx
Go to the documentation of this file.
00001 // Copyright: (C) 2009 Imorphics PLC
00002 #include "clsfy_binary_1d_wrapper_builder.h"
00003 //:
00004 // \file
00005 // \brief Wrap a builder_1d in general builder_base derivative.
00006 // \author Ian Scott
00007 // \date 2 Sep 2009
00008 
00009 //=======================================================================
00010 
00011 #include <vcl_string.h>
00012 #include <vcl_iostream.h>
00013 #include <vcl_sstream.h>
00014 #include <vcl_vector.h>
00015 #include <vcl_cassert.h>
00016 #include <vcl_algorithm.h>
00017 #include <mbl/mbl_parse_block.h>
00018 #include <mbl/mbl_read_props.h>
00019 #include <clsfy/clsfy_binary_threshold_1d_builder.h>
00020 #include <clsfy/clsfy_classifier_1d.h>
00021 
00022 // Dflt ctor
00023 clsfy_binary_1d_wrapper_builder::clsfy_binary_1d_wrapper_builder():
00024   builder_1d_(new clsfy_binary_threshold_1d_builder)
00025 {}
00026 
00027 //: Create a new untrained linear classifier with binary output
00028   clsfy_classifier_base* clsfy_binary_1d_wrapper_builder::new_classifier() const
00029 {
00030   vcl_auto_ptr<clsfy_classifier_1d> c_1d(builder_1d_->new_classifier());
00031 
00032   clsfy_binary_1d_wrapper classifier;
00033   classifier.set_classifier_1d(*c_1d);
00034   return classifier.clone();
00035 }
00036 
00037 
00038 //=======================================================================
00039 
00040 vcl_string clsfy_binary_1d_wrapper_builder::is_a() const
00041 {
00042   return vcl_string("clsfy_binary_1d_wrapper_builder");
00043 }
00044 
00045 //=======================================================================
00046 
00047 bool clsfy_binary_1d_wrapper_builder::is_class(vcl_string const& s) const
00048 {
00049   return s == clsfy_binary_1d_wrapper_builder::is_a() || clsfy_builder_base::is_class(s);
00050 }
00051 
00052 //=======================================================================
00053 
00054 void clsfy_binary_1d_wrapper_builder::print_summary(vcl_ostream& os) const
00055 {
00056   os << "Underlying 1d builder: "; vsl_print_summary(os, builder_1d_);
00057 }
00058 
00059 //=======================================================================
00060 
00061 //: Build a multi layer perceptron classifier, with the given data.
00062 double clsfy_binary_1d_wrapper_builder::build(
00063   clsfy_classifier_base &classifier,
00064   mbl_data_wrapper<vnl_vector<double> > &inputs,
00065   const vcl_vector<unsigned> &outputs) const
00066 {
00067   assert(outputs.size() == inputs.size());
00068   assert(* vcl_max_element(outputs.begin(), outputs.end()) <= 1);
00069   assert(classifier.is_class("clsfy_binary_1d_wrapper"));
00070 
00071   clsfy_binary_1d_wrapper &c_wrap = (clsfy_binary_1d_wrapper &) classifier;
00072 
00073   vcl_auto_ptr<clsfy_classifier_1d> c_1d(builder_1d_->new_classifier());
00074 
00075   vnl_vector<double> inputs_1d(inputs.size());
00076   unsigned i=0;
00077   do
00078   {
00079     inputs_1d(i++)=inputs.current()(0);
00080   } while (inputs.next());
00081 
00082   assert (i=inputs.size());
00083   vnl_vector<double> wts(inputs.size(), 1.0/inputs.size());
00084   builder_1d_->build(*c_1d, inputs_1d, wts, outputs);
00085 
00086   c_wrap.set_classifier_1d(*c_1d);
00087 
00088   return clsfy_test_error(classifier, inputs, outputs);
00089 }
00090 
00091 
00092 //=======================================================================
00093 
00094 
00095 //: Build a linear classifier, with the given data.
00096 // Return the mean error over the training set.
00097 // n_classes must be 1.
00098 double clsfy_binary_1d_wrapper_builder::build(
00099   clsfy_classifier_base &classifier, mbl_data_wrapper<vnl_vector<double> > &inputs,
00100   unsigned n_classes, const vcl_vector<unsigned> &outputs) const
00101 {
00102   assert (n_classes == 1);
00103   return build(classifier, inputs, outputs);
00104 }
00105 
00106 //=======================================================================
00107 
00108 void clsfy_binary_1d_wrapper_builder::b_write(vsl_b_ostream &bfs) const
00109 {
00110   const short version_no=1;
00111   vsl_b_write(bfs, version_no);
00112   vsl_b_write(bfs, builder_1d_);
00113 }
00114 
00115 //=======================================================================
00116 
00117 void clsfy_binary_1d_wrapper_builder::b_read(vsl_b_istream &bfs)
00118 {
00119   if (!bfs) return;
00120 
00121   short version;
00122   vsl_b_read(bfs, version);
00123   switch (version)
00124   {
00125     case 1:
00126      vsl_b_read(bfs, builder_1d_);
00127      break;
00128     default:
00129       vcl_cerr << "I/O ERROR: clsfy_binary_1d_wrapper_builder::b_read(vsl_b_istream&)\n"
00130                << "           Unknown version number "<< version << '\n';
00131       bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00132   }
00133 }
00134 
00135 
00136 //=======================================================================
00137 //: Initialise the parameters from a text stream.
00138 // The next non-ws character in the stream should be a '{'
00139 // \verbatim
00140 // {
00141 //   builder_1d: clsfy_binary_threshold_1d_builder  (required, underlying builder)
00142 // }
00143 // \endverbatim
00144 // \throw mbl_exception_parse_error if the parse fails.
00145 void clsfy_binary_1d_wrapper_builder::config(vcl_istream &as)
00146 {
00147  vcl_string s = mbl_parse_block(as);
00148 
00149   vcl_istringstream ss(s);
00150   mbl_read_props_type props = mbl_read_props_ws(ss);
00151 
00152   {
00153     vcl_stringstream ss2(props.get_required_property("builder_1d"));
00154     vcl_auto_ptr<clsfy_builder_1d> b_1d =
00155       clsfy_builder_1d::new_builder(ss2);
00156   }
00157 
00158   // Check for unused props
00159   mbl_read_props_look_for_unused_props(
00160     "clsfy_binary_1d_wrapper_builder::config", props, mbl_read_props_type());
00161 }