source: trunk/test/ncc_test.cc @ 898

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

A first suggestion for how to adress #250. Also removed contamination of namespace std (see #251).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1// $Id: ncc_test.cc 898 2007-09-26 13:44:19Z 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/trac/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/MatrixLookup.h"
26#include "yat/classifier/MatrixLookupWeighted.h"
27#include "yat/classifier/NCC.h"
28#include "yat/classifier/Target.h"
29#include "yat/utility/matrix.h"
30#include "yat/statistics/vector_distance_ptr.h"
31#include "yat/statistics/pearson_vector_distance.h"
32#include "yat/utility/utility.h"
33
34#include <cassert>
35#include <fstream>
36#include <iostream>
37#include <sstream>
38#include <string>
39#include <limits>
40#include <cmath>
41
42using namespace theplu::yat;
43
44int main(const int argc,const char* argv[])
45{ 
46
47  std::ostream* error;
48  if (argc>1 && argv[1]==std::string("-v"))
49    error = &std::cerr;
50  else {
51    error = new std::ofstream("/dev/null");
52    if (argc>1)
53      std::cout << "ncc_test -v : for printing extra information\n";
54  }
55  *error << "testing ncc" << std::endl;
56  bool ok = true;
57
58  std::ifstream is("data/sorlie_centroid_data.txt");
59  utility::matrix data(is,'\t');
60  is.close();
61
62  is.open("data/sorlie_centroid_classes.txt");
63  classifier::Target targets(is);
64  is.close();
65
66  // Generate weight matrix with 0 for missing values and 1 for others.
67  utility::matrix weights(data.rows(),data.columns(),0.0);
68  for(size_t i=0;i<data.rows();++i)
69    for(size_t j=0;j<data.columns();++j)
70      if(!std::isnan(data(i,j)))
71        weights(i,j)=1.0;
72     
73  classifier::MatrixLookupWeighted dataviewweighted(data,weights);
74  statistics::vector_distance_lookup_weighted_ptr distance=
75    statistics::vector_distance<statistics::pearson_vector_distance_tag>;
76  classifier::NCC ncc(dataviewweighted,targets,distance);
77  ncc.train();
78
79  // Comparing the centroids to stored result
80  is.open("data/sorlie_centroids.txt");
81  utility::matrix centroids(is);
82  is.close();
83
84  if(centroids.rows() != ncc.centroids().rows() ||
85     centroids.columns() != ncc.centroids().columns()) {
86    *error << "Error in the dimensionality of centroids\n";
87    *error << "Nof rows: " << centroids.rows() << " expected: " 
88           << ncc.centroids().rows() << std::endl;
89    *error << "Nof columns: " << centroids.columns() << " expected: " 
90           << ncc.centroids().columns() << std::endl;
91  }
92
93  double slack = 0;
94  for (size_t i=0; i<centroids.rows(); i++){
95    for (size_t j=0; j<centroids.columns(); j++){
96      slack += fabs(centroids(i,j)-ncc.centroids()(i,j));
97    }
98  }
99  slack /= (centroids.columns()*centroids.rows());
100  double slack_bound=2e-7;
101  if (slack > slack_bound || std::isnan(slack)){
102    *error << "Difference to stored centroids too large\n";
103    *error << "slack: " << slack << std::endl;
104    *error << "expected less than " << slack_bound << std::endl;
105    ok = false;
106  }
107
108  utility::matrix prediction;
109  ncc.predict(dataviewweighted,prediction);
110 
111  // Comparing the prediction to stored result
112  is.open("data/sorlie_centroid_predictions.txt");
113  utility::matrix result(is,'\t');
114  is.close();
115
116  slack = 0;
117  for (size_t i=0; i<result.rows(); i++){
118    for (size_t j=0; j<result.columns(); j++){
119        slack += fabs(result(i,j)-prediction(i,j));
120    }
121  }
122  slack /= (result.columns()*result.rows());
123  if (slack > slack_bound || std::isnan(slack)){
124    *error << "Difference to stored prediction too large\n";
125    *error << "slack: " << slack << std::endl;
126    *error << "expected less than " << slack_bound << std::endl;
127    ok = false;
128  }
129 
130
131  if(ok)
132    *error << "OK" << std::endl;
133
134
135  if (error!=&std::cerr)
136    delete error;
137
138  if(ok) 
139    return 0;
140  return -1;
141 
142}
Note: See TracBrowser for help on using the repository browser.