Changeset 1238


Ignore:
Timestamp:
Mar 15, 2008, 7:03:05 PM (13 years ago)
Author:
Peter
Message:

working on #223

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/kernel_test.cc

    r1210 r1238  
    2323  02111-1307, USA.
    2424*/
     25
     26#include "Suite.h"
    2527
    2628#include "yat/utility/Matrix.h"
     
    4547              const classifier::KernelFunction* kf,
    4648              const utility::Matrix& control, const double error_bound,
    47               std::ostream* error);
     49              test::Suite& suite);
    4850
    4951bool test_SEV(const classifier::MatrixLookup& data,
    5052              const classifier::KernelFunction* kf,
    5153              const utility::Matrix& control, const double error_bound,
    52               std::ostream* error);
    53 
    54 
    55 int main(const int argc,const char* argv[])
     54              test::Suite& suite);
     55
     56
     57int main(int argc, char* argv[])
    5658
    5759
    58   std::ostream* error;
    59   if (argc>1 && argv[1]==std::string("-v"))
    60     error = &std::cerr;
    61   else {
    62     error = new std::ofstream("/dev/null");
    63     if (argc>1)
    64       std::cout << "kernel_test -v : for printing extra information\n";
    65   }
    66   *error << "testing kernels" << std::endl;
    67   bool ok = true;
     60  test::Suite suite(argc, argv);
     61  suite.err() << "testing kernels" << std::endl;
    6862
    6963  utility::Matrix data2_core(2,3);
     
    8175     kernel2(1,0) || kernel2(1,1)!=1 || kernel2(1,2) ||
    8276     kernel2(2,0) || kernel2(2,1) || kernel2(2,2)!=1){
    83     ok = false;
    84     *error << "Found:\n";
     77    suite.add(false);
     78    suite.err() << "Found:\n";
    8579    for (size_t i=0; i<3; i++){
    8680      for (size_t j=0; j<3; j++)
    87         *error << kernel2(i,j) << " ";
    88       *error << std::endl;
     81        suite.err() << kernel2(i,j) << " ";
     82      suite.err() << std::endl;
    8983    }
    90     *error << "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl;
     84    suite.err() << "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl;
    9185  }
    9286  classifier::KernelLookup kv2(kernel2);
    9387  delete kf2;
    9488
    95   // Peter, the hardcoded number below should be changed.
    96   double error_bound = 1e-8;
     89  // Peter, the hardcoded number below reflects precision in files
     90  // containing kernel matrices calculated in matlab.
     91  double error_bound = 1e-6;
    9792  std::ifstream is("data/nm_data_centralized.txt");
    9893  utility::Matrix data_core(is);
     
    105100  is.close();
    106101  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction();
    107   ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound, error)
    108         & test_SEV(data,kf,kernel_matlab,error_bound, error));
     102  suite.add(test_MEV(data,kf,kernel_matlab,error_bound, suite) &&
     103            test_SEV(data,kf,kernel_matlab,error_bound, suite));
    109104  delete kf;
    110105 
     
    113108  is.close();
    114109  kf = new classifier::PolynomialKernelFunction(2);
    115   ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound, error)
    116         && test_SEV(data,kf,kernel_matlab2,error_bound, error));
     110  suite.add(test_MEV(data,kf,kernel_matlab2,error_bound, suite) &&
     111            test_SEV(data,kf,kernel_matlab2,error_bound, suite));
    117112  delete kf;
    118113
    119114  // Checking that GaussianKernelFunction.
    120   *error << "Checking GaussianKernelFunction.\n";
     115  suite.err() << "Checking GaussianKernelFunction.\n";
    121116  is.open("data/nm_kernel_gaussian.txt");
    122117  utility::Matrix kernel_gaussian(is);
    123118  is.close();
    124119  kf = new classifier::GaussianKernelFunction(100);
    125   ok = ok && test_MEV(data,kf,kernel_gaussian,error_bound, error);
    126   ok = ok && test_SEV(data,kf,kernel_gaussian,error_bound, error);
     120  suite.add(test_MEV(data,kf,kernel_gaussian,error_bound, suite));
     121  suite.add(test_SEV(data,kf,kernel_gaussian,error_bound, suite));
    127122  delete kf;
    128123
     
    134129  classifier::Kernel_SEV kernel(data,pkf);
    135130 
    136 
    137   if (error!=&std::cerr)
    138     delete error;
    139 
    140   if (ok=true) 
    141     return 0;
    142   return -1;
     131  return suite.return_value();
    143132}
    144133
     
    146135              const classifier::KernelFunction* kf,
    147136              const utility::Matrix& control, const double error_bound,
    148               std::ostream* error)
     137              test::Suite& suite)
    149138{
    150   *error << "testing Kernel_MEV" << std::endl;
     139  suite.err() << "testing Kernel_MEV" << std::endl;
    151140  classifier::Kernel_MEV kernel(data,*kf);
    152141  for(u_int i=0;i<control.rows();i++)
    153142    for(u_int j=0;j<control.columns();j++)
    154       if (std::abs(kernel(i,j)-control(i,j))>error_bound)
     143      if (std::abs(kernel(i,j)-control(i,j))>error_bound){
     144        suite.err() << "ERROR: comparing kernel(" << i << "," << j << "): "
     145                    << kernel(i,j)
     146                    << "\n and control(" << i << "," << j << "): "
     147                    << control(i,j) << "\n"
     148                    << "difference: " << kernel(i,j)-control(i,j) << "\n"
     149                    << "expected less than " << error_bound << "\n";
    155150        return false;
    156 
     151      }
    157152  // checking view
    158153  std::vector<size_t> index_vec(3);
     
    164159  classifier::KernelLookup kv(kv_raw,index,index);
    165160  if (kv.rows()!=index.size()){
    166     *error << "Error: KernelLookup(kernel, index)\n" << std::endl
    167            << "Size of KernelLookup is " << kv.rows() << std::endl
    168            << "expected " << index.size() << std::endl;
     161    suite.err() << "Error: KernelLookup(kernel, index)\n"
     162                << "Size of KernelLookup is " << kv.rows() << "\n"
     163                << "expected " << index.size() << std::endl;
    169164   
    170165    return false;
     
    172167  classifier::KernelLookup kv2(kernel);
    173168  if (kv2.rows()!=kernel.size()){
    174     *error << "Error: KernelLookup(kernel)\n" << std::endl
     169    suite.err() << "Error: KernelLookup(kernel)\n" << std::endl
    175170           << "Size of KernelLookup is " << kv.rows() << std::endl
    176171           << "expected " << kernel.size() << std::endl;
     
    178173    return false;
    179174  }
    180 
    181175  return true;
    182176}
     
    185179              const classifier::KernelFunction* kf,
    186180              const utility::Matrix& control, const double error_bound,
    187               std::ostream* error)
     181              test::Suite& suite)
    188182{
    189   *error << "testing Kernel_SEV" << std::endl;
     183  suite.err() << "testing Kernel_SEV" << std::endl;
    190184  classifier::Kernel_SEV kernel(data,*kf);
    191185  for(u_int i=0;i<control.rows();i++)
    192186    for(u_int j=0;j<control.columns();j++)
    193       if (std::abs(kernel(i,j)-control(i,j))>error_bound)
     187      if (std::abs(kernel(i,j)-control(i,j))>error_bound){
     188        suite.err() << "ERROR: comparing kernel(" << i << "," << j << "): "
     189                    << kernel(i,j)
     190                    << "\n and control(" << i << "," << j << "): "
     191                    << control(i,j) << "\n"
     192                    << "difference: " << kernel(i,j)-control(i,j) << "\n"
     193                    << "expected less than " << error_bound << "\n";
    194194        return false;
     195      }
    195196
    196197  // checking view
     
    203204  classifier::KernelLookup kv(kv_raw,index, index);
    204205  if (kv.rows()!=index.size()){
    205     *error << "Error: KernelLookup(kernel, index)\n" << std::endl
     206    suite.err() << "Error: KernelLookup(kernel, index)\n" << std::endl
    206207           << "Size of KernelLookup is " << kv.rows() << std::endl
    207208           << "expected " << index.size() << std::endl;
     
    211212  classifier::KernelLookup kv2(kernel);
    212213  if (kv2.rows()!=kernel.size()){
    213     *error << "Error: KernelLookup(kernel)\n" << std::endl
     214    suite.err() << "Error: KernelLookup(kernel)\n" << std::endl
    214215           << "Size of KernelLookup is " << kv.rows() << std::endl
    215216           << "expected " << kernel.size() << std::endl;
Note: See TracChangeset for help on using the changeset viewer.