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