-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.h
308 lines (271 loc) · 8.81 KB
/
commands.h
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#ifndef COMMANDS_H_
#define COMMANDS_H_
#include<iostream>
#include <string.h>
#include <fstream>
#include <vector>
#include "HybridAnomalyDetector.h"
using namespace std;
class DefaultIO{
public:
virtual string read()=0;
virtual void write(string text)=0;
virtual void write(float f)=0;
virtual void read(float* f)=0;
virtual ~DefaultIO(){}
// you may add additional methods here
};
/**
* the class hold all the data relevant to the Anomaly Detector
*/
class AnomalyDetectorData{
public:
TimeSeries* learnData;
TimeSeries* TestDate;
SimpleAnomalyDetector* detector = new HybridAnomalyDetector();
vector<AnomalyReport> reports;
virtual ~AnomalyDetectorData(){};
};
class Command{
protected:
DefaultIO* dio;
AnomalyDetectorData* data;
string description;
public:
Command(DefaultIO* dio, AnomalyDetectorData* data):dio(dio), data(data){}
virtual void execute()=0;
virtual ~Command(){}
virtual string getDes () {
return this->description;
};
};
//command 1
class UploadCommand : public Command{
public:
UploadCommand (DefaultIO* dio, AnomalyDetectorData* data) : Command(dio,data){
this->description = "upload a time series csv file";
};
virtual void execute(){
vector<string> train;
vector<string> test;
string line;
FILE f;
const char* trainName = "anomalyTrain.csv";
const char* testName = "anomalyTest.csv";
///// do it with loop - it will be better
/// read train.
dio->write("Please upload your local train CSV file.\n");
line = dio->read();
while ("done" != line){
train.push_back(line);
line = dio->read();
}
makeFileFromVector(train, trainName);
TimeSeries* ts1 = new TimeSeries(trainName);
this->data->learnData = ts1;
dio->write("Upload complete.\n");
/// read test
dio->write("Please upload your local test CSV file.\n");
line = dio->read();
while ("done" != line){
test.push_back(line);
line = dio->read();
}
makeFileFromVector(test, testName);
TimeSeries* ts2 = new TimeSeries(testName);
this->data->TestDate = ts2;
dio->write("Upload complete.\n");
};
void makeFileFromVector(vector<string> vec, const char* fileName){
ofstream file(fileName);
ofstream output_file(fileName);
ostream_iterator<std::string> output_iterator(output_file, "\n");
copy(vec.begin(), vec.end(), output_iterator);
}
};
//command 2
class SettingsCommand : public Command {
public:
SettingsCommand (DefaultIO* dio, AnomalyDetectorData* data) : Command(dio,data){
this->description = "algorithm settings";
};
/**
* the function presents the current correlation and allows to replace it with a value in the range 0-1
*/
virtual void execute(){
//print the current corr
this->dio->write("The current correlation threshold is " );
this->dio->write(this->data->detector->GetLinThresh() );
this->dio->write("\n" );
this->dio->write("Type a new threshold\n");
//get new corr
float corr;
this->dio->read(&corr);
//while the correlation entered isn't in the right range, show alert and start over.
if (corr <= 0 || corr > 1) {
this->dio->write("please choose a value between 0 and 1" );
this->execute();
} else {
this->data->detector->ChangeLinThresh(corr);
return;
}
};
};
//command 3
class AnomalyDetectionCommand : public Command{
public:
AnomalyDetectionCommand (DefaultIO* dio, AnomalyDetectorData* data) : Command(dio,data){
this->description = "detect anomalies";
};
/**
* the function implements the anomaly detector's algorithm on the input data
*/
virtual void execute(){
//learn
this->data->detector->learnNormal(*this->data->learnData);
//detect
this->data->reports = this->data->detector->detect(*this->data->TestDate);
this->dio->write("anomaly detection complete.\n" );
};
};
//command 4
class ResultCommand : public Command{
public:
ResultCommand (DefaultIO* dio, AnomalyDetectorData* data) : Command(dio,data){
this->description = "display results";
};
/**
* the function print the anomaly found in the data.
*/
virtual void execute(){
for(AnomalyReport& r : this->data->reports) {
this->dio->write(r.timeStep);
this->dio->write("\t");
this->dio->write(r.description);
this->dio->write("\n");
}
this->dio->write("Done. \n");
};
};
//command 5
class AnalyzeCommand : public Command {
int TP;
int FP;
public:
AnalyzeCommand(DefaultIO *dio, AnomalyDetectorData *data) : Command(dio, data) {
this->description = "upload anomalies and analyze results";
this->TP = 0;
this->FP = 0;
}
void add(vector<pair<int, int>> *v, string line, vector<pair<long, long>> testvec) {
int xtest, ytest, flag = 0;
int sizeTest = testvec.size();
vector<int> temp;
/// parse the 2 time-steps from the string to vector
stringstream ss(line);
while (ss.good()) {
string substr;
getline(ss, substr, ',');
temp.push_back(stof(substr));
}
int x = temp[0];
int y = temp[1];
/// push it into interval vectors.
v->push_back({x, y});
/// check the interval with the test intervals.
for (int i = 0; i < sizeTest; ++i) {
xtest = testvec[i].first;
ytest = testvec[i].second;
if (xtest <= x) {
if (ytest >= y)
TP++;
else if ((ytest - xtest) + (y - x) >= (y - xtest))
TP++;
} else {
if (y >= ytest)
TP++;
else if ((y - x) + (ytest - xtest) >= (ytest - x))
TP++;
}
}
}
string numberformat(float number) {
string s = to_string(number);
int dot = s.find('.');
int cutOffPosition = dot + 3;
while (s[cutOffPosition] == '0') {
cutOffPosition--;
}
/// if the number is a natural number - remove the dot
if (cutOffPosition == dot) {
cutOffPosition--;
}
return s.substr(0, cutOffPosition + 1);
}
virtual void execute() {
this->TP = 0;
this->FP = 0;
string line;
long s, e;
int N, P, sum = 0;
vector<pair<int, int>> *points = new vector<pair<int, int>>();
vector<AnomalyReport> reports = this->data->reports;
vector<pair<long, long>> intervalsFromTest;
int sizeOfReports = reports.size();
/// making vector of intervals from test.
for (int i = 0; i < sizeOfReports; ++i) {
if (0 == i) {
s = reports[i].timeStep;
}
if (sizeOfReports == i) {
e = reports[i].timeStep;
intervalsFromTest.push_back({s, e});
}
if ((reports[i + 1].timeStep != (reports[i].timeStep + 1))) {
e = reports[i].timeStep;
intervalsFromTest.push_back({s, e});
s = reports[i + 1].timeStep;
}
}
//// reading the file into vector.
this->dio->write("Please upload your local anomalies file.\n");
line = this->dio->read();
while (line != "done") {
add(points, line, intervalsFromTest);
line = this->dio->read();
}
dio->write("Upload complete.\n");
/// making N - time steps without exceptions, P- Amount of exceptions.
int n = this->data->TestDate->lineSize(); // all data lines.
for (pair<int, int> p: *points) {
sum += (p.second - p.first + 1);
}
/// calc N, P , FP
N = n - sum;
P = points->size();
this->FP = intervalsFromTest.size() - this->TP;
//// print to user
float tpRate = (float) (this->TP) / (float) P;
float fpRate = (float) (this->FP) / (float) N;
string tpR = numberformat(tpRate);
string fpR = numberformat(fpRate);
this->dio->write("True Positive Rate: ");
this->dio->write(tpR);
this->dio->write("\n");
this->dio->write("False Positive Rate: ");
this->dio->write(fpR);
this->dio->write("\n");
}
};
//command 6
class ExitCommand : public Command{
public:
ExitCommand (DefaultIO* dio, AnomalyDetectorData* data) : Command(dio,data){
this->description = "exit";
};
virtual void execute(){};
virtual string getDes () {
return this->description;
};
};
#endif /* COMMANDS_H_ */