seriesClassification.cpp 1.65 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
9
10
11
12
13
14
15
16
17
//  Copyright © 2017 Goldsmiths. All rights reserved.
//

#include <vector>
#include "seriesClassification.h"
#ifdef EMSCRIPTEN
#include "emscripten/seriesClassificationEmbindings.h"
#endif

seriesClassification::seriesClassification() {};

seriesClassification::~seriesClassification() {};

18
bool seriesClassification::train(const std::vector<trainingSeries> &seriesSet) {
mzed's avatar
mzed committed
19
20
    reset();
    bool trained = true;
mzed's avatar
mzed committed
21
    allTrainingSeries = seriesSet;
mzed's avatar
mzed committed
22

mzed's avatar
mzed committed
23
24
    //TODO: calculate some size statistics here?
    
mzed's avatar
mzed committed
25
26
27
    return trained;
};

mzed's avatar
mzed committed
28
void seriesClassification::reset() {
mzed's avatar
mzed committed
29
30
    allCosts.clear();
    allTrainingSeries.clear();
31
32
}

33
std::string seriesClassification::run(const std::vector<std::vector<double>> &inputSeries) {
mzed's avatar
mzed committed
34
    fastDTW fastDtw;
mzed's avatar
mzed committed
35
    int searchRadius = 1; //TODO: Define this properly, elsewhere?
36
    int closestSeries = 0;
mzed's avatar
mzed committed
37
    allCosts.clear();
mzed's avatar
mzed committed
38
    double lowestCost = fastDtw.getCost(inputSeries, allTrainingSeries[0].input, searchRadius);
mzed's avatar
mzed committed
39
    allCosts.push_back(lowestCost);
mzed's avatar
mzed committed
40
41
    
    for (int i = 1; i < allTrainingSeries.size(); ++i) {
mzed's avatar
mzed committed
42
        double currentCost = fastDtw.getCost(inputSeries, allTrainingSeries[i].input, searchRadius);
mzed's avatar
mzed committed
43
        allCosts.push_back(currentCost);
44
45
46
47
48
        if (currentCost < lowestCost) {
            lowestCost = currentCost;
            closestSeries = i;
        }
    }
mzed's avatar
mzed committed
49
    return allTrainingSeries[closestSeries].label;
mzed's avatar
mzed committed
50
51
};

mzed's avatar
mzed committed
52

53
54
55
56
std::vector<double> seriesClassification::getCosts() {
    return allCosts;
}

mzed's avatar
mzed committed
57
58
59
60
61
//
//std::vector<double> seriesClassification::getCosts(const std::vector<trainingExample> &trainingSet) {
//    run(trainingSet);
//    return allCosts;
//}