Commit b85907a7 authored by Michael Zbyszyński's avatar Michael Zbyszyński
Browse files

general cleanup, remove namespace macro

parent 02d93040
/*
* machineLearning.cpp
* Created by Michael Zbyszynski on 10 Jan 2016
/**
* @file machineLearning.cpp
* @author Michael Zbyszynski
* @date 10 Jan 2016
* @copyright
* Copyright © 2017 Goldsmiths. All rights reserved.
*/
#include "machineLearning.h"
RAPIDMIX_BEGIN_NAMESPACE
void trainingData2rapidLib (const trainingData &newTrainingData, std::vector<trainingExample> &trainingSet) {
for (int h = 0; h < newTrainingData.trainingSet.size(); ++h) { //Go through every phrase
for (int i = 0; i < newTrainingData.trainingSet[h].elements.size(); ++i) { //...and every element
trainingExample tempExample;
tempExample.input = newTrainingData.trainingSet[h].elements[i].input;
if (newTrainingData.trainingSet[h].elements[i].output.size() > 0) {
tempExample.output = newTrainingData.trainingSet[h].elements[i].output;
} else {
tempExample.output.push_back(double(h));
namespace rapidmix {
void trainingData2rapidLib (const trainingData &newTrainingData, std::vector<trainingExample> &trainingSet) {
for (int h = 0; h < newTrainingData.trainingSet.size(); ++h) { //Go through every phrase
for (int i = 0; i < newTrainingData.trainingSet[h].elements.size(); ++i) { //...and every element
trainingExample tempExample;
tempExample.input = newTrainingData.trainingSet[h].elements[i].input;
if (newTrainingData.trainingSet[h].elements[i].output.size() > 0) {
tempExample.output = newTrainingData.trainingSet[h].elements[i].output;
} else {
tempExample.output.push_back(double(h));
}
trainingSet.push_back(tempExample);
}
trainingSet.push_back(tempExample);
}
};
template<>
bool machineLearning<classification>::train(const trainingData &newTrainingData) {
std::vector<trainingExample> trainingSet;
labels.clear();
for (int i = 0; i < newTrainingData.trainingSet.size(); ++i) {
labels.push_back(newTrainingData.trainingSet[i].label);
}
trainingData2rapidLib(newTrainingData, trainingSet);
return classification::train(trainingSet);
}
};
template<>
bool machineLearning<classification>::train(const trainingData &newTrainingData) {
std::vector<trainingExample> trainingSet;
labels.clear();
for (int i = 0; i < newTrainingData.trainingSet.size(); ++i) {
labels.push_back(newTrainingData.trainingSet[i].label);
template<>
bool machineLearning<regression>::train(const trainingData &newTrainingData) {
std::vector<trainingExample> trainingSet;
trainingData2rapidLib(newTrainingData, trainingSet);
return regression::train(trainingSet);
}
trainingData2rapidLib(newTrainingData, trainingSet);
return classification::train(trainingSet);
}
template<>
bool machineLearning<regression>::train(const trainingData &newTrainingData) {
std::vector<trainingExample> trainingSet;
trainingData2rapidLib(newTrainingData, trainingSet);
return regression::train(trainingSet);
}
template<>
bool machineLearning<seriesClassification>::train(const trainingData &newTrainingData) {
std::vector<trainingSeries> seriesSet;
for (int i = 0; i < newTrainingData.trainingSet.size(); ++i) { //each phrase
trainingSeries tempSeries;
tempSeries.label = newTrainingData.trainingSet[i].label;
for (int j = 0; j < newTrainingData.trainingSet[i].elements.size(); ++j) { //each element
tempSeries.input.push_back(newTrainingData.trainingSet[i].elements[j].input);
template<>
bool machineLearning<seriesClassification>::train(const trainingData &newTrainingData) {
std::vector<trainingSeries> seriesSet;
for (int i = 0; i < newTrainingData.trainingSet.size(); ++i) { //each phrase
trainingSeries tempSeries;
tempSeries.label = newTrainingData.trainingSet[i].label;
for (int j = 0; j < newTrainingData.trainingSet[i].elements.size(); ++j) { //each element
tempSeries.input.push_back(newTrainingData.trainingSet[i].elements[j].input);
}
seriesSet.push_back(tempSeries);
}
seriesSet.push_back(tempSeries);
return seriesClassification::trainLabel(seriesSet);
}
return seriesClassification::trainLabel(seriesSet);
}
template<>
bool machineLearning<rapidGVF>::train(const trainingData &newTrainingData) {
return rapidGVF::train(newTrainingData);
}
///Generic train
template <class MachineLearningModule>
bool machineLearning<MachineLearningModule>::train(const trainingData &newTrainingData) {
return MachineLearningModule::train(newTrainingData);
}
template<>
std::string machineLearning<classification>::run(const std::vector<double> &inputVector, const std::string &label) {
int classIndex = classification::run(inputVector)[0];
return labels[classIndex];
};
template<>
std::string machineLearning<seriesClassification>::run(const std::vector<std::vector<double> > &inputSeries) {
return seriesClassification::runLabel(inputSeries);
}
RAPIDMIX_END_NAMESPACE
template<>
bool machineLearning<rapidGVF>::train(const trainingData &newTrainingData) {
return rapidGVF::train(newTrainingData);
}
///Generic train
template <class MachineLearningModule>
bool machineLearning<MachineLearningModule>::train(const trainingData &newTrainingData) {
return MachineLearningModule::train(newTrainingData);
}
template<>
std::string machineLearning<classification>::run(const std::vector<double> &inputVector, const std::string &label) {
int classIndex = classification::run(inputVector)[0];
return labels[classIndex];
};
template<>
std::string machineLearning<seriesClassification>::run(const std::vector<std::vector<double> > &inputSeries) {
return seriesClassification::runLabel(inputSeries);
}
}
\ No newline at end of file
......@@ -11,7 +11,6 @@
#define machineLearning_h
#include "rapidMix.h"
#include "trainingData.h"
////////// Include all of the machine learning algorithms here
#include "classification.h"
......@@ -20,97 +19,97 @@
#include "rapidXMM.h"
#include "rapidGVF.h"
// forward declaration
namespace rapidmix { class trainingData; }
RAPIDMIX_BEGIN_NAMESPACE
/** @brief A generic ouptut struct to fit all kinds of models */
typedef struct runResults_t {
std::vector<double> likelihoods;
std::vector<double> regression;
std::vector<double> progressions;
std::string likeliest;
} runResults;
/**
* @brief Host class for machine learning algorithms
*/
template <typename MachineLearningModule>
class machineLearning : public MachineLearningModule {
public:
namespace rapidmix {
//* Constructors */
machineLearning() : MachineLearningModule() {};
// forward declaration
class trainingData;
template<class T>
machineLearning(T type) : MachineLearningModule(type) {};
/** @brief A generic ouptut struct to fit all kinds of models */
typedef struct runResults_t {
std::vector<double> likelihoods;
std::vector<double> regression;
std::vector<double> progressions;
std::string likeliest;
} runResults;
/**
* @brief This function becomes specialized in the implementation
* @brief Host class for machine learning algorithms
*/
bool train(const trainingData &newTrainingData);
template <typename MachineLearningModule>
class machineLearning : public MachineLearningModule {
public:
//* Constructors */
machineLearning() : MachineLearningModule() {};
template<class T>
machineLearning(T type) : MachineLearningModule(type) {};
/**
* @brief This function becomes specialized in the implementation
*/
bool train(const trainingData &newTrainingData);
//* this function is not being specialized
std::vector<double> run(const std::vector<double> &inputVector) {
return MachineLearningModule::run(inputVector);
}
// This is a hack while I think about how to do this. -MZ //
std::string run(const std::vector<double> &inputVector, const std::string &label);
//* This is the one I'm using for DTW */
std::string run(const std::vector<std::vector<double> > &inputSeries);
bool reset() {
return MachineLearningModule::reset();
}
private:
MachineLearningModule module;
//this holds string labels
std::vector<std::string> labels; //FIXME: This probably should be pushed down into rapidLib?
std::string getLabel(int value);
};
//* this function is not being specialized
std::vector<double> run(const std::vector<double> &inputVector) {
return MachineLearningModule::run(inputVector);
}
////////// typedefs for calling different algorithms
// This is a hack while I think about how to do this. -MZ //
std::string run(const std::vector<double> &inputVector, const std::string &label);
///// RapidLib
//* This is the one I'm using for DTW */
std::string run(const std::vector<std::vector<double> > &inputSeries);
/** @brief static classification using KNN from RapidLib */
typedef machineLearning<classification> staticClassification;
bool reset() {
return MachineLearningModule::reset();
}
/** @brief static regression using Neural Networks from RapidLib */
typedef machineLearning<regression> staticRegression;
private:
MachineLearningModule module;
/** @brief temporal classification using Dynamic Time Warping from RapidLib */
typedef machineLearning<seriesClassification> dtwTemporalClassification;
//this holds string labels
std::vector<std::string> labels; //FIXME: This probably should be pushed down into rapidLib?
std::string getLabel(int value);
};
////////// typedefs for calling different algorithms
///// RapidLib
/** @brief static classification using KNN from RapidLib */
typedef machineLearning<classification> staticClassification;
/** @brief static regression using Neural Networks from RapidLib */
typedef machineLearning<regression> staticRegression;
/** @brief temporal classification using Dynamic Time Warping from RapidLib */
typedef machineLearning<seriesClassification> dtwTemporalClassification;
///// XMM
/** @brief configuration for XMM based algorithms */
typedef xmmToolConfig xmmConfig;
/** @brief static classification using Gaussian Mixture Models from XMM */
typedef machineLearning<rapidXmmGmm> xmmStaticClassification;
/** @brief static regression using Gaussian Mixture Models from XMM */
typedef machineLearning<rapidXmmGmr> xmmStaticRegression;
/** @brief temporal classification using Hierarchical Hidden Markov Models from XMM */
typedef machineLearning<rapidXmmHmm> xmmTemporalClassification;
/** @brief temporal regression using Hierarchical Hidden Markov Models from XMM */
typedef machineLearning<rapidXmmHmr> xmmTemporalRegression;
///// GVF
/** @brief temporal variation estimation using GVF library */
typedef machineLearning<rapidGVF> gvfTemporalVariation;
RAPIDMIX_END_NAMESPACE
///// XMM
/** @brief configuration for XMM based algorithms */
typedef xmmToolConfig xmmConfig;
/** @brief static classification using Gaussian Mixture Models from XMM */
typedef machineLearning<rapidXmmGmm> xmmStaticClassification;
/** @brief static regression using Gaussian Mixture Models from XMM */
typedef machineLearning<rapidXmmGmr> xmmStaticRegression;
/** @brief temporal classification using Hierarchical Hidden Markov Models from XMM */
typedef machineLearning<rapidXmmHmm> xmmTemporalClassification;
/** @brief temporal regression using Hierarchical Hidden Markov Models from XMM */
typedef machineLearning<rapidXmmHmr> xmmTemporalRegression;
///// GVF
/** @brief temporal variation estimation using GVF library */
typedef machineLearning<rapidGVF> gvfTemporalVariation;
}
#endif
/*
* trainingData.cpp
* Created by Michael Zbyszynski on 2 Feb 2017
* Copyright © 2017 Goldsmiths. All rights reserved.
/**
* @file trainingData.cpp
* @author Michael Zbyszynski
* @date 2 Feb 2017
* @copyright Copyright © 2017 Goldsmiths. All rights reserved.
*/
#include <fstream>
#include "trainingData.h"
RAPIDMIX_BEGIN_NAMESPACE
trainingData::trainingData () {
currentId = 0;
targetPhrase = 0;
};
uint32_t trainingData::assignCurrentId() {
uint32_t returnVal = currentId;
++currentId;
return returnVal;
}
uint32_t trainingData::startRecording() {
phrase tempPhrase = { assignCurrentId(), std::to_string(tempPhrase.uniqueId) }; //TODO: Is this label helpful? -MZ
trainingSet.push_back(tempPhrase);
targetPhrase = int(trainingSet.size() - 1);
return tempPhrase.uniqueId;
};
uint32_t trainingData::startRecording(const std::string &label) {
phrase tempPhrase = { assignCurrentId(), label };
trainingSet.push_back(tempPhrase);
targetPhrase = int(trainingSet.size() - 1);
return tempPhrase.uniqueId;
namespace rapidmix {
};
uint32_t trainingData::addElement(const std::vector<double> &input, const std::vector<double> &output) {
element newElement;
newElement.uniqueId = assignCurrentId();
newElement.input = input;
newElement.output = output;
newElement.timeStamp = NULL;
trainingSet[targetPhrase].elements.push_back(newElement);
return newElement.uniqueId;
}
uint32_t trainingData::addElement(const std::vector<double> &input) {
element newElement;
newElement.uniqueId = assignCurrentId();
newElement.input = input;
newElement.timeStamp = NULL;
trainingSet[targetPhrase].elements.push_back(newElement);
return newElement.uniqueId;
}
void trainingData::stopRecording() {
//TODO: This doesn't do much. -MZ
}
uint32_t trainingData::recordSingleElement(const std::string &label, const std::vector<double> &input) {
startRecording(label);
int returnId = addElement(input);
stopRecording();
return returnId;
};
uint32_t trainingData::recordSingleElement(const std::string &label, const std::vector<double> &input, const std::vector<double> &output) {
startRecording(label);
int returnId = addElement(input, output);
stopRecording();
return returnId;
};
std::vector<std::string> trainingData::getColumnNames() {
return trainingSet[targetPhrase].columnNames;
}
void trainingData::setColumnNames(const std::vector<std::string> &column_names) {
trainingSet[targetPhrase].columnNames = column_names;
}
Json::Value trainingData::parse2json() {
Json::Value root;
Json::Value metadata;
Json::Value trainingSetJSON;
metadata["creator"] = "RAPID-MIX API C++";
metadata["version"] = RAPIDMIX_VERSION;
//Go through all the phrases
for (int i = 0; i < trainingSet.size(); ++i) {
Json::Value thisPhrase;
thisPhrase.append(trainingSet[i].uniqueId);
thisPhrase.append(trainingSet[i].label);
Json::Value column_names;
for (int j = 0; j < trainingSet[i].columnNames.size(); ++j) {
column_names.append(trainingSet[i].columnNames[j]);
}
thisPhrase.append(column_names);
trainingData::trainingData () {
currentId = 0;
targetPhrase = 0;
};
uint32_t trainingData::assignCurrentId() {
uint32_t returnVal = currentId;
++currentId;
return returnVal;
}
uint32_t trainingData::startRecording() {
phrase tempPhrase = { assignCurrentId(), std::to_string(tempPhrase.uniqueId) }; //TODO: Is this label helpful? -MZ
trainingSet.push_back(tempPhrase);
targetPhrase = int(trainingSet.size() - 1);
return tempPhrase.uniqueId;
};
uint32_t trainingData::startRecording(const std::string &label) {
phrase tempPhrase = { assignCurrentId(), label };
trainingSet.push_back(tempPhrase);
targetPhrase = int(trainingSet.size() - 1);
return tempPhrase.uniqueId;
};
uint32_t trainingData::addElement(const std::vector<double> &input, const std::vector<double> &output) {
element newElement;
newElement.uniqueId = assignCurrentId();
newElement.input = input;
newElement.output = output;
newElement.timeStamp = NULL;
trainingSet[targetPhrase].elements.push_back(newElement);
return newElement.uniqueId;
}
uint32_t trainingData::addElement(const std::vector<double> &input) {
element newElement;
newElement.uniqueId = assignCurrentId();
newElement.input = input;
newElement.timeStamp = NULL;
trainingSet[targetPhrase].elements.push_back(newElement);
return newElement.uniqueId;
}
void trainingData::stopRecording() {
//TODO: This doesn't do much. -MZ
}
uint32_t trainingData::recordSingleElement(const std::string &label, const std::vector<double> &input) {
startRecording(label);
int returnId = addElement(input);
stopRecording();
return returnId;
};
uint32_t trainingData::recordSingleElement(const std::string &label, const std::vector<double> &input, const std::vector<double> &output) {
startRecording(label);
int returnId = addElement(input, output);
stopRecording();
return returnId;
};
std::vector<std::string> trainingData::getColumnNames() {
return trainingSet[targetPhrase].columnNames;
}
void trainingData::setColumnNames(const std::vector<std::string> &column_names) {
trainingSet[targetPhrase].columnNames = column_names;
}
Json::Value trainingData::parse2json() {
Json::Value root;
Json::Value metadata;
Json::Value trainingSetJSON;
metadata["creator"] = "RAPID-MIX API C++";
metadata["version"] = RAPIDMIX_VERSION;
Json::Value elements;
for (int j = 0; j < trainingSet[i].elements.size(); ++j) {
Json::Value singleElement;
//Go through all the phrases
for (int i = 0; i < trainingSet.size(); ++i) {
Json::Value thisPhrase;
thisPhrase.append(trainingSet[i].uniqueId);
thisPhrase.append(trainingSet[i].label);
Json::Value elementInput;
for (int k = 0; k < trainingSet[i].elements[j].input.size(); ++k) {
elementInput.append(trainingSet[i].elements[j].input[k]);
Json::Value column_names;
for (int j = 0; j < trainingSet[i].columnNames.size(); ++j) {
column_names.append(trainingSet[i].columnNames[j]);
}
singleElement.append(elementInput);
thisPhrase.append(column_names);
Json::Value elementOutput;
for (int k = 0; k < trainingSet[i].elements[j].output.size(); ++k) {
elementOutput.append(trainingSet[i].elements[j].output[k]);
Json::Value elements;
for (int j = 0; j < trainingSet[i].elements.size(); ++j) {
Json::Value singleElement;
Json::Value elementInput;
for (int k = 0; k < trainingSet[i].elements[j].input.size(); ++k) {
elementInput.append(trainingSet[i].elements[j].input[k]);
}
singleElement.append(elementInput);
Json::Value elementOutput;
for (int k = 0; k < trainingSet[i].elements[j].output.size(); ++k) {
elementOutput.append(trainingSet[i].elements[j].output[k]);
}
singleElement.append(elementOutput);
singleElement.append(trainingSet[i].elements[j].timeStamp);
elements.append(singleElement);
}
singleElement.append(elementOutput);
thisPhrase.append(elements);
singleElement.append(trainingSet[i].elements[j].timeStamp);
elements.append(singleElement);
trainingSetJSON.append(thisPhrase);
}
thisPhrase.append(elements);
trainingSetJSON.append(thisPhrase);
root["metadata"] = metadata;
root["trainingSet"] = trainingSetJSON;
return root;
}
root["metadata"] = metadata;
root["trainingSet"] = trainingSetJSON;
return root;
}