Esempio n. 1
0
        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!");
        }
Esempio n. 2
0
        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!");
        }