/*
* $RCSfile: CrossValidationTester.java,v $
* $Revision: 1.1 $
* $Date: 2007/07/07 10:29:35 $
* $Author: wojna $
*
* Copyright (C) 2002 - 2007 Logic Group, Institute of Mathematics, Warsaw University
*
* This file is part of Rseslib.
*
* Rseslib is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Rseslib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package rseslib.example;
import java.io.File;
import java.util.Map;
import rseslib.processing.classification.*;
import rseslib.structure.attribute.ArrayHeader;
import rseslib.structure.attribute.Header;
import rseslib.structure.attribute.formats.DataFormatRecognizer;
import rseslib.structure.table.ArrayListDoubleDataTable;
import rseslib.structure.table.DoubleDataTable;
import rseslib.system.Report;
import rseslib.system.output.FileOutput;
import rseslib.system.output.StandardErrorOutput;
import rseslib.system.progress.EmptyProgress;
import rseslib.system.progress.StdOutProgress;
/**
* Cross-validation test for a set of classifiers.
* It is easily convertable to multitest
* and cross-validation multitest (see the comments in the main class method).
*
* @author Arkadiusz Wojna
*/
public class CrossValidationTester
{
/**
* The main method creates the subdirectory results/
* in the directory with a data file and writes the cross-validation results
* to the file with the name corresponding to the data file name.
*
* @param args Two arguments: the path to data file and the path to header file.
* @throws Exception when an error occurs.
*/
public static void main(String[] args) throws Exception
{
// utworzenie pliku logowania
if (args.length <= 0) throw new IllegalArgumentException("For rseslib format 2 should be provided and for rses format 1 argument should be provided");
String resultDirectory = "";
int lastSep = args[0].lastIndexOf('/');
if (args[0].lastIndexOf('\\') > lastSep) lastSep = args[0].lastIndexOf('\\');
if (lastSep >= 0) resultDirectory = args[0].substring(0, lastSep)+File.separator;
resultDirectory = resultDirectory+"results"+File.separator;
File resultDirFile = new File(resultDirectory);
if (!resultDirFile.exists()) resultDirFile.mkdirs();
String dataName = args[0].substring(lastSep+1);
int dotIndex = dataName.lastIndexOf('.');
if (dotIndex >= 0) dataName = dataName.substring(0, dotIndex);
Report.addErrorOutput(new StandardErrorOutput());
Report.addInfoOutput(new FileOutput(new File(resultDirectory+dataName+".txt")));
// analiza argumentow i utworzenie naglowka
DataFormatRecognizer rec = new DataFormatRecognizer();
File dataFile = new File(args[0]);
Header dataHeader = null;
if (rec.isRses2xFormat(dataFile))
{
if (args.length!=1) throw new IllegalArgumentException("For rses format 1 argument should be provided");
dataHeader = new ArrayHeader(dataFile);
}
else
{
if (args.length != 2) throw new IllegalArgumentException("For rseslib format 2 arguments should be provided");
dataHeader = new ArrayHeader(new File(args[args.length-1]));
}
// wczytanie tabeli
DoubleDataTable table = new ArrayListDoubleDataTable(dataFile, dataHeader, new EmptyProgress());
Report.displaynl(table);
// test kroswalidacyjny
CrossValidationTest crossValid = new CrossValidationTest(null, new RseslibClassifiers(null).getClassifierSet());
Map<String,MultipleTestResult> results = crossValid.test(table, new StdOutProgress());
Report.displaynl();
Report.displaynl();
// test wielokrotny
// MultipleRandomSplitTest multiTst = new MultipleRandomSplitTest(null, new BasicMultiClassifier(false));
// Map<String,MultipleTestResult> results = multiTst.test(table, new StdOutProgress());
// test wielokrotny kroswalidacji
// MultipleCrossValidationTest cvMultiTst = new MultipleCrossValidationTest(null, new BasicMultiClassifier(null, false));
// Map<String,MultipleTestResult> results = cvMultiTst.test(table, new StdOutProgress());
// wypisanie wynikow
Report.displayMapWithMultiLines("Classification results", results);
Report.close();
}
}