source: trunk/test/ncc_test.cc @ 1013

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

Adding functionality tests for NCC

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1// $Id: ncc_test.cc 1013 2008-02-01 15:34:30Z 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/euclidean_vector_distance.h"
34#include "yat/statistics/pearson_vector_distance.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::pearson_vector_distance_tag> 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  utility::matrix data1(3,4);
89  for(size_t i=0;i<3;i++) {
90    data1(i,0)=3-i;
91    data1(i,1)=5-i;
92    data1(i,2)=i+1;
93    data1(i,3)=i+3;
94  }
95  std::vector<std::string> vec1(4, "pos");
96  vec1[0]="neg";
97  vec1[1]="neg";
98
99  classifier::MatrixLookup ml1(data1);
100  classifier::Target target1(vec1);
101
102  classifier::NCC<statistics::euclidean_vector_distance_tag> ncc1(ml1,target1);
103  ncc1.train();
104  utility::matrix prediction1;
105  ncc1.predict(ml1,prediction1);
106  double slack_bound=2e-7;
107  utility::matrix result1(2,4);
108  result1(0,0)=result1(0,1)=result1(1,2)=result1(1,3)=sqrt(3.0);
109  result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=sqrt(11.0);
110  double slack = deviation(prediction1,result1); 
111  if (slack > slack_bound || std::isnan(slack)){
112    *error << "Difference to expected prediction too large\n";
113    *error << "slack: " << slack << std::endl;
114    *error << "expected less than " << slack_bound << std::endl;
115    ok = false;
116  }
117
118  //////////////////////////////////////////////////////////////////////////
119  // A test of predictions using unweighted training and weighted test data
120  //////////////////////////////////////////////////////////////////////////
121  utility::matrix weights1(3,4,1.0);
122  weights1(0,0)=weights1(1,1)=weights1(2,2)=weights1(1,3)=0.0;
123  classifier::MatrixLookupWeighted mlw1(data1,weights1);
124  ncc1.predict(mlw1,prediction1); 
125  result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=sqrt(15.0);
126  slack = deviation(prediction1,result1);
127  if (slack > slack_bound || std::isnan(slack)){
128    *error << "Difference to expected prediction too large\n";
129    *error << "slack: " << slack << std::endl;
130    *error << "expected less than " << slack_bound << std::endl;
131    ok = false;
132  }
133
134 
135
136  //////////////////////////////////////////////////////////////////////////
137  // A test of predictions using Sorlie data
138  //////////////////////////////////////////////////////////////////////////
139  std::ifstream is("data/sorlie_centroid_data.txt");
140  utility::matrix data(is,'\t');
141  is.close();
142
143  is.open("data/sorlie_centroid_classes.txt");
144  classifier::Target targets(is);
145  is.close();
146
147  // Generate weight matrix with 0 for missing values and 1 for others.
148  utility::matrix weights(data.rows(),data.columns(),0.0);
149  utility::nan(data,weights);
150     
151  classifier::MatrixLookupWeighted dataviewweighted(data,weights);
152  classifier::NCC<statistics::pearson_vector_distance_tag> ncc(dataviewweighted,targets);
153  *error << "training...\n";
154  ncc.train();
155
156  // Comparing the centroids to stored result
157  is.open("data/sorlie_centroids.txt");
158  utility::matrix centroids(is);
159  is.close();
160
161  if(centroids.rows() != ncc.centroids().rows() ||
162     centroids.columns() != ncc.centroids().columns()) {
163    *error << "Error in the dimensionality of centroids\n";
164    *error << "Nof rows: " << centroids.rows() << " expected: " 
165           << ncc.centroids().rows() << std::endl;
166    *error << "Nof columns: " << centroids.columns() << " expected: " 
167           << ncc.centroids().columns() << std::endl;
168  }
169
170  slack = deviation(centroids,ncc.centroids());
171  if (slack > slack_bound || std::isnan(slack)){
172    *error << "Difference to stored centroids too large\n";
173    *error << "slack: " << slack << std::endl;
174    *error << "expected less than " << slack_bound << std::endl;
175    ok = false;
176  }
177
178  *error << "...predicting...\n";
179  utility::matrix prediction;
180  ncc.predict(dataviewweighted,prediction);
181 
182  // Comparing the prediction to stored result
183  is.open("data/sorlie_centroid_predictions.txt");
184  utility::matrix result(is,'\t');
185  is.close();
186
187  slack = deviation(result,prediction);
188  if (slack > slack_bound || std::isnan(slack)){
189    *error << "Difference to stored prediction too large\n";
190    *error << "slack: " << slack << std::endl;
191    *error << "expected less than " << slack_bound << std::endl;
192    ok = false;
193  }
194  *error << "done\n";
195
196  //////////////////////////////////////////////////////////////////////////
197  // Testing rejection of KernelLookups
198  //////////////////////////////////////////////////////////////////////////
199  classifier::PolynomialKernelFunction kf;
200  classifier::Kernel_MEV kernel(ml,kf);
201  classifier::DataLookup2D* dl_kernel = new classifier::KernelLookup(kernel); 
202  try {
203    ok=0; // should catch error here
204    *error << ncc.make_classifier(*dl_kernel,target) << std::endl;
205  }
206  catch (std::runtime_error) {   
207    *error << "caught expected bad cast runtime_error" << std::endl;
208    ok=1;
209  }
210  try {
211    ok=0; // should catch error here
212    ncc.predict(*dl_kernel,prediction); 
213  }
214  catch (std::runtime_error) {   
215    *error << "caught expected bad cast runtime_error" << std::endl;
216    ok=1;
217  }
218  delete dl_kernel;
219 
220  if(ok)
221    *error << "OK" << std::endl;
222
223  if (error!=&std::cerr)
224    delete error;
225
226  if(ok) 
227    return 0;
228  return -1; 
229}
Note: See TracBrowser for help on using the repository browser.