public static double CalculateEarlyScore(PointsOfInterest poiT, List<Events> eventsT, int startT, int endT) { double timeReduction = 1 - ((double)poiT.GetFlaggedAreas().Where(x=>x.Item2> startT).Sum(x => (x.Item2 - x.Item1)) / (endT - startT)); double eventsHit = (double)eventsT.Where(x => x.isHit).Count() / eventsT.Count; return ((TIME_WEIGHT * timeReduction) * (HIT_WEIGHT * eventsHit)) / (HIT_WEIGHT + TIME_WEIGHT); }
public static double CalculateEarlyScore(PointsOfInterest poiT, List <Events> eventsT, int startT, int endT) { double timeReduction = 1 - ((double)poiT.GetFlaggedAreas().Where(x => x.Item2 > startT).Sum(x => (x.Item2 - x.Item1)) / (endT - startT)); double eventsHit = (double)eventsT.Where(x => x.isHit).Count() / eventsT.Count; return(((TIME_WEIGHT * timeReduction) * (HIT_WEIGHT * eventsHit)) / (HIT_WEIGHT + TIME_WEIGHT)); }
public NoveltyResult(PointsOfInterest poi, List<Events> events, int start, int end, LibSVMsharp.SVMParameter parameter, List<OneClassFV> anomalis) { this.poi = poi; this.events = events.Select(x => x.Copy()).ToList(); this.start = start; this.end = end; this.parameter = parameter; this.anomalis = anomalis.ToList(); }
public NoveltyResult(PointsOfInterest poi, List <Events> events, int start, int end, LibSVMsharp.SVMParameter parameter, List <OneClassFV> anomalis) { this.poi = poi; this.events = events.Select(x => x.Copy()).ToList(); this.start = start; this.end = end; this.parameter = parameter; this.anomalis = anomalis.ToList(); }
public static void SavePathPointsOfInterest(PointsOfInterest POIs, string path, string name) { if (!Directory.Exists(path + "/POI")) { Directory.CreateDirectory(path + "/POI"); } List <string> data = new List <string>(); var tempAreas = POIs.GetFlaggedAreas(); foreach (Tuple <int, int> area in tempAreas) { data.Add($"{area.Item1}, {area.Item2}"); } File.WriteAllLines(path + "/POI/" + name + ".txt", data); }
public static Dictionary <SENSOR, PointsOfInterest> LoadPointOfInterest(string path) { Dictionary <SENSOR, PointsOfInterest> pois = new Dictionary <SENSOR, PointsOfInterest>(); if (!Directory.Exists(path + "/POI")) { return(null); } foreach (SENSOR key in Enum.GetValues(typeof(SENSOR))) { string[] data = File.ReadAllLines(path + "/Anomalis/" + key.ToString() + ".txt"); List <int> anoma = new List <int>(); for (int i = 1; i < data.Length; i++) { anoma.Add(int.Parse(data[i])); } PointsOfInterest currentPoi = new PointsOfInterest(anoma); pois.Add(key, currentPoi); } return(pois); }
public static Dictionary<SENSOR, PointsOfInterest> LoadPointOfInterest(string path) { Dictionary<SENSOR, PointsOfInterest> pois = new Dictionary<SENSOR, PointsOfInterest>(); if (!Directory.Exists(path + "/POI")) { return null; } foreach (SENSOR key in Enum.GetValues(typeof(SENSOR))) { string[] data = File.ReadAllLines(path + "/Anomalis/" + key.ToString() + ".txt"); List<int> anoma = new List<int>(); for (int i = 1; i < data.Length; i++) { anoma.Add(int.Parse(data[i])); } PointsOfInterest currentPoi = new PointsOfInterest(anoma); pois.Add(key, currentPoi); } return pois; }
private void CreateVotingPOI() { anomalies = new List<OneClassFV>(); List<Tuple<int, int>> gsr = pois[SENSOR.GSR].GetFlaggedAreas(); List<Tuple<int, int>> hr = pois[SENSOR.HR].GetFlaggedAreas(); List<Tuple<int, int>> eeg = new List<Tuple<int, int>>(); if (pois.Keys.Contains(SENSOR.EEG)) { eeg = pois[SENSOR.EEG].GetFlaggedAreas(); } List<Tuple<int, int>> face = pois[SENSOR.FACE].GetFlaggedAreas(); Dictionary<string, bool> anomaliPresent = new Dictionary<string, bool>(); anomaliPresent.Add("gsr", false); anomaliPresent.Add("hr", false); anomaliPresent.Add("eeg", false); anomaliPresent.Add("face", false); for (int time = start; time < end; time++) { if(gsr.Count > 0 && gsr.First().Item1 < time && time < gsr.First().Item2) { anomaliPresent["gsr"] = true; } if (hr.Count > 0 && hr.First().Item1 < time && time < hr.First().Item2) { anomaliPresent["hr"] = true; } if (eeg.Count > 0 && eeg.First().Item1 < time && time < eeg.First().Item2) { anomaliPresent["eeg"] = true; } if (face.Count > 0 && face.First().Item1 < time && time < face.First().Item2) { anomaliPresent["face"] = true; } if (anomaliPresent.Where(x=>x.Value == true).Count() >= voteThreashold) { List<double> centers = new List<double>(); foreach (var key in anomaliPresent.Keys) { if (anomaliPresent[key] == true) { if (key == "gsr") { centers.Add(((gsr.First().Item2 -gsr.First().Item1)/2) + gsr.First().Item1); gsr.RemoveAt(0); } else if (key == "eeg") { centers.Add(((eeg.First().Item2 - eeg.First().Item1) / 2) + eeg.First().Item1); eeg.RemoveAt(0); } else if (key == "face") { centers.Add(((face.First().Item2 - face.First().Item1) / 2) + face.First().Item1); face.RemoveAt(0); } else { centers.Add(((hr.First().Item2 - hr.First().Item1) / 2) + hr.First().Item1); hr.RemoveAt(0); } } } anomalies.Add(new OneClassFV(null, (int)Math.Round(centers.Average()))); } if (gsr.Count > 0 && time > gsr.First().Item2) { gsr.RemoveAt(0); } if (hr.Count > 0 && time > hr.First().Item2) { hr.RemoveAt(0); } if (eeg.Count > 0 && time > eeg.First().Item2) { eeg.RemoveAt(0); } if (face.Count > 0 && time > face.First().Item2) { face.RemoveAt(0); } //Clean anomaliPresent["gsr"] = false; anomaliPresent["eeg"] = false; anomaliPresent["hr"] = false; anomaliPresent["face"] = false; } votingPOI = new PointsOfInterest(anomalies); }
private void CreateVotingPOI() { anomalies = new List <OneClassFV>(); List <Tuple <int, int> > gsr = pois[SENSOR.GSR].GetFlaggedAreas(); List <Tuple <int, int> > hr = pois[SENSOR.HR].GetFlaggedAreas(); List <Tuple <int, int> > eeg = new List <Tuple <int, int> >(); if (pois.Keys.Contains(SENSOR.EEG)) { eeg = pois[SENSOR.EEG].GetFlaggedAreas(); } List <Tuple <int, int> > face = pois[SENSOR.FACE].GetFlaggedAreas(); Dictionary <string, bool> anomaliPresent = new Dictionary <string, bool>(); anomaliPresent.Add("gsr", false); anomaliPresent.Add("hr", false); anomaliPresent.Add("eeg", false); anomaliPresent.Add("face", false); for (int time = start; time < end; time++) { if (gsr.Count > 0 && gsr.First().Item1 < time && time < gsr.First().Item2) { anomaliPresent["gsr"] = true; } if (hr.Count > 0 && hr.First().Item1 < time && time < hr.First().Item2) { anomaliPresent["hr"] = true; } if (eeg.Count > 0 && eeg.First().Item1 < time && time < eeg.First().Item2) { anomaliPresent["eeg"] = true; } if (face.Count > 0 && face.First().Item1 < time && time < face.First().Item2) { anomaliPresent["face"] = true; } if (anomaliPresent.Where(x => x.Value == true).Count() >= voteThreashold) { List <double> centers = new List <double>(); foreach (var key in anomaliPresent.Keys) { if (anomaliPresent[key] == true) { if (key == "gsr") { centers.Add(((gsr.First().Item2 - gsr.First().Item1) / 2) + gsr.First().Item1); gsr.RemoveAt(0); } else if (key == "eeg") { centers.Add(((eeg.First().Item2 - eeg.First().Item1) / 2) + eeg.First().Item1); eeg.RemoveAt(0); } else if (key == "face") { centers.Add(((face.First().Item2 - face.First().Item1) / 2) + face.First().Item1); face.RemoveAt(0); } else { centers.Add(((hr.First().Item2 - hr.First().Item1) / 2) + hr.First().Item1); hr.RemoveAt(0); } } } anomalies.Add(new OneClassFV(null, (int)Math.Round(centers.Average()))); } if (gsr.Count > 0 && time > gsr.First().Item2) { gsr.RemoveAt(0); } if (hr.Count > 0 && time > hr.First().Item2) { hr.RemoveAt(0); } if (eeg.Count > 0 && time > eeg.First().Item2) { eeg.RemoveAt(0); } if (face.Count > 0 && time > face.First().Item2) { face.RemoveAt(0); } //Clean anomaliPresent["gsr"] = false; anomaliPresent["eeg"] = false; anomaliPresent["hr"] = false; anomaliPresent["face"] = false; } votingPOI = new PointsOfInterest(anomalies); }
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 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!"); }
public static void SaveVotingPointsOfInterest(PointsOfInterest POIs, string path, string name) { if (!Directory.Exists(path + "/POI")) { Directory.CreateDirectory(path + "/POI"); } List<string> data = new List<string>(); var tempAreas = POIs.GetFlaggedAreas(); foreach (Tuple<int, int> area in tempAreas) { data.Add($"{area.Item1}, {area.Item2}"); } File.WriteAllLines(path + "/POI/" + name + ".txt", data); }