Changeset 536
- Timestamp:
- Mar 3, 2006, 10:19:29 PM (17 years ago)
- Location:
- trunk
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/classifier/DataLookup1D.cc
r527 r536 2 2 3 3 #include <c++_tools/classifier/DataLookup1D.h> 4 5 #include <cassert> 4 6 #include <iostream> 5 7 … … 8 10 9 11 DataLookup1D::DataLookup1D(const DataLookup2D& m, const size_t i, 10 const bool column_vector)11 : column_vector_( column_vector), index_(i), matrix_(&m)12 const bool row_vector) 13 : column_vector_(!row_vector), index_(i), matrix_(&m) 12 14 { 15 assert( !column_vector_ || i<m.columns()); 16 assert( column_vector_ || i<m.rows()); 13 17 } 14 18 -
trunk/lib/classifier/DataLookup1D.h
r533 r536 28 28 /// 29 29 /// @parameter row_vector if true (default) DataLookup1D is 30 /// looking into a columnof DataLookup2D, otherwise looking into31 /// a row.@parameter index which row/column to look into.30 /// looking into a row of DataLookup2D, otherwise looking into 31 /// a column. @parameter index which row/column to look into. 32 32 /// 33 33 DataLookup1D(const DataLookup2D&, const size_t index, … … 43 43 /// 44 44 inline size_t size(void) const 45 { return column_vector_ ? matrix_-> columns() : matrix_->rows(); }45 { return column_vector_ ? matrix_->rows() : matrix_->columns(); } 46 46 47 47 /// … … 49 49 /// 50 50 inline double operator()(const size_t i) const 51 { assert(i<size()); 52 return column_vector_ ? (*matrix_)(index_,i) : (*matrix_)(i,index_); } 51 { 52 assert(i<size()); 53 return column_vector_ ? (*matrix_)(i,index_) : (*matrix_)(index_,i); 54 } 53 55 54 56 /// -
trunk/lib/classifier/DataLookup2D.cc
r534 r536 4 4 5 5 #include <vector> 6 7 #include <cassert> 8 #ifndef NDEBUG 9 #include <algorithm> 10 #endif 11 6 12 7 13 namespace theplu { … … 15 21 assert(row_index_.empty()); 16 22 row_index_.reserve(row.size()); 17 for (size_t i=0; i<row.size(); i++) 23 for (size_t i=0; i<row.size(); i++) { 24 assert(row[i]<m.row_index_.size()); 18 25 row_index_.push_back(m.row_index_[row[i]]); 26 } 19 27 assert(column_index_.empty()); 20 28 column_index_.reserve(col.size()); 21 for (size_t i=0; i<col.size(); i++) 29 for (size_t i=0; i<col.size(); i++) { 30 assert(col[i]<m.column_index_.size()); 22 31 column_index_.push_back(m.column_index_[col[i]]); 32 } 23 33 } 24 34 … … 26 36 27 37 DataLookup2D::DataLookup2D(const DataLookup2D& m, 28 const std::vector<size_t>& index,29 const bool row)38 const std::vector<size_t>& index, 39 const bool row) 30 40 { 31 41 if (row){ 32 42 assert(row_index_.empty()); 33 43 row_index_.reserve(index.size()); 34 for (size_t i=0; i<index.size(); i++) 44 for (size_t i=0; i<index.size(); i++) { 45 assert(index[i]<m.row_index_.size()); 35 46 row_index_.push_back(m.row_index_[index[i]]); 47 } 36 48 column_index_= m.column_index_; 37 49 } … … 39 51 assert(column_index_.empty()); 40 52 column_index_.reserve(index.size()); 41 for (size_t i=0; i<index.size(); i++) 42 column_index_.push_back(m.row_index_[index[i]]); 53 for (size_t i=0; i<index.size(); i++) { 54 column_index_.push_back(m.column_index_[index[i]]); 55 } 43 56 row_index_= m.row_index_; 44 57 } -
trunk/lib/classifier/InputRanker.cc
r527 r536 22 22 statistics::Score& score_object) 23 23 { 24 assert(data.columns()==target.size()); 25 24 26 size_t nof_genes = data.rows(); 25 // size_t nof_samples = data.columns();26 27 27 28 //scoring each input -
trunk/lib/classifier/Kernel.h
r527 r536 52 52 53 53 /// 54 /// @return number columns in Kernel 55 /// 56 inline size_t columns(void) const { return size(); } 57 58 /// 59 /// @return number of rows in Kernel 60 /// 61 inline size_t rows(void) const { return size(); } 62 63 /// 54 64 /// @brief number of samples 55 65 /// -
trunk/lib/classifier/KernelLookup.h
r527 r536 24 24 /// Constructor 25 25 /// 26 KernelLookup(const Kernel&); 26 /// @note If underlying Kernel goes out of scope or is deleted, the 27 /// returned pointer becomes invalid and the result of further use is 28 /// undefined. 29 /// 30 KernelLookup(const Kernel& kernel); 27 31 28 32 /// 29 /// Contructor taking the Kernel to view into, row index vector, 30 /// and column index vector. 33 /// Constructor creating a subKernel. The 34 /// \f$i\f$th row in constructed lookup is identical to row number 35 /// row[i] in matrix. The \f$i\f$th column in constructed lookup 36 /// is identical to column number column[i] in matrix. 37 /// 38 /// @note If @a kernel goes out of scope or is deleted, the 39 /// returned pointer becomes invalid and the result of further use is 40 /// undefined. 31 41 /// 32 42 /// @note For training usage row index shall always be equal to -
trunk/lib/classifier/MatrixLookup.cc
r534 r536 2 2 3 3 #include <c++_tools/classifier/MatrixLookup.h> 4 5 #ifndef NDEBUG 6 #include <algorithm> 7 #endif 4 8 5 9 namespace theplu { … … 18 22 19 23 MatrixLookup::MatrixLookup(const gslapi::matrix& data, 20 const std::vector<size_t>& row,21 const std::vector<size_t>& col)24 const std::vector<size_t>& row, 25 const std::vector<size_t>& col) 22 26 : DataLookup2D(row,col), data_(&data) 23 27 { 28 // Checking that each row index is less than data.rows() 29 assert(row.empty() || 30 *(std::max_element(row.begin(),row.end()))<data.rows()); 31 // Checking that each row index is less than data.column() 32 assert(col.empty() || 33 *(std::max_element(col.begin(),col.end()))<data.columns()); 24 34 } 25 35 … … 32 42 { 33 43 if (row){ 44 // Checking that each row index is less than data.rows() 45 assert(index.empty() || 46 *(std::max_element(index.begin(),index.end()))<data.rows()); 34 47 row_index_=index; 35 48 assert(column_index_.empty()); … … 39 52 } 40 53 else{ 54 // Checking that each column index is less than data.column() 55 assert(index.empty() || 56 *(std::max_element(index.begin(),index.end()))<data.columns()); 41 57 column_index_=index; 42 58 assert(row_index_.empty()); … … 69 85 : DataLookup2D(ml,index,row), data_(ml.data_) 70 86 { 87 // Checking that no index is out of range 88 assert(row_index_.empty() || 89 *(max_element(row_index_.begin(), row_index_.end()))<data_->rows()); 90 assert(column_index_.empty() || 91 *(max_element(column_index_.begin(), column_index_.end()))< 92 data_->columns()); 71 93 } 72 94 -
trunk/lib/classifier/MatrixLookup.h
r534 r536 7 7 #include <c++_tools/gslapi/matrix.h> 8 8 9 #include <cassert> 9 10 10 11 namespace theplu { … … 16 17 /// Interface class for classifier data 17 18 /// 18 19 /// @todo document, especially describe when object becomes invalid, 20 /// as now documentation is conservative. 19 21 class MatrixLookup : public DataLookup2D 20 22 { … … 26 28 /// Constructor creating a lookup into the whole matrix. 27 29 /// 28 explicit MatrixLookup(const gslapi::matrix&); 30 /// @note If @a matrix goes out of scope or is deleted, the 31 /// MatrixLookup becomes invalid and the result of further use is 32 /// undefined. 33 /// 34 explicit MatrixLookup(const gslapi::matrix& matrix); 29 35 30 36 /// … … 34 40 /// is identical to column number column[i] in matrix. 35 41 /// 36 MatrixLookup(const gslapi::matrix&, const std::vector<size_t>& row, 42 /// @note If @a matrix goes out of scope or is deleted, the 43 /// MatrixLookup becomes invalid and the result of further use is 44 /// undefined. 45 /// 46 MatrixLookup(const gslapi::matrix& matrix, const std::vector<size_t>& row, 37 47 const std::vector<size_t>& column); 38 48 … … 46 56 /// by @a index. 47 57 /// 48 MatrixLookup(const gslapi::matrix&, const std::vector<size_t>& index, 58 /// @note If @a matrix goes out of scope or is deleted, the 59 /// MatrixLookup becomes invalid and the result of further use is 60 /// undefined. 61 /// 62 MatrixLookup(const gslapi::matrix& matrix, 63 const std::vector<size_t>& index, 49 64 const bool row_vectors=true); 50 65 … … 52 67 /// @brief Copy constructor. 53 68 /// 69 /// @note If underlying matrix goes out of scope or is deleted, the 70 /// MatrixLookup becomes invalid and the result of further use is 71 /// undefined. 72 /// 54 73 MatrixLookup(const MatrixLookup&); 55 74 … … 58 77 /// as input. 59 78 /// 60 MatrixLookup(const MatrixLookup&, const std::vector<size_t>&, 79 /// @note If underlying matrix goes out of scope or is deleted, the 80 /// MatrixLookup becomes invalid and the result of further use is 81 /// undefined. 82 /// 83 MatrixLookup(const MatrixLookup& matrix, const std::vector<size_t>&, 61 84 const std::vector<size_t>&); 62 85 … … 65 88 /// as input. 66 89 /// 67 MatrixLookup(const MatrixLookup&, const std::vector<size_t>&, 90 /// @note If underlying matrix goes out of scope or is deleted, the 91 /// MatrixLookup becomes invalid and the result of further use is 92 /// undefined. 93 /// 94 MatrixLookup(const MatrixLookup& matrix, const std::vector<size_t>&, 68 95 const bool row=false); 69 96 … … 77 104 /// @return pointer to sub-Lookup of the MatrixLookup 78 105 /// 106 /// @note If underlying matrix goes out of scope or is deleted, the 107 /// returned pointer becomes invalid and the result of further use is 108 /// undefined. 109 /// 79 110 const MatrixLookup* training_data(const std::vector<size_t>& i) const; 80 111 81 112 /// 82 113 /// @return pointer to sub-Lookup of the MatrixLookup 114 /// 115 /// @note If underlying matrix goes out of scope or is deleted, the 116 /// returned pointer becomes invalid and the result of further use is 117 /// undefined. 83 118 /// 84 119 const MatrixLookup* validation_data(const std::vector<size_t>&, … … 90 125 /// 91 126 inline double operator()(const size_t row, const size_t column) const 92 { return (*data_)(row_index_[row], column_index_[column]); } 127 { 128 assert(row<rows()); 129 assert(columns()); 130 return (*data_)(row_index_[row], column_index_[column]); 131 } 93 132 94 133 private: -
trunk/lib/gslapi/matrix.h
r535 r536 336 336 /// 337 337 inline double& operator()(size_t row,size_t column) 338 { return (*gsl_matrix_ptr(m_,row,column)); } 338 { 339 assert(row<rows()); 340 assert(column<columns()); 341 return (*gsl_matrix_ptr(m_,row,column)); 342 } 339 343 340 344 /// … … 343 347 /// 344 348 inline const double& operator()(size_t row,size_t column) const 345 { return (*gsl_matrix_const_ptr(m_,row,column)); } 349 { 350 assert(row<rows()); 351 assert(column<columns()); 352 return (*gsl_matrix_const_ptr(m_,row,column)); 353 } 346 354 347 355 /// -
trunk/lib/gslapi/vector.cc
r535 r536 144 144 { 145 145 v_ = gsl_vector_alloc(data.size()); 146 for (size_t i=0; i<data.size(); i++){ 147 std::cout << "i " << i << std::endl; 146 for (size_t i=0; i<data.size(); i++) 148 147 gsl_vector_set( v_, i, data(i) ); 149 }150 151 148 } 152 149 -
trunk/lib/statistics/Score.h
r532 r536 5 5 6 6 #include <c++_tools/gslapi/vector.h> 7 8 #include <cassert> 9 #ifndef NDEGUG 10 #include <c++_tools/classifier/Target.h> 11 #include <c++_tools/classifier/DataLookup1D.h> 12 #endif 7 13 8 14 namespace theplu { … … 60 66 score(const classifier::Target& target, 61 67 const classifier::DataLookup1D& value) 62 { return score(target,gslapi::vector(value)); } 68 { 69 assert(target.size()==value.size()); 70 return score(target,gslapi::vector(value)); 71 } 63 72 64 73 /// -
trunk/test/inputranker_test.cc
r475 r536 44 44 } 45 45 46 *error << "second test" << std::endl;47 46 if (ir.rank(0)!=1 || ir.rank(1)!=2 || ir.rank(2)!=0){ 48 47 *error << "wrong rank" << std::endl; -
trunk/test/kernel_test.cc
r527 r536 102 102 delete kf; 103 103 104 data_core = gslapi::matrix(1,5); 105 for (size_t i=0; i<data_core.columns(); i++) 106 data_core(0,i)=i; 107 data = classifier::MatrixLookup(data_core); 108 classifier::PolynomialKernelFunction pkf; 109 classifier::Kernel_SEV kernel(data,pkf); 110 111 104 112 if (error!=&std::cerr) 105 113 delete error; -
trunk/test/lookup_test.cc
r534 r536 2 2 3 3 #include <c++_tools/gslapi/matrix.h> 4 #include <c++_tools/classifier/DataLookup1D.h> 5 #include <c++_tools/classifier/KernelLookup.h> 6 #include <c++_tools/classifier/Kernel_SEV.h> 4 7 #include <c++_tools/classifier/MatrixLookup.h> 8 #include <c++_tools/classifier/PolynomialKernelFunction.h> 9 5 10 6 11 #include <fstream> … … 12 17 gslapi::matrix matrix(size_t n); 13 18 19 bool test_data_lookup1D(std::ostream* error); 20 bool test_kernel_lookup(std::ostream* error); 14 21 bool test_matrix_lookup(std::ostream* error); 15 22 … … 27 34 } 28 35 29 *error << "Testing lookup classes" << std::endl; 30 bool ok = true; 31 32 ok = test_matrix_lookup(error); 33 36 *error << "Testing Lookup Classes" << std::endl; 37 bool ok = test_data_lookup1D(error); 38 39 ok = ok && test_matrix_lookup(error); 40 41 ok = ok && test_kernel_lookup(error); 34 42 35 43 if (ok) … … 51 59 } 52 60 61 bool test_data_lookup1D(std::ostream* error) 62 { 63 bool ok =true; 64 *error << "Testing DataLookup1D" << std::endl; 65 gslapi::matrix gsl_m1(matrix(5)); 66 std::vector<size_t> index_odd; 67 index_odd.push_back(1); 68 index_odd.push_back(3); 69 std::vector<size_t> index_even; 70 index_even.push_back(2); 71 index_even.push_back(0); 72 index_even.push_back(4); 73 classifier::MatrixLookup m1(gsl_m1,index_odd,index_even); 74 *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n" 75 << " const size_t, const bool)..."; 76 classifier::DataLookup1D v1(m1,1); 77 if (v1.size()!=m1.columns() || v1(0)!=m1(1,0) || 78 v1(1)!=m1(1,1) ) { 79 ok =false; 80 *error << "\nERROR" << std::endl; 81 *error << "size: " << v1.size() << " expected " << m1.columns() << "\n" 82 << "v1(0): " << v1(0) << " expected " << m1(1,0) << "\n" 83 << "v1(1): " << v1(1) << " expected " << m1(1,1) 84 << std::endl; 85 } 86 else 87 *error << "Ok" << std::endl; 88 89 *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n" 90 << " const size_t, const bool)..."; 91 classifier::DataLookup1D v2(m1,1,false); 92 if (v2.size()!=m1.rows() || v2(0)!=m1(0,1) || v2(1)!=m1(1,1) ) { 93 ok =false; 94 *error << "\nERROR\n" 95 << "size: " << v2.size() << " expected " << m1.rows() << "\n" 96 << "v2(0): " << v2(0) << " expected " << m1(0,1) << "\n" 97 << "v2(1): " << v2(1) << " expected " << m1(1,1) 98 << std::endl; 99 } 100 else 101 *error << "Ok" << std::endl; 102 103 return ok; 104 } 105 106 bool test_kernel_lookup(std::ostream* error) 107 { 108 bool ok =true; 109 *error << "\nTesting KernelLookup" << std::endl; 110 gslapi::matrix data_core(1,5); 111 for (size_t i=0; i<data_core.columns(); i++) 112 data_core(0,i)=i; 113 classifier::MatrixLookup data(data_core); 114 classifier::PolynomialKernelFunction kf; 115 classifier::Kernel_SEV kernel(data,kf); 116 *error << "KernelLookup::KernelLookup(const Kernel&)..."; 117 classifier::KernelLookup k1(kernel); 118 if (k1.rows()!=kernel.rows() || k1.columns()!=kernel.columns()) { 119 ok =false; 120 *error << "ERROR:" << std::endl; 121 *error << "Dimensions do not agree." << std::endl; 122 } 123 for (size_t i=0; i<k1.rows(); i++) 124 for (size_t j=0; j<k1.columns(); j++) 125 if (k1(i,j)!=kernel(i,j)) { 126 ok =false; 127 *error << "ERROR:\n" 128 << "KernelLookup::KernelLookup(const Kernel& data)" 129 << std::endl; 130 *error << "k(" << i << "," << j << ") is " << k1(i,j) 131 << "expected " << kernel(i,j) << std::endl; 132 } 133 134 std::vector<size_t> index_odd; 135 index_odd.push_back(1); 136 index_odd.push_back(3); 137 std::vector<size_t> index_even; 138 index_even.push_back(2); 139 index_even.push_back(0); 140 index_even.push_back(5); 141 142 143 144 return ok; 145 } 146 53 147 bool test_matrix_lookup(std::ostream* error) 54 148 { 55 149 bool ok =true; 56 *error << "Testing MatrixLookup" << std::endl; 150 *error << "\nTesting MatrixLookup" << std::endl; 151 *error << "MatrixLookup::MatrixLookup(const gslapi::matrix& data)..."; 57 152 gslapi::matrix gsl_m1(matrix(2)); 58 153 classifier::MatrixLookup m1(gsl_m1); … … 61 156 m1(1,0)!=gsl_m1(1,0) || m1(1,1)!=gsl_m1(1,1) ) { 62 157 ok =false; 63 *error << "ERROR:\nMatrixLookup::MatrixLookup(const gslapi::matrix& data)" 64 << std::endl; 65 } 66 158 *error << "ERROR:" << std::endl; 159 } 160 else 161 *error << "Ok" << std::endl; 162 163 164 *error << "MatrixLookup::MatrixLookup(const gslapi::matrix&,\n" 165 << " const std::vector<size_t>&,\n" 166 << " const std::vector<size_t>&)..."; 67 167 gslapi::matrix gsl_m2(matrix(4)); 68 168 std::vector<size_t> index_odd; … … 78 178 m2(1,0)!=gsl_m2(3,2) || m2(1,1)!=gsl_m2(3,0) ) { 79 179 ok =false; 80 *error << "ERROR:\n" 81 << "MatrixLookup::MatrixLookup(const gslapi::matrix& data\n" 82 << " const std::vector<size_t>& row,\n" 83 << " const std::vector<size_t>& col)" 84 << std::endl; 85 } 86 180 *error << "ERROR:" << std::endl; 181 } 182 else 183 *error << "Ok" << std::endl; 184 185 *error << "MatrixLookup::MatrixLookup(const gslapi::matrix&,\n" 186 << " const std::vector<size_t>&,\n" 187 << " const bool)..."; 87 188 std::vector<size_t> one(1,1); 88 189 classifier::MatrixLookup m3(gsl_m2,one,true); … … 90 191 m3(0,1)!=gsl_m2(1,1) || m3(0,2)!=gsl_m2(1,2) || m3(0,3)!=gsl_m2(1,3)) { 91 192 ok =false; 92 *error << "ERROR:\n" 93 << "MatrixLookup::MatrixLookup(const gslapi::matrix&,\n" 94 << " const std::vector<size_t>&,\n" 95 << " const bool)" 96 << std::endl; 193 *error << "ERROR:" << std::endl; 97 194 *error << "m3.rows(): " << m3.rows() << " expected 1" << std::endl; 98 195 *error << "m3.columns(): " << m3.columns() << " expected " 99 196 << gsl_m2.columns() << std::endl; 100 197 } 101 198 else 199 *error << "Ok" << std::endl; 200 201 *error << "MatrixLookup::MatrixLookup(const MatrixLookup&)..."; 102 202 classifier::MatrixLookup m4(m2); 103 203 if (m4.rows()!=m2.rows() || m4.columns()!=m2.rows() || m4(0,0)!=m2(0,0) || 104 204 m4(0,1)!=m2(0,1) || m4(1,0)!=m2(1,0) || m4(1,1)!=m2(1,1) ) { 105 205 ok =false; 106 *error << "ERROR:\n" 107 << "MatrixLookup::MatrixLookup(const MatrixLookup& data)" 108 << std::endl; 109 } 110 206 *error << "ERROR:" << std::endl; 207 } 208 else 209 *error << "Ok" << std::endl; 210 211 *error << "MatrixLookup::MatrixLookup(const MatrixLookup& data\n" 212 << " const std::vector<size_t>&,\n" 213 << " const std::vector<size_t>&)..."; 111 214 classifier::MatrixLookup m5(m2,one,one); 112 215 if (m5.rows()!=1 || m5.columns()!=1 || m5(0,0)!=m2(1,1) ) { 113 216 ok =false; 114 *error << "ERROR:\n" 115 << "MatrixLookup::MatrixLookup(const MatrixLookup& data\n" 116 << " const std::vector<size_t>& row,\n" 117 << " const std::vector<size_t>& col)" 118 << std::endl; 217 *error << "ERROR:" << std::endl; 119 218 *error << "MatrixLookup is " << m5(0,0) << " expected " << m2(1,1) 120 219 << std::endl; 121 220 } 122 221 else 222 *error << "Ok" << std::endl; 223 224 *error << "MatrixLookup::MatrixLookup(const MatrixLookup&,\n" 225 << " const std::vector<size_t>&,\n" 226 << " const bool)..."; 123 227 classifier::MatrixLookup m6(m2,one,true); 124 228 if (m6.rows()!=1 || m6.columns()!=m2.columns() || m6(0,0)!=m2(1,0) || 125 229 m6(0,1)!=m2(1,1)) { 126 230 ok =false; 127 *error << "ERROR:\n" 128 << "MatrixLookup::MatrixLookup(const MatrixLookup&,\n" 129 << " const std::vector<size_t>&,\n" 130 << " const bool)" 131 << std::endl; 132 } 133 134 231 *error << "ERROR:" << std::endl; 232 } 233 else 234 *error << "Ok" << std::endl; 235 236 237 *error << "MatrixLookup::training_data(const std::vector<size_t>)..."; 135 238 const classifier::MatrixLookup* TrnData = m2.training_data(one); 136 239 if (TrnData->rows() != m2.rows() || TrnData->columns()!=one.size()){ 137 240 ok =false; 138 *error << "ERROR:\nMatrixLookup::training_data(const std::vector<size_t>)" 139 << std::endl; 140 } 241 *error << "ERROR:" << std::endl; 242 } 243 else 244 *error << "Ok" << std::endl; 141 245 delete TrnData; 142 246 247 *error << "MatrixLookup::validation_data(const std::vector<size_t>,\n" 248 << " const std::vector<size_t>)..."; 143 249 std::vector<size_t> val(23,2); 144 250 const classifier::MatrixLookup* ValData = m2.validation_data(one, val); 145 251 if (ValData->rows() != m2.rows() || TrnData->columns()!=val.size()){ 146 252 ok =false; 147 *error << "ERROR:\n" 148 << "MatrixLookup::validation_data(const std::vector<size_t>,\n" 149 << " const std::vector<size_t>)" 150 << std::endl; 151 } 253 *error << "ERROR:" << std::endl; 254 } 255 else 256 *error << "Ok" << std::endl; 152 257 delete ValData; 153 258
Note: See TracChangeset
for help on using the changeset viewer.