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