Changeset 593


Ignore:
Timestamp:
Aug 25, 2006, 2:59:21 PM (15 years ago)
Author:
Markus Ringnér
Message:

Fixed std includes to compile with g++ 4.1.

Location:
trunk
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/c++_tools/classifier/DataLookup1D.cc

    r537 r593  
    66#include <cassert>
    77#include <iostream>
     8#include <iomanip>
    89
    910namespace theplu {
     
    3637  }
    3738
     39
    3840  double DataLookup1D::operator*(const DataLookup1D& other) const
    3941  {
     
    4547  }
    4648
    47   std::ostream& operator<< (std::ostream& os, const DataLookup1D& x)
     49
     50  std::ostream& operator<<(std::ostream& os, const DataLookup1D& x)
    4851  {
    4952    os.setf(std::ios::dec);
    5053    os.precision(12);
    51     for (size_t i = 0; i < x.size(); ++i) {
     54   
     55    for (size_t i=0; i<x.size(); ++i) {
    5256      os << x(i);
    53       if ( (i+1)<x.size() )
    54         os << " ";
     57      if ((i+1)<x.size())
     58        os << os.fill();
    5559    }
    5660    return os;
    5761  }
     62
    5863}} // of namespace classifier and namespace theplu
  • trunk/c++_tools/classifier/DataLookup1D.h

    r581 r593  
    8787    const bool owner_;
    8888   
    89   }; 
     89  };
     90
    9091  ///
    91   /// The output operator DataLook1D
     92  /// @brief The output operator for DataLookup1D.
    9293  ///
    93   /// @todo test that output can be used from gslapi::vector istream constructor
    94   std::ostream& operator<< (std::ostream& s, const DataLookup1D&);
     94  /**
     95   * Elements are separated by the character from the omanip fill.
     96   * The following example will write the elements separated by tab.
     97   *
     98   @verbatim
     99   char prev=s.fill('\t');
     100   s << v;
     101   s.fill(prev);
     102   @endverbatim
     103  */
     104  std::ostream& operator<<(std::ostream& s, const DataLookup1D& v);
    95105 
    96106}} // of namespace classifier and namespace theplu
  • trunk/c++_tools/classifier/EnsembleBuilder.cc

    r559 r593  
    3030    cross_splitter_.reset();
    3131    while(cross_splitter_.more()) {
    32       const DataLookup2D& training=cross_splitter_.training_data();
    33       const Target& targets=cross_splitter_.training_target();
    34 
    3532      SupervisedClassifier* classifier=
    36         mother_.make_classifier(training,targets);
     33        mother_.make_classifier(cross_splitter_);
    3734      classifier->train();
    3835      classifier_.push_back(classifier);
  • trunk/c++_tools/classifier/MatrixLookup.cc

    r592 r593  
    162162        s << m(i,j);
    163163        if (j<m.columns()-1)
    164           s << "\t";
     164          s << s.fill();
    165165        else if (i<m.rows()-1)
    166166          s << "\n";
  • trunk/c++_tools/classifier/NCC.cc

    r584 r593  
    33#include <c++_tools/classifier/NCC.h>
    44
     5#include <c++_tools/classifier/CrossSplitter.h>
    56#include <c++_tools/classifier/DataLookup1D.h>
    67#include <c++_tools/classifier/DataLookup2D.h>
     
    2223  NCC::NCC(const MatrixLookup& data, const Target& target,
    2324           const statistics::Distance& distance)
    24     : SupervisedClassifier(target), distance_(distance), matrix_(data)
    25   {   
     25    : SupervisedClassifier(target), distance_(distance), matrix_(data),
     26      weighted_(false),
     27      weights_(new MatrixLookup(data.rows(),data.columns(),1.0))
     28  {
    2629  }
     30
     31  NCC::NCC(const MatrixLookup& data, const Target& target,
     32           const statistics::Distance& distance, const MatrixLookup& weights)
     33    : SupervisedClassifier(target), distance_(distance), matrix_(data),
     34      weighted_(true),weights_(&weights)
     35  {
     36  }
     37
    2738
    2839  NCC::NCC(const MatrixLookup& data, const Target& target,
     
    3041           statistics::Score& score, size_t nof_inputs)
    3142    : SupervisedClassifier(target, &score, nof_inputs),
    32       distance_(distance), matrix_(data)
     43      distance_(distance), matrix_(data),weighted_(false),
     44      weights_(new MatrixLookup(data.rows(),data.columns(),1.0))
    3345  {
    3446  }
    3547
    36   NCC::~NCC()   
     48  NCC::NCC(const MatrixLookup& data, const Target& target,
     49           const statistics::Distance& distance,
     50           const MatrixLookup& weights,
     51           statistics::Score& score, size_t nof_inputs)
     52    : SupervisedClassifier(target, &score, nof_inputs),
     53      distance_(distance), matrix_(data),weighted_(true),
     54      weights_(&weights)
    3755  {
    3856  }
    3957
    4058
     59  NCC::~NCC()   
     60  {
     61    if(!weighted_)
     62      if(weights_)
     63        delete weights_;
     64      else
     65        std::cerr << "Error in NCC implementation: probably a constructor"
     66                  << " should be debugged to make sure a unity weight matrix is"
     67                  << " dynamically allocated for all unweighted cases"
     68                  << std::endl;
     69  }
     70
     71
    4172  SupervisedClassifier*
    42   NCC::make_classifier(const DataLookup2D& data,
    43                        const Target& target) const
     73  NCC::make_classifier(const CrossSplitter& cs) const
    4474  {     
    45     const MatrixLookup& tmp = dynamic_cast<const MatrixLookup&>(data);
    46 
    47     NCC* ncc= new NCC(tmp,target,this->distance_);
     75    const MatrixLookup& training_data =
     76      dynamic_cast<const MatrixLookup&>(cs.training_data());
     77    NCC* ncc=0;
     78    if(cs.weighted()) {
     79      ncc= new NCC(training_data,cs.training_target(),this->distance_,
     80                        cs.training_weight());
     81    }
     82    else {
     83      ncc= new NCC(training_data,cs.training_target(),this->distance_);
     84    }
    4885    ncc->score_=this->score_;
    4986    ncc->nof_inputs_=this->nof_inputs_;
     
    6198    size_t rows=matrix_.rows();
    6299    if(score_) {
    63       // Markus: missing values should not be handled here, but a weight matrix
    64       // should be supported throughout the classifier class structure.
    65       gslapi::matrix weight(matrix_.rows(),matrix_.columns(),0.0);
    66       for(size_t i=0; i<matrix_.rows(); i++)
    67         for(size_t j=0; j<matrix_.columns(); j++)
    68           if(!std::isnan(matrix_(i,j)))
    69             weight(i,j)=1.0;
    70       MatrixLookup weightview(weight);
    71       ranker_=new InputRanker(matrix_, target_, *score_, weightview);
     100      ranker_=new InputRanker(matrix_, target_, *score_, *weights_);
    72101      rows=nof_inputs_;
    73102    }
     
    77106      for(size_t j=0; j<matrix_.columns(); j++) {
    78107        double value=matrix_(i,j);
    79         if(score_)
     108        double weight=(*weights_)(i,j);
     109        if(score_) {
    80110          value=matrix_(ranker_->id(i),j);
    81         if(!std::isnan(value)) {
    82           centroids_(i,target_(j)) += value;
    83           nof_in_class(i,target_(j))++;
     111          weight=(*weights_)(ranker_->id(i),j);
     112        }
     113        if(weight) {
     114          centroids_(i,target_(j)) += value*weight;
     115          nof_in_class(i,target_(j))+=weight;
    84116        }
    85117      }
  • trunk/c++_tools/classifier/NCC.h

    r526 r593  
    1919namespace classifier { 
    2020
    21   class Target;
     21  class CrossSplitter;
    2222  class DataLookup1D;
    2323  class DataLookup2D;
    2424  class MatrixLookup;
     25  class Target;
    2526
    2627  ///
     
    3334  public:
    3435    ///
    35     /// Constructor taking the training data, the target vector and
    36     /// the distance measure as input. 
     36    /// Constructor taking the training data, the target vector, and
     37    /// the distance measure as input.
    3738    ///
    3839    NCC(const MatrixLookup&, const Target&, const statistics::Distance&);
     40   
     41    ///
     42    /// Constructor taking the training data, the target vector, the
     43    /// distance measure, and a weight matrix for the training data as
     44    /// input.
     45    ///
     46    NCC(const MatrixLookup&, const Target&, const statistics::Distance&,
     47        const MatrixLookup&);
    3948
     49   
    4050
    4151    ///
    4252    /// Constructor taking the training data, the target vector, the
    4353    /// distance measure, the score used to rank data inputs, and the
    44     /// number of top ranked data inputs to use in the classification.
     54    /// number of top ranked data inputs to use in the classification
     55    /// as input
    4556    ///
    4657    NCC(const MatrixLookup&, const Target&, const statistics::Distance&,
    4758        statistics::Score&, const size_t);
     59
     60    ///
     61    /// Constructor taking the training data, the target vector, the
     62    /// distance measure, a weight matrix for the training data, the
     63    /// score used to rank data inputs, and the number of top ranked
     64    /// data inputs to use in the classification as input
     65    ///
     66    NCC(const MatrixLookup&, const Target&, const statistics::Distance&,
     67        const MatrixLookup&, statistics::Score&, const size_t);
     68
    4869
    4970    virtual ~NCC();
     
    5576
    5677    inline SupervisedClassifier*
    57     make_classifier(const DataLookup2D&, const Target&) const;
     78    make_classifier(const CrossSplitter&) const;
    5879   
    5980    ///
     
    81102    const statistics::Distance& distance_;                 
    82103    const MatrixLookup& matrix_;
    83 
     104    bool weighted_;
     105    const MatrixLookup* weights_;
    84106  };
    85107
  • trunk/c++_tools/classifier/SVM.cc

    r571 r593  
    33#include <c++_tools/classifier/SVM.h>
    44
     5#include <c++_tools/classifier/CrossSplitter.h>
    56#include <c++_tools/classifier/DataLookup2D.h>
    67#include <c++_tools/classifier/InputRanker.h>
     
    111112
    112113
    113   SupervisedClassifier* SVM::make_classifier(const DataLookup2D& data,
    114                                              const Target& target) const
    115   {
     114  SupervisedClassifier* SVM::make_classifier(const CrossSplitter& cs) const
     115  {
     116    // Peter, should check success of dynamic_cast
     117    const KernelLookup& data =
     118      dynamic_cast<const KernelLookup&>(cs.training_data());
     119    const Target& target=cs.training_target();
     120
    116121    assert(data.rows()==data.columns());
    117122    assert(data.columns()==target.size());
    118     // Peter, should check success of dynamic_cast
    119     const KernelLookup& tmp = dynamic_cast<const KernelLookup&>(data);
    120123    SVM* sc;
    121124    if (ranker_)
    122       sc = new SVM(tmp,target,*score_,nof_inputs_);
     125      sc = new SVM(data,target,*score_,nof_inputs_);
    123126    else
    124       sc = new SVM(tmp,target);
     127      sc = new SVM(data,target);
    125128
    126129
  • trunk/c++_tools/classifier/SVM.h

    r592 r593  
    1717namespace theplu {
    1818namespace classifier { 
     19
     20  // forward declarations
     21  class CrossSplitter;
    1922
    2023  // @internal Class keeping track of which samples are support vectors and
     
    136139    ///
    137140    SupervisedClassifier*
    138     make_classifier(const DataLookup2D&, const Target&) const;
     141    make_classifier(const CrossSplitter&) const;
    139142
    140143    ///
  • trunk/c++_tools/classifier/SupervisedClassifier.h

    r592 r593  
    2020namespace classifier { 
    2121
     22  class CrossSplitter;
    2223  class DataLookup2D;
    23   class InputRanker;  class Target;
    24 
     24  class InputRanker;
     25  class Target;
    2526
    2627  ///
     
    5960    ///
    6061    virtual SupervisedClassifier*
    61     make_classifier(const DataLookup2D&, const Target&) const =0;
     62    make_classifier(const CrossSplitter&) const =0;
    6263   
    6364
  • trunk/c++_tools/gslapi/matrix.cc

    r570 r593  
    231231        s << m(i,j);
    232232        if (j<m.columns()-1)
    233           s << "\t";
     233          s << s.fill();
    234234        else if (i<m.rows()-1)
    235235          s << "\n";
  • trunk/c++_tools/gslapi/vector.cc

    r581 r593  
    258258      s << a[j];
    259259      if ( (j+1)<a.size() )
    260         s << " ";
     260        s << s.fill();
    261261    }
    262262
  • trunk/c++_tools/statistics/AveragerPair.h

    r582 r593  
    66#include <c++_tools/statistics/Averager.h>
    77
     8#include <cassert>
    89#include <cmath>
    910#include <utility>
  • trunk/test/data_lookup_1d_test.cc

    r565 r593  
    88#include <iostream>
    99#include <vector>
     10#include <cmath>
    1011
    1112using namespace theplu;
     
    3940  index_even.push_back(4);
    4041  classifier::MatrixLookup m1(gsl_m1,index_odd,index_even);
     42  *error << gsl_m1 << std::endl << '\n';
     43  *error << m1 << std::endl;
    4144  *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n"
    4245         << "                           const size_t, const bool)...";
     
    9396    *error << "Ok" << std::endl;
    9497
     98  *error << "Testing that output from ostream operator for DataLookup1D"
     99         << " can be used by the gslapi::vector istream constructor...";
     100
     101  // First with a vector with no missing values separated by ' '.
     102  std::ofstream my_out("data/tmp_test_datalookup1D.txt");
     103  my_out << v1;
     104  my_out.close();
     105  std::ifstream is("data/tmp_test_datalookup1D.txt");
     106  gslapi::vector v5(is);
     107  is.close();
     108  if (v5.size()!=v1.size() || v5(0)!=v1(0) || v5(1)!=v1(1) ||
     109      v5(2)!=v1(2)) {
     110    ok=false;
     111    *error << "\nERROR\n" << std::endl;
     112  }
     113  unlink("data/tmp_test_datalookup1D.txt");
     114 
     115  // Second with a vector with a missing value separated by '\t'.
     116  gsl_m1(3,0)=std::numeric_limits<double>::quiet_NaN();
     117  classifier::DataLookup1D v6(m1,1, true); 
     118  my_out.open("data/tmp_test_datalookup1D.txt");
     119  char prev=my_out.fill('\t');
     120  my_out << v1;
     121  my_out.fill(prev);
     122  my_out.close();
     123  is.open("data/tmp_test_datalookup1D.txt");
     124  gslapi::vector v7(is,'\t');
     125  is.close();
     126  if (v7.size()!=v6.size() || !std::isnan(v7(1))) {
     127    ok=false;
     128    *error << "\nERROR\n"
     129           << "size: " << v7.size() << " expected " << v6.size() << "\n"
     130           << "v7(1): " << v7(1) << " expected nan\n"
     131           << std::endl;
     132  }
     133  unlink("data/tmp_test_datalookup1D.txt");
     134   
     135
    95136  if (ok)
    96137    *error << "Ok." << std::endl;
  • trunk/test/ncc_test.cc

    r584 r593  
    3434  bool ok = true;
    3535
    36 
    3736  std::ifstream is("data/sorlie_centroids.txt");
    3837  gslapi::matrix data(is,'\t');
     
    4342  is.close();
    4443
    45    
     44  // Generate weight matrix with 0 for missing values and 1 for others.
     45  gslapi::matrix weights(data.rows(),data.columns(),0.0);
     46  for(size_t i=0;i<data.rows();++i)
     47    for(size_t j=0;j<data.columns();++j)
     48      if(!std::isnan(data(i,j)))
     49        weights(i,j)=1.0;
     50     
    4651  classifier::MatrixLookup dataview(data);
     52  classifier::MatrixLookup weightview(weights);
    4753  statistics::PearsonDistance pearson; 
    48   classifier::NCC ncc(dataview,targets,pearson);
     54  classifier::NCC ncc(dataview,targets,pearson,weightview);
    4955  ncc.train();
    5056
     
    7884  *error << igp.score() << std::endl;
    7985
     86  if(ok)
     87    *error << "OK" << std::endl;
     88
    8089
    8190  if (error!=&std::cerr)
    8291    delete error;
    8392
    84   if(ok)
     93  if(ok) 
    8594    return 0;
    8695  return -1;
Note: See TracChangeset for help on using the changeset viewer.