contrib/mul/mcal/mcal_component_analyzer.h
Go to the documentation of this file.
00001 #ifndef mcal_component_analyzer_h
00002 #define mcal_component_analyzer_h
00003 //:
00004 // \file
00005 // \author Tim Cootes
00006 // \brief Base for objects which perform some form of linear component analysis.
00007 
00008 #include <vsl/vsl_fwd.h>
00009 #include <vcl_string.h>
00010 #include <vcl_memory.h>
00011 #include <vnl/vnl_vector.h>
00012 #include <vnl/vnl_matrix.h>
00013 #include <mbl/mbl_data_wrapper.h>
00014 #include <vcl_iosfwd.h>
00015 
00016 //: Base for objects which perform some form of linear component analysis.
00017 // Derived classes (such as mcal_pca) generate linear modes to span
00018 // the data space so as to optimise certain criteria.
00019 class mcal_component_analyzer
00020 {
00021  public:
00022 
00023   //: Dflt ctor
00024   mcal_component_analyzer();
00025 
00026   //: Destructor
00027   virtual ~mcal_component_analyzer();
00028 
00029   //: Compute the mean of the supplied data
00030   void compute_mean(mbl_data_wrapper<vnl_vector<double> >& data,
00031                      vnl_vector<double>& mean);
00032 
00033   //: Compute mean and modes from the supplied data
00034   //  Model is x = mean + modes*b,  where b is a vector of weights on each mode.
00035   //  mode_var[i] gives the variance of the data projected onto that mode.
00036   //  Default computes the mean, then calls build_about_mean
00037   virtual void build(mbl_data_wrapper<vnl_vector<double> >& data,
00038                      vnl_vector<double>& mean,
00039                      vnl_matrix<double>& modes,
00040                      vnl_vector<double>& mode_var);
00041 
00042   //: Compute modes of the supplied data relative to the supplied mean
00043   //  Model is x = mean + modes*b,  where b is a vector of weights on each mode.
00044   //  mode_var[i] gives the variance of the data projected onto that mode.
00045   virtual void build_about_mean(mbl_data_wrapper<vnl_vector<double> >& data,
00046                                 const vnl_vector<double>& mean,
00047                                 vnl_matrix<double>& modes,
00048                                 vnl_vector<double>& mode_var) =0;
00049 
00050   //: Build mean and modes from data in array data[0..n-1]
00051   //  Utility function - wraps data and uses build(data,mean,modes)
00052   virtual void build_from_array(const vnl_vector<double>* data, int n,
00053                                 vnl_vector<double>& mean,
00054                                 vnl_matrix<double>& modes,
00055                                 vnl_vector<double>& mode_var);
00056 
00057   //: Version number for I/O
00058   short version_no() const;
00059 
00060   //: Name of the class
00061   virtual vcl_string is_a() const = 0;
00062 
00063   //: Create a copy on the heap and return base class pointer
00064   virtual mcal_component_analyzer* clone() const = 0;
00065 
00066   //: Print class to os
00067   virtual void print_summary(vcl_ostream& os) const =0;
00068 
00069   //: Save class to binary file stream
00070   virtual void b_write(vsl_b_ostream& bfs) const=0;
00071 
00072   //: Load class from binary file stream
00073   virtual void b_read(vsl_b_istream& bfs)=0;
00074 
00075   //: Read initialisation settings from a stream.
00076   // The default implementation merely checks that no properties have
00077   // been specified.
00078   virtual void config_from_stream(vcl_istream &);
00079 
00080   //: Create a concrete mcal_component_analyzer object, from a text specification.
00081   static vcl_auto_ptr<mcal_component_analyzer> create_from_stream(vcl_istream &is);
00082 };
00083 
00084 //: Allows derived class to be loaded by base-class pointer
00085 //  A loader object exists which is invoked by calls
00086 //  of the form "vsl_b_read(bfs,base_ptr);".  This loads derived class
00087 //  objects from the disk, places them on the heap and
00088 //  returns a base class pointer.
00089 //  In order to work the loader object requires
00090 //  an instance of each derived class that might be
00091 //  found.  This function gives the model class to
00092 //  the appropriate loader.
00093 void vsl_add_to_binary_loader(const mcal_component_analyzer& b);
00094 
00095 //: Binary file stream output operator for class reference
00096 void vsl_b_write(vsl_b_ostream& bfs, const mcal_component_analyzer& b);
00097 
00098 //: Binary file stream input operator for class reference
00099 void vsl_b_read(vsl_b_istream& bfs, mcal_component_analyzer& b);
00100 
00101 //: Stream output operator for class reference
00102 vcl_ostream& operator<<(vcl_ostream& os,const mcal_component_analyzer& b);
00103 
00104 //: Stream output operator for class pointer
00105 vcl_ostream& operator<<(vcl_ostream& os,const mcal_component_analyzer* b);
00106 
00107 #endif // mcal_component_analyzer_h