contrib/mul/vpdfl/vpdfl_pc_gaussian_builder.h
Go to the documentation of this file.
00001 // This is mul/vpdfl/vpdfl_pc_gaussian_builder.h
00002 #ifndef vpdfl_pc_gaussian_builder_h
00003 #define vpdfl_pc_gaussian_builder_h
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Interface for Multi-variate Principle Component Gaussian PDF Builder.
00010 // \author Ian Scott
00011 // \date 21-Jul-2000
00012 //
00013 // \verbatim
00014 //  Modifications
00015 //   23 April 2001 IMS - Ported to VXL
00016 // \endverbatim
00017 //=======================================================================
00018 
00019 #include <vpdfl/vpdfl_gaussian_builder.h>
00020 #include <vcl_iosfwd.h>
00021 
00022 //=======================================================================
00023 
00024 class vpdfl_gaussian;
00025 class vpdfl_pdf_base;
00026 class vpdfl_pc_gaussian;
00027 
00028 //: Class to build vpdfl_pc_gaussian objects with a fixed number of principle components
00029 class vpdfl_pc_gaussian_builder : public vpdfl_gaussian_builder
00030 {
00031  public:
00032   enum partitionMethods { fixed, proportionate };
00033  private:
00034 
00035   vpdfl_pc_gaussian& gaussian(vpdfl_pdf_base& model) const;
00036 
00037   //: The method used to decide how to calculate the number of principle components.
00038   // defaults to fixed.
00039   partitionMethods partitionMethod_;
00040 
00041   //: The proportion of variance that should be encoded with the principle components..
00042   // Isn't used by default..
00043   double proportionOfVariance_;
00044 
00045   //: The number of components to represent in the principle space.
00046   unsigned fixed_partition_;
00047 
00048  public:
00049   //: Dflt ctor
00050   vpdfl_pc_gaussian_builder();
00051 
00052   //: Destructor
00053   virtual ~vpdfl_pc_gaussian_builder();
00054 
00055   //: Create empty model
00056   virtual vpdfl_pdf_base* new_model() const;
00057 
00058   //: Build default model with given mean
00059   virtual void build(vpdfl_pdf_base& model,
00060                      const vnl_vector<double>& mean) const;
00061 
00062   //: Build model from data
00063   virtual void build(vpdfl_pdf_base& model,
00064                      mbl_data_wrapper<vnl_vector<double> >& data) const;
00065 
00066   //: Build model from weighted data
00067   virtual void weighted_build(vpdfl_pdf_base& model,
00068                               mbl_data_wrapper<vnl_vector<double> >& data,
00069                               const vcl_vector<double>& wts) const;
00070 
00071   //: Computes mean and covariance of given data
00072   void mean_covar(vnl_vector<double>& mean, vnl_matrix<double>& covar,
00073                   mbl_data_wrapper<vnl_vector<double> >& data) const;
00074 
00075   //: Decide where to partition an Eigenvector space
00076   // Returns the number of principle components to be used.
00077   // Pass in the eigenvalues (eVals), the number of samples
00078   // that went to make up this Gaussian (nSamples), and the noise floor
00079   // for the dataset. The method may use simplified algorithms if
00080   // you indicate that the number of samples or noise floor is unknown
00081   // (by setting the latter parameters to 0.)
00082   virtual unsigned decide_partition(const vnl_vector<double>& eVals,
00083                                     unsigned nSamples=0, double noise=0.0) const;
00084 
00085   //: Return the number of principle components when using fixed partition.
00086   int fixed_partition() const
00087   {
00088     if (partitionMethod_ == vpdfl_pc_gaussian_builder::fixed) return fixed_partition_;
00089     else return -1;
00090   }
00091 
00092   //: Set the number of principle components when using fixed partition.
00093   void set_fixed_partition(int n_principle_components);
00094 
00095   //: Use proportion of variance to decide on the number of principle components.
00096   // Specify the proportion (between 0 and 1).
00097   // The default setting uses a fixed number of principle components.
00098   void set_proportion_partition( double proportion);
00099 
00100   //: Find the proportion of variance to decide on the number of principle components.
00101   // returns a negative value if not using proportion of variance method.
00102   double proportion_partition() const
00103   {
00104     if (partitionMethod_ == proportionate) return proportionOfVariance_;
00105     else return -1.0;
00106   }
00107 
00108   //: How is the partition between principle and complementary spaces
00109   partitionMethods partition_method() const
00110   {return partitionMethod_;}
00111 
00112   //: Read initialisation settings from a stream.
00113   // Parameters:
00114   // \verbatim
00115   // {
00116   //   mode_choice: fixed  // Alternative: proportionate
00117   //   var_prop: 0.95
00118   //   n_modes: 3
00119   //   min_var: 1.0e-6
00120   // }
00121   // \endverbatim
00122   // \throw mbl_exception_parse_error if the parse fails.
00123   virtual void config_from_stream(vcl_istream & is);
00124 
00125 
00126   //: Version number for I/O
00127   short version_no() const;
00128 
00129   //: Name of the class
00130   virtual vcl_string is_a() const;
00131 
00132   //: Does the name of the class match the argument?
00133   virtual bool is_class(vcl_string const& s) const;
00134 
00135   //: Create a copy on the heap and return base class pointer
00136   virtual vpdfl_builder_base* clone() const;
00137 
00138   //: Print class to os
00139   virtual void print_summary(vcl_ostream& os) const;
00140 
00141   //: Save class to binary file stream
00142   virtual void b_write(vsl_b_ostream& bfs) const;
00143 
00144   //: Load class from binary file stream
00145   virtual void b_read(vsl_b_istream& bfs);
00146 };
00147 
00148 #endif // vpdfl_pc_gaussian_builder_h