source: trunk/test/ncc_test.cc @ 1076

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

NCC done for ticket:259

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.4 KB
Line 
1// $Id: ncc_test.cc 1076 2008-02-12 13:51:11Z 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  // A test of predictions using weighted training resulting in NaN's
138  // in centroids and unweighted test data
139  //////////////////////////////////////////////////////////////////////////
140  *error << "test of predictions using nan centroids and unweighted test data\n";
141  utility::matrix weights2(3,4,1.0);
142  weights2(1,0)=weights2(1,1)=0.0;
143  classifier::MatrixLookupWeighted mlw2(data1,weights2);
144  classifier::NCC<statistics::EuclideanDistance> ncc2(mlw2,target1);
145  ncc2.train();
146  ncc2.predict(ml1,prediction1); 
147  result1(0,0)=result1(0,1)=result1(1,2)=result1(1,3)=sqrt(3.0);
148  result1(1,0)=result1(1,1)=sqrt(11.0);
149  result1(0,2)=result1(0,3)=sqrt(15.0);
150  slack = deviation(prediction1,result1);
151  if (slack > slack_bound || std::isnan(slack)){
152    *error << "Difference to expected prediction too large\n";
153    *error << "slack: " << slack << std::endl;
154    *error << "expected less than " << slack_bound << std::endl;
155    ok = false;
156  }
157
158  //////////////////////////////////////////////////////////////////////////
159  // A test of predictions using Sorlie data
160  //////////////////////////////////////////////////////////////////////////
161  *error << "test with Sorlie data\n";
162  std::ifstream is("data/sorlie_centroid_data.txt");
163  utility::matrix data(is,'\t');
164  is.close();
165
166  is.open("data/sorlie_centroid_classes.txt");
167  classifier::Target targets(is);
168  is.close();
169
170  // Generate weight matrix with 0 for missing values and 1 for others.
171  utility::matrix weights(data.rows(),data.columns(),0.0);
172  utility::nan(data,weights);
173     
174  classifier::MatrixLookupWeighted dataviewweighted(data,weights);
175  classifier::NCC<statistics::PearsonDistance> ncc(dataviewweighted,targets);
176  *error << "training...\n";
177  ncc.train();
178
179  // Comparing the centroids to stored result
180  is.open("data/sorlie_centroids.txt");
181  utility::matrix centroids(is);
182  is.close();
183
184  if(centroids.rows() != ncc.centroids().rows() ||
185     centroids.columns() != ncc.centroids().columns()) {
186    *error << "Error in the dimensionality of centroids\n";
187    *error << "Nof rows: " << centroids.rows() << " expected: " 
188           << ncc.centroids().rows() << std::endl;
189    *error << "Nof columns: " << centroids.columns() << " expected: " 
190           << ncc.centroids().columns() << std::endl;
191  }
192
193  slack = deviation(centroids,ncc.centroids());
194  if (slack > slack_bound || std::isnan(slack)){
195    *error << "Difference to stored centroids too large\n";
196    *error << "slack: " << slack << std::endl;
197    *error << "expected less than " << slack_bound << std::endl;
198    ok = false;
199  }
200
201  *error << "...predicting...\n";
202  utility::matrix prediction;
203  ncc.predict(dataviewweighted,prediction);
204 
205  // Comparing the prediction to stored result
206  is.open("data/sorlie_centroid_predictions.txt");
207  utility::matrix result(is,'\t');
208  is.close();
209
210  slack = deviation(result,prediction);
211  if (slack > slack_bound || std::isnan(slack)){
212    *error << "Difference to stored prediction too large\n";
213    *error << "slack: " << slack << std::endl;
214    *error << "expected less than " << slack_bound << std::endl;
215    ok = false;
216  }
217  *error << "done\n";
218
219  //////////////////////////////////////////////////////////////////////////
220  // Testing rejection of KernelLookups
221  //////////////////////////////////////////////////////////////////////////
222  classifier::PolynomialKernelFunction kf;
223  classifier::Kernel_MEV kernel(ml,kf);
224  classifier::DataLookup2D* dl_kernel = new classifier::KernelLookup(kernel); 
225  bool catch_error=false;
226  try {
227    catch_error=false; // should catch error here
228    *error << ncc.make_classifier(*dl_kernel,target) << std::endl;
229  }
230  catch (std::runtime_error) {   
231    *error << "caught expected bad cast runtime_error" << std::endl;
232    catch_error=true;
233  }
234  if(!catch_error) {
235    ok=false;
236  }
237  try {
238    catch_error=false; // should catch error here
239    ncc.predict(*dl_kernel,prediction); 
240  }
241  catch (std::runtime_error) {   
242    *error << "caught expected bad cast runtime_error" << std::endl;
243    catch_error=true;
244  }
245  if(!catch_error) {
246    ok=false;
247  }
248  delete dl_kernel;
249 
250  if(ok)
251    *error << "OK" << std::endl;
252  else
253    *error << "FAILED" << std::endl;
254
255  if (error!=&std::cerr)
256    delete error;
257
258  if(ok) 
259    return 0;
260  return -1; 
261}
Note: See TracBrowser for help on using the repository browser.