source: trunk/test/kernel_test.cc @ 463

Last change on this file since 463 was 463, checked in by Peter, 17 years ago

fixed bug in KernelView? constructor and updated tests

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1// $Id: kernel_test.cc 463 2005-12-16 17:59:15Z peter $
2
3
4// C++ tools include
5////////////////////
6#include <c++_tools/gslapi/matrix.h>
7#include <c++_tools/gslapi/vector.h>
8#include <c++_tools/classifier/KernelFunction.h>
9#include <c++_tools/classifier/PolynomialKernelFunction.h>
10#include <c++_tools/classifier/GaussianKernelFunction.h>
11#include <c++_tools/classifier/KernelView.h>
12#include <c++_tools/classifier/Kernel_MEV.h>
13#include <c++_tools/classifier/Kernel_SEV.h>
14
15#include <cmath>
16#include <cstdlib>
17#include <fstream>
18#include <iostream>
19#include <vector>
20
21using namespace theplu;
22
23bool test_MEV(const gslapi::matrix& data, const classifier::KernelFunction* kf, 
24              const gslapi::matrix& control, const double error_bound,
25              std::ostream* error)
26{
27  classifier::Kernel_MEV kernel(data,*kf);
28  for(u_int i=0;i<control.rows();i++)
29    for(u_int j=0;j<control.columns();j++)
30      if (fabs(kernel(i,j)-control(i,j))>error_bound)
31        return false;
32
33  // checking view
34  std::vector<size_t> index(3);
35  index[0]=1;
36  index[1]=2;
37  index[2]=3;
38  classifier::KernelView kv(kernel,index);
39  if (kv.rows()!=index.size()){
40    *error << "Error: KernelView(kernel, index)\n" << std::endl
41           << "Size of KernelView is " << kv.rows() << std::endl
42           << "expected " << index.size() << std::endl;
43   
44    return false;
45  }
46  classifier::KernelView kv2(kernel);
47  if (kv2.rows()!=kernel.size()){
48    *error << "Error: KernelView(kernel)\n" << std::endl
49           << "Size of KernelView is " << kv.rows() << std::endl
50           << "expected " << kernel.size() << std::endl;
51   
52    return false;
53  }
54
55  return true;
56}
57
58bool test_SEV(const gslapi::matrix& data, const classifier::KernelFunction* kf, 
59              const gslapi::matrix& control, const double error_bound,
60              std::ostream* error)
61{
62  classifier::Kernel_SEV kernel(data,*kf);
63  for(u_int i=0;i<control.rows();i++)
64    for(u_int j=0;j<control.columns();j++)
65      if (fabs(kernel(i,j)-control(i,j))>error_bound)
66        return false;
67
68  // checking view
69  std::vector<size_t> index(3);
70  index[0]=1;
71  index[1]=2;
72  index[2]=3;
73  classifier::KernelView kv(kernel,index);
74  if (kv.rows()!=index.size()){
75    *error << "Error: KernelView(kernel, index)\n" << std::endl
76           << "Size of KernelView is " << kv.rows() << std::endl
77           << "expected " << index.size() << std::endl;
78   
79    return false;
80  }
81  classifier::KernelView kv2(kernel);
82  if (kv2.rows()!=kernel.size()){
83    *error << "Error: KernelView(kernel)\n" << std::endl
84           << "Size of KernelView is " << kv.rows() << std::endl
85           << "expected " << kernel.size() << std::endl;
86   
87    return false;
88  }
89  return true;
90}
91
92
93int main(const int argc,const char* argv[])
94
95{ 
96  std::ostream* error;
97  if (argc>1 && argv[1]==std::string("-v"))
98    error = &std::cerr;
99  else {
100    error = new std::ofstream("/dev/null");
101    if (argc>1)
102      std::cout << "kernel_test -v : for printing extra information\n";
103  }
104  *error << "testing kernels" << std::endl;
105  bool ok = true;
106
107  // Peter, the hardcoded number below should be changed.
108  double error_bound = 1e-8; 
109  std::ifstream is("data/nm_data_centralized.txt");
110  gslapi::matrix transposed_data(is);
111  is.close();
112  // Because how the kernel is treated is changed, data must be transposed.
113  gslapi::matrix data=transposed_data;
114
115  is.open("data/nm_kernel.txt");
116  gslapi::matrix kernel_matlab(is);
117  is.close();
118  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); 
119  ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound, error)
120        & test_SEV(data,kf,kernel_matlab,error_bound, error));
121  delete kf;
122 
123  is.open("data/nm_kernel2.txt");
124  gslapi::matrix kernel_matlab2(is);
125  is.close();
126  kf = new classifier::PolynomialKernelFunction(2); 
127  ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound, error)
128        & test_SEV(data,kf,kernel_matlab2,error_bound, error));
129  delete kf;
130
131  // Checking that a GaussianKernelFunction object can be built at
132  // compile time.
133  kf = new classifier::GaussianKernelFunction(); 
134  delete kf;
135
136  if (error!=&std::cerr)
137    delete error;
138
139  if (ok=true) 
140    return 0;
141  return -1;
142}
Note: See TracBrowser for help on using the repository browser.