/** 
* @file laplaceTests.cpp
* @brief Laplace Approximation Tests
* @author Erik Rodner
* @date 02/17/2010

*/
#include "core/imagedisplay/ImageDisplay.h"

#include "core/basics/Config.h"
#include "vislearning/baselib/ICETools.h"
#include "vislearning/cbaselib/LabeledSet.h"

#include "vislearning/classifier/kernelclassifier/LikelihoodFunction.h"
#include "vislearning/classifier/kernelclassifier/LaplaceApproximation.h"

#include "vislearning/classifier/kernelclassifier/KCGPLaplace.h"

#include "vislearning/math/kernels/KernelData.h"
#include "vislearning/math/kernels/Kernel.h"
#include "vislearning/math/kernels/KernelRBF.h"
#include "vislearning/math/kernels/KernelExp.h"



using namespace std;
using namespace OBJREC;
using namespace NICE;

/** 
    Laplace Approximation Tests 
*/
int main (int argc, char **argv)
{   
    std::set_terminate(__gnu_cxx::__verbose_terminate_handler);

    Config conf ( argc, argv );
    
	LabeledSetVector train;
	train.read ( conf.gS("main", "set" ), LabeledSetVector::FILEFORMAT_NOINDEX );

	LOOP_ALL_NONCONST ( train )
	{
		EACH_NONCONST (classno, v );
		v[0] /= 300.0;
		v[1] /= 300.0;
	}

 	double rbf_sigma = conf.gD("main", "rbf_sigma", -2.0 );
    KernelRBF kernelFunction ( rbf_sigma, 0.0 );
    //KernelExp kernelFunction ( rbf_sigma, 0.0, 0.0 );

	KernelClassifier *classifier = new KCGPLaplace ( &conf, &kernelFunction );
	classifier->teach( train );

	FloatImage predictions ( 100, 100 );
	for ( uint i = 0 ; i < (uint)predictions.height(); i++ )
		for ( uint j = 0 ; j < (uint)predictions.width(); j++ )
		{
			double yy = i/(double)predictions.height();
			double xx = j/(double)predictions.width();

			Vector vec (2);
			vec[0] = xx;
			vec[1] = yy;

			if ( train.dimension() == 3 )
				vec.append(1.0);

			ClassificationResult r = classifier->classify ( vec );
			predictions.setPixel(j,i,r.scores[1]);
		}



	ColorImage img;
	ICETools::convertToRGB ( predictions, img );
	LOOP_ALL(train)
	{
		EACH(classno,vec);
		int xx = vec[0]*predictions.width();
		int yy = vec[1]*predictions.height();
		img.setPixel(xx,yy,0, 255*classno );
		img.setPixel(xx,yy,1, 255*classno );
		img.setPixel(xx,yy,2, 255*classno );
	}

	showImage ( img );
    
    return 0;
}
