source: trunk/test/ncc_test.cc @ 584

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

Fixed so NCC and IGP works when there are NaNs? in centroids.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.0 KB
Line 
1// $Id: ncc_test.cc 584 2006-05-18 07:11:06Z markus $
2
3#include <c++_tools/classifier/IGP.h>
4#include <c++_tools/classifier/MatrixLookup.h>
5#include <c++_tools/classifier/NCC.h>
6#include <c++_tools/classifier/Target.h>
7#include <c++_tools/gslapi/matrix.h>
8#include <c++_tools/gslapi/vector.h>
9#include <c++_tools/statistics/PearsonDistance.h>
10#include <c++_tools/utility/utility.h>
11
12#include <cassert>
13#include <fstream>
14#include <iostream>
15#include <sstream>
16#include <string>
17#include <limits>
18#include <cmath>
19
20using namespace theplu;
21
22int main(const int argc,const char* argv[])
23{ 
24
25  std::ostream* error;
26  if (argc>1 && argv[1]==std::string("-v"))
27    error = &std::cerr;
28  else {
29    error = new std::ofstream("/dev/null");
30    if (argc>1)
31      std::cout << "ncc_test -v : for printing extra information\n";
32  }
33  *error << "testing ncc" << std::endl;
34  bool ok = true;
35
36
37  std::ifstream is("data/sorlie_centroids.txt");
38  gslapi::matrix data(is,'\t');
39  is.close();
40
41  is.open("data/sorlie_centroid_classes.txt");
42  classifier::Target targets(is);
43  is.close();
44
45   
46  classifier::MatrixLookup dataview(data);
47  statistics::PearsonDistance pearson; 
48  classifier::NCC ncc(dataview,targets,pearson);
49  ncc.train();
50
51  gslapi::matrix prediction;
52  ncc.predict(dataview,prediction);
53 
54  is.open("data/sorlie_centroid_predictions.txt");
55  gslapi::matrix result(is,'\t');
56  is.close();
57
58  // Comparing the prediction to stored result
59  double slack = 0;
60  for (size_t i=0; i<result.rows(); i++){
61    for (size_t j=0; j<result.columns(); j++){
62        slack += fabs(result(i,j)-prediction(i,j));
63    }
64  }
65  slack /= (result.columns()*result.rows());
66  double slack_bound=2e-7;
67  if (slack > slack_bound){
68    *error << "Difference to stored prediction too large\n";
69    *error << "slack: " << slack << std::endl;
70    *error << "expected less than " << slack_bound << std::endl;
71    ok = false;
72  }
73 
74
75  // Testing IGP
76  *error << "testing igp" << std::endl;
77  classifier::IGP igp(dataview,targets,pearson);
78  *error << igp.score() << std::endl;
79
80
81  if (error!=&std::cerr)
82    delete error;
83
84  if(ok)
85    return 0;
86  return -1;
87 
88}
Note: See TracBrowser for help on using the repository browser.