contrib/mul/clsfy/clsfy_null_builder.cxx
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_null_builder.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \brief Implement a null classifier builder
00008 // \author Ian Scott
00009 // \date 2009-08-27
00010 
00011 #include "clsfy_null_builder.h"
00012 
00013 #include <vcl_iostream.h>
00014 #include <vcl_sstream.h>
00015 #include <vcl_string.h>
00016 #include <vcl_algorithm.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_null_classifier.h>
00022 
00023 
00024 //=======================================================================
00025 
00026 vcl_string clsfy_null_builder::is_a() const
00027 {
00028   return vcl_string("clsfy_null_builder");
00029 }
00030 
00031 //=======================================================================
00032 
00033 bool clsfy_null_builder::is_class(vcl_string const& s) const
00034 {
00035   return s == clsfy_null_builder::is_a() || clsfy_builder_base::is_class(s);
00036 }
00037 
00038 //=======================================================================
00039 
00040 clsfy_builder_base* clsfy_null_builder::clone() const
00041 {
00042   return new clsfy_null_builder(*this);
00043 }
00044 
00045 //=======================================================================
00046 
00047 void clsfy_null_builder::print_summary(vcl_ostream& os) const
00048 {
00049   os << "default_class: " << default_class_;
00050 }
00051 
00052 //=======================================================================
00053 
00054 void clsfy_null_builder::b_write(vsl_b_ostream& bfs) const
00055 {
00056   const short version_no = 1;
00057   vsl_b_write(bfs, version_no);
00058   vsl_b_write(bfs, default_class_);
00059 }
00060 
00061 //=======================================================================
00062 
00063 void clsfy_null_builder::b_read(vsl_b_istream& bfs)
00064 {
00065   if (!bfs) return;
00066 
00067   short version;
00068   vsl_b_read(bfs,version);
00069   switch (version)
00070   {
00071   case 1:
00072     vsl_b_read(bfs, default_class_);
00073     break;
00074   default:
00075     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, clsfy_null_builder&)\n"
00076              << "           Unknown version number "<< version << '\n';
00077     bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00078   }
00079 }
00080 
00081 //=======================================================================
00082 
00083 //: Build model from data
00084 // return the mean error over the training set.
00085 // For many classifiers, you may use nClasses==1 to
00086 // indicate a binary classifier
00087 double clsfy_null_builder::build(clsfy_classifier_base& model,
00088                                  mbl_data_wrapper<vnl_vector<double> >& inputs,
00089                                  unsigned /* nClasses */,
00090                                  const vcl_vector<unsigned> &outputs) const
00091 {
00092   clsfy_null_classifier &classifier = dynamic_cast<clsfy_null_classifier&>(model);
00093 
00094   classifier.set_default_class(default_class_);
00095 
00096   return static_cast<double>(
00097     vcl_count(outputs.begin(), outputs.end(), default_class_) )
00098     / outputs.size();
00099 }
00100 
00101 //=======================================================================
00102 //: Create empty classifier
00103 // Caller is responsible for deletion
00104 clsfy_classifier_base* clsfy_null_builder::new_classifier() const
00105 {
00106   return new clsfy_null_classifier();
00107 }
00108 
00109 //=======================================================================
00110 //: Initialise the parameters from a text stream.
00111 // The next non-ws character in the stream should be a '{'
00112 // \verbatim
00113 // {
00114 //   default_class: 1  (default 0)
00115 // }
00116 // \endverbatim
00117 // \throw mbl_exception_parse_error if the parse fails.
00118 void clsfy_null_builder::config(vcl_istream &as)
00119 {
00120  vcl_string s = mbl_parse_block(as);
00121 
00122   vcl_istringstream ss(s);
00123   mbl_read_props_type props = mbl_read_props_ws(ss);
00124 
00125   {
00126     default_class_ = vul_string_atoi(
00127       props.get_optional_property("default_class", "0") );
00128   }
00129 
00130   // Check for unused props
00131   mbl_read_props_look_for_unused_props(
00132     "clsfy_null_builder::config", props, mbl_read_props_type());
00133 }