Skip to content

Commit

Permalink
kibalt? 30/12 13:50
Browse files Browse the repository at this point in the history
  • Loading branch information
yarintz33 committed Dec 30, 2021
1 parent fffe290 commit 8833015
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 13 deletions.
2 changes: 1 addition & 1 deletion AnomalyDetector.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

//Orpaz Sondhelm 206492324 Yarin Tzdaka 319091278

#ifndef ANOMALYDETECTOR_H_
#define ANOMALYDETECTOR_H_
Expand Down
14 changes: 8 additions & 6 deletions CLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CLI::CLI(DefaultIO* dio) {
this->theCommands.push_back(new Correlation(this->dio));
this->theCommands.push_back(new HybridAlgo(this->dio));
this->theCommands.push_back(new printAnomaly(this->dio));
this->theCommands.push_back(new UploadAnom(this->dio));
this->theCommands.push_back(new Exit(this->dio));
}

void CLI::start(){
Expand All @@ -15,12 +17,12 @@ void CLI::start(){
while (number != 6) {
this->dio->write("Welcome to the Anomaly Detection Server.\n"
"Please choose an option:\n"
"1. upload a time series csv file\n"
"2. algorithm settings\n"
"3. detect anomalies\n"
"4. display results\n"
"5. upload anomalies and analyze results\n"
"6. exit\n");
"1.upload a time series csv file\n"
"2.algorithm settings\n"
"3.detect anomalies\n"
"4.display results\n"
"5.upload anomalies and analyze results\n"
"6.exit\n");
number = std::stoi(this->dio->read());
if (number >= 1 && number <= 5) {
this->theCommands[number - 1]->execute(&d);
Expand Down
6 changes: 4 additions & 2 deletions MainTrain.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* run2.cpp
*
* new mainnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
* Created on: 8 áãöî× 2019
* Author: Eli
*/

Expand Down Expand Up @@ -58,6 +58,8 @@ void check(string outputFile,string expectedOutputFile){
while(!st.eof() && !ex.eof()){
getline(st,lst);
getline(ex,lex);
cout<<lst<<endl;
/*
if(i<13 && lst.compare(lex)!=0){ // 12
cout<<"line "<<i<<" expected: "<<lex<<" you got "<<lst<<endl;
cout<<"wrong output (-1)"<<endl;
Expand All @@ -69,7 +71,7 @@ void check(string outputFile,string expectedOutputFile){
}
j++;
}
i++;
i++;*/
}
if(j<11)
cout<<"wrong output size (-"<<(11-j)*8<<")"<<endl;
Expand Down
1 change: 1 addition & 0 deletions SimpleAnomalyDetector.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//Orpaz Sondhelm 206492324 Yarin Tzdaka 319091278

#include "SimpleAnomalyDetector.h"

Expand Down
2 changes: 1 addition & 1 deletion SimpleAnomalyDetector.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

//Orpaz Sondhelm 206492324 Yarin Tzdaka 319091278

#ifndef SIMPLEANOMALYDETECTOR_H_
#define SIMPLEANOMALYDETECTOR_H_
Expand Down
2 changes: 1 addition & 1 deletion anomaly_detection_util.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Created by orpaz on 10/17/21.fdh
//Orpaz Sondhelm 206492324 Yarin Tzdaka 319091278
//

#include <math.h>
Expand Down
1 change: 1 addition & 0 deletions anomaly_detection_util.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//Orpaz Sondhelm 206492324 Yarin Tzdaka 319091278

#ifndef ANOMALYDETECTORUTIL_H_
#define ANOMALYDETECTORUTIL_H_
Expand Down
107 changes: 105 additions & 2 deletions commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,23 @@ class DefaultIO{

// you may add here helper classes

struct Anomalyseq{
int start;
int end;
string description;
bool tp;
};


struct data {
float THRESHOLD;
std::vector<AnomalyReport> AnomalyReportList;
std::vector<Anomalyseq> anomalysequences;
int fileSize;
data() {
THRESHOLD = 0.9;
fileSize = 0;
}
};

// you may edit this class
Expand Down Expand Up @@ -69,7 +83,9 @@ class Correlation:public Command{
public:
Correlation(DefaultIO* dio):Command(dio){}
void execute(data *d){
this->dio->write("The current correlation threshold is 0.9\n");
this->dio->write("The current correlation threshold is ");
this->dio->write(d->THRESHOLD);
this->dio->write("\nType a new threshold\n");
float number = std::stof(this->dio->read());
while (number >= 1 || number <= 0){
this->dio->write("please choose a value between 0 and 1.\n");
Expand All @@ -86,11 +102,38 @@ class HybridAlgo:public Command{
void execute(data *d) {
TimeSeries tsTrain = TimeSeries("anomalyTrain.csv");
TimeSeries tsTest = TimeSeries("anomalyTest.csv");
d->fileSize = tsTest.getSize();
HybridAnomalyDetector hd = HybridAnomalyDetector();
hd.setThreshold(d->THRESHOLD);
hd.learnNormal(tsTrain);
d->AnomalyReportList = hd.detect(tsTest);
d->AnomalyReportList = hd.detect(tsTest);;
vector <AnomalyReport> ar = d->AnomalyReportList;
if(ar.size() == 0){
return;
}
Anomalyseq anomalyseq;
anomalyseq.start = ar.at(0).timeStep;
anomalyseq.end = anomalyseq.start;
anomalyseq.description=ar.at(0).description;
anomalyseq.tp = false;
int anomalyVecLen = ar.size();
for(size_t i = 1; i<anomalyVecLen; i++){
AnomalyReport currentRep = ar.at(i);
if(currentRep.timeStep == anomalyseq.end + 1 && currentRep.description == anomalyseq.description){
anomalyseq.end ++;
}
else {
d->anomalysequences.push_back(anomalyseq);
anomalyseq.start = currentRep.timeStep;
anomalyseq.end = anomalyseq.start;
anomalyseq.description = currentRep.description;
anomalyseq.tp = false;
}
}
d->anomalysequences.push_back(anomalyseq);
this->dio->write("anomaly detection complete.\n");
}

};

class printAnomaly:public Command{
Expand All @@ -105,7 +148,67 @@ class printAnomaly:public Command{
this->dio->write("\n");
i++;
}
this->dio->write("Done.\n");
}
};

class UploadAnom:public Command{
public:
UploadAnom(DefaultIO* dio):Command(dio){}

bool isTP(int start, int end,data* data){
for(size_t i=0;i< data->anomalysequences.size();i++){
Anomalyseq anomalyseq = data->anomalysequences[i];
if(start <= anomalyseq.end && end >= anomalyseq.start){
data->anomalysequences[i].tp=true;
return true;
}
}
return false;
}

virtual void execute(data* data){

// for(size_t i=0;i<sharedState->fixdRports.size();i++){
// sharedState->fixdRports[i].tp=false;
// }

dio->write("Please upload your local anomalies file.\n");
string s="";
float TP=0,sum=0,P=0;
while((s=dio->read())!="done"){
P++;
size_t t=0;
while(s[t] != ','){
t++;
}
int start = stoi(s.substr(0,t));
int end = stoi(s.substr(t+1,s.length()));
if(isTP(start,end,data))
TP++;
sum += end - start + 1;
}
dio->write("Upload complete.\n");
float FP= data->anomalysequences.size() - TP;
float N= data->fileSize - sum;
float truePositiveRate=((int)(1000.0*TP/P))/1000.0f;
float falsePositiveRate=((int)(1000.0*FP/N))/1000.0f;
dio->write("True Positive Rate: ");
dio->write(truePositiveRate);
dio->write("\nFalse Positive Rate: ");
dio->write(falsePositiveRate);
dio->write("\n");

}
};

class Exit:public Command{
public:
Exit(DefaultIO* dio):Command(dio){}
virtual void execute(data* data){
//cout<<description<<endl;
}
};


#endif /* COMMANDS_H_ */
2 changes: 2 additions & 0 deletions timeseries.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//Orpaz Sondhelm 206492324 Yarin Tzdaka 319091278

#include "timeseries.h"


6 changes: 6 additions & 0 deletions timeseries.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//Orpaz Sondhelm 206492324 Yarin Tzdaka 319091278


#include <fstream>
#include <vector>
//#include <string>
Expand Down Expand Up @@ -64,6 +67,9 @@ class TimeSeries {
myFile.close();
}

int getSize(){
return columns[0].second.size();
}
const vector<pair<string, vector<float>>> & getVector() const {
return columns;
};
Expand Down

0 comments on commit 8833015

Please sign in to comment.