/*
* $RCSfile: TrainAndTest.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.ArrayList;
import java.util.Map;
import rseslib.processing.classification.ClassifierSet;
import rseslib.processing.classification.TestResult;
import rseslib.structure.attribute.ArrayHeader;
import rseslib.structure.attribute.Header;
import rseslib.structure.attribute.formats.DataFormatRecognizer;
import rseslib.structure.data.DoubleData;
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;
/**
* Single test of classifiers on data
* split into a training and a test table.
* If only one data file is provided,
* the method splits data randomly
* with the ratio of the training data size
* to the test data size 2 to 1.
*
* @author Arkadiusz Wojna
*/
public class TrainAndTest
{
/**
* Main testing method.
*
* @param args Parameters of the method: path to training data,
* path to test data file (optionally) and path to data header.
* @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 or 3 arguments should be provided and for rses format 1 or 2 arguments 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;
boolean oneFile = true;
if (rec.isRses2xFormat(dataFile))
{
if (args.length > 2) throw new IllegalArgumentException("For rses format 1 or 2 arguments should be provided");
dataHeader = new ArrayHeader(dataFile);
if (args.length==2) oneFile = false;
}
else
{
if (args.length != 2 && args.length != 3) throw new IllegalArgumentException("For rseslib format 2 or 3 arguments should be provided");
dataHeader = new ArrayHeader(new File(args[args.length-1]));
if (args.length==3) oneFile = false;
}
// wczytanie tabel i podzial na treningowa i testowa czesc w przypadku pojedynczej tabeli
DoubleDataTable trainTable = new ArrayListDoubleDataTable(dataFile, dataHeader, new EmptyProgress());
DoubleDataTable testTable;
if (oneFile)
{
ArrayList<DoubleData>[] parts = trainTable.randomSplit(2, 1);
trainTable = new ArrayListDoubleDataTable(parts[0]);
testTable = new ArrayListDoubleDataTable(parts[1]);
}
else
{
testTable = new ArrayListDoubleDataTable(new File(args[1]), dataHeader, new EmptyProgress());
}
ClassifierSet tester = new RseslibClassifiers(null).getClassifierSet();
// uczenie klasyfikatorow na tabeli treningowej
if (oneFile) Report.displaynl(args[0]+" (training part)");
else Report.displaynl(args[0]);
Report.displaynl(trainTable);
tester.train(trainTable, new StdOutProgress());
Report.displaynl();
// klasyfikacja tabeli testowej
if (oneFile) Report.displaynl(args[0]+" (testing part)");
else Report.displaynl(args[1]);
Report.displaynl(testTable);
Map<String,TestResult> results = tester.classify(testTable, new StdOutProgress());
Report.displaynl();
// wypisanie wynikow
Report.displayMapWithMultiLines("Classification results for test table", results);
Report.close();
}
}