contrib/mul/mcal/mcal_component_analyzer.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 #include "mcal_component_analyzer.h"
00004 #include <vcl_cstdlib.h>
00005 #include <mbl/mbl_data_array_wrapper.h>
00006 #include <mbl/mbl_exception.h>
00007 #include <mbl/mbl_cloneables_factory.h>
00008 #include <mbl/mbl_parse_block.h>
00009 #include <vsl/vsl_indent.h>
00010 #include <vsl/vsl_binary_loader.h>
00011 
00012 //=======================================================================
00013 
00014 mcal_component_analyzer::mcal_component_analyzer()
00015 {
00016 }
00017 
00018 //=======================================================================
00019 
00020 mcal_component_analyzer::~mcal_component_analyzer()
00021 {
00022 }
00023 
00024 //: Compute the mean of the supplied data
00025 void mcal_component_analyzer::compute_mean(mbl_data_wrapper<vnl_vector<double> >& data,
00026                      vnl_vector<double>& mean)
00027 {
00028   if (data.size()==0)
00029   {
00030     mean.set_size(0);
00031     return;
00032   }
00033   data.reset();
00034   mean = data.current();
00035   while (data.next()) mean += data.current();
00036   mean/=data.size();
00037 }
00038 
00039 //: Compute mean and modes from the supplied data
00040 //  Computes the mean, then calls build_about_mean
00041 void mcal_component_analyzer::build(mbl_data_wrapper<vnl_vector<double> >& data,
00042                      vnl_vector<double>& mean,
00043                      vnl_matrix<double>& modes,
00044                      vnl_vector<double>& mode_var)
00045 {
00046   compute_mean(data,mean);
00047   build_about_mean(data,mean,modes,mode_var);
00048 }
00049 
00050 
00051 void mcal_component_analyzer::build_from_array(const vnl_vector<double>* data, int n,
00052                                 vnl_vector<double>& mean,
00053                                 vnl_matrix<double>& modes,
00054                                 vnl_vector<double>& mode_var)
00055 {
00056   mbl_data_array_wrapper<vnl_vector<double> > data_array(data,n);
00057   build(data_array,mean,modes,mode_var);
00058 }
00059 
00060 //=======================================================================
00061 
00062 short mcal_component_analyzer::version_no() const
00063 {
00064   return 1;
00065 }
00066 
00067 //=======================================================================
00068 
00069 void vsl_add_to_binary_loader(const mcal_component_analyzer& b)
00070 {
00071   vsl_binary_loader<mcal_component_analyzer>::instance().add(b);
00072 }
00073 
00074 //=======================================================================
00075 
00076 vcl_string  mcal_component_analyzer::is_a() const
00077 {
00078   return vcl_string("mcal_component_analyzer");
00079 }
00080 
00081 
00082 //: Create a concrete mcal_component_analyzer object, from a text specification.
00083 vcl_auto_ptr<mcal_component_analyzer>
00084   mcal_component_analyzer::create_from_stream(vcl_istream &is)
00085 {
00086   vcl_string name;
00087   is >> name;
00088 
00089   vcl_auto_ptr<mcal_component_analyzer> mca;
00090   try
00091   {
00092     mca = mbl_cloneables_factory<mcal_component_analyzer>::get_clone(name);
00093   }
00094   catch (const mbl_exception_no_name_in_factory & e)
00095   {
00096       vcl_cerr<<"ERROR in mcal_component_analyzer::new_vm_builder_from_stream\n"
00097               <<"\tRequired vector model builder of "<<name<<" is not in the factory. Further exception details follow:\n"
00098               <<'\t'<<e.what()<<vcl_endl;
00099       vcl_abort();
00100   }
00101   mca->config_from_stream(is);
00102   return mca;
00103 }
00104 
00105 //: Read initialisation settings from a stream.
00106 // The default implementation merely checks that no properties have
00107 // been specified.
00108 void mcal_component_analyzer::config_from_stream(vcl_istream& is)
00109 {
00110   vcl_string s = mbl_parse_block(is);
00111   if (s.empty() || s=="{}") return;
00112 
00113   throw mbl_exception_parse_error(
00114     this->is_a() + " expects no properties in initialisation,\n"
00115     "But the following properties were given:\n" + s);
00116 }
00117 
00118 
00119 //=======================================================================
00120 // Associated function: operator<<
00121 //=======================================================================
00122 
00123 void vsl_b_write(vsl_b_ostream& bfs, const mcal_component_analyzer& b)
00124 {
00125     b.b_write(bfs);
00126 }
00127 
00128 //=======================================================================
00129 // Associated function: operator>>
00130 //=======================================================================
00131 
00132 void vsl_b_read(vsl_b_istream& bfs, mcal_component_analyzer& b)
00133 {
00134     b.b_read(bfs);
00135 }
00136 
00137 //=======================================================================
00138 // Associated function: operator<<
00139 //=======================================================================
00140 
00141 vcl_ostream& operator<<(vcl_ostream& os,const mcal_component_analyzer& b)
00142 {
00143   os << b.is_a() << ": ";
00144   vsl_indent_inc(os);
00145   b.print_summary(os);
00146   vsl_indent_dec(os);
00147   return os;
00148 }
00149 
00150 //=======================================================================
00151 // Associated function: operator<<
00152 //=======================================================================
00153 
00154 vcl_ostream& operator<<(vcl_ostream& os,const mcal_component_analyzer* b)
00155 {
00156     if (b)
00157     return os << *b;
00158     else
00159     return os << "No mcal_component_analyzer defined.";
00160 }