added plot generators and current c++ code

renamed some matlab files
This commit is contained in:
2016-01-10 14:43:47 +01:00
parent a5f2ee6f04
commit 844484f0ef
21 changed files with 58697 additions and 180 deletions

View File

@@ -1,198 +1,32 @@
//#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 ClassStats {
int counts[6] = {};
};
struct Stats{
int match;
int error;
int unknown;
Stats() : match(0), error(0), unknown(0) {;}
float getSum() {return match+error+unknown;}
};
std::vector<ClassifiedPattern> removePatterns(const std::vector<ClassifiedPattern>& patAll, const std::string& fileName) {
std::vector<ClassifiedPattern> res;
for (const ClassifiedPattern& pat : patAll) {
if (pat.belongsToFile(fileName)) {
continue;
} else {
res.push_back(pat);
}
}
return res;
}
template <int numFeatures> struct PCA {
aKNN<ClassifiedFeature, numFeatures> knn;
TrainPCA::Matrices m;
};
class Plot {
K::Gnuplot gp;
K::GnuplotSplot splot;
K::GnuplotSplotElementLines lines[5];
public:
Plot() {
for (int i = 0; i < 5; ++i) {lines[i].setColorHex(COLORS[i]);}
for (int i = 0; i < 5; ++i) {splot.add(&lines[i]);}
}
void add(int idx, std::vector<float>& vec) {
K::GnuplotPoint3 p3(vec[0], vec[1], vec[2]);
lines[idx].add(p3);
}
void clear() {
for (int i = 0; i < 5; ++i) {lines[i].clear();}
}
void show() {
gp.setDebugOutput(false);
gp.draw(splot);
gp.flush();
}
};
#include "usingpca.h"
//#include "plotFile.h"
//#include "usingneuralnet.h"
int main(void) {
omp_set_dynamic(false);
omp_set_num_threads(3);
const int numFeatures = 10;
TrainPCA::Settings setTrain;
TrainPCA::Settings setClass; setClass.regionStart_ms += 25;
Data::getAllDataFiles();
Plot p;
// convert all provided datasets into patterns
std::vector<ClassifiedPattern> srcTrain = TrainPCA::getAllData(setTrain);
std::vector<ClassifiedPattern> srcClass = TrainPCA::getAllData(setClass);
std::cout << "windows: " << srcTrain.size() << std::endl;
// error calculation
std::unordered_map<std::string, Stats> stats;
std::unordered_map<std::string, ClassStats> classStats;
//int xx = 0;
std::unordered_map<std::string, PCA<numFeatures>*> pcas;
// try to classify each pattern
for (const ClassifiedPattern& patClassify : srcClass) {
// construct knn search for this leave-one-out ONLY ONCE
if (pcas.find(patClassify.fileName) == pcas.end()) {
std::cout << "constructing PCA for all files but " << patClassify.fileName << std::endl;
// remove all training patterns belonging to the same source file as the to be classifed pattern
std::vector<ClassifiedPattern> srcTrainLOO = removePatterns(srcTrain, patClassify.fileName);
// sanity check (have we removed all patterns?)
int diff = srcTrain.size() - srcTrainLOO.size();
if (diff < 200) {throw 1;}
p.clear();
PCA<numFeatures>* pca = new PCA<numFeatures>();
pcas[patClassify.fileName] = pca;
// train PCA using all pattern without those belonging to the same source file as the to-be-classified one
pca->m = TrainPCA::getMatrices(srcTrainLOO, numFeatures);
// calculate features and add them to the KNN
for (const ClassifiedPattern& pat : srcTrainLOO) {
K::DynColVector<float> vec = pca->m.A1 * K::PCAHelper<float>::toVector(pat.pattern);
std::vector<float> arr;
for (int i = 0; i < numFeatures; ++i) {arr.push_back(vec(i));}
pca->knn.add(ClassifiedFeature(pat.className, arr));
const int idx = Settings::classToInt(pat.className);
p.add(idx, arr);
}
pca->knn.build();
//p.show();
//sleep(100);
}
{
PCA<numFeatures>* pca = pcas[patClassify.fileName];
// calculate features for the to-be-classified pattern
//const int idx = Settings::classToInt(pat.className);
K::DynColVector<float> vec = pca->m.A1 * K::PCAHelper<float>::toVector(patClassify.pattern);
// get KNN's answer
std::vector<float> arr;
for (int i = 0; i < numFeatures; ++i) {arr.push_back(vec(i));}
std::vector<ClassifiedFeature> neighbors = pca->knn.get(arr.data(), 5);
std::string gotClass = getClass(neighbors);
if (patClassify.className == gotClass) {stats["all"].match++; stats[patClassify.fileName].match++; stats[patClassify.className].match++;}
else if (gotClass == "") {stats["all"].unknown++; stats[patClassify.fileName].unknown++; stats[patClassify.className].unknown++;}
else {stats["all"].error++; stats[patClassify.fileName].error++; stats[patClassify.className].error++;}
int gotIdx = (gotClass == "") ? (5) : Settings::classToInt(gotClass);
++classStats[patClassify.className].counts[gotIdx];
}
}
for (auto& it : stats) {
Stats& stats = it.second;
std::cout << "'" <<it.first << "',";
std::cout << stats.match/stats.getSum() << ",";
std::cout << stats.error/stats.getSum() << ",";
std::cout << stats.unknown/stats.getSum();
std::cout << std::endl;
}
for (auto& it : classStats) {
ClassStats& stats = it.second;
std::cout << "'" << it.first << "',";
for (int i = 0; i < 6; ++i) {
std::cout << stats.counts[i] << ",";
}
std::cout << std::endl;
}
runPCA();
//runPlot();
//UsingNeuralNet::run();
/*

50
workspace/plotFile.h Normal file
View File

@@ -0,0 +1,50 @@
#ifndef PLOTFILE_H
#define PLOTFILE_H
#include <KLib/misc/gnuplot/Gnuplot.h>
#include <KLib/misc/gnuplot/GnuplotPlot.h>
#include <KLib/misc/gnuplot/GnuplotPlotElementLines.h>
#include "pca/Data.h"
#include "pca/Settings.h"
void runPlot() {
// read all sensor-values within the given data-file
Recording rec = SensorReader::read("/mnt/firma/kunden/HandyGames/datenOK/jumpingjack/jumpingjack_gl_5_subject_3_left.txt");
// get the value-interpolator
K::Interpolator<uint64_t, SensorAccelerometer> intAccel;
for (const auto& val : rec.accel.values) {intAccel.add(val.ts, val.val);}
intAccel.makeRelative();
K::Gnuplot gp;
K::GnuplotPlot plot;
K::GnuplotPlotElementLines lines[3];
plot.add(&lines[0]); lines[0].setColorHex("#0000ff");
plot.add(&lines[1]); lines[1].setColorHex("#00FF00");
plot.add(&lines[2]); lines[2].setColorHex("#ff0000");
for (int ms = 0; ms < 20000; ms += 50) {
SensorAccelerometer sa = intAccel.get(ms);
lines[0].add(K::GnuplotPoint2(ms, sa.x));
lines[1].add(K::GnuplotPoint2(ms, sa.y));
lines[2].add(K::GnuplotPoint2(ms, sa.z));
}
gp.draw(plot);
std::ofstream out("/tmp/1.dat");
out << gp.getBuffer();
out.close();
gp.flush();
sleep(1000);
}
#endif // PLOTFILE_H

View File

@@ -440,8 +440,8 @@ public:
opt.setElitism(0.07f);
opt.setPopulationSize(100);
opt.setMaxIterations(200);
opt.setMutation(0.20f);
opt.setValRange(0.25);
opt.setMutation(0.40f);
opt.setValRange(0.20);
opt.calculateOptimum(func, vec);
@@ -514,7 +514,7 @@ public:
netLines[i].clear();
}
for (int ms = 0; ms < 20000; ms += 50) { // K::Gnuplot gp;
for (int ms = 0; ms < 12000; ms += 100) { // K::Gnuplot gp;
// K::GnuplotPlot plot;
// K::GnuplotPlotElementLines line[3];
// line[0].setColorHex("#ff0000"); line[0].setTitle("x");
@@ -546,7 +546,7 @@ public:
}
// process every (positive) occurence within the practice
for (int ts = 1000; ts < 10000; ts += 50) {
for (int ts = 0; ts < 12000; ts += 100) {
std::vector<float> values = getNetworkInput(interpol, ts);
std::vector<float> res = net.get(values);
@@ -566,11 +566,32 @@ public:
gp2.draw(plot2);
gp2.flush();
usleep(1000*50);
//usleep(1000*50);
}
gp1.flush();;
gp2.flush();;
std::string fileRaw = "raw_" + std::to_string((int)p.type);
std::string fileNet = "net_" + std::to_string((int)p.type);
gp1 << "set terminal emf size 600,250\n set output '"<<fileRaw<<".emf'\n unset xtics\n unset key\n unset arrow 1\n unset arrow 2\n set format y ' '\n";
gp2 << "set terminal emf size 600,250\n set output '"<<fileNet<<".emf'\n unset xtics\n unset key\n set format y ' '\n";
gp1.draw(plot1);
gp2.draw(plot2);
std::ofstream out1("/tmp/"+fileRaw+".gp"); out1 << gp1.getBuffer(); out1.close();
std::ofstream out2("/tmp/"+fileNet+".gp"); out2 << gp2.getBuffer(); out2.close();
gp1.flush();
gp2.flush();
}
sleep(1000);

View File

@@ -1,6 +1,204 @@
#ifndef USINGPCA_H
#define USINGPCA_H
#include "pca/TrainPCA.h"
#include "pca/KNN.h"
#include "pca/aKNN.h"
std::vector<std::string> COLORS = {"#000000", "#0000ff", "#00ff00", "#ff0000", "#00ffff"};
struct Plot {
K::Gnuplot gp;
K::GnuplotSplot splot;
K::GnuplotSplotElementLines lines[5];
public:
Plot() {
for (int i = 0; i < 5; ++i) {lines[i].setColorHex(COLORS[i]);}
for (int i = 0; i < 5; ++i) {splot.add(&lines[i]);}
}
void add(int idx, std::vector<float>& vec) {
K::GnuplotPoint3 p3(vec[0], vec[1], vec[2]);
lines[idx].add(p3);
}
void clear() {
for (int i = 0; i < 5; ++i) {lines[i].clear();}
}
void show() {
gp.setDebugOutput(false);
gp.draw(splot);
gp.flush();
}
};
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 ClassStats {
int counts[6] = {};
};
struct Stats{
int match;
int error;
int unknown;
Stats() : match(0), error(0), unknown(0) {;}
float getSum() {return match+error+unknown;}
};
std::vector<ClassifiedPattern> removePatterns(const std::vector<ClassifiedPattern>& patAll, const std::string& fileName) {
std::vector<ClassifiedPattern> res;
for (const ClassifiedPattern& pat : patAll) {
if (pat.belongsToFile(fileName)) {
continue;
} else {
res.push_back(pat);
}
}
return res;
}
template <int numFeatures> struct PCA {
aKNN<ClassifiedFeature, numFeatures> knn;
TrainPCA::Matrices m;
};
void runPCA() {
const int numFeatures = 10;
TrainPCA::Settings setTrain;
TrainPCA::Settings setClass; setClass.regionStart_ms += 25;
Plot p;
// convert all provided datasets into patterns
std::vector<ClassifiedPattern> srcTrain = TrainPCA::getAllData(setTrain);
std::vector<ClassifiedPattern> srcClass = TrainPCA::getAllData(setClass);
std::cout << "windows: " << srcTrain.size() << std::endl;
// error calculation
std::unordered_map<std::string, Stats> stats;
std::unordered_map<std::string, ClassStats> classStats;
int xx = 0;
std::unordered_map<std::string, PCA<numFeatures>*> pcas;
// try to classify each pattern
for (const ClassifiedPattern& patClassify : srcClass) {
// construct knn search for this leave-one-out ONLY ONCE
if (pcas.find(patClassify.fileName) == pcas.end()) {
std::cout << "constructing PCA for all files but " << patClassify.fileName << std::endl;
// remove all training patterns belonging to the same source file as the to be classifed pattern
std::vector<ClassifiedPattern> srcTrainLOO = removePatterns(srcTrain, patClassify.fileName);
// sanity check (have we removed all patterns?)
int diff = srcTrain.size() - srcTrainLOO.size();
if (diff < 200) {throw 1;}
p.clear();
PCA<numFeatures>* pca = new PCA<numFeatures>();
pcas[patClassify.fileName] = pca;
// train PCA using all pattern without those belonging to the same source file as the to-be-classified one
pca->m = TrainPCA::getMatrices(srcTrainLOO, numFeatures);
// calculate features and add them to the KNN
for (const ClassifiedPattern& pat : srcTrainLOO) {
K::DynColVector<float> vec = pca->m.A1 * K::PCAHelper<float>::toVector(pat.pattern);
std::vector<float> arr;
for (int i = 0; i < numFeatures; ++i) {arr.push_back(vec(i));}
pca->knn.add(ClassifiedFeature(pat.className, arr));
const int idx = Settings::classToInt(pat.className);
p.add(idx, arr);
}
pca->knn.build();
if (xx == 0) {
++xx;
std::ofstream out("/tmp/pca.gp"); p.gp.draw(p.splot); out << p.gp.getBuffer(); out.close();
}
//p.show();
//sleep(100);
}
{
PCA<numFeatures>* pca = pcas[patClassify.fileName];
// calculate features for the to-be-classified pattern
//const int idx = Settings::classToInt(pat.className);
K::DynColVector<float> vec = pca->m.A1 * K::PCAHelper<float>::toVector(patClassify.pattern);
// get KNN's answer
std::vector<float> arr;
for (int i = 0; i < numFeatures; ++i) {arr.push_back(vec(i));}
std::vector<ClassifiedFeature> neighbors = pca->knn.get(arr.data(), 5);
std::string gotClass = getClass(neighbors);
if (patClassify.className == gotClass) {stats["all"].match++; stats[patClassify.fileName].match++; stats[patClassify.className].match++;}
else if (gotClass == "") {stats["all"].unknown++; stats[patClassify.fileName].unknown++; stats[patClassify.className].unknown++;}
else {stats["all"].error++; stats[patClassify.fileName].error++; stats[patClassify.className].error++;}
int gotIdx = (gotClass == "") ? (5) : Settings::classToInt(gotClass);
++classStats[patClassify.className].counts[gotIdx];
}
}
for (auto& it : stats) {
Stats& stats = it.second;
std::cout << "'" <<it.first << "',";
std::cout << stats.match/stats.getSum() << ",";
std::cout << stats.error/stats.getSum() << ",";
std::cout << stats.unknown/stats.getSum();
std::cout << std::endl;
}
for (auto& it : classStats) {
ClassStats& stats = it.second;
std::cout << "'" << it.first << "',";
for (int i = 0; i < 6; ++i) {
std::cout << stats.counts[i] << ",";
}
std::cout << std::endl;
}
}
/*
#include <vector>
#include "sensors/SensorReader.h"
@@ -8,6 +206,7 @@
#include <eigen3/Eigen/Dense>
enum class PracticeType {
//REST,
JUMPING_JACK,
@@ -108,6 +307,7 @@ public:
}
};
*/
#endif // USINGPCA_H