Changeset 1509
- Timestamp:
- Sep 17, 2008, 10:56:30 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/Suite.cc
r1487 r1509 35 35 36 36 Suite::Suite(int argc, char* argv[]) 37 : ok_(true)37 : known_issues_(0), ok_(true) 38 38 { 39 39 dev_null_ = new std::ofstream("/dev/null"); … … 128 128 int Suite::return_value(void) const 129 129 { 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"; 130 134 if (ok()){ 131 135 out() << "Test is ok.\n"; … … 137 141 138 142 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 139 155 }}} -
trunk/test/Suite.h
r1487 r1509 100 100 bool test_stream(const T&) const; 101 101 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 102 117 private: 118 std::ofstream* dev_null_; 119 unsigned int known_issues_; 120 bool ok_; 103 121 bool verbose_; 104 bool ok_;105 std::ofstream* dev_null_;106 122 107 123 }; -
trunk/test/normalization_test.cc
r1497 r1509 24 24 #include "yat/normalizer/Centralizer.h" 25 25 #include "yat/normalizer/ColumnNormalizer.h" 26 #include "yat/utility/DataIterator.h" 26 27 #include "yat/normalizer/QuantileNormalizer.h" 27 28 #include "yat/normalizer/Spearman.h" 28 29 30 #include "yat/utility/DataIterator.h" 29 31 #include "yat/utility/Matrix.h" 32 #include "yat/utility/MatrixWeighted.h" 33 #include "yat/utility/WeightIterator.h" 30 34 35 #include <limits> 31 36 #include <vector> 32 37 … … 36 41 void test_quantile_normalize(test::Suite&); 37 42 void test_spearman(test::Suite&); 43 void test_spearman_weighted(test::Suite&); 38 44 39 45 int main(int argc, char* argv[]) … … 124 130 correct.push_back(0.25); 125 131 suite.equal_range(vec.begin(), vec.end(), correct.begin()); 132 test_spearman_weighted(suite); 126 133 } 134 135 136 void 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 21 21 */ 22 22 23 #include "yat/utility/DataIterator.h" 23 24 #include "yat/utility/iterator_traits.h" 24 25 #include "yat/utility/sort_index.h" 26 #include "yat/utility/WeightIterator.h" 25 27 28 #include <algorithm> 29 #include <functional> 26 30 #include <vector> 27 31 … … 52 56 \return result + (last-first) 53 57 */ 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, 56 60 RandomAccessIterator result) const 57 61 { 58 typename utility::weighted_iterator_traits< InputIterator>::type tag;62 typename utility::weighted_iterator_traits<ForwardIterator>::type tag; 59 63 return normalize(first, last, result, tag); 60 64 } … … 63 67 private: 64 68 // 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, 67 71 RandomAccessIterator result, 68 72 utility::unweighted_iterator_tag) const … … 78 82 79 83 // 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, 82 86 RandomAccessIterator result, 83 87 utility::weighted_iterator_tag) const 84 88 { 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; 86 114 } 87 115
Note: See TracChangeset
for help on using the changeset viewer.