Commit 1e0b6769 authored by mzed's avatar mzed
Browse files

adding documentation to seriesClassification

parent 022647c5
......@@ -52,9 +52,17 @@ public:
*/
void train(const std::vector<trainingExample<T> > &trainingSet);
/** Reset the model to its empty state. */
void reset();
/** Find out how many inputs the model expects
* @return Integer number of intpus
*/
int getNumInputs() const;
/** Find out which inputs in a vector will be used
* @return Vector of ints, specifying input indices.
*/
std::vector<int> getWhichInputs() const;
/** Get the number of nearest neighbours used by the kNN algorithm. */
......@@ -65,6 +73,9 @@ public:
void setK(int newK);
#ifndef EMSCRIPTEN
/** Populate a JSON value with a description of the current model
* @param A JSON value to be populated
*/
void getJSONDescription(Json::Value &currentModel);
#endif
......
......@@ -102,19 +102,19 @@ T seriesClassification<T>::run(const std::vector<std::vector<T>> &inputSeries, s
};
template<typename T>
std::vector<T> seriesClassification<T>::getCosts() {
std::vector<T> seriesClassification<T>::getCosts() const{
return allCosts;
}
template<typename T>
int seriesClassification<T>::getMinLength() {
int seriesClassification<T>::getMinLength() const{
return minLength;
}
template<typename T>
int seriesClassification<T>::getMinLength(std::string label) {
int seriesClassification<T>::getMinLength(std::string label) const {
int labelMinLength = -1;
typename std::map<std::string, minMax<int> >::iterator it = lengthsPerLabel.find(label);
typename std::map<std::string, minMax<int> >::const_iterator it = lengthsPerLabel.find(label);
if (it != lengthsPerLabel.end()) {
labelMinLength = it->second.min;
}
......@@ -122,14 +122,14 @@ int seriesClassification<T>::getMinLength(std::string label) {
}
template<typename T>
int seriesClassification<T>::getMaxLength() {
int seriesClassification<T>::getMaxLength() const {
return maxLength;
}
template<typename T>
int seriesClassification<T>::getMaxLength(std::string label) {
int seriesClassification<T>::getMaxLength(std::string label) const {
int labelMaxLength = -1;
typename std::map<std::string, minMax<int> >::iterator it = lengthsPerLabel.find(label);
typename std::map<std::string, minMax<int> >::const_iterator it = lengthsPerLabel.find(label);
if (it != lengthsPerLabel.end()) {
labelMaxLength = it->second.max;
}
......@@ -137,65 +137,52 @@ int seriesClassification<T>::getMaxLength(std::string label) {
}
template<typename T>
seriesClassification<T>::minMax<T> seriesClassification<T>::calculateCosts(std::string label) {
seriesClassification<T>::minMax<T> seriesClassification<T>::calculateCosts(std::string label) const {
minMax<T> calculatedMinMax;
calculatedMinMax.min = std::numeric_limits<T>::max();
calculatedMinMax.max = std::numeric_limits<T>::min();
int numSeries = 0;
bool foundSeries = false;
std::vector<T> labelCosts;
for (int i = 0; i < (allTrainingSeries.size() - 1); ++i) { //these loops are a little different than the two-label case
if (allTrainingSeries[i].label == label) {
foundSeries = true;
for (int j = (i + 1); j < allTrainingSeries.size(); ++j) {
if (allTrainingSeries[j].label == label) {
numSeries++;
T currentCost = fastDTW<T>::getCost(allTrainingSeries[i].input, allTrainingSeries[j].input, SEARCH_RADIUS);
if (numSeries == 1) {
calculatedMinMax.min = calculatedMinMax.max = currentCost; //first match is both min and max
} else {
if (currentCost < calculatedMinMax.min) {
calculatedMinMax.min = currentCost;
}
if (currentCost > calculatedMinMax.max) {
calculatedMinMax.max = currentCost;
}
}
labelCosts.push_back(fastDTW<T>::getCost(allTrainingSeries[i].input, allTrainingSeries[j].input, SEARCH_RADIUS));
}
}
}
}
if (numSeries == 0) {
if (foundSeries) {
auto minmax_result = std::minmax_element(std::begin(labelCosts), std::end(labelCosts));
calculatedMinMax.min = *minmax_result.first;
calculatedMinMax.max = *minmax_result.second;
} else {
calculatedMinMax.min = calculatedMinMax.max = 0;
}
return calculatedMinMax;
}
template<typename T>
seriesClassification<T>::minMax<T> seriesClassification<T>::calculateCosts(std::string label1, std::string label2) {
seriesClassification<T>::minMax<T> seriesClassification<T>::calculateCosts(std::string label1, std::string label2) const {
minMax<T> calculatedMinMax;
calculatedMinMax.min = std::numeric_limits<T>::max();
calculatedMinMax.max = std::numeric_limits<T>::min();
int numSeries = 0;
bool foundSeries = false;
std::vector<T> labelCosts;
for (int i = 0; i < (allTrainingSeries.size()); ++i) {
if (allTrainingSeries[i].label == label1) {
for (int j = 0; j < allTrainingSeries.size(); ++j) {
if (allTrainingSeries[j].label == label2) {
numSeries++;
T currentCost = fastDTW<T>::getCost(allTrainingSeries[i].input, allTrainingSeries[j].input, SEARCH_RADIUS);
if (numSeries == 1) {
calculatedMinMax.min = calculatedMinMax.max = currentCost; //first match is both min and max
} else {
if (currentCost < calculatedMinMax.min) {
calculatedMinMax.min = currentCost;
}
if (currentCost > calculatedMinMax.max) {
calculatedMinMax.max = currentCost;
}
}
foundSeries = true;
labelCosts.push_back(fastDTW<T>::getCost(allTrainingSeries[i].input, allTrainingSeries[j].input, SEARCH_RADIUS));
}
}
}
}
if (foundSeries) {
auto minmax_result = std::minmax_element(std::begin(labelCosts), std::end(labelCosts));
calculatedMinMax.min = *minmax_result.first;
calculatedMinMax.max = *minmax_result.second;
} else {
calculatedMinMax.min = calculatedMinMax.max = 0;
}
return calculatedMinMax;
}
......
......@@ -15,40 +15,90 @@
#include "fastDTW.h"
#include "trainingExample.h"
/** Class for containing time series classifiers.
*
* Currently only (fast)DTW.
*/
template<typename T>
class seriesClassification {
public:
/** Constructor, no params */
seriesClassification();
~seriesClassification();
/** Train on a specified set of trainingSeries
* @param a vector of training series
*/
bool train(const std::vector<trainingSeries<T> > &seriesSet);
/** Reset model to its initial state, forget all costs and training data*/
void reset();
/** Compare an input series to the stored training series
* @param vector of vectors, either float or double input data
* @return The label of the closest training series.
*/
std::string run(const std::vector<std::vector<T> > &inputSeries);
/** Compare an input series to all of the stored series with a specified label
* @param vector of vectors, either float or double input data
* @param String, label to compare with
* @return The lowest cost match, float or double
*/
T run(const std::vector<std::vector<T> > &inputSeries, std::string label);
std::string run(const std::vector<T> &foo);
std::vector<T> getCosts();
/** Get the costs that were calculated by the run method
* @return A vector of floats or doubles, the cost of matching to each training series
*/
std::vector<T> getCosts() const;
int getMinLength();
int getMinLength(std::string label);
int getMaxLength();
int getMaxLength(std::string label);
/** Get minimum training series length
* @return The minimum length training series
*/
int getMinLength() const;
/** Get minimum training series length from a specified label
* @param The label to check
* @return The minimum length training series of that label
*/
int getMinLength(std::string label) const;
/** Get maximum training series length
* @return The maximum length training series
*/
int getMaxLength() const;
/** Get maximum training series length from a specified label
* @param The label to check
* @return The maximum length training series of that label
*/
int getMaxLength(std::string label) const;
/** Return struct for calculate costs */
template<typename TT>
struct minMax {
TT min;
TT max;
};
minMax<T> calculateCosts(std::string label);
minMax<T> calculateCosts(std::string label1, std::string label2);
/** Calculate minimum and maximum cost between examples in a label.
* @param Label to calculate
* @return minMax struct containing min and max
*/
minMax<T> calculateCosts(std::string label) const;
/** Calculate minimum and maximum cost between examples in one label and examples in a second.
* @param first label to compare
* @param second label to compare
* @return minMax struct containing min and max
*/
minMax<T> calculateCosts(std::string label1, std::string label2) const;
private:
std::vector<trainingSeries<T > > allTrainingSeries;
std::vector<T> allCosts;
int maxLength;
int minLength;
std::map<std::string, minMax<int> > lengthsPerLabel;
......
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