contrib/mul/clsfy/clsfy_builder_1d.h
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_builder_1d.h
00002 #ifndef clsfy_builder_1d_h_
00003 #define clsfy_builder_1d_h_
00004 //:
00005 // \file
00006 // \brief Describe an abstract classifier builder for scalar data
00007 // \author Tim Cootes
00008 
00009 #include <vcl_string.h>
00010 #include <vcl_vector.h>
00011 #include <vcl_memory.h>
00012 #include <vbl/vbl_triple.h>
00013 #include <vsl/vsl_binary_io.h>
00014 #include <vnl/vnl_vector.h>
00015 #include <vcl_iostream.h>
00016 
00017 class clsfy_classifier_1d;
00018 
00019 //: Base for classes to build clsfy_classifier_1d objects
00020 class clsfy_builder_1d
00021 {
00022  public:
00023 
00024   // Destructor
00025   virtual ~clsfy_builder_1d() {}
00026 
00027   //: Create empty model
00028   virtual clsfy_classifier_1d* new_classifier() const = 0;
00029 
00030 
00031   //: Build a binary_threshold classifier
00032   //  Train classifier, returning weighted error
00033   //  Selects parameters of classifier which best separate examples from two classes,
00034   //  weighting examples appropriately when estimating the misclassification rate.
00035   //  Returns weighted sum of error, e.wts, where e_i =0 for correct classifications,
00036   //  e_i=1 for incorrect.
00037   virtual double build(clsfy_classifier_1d& classifier,
00038                        const vnl_vector<double>& egs,
00039                        const vnl_vector<double>& wts,
00040                        const vcl_vector<unsigned> &outputs) const = 0;
00041 
00042 
00043   //: Build a binary_threshold classifier
00044   //  Train classifier, returning weighted error
00045   //  Selects parameters of classifier which best separate examples from two classes,
00046   //  weighting examples appropriately when estimating the misclassification rate.
00047   //  Returns weighted sum of error, e.wts, where e_i =0 for correct classifications,
00048   //  e_i=1 for incorrect.
00049   virtual double build(clsfy_classifier_1d& classifier,
00050                        vnl_vector<double>& egs0,
00051                        vnl_vector<double>& wts0,
00052                        vnl_vector<double>& egs1,
00053                        vnl_vector<double>& wts1) const = 0;
00054 
00055   //: Train classifier, returning weighted error
00056   //  Selects parameters of classifier which best separate examples,
00057   //  weighting examples appropriately when estimating the misclassification rate.
00058   //  data[i] is a triple, {value,class_number,weight}
00059   //  Returns weighted sum of error.
00060   //  Note that input "data" must be sorted to use this routine
00061   virtual double build_from_sorted_data(clsfy_classifier_1d& classifier,
00062                                         const vbl_triple<double,int,int> *data,
00063                                         const vnl_vector<double>& wts) const = 0;
00064 
00065   //: Name of the class
00066   virtual vcl_string is_a() const;
00067 
00068   //: Name of the class
00069   virtual bool is_class(vcl_string const& s) const;
00070 
00071   //: Create a copy on the heap and return base class pointer
00072   virtual clsfy_builder_1d* clone() const = 0;
00073 
00074   //: Print class to os
00075   virtual void print_summary(vcl_ostream& os) const = 0;
00076 
00077   //: Save class to binary file stream
00078   virtual void b_write(vsl_b_ostream& bfs) const = 0;
00079 
00080   //: Load class from binary file stream
00081 
00082   virtual void b_read(vsl_b_istream& bfs) = 0;
00083   //: Initialise the parameters from a text stream.
00084   // Default case accepts no parameters.
00085   virtual void config(vcl_istream &as);
00086 
00087   //: Load description from a text stream
00088   static vcl_auto_ptr<clsfy_builder_1d> new_builder(
00089     vcl_istream &as);
00090 };
00091 
00092 //: Allows derived class to be loaded by base-class pointer
00093 void vsl_add_to_binary_loader(const clsfy_builder_1d& b);
00094 
00095 //: Binary file stream output operator for class reference
00096 void vsl_b_write(vsl_b_ostream& bfs, const clsfy_builder_1d& b);
00097 
00098 //: Binary file stream input operator for class reference
00099 void vsl_b_read(vsl_b_istream& bfs, clsfy_builder_1d& b);
00100 
00101 //: Stream output operator for class reference
00102 vcl_ostream& operator<<(vcl_ostream& os,const clsfy_builder_1d& b);
00103 
00104 //: Stream output operator for class pointer
00105 vcl_ostream& operator<<(vcl_ostream& os,const clsfy_builder_1d* b);
00106 
00107 //: Stream output operator for class pointer
00108 vcl_ostream& operator<<(vcl_ostream& os,const clsfy_builder_1d* b);
00109 
00110 //! Stream output operator for class reference
00111 inline void vsl_print_summary(vcl_ostream& os, const clsfy_builder_1d& b)
00112 { os << b; }
00113 
00114 //! Stream output operator for class pointer
00115 inline void vsl_print_summary(vcl_ostream& os, const clsfy_builder_1d* b)
00116 { os << b; }
00117 
00118 #endif // clsfy_builder_1d_h_