seriesClassification.cpp 3.41 KB
Newer Older
1
2
3
//
//  seriesClassification.cpp
//
mzed's avatar
mzed committed
4
//  Created by Michael Zbyszynski on 08/06/2017.
5
6
7
8
//  Copyright © 2017 Goldsmiths. All rights reserved.
//

#include <vector>
mzed's avatar
mzed committed
9
#include <cassert>
10
11
12
13
14
15
16
17
18
#include "seriesClassification.h"
#ifdef EMSCRIPTEN
#include "emscripten/seriesClassificationEmbindings.h"
#endif

seriesClassification::seriesClassification() {};

seriesClassification::~seriesClassification() {};

19
bool seriesClassification::train(const std::vector<trainingSeries> &seriesSet) {
mzed's avatar
mzed committed
20
    assert(seriesSet.size() > 0);
mzed's avatar
mzed committed
21
22
    reset();
    bool trained = true;
mzed's avatar
mzed committed
23
    allTrainingSeries = seriesSet;
mzed's avatar
mzed committed
24
25
    minLength = maxLength = int(allTrainingSeries[0].input.size());
    for (int i = 0; i < allTrainingSeries.size(); ++i) {
mzed's avatar
mzed committed
26
27
28
29
        //Global
        int newLength = int(allTrainingSeries[i].input.size());
        if (newLength < minLength) {
            minLength = newLength;
mzed's avatar
mzed committed
30
        }
mzed's avatar
mzed committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
        if (newLength > maxLength) {
            maxLength = newLength;
        }
        //Per Label
        std::map<std::string, lengths>::iterator it = lengthsPerLabel.find(allTrainingSeries[i].label);
        if (it != lengthsPerLabel.end()) {
            int newLength = int(allTrainingSeries[i].input.size());
            if (newLength < it->second.min) {
                it->second.min = newLength;
            }
            if (newLength > it->second.max) {
                it->second.max = newLength;
            }
        } else {
            lengths tempLengths;
            tempLengths.min = tempLengths.max = int(allTrainingSeries[i].input.size());
            lengthsPerLabel[allTrainingSeries[i].label] = tempLengths;
mzed's avatar
mzed committed
48
49
        }
    }
mzed's avatar
mzed committed
50
51
52
    return trained;
};

mzed's avatar
mzed committed
53
void seriesClassification::reset() {
mzed's avatar
mzed committed
54
55
    allCosts.clear();
    allTrainingSeries.clear();
mzed's avatar
mzed committed
56
57
58
    lengthsPerLabel.clear();
    minLength = -1;
    maxLength = -1;
59
60
}

61
std::string seriesClassification::run(const std::vector<std::vector<double>> &inputSeries) {
mzed's avatar
mzed committed
62
    fastDTW fastDtw;
mzed's avatar
mzed committed
63
    int searchRadius = 1; //TODO: Define this properly, elsewhere?
64
    int closestSeries = 0;
mzed's avatar
mzed committed
65
    allCosts.clear();
mzed's avatar
mzed committed
66
    double lowestCost = fastDtw.getCost(inputSeries, allTrainingSeries[0].input, searchRadius);
mzed's avatar
mzed committed
67
    allCosts.push_back(lowestCost);
mzed's avatar
mzed committed
68
69
    
    for (int i = 1; i < allTrainingSeries.size(); ++i) {
mzed's avatar
mzed committed
70
        double currentCost = fastDtw.getCost(inputSeries, allTrainingSeries[i].input, searchRadius);
mzed's avatar
mzed committed
71
        allCosts.push_back(currentCost);
72
73
74
75
76
        if (currentCost < lowestCost) {
            lowestCost = currentCost;
            closestSeries = i;
        }
    }
mzed's avatar
mzed committed
77
    return allTrainingSeries[closestSeries].label;
mzed's avatar
mzed committed
78
79
};

mzed's avatar
mzed committed
80

81
82
83
84
std::vector<double> seriesClassification::getCosts() {
    return allCosts;
}

mzed's avatar
mzed committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
int seriesClassification::getMinLength() {
    return minLength;
}

int seriesClassification::getMinLength(std::string label) {
    int labelMinLength = -1;
    std::map<std::string, lengths>::iterator it = lengthsPerLabel.find(label);
    if (it != lengthsPerLabel.end()) {
        labelMinLength = it->second.min;
    }
    return labelMinLength;
}

int seriesClassification::getMaxLength() {
    return maxLength;
}

int seriesClassification::getMaxLength(std::string label) {
    int labelMaxLength = -1;
    std::map<std::string, lengths>::iterator it = lengthsPerLabel.find(label);
    if (it != lengthsPerLabel.end()) {
        labelMaxLength = it->second.max;
    }
    return labelMaxLength;
}

mzed's avatar
mzed committed
111
112
113
114
115
//
//std::vector<double> seriesClassification::getCosts(const std::vector<trainingExample> &trainingSet) {
//    run(trainingSet);
//    return allCosts;
//}