Changeset 146


Ignore:
Timestamp:
Sep 8, 2004, 10:51:20 PM (17 years ago)
Author:
Jari Häkkinen
Message:

Changed the NNI algorithms to accept matrix instead of istreams.

Location:
trunk
Files:
3 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/NNI.cc

    r145 r146  
    1313  using namespace std;
    1414
    15   NNI::NNI(istream& matrix,istream& weight,const u_int neighbours)
     15  NNI::NNI(gslapi::matrix& matrix,const gslapi::matrix& weight,
     16           const u_int neighbours)
    1617    : data_(matrix), weight_(weight), neighbours_(neighbours)
    1718  {
  • trunk/src/NNI.h

    r145 r146  
    2121  {
    2222  public:
    23     NNI(istream& matrix,istream& weight,const u_int neighbours);
     23    ///
     24    /// \a matrix is changed.
     25    ///
     26    NNI(gslapi::matrix& matrix,const gslapi::matrix& weight,
     27        const u_int neighbours);
    2428
    2529    virtual ~NNI(void) {};
    2630
    27     ///
    28     /// Estimate missing values
    29     ///
    30     virtual void estimate(void)=0;
    31 
    32     ///
    33     /// Return matrix
    34     ///
    35     /// @return Const reference to the data matrix.
    36     ///
    37     inline const gslapi::matrix& matrix(void) const { return data_; }
    38 
    3931  protected:
    4032    vector<pair<u_int,double> > calculate_distances(const u_int) const;
     33    virtual void estimate(void)=0;
    4134    vector<u_int> nearest_neighbours(const u_int,
    4235                                     const vector<pair<u_int,double> >&) const;
    4336
    44     gslapi::matrix data_;
    45     gslapi::matrix weight_;
     37    gslapi::matrix& data_;
     38    const gslapi::matrix& weight_;
    4639    u_int neighbours_;
    4740  };
  • trunk/src/WeNNI.cc

    r145 r146  
    1313
    1414
    15   WeNNI::WeNNI(istream& matrix,istream& flag,const u_int neighbours)
     15  WeNNI::WeNNI(gslapi::matrix& matrix,const gslapi::matrix& flag,
     16               const u_int neighbours)
    1617    : NNI(matrix,flag,neighbours)
    1718  {
     19    estimate();
    1820  }
    1921
     
    3537        double norm=0.0;
    3638        for (vector<u_int>::const_iterator k=knn.begin(); k!=knn.end(); k++) {
    37           double w=weight_(distance[*k].first,j);
    3839          new_value+=1.0*data_(distance[*k].first,j)/(distance[*k].second);
    3940          norm+=1.0/(distance[*k].second);
  • trunk/src/WeNNI.h

    r145 r146  
    2121  {
    2222  public:
     23    ///
     24    /// \a matrix is changed.
     25    ///
     26    WeNNI(gslapi::matrix& matrix,const gslapi::matrix& weight,
     27         const u_int neighbours);
    2328
    24     WeNNI(istream& matrix,istream& weight,const u_int neighbours);
    25 
    26     ///
    27     /// Estimate missing values
    28     ///
     29  private:
    2930    void estimate(void);
    3031
  • trunk/src/kNNI.cc

    r145 r146  
    1313namespace cpptools {
    1414
    15   kNNI::kNNI(istream& matrix,istream& flag,const u_int neighbours)
     15  kNNI::kNNI(gslapi::matrix& matrix,const gslapi::matrix& flag,
     16             const u_int neighbours)
    1617    : NNI(matrix,flag,neighbours)
    1718  {
     
    2223          break;
    2324        }
     25    estimate();
    2426  }
    2527
  • trunk/src/kNNI.h

    r145 r146  
    1818  {
    1919  public:
    20     kNNI(istream& matrix,istream& weight,const u_int neighbours);
     20    ///
     21    /// \a matrix is changed.
     22    ///
     23    kNNI(gslapi::matrix& matrix,const gslapi::matrix& weight,
     24         const u_int neighbours);
    2125
    22     ///
    23     /// Estimate missing values
    24     ///
     26  private:
    2527    void estimate(void);
    2628
    27   private:
    2829    vector<u_int> mv_rows_; // index to rows that have values to estimate
    2930  };
  • trunk/test/test_nni.cc

    r145 r146  
    1010#include "WeNNI.h"
    1111
    12 
    13 
    14 // class for command line options.
    15 class Parameter {
    16 public:
    17   Parameter(const Parameter&);  // not implemented
    18   Parameter(const int argc,const char *argv[]);
    19 
    20   inline const std::string& matrix(void) const { return matrix_file_; }
    21   inline const std::string& weight(void) const { return weight_file_; }
    22   inline int neighbours(void) const { return neighbours_; }
    23 
    24 private:
    25   void analyse(void);
    26   void defaults(void);
    27 
    28   std::string weight_file_;
    29   std::string matrix_file_;
    30   int neighbours_;
    31 };
    32 
    33 Parameter::Parameter(const int argc,const char *argv[])
     12void check_file_access(std::string& str)
    3413{
    35   using namespace std;
    36   // check if help requested
    37   for (int i=1; i<argc; i++)
    38     if (string(argv[i])==string("-help") || string(argv[i])==string("-h")) {
    39       cout << "Please write some help information\n";
    40       exit(0);      // always exit after printing help
    41     }
    42 
    43   defaults();
    44 
    45   for (int i=1; i<argc; i++) {
    46     bool ok=false;
    47     string myargv(argv[i]);
    48     if (myargv==string("-matrix"))
    49       if ((i+1)<argc) {
    50         matrix_file_=argv[++i];
    51         ok=true;
    52       }
    53     if (myargv==string("-weight"))
    54       if ((i+1)<argc) {
    55         weight_file_=argv[++i];
    56         ok=true;
    57       }
    58     if (myargv==string("-neighbours"))
    59       if ((i+1)<argc) {
    60         neighbours_=atoi(argv[++i]);
    61         ok=true;
    62       }
    63 
    64     if (!ok)
    65       std::cerr << "# Parameter::Parameter     Invalid option: "
    66                 << argv[i] << '\n';
     14  if (theplu::cpptools::FileIO().access_rights(str,"r")) {
     15    std::cerr << "test_nni: Cannot access file " << str << std::endl;
     16    exit(-1);
    6717  }
    68 
    69   analyse();
    7018}
    71 
    72 void Parameter::analyse(void)
    73 {
    74   using namespace theplu::cpptools;
    75   bool ok=true;
    76   if (FileIO().access_rights(matrix(),"r")) {
    77     std::cerr << "Cannot access matrix file " << matrix() << std::endl;
    78     ok=false;
    79   }
    80   if (FileIO().access_rights(weight(),"r")) {
    81     std::cerr << "Cannot access weight file " << weight() << std::endl;
    82     ok=false;
    83   }
    84   if (!ok)
    85     std::exit(-1);
    86 }
    87 
    88 void Parameter::defaults(void)
    89 {
    90   weight_file_="data/knni_weight.data";
    91   matrix_file_="data/knni_matrix.data";
    92   neighbours_=3;
    93 }
    94 
    95 
    9619
    9720int main(const int argc,const char* argv[])
    9821{
    99   Parameter option(argc,argv);
    100   std::ifstream matrix_stream(option.matrix().c_str());
    101   std::ifstream weight_stream(option.weight().c_str());
    102   theplu::cpptools::kNNI knni(matrix_stream,weight_stream,option.neighbours());
    103   knni.estimate();
    104   std::cout << knni.matrix() << std::endl;
    105   std::ifstream control_stream("data/knni_result.data");
     22
     23  uint neighbours=3;
     24  std::string knni_data("data/knni_matrix.data");
     25  std::string knni_result("data/knni_result.data");
     26  std::string knni_weight("data/knni_weight.data");
     27  std::string wenni_data("data/knni_matrix.data");
     28  std::string wenni_result("data/wenni_result.data");
     29  std::string wenni_weight("data/wenni_weight.data");
     30  check_file_access(knni_data);
     31  check_file_access(knni_result);
     32  check_file_access(knni_weight);
     33  check_file_access(wenni_data);
     34  check_file_access(wenni_result);
     35  check_file_access(wenni_weight);
     36
     37  // test kNNI
     38  std::ifstream data_stream(knni_data.c_str());
     39  std::ifstream weight_stream(knni_weight.c_str());
     40  theplu::gslapi::matrix data(data_stream);
     41  theplu::gslapi::matrix weight(weight_stream);
     42  theplu::cpptools::kNNI knni(data,weight,neighbours);
     43  std::ifstream control_stream(knni_result.c_str());
    10644  theplu::gslapi::matrix control(control_stream);
    107   control-=knni.matrix();
     45  control-=data;
    10846  for (unsigned int i=0; i<control.rows(); i++)
    10947    for (unsigned int j=0; j<control.columns(); j++)
     
    11149      // hardcoded number below should be changed.
    11250      if (fabs(control(i,j))>5e-13) {
    113         std::cerr << "test_nni: FAILED, could not reproduce test result\n";
     51        std::cerr << "test_nni: kNNI FAILED, cannot reproduce test result\n";
    11452        exit(-1); // calculation result out of accepted error bounds
    11553      }
     54  control_stream.close();
     55  data_stream.close();
     56  weight_stream.close();
     57
     58  // test WeNNI
     59  data_stream.open(wenni_data.c_str());
     60  data=theplu::gslapi::matrix(data_stream);
     61  weight_stream.open(wenni_weight.c_str());
     62  weight=theplu::gslapi::matrix(weight_stream);
     63  theplu::cpptools::WeNNI wenni(data,weight,neighbours);
     64  control_stream.open(wenni_result.c_str());
     65  control=theplu::gslapi::matrix(control_stream);
     66  control-=data;
     67  for (unsigned int i=0; i<control.rows(); i++)
     68    for (unsigned int j=0; j<control.columns(); j++)
     69      // Jari, should we use GSL defined round off errors? Anyway, the
     70      // hardcoded number below should be changed.
     71      if (fabs(control(i,j))>5e-13) {
     72        std::cerr << "test_nni: WeNNI FAILED, cannot reproduce test result\n";
     73        exit(-1); // calculation result out of accepted error bounds
     74      }
     75
    11676  std::cout << "test_nni: SUCCESS, reproducing test result\n";
    11777  exit(0);        // normal exit
Note: See TracChangeset for help on using the changeset viewer.