Commit ecda709f authored by mzed's avatar mzed
Browse files

working on dtw. fine in C++, junk in JS

parent 59e5d46a
......@@ -110,9 +110,9 @@
BE325F331DB50BE100F199A8 /* RapidAPI */ = {
isa = PBXGroup;
children = (
BE325F341DB50BE100F199A8 /* main.cpp */,
BE15ECDE1DBA4E1000E88F7C /* dependancies */,
BE325F3B1DB50BFE00F199A8 /* src */,
BE325F341DB50BE100F199A8 /* main.cpp */,
);
path = RapidAPI;
sourceTree = "<group>";
......
......@@ -154,5 +154,54 @@ int main(int argc, const char * argv[]) {
std::cout << "dtw: " << myDtw.process(seriesOne) << std::endl;
std::cout << "dtw: " << myDtw.process(seriesTwo) << std::endl;
seriesClassification myDtw2;
std::vector<trainingExample> tsOne;
tempExample.input = { 1., 5. };
tempExample.output = { 0.0 };
tsOne.push_back(tempExample);
tempExample.input = { 2., 4. };
tempExample.output = { 0.0 };
tsOne.push_back(tempExample);
tempExample.input = { 3., 3. };
tempExample.output = { 0.0 };
tsOne.push_back(tempExample);
tempExample.input = { 4., 2. };
tempExample.output = { 0.0 };
tsOne.push_back(tempExample);
tempExample.input = { 5., 1. };
tempExample.output = { 0.0 };
tsOne.push_back(tempExample);
myDtw2.addTrainingSet(tsOne);
std::vector<trainingExample> tsTwo;
tempExample.input = { 1., 4. };
tempExample.output = { 0.0 };
tsTwo.push_back(tempExample);
tempExample.input = { 2., -3. };
tempExample.output = { 0.0 };
tsTwo.push_back(tempExample);
tempExample.input = { 1., 5. };
tempExample.output = { 0.0 };
tsTwo.push_back(tempExample);
tempExample.input = { -2., 1. };
tempExample.output = { 0.0 };
tsTwo.push_back(tempExample);
myDtw2.addTrainingSet(tsTwo);
std::cout << "dtw2: " << myDtw2.processTrainingSet(tsOne) << std::endl;
std::cout << "dtw2: " << myDtw2.processTrainingSet(tsTwo) << std::endl;
return 0;
}
......@@ -308,6 +308,31 @@ Module.ModelSet.prototype.process = function (input) {
}
return output;
};
////////////////////////////////////////////////
/**
* Creates a series classification object using the constructor from emscripten
* @constructor
* @property {function} Module.SeriesClassificationCpp - constructor from emscripten
*/
Module.SeriesClassification = function () {
this.seriesClassification = new Module.SeriesClassificationCpp(); //TODO implement optional arguments
};
Module.SeriesClassification.prototype = {
addSeries: function (newSeries) {
return this.seriesClassification.addTrainingSet(Module.prepTrainingSet(newSeries));
},
clear: function () {
this.seriesClassification.clear();
},
process: function (inputSeries) {
return this.seriesClassification.processTrainingSet(Module.prepTrainingSet(inputSeries));
}
};
/////////////////////////////////////////////////
......@@ -319,9 +344,9 @@ Module.ModelSet.prototype.process = function (input) {
Module.StreamProcess = function (windowSize) {
if (windowSize) {
this.rapidStream = new Module.RapidStreamCpp(windowSize);
this.rapidStream = new Module.RapidStreamCpp(windowSize);
} else {
this.rapidStream = new Module.RapidStreamCpp();
this.rapidStream = new Module.RapidStreamCpp();
}
};
......@@ -329,37 +354,37 @@ Module.StreamProcess = function (windowSize) {
* TODO: Add documentation -mz
*/
Module.StreamProcess.prototype = {
push: function(input) {
this.rapidStream.pushToWindow(parseFloat(input));
push: function (input) {
this.rapidStream.pushToWindow(parseFloat(input));
},
clear: function() {
clear: function () {
this.rapidStream.clear();
},
velocity: function() {
velocity: function () {
return this.rapidStream.velocity();
},
acceleration: function() {
acceleration: function () {
return this.rapidStream.acceleration();
},
sum: function() {
sum: function () {
return this.rapidStream.sum();
},
mean: function() {
mean: function () {
return this.rapidStream.mean();
},
standardDeviation: function() {
standardDeviation: function () {
return this.rapidStream.standardDeviation();
},
minVelocity: function() {
minVelocity: function () {
return this.rapidStream.minVelocity();
},
maxVelocity: function() {
maxVelocity: function () {
return this.rapidStream.maxVelocity();
},
minAcceleration: function() {
minAcceleration: function () {
return this.rapidStream.minAcceleration();
},
maxAcceleration: function() {
maxAcceleration: function () {
return this.rapidStream.maxAcceleration();
}
......
......@@ -12,7 +12,6 @@
#include "emscripten/seriesClassificationEmbindings.h"
#endif
seriesClassification::seriesClassification() {};
seriesClassification::~seriesClassification() {};
......@@ -24,6 +23,14 @@ bool seriesClassification::addSeries(std::vector<std::vector<double>> newSeries)
return true;
}
bool seriesClassification::addTrainingSet(const std::vector<trainingExample> &trainingSet) {
std::vector<std::vector<double>> newSeries;
for (int i = 0; i < trainingSet.size(); ++i) {
newSeries.push_back(trainingSet[i].input);
}
return addSeries(newSeries);
};
void seriesClassification::clear() {
dtwClassifiers.clear();
}
......@@ -42,4 +49,12 @@ int seriesClassification::process(std::vector<std::vector<double>> inputSeries)
}
return closestSeries;
};
int seriesClassification::processTrainingSet(const std::vector<trainingExample> &trainingSet) {
std::vector<std::vector<double>> newSeries;
for (int i = 0; i < trainingSet.size(); ++i) {
newSeries.push_back(trainingSet[i].input);
}
return process(newSeries);
};
\ No newline at end of file
......@@ -11,6 +11,7 @@
#include <vector>
#include "dtw.h"
#include "trainingExample.h"
class seriesClassification {
......@@ -19,13 +20,16 @@ public:
~seriesClassification();
bool addSeries(std::vector<std::vector<double>> newSeries);
bool addTrainingSet(const std::vector<trainingExample> &trainingSet); //hacky solution for JavaScipt. -mz
void clear();
int process(std::vector<std::vector<double>> inputSeries);
int processTrainingSet(const std::vector<trainingExample> &trainingSet);
private:
std::vector<dtw> dtwClassifiers;
};
#endif
......@@ -15,7 +15,7 @@ var testSet = [
},
{
input: [1, 0],
output: [1 ]
output: [1]
},
{
input: [1, 1],
......@@ -51,7 +51,49 @@ var badSet = [
input: [6],
output: [7]
}
]
];
let testSeries = [
{
input: [1., 5.],
output: [0]
},
{
input: [2., 4.],
output: [0]
},
{
input: [3., 3.],
output: [0]
},
{
input: [4., 2.],
output: [0]
},
{
input: [1., 5.],
output: []
}
];
let testSeries2 = [
{
input: [1., 4.],
output: [0]
},
{
input: [2., -3.],
output: [0]
},
{
input: [1., 5.],
output: [0]
},
{
input: [-2., 1.],
output: [0]
},
];
describe('RapidLib Machine Learning', function () {
describe('Regression', function () {
......@@ -175,7 +217,7 @@ describe('RapidLib Machine Learning', function () {
expect(response3[0]).to.equal(4);
});
});
/*
describe('SVM Classification', function () {
let mySVM = new rapidMix.Classification(rapidMix.ClassificationTypes.SVM);
it('should create a new Classification object', function () {
......@@ -195,7 +237,7 @@ describe('RapidLib Machine Learning', function () {
expect(trained).to.be.true;
});
it('process() should return expected results', function () {
it('process() should return expected results' , function () {
let response1 = mySVM.process([0, 0]);
expect(response1[0]).to.be.equal(0);
let response2 = mySVM.process([0.8789, 0.1574]);
......@@ -208,30 +250,31 @@ describe('RapidLib Machine Learning', function () {
expect(response1[0]).to.equal(0);
let response2 = mySVM.process([1]);
expect(response2[0]).to.equal(0);
});/*
it('should work with multiple outputs', function () {
let mySVM2 = new rapidMix.Classification(rapidMix.ClassificationTypes.SVM);
expect(mySVM2).to.be.an.instanceof(rapidMix.Classification);
expect(mySVM2).to.have.property('modelSet');
let trained = mySVM2.train(testSet2);
expect(trained).to.be.true;
let response1 = mySVM2.process([0, 0]);
expect(response1[0]).to.equal(0);
expect(response1[1]).to.equal(9);
let response2 = mySVM2.process([0.8789, 0.1574]);
expect(response2[0]).to.equal(1);
expect(response2[1]).to.equal(2);
let response3 = mySVM2.process([0.9, 0.7]);
expect(response3[0]).to.equal(2);
expect(response3[1]).to.equal(4);
});*/
});
it('should work with multiple outputs', function () {
let mySVM2 = new rapidMix.Classification(rapidMix.ClassificationTypes.SVM);
expect(mySVM2).to.be.an.instanceof(rapidMix.Classification);
expect(mySVM2).to.have.property('modelSet');
let trained = mySVM2.train(testSet2);
expect(trained).to.be.true;
let response1 = mySVM2.process([0, 0]);
expect(response1[0]).to.equal(0);
expect(response1[1]).to.equal(9);
let response2 = mySVM2.process([0.8789, 0.1574]);
expect(response2[0]).to.equal(1);
expect(response2[1]).to.equal(2);
let response3 = mySVM2.process([0.9, 0.7]);
expect(response3[0]).to.equal(2);
expect(response3[1]).to.equal(4);
});
it('can be initialized', function () {
mySVM.reset();
let response2 = mySVM.process([0.2789, 0.4574]);
expect(response2[0]).to.equal(0); //initialized models return 0
});
});
*/
describe('ModelSet', function () {
let myModelSet = new rapidMix.ModelSet();
......@@ -248,6 +291,33 @@ describe('RapidLib Machine Learning', function () {
});
it('should process input');
});
describe('seriesClassification', function () {
let myDTW = new rapidMix.SeriesClassification();
it('should create a new seriesClassification object', function () {
expect(myDTW).to.be.an.instanceof(rapidMix.SeriesClassification);
});
it('should allow me to add a series', function () {
let added = myDTW.addSeries(testSeries);
expect(added).to.be.true;
});
let myDTW2 = new rapidMix.SeriesClassification();
myDTW2.addSeries(testSeries);
myDTW2.addSeries(testSeries);
myDTW2.addSeries(testSeries2);
myDTW2.addSeries(testSeries);
myDTW2.addSeries(testSeries);
myDTW2.addSeries(testSeries2);
it('should correctly identify series 1', function () {
expect(myDTW2.process(testSeries)).to.equal(0);
});
// it('should correctly identify series 2', function () {
// expect(myDTW2.process(testSeries2)).to.equal(1);
// });
it('should test clear');
});
});
describe('RapidLib Signal Processing', function () {
......@@ -269,9 +339,9 @@ describe('RapidLib Signal Processing', function () {
accelStream.push(0);
accelStream.push(0);
accelStream.push(11);
it('acceleration should be 10', function () {
//expect(accelStream.acceleration()).to.equal(10);
});
it('acceleration should be 10'/*, function () {
expect(accelStream.acceleration()).to.equal(10);
}*/);
describe('when streaming to statStream', function () {
let statStream = new rapidMix.StreamProcess(5);
statStream.push(1.1);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment