Changeset 1509 for trunk


Ignore:
Timestamp:
Sep 17, 2008, 10:56:30 PM (13 years ago)
Author:
Peter
Message:

fixes #437 and added support for having known issues in test::Suite

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/Suite.cc

    r1487 r1509  
    3535
    3636  Suite::Suite(int argc, char* argv[])
    37     : ok_(true)
     37    : known_issues_(0), ok_(true)
    3838  {
    3939    dev_null_ = new std::ofstream("/dev/null");
     
    128128  int Suite::return_value(void) const
    129129  {
     130    if (known_issues_>1)
     131      out() << known_issues_ << " known issues were detected.\n";
     132    else if (known_issues_==1)
     133      out() << known_issues_ << " known issue was detected.\n";
    130134    if (ok()){
    131135      out() << "Test is ok.\n";
     
    137141
    138142
     143  bool Suite::xadd(bool ok)
     144  {
     145    if (!ok)
     146      ++known_issues_;
     147    else {
     148      err() << " test for a known issue returned true\n";
     149      add(false);
     150    }
     151    return ok;
     152  }
     153
     154
    139155}}}
  • trunk/test/Suite.h

    r1487 r1509  
    100100    bool test_stream(const T&) const;
    101101
     102    /**
     103       This function is similar to add(bool) and could be used to
     104       detect/count known issues. When the issue is fixed, one can
     105       replace the call to xadd(bool) with a call to add(bool).
     106
     107       If \a b is false a counter is incremented, which is used to in
     108       return_value() to generate some printout on how many known
     109       issues were detected.
     110
     111       If \a b is true, ok_ is set to false, becasue the known issue
     112       is no longer an issue and one should replace the call with a
     113       call to add(bool).
     114     */
     115    bool xadd(bool b);
     116
    102117  private:
     118    std::ofstream* dev_null_;
     119    unsigned int known_issues_;
     120    bool ok_;
    103121    bool verbose_;
    104     bool ok_;
    105     std::ofstream* dev_null_;
    106122
    107123  };
  • trunk/test/normalization_test.cc

    r1497 r1509  
    2424#include "yat/normalizer/Centralizer.h"
    2525#include "yat/normalizer/ColumnNormalizer.h"
     26#include "yat/utility/DataIterator.h"
    2627#include "yat/normalizer/QuantileNormalizer.h"
    2728#include "yat/normalizer/Spearman.h"
    2829
     30#include "yat/utility/DataIterator.h"
    2931#include "yat/utility/Matrix.h"
     32#include "yat/utility/MatrixWeighted.h"
     33#include "yat/utility/WeightIterator.h"
    3034
     35#include <limits>
    3136#include <vector>
    3237
     
    3641void test_quantile_normalize(test::Suite&);
    3742void test_spearman(test::Suite&);
     43void test_spearman_weighted(test::Suite&);
    3844
    3945int main(int argc, char* argv[])
     
    124130  correct.push_back(0.25);
    125131  suite.equal_range(vec.begin(), vec.end(), correct.begin());
     132  test_spearman_weighted(suite);
    126133}
     134
     135
     136void test_spearman_weighted(test::Suite& suite)
     137{
     138  suite.err() << "Testing Weighted Spearman\n";
     139  normalizer::Spearman spearman;
     140
     141  suite.err() << "Testing that unity weights reproduces unweighted case\n";
     142  utility::MatrixWeighted m(1,4,0,1);
     143  utility::MatrixWeighted res(m.rows(), m.columns(),3.14,0);
     144  m(0,0).data()=0;
     145  m(0,1).data()=2;
     146  m(0,2).data()=3;
     147  m(0,3).data()=1;
     148  std::vector<double> correct(m.columns());
     149  std::vector<double> correct_w(m.columns(), 1.0);
     150  std::copy(utility::data_iterator(m.begin_row(0)),
     151            utility::data_iterator(m.end_row(0)),
     152            correct.begin());
     153  spearman(correct.begin(), correct.end(), correct.begin());
     154  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
     155
     156  using utility::data_iterator;
     157  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
     158                               data_iterator(res.end_row(0)),
     159                               correct.begin()));
     160  using utility::weight_iterator;
     161  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
     162                               weight_iterator(res.end_row(0)),
     163                               correct_w.begin()));
     164
     165  suite.err() << "Testing rescaling of weights\n";
     166  for (size_t i=0; i<m.columns(); ++i) {
     167    m(0,i).weight() *= 2;
     168    correct_w[i] *= 2;
     169  }   
     170  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
     171  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
     172                               data_iterator(res.end_row(0)),
     173                               correct.begin()));
     174  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
     175                               weight_iterator(res.end_row(0)),
     176                               correct_w.begin()));
     177
     178 
     179  suite.err() << "Testing case with a zero weight\n";
     180  m(0,1).data() = std::numeric_limits<double>::quiet_NaN();
     181  m(0,1).weight() = 0.0;
     182  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
     183  suite.add(suite.equal(res(0,0).data(), 0));
     184  suite.add(suite.equal(res(0,2).data(), 2.0/3));
     185  suite.add(suite.equal(res(0,3).data(), 1.0/3));
     186}
  • trunk/yat/normalizer/Spearman.h

    r1508 r1509  
    2121*/
    2222
     23#include "yat/utility/DataIterator.h"
    2324#include "yat/utility/iterator_traits.h"
    2425#include "yat/utility/sort_index.h"
     26#include "yat/utility/WeightIterator.h"
    2527
     28#include <algorithm>
     29#include <functional>
    2630#include <vector>
    2731
     
    5256       \return result + (last-first)
    5357     */
    54     template<typename InputIterator, typename RandomAccessIterator>
    55     RandomAccessIterator operator()(InputIterator first, InputIterator last,
     58    template<typename ForwardIterator, typename RandomAccessIterator>
     59    RandomAccessIterator operator()(ForwardIterator first, ForwardIterator last,
    5660                                    RandomAccessIterator result) const
    5761    {
    58       typename utility::weighted_iterator_traits<InputIterator>::type tag;
     62      typename utility::weighted_iterator_traits<ForwardIterator>::type tag;
    5963      return normalize(first, last, result, tag);
    6064    }
     
    6367  private:
    6468    // unweighted version
    65     template<typename InputIterator, typename RandomAccessIterator>
    66     RandomAccessIterator normalize(InputIterator first, InputIterator last,
     69    template<typename ForwardIterator, typename RandomAccessIterator>
     70    RandomAccessIterator normalize(ForwardIterator first, ForwardIterator last,
    6771                                   RandomAccessIterator result,
    6872                                   utility::unweighted_iterator_tag) const
     
    7882
    7983    // weighted version
    80     template<typename InputIterator, typename RandomAccessIterator>
    81     RandomAccessIterator normalize(InputIterator first, InputIterator last,
     84    template<typename ForwardIterator, typename RandomAccessIterator>
     85    RandomAccessIterator normalize(ForwardIterator first, ForwardIterator last,
    8286                                   RandomAccessIterator result,
    8387                                   utility::weighted_iterator_tag) const
    8488    {
    85       return result + std::distance(first, last);
     89      std::copy(utility::weight_iterator(first),
     90                utility::weight_iterator(last),
     91                utility::weight_iterator(result));
     92      // set values with w=0 to 0 to avoid problems with NaNs
     93      utility::iterator_traits<ForwardIterator> forward_trait;
     94      for (ForwardIterator i=first; i!=last; ++i)
     95        if (forward_trait.weight(i)==0)
     96          forward_trait.data(i)=0.0;
     97
     98      std::vector<size_t> index(std::distance(first, last));
     99      utility::sort_index(utility::data_iterator(first),
     100                          utility::data_iterator(last), index);
     101      utility::iterator_traits<RandomAccessIterator> trait;
     102      trait.data(result+index[0])=0;
     103      for (size_t i=1; i<index.size(); ++i)
     104        trait.data(result+index[i]) =
     105          trait.data(result+index[i-1]) + trait.weight(result+index[i-1]);
     106      size_t n = std::distance(first, last);
     107      double w_sum = trait.data(result+index.back()) +
     108        trait.weight(result+index.back());
     109      std::transform(utility::data_iterator(result),
     110                     utility::data_iterator(result+n),
     111                     utility::data_iterator(result),
     112                     std::bind2nd(std::divides<double>(), w_sum));
     113      return result + n;
    86114    }
    87115
Note: See TracChangeset for help on using the changeset viewer.