private static List <Tuple <string, Parameter> > GetPointPredictionValues(Dictionary <int, float> pointIdOverallDensity, Dictionary <int, Dictionary <string, double> > pointIdIncidentDensity) { List <Tuple <string, Parameter> > pointPredictionValues = new List <Tuple <string, Parameter> >(pointIdIncidentDensity.Count); foreach (int pointId in pointIdIncidentDensity.Keys) { string timeParameterName = "@time_" + pointId; pointPredictionValues.Add(new Tuple <string, Parameter>(PointPrediction.GetValue(pointId, timeParameterName, pointIdIncidentDensity[pointId], pointIdOverallDensity[pointId]), new Parameter(timeParameterName, NpgsqlDbType.Timestamp, DateTime.MinValue))); } return(pointPredictionValues); }
public override void Apply(Prediction prediction) { List <PointPrediction> pointPredictions = prediction.PointPredictions; if (pointPredictions.Count > 0) { Dictionary <int, Point> idPoint = new Dictionary <int, Point>(); foreach (Point p in prediction.Points) { idPoint.Add(p.Id, p); } IEnumerable <PostGIS.Point> kdeEvalPoints = pointPredictions.Select(p => idPoint[p.PointId].Location); List <PostGIS.Point> kdeInputPoints = new List <PostGIS.Point>(); foreach (string incident in pointPredictions[0].IncidentScore.Keys.ToArray()) { if (incident != PointPrediction.NullLabel) { double minScore = pointPredictions.Min(p => p.IncidentScore[incident]); kdeInputPoints.Clear(); foreach (PointPrediction pointPrediction in pointPredictions) { PostGIS.Point pointPredictionLocation = idPoint[pointPrediction.PointId].Location; double replicates = pointPrediction.IncidentScore[incident] / minScore; for (int i = 0; i < replicates; ++i) { kdeInputPoints.Add(pointPredictionLocation); } } List <float> density = KernelDensityDCM.GetDensityEstimate(kdeInputPoints, _sampleSize, false, 0, 0, kdeEvalPoints, _normalize); for (int i = 0; i < density.Count; ++i) { pointPredictions[i].IncidentScore[incident] = density[i]; } } } foreach (PointPrediction pointPrediction in pointPredictions) { pointPrediction.TotalThreat = pointPrediction.IncidentScore.Keys.Sum(incident => incident == PointPrediction.NullLabel ? 0 : pointPrediction.IncidentScore[incident]); } PointPrediction.UpdateThreatScores(pointPredictions, prediction); } prediction.SmoothingDetails = GetSmoothingDetails(); }
protected override void Run(Prediction prediction) { List <PostGIS.Point> predictionPoints = new List <PostGIS.Point>(); Area predictionArea = prediction.PredictionArea; double areaMinX = predictionArea.BoundingBox.MinX; double areaMaxX = predictionArea.BoundingBox.MaxX; double areaMinY = predictionArea.BoundingBox.MinY; double areaMaxY = predictionArea.BoundingBox.MaxY; for (double x = areaMinX + prediction.PredictionPointSpacing / 2d; x <= areaMaxX; x += prediction.PredictionPointSpacing) // place points in the middle of the square boxes that cover the region - we get display errors from pixel rounding if the points are exactly on the boundaries { for (double y = areaMinY + prediction.PredictionPointSpacing / 2d; y <= areaMaxY; y += prediction.PredictionPointSpacing) { predictionPoints.Add(new PostGIS.Point(x, y, predictionArea.Shapefile.SRID)); } } List <PostGIS.Point> incidentPoints = new List <PostGIS.Point>(Incident.Get(TrainingStart, TrainingEnd, predictionArea, IncidentTypes.ToArray()).Select(i => i.Location)); predictionPoints.AddRange(incidentPoints); Console.Out.WriteLine("Filtering prediction points to prediction area"); predictionPoints = predictionArea.Intersects(predictionPoints, prediction.PredictionPointSpacing / 2f).Select(i => predictionPoints[i]).ToList(); NpgsqlConnection connection = DB.Connection.OpenConnection; try { Console.Out.WriteLine("Inserting points into prediction"); Point.CreateTable(prediction, predictionArea.Shapefile.SRID); List <int> predictionPointIds = Point.Insert(connection, predictionPoints.Select(p => new Tuple <PostGIS.Point, string, DateTime>(p, PointPrediction.NullLabel, DateTime.MinValue)), prediction, predictionArea, false); Console.Out.WriteLine("Running overall KDE for " + IncidentTypes.Count + " incident type(s)"); List <float> density = GetDensityEstimate(incidentPoints, _trainingSampleSize, false, 0, 0, predictionPoints, _normalize); Dictionary <int, float> pointIdOverallDensity = new Dictionary <int, float>(predictionPointIds.Count); int pointNum = 0; foreach (int predictionPointId in predictionPointIds) { pointIdOverallDensity.Add(predictionPointId, density[pointNum++]); } Dictionary <int, Dictionary <string, double> > pointIdIncidentDensity = new Dictionary <int, Dictionary <string, double> >(pointIdOverallDensity.Count); if (IncidentTypes.Count == 1) { string incident = IncidentTypes.First(); foreach (int pointId in pointIdOverallDensity.Keys) { Dictionary <string, double> incidentDensity = new Dictionary <string, double>(); incidentDensity.Add(incident, pointIdOverallDensity[pointId]); pointIdIncidentDensity.Add(pointId, incidentDensity); } } else { foreach (string incidentType in IncidentTypes) { Console.Out.WriteLine("Running KDE for incident \"" + incidentType + "\""); incidentPoints = new List <PostGIS.Point>(Incident.Get(TrainingStart, TrainingEnd, predictionArea, incidentType).Select(i => i.Location)); density = GetDensityEstimate(incidentPoints, _trainingSampleSize, false, 0, 0, predictionPoints, _normalize); if (density.Count > 0) { pointNum = 0; foreach (int predictionPointId in predictionPointIds) { pointIdIncidentDensity.EnsureContainsKey(predictionPointId, typeof(Dictionary <string, double>)); pointIdIncidentDensity[predictionPointId].Add(incidentType, density[pointNum++]); } } } } PointPrediction.CreateTable(prediction); PointPrediction.Insert(GetPointPredictionValues(pointIdOverallDensity, pointIdIncidentDensity), prediction, false); Smooth(prediction); } finally { DB.Connection.Return(connection); } }
public override void Apply(Prediction prediction) { List <PointPrediction> pointPredictions = prediction.PointPredictions; if (pointPredictions.Count > 0) { Dictionary <int, Point> idPoint = new Dictionary <int, Point>(); foreach (Point p in prediction.Points) { idPoint.Add(p.Id, p); } List <Tuple <PointPrediction, Dictionary <string, double> > > pointPredictionIncidentScore = new List <Tuple <PointPrediction, Dictionary <string, double> > >(pointPredictions.Count); Set <Thread> threads = new Set <Thread>(Configuration.ProcessorCount); for (int i = 0; i < Configuration.ProcessorCount; ++i) { Thread t = new Thread(new ParameterizedThreadStart(core => { List <Tuple <PointPrediction, Dictionary <string, double> > > threadPointPredictionIncidentScore = new List <Tuple <PointPrediction, Dictionary <string, double> > >((int)((pointPredictions.Count / (float)Configuration.ProcessorCount) + 1)); for (int j = (int)core; j < pointPredictions.Count; j += Configuration.ProcessorCount) { PointPrediction pointPrediction = pointPredictions[j]; Dictionary <PointPrediction, double> neighborInvDist = new Dictionary <PointPrediction, double>(); foreach (PointPrediction neighbor in pointPredictions) { double distance = idPoint[pointPrediction.PointId].Location.DistanceTo(idPoint[neighbor.PointId].Location); if (pointPrediction == neighbor || (distance >= _minimum && distance <= _maximum)) { neighborInvDist.Add(neighbor, _maximum - distance); } } double totalInvDistance = neighborInvDist.Values.Sum(); Dictionary <string, double> incidentScore = new Dictionary <string, double>(pointPrediction.IncidentScore.Count); foreach (string incident in pointPrediction.IncidentScore.Keys) { incidentScore.Add(incident, neighborInvDist.Keys.Sum(neighbor => (neighborInvDist[neighbor] / totalInvDistance) * neighbor.IncidentScore[incident])); } threadPointPredictionIncidentScore.Add(new Tuple <PointPrediction, Dictionary <string, double> >(pointPrediction, incidentScore)); } lock (pointPredictionIncidentScore) { pointPredictionIncidentScore.AddRange(threadPointPredictionIncidentScore); } })); t.Start(i); threads.Add(t); } foreach (Thread t in threads) { t.Join(); } foreach (Tuple <PointPrediction, Dictionary <string, double> > pointPredictionScores in pointPredictionIncidentScore) { pointPredictionScores.Item1.IncidentScore = pointPredictionScores.Item2; pointPredictionScores.Item1.TotalThreat = pointPredictionScores.Item2.Values.Sum(); } PointPrediction.UpdateThreatScores(pointPredictions, prediction); prediction.SmoothingDetails = GetSmoothingDetails(); } }
public override void Apply(Prediction prediction) { List <PointPrediction> pointPredictions = prediction.PointPredictions; if (pointPredictions.Count > 0) { Dictionary <int, Point> idPoint = new Dictionary <int, Point>(); foreach (Point p in prediction.Points) { idPoint.Add(p.Id, p); } string inputPointsPath = Path.GetTempFileName(); string evalPointsPath = Path.GetTempFileName(); string outputPath = Path.GetTempFileName(); using (StreamWriter evalPointsFile = new StreamWriter(evalPointsPath)) { foreach (PostGIS.Point p in pointPredictions.Select(p => idPoint[p.PointId].Location)) { evalPointsFile.WriteLine(p.X + "," + p.Y); } evalPointsFile.Close(); } foreach (string incident in pointPredictions[0].IncidentScore.Keys.ToArray()) { if (incident != PointPrediction.NullLabel) { using (StreamWriter inputPointsFile = new StreamWriter(inputPointsPath)) { inputPointsFile.WriteLine("threat,x,y"); foreach (PointPrediction pointPrediction in pointPredictions) { PostGIS.Point location = idPoint[pointPrediction.PointId].Location; inputPointsFile.WriteLine(pointPrediction.IncidentScore[incident] + "," + location.X + "," + location.Y); } inputPointsFile.Close(); } R.Execute(@" library(earth) input.points = read.csv(""" + inputPointsPath.Replace(@"\", @"\\") + @""",header=TRUE) model = earth(threat ~ ., data = input.points, " + (_numberOfKnots == -1 ? "" : "nk = " + _numberOfKnots + ", ") + "fast.k = " + _consideredParentTerms + ", degree = " + _interactionDegree + @") eval.points = read.csv(""" + evalPointsPath.Replace(@"\", @"\\") + @""",header=FALSE) prediction = predict(model, eval.points, type=""response"") prediction = (prediction - min(prediction)) / (max(prediction) - min(prediction)) write.table(prediction,file=""" + outputPath.Replace(@"\", @"\\") + @""",row.names=FALSE,col.names=FALSE)", false); int pointNum = 0; foreach (string line in File.ReadLines(outputPath)) { pointPredictions[pointNum++].IncidentScore[incident] = double.Parse(line); } } } File.Delete(inputPointsPath); File.Delete(evalPointsPath); File.Delete(outputPath); foreach (PointPrediction pointPrediction in pointPredictions) { pointPrediction.TotalThreat = pointPrediction.IncidentScore.Keys.Sum(incident => incident == PointPrediction.NullLabel ? 0 : pointPrediction.IncidentScore[incident]); } PointPrediction.UpdateThreatScores(pointPredictions, prediction); prediction.SmoothingDetails = GetSmoothingDetails(); } }