contrib/mul/clsfy/clsfy_binary_1d_wrapper.h
Go to the documentation of this file.
00001 // Copyright: (C) 2009 Imorphics PLC
00002 #ifndef clsfy_binary_1d_wrapper_h_
00003 #define clsfy_binary_1d_wrapper_h_
00004 //:
00005 // \file
00006 // \brief Wrap a classifier_1d in general classifier_base derivative.
00007 // \author Ian Scott
00008 // \date 2 Sep 2009
00009 
00010 #include <vcl_iosfwd.h>
00011 #include <mbl/mbl_cloneable_ptr.h>
00012 #include <clsfy/clsfy_classifier_base.h>
00013 #include <clsfy/clsfy_classifier_1d.h>
00014 
00015 //: Make a clsfy_classifier_1d derivative look like a clsfy_classifierbase derivative.
00016 class clsfy_binary_1d_wrapper : public clsfy_classifier_base
00017 {
00018  protected:
00019   //: Underlying 1d classifier.
00020   mbl_cloneable_nzptr<clsfy_classifier_1d> classifier_1d_;
00021 
00022  public:
00023 
00024   clsfy_binary_1d_wrapper();
00025 
00026   //: Find the posterior probability of the input being in the positive class.
00027   // The result is outputs(0)
00028   virtual void class_probabilities(vcl_vector<double> &outputs, const vnl_vector<double> &input) const
00029   { return classifier_1d_->class_probabilities(outputs, input(0)); }
00030 
00031   //: Classify the input vector.
00032   // Returns a number between 0 and nClasses-1 inclusive to represent the most likely class
00033   virtual unsigned classify(const vnl_vector<double> &input) const
00034   { return classifier_1d_->classify(input(0)); }
00035 
00036   //: Log likelihood of being in the positive class.
00037   // Class probability = 1 / (1+exp(-log_l))
00038   virtual double log_l(const vnl_vector<double> &input) const
00039   { return classifier_1d_->log_l(input(0)); }
00040 
00041   //: Set the underlying classifier.
00042   // The object will take and maintain its own deep copy of the data.
00043   // This method should only be used by builders.
00044   void set_classifier_1d(const clsfy_classifier_1d &classifier)
00045   { classifier_1d_ = classifier; }
00046 
00047   //: Get the underlying classifier.
00048   const clsfy_classifier_1d &classifier_1d() const
00049   { return *classifier_1d_; }
00050 
00051   //: The dimensionality of input vectors.
00052   virtual unsigned n_dims() const { return 1u; }
00053 
00054   //: The number of possible output classes.
00055   // 1 indicates a binary classifier
00056   virtual unsigned n_classes() const { return 1u; }
00057 
00058   //: Name of the class
00059   virtual vcl_string is_a() const;
00060 
00061   //: Name of the class
00062   virtual bool is_class(vcl_string const& s) const;
00063 
00064   //: Print class to os
00065   virtual void print_summary(vcl_ostream& os) const;
00066 
00067   //: Save class to a binary File Stream
00068   virtual void b_write(vsl_b_ostream& bfs) const;
00069 
00070   //: Load the class from a Binary File Stream
00071   virtual void b_read(vsl_b_istream& bfs);
00072 
00073   //: Create a deep copy.
00074   // Client is responsible for deleting returned object.
00075   virtual clsfy_classifier_base* clone() const
00076   { return new clsfy_binary_1d_wrapper(*this); }
00077 };
00078 
00079 #endif // clsfy_binary_1d_wrapper_h_