diff --git a/AnomalyDetector.h b/AnomalyDetector.h index e7c00ee..18d199d 100644 --- a/AnomalyDetector.h +++ b/AnomalyDetector.h @@ -1,4 +1,4 @@ - +//Orpaz Sondhelm 206492324 Yarin Tzdaka 319091278 #ifndef ANOMALYDETECTOR_H_ #define ANOMALYDETECTOR_H_ diff --git a/CLI.cpp b/CLI.cpp index a66af70..4930f7e 100644 --- a/CLI.cpp +++ b/CLI.cpp @@ -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(){ @@ -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); diff --git a/MainTrain.cpp b/MainTrain.cpp index 9c2d72e..0f84250 100644 --- a/MainTrain.cpp +++ b/MainTrain.cpp @@ -1,7 +1,7 @@ /* * run2.cpp * - * new mainnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn + * Created on: 8 áãöî× 2019 * Author: Eli */ @@ -58,6 +58,8 @@ void check(string outputFile,string expectedOutputFile){ while(!st.eof() && !ex.eof()){ getline(st,lst); getline(ex,lex); + cout< diff --git a/anomaly_detection_util.h b/anomaly_detection_util.h index 75e6eb0..e3c4034 100644 --- a/anomaly_detection_util.h +++ b/anomaly_detection_util.h @@ -1,3 +1,4 @@ +//Orpaz Sondhelm 206492324 Yarin Tzdaka 319091278 #ifndef ANOMALYDETECTORUTIL_H_ #define ANOMALYDETECTORUTIL_H_ diff --git a/commands.h b/commands.h index 3df6f00..59d78c5 100644 --- a/commands.h +++ b/commands.h @@ -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 AnomalyReportList; + std::vector anomalysequences; + int fileSize; + data() { + THRESHOLD = 0.9; + fileSize = 0; + } }; // you may edit this class @@ -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"); @@ -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 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; ianomalysequences.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{ @@ -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;ifixdRports.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< #include //#include @@ -64,6 +67,9 @@ class TimeSeries { myFile.close(); } + int getSize(){ + return columns[0].second.size(); + } const vector>> & getVector() const { return columns; };