summaryrefslogtreecommitdiffstats
path: root/ml/SamplesBuffer.cc
blob: 359b60c232578983d27651e610a8555fbdcb447a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// SPDX-License-Identifier: GPL-3.0-or-later
//
#include "SamplesBuffer.h"

#include <fstream>
#include <sstream>
#include <string>

void Sample::print(std::ostream &OS) const {
    for (size_t Idx = 0; Idx != NumDims - 1; Idx++)
        OS << CNs[Idx] << ", ";

    OS << CNs[NumDims - 1];
}

void SamplesBuffer::print(std::ostream &OS) const {
    for (size_t Idx = Preprocessed ? (DiffN + (SmoothN - 1) + (LagN)) : 0;
         Idx != NumSamples; Idx++) {
        Sample S = Preprocessed ? getPreprocessedSample(Idx) : getSample(Idx);
        OS << S << std::endl;
    }
}

std::vector<Sample> SamplesBuffer::getPreprocessedSamples() const {
    std::vector<Sample> V;

    for (size_t Idx = Preprocessed ? (DiffN + (SmoothN - 1) + (LagN)) : 0;
         Idx != NumSamples; Idx++) {
        Sample S = Preprocessed ? getPreprocessedSample(Idx) : getSample(Idx);
        V.push_back(S);
    }

    return V;
}

void SamplesBuffer::diffSamples() {
    // Panda's DataFrame default behaviour is to subtract each element from
    // itself. For us `DiffN = 0` means "disable diff-ing" when preprocessing
    // the samples buffer. This deviation will make it easier for us to test
    // the KMeans implementation.
    if (DiffN == 0)
        return;

    for (size_t Idx = 0; Idx != (NumSamples - DiffN); Idx++) {
        size_t High = (NumSamples - 1) - Idx;
        size_t Low = High - DiffN;

        Sample LHS = getSample(High);
        Sample RHS = getSample(Low);

        LHS.diff(RHS);
    }
}

void SamplesBuffer::smoothSamples() {
    // Holds the mean value of each window
    CalculatedNumber AccCNs[1] = { 0 };
    Sample Acc(AccCNs, 1);

    // Used to avoid clobbering the accumulator when moving the window
    CalculatedNumber TmpCNs[1] = { 0 };
    Sample Tmp(TmpCNs, 1);

    CalculatedNumber Factor = (CalculatedNumber) 1 / SmoothN;

    // Calculate the value of the 1st window
    for (size_t Idx = 0; Idx != std::min(SmoothN, NumSamples); Idx++) {
        Tmp.add(getSample(NumSamples - (Idx + 1)));
    }

    Acc.add(Tmp);
    Acc.scale(Factor);

    // Move the window and update the samples
    for (size_t Idx = NumSamples; Idx != (DiffN + SmoothN - 1); Idx--) {
        Sample S = getSample(Idx - 1);

        // Tmp <- Next window (if any)
        if (Idx >= (SmoothN + 1)) {
            Tmp.diff(S);
            Tmp.add(getSample(Idx - (SmoothN + 1)));
        }

        // S <- Acc
        S.copy(Acc);

        // Acc <- Tmp
        Acc.copy(Tmp);
        Acc.scale(Factor);
    }
}

void SamplesBuffer::lagSamples() {
    if (LagN == 0)
        return;

    for (size_t Idx = NumSamples; Idx != LagN; Idx--) {
        Sample PS = getPreprocessedSample(Idx - 1);
        PS.lag(getSample(Idx - 1), LagN);
    }
}

void SamplesBuffer::preprocess(std::vector<DSample> &Samples) {
    assert(Preprocessed == false);

    size_t OutN = NumSamples;

    // Diff
    if (DiffN >= OutN)
        return;
    OutN -= DiffN;
    diffSamples();

    // Smooth
    if (SmoothN == 0 || SmoothN > OutN)
        return;
    OutN -= (SmoothN - 1);
    smoothSamples();

    // Lag
    if (LagN >= OutN)
        return;
    OutN -= LagN;
    lagSamples();

    Samples.reserve(OutN);
    Preprocessed = true;

    uint32_t MaxMT = std::numeric_limits<uint32_t>::max();
    uint32_t CutOff = static_cast<double>(MaxMT) * SamplingRatio;

    for (size_t Idx = NumSamples - OutN; Idx != NumSamples; Idx++) {
        if (RandNums[Idx] > CutOff)
            continue;

        DSample DS;
        DS.set_size(NumDimsPerSample * (LagN + 1));

        const Sample PS = getPreprocessedSample(Idx);
        PS.initDSample(DS);

        Samples.push_back(std::move(DS));
    }
}

void SamplesBuffer::preprocess(DSample &Feature) {
    assert(Preprocessed == false);

    size_t OutN = NumSamples;

    // Diff
    if (DiffN >= OutN)
        return;
    OutN -= DiffN;
    diffSamples();

    // Smooth
    if (SmoothN == 0 || SmoothN > OutN)
        return;
    OutN -= (SmoothN - 1);
    smoothSamples();

    // Lag
    if (LagN >= OutN)
        return;
    OutN -= LagN;
    lagSamples();

    Preprocessed = true;

    uint32_t MaxMT = std::numeric_limits<uint32_t>::max();
    uint32_t CutOff = static_cast<double>(MaxMT) * SamplingRatio;

    for (size_t Idx = NumSamples - OutN; Idx != NumSamples; Idx++) {
        if (RandNums[Idx] > CutOff)
            continue;

        Feature.set_size(NumDimsPerSample * (LagN + 1));

        const Sample PS = getPreprocessedSample(Idx);
        PS.initDSample(Feature);
    }
}