source: trunk/test/ncc_test.cc @ 1075

Last change on this file since 1075 was 1075, checked in by Markus Ringnér, 14 years ago

Fixed bug in test

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.4 KB
Line 
1// $Id: ncc_test.cc 1075 2008-02-12 12:55:31Z markus $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér
5
6  This file is part of the yat library, http://trac.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "yat/classifier/IGP.h"
25#include "yat/classifier/Kernel_MEV.h"
26#include "yat/classifier/KernelLookup.h"
27#include "yat/classifier/MatrixLookup.h"
28#include "yat/classifier/MatrixLookupWeighted.h"
29#include "yat/classifier/NCC.h"
30#include "yat/classifier/PolynomialKernelFunction.h"
31#include "yat/classifier/Target.h"
32#include "yat/utility/matrix.h"
33#include "yat/statistics/EuclideanDistance.h"
34#include "yat/statistics/PearsonDistance.h"
35#include "yat/utility/utility.h"
36
37#include <cassert>
38#include <fstream>
39#include <iostream>
40#include <stdexcept>
41#include <sstream>
42#include <string>
43#include <limits>
44#include <cmath>
45
46using namespace theplu::yat;
47
48double deviation(const utility::matrix& a, const utility::matrix& b) {
49  double sl=0;
50  for (size_t i=0; i<a.rows(); i++){
51    for (size_t j=0; j<a.columns(); j++){
52      sl += fabs(a(i,j)-b(i,j));
53    }
54  }
55  sl /= (a.columns()*a.rows());
56  return sl;
57}
58
59int main(const int argc,const char* argv[])
60{ 
61
62  std::ostream* error;
63  if (argc>1 && argv[1]==std::string("-v"))
64    error = &std::cerr;
65  else {
66    error = new std::ofstream("/dev/null");
67    if (argc>1)
68      std::cout << "ncc_test -v : for printing extra information\n";
69  }
70  *error << "testing ncc" << std::endl;
71  bool ok = true;
72
73  /////////////////////////////////////////////
74  // First test of constructor and training 
75  /////////////////////////////////////////////
76  classifier::MatrixLookup ml(4,4);
77  std::vector<std::string> vec(4, "pos");
78  vec[3]="bjds";
79  classifier::Target target(vec);
80  classifier::NCC<statistics::EuclideanDistance> ncctmp(ml,target);
81  *error << "training...\n";
82  ncctmp.train();
83  *error << "done\n";
84
85  /////////////////////////////////////////////
86  // A test of predictions using unweighted data
87  /////////////////////////////////////////////
88  *error << "test of predictions using unweighted test data\n";
89  utility::matrix data1(3,4);
90  for(size_t i=0;i<3;i++) {
91    data1(i,0)=3-i;
92    data1(i,1)=5-i;
93    data1(i,2)=i+1;
94    data1(i,3)=i+3;
95  }
96  std::vector<std::string> vec1(4, "pos");
97  vec1[0]="neg";
98  vec1[1]="neg";
99
100  classifier::MatrixLookup ml1(data1);
101  classifier::Target target1(vec1);
102
103  classifier::NCC<statistics::EuclideanDistance> ncc1(ml1,target1);
104  ncc1.train();
105  utility::matrix prediction1;
106  ncc1.predict(ml1,prediction1);
107  double slack_bound=2e-7;
108  utility::matrix result1(2,4);
109  result1(0,0)=result1(0,1)=result1(1,2)=result1(1,3)=sqrt(3.0);
110  result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=sqrt(11.0);
111  double slack = deviation(prediction1,result1); 
112  if (slack > slack_bound || std::isnan(slack)){
113    *error << "Difference to expected prediction too large\n";
114    *error << "slack: " << slack << std::endl;
115    *error << "expected less than " << slack_bound << std::endl;
116    ok = false;
117  }
118
119  //////////////////////////////////////////////////////////////////////////
120  // A test of predictions using unweighted training and weighted test data
121  //////////////////////////////////////////////////////////////////////////
122  *error << "test of predictions using unweighted training and weighted test data\n";
123  utility::matrix weights1(3,4,1.0);
124  weights1(0,0)=weights1(1,1)=weights1(2,2)=weights1(1,3)=0.0;
125  classifier::MatrixLookupWeighted mlw1(data1,weights1);
126  ncc1.predict(mlw1,prediction1); 
127  result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=sqrt(15.0);
128  slack = deviation(prediction1,result1);
129  if (slack > slack_bound || std::isnan(slack)){
130    *error << "Difference to expected prediction too large\n";
131    *error << "slack: " << slack << std::endl;
132    *error << "expected less than " << slack_bound << std::endl;
133    ok = false;
134  }
135
136 
137
138  //////////////////////////////////////////////////////////////////////////
139  // A test of predictions using Sorlie data
140  //////////////////////////////////////////////////////////////////////////
141  *error << "test with Sorlie data\n";
142  std::ifstream is("data/sorlie_centroid_data.txt");
143  utility::matrix data(is,'\t');
144  is.close();
145
146  is.open("data/sorlie_centroid_classes.txt");
147  classifier::Target targets(is);
148  is.close();
149
150  // Generate weight matrix with 0 for missing values and 1 for others.
151  utility::matrix weights(data.rows(),data.columns(),0.0);
152  utility::nan(data,weights);
153     
154  classifier::MatrixLookupWeighted dataviewweighted(data,weights);
155  classifier::NCC<statistics::PearsonDistance> ncc(dataviewweighted,targets);
156  *error << "training...\n";
157  ncc.train();
158
159  // Comparing the centroids to stored result
160  is.open("data/sorlie_centroids.txt");
161  utility::matrix centroids(is);
162  is.close();
163
164  if(centroids.rows() != ncc.centroids().rows() ||
165     centroids.columns() != ncc.centroids().columns()) {
166    *error << "Error in the dimensionality of centroids\n";
167    *error << "Nof rows: " << centroids.rows() << " expected: " 
168           << ncc.centroids().rows() << std::endl;
169    *error << "Nof columns: " << centroids.columns() << " expected: " 
170           << ncc.centroids().columns() << std::endl;
171  }
172
173  slack = deviation(centroids,ncc.centroids());
174  if (slack > slack_bound || std::isnan(slack)){
175    *error << "Difference to stored centroids too large\n";
176    *error << "slack: " << slack << std::endl;
177    *error << "expected less than " << slack_bound << std::endl;
178    ok = false;
179  }
180
181  *error << "...predicting...\n";
182  utility::matrix prediction;
183  ncc.predict(dataviewweighted,prediction);
184 
185  // Comparing the prediction to stored result
186  is.open("data/sorlie_centroid_predictions.txt");
187  utility::matrix result(is,'\t');
188  is.close();
189
190  slack = deviation(result,prediction);
191  if (slack > slack_bound || std::isnan(slack)){
192    *error << "Difference to stored prediction too large\n";
193    *error << "slack: " << slack << std::endl;
194    *error << "expected less than " << slack_bound << std::endl;
195    ok = false;
196  }
197  *error << "done\n";
198
199  //////////////////////////////////////////////////////////////////////////
200  // Testing rejection of KernelLookups
201  //////////////////////////////////////////////////////////////////////////
202  classifier::PolynomialKernelFunction kf;
203  classifier::Kernel_MEV kernel(ml,kf);
204  classifier::DataLookup2D* dl_kernel = new classifier::KernelLookup(kernel); 
205  bool catch_error=false;
206  try {
207    catch_error=false; // should catch error here
208    *error << ncc.make_classifier(*dl_kernel,target) << std::endl;
209  }
210  catch (std::runtime_error) {   
211    *error << "caught expected bad cast runtime_error" << std::endl;
212    catch_error=true;
213  }
214  if(!catch_error) {
215    ok=false;
216  }
217  try {
218    catch_error=false; // should catch error here
219    ncc.predict(*dl_kernel,prediction); 
220  }
221  catch (std::runtime_error) {   
222    *error << "caught expected bad cast runtime_error" << std::endl;
223    catch_error=true;
224  }
225  if(!catch_error) {
226    ok=false;
227  }
228  delete dl_kernel;
229 
230  if(ok)
231    *error << "OK" << std::endl;
232  else
233    *error << "FAILED" << std::endl;
234
235  if (error!=&std::cerr)
236    delete error;
237
238  if(ok) 
239    return 0;
240  return -1; 
241}
Note: See TracBrowser for help on using the repository browser.