Changeset 1239 for trunk


Ignore:
Timestamp:
Mar 16, 2008, 6:09:56 AM (14 years ago)
Author:
Peter
Message:

working on #223

Location:
trunk/test
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/Suite.cc

    r1234 r1239  
    2424#include "Suite.h"
    2525
     26#include <algorithm>
    2627#include <cmath>
    2728#include <fstream>
     
    7677  bool Suite::equal(double a, double b, u_int n)
    7778  {
    78     last_error_bound_ = n*std::numeric_limits<double>().epsilon()*
     79    double last_error_bound = n*std::numeric_limits<double>().epsilon()*
    7980      std::min(std::abs(a), std::abs(b));
    80     return std::abs(a-b) <= last_error_bound_;
    81   }
    82 
    83 
    84   double Suite::last_error_bound(void) const
    85   {
    86     return last_error_bound_;
     81    if (!(std::abs(a-b) <= last_error_bound)){
     82      err() << "Error: Comparing " << a << " and " << b
     83            << "\n  Difference: " << a - b
     84            << "\n  expected difference to be at most " << last_error_bound
     85            << std::endl;
     86      return false;
     87    }
     88    return true;
    8789  }
    8890
  • trunk/test/Suite.h

    r1234 r1239  
    5959    bool equal(double a, double b, unsigned int N=1);
    6060
    61     double last_error_bound(void) const;
     61    template<typename Iterator1, typename Iterator2>
     62    bool equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
     63                     unsigned int N=1);
    6264
    6365    /**
     
    8587    bool ok_;
    8688    std::ofstream* dev_null_;
    87     double last_error_bound_;
    8889
    8990  };
     91
     92  template<typename Iterator1, typename Iterator2>
     93  bool Suite::equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
     94                          unsigned int N)
     95  {
     96    while (first1!=last1){
     97      if (!this->equal(*first1, *first2, N) )  {
     98        return false;
     99      }
     100      ++first1;
     101      ++first2;
     102    }
     103    return true;
     104  }
    90105
    91106}}}
  • trunk/test/distance_test.cc

    r1234 r1239  
    154154  if (!suite.equal(dist1, dist2, N)) {
    155155    suite.err() << "Error: " << msg << " failed.\n";
    156     suite.err() << "  dist1: " << dist1 << "\n";
    157     suite.err() << "  dist2: " << dist2 << "\n";
    158     suite.err() << "  difference: " << dist1-dist2 << "\n";
    159     suite.err() << "  error bound: " << suite.last_error_bound() << "\n";
    160156    suite.add(false);
    161157  }
  • trunk/test/knn_test.cc

    r1210 r1239  
    22
    33/*
    4   Copyright (C) 2007 Peter Johansson, Markus Ringnér
     4  Copyright (C) 2007 2008 Peter Johansson, Markus Ringnér
    55
    66  This file is part of the yat library, http://trac.thep.lu.se/yat
     
    2121  02111-1307, USA.
    2222*/
     23
     24#include "Suite.h"
    2325
    2426#include "yat/classifier/KNN.h"
     
    5254}
    5355
    54 int main(const int argc,const char* argv[])
    55 
     56int main(int argc, char* argv[])
    5657
    57   std::ostream* error;
    58   if (argc>1 && argv[1]==std::string("-v"))
    59     error = &std::cerr;
    60   else {
    61     error = new std::ofstream("/dev/null");
    62     if (argc>1)
    63       std::cout << "knn_test -v : for printing extra information\n";
    64   }
    65   *error << "testing knn" << std::endl;
    66   bool ok = true;
     58  test::Suite suite(argc, argv);
     59  suite.err() << "testing knn" << std::endl;
    6760
    6861  ////////////////////////////////////////////////////////////////
    6962  // A test of training and predictions using unweighted data
    7063  ////////////////////////////////////////////////////////////////
    71   *error << "test of predictions using unweighted training and test data\n";
     64  suite.err() << "test of predictions using unweighted training "
     65              << "and test data\n";
    7266  utility::Matrix data1(3,4);
    7367  for(size_t i=0;i<3;i++) {
     
    8983  utility::Matrix prediction1;
    9084  knn1.predict(ml1,prediction1);
    91   double slack_bound=2e-7;
    9285  utility::Matrix result1(2,4);
    9386  result1(0,0)=result1(0,1)=result1(1,2)=result1(1,3)=2.0;
    9487  result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=1.0;
    95   double slack = deviation(prediction1,result1);
    96   if (slack > slack_bound || std::isnan(slack)){
    97     *error << "Difference to expected prediction too large\n";
    98     *error << "slack: " << slack << std::endl;
    99     *error << "expected less than " << slack_bound << std::endl;
    100     ok = false;
    101   }
     88  suite.add(suite.equal_range(result1.begin(), result1.end(),
     89                              prediction1.begin(), 1));
    10290 
    10391
     
    10593  // A test of training unweighted and test weighted
    10694  ////////////////////////////////////////////////////////////////
    107   *error << "test of predictions using unweighted training and weighted test data\n";
     95  suite.err() << "test of predictions using unweighted training and weighted test data\n";
    10896  utility::Matrix weights1(3,4,1.0);
    10997  weights1(2,0)=0;
     
    112100  result1(0,0)=1.0;
    113101  result1(1,0)=2.0;
    114   slack = deviation(prediction1,result1);
    115   if (slack > slack_bound || std::isnan(slack)){
    116     *error << "Difference to expected prediction too large\n";
    117     *error << "slack: " << slack << std::endl;
    118     *error << "expected less than " << slack_bound << std::endl;
    119     ok = false;
    120   }
     102  suite.add(suite.equal_range(result1.begin(), result1.end(),
     103                              prediction1.begin(), 1));
    121104
    122105  ////////////////////////////////////////////////////////////////
    123106  // A test of training and test both weighted
    124107  ////////////////////////////////////////////////////////////////
    125   *error << "test of predictions using weighted training and test data\n";
    126   *error << "... uniform neighbor weighting" << std::endl;
     108  suite.err() << "test of predictions using weighted training and test data\n";
     109  suite.err() << "... uniform neighbor weighting" << std::endl;
    127110  weights1(0,1)=0;
    128111  utility::Matrix weights2(3,4,1.0);
     
    135118  result1(0,1)=1.0;
    136119  result1(1,1)=2.0;
    137   slack = deviation(prediction1,result1);
    138   if (slack > slack_bound || std::isnan(slack)){
    139     *error << "Difference to expected prediction too large\n";
    140     *error << "slack: " << slack << std::endl;
    141     *error << "expected less than " << slack_bound << std::endl;
    142     ok = false;
    143   }
     120  suite.add(suite.equal_range(result1.begin(), result1.end(),
     121                              prediction1.begin(), 1));
    144122
    145123
     
    147125  // A test of reciprocal ranks weighting with training and test both weighted
    148126  ////////////////////////////////////////////////////////////////
    149   *error << "... reciprokal rank neighbor weighting" << std::endl;
     127  suite.err() << "... reciprokal rank neighbor weighting" << std::endl;
    150128  utility::Matrix data2(data1);
    151129  data2(1,3)=7;
     
    159137  result1(0,2)=result1(1,1)=1.0/2.0;
    160138  result1(0,1)=result1(1,2)=4.0/3.0;
    161   slack = deviation(prediction1,result1);
    162   if (slack > slack_bound || std::isnan(slack)){
    163     *error << "Difference to expected prediction too large\n";
    164     *error << "slack: " << slack << std::endl;
    165     *error << "expected less than " << slack_bound << std::endl;
    166     ok = false;
    167   }
     139  suite.add(suite.equal_range(result1.begin(), result1.end(),
     140                              prediction1.begin(), 1));
    168141
    169142 
     
    171144  // A test of reciprocal distance weighting with training and test both weighted
    172145  ////////////////////////////////////////////////////////////////
    173   *error << "... reciprokal distance neighbor weighting" << std::endl;
     146  suite.err() << "... reciprocal distance neighbor weighting" << std::endl;
    174147  classifier::KNN<statistics::EuclideanDistance,classifier::KNN_ReciprocalDistance>
    175148    knn4;
     
    179152  if (!(std::isinf(prediction1(0,0)) && std::isinf(prediction1(0,1)) &&
    180153        std::isinf(prediction1(1,2)) &&
    181         std::abs(prediction1(1,3)-(1.0/3.67423461417))<slack_bound &&
    182         std::abs(prediction1(1,0)-(1.0/2.82842712475+1.0/2.44948974278))<slack_bound)){
    183     *error << "Difference to expected prediction too large\n";
    184     ok = false;
     154        suite.equal(prediction1(1,3), 1.0/3.6742346141747673, 5000) &&
     155        suite.equal(prediction1(1,0), 1.0/2.82842712475+1.0/2.4494897427831779,
     156                    5000)
     157        )){
     158    suite.err() << "Difference to expected prediction too large\n";
     159    suite.add(false);
    185160  }
    186161
     
    202177  if (!(std::isnan(prediction1(0,0)) && std::isnan(prediction1(0,1)) &&
    203178        std::isnan(prediction1(0,2)) && std::isnan(prediction1(0,3)) &&
    204         std::abs(prediction1(1,0)-2.0)<slack_bound &&
    205         std::abs(prediction1(1,1)-2.0)<slack_bound &&
    206         std::abs(prediction1(1,2)-2.0)<slack_bound &&
    207         std::abs(prediction1(1,3)-2.0)<slack_bound)) {
    208     *error << "Difference to expected prediction too large\n";
    209     ok = false;
     179        suite.equal(prediction1(1,0),2.0) &&
     180        suite.equal(prediction1(1,1),2.0) &&
     181        suite.equal(prediction1(1,2),2.0) &&
     182        suite.equal(prediction1(1,3),2.0) )) {
     183    suite.err() << "Difference to expected prediction too large\n";
     184    suite.add(false);
    210185  }
    211186
     
    214189  // weights in common with training samples: should not vote
    215190  ////////////////////////////////////////////////////////////////
    216   *error << "test of predictions with nan distances (set to infinity in KNN)\n";
     191  suite.err() << "test of predictions with nan distances (set to infinity in KNN)\n";
    217192  weights1.all(1);
    218193  weights1(1,0)=weights1(1,1)=weights1(2,0)=weights1(2,1)=0.0;
     
    226201  result1(0,2)=result1(1,1)=result1(1,3)=1.0;
    227202  result1(0,1)=result1(0,3)=result1(1,0)=result1(1,2)=2.0;
    228   slack = deviation(prediction1,result1);
    229   if (slack > slack_bound || std::isnan(slack)){
    230     *error << "Difference to expected prediction too large\n";
    231     *error << "slack: " << slack << std::endl;
    232     *error << "expected less than " << slack_bound << std::endl;
    233     ok = false;
    234   }
    235  
    236 
    237 
    238   if(!ok) {
    239     *error << "knn_test failed" << std::endl;
    240   }
    241   else {
    242     *error << "OK" << std::endl;
    243   }
    244   if (error!=&std::cerr)
    245     delete error;
    246   if (ok=true) 
    247     return 0;
    248   return -1;
     203  suite.add(suite.equal_range(result1.begin(), result1.end(),
     204                              prediction1.begin(), 1));
     205  return suite.return_value();
    249206}
    250207
Note: See TracChangeset for help on using the changeset viewer.