Changeset 916


Ignore:
Timestamp:
Sep 30, 2007, 2:50:10 AM (16 years ago)
Author:
Peter
Message:

Sorry this commit is a bit to big.

Adding a yat_assert. The yat assert are turned on by providing a
'-DYAT_DEBUG' flag to preprocessor if normal cassert is turned
on. This flag is activated for developers running configure with
--enable-debug. The motivation is that we can use these yat_asserts in
header files and the yat_asserts will be invisible to the normal user
also if he uses C-asserts.

added output operator in DataLookup2D and removed output operator in
MatrixLookup?

Removed template function add_values in Averager and weighted version

Added function to AveragerWeighted? taking iterator to four ranges.

Location:
trunk
Files:
1 added
24 edited

Legend:

Unmodified
Added
Removed
  • trunk/configure.ac

    r865 r916  
    6767if test "${enable_debug}" = "yes" ; then
    6868  CXXFLAGS="$CXXFLAGS -g -O"
     69  CPPFLAGS="$CPPFLAGS -DYAT_DEBUG=1"
    6970else
    7071  CXXFLAGS="$CXXFLAGS -O3"
  • trunk/test/Makefile.am

    r904 r916  
    3232  distance_test \
    3333  ensemble_test feature_selection_test fileutil_test inputranker_test \
    34   iterator_test kernel_test kernel_lookup_test \ 
     34  iterator_test kernel_test kernel_lookup_test \
    3535  knn_test matrix_test matrix_lookup_test \
    3636  nbc_test \
  • trunk/test/averager_test.cc

    r887 r916  
    141141  theplu::yat::utility::vector w(3,1);
    142142  theplu::yat::statistics::AveragerWeighted aw;
    143   aw.add_values(x,w);
     143  add(aw, x.begin(), x.end(), w.begin());
    144144  a.reset();
    145   a.add_values(x);
     145  add(a, x.begin(), x.end());
    146146  const double tol=std::numeric_limits<double>().round_error();
    147147  if (!equal(a,aw,tol,error)){
     
    166166  aw2->reset();
    167167  w*=17;
    168   aw2->add_values(x,w);
     168  add(*aw2, x.begin(), x.end(), w.begin());
    169169  if (!equal(aw,*aw2,tol,error)){
    170170    *error << "error: AveragerWeighted rescaling weights "
  • trunk/test/knn_test.cc

    r903 r916  
    44#include "yat/classifier/MatrixLookupWeighted.h"
    55#include "yat/statistics/euclidean_vector_distance.h"
     6#include "yat/statistics/pearson_vector_distance.h"
    67#include "yat/utility/matrix.h"
    78
     
    4243  utility::matrix weights(data.rows(),data.columns(),0.0);
    4344  utility::nan(data,weights);
    44      
     45
    4546  classifier::MatrixLookupWeighted dataviewweighted(data,weights);
    46   classifier::KNN<statistics::euclidean_vector_distance_tag> knn(dataviewweighted,targets);
     47  classifier::KNN<statistics::pearson_vector_distance_tag> knn(dataviewweighted,targets);
     48  *error << "Training KNN" << std::endl;
    4749  knn.train();
    4850 
  • trunk/test/ncc_test.cc

    r901 r916  
    5656  bool ok = true;
    5757
     58  classifier::MatrixLookupWeighted ml(4,4);
     59  std::vector<std::string> vec(4, "pos");
     60  vec[3]="bjds";
     61  classifier::Target target(vec);
     62  statistics::vector_distance_lookup_weighted_ptr dist=
     63    statistics::vector_distance<statistics::pearson_vector_distance_tag>;
     64  classifier::NCC ncctmp(ml,target,dist);
     65  *error << "training...\n";
     66  ncctmp.train();
     67 
    5868  std::ifstream is("data/sorlie_centroid_data.txt");
    5969  utility::matrix data(is,'\t');
     
    7282    statistics::vector_distance<statistics::pearson_vector_distance_tag>;
    7383  classifier::NCC ncc(dataviewweighted,targets,distance);
     84  *error << "training...\n";
    7485  ncc.train();
    7586
     
    103114  }
    104115
     116  *error << "prediction...\n";
    105117  utility::matrix prediction;
    106118  ncc.predict(dataviewweighted,prediction);
  • trunk/yat/classifier/DataLookup2D.cc

    r865 r916  
    139139  }
    140140
     141  std::ostream& operator<<(std::ostream& s, const DataLookup2D& m)
     142  {
     143    s.setf(std::ios::dec);
     144    s.precision(12);
     145    for(size_t i=0, j=0; i<m.rows(); i++)
     146      for (j=0; j<m.columns(); j++) {
     147        s << m(i,j);
     148        if (j<m.columns()-1)
     149          s << s.fill();
     150        else if (i<m.rows()-1)
     151          s << "\n";
     152      }
     153    return s;
     154  }
     155
    141156}}} // of namespace classifier, yat, and theplu
  • trunk/yat/classifier/DataLookup2D.h

    r865 r916  
    175175  }; 
    176176 
     177  ///
     178  /// The output operator DataLookup2D
     179  ///
     180  std::ostream& operator<< (std::ostream& s, const DataLookup2D&);
     181
    177182}}} // of namespace classifier, yat, and theplu
    178183
  • trunk/yat/classifier/KNN.h

    r904 r916  
    1010#include "yat/statistics/vector_distance.h"
    1111#include "yat/utility/matrix.h"
    12 
     12#include "yat/utility/yat_assert.h"
     13
     14#include <cmath>
    1315#include <map>
    1416
     
    119121
    120122    // matrix with training samples as rows and test samples as columns
    121     utility::matrix* distances = new utility::matrix(data_.columns(),input.columns());
     123    utility::matrix* distances =
     124      new utility::matrix(data_.columns(),input.columns());
    122125     
    123126    if(weighted_data && weighted_input) {
     
    126129        for(size_t j=0; j<input.columns(); j++) {
    127130          classifier::DataLookupWeighted1D test(*weighted_input,j,false);
     131          yat_assert(training.size()==test.size());
    128132          (*distances)(i,j)=statistics::vector_distance(training.begin(),training.end(),test.begin(),typename statistics::vector_distance_traits<Distance>::distance());
     133          yat_assert(!std::isnan((*distances)(i,j)));
    129134        }
    130135      }
     
    186191    utility::matrix* distances=calculate_distances(input);
    187192   
    188     // for each test sample (column in distances) find the closest training samples
    189     prediction.clone(utility::matrix(target_.nof_classes(), input.columns(),0.0));
     193    // for each test sample (column in distances) find the closest
     194    // training samples
     195    prediction.clone(utility::matrix(target_.nof_classes(),input.columns(),0.0));
    190196    for(size_t sample=0;sample<distances->columns();sample++) {
    191197      std::vector<size_t> k_index;
  • trunk/yat/classifier/MatrixLookup.cc

    r865 r916  
    217217
    218218
    219   std::ostream& operator<<(std::ostream& s, const MatrixLookup& m)
    220   {
    221     s.setf(std::ios::dec);
    222     s.precision(12);
    223     for(size_t i=0, j=0; i<m.rows(); i++)
    224       for (j=0; j<m.columns(); j++) {
    225         s << m(i,j);
    226         if (j<m.columns()-1)
    227           s << s.fill();
    228         else if (i<m.rows()-1)
    229           s << "\n";
    230       }
    231     return s;
    232   }
    233 
    234219
    235220
  • trunk/yat/classifier/MatrixLookup.h

    r865 r916  
    284284  }; 
    285285 
    286   ///
    287   /// The output operator MatrixLookup
    288   ///
    289   std::ostream& operator<< (std::ostream& s, const MatrixLookup&);
    290 
    291286}}} // of namespace classifier, yat, and theplu
    292287
  • trunk/yat/classifier/NCC.cc

    r898 r916  
    130130        for(size_t k=0; k<centroids_.columns();k++) {
    131131          DataLookupWeighted1D centroid(weighted_centroids,k,false);
     132
     133          assert(in.size()==centroid.size());
    132134          prediction(k,j)=(*distance_)(in.begin(),in.end(),centroid.begin());
    133135        }
  • trunk/yat/regression/MultiDimensionalWeighted.cc

    r865 r916  
    7474
    7575    statistics::AveragerWeighted aw;
    76     aw.add_values(y,w);
     76    add(aw, y.begin(), y.end(), w.begin());
    7777    s2_ = chisquare_ / (aw.n()-fit_parameters_.size());
    7878    covariance_ *= s2_;
  • trunk/yat/statistics/Averager.cc

    r865 r916  
    2525#include "Averager.h"
    2626
     27#include <cassert>
     28
    2729namespace theplu {
    2830namespace yat {
     
    4648  void Averager::add(double d, u_long n)
    4749  {
     50    assert(!std::isnan(d));
    4851    n_  += n;
    4952    x_  += n*d;
  • trunk/yat/statistics/Averager.h

    r914 r916  
    6767    ///
    6868    void add(double d, u_long n=1);
    69 
    70     ///
    71     /// Adding each value in an array \a v \a n (default=1)
    72     /// number of times. The requirements for the type T of the
    73     /// array \a v are: operator[] returning an element and function
    74     /// size() returning the number of elements.
    75     ///   
    76     ///
    77     template <typename T>
    78     void add_values(const T& v, u_long n=1);
    7969
    8070    /**
     
    205195  }
    206196
    207   // Template implementations
    208   template <typename T>
    209   void  Averager::add_values(const T& v, u_long n)
    210   {
    211     for (size_t i=0; i<v.size(); i++)
    212       add(v[i],n);
    213   }
    214 
    215197}}} // of namespace statistics, yat, and theplu
    216198
  • trunk/yat/statistics/AveragerPairWeighted.cc

    r899 r916  
    4949      return;
    5050    }
     51    assert(!std::isnan(x) && "x is nan");
     52    assert(!std::isnan(y) && "y is nan");
     53    assert(!std::isnan(wx) && "wx is nan");
     54    assert(!std::isnan(wy) && "wy is nan");
    5155    double w=wx*wy;
    5256    x_.add(x,w);
     
    8488  double AveragerPairWeighted::correlation(void) const
    8589  {
    86     return covariance() / ( x_.std()*y_.std() );
     90    return ( x_.variance()>0 && y_.variance()>0 ?
     91             covariance() / sqrt(x_.variance()*y_.variance()) : 0 );
    8792  }
    8893
  • trunk/yat/statistics/AveragerPairWeighted.h

    r915 r916  
    3030
    3131#include "yat/utility/IteratorTraits.h"
     32#include "yat/utility/yat_assert.h"
    3233
    3334#include <cmath>
     
    183184  void add(AveragerPairWeighted& ap, Iter1 first1, Iter1 last1, Iter2 first2)
    184185  {
    185     for ( ; first1 != last1; ++first1, ++first2)
     186    for ( ; first1 != last1; ++first1, ++first2) {
    186187      ap.add(utility::iterator_traits_data(first1),
    187188             utility::iterator_traits_data(first2),
    188189             utility::iterator_traits_weight(first1),
    189190             utility::iterator_traits_weight(first2));
     191    }
    190192  }
    191193
     
    197199                                         const T4& wy)
    198200  {
    199     for (size_t i=0; i<x.size(); i++)
     201    for (size_t i=0; i<x.size(); ++i){
     202      yat_assert(!std::isnan(x[i]));
    200203      add(x[i],y[i],wx[i],wy[i]);
     204    }
    201205  }
    202206
  • trunk/yat/statistics/AveragerWeighted.h

    r915 r916  
    8383
    8484    ///
    85     /// Adding each value in an array \a x and corresponding value in
    86     /// weight array \a w.
    87     ///
    88     /// The requirements for the types T1 and T2 of the arrays \a x
    89     /// and \a w are: operator[] returning an element and function
    90     /// size() returning the number of elements.
    91     ///
    92     template <typename T1, typename T2>
    93     void add_values(const T1& x, const T2& w);
    94 
    95     ///
    9685    /// @brief Calculate the weighted mean
    9786    ///
     
    221210
    222211  /**
    223      \brief adding a ranges of values to AveragerWeighted \a a
     212     \brief adding a range of values to AveragerWeighted \a a
    224213   */
    225214  template <typename Iter>
     
    230219  }
    231220
    232   // Template implementations
    233   template <typename T1, typename T2>
    234   void AveragerWeighted::add_values(const T1& x, const T2& w)
     221  /**
     222     \brief add values from two ranges to AveragerWeighted \a a
     223
     224     Add data from range [first1, last1) with their corresponding
     225     weight in range [first2, first2 + distance(first, last) ).
     226
     227     Requirement: Iter1 and Iter2 are unweighted iterators.
     228   */
     229  template <typename Iter1, typename Iter2>
     230  void add(AveragerWeighted& a, Iter1 first1, Iter1 last1, Iter2 first2)
    235231  {
    236     for (size_t i=0; i<x.size(); i++)
    237       add(x[i],w[i]);
     232    utility::check_iterator_is_unweighted(first1);
     233    utility::check_iterator_is_unweighted(first2);
     234    for ( ; first1 != last1; ++first1, ++first2)
     235      a.add(*first1, *first2);
    238236  }
    239237
    240 ///
    241 /// The AveragerWeighted output operator
    242 ///
    243 ///std::ostream& operator<<(std::ostream& s,const AveragerWeighted&);
    244 
    245238}}} // of namespace statistics, yat, and theplu
    246239
  • trunk/yat/statistics/vector_distance.h

    r909 r916  
    7171    return vector_distance(beg1,end1,beg2, typename statistics::vector_distance_traits<Dist>::distance());
    7272  }
    73 
    7473 
    7574}}} // of namespace statistics, yat, and theplu
  • trunk/yat/utility/Iterator.h

    r884 r916  
    2424  02111-1307, USA.
    2525*/
     26
     27#include "yat_assert.h"
    2628
    2729#include <iterator>
     
    6264       \return element
    6365     */
    64     return_type operator*(void) const { return container_->operator()(index_); }
     66    return_type operator*(void) const
     67    { yat_assert(index_<container_->size());
     68      return container_->operator()(index_); }
    6569
    6670    /**
  • trunk/yat/utility/IteratorTraits.h

    r914 r916  
    6161  };
    6262
     63
     64
     65  /*
     66    struct used to determine if a pair of iterators should be treated
     67    as weighted
     68
     69  template <class T1, class T2>
     70  struct weighted_iterator_traits2 {
     71    typedef weighted_type type;
     72  };
     73
     74  // but specialized to return weighted type for some things
     75  template <class T1, class T2>
     76  struct weighted_iterator_traits2<yat::utility::IteratorWeighted<U,V> > {
     77    typedef weighted_type type;
     78  };
     79  */
     80
    6381  // check (at compile time) that iterator is unweighted.
    6482  inline void check_iterator_is_unweighted(unweighted_type x){}
    6583  template <class Iter>
    6684  void check_iterator_is_unweighted(Iter iter)
    67   {check_iterator_unweighted(typename weighted_iterator_traits<Iter>::type());}
     85  { check_iterator_is_unweighted(typename
     86                                 weighted_iterator_traits<Iter>::type());
     87  }
    6888
    6989
     
    7999  template <class Iter>
    80100  double iterator_traits_data(Iter iter)
    81   { return iterator_traits_data(iter, weighted_iterator_traits<Iter>::type()); }
     101  { return iterator_traits_data(iter, typename
     102                                weighted_iterator_traits<Iter>::type()); }
    82103
    83104  template <class Iter>
     
    101122  template <class Iter>
    102123  double iterator_traits_weight(Iter iter)
    103   { return iterator_traits_data(iter, weighted_iterator_traits<Iter>::type()); }
     124  { return iterator_traits_weight(iter, typename
     125                                  weighted_iterator_traits<Iter>::type()); }
    104126
    105127  template <class Iter>
  • trunk/yat/utility/IteratorWeighted.h

    r909 r916  
    33
    44// $Id$
     5
     6#include "yat_assert.h"
    57
    68#include <iterator>
     
    4143       \return element
    4244     */
    43     return_type operator*(void) const { return container_->operator()(index_); }
     45    return_type operator*(void) const
     46    {
     47      yat_assert(index_<container_->size());
     48      return container_->operator()(index_);
     49    }
    4450
    4551    /**
    4652       \return data
    4753    */
    48     return_type data(void) const { return container_->data(index_); }
     54    return_type data(void) const
     55    { yat_assert(index_<container_->size()); return container_->data(index_); }
    4956
    5057    /**
    5158       \return weight
    5259    */
    53     return_type weight(void) const { return container_->weight(index_); }
     60    return_type weight(void) const
     61    { yat_assert(index_<container_->size());return container_->weight(index_); }
    5462
    5563
  • trunk/yat/utility/Makefile.am

    r908 r916  
    3434  Alignment.h Exception.h FileUtil.h Iterator.h IteratorTraits.h \
    3535  IteratorWeighted.h kNNI.h matrix.h NNI.h  \
    36   PCA.h stl_utility.h SVD.h TypeInfo.h utility.h vector.h WeNNI.h
     36  PCA.h stl_utility.h SVD.h TypeInfo.h utility.h vector.h WeNNI.h \
     37  yat_assert.h
  • trunk/yat/utility/matrix.cc

    r865 r916  
    406406  {
    407407    assert(m_);
    408     if (gsl_matrix_memcpy(m_, other.gsl_matrix_p()))
    409       throw utility::GSL_error("matrix::create_gsl_matrix_copy dimension mis-match");
     408    if (this!=&other)
     409      if (gsl_matrix_memcpy(m_, other.gsl_matrix_p()))
     410        throw utility::GSL_error("matrix::create_gsl_matrix_copy dimension mis-match");
    410411    return *this;
    411412  }
  • trunk/yat/utility/utility.h

    r865 r916  
    6464  bool is_nan(const std::string& s);
    6565
    66 
    67 
    6866}}} // of namespace utility, yat, and theplu
    6967
Note: See TracChangeset for help on using the changeset viewer.