private void PredictionNuThread(ref int count, SENSOR sensor, int start, int end, ref ConcurrentStack<SVMParameter> svmParams, List<SVMNode[]> data, int svmCount, ref NoveltyResult bestCoveredResult, ref Mutex mutex, ref ConcurrentBag<string> nuResults) { OneClassClassifier occ = new OneClassClassifier(data); List<OneClassFV> anomali = new List<OneClassFV>(); List<Events> eventResult = new List<Events>(); int maxCount = svmCount; string sensorPath = path + "/" + sensor.ToString(); foreach (Events p in events) { var evt = p.Copy(); eventResult.Add(evt); } while (!svmParams.IsEmpty) { SVMParameter svmParam = null; svmParams.TryPop(out svmParam); if (svmParam == null) { break; } anomali = new List<OneClassFV>(); occ.CreateModel(svmParam); anomali.AddRange(occ.PredictOutliers(featureVectors[sensor].Where(x => start < x.TimeStamp && x.TimeStamp < end).ToList())); PointsOfInterest dPointsOfInterest = new PointsOfInterest(anomali); foreach (Events evt in eventResult) { evt.SetPointOfInterest(dPointsOfInterest); } NoveltyResult tempResult = new NoveltyResult(dPointsOfInterest, eventResult, start, end, svmParam, anomali); /*NoveltyResult.ConfusionMatrix cfm = tempResult.CalculateConfusionMatrix(); decimal tpr = ((decimal)cfm.TruePostive) / ((decimal)cfm.TruePostive + cfm.FalseNegative); decimal fpr = 1 - ((decimal)cfm.TrueNegative / ((decimal)cfm.TrueNegative + cfm.FalsePostive)); */ double temp = tempResult.FlaggedAreaSize(); double temp2 = tempResult.CalculateTotalNormalArea(); double areaCovered = ((double)tempResult.FlaggedAreaSize() / tempResult.CalculateTotalNormalArea() > 1) ? 1 : tempResult.FlaggedAreaSize() / (double)tempResult.CalculateTotalNormalArea(); nuResults.Add($"{tempResult.parameter.Nu.ToString()}:" + $"{tempResult.CalculateHitResult().eventHits/ (double)tempResult.CalculateHitResult().eventsTotal};" + $"{tempResult.CalculateHitResult().hits / ((double)tempResult.CalculateHitResult().misses+ tempResult.CalculateHitResult().hits)};" + $"{areaCovered}"); mutex.WaitOne(); count++; SetProgress(count, sensor); mutex.ReleaseMutex(); } Log.LogMessage(sensor + " done!"); }
// private int numberOfTasks = 10; private async Task<NoveltyResult> DoNoveltyDetection(SENSOR sensor, int start, int end) { string sensorPath = path + "/" + sensor.ToString(); var data = featureVectors[sensor].Select(x => x.Features).ToList(); ConcurrentStack<SVMParameter> svmParams = new ConcurrentStack<SVMParameter>(); //Debug purpose /*for (int i = 0; i < 10; i++) { SVMParameter s = new SVMParameter(); s.C = 100; s.Gamma = 0.01; s.Kernel = SVMKernelType.RBF; s.Type = SVMType.ONE_CLASS; s.Nu = 0.01; svmParams.Push(s); }*/ svmParams.PushRange(GenerateOneClassSVMParameters().ToArray()); SetProgressMax(svmParams.Count + 1); NoveltyResult bestCoveredResult = null; Mutex bestResultMu = new Mutex(false, sensor.ToString()); int count = 1; List<Task> tasks = new List<Task>(); for (int i = 0; i < threadMAX.Value; i++) { Task task = Task.Run(() => PredictionThread(ref count, sensor, start, end, ref svmParams, data, svmParams.Count, ref bestCoveredResult, ref bestResultMu)); tasks.Add(task); } await Task.WhenAll(tasks); List<int> integers = Enumerable.Range(1, 100).ToList(); List <double> nus = integers.Select(x => ((double)x) / 100).ToList(); //Cover foreach (double d in nus) { SVMParameter para = new SVMParameter(); para.Gamma = bestCoveredResult.parameter.Gamma; para.Nu = d; para.Kernel = bestCoveredResult.parameter.Kernel; svmParams.Push(para); } count = 1; SetProgressMax(svmParams.Count + 1); bestResultMu.Dispose(); bestResultMu = new Mutex(false, sensor.ToString()); List<Task> nuCov = new List<Task>(); ConcurrentBag<string> covNu = new ConcurrentBag<string>(); for (int i = 0; i < threadMAX.Value; i++) { Task task = Task.Run(() => PredictionNuThread(ref count, sensor, start, end, ref svmParams, data, svmParams.Count, ref bestCoveredResult, ref bestResultMu, ref covNu)); tasks.Add(task); } await Task.WhenAll(tasks); File.WriteAllLines(path + "/Params.txt", new List<string> { bestCoveredResult.parameter.Gamma.ToString(), bestCoveredResult.parameter.Kernel.ToString() }); File.WriteAllLines(path + $"/{sensor.ToString()}CovNu.txt", covNu.ToList()); bestResultMu.Dispose(); return bestCoveredResult; }
private void PredictionThread(ref int count, SENSOR sensor, int start, int end, ref ConcurrentStack<SVMParameter> svmParams, List<SVMNode[]> data, int svmCount, ref NoveltyResult bestCoveredResult, ref Mutex mutex) { OneClassClassifier occ = new OneClassClassifier(data); List<OneClassFV> anomali = new List<OneClassFV>(); List<Events> eventResult = new List<Events>(); List<OneClassFV> outliersFromSam = new List<OneClassFV>(); int maxCount = svmCount; string sensorPath = path + "/" + sensor.ToString(); foreach (Events p in events) { var evt = p.Copy(); eventResult.Add(evt); } while (!svmParams.IsEmpty) { SVMParameter svmParam = null; svmParams.TryPop(out svmParam); if (svmParam == null) { break; } anomali = new List<OneClassFV>(); occ.CreateModel(svmParam); anomali.AddRange(occ.PredictOutliers(featureVectors[sensor].Where(x => start < x.TimeStamp && x.TimeStamp < end).ToList())); PointsOfInterest dPointsOfInterest = new PointsOfInterest(anomali); foreach (Events evt in eventResult) { evt.SetPointOfInterest(dPointsOfInterest); } NoveltyResult tempResult = new NoveltyResult(dPointsOfInterest, eventResult, start, end, svmParam, anomali); /*NoveltyResult.ConfusionMatrix cfm = tempResult.CalculateConfusionMatrix(); decimal tpr = ((decimal)cfm.TruePostive) / ((decimal)cfm.TruePostive + cfm.FalseNegative); decimal fpr = 1 - ((decimal)cfm.TrueNegative / ((decimal)cfm.TrueNegative + cfm.FalsePostive)); */ mutex.WaitOne(); if (bestCoveredResult == null) { bestCoveredResult = new NoveltyResult(dPointsOfInterest, eventResult, start, end, svmParam, anomali); } else if (tempResult.CalculateCoveredScore() > bestCoveredResult.CalculateCoveredScore()) { //bestResult = new NoveltyResult(dPointsOfInterest, eventResult, start, end, svmParam, anomali); ; bestCoveredResult = tempResult; } count++; SetProgress(count, sensor); mutex.ReleaseMutex(); } Log.LogMessage(sensor + " done!"); }
private async Task<NoveltyResult> DoVotingNoveltyDetection(SENSOR sensor, int start, int end, double nu, string type) { string sensorPath = path + "/" + sensor.ToString(); var data = featureVectors[sensor].Select(x => x.Features).ToList(); ConcurrentStack<SVMParameter> svmParams = new ConcurrentStack<SVMParameter>(); svmParams.PushRange(GenerateVotingSVMParameters(nu).ToArray()); SetProgressMax(svmParams.Count + 1); NoveltyResult bestResult = null; Mutex bestResultMu = new Mutex(false, sensor.ToString()); int count = 1; List<Task> tasks = new List<Task>(); for (int i = 0; i < threadMAX.Value; i++) { Task task = Task.Run(() => PredictionVoteThread(ref count, sensor, start, end, ref svmParams, data, svmParams.Count, ref bestResult, bestResultMu, type)); tasks.Add(task); } await Task.WhenAll(tasks); bestResultMu.Dispose(); return bestResult; }