test/crossvalidation.cc

Code
Comments
Other
Rev Date Author Line
116 19 Jul 04 peter 1 // $Id$
116 19 Jul 04 peter 2
675 10 Oct 06 jari 3 /*
831 27 Mar 07 peter 4   Copyright (C) 2004, 2005 Peter Johansson
2121 13 Dec 09 peter 5   Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
2121 13 Dec 09 peter 6   Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
4207 26 Aug 22 peter 7   Copyright (C) 2012, 2022 Peter Johansson
116 19 Jul 04 peter 8
1437 25 Aug 08 peter 9   This file is part of the yat library, http://dev.thep.lu.se/yat
675 10 Oct 06 jari 10
675 10 Oct 06 jari 11   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 12   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 13   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 14   License, or (at your option) any later version.
675 10 Oct 06 jari 15
675 10 Oct 06 jari 16   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 17   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
675 10 Oct 06 jari 19   General Public License for more details.
675 10 Oct 06 jari 20
675 10 Oct 06 jari 21   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 22   along with yat. If not, see <http://www.gnu.org/licenses/>.
675 10 Oct 06 jari 23 */
675 10 Oct 06 jari 24
2881 18 Nov 12 peter 25 #include <config.h>
2881 18 Nov 12 peter 26
675 10 Oct 06 jari 27 #include "yat/classifier/CrossValidationSampler.h"
675 10 Oct 06 jari 28 #include "yat/classifier/MatrixLookup.h"
675 10 Oct 06 jari 29 #include "yat/classifier/Target.h"
675 10 Oct 06 jari 30 #include "yat/utility/matrix.h"
675 10 Oct 06 jari 31
781 05 Mar 07 peter 32 #include <cassert>
116 19 Jul 04 peter 33 #include <cstdlib>
463 16 Dec 05 peter 34 #include <fstream>
116 19 Jul 04 peter 35 #include <iostream>
509 18 Feb 06 peter 36 #include <string>
463 16 Dec 05 peter 37 #include <vector>
116 19 Jul 04 peter 38
551 07 Mar 06 markus 39 // forward declaration
551 07 Mar 06 markus 40 void class_count_test(const std::vector<size_t>&, std::ostream*, bool&);
551 07 Mar 06 markus 41 void sample_count_test(const std::vector<size_t>&, std::ostream*, bool&);
551 07 Mar 06 markus 42
551 07 Mar 06 markus 43
463 16 Dec 05 peter 44 int main(const int argc,const char* argv[])
4200 19 Aug 22 peter 45 {
680 11 Oct 06 jari 46   using namespace theplu::yat;
4200 19 Aug 22 peter 47
463 16 Dec 05 peter 48   std::ostream* error;
463 16 Dec 05 peter 49   if (argc>1 && argv[1]==std::string("-v"))
463 16 Dec 05 peter 50     error = &std::cerr;
463 16 Dec 05 peter 51   else {
463 16 Dec 05 peter 52     error = new std::ofstream("/dev/null");
463 16 Dec 05 peter 53     if (argc>1)
463 16 Dec 05 peter 54       std::cout << "crossvalidation_test -v : for printing extra information\n";
463 16 Dec 05 peter 55   }
482 02 Jan 06 peter 56   *error << "testing crosssplitter" << std::endl;
463 16 Dec 05 peter 57   bool ok = true;
301 30 Apr 05 peter 58
463 16 Dec 05 peter 59   if (error!=&std::cerr)
463 16 Dec 05 peter 60     delete error;
4200 19 Aug 22 peter 61
482 02 Jan 06 peter 62   if (ok)
482 02 Jan 06 peter 63     return 0;
482 02 Jan 06 peter 64   return -1;
116 19 Jul 04 peter 65 }
551 07 Mar 06 markus 66
551 07 Mar 06 markus 67
4200 19 Aug 22 peter 68 void class_count_test(const std::vector<size_t>& class_count,
4200 19 Aug 22 peter 69                       std::ostream* error, bool& ok)
551 07 Mar 06 markus 70 {
551 07 Mar 06 markus 71   for (size_t i=0; i<class_count.size(); i++)
551 07 Mar 06 markus 72     if (class_count[i]==0){
551 07 Mar 06 markus 73       ok = false;
4200 19 Aug 22 peter 74       *error << "ERROR: class " << i << " was not in set."
4200 19 Aug 22 peter 75              << " Expected at least one sample from each class."
551 07 Mar 06 markus 76              << std::endl;
551 07 Mar 06 markus 77     }
551 07 Mar 06 markus 78 }
551 07 Mar 06 markus 79
4200 19 Aug 22 peter 80 void sample_count_test(const std::vector<size_t>& sample_count,
4200 19 Aug 22 peter 81                        std::ostream* error, bool& ok)
551 07 Mar 06 markus 82 {
551 07 Mar 06 markus 83   for (size_t i=0; i<sample_count.size(); i++){
551 07 Mar 06 markus 84     if (sample_count[i]!=1){
551 07 Mar 06 markus 85       ok = false;
4200 19 Aug 22 peter 86       *error << "ERROR: sample " << i << " was in a group " << sample_count[i]
551 07 Mar 06 markus 87              << " times." << " Expected to be 1 time" << std::endl;
551 07 Mar 06 markus 88     }
551 07 Mar 06 markus 89   }
551 07 Mar 06 markus 90 }