
//#include "usingneuralnet.h"
#include "usingpca.h"
#include <omp.h>
#include "pca/TrainPCA.h"
#include <KLib/misc/gnuplot/Gnuplot.h>
#include <KLib/misc/gnuplot/GnuplotSplot.h>
#include <KLib/misc/gnuplot/GnuplotSplotElementLines.h>

#include "pca/KNN.h"
#include "pca/aKNN.h"

#include <vector>

std::vector<std::string> COLORS = {"#000000", "#0000ff", "#00ff00", "#ff0000", "#00ffff"};

std::string getClass(const std::vector<ClassifiedFeature>& nns) {
	std::unordered_map<std::string, int> map;
	for(const ClassifiedFeature& nn : nns) { map[nn.className] += 1; }
	for (auto& it : map) {
		if (it.second > nns.size() * 0.75) {return it.first;}
	}
	return "";
}

struct Stats{
	int match;
	int error;
	int unknown;
	Stats() : match(0), error(0), unknown(0) {;}
	float getSum() {return match+error+unknown;}
};

int main(void) {

	omp_set_dynamic(false);
	omp_set_num_threads(3);

	const int numFeatures = 3;

	std::vector<ClassifiedPattern> patTrain = TrainPCA::getTrainData();
	TrainPCA::Matrices m = TrainPCA::getMatrices(patTrain, numFeatures);

	std::vector<ClassifiedPattern> patTest = TrainPCA::getTestData();

	// construct knn
	aKNN<ClassifiedFeature, 3> knn;
	for (const ClassifiedPattern& pat : patTrain) {
		K::DynColVector<float> vec = m.A1 * K::PCAHelper<float>::toVector(pat.pattern);
		const std::vector<float> arr = {vec(0), vec(1), vec(2)};
		knn.add(ClassifiedFeature(pat.className, arr));
	}
	knn.build();

	K::Gnuplot gp;
	K::GnuplotSplot splot;
	K::GnuplotSplotElementLines lines[5];


	Stats stats;
	int xx = 0;
	for (const ClassifiedPattern& pat : patTest) {

		const int idx = Settings::classToInt(pat.className);
		K::DynColVector<float> vec = m.A1 * K::PCAHelper<float>::toVector(pat.pattern);

		// get KNN's answer
		std::vector<float> arr = {vec(0), vec(1), vec(2)};
		std::vector<ClassifiedFeature> neighbors = knn.get(arr.data(), 10);
		std::string gotClass = getClass(neighbors);

		if		(pat.className == gotClass) {stats.match++;}
		else if	(gotClass == "")			{stats.unknown++;}
		else								{stats.error++;}

		if (++xx % 16 == 0) {
			std::cout << pat.className << " -> " << gotClass << std::endl;
			std::cout << stats.getSum() << ":" << stats.match << ":" << stats.error << ":" << stats.unknown << std::endl;
			std::cout << stats.match/stats.getSum() << ":" << stats.error/stats.getSum() << ":" << stats.unknown/stats.getSum() << std::endl;
		}

		// plot
		K::GnuplotPoint3 p3(vec(0), vec(1), vec(2));
		lines[idx].add(p3);

	}

	for (int i = 0; i < 5; ++i) {lines[i].setColorHex(COLORS[i]);}
	for (int i = 0; i < 5; ++i) {splot.add(&lines[i]);}
	gp.setDebugOutput(false);
	gp.draw(splot);
	gp.flush();

	sleep(10000);







//	std::vector<std::vector<float>> vecs = {vec1, vec2};
//	std::cout << K::PCAHelper<float>::getR(vecs) << std::endl;
//	std::cout << K::PCAHelper<float>::getM(vecs) << std::endl;

//	K::PCAHelper<float>::R r;
//	r.add(vec1); r.add(vec2); std::cout << r.get() << std::endl;


//	Eigen::Vector3f v1; v1 << 1,2,3;
//	Eigen::Vector3f v2; v2 << 3,4,5;
//	std::vector<Eigen::Vector3f> vecs2 = {v1, v2};
//	std::cout << K::PCAHelper<float>::getR(vecs2) << std::endl;
//	std::cout << K::PCAHelper<float>::getM(vecs2) << std::endl;

//	UsingNeuralNet::run();

	//UsingPCA::run();



//	UsingNeuralNet::debugPlot(
//				Practice {
//					PracticeType::KNEEBEND,
//					SensorReader::read("/mnt/firma/kunden/HandyGames/daten/kneebend/kneebend_gl_0_subject_0_right.txt"),
//					{2650, 4750, 6750, 8800, 10800, 12800}
//					//{3500, 5000, 8300, 9900, 11550}
//				}

//	);


	//sleep(1000);

}
