Changeset 1238 for trunk/test/kernel_test.cc
 Timestamp:
 Mar 15, 2008, 7:03:05 PM (15 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test/kernel_test.cc
r1210 r1238 23 23 021111307, USA. 24 24 */ 25 26 #include "Suite.h" 25 27 26 28 #include "yat/utility/Matrix.h" … … 45 47 const classifier::KernelFunction* kf, 46 48 const utility::Matrix& control, const double error_bound, 47 std::ostream* error);49 test::Suite& suite); 48 50 49 51 bool test_SEV(const classifier::MatrixLookup& data, 50 52 const classifier::KernelFunction* kf, 51 53 const utility::Matrix& control, const double error_bound, 52 std::ostream* error);53 54 55 int main( const int argc,constchar* argv[])54 test::Suite& suite); 55 56 57 int main(int argc, char* argv[]) 56 58 57 59 { 58 std::ostream* error; 59 if (argc>1 && argv[1]==std::string("v")) 60 error = &std::cerr; 61 else { 62 error = new std::ofstream("/dev/null"); 63 if (argc>1) 64 std::cout << "kernel_test v : for printing extra information\n"; 65 } 66 *error << "testing kernels" << std::endl; 67 bool ok = true; 60 test::Suite suite(argc, argv); 61 suite.err() << "testing kernels" << std::endl; 68 62 69 63 utility::Matrix data2_core(2,3); … … 81 75 kernel2(1,0)  kernel2(1,1)!=1  kernel2(1,2)  82 76 kernel2(2,0)  kernel2(2,1)  kernel2(2,2)!=1){ 83 ok = false;84 *error<< "Found:\n";77 suite.add(false); 78 suite.err() << "Found:\n"; 85 79 for (size_t i=0; i<3; i++){ 86 80 for (size_t j=0; j<3; j++) 87 *error<< kernel2(i,j) << " ";88 *error<< std::endl;81 suite.err() << kernel2(i,j) << " "; 82 suite.err() << std::endl; 89 83 } 90 *error<< "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl;84 suite.err() << "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl; 91 85 } 92 86 classifier::KernelLookup kv2(kernel2); 93 87 delete kf2; 94 88 95 // Peter, the hardcoded number below should be changed. 96 double error_bound = 1e8; 89 // Peter, the hardcoded number below reflects precision in files 90 // containing kernel matrices calculated in matlab. 91 double error_bound = 1e6; 97 92 std::ifstream is("data/nm_data_centralized.txt"); 98 93 utility::Matrix data_core(is); … … 105 100 is.close(); 106 101 classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); 107 ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound, error)108 & test_SEV(data,kf,kernel_matlab,error_bound, error));102 suite.add(test_MEV(data,kf,kernel_matlab,error_bound, suite) && 103 test_SEV(data,kf,kernel_matlab,error_bound, suite)); 109 104 delete kf; 110 105 … … 113 108 is.close(); 114 109 kf = new classifier::PolynomialKernelFunction(2); 115 ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound, error)116 && test_SEV(data,kf,kernel_matlab2,error_bound, error));110 suite.add(test_MEV(data,kf,kernel_matlab2,error_bound, suite) && 111 test_SEV(data,kf,kernel_matlab2,error_bound, suite)); 117 112 delete kf; 118 113 119 114 // Checking that GaussianKernelFunction. 120 *error<< "Checking GaussianKernelFunction.\n";115 suite.err() << "Checking GaussianKernelFunction.\n"; 121 116 is.open("data/nm_kernel_gaussian.txt"); 122 117 utility::Matrix kernel_gaussian(is); 123 118 is.close(); 124 119 kf = new classifier::GaussianKernelFunction(100); 125 ok = ok && test_MEV(data,kf,kernel_gaussian,error_bound, error);126 ok = ok && test_SEV(data,kf,kernel_gaussian,error_bound, error);120 suite.add(test_MEV(data,kf,kernel_gaussian,error_bound, suite)); 121 suite.add(test_SEV(data,kf,kernel_gaussian,error_bound, suite)); 127 122 delete kf; 128 123 … … 134 129 classifier::Kernel_SEV kernel(data,pkf); 135 130 136 137 if (error!=&std::cerr) 138 delete error; 139 140 if (ok=true) 141 return 0; 142 return 1; 131 return suite.return_value(); 143 132 } 144 133 … … 146 135 const classifier::KernelFunction* kf, 147 136 const utility::Matrix& control, const double error_bound, 148 std::ostream* error)137 test::Suite& suite) 149 138 { 150 *error<< "testing Kernel_MEV" << std::endl;139 suite.err() << "testing Kernel_MEV" << std::endl; 151 140 classifier::Kernel_MEV kernel(data,*kf); 152 141 for(u_int i=0;i<control.rows();i++) 153 142 for(u_int j=0;j<control.columns();j++) 154 if (std::abs(kernel(i,j)control(i,j))>error_bound) 143 if (std::abs(kernel(i,j)control(i,j))>error_bound){ 144 suite.err() << "ERROR: comparing kernel(" << i << "," << j << "): " 145 << kernel(i,j) 146 << "\n and control(" << i << "," << j << "): " 147 << control(i,j) << "\n" 148 << "difference: " << kernel(i,j)control(i,j) << "\n" 149 << "expected less than " << error_bound << "\n"; 155 150 return false; 156 151 } 157 152 // checking view 158 153 std::vector<size_t> index_vec(3); … … 164 159 classifier::KernelLookup kv(kv_raw,index,index); 165 160 if (kv.rows()!=index.size()){ 166 *error << "Error: KernelLookup(kernel, index)\n" << std::endl167 << "Size of KernelLookup is " << kv.rows() << std::endl168 161 suite.err() << "Error: KernelLookup(kernel, index)\n" 162 << "Size of KernelLookup is " << kv.rows() << "\n" 163 << "expected " << index.size() << std::endl; 169 164 170 165 return false; … … 172 167 classifier::KernelLookup kv2(kernel); 173 168 if (kv2.rows()!=kernel.size()){ 174 *error<< "Error: KernelLookup(kernel)\n" << std::endl169 suite.err() << "Error: KernelLookup(kernel)\n" << std::endl 175 170 << "Size of KernelLookup is " << kv.rows() << std::endl 176 171 << "expected " << kernel.size() << std::endl; … … 178 173 return false; 179 174 } 180 181 175 return true; 182 176 } … … 185 179 const classifier::KernelFunction* kf, 186 180 const utility::Matrix& control, const double error_bound, 187 std::ostream* error)181 test::Suite& suite) 188 182 { 189 *error<< "testing Kernel_SEV" << std::endl;183 suite.err() << "testing Kernel_SEV" << std::endl; 190 184 classifier::Kernel_SEV kernel(data,*kf); 191 185 for(u_int i=0;i<control.rows();i++) 192 186 for(u_int j=0;j<control.columns();j++) 193 if (std::abs(kernel(i,j)control(i,j))>error_bound) 187 if (std::abs(kernel(i,j)control(i,j))>error_bound){ 188 suite.err() << "ERROR: comparing kernel(" << i << "," << j << "): " 189 << kernel(i,j) 190 << "\n and control(" << i << "," << j << "): " 191 << control(i,j) << "\n" 192 << "difference: " << kernel(i,j)control(i,j) << "\n" 193 << "expected less than " << error_bound << "\n"; 194 194 return false; 195 } 195 196 196 197 // checking view … … 203 204 classifier::KernelLookup kv(kv_raw,index, index); 204 205 if (kv.rows()!=index.size()){ 205 *error<< "Error: KernelLookup(kernel, index)\n" << std::endl206 suite.err() << "Error: KernelLookup(kernel, index)\n" << std::endl 206 207 << "Size of KernelLookup is " << kv.rows() << std::endl 207 208 << "expected " << index.size() << std::endl; … … 211 212 classifier::KernelLookup kv2(kernel); 212 213 if (kv2.rows()!=kernel.size()){ 213 *error<< "Error: KernelLookup(kernel)\n" << std::endl214 suite.err() << "Error: KernelLookup(kernel)\n" << std::endl 214 215 << "Size of KernelLookup is " << kv.rows() << std::endl 215 216 << "expected " << kernel.size() << std::endl;
Note: See TracChangeset
for help on using the changeset viewer.