contrib/mul/mcal/mcal_general_ca.h
Go to the documentation of this file.
00001 #ifndef mcal_general_ca_h_
00002 #define mcal_general_ca_h_
00003 //:
00004 // \file
00005 // \author Tim Cootes
00006 // \brief Class to perform general Component Analysis
00007 
00008 #include <mcal/mcal_component_analyzer.h>
00009 #include <mcal/mcal_single_basis_cost.h>
00010 #include <mbl/mbl_cloneable_ptr.h>
00011 #include <vnl/io/vnl_io_vector.h>
00012 #include <vnl/io/vnl_io_matrix.h>
00013 #include <mbl/mbl_data_wrapper.h>
00014 #include <vcl_iosfwd.h>
00015 
00016 //: Class to perform general Component Analysis
00017 //  After an initial component analysis, this rotates pairs of 
00018 //  basis vectors so as to optimise an objective function,
00019 //  which includes terms to do with the basis vectors themselves.
00020 //
00021 //  It assumes that the initial components are orthogonal.
00022 class mcal_general_ca : public mcal_component_analyzer
00023 {
00024  private:
00025   //: Object to perform initial component analysis (usually PCA)
00026   mbl_cloneable_ptr<mcal_component_analyzer> initial_ca_;
00027 
00028   //: Object to evaluate component of cost associated with each basis vector
00029   mbl_cloneable_ptr<mcal_single_basis_cost> basis_cost_;
00030 
00031   //: Maximum number of passes during optimisation
00032   unsigned max_passes_;
00033 
00034   //: Threshold on angular movement, used to terminate optimisation
00035   double move_thresh_;
00036 
00037   //: Optimise the mode vectors so as to minimise the cost function
00038   double optimise_mode_pair(vnl_vector<double>& proj1,
00039                                 vnl_vector<double>& proj2,
00040                                 vnl_vector<double>& mode1,
00041                                 vnl_vector<double>& mode2);
00042 
00043   //: Optimise the mode vectors so as to minimise the cost function
00044   double optimise_one_pass(vcl_vector<vnl_vector<double> >& proj,
00045                                 vnl_matrix<double>& modes);
00046 
00047   //: Optimise the mode vectors so as to minimise the cost function
00048   void optimise_about_mean(mbl_data_wrapper<vnl_vector<double> >& data,
00049                                 const vnl_vector<double>& mean,
00050                                 vnl_matrix<double>& modes,
00051                                 vnl_vector<double>& mode_var);
00052 
00053   //: Compute projections onto each mode
00054   //  proj[j][i] is the projection of the i-th data sample onto the j-th mode
00055   void compute_projections(mbl_data_wrapper<vnl_vector<double> >& data,
00056                            const vnl_vector<double>& mean,
00057                            vnl_matrix<double>& modes,
00058                            vcl_vector<vnl_vector<double> >& proj);
00059 
00060   //: Set parameters to default values
00061   void set_defaults();
00062  public:
00063 
00064     //: Dflt ctor
00065   mcal_general_ca();
00066 
00067     //: Destructor
00068   virtual ~mcal_general_ca();
00069 
00070    //: Initialise, taking clones of supplied objects
00071   void set(const mcal_component_analyzer& initial_ca,
00072            const mcal_single_basis_cost& basis_cost);
00073 
00074   //: Object to perform initial component analysis (usually PCA)
00075   mcal_component_analyzer& initial_ca() { return initial_ca_; }
00076 
00077   //: Object to evaluate component of cost associated with each basis vector
00078   mcal_single_basis_cost& basis_cost() { return basis_cost_; }
00079 
00080   //: Compute modes of the supplied data relative to the supplied mean
00081   //  Model is x = mean + modes*b,  where b is a vector of weights on each mode.
00082   //  mode_var[i] gives the variance of the data projected onto that mode.
00083   virtual void build_about_mean(mbl_data_wrapper<vnl_vector<double> >& data,
00084                                 const vnl_vector<double>& mean,
00085                                 vnl_matrix<double>& modes,
00086                                 vnl_vector<double>& mode_var);
00087 
00088     //: Version number for I/O
00089   short version_no() const;
00090 
00091     //: Name of the class
00092   virtual vcl_string is_a() const;
00093 
00094     //: Create a copy on the heap and return base class pointer
00095   virtual  mcal_component_analyzer*  clone()  const;
00096 
00097     //: Print class to os
00098   virtual void print_summary(vcl_ostream& os) const;
00099 
00100     //: Save class to binary file stream
00101   virtual void b_write(vsl_b_ostream& bfs) const;
00102 
00103     //: Load class from binary file stream
00104   virtual void b_read(vsl_b_istream& bfs);
00105 
00106   //: Read initialisation settings from a stream.
00107   // Parameters:
00108   // \verbatim
00109   // {
00110   //   initial_ca: mcal_pca { ... }
00111   //   basis_cost: mcal_sparse_basis_cost { alpha: 0.1 }
00112   //   max_passes: 50
00113   //   move_thresh: 0.0001
00114   // }
00115   // \endverbatim
00116   // \throw mbl_exception_parse_error if the parse fails.
00117   virtual void config_from_stream(vcl_istream & is);
00118 };
00119 
00120 #endif // mcal_general_ca_h_