public static Tuple <SurveillancePlot, float> GetAggregateSurveillancePlotAndCorrelation(IEnumerable <Prediction> predictions, int plotWidth, int plotHeight, string seriesName, string plotTitle)
        {
            Dictionary <string, int>            aggregateLocationTrueCount = new Dictionary <string, int>();
            Dictionary <string, List <double> > aggregateLocationThreats   = new Dictionary <string, List <double> >();

            foreach (Prediction prediction in predictions)
            {
                IEnumerable <Incident>   incidents         = Incident.Get(prediction.PredictionStartTime, prediction.PredictionEndTime, prediction.PredictionArea, prediction.Model.IncidentTypes.ToArray());
                Dictionary <string, int> locationTrueCount = SurveillancePlot.GetOverallLocationTrueCount(incidents, prediction);
                foreach (string location in locationTrueCount.Keys)
                {
                    aggregateLocationTrueCount.Add(prediction.Id + "-" + location, locationTrueCount[location]);
                }

                Dictionary <string, List <double> > locationThreats = SurveillancePlot.GetOverallLocationThreats(prediction);
                foreach (string location in locationThreats.Keys)
                {
                    aggregateLocationThreats.Add(prediction.Id + "-" + location, locationThreats[location]);
                }

                prediction.ReleaseAllLazyLoadedData();
            }

            Dictionary <string, List <PointF> > seriesPoints = new Dictionary <string, List <PointF> >();

            seriesPoints.Add(seriesName, SurveillancePlot.GetSurveillancePlotPoints(aggregateLocationTrueCount, aggregateLocationThreats, true, true));
            seriesPoints.Add(OptimalSeriesName, SurveillancePlot.GetOptimalSurveillancePlotPoints(aggregateLocationTrueCount, aggregateLocationThreats, true, true));

            return(new Tuple <SurveillancePlot, float>(new SurveillancePlot(plotTitle, -1, seriesPoints, plotHeight, plotWidth, Plot.Format.JPEG, 2), GetThreatCorrelation(aggregateLocationThreats, aggregateLocationTrueCount)));
        }
Exemplo n.º 2
0
 protected void Page_Load(object sender, EventArgs e)
 {
     Response.Cookies["loginreferrer"].Value   = "/admin/incidents.aspx";
     Response.Cookies["loginreferrer"].Expires = DateTime.Now.AddDays(30);
     if (Request.Cookies["adminid"] != null && Request.Cookies["adminid"].Value != "")
     {
         intProfile = Int32.Parse(Request.Cookies["adminid"].Value);
     }
     else
     {
         Response.Redirect("/admin/login.aspx");
     }
     oIncident = new Incident(intProfile, dsn);
     oVariable = new Variables(intEnvironment);
     if (!IsPostBack)
     {
         LoadList();
     }
     if (Request.QueryString["id"] == null)
     {
         if (Request.QueryString["add"] == null)
         {
             LoopRepeater();
         }
         else
         {
             panAdd.Visible    = true;
             btnDelete.Enabled = false;
         }
     }
     else
     {
         panAdd.Visible = true;
         intID          = Int32.Parse(Request.QueryString["id"]);
         if (intID > 0 && !IsPostBack)
         {
             DataSet ds = oIncident.Get(intID);
             hdnId.Value               = intID.ToString();
             txtError.Text             = ds.Tables[0].Rows[0]["error"].ToString();
             txtCompare.Text           = ds.Tables[0].Rows[0]["compare"].ToString();
             ddlRoute.SelectedValue    = ds.Tables[0].Rows[0]["route"].ToString();
             chkAutomatic.Checked      = (ds.Tables[0].Rows[0]["automatic"].ToString() == "1");
             txtMessage.Text           = ds.Tables[0].Rows[0]["message"].ToString();
             ddlPriority.SelectedValue = ds.Tables[0].Rows[0]["priority"].ToString();
             radWorkstation.Checked    = (ds.Tables[0].Rows[0]["workstation"].ToString() == "1");
             radServer.Checked         = !radWorkstation.Checked;
             chkEnabled.Checked        = (ds.Tables[0].Rows[0]["enabled"].ToString() == "1");
         }
     }
     btnDelete.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this item?');");
 }
        protected override void ExecuteWorkflowLogic()
        {
            var incidentCloseRequest = new CloseIncidentRequest()
            {
                Status             = IncidentStatus.Get(Context.ExecutionContext),
                IncidentResolution = new Entity("incidentresolution")
                {
                    ["subject"]     = Subject.Get(Context.ExecutionContext),
                    ["incidentid"]  = Incident.Get(Context.ExecutionContext),
                    ["description"] = Description.Get(Context.ExecutionContext),
                    ["timespent"]   = TimeSpent.Get(Context.ExecutionContext)
                }
            };

            Context.UserService.Execute(incidentCloseRequest);
        }
Exemplo n.º 4
0
        protected override void ExecuteWorkflowLogic(CodeActivityContext executionContext, IWorkflowContext context, IOrganizationService service)
        {
            var incidentCloseRequest = new CloseIncidentRequest()
            {
                Status             = IncidentStatus.Get(executionContext),
                IncidentResolution = new Entity("incidentresolution")
                {
                    ["subject"]     = Subject.Get(executionContext),
                    ["incidentid"]  = Incident.Get(executionContext),
                    ["actualend"]   = CloseDate.Get(executionContext),
                    ["description"] = Description.Get(executionContext)
                }
            };

            service.Execute(incidentCloseRequest);
        }
Exemplo n.º 5
0
        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 static void Evaluate(Prediction prediction, int plotWidth, int plotHeight)
        {
            List <Incident> newIncidents      = new List <Incident>();
            DateTime        newIncidentsStart = prediction.PredictionStartTime;

            if (prediction.MostRecentlyEvaluatedIncidentTime >= newIncidentsStart)
            {
                newIncidentsStart = prediction.MostRecentlyEvaluatedIncidentTime + new TimeSpan(0, 0, 0, 0, 1);
            }

            foreach (Incident i in Incident.Get(newIncidentsStart, prediction.PredictionEndTime, prediction.PredictionArea, prediction.Model.IncidentTypes.ToArray()))
            {
                newIncidents.Add(i);
            }

            if (newIncidents.Count == 0)
            {
                return;
            }

            List <Incident> oldIncidents    = new List <Incident>();
            DateTime        oldIncidentsEnd = newIncidentsStart - new TimeSpan(0, 0, 0, 0, 1);

            if (oldIncidentsEnd >= prediction.PredictionStartTime)
            {
                foreach (Incident i in Incident.Get(prediction.PredictionStartTime, oldIncidentsEnd, prediction.PredictionArea, prediction.Model.IncidentTypes.ToArray()))
                {
                    oldIncidents.Add(i);
                }
            }

            IEnumerable <Incident> incidents = oldIncidents.Union(newIncidents);

            DiscreteChoiceModel model = prediction.Model;

            long sliceTicks = -1;

            if (model is TimeSliceDCM)
            {
                sliceTicks = (model as TimeSliceDCM).TimeSliceTicks;
            }

            prediction.AssessmentPlots.Clear();
            prediction.SliceThreatCorrelation.Clear();
            prediction.OverallCrimeThreatCorrelation = -1;
            int sliceNum = 1;
            Dictionary <long, Dictionary <string, int> >            sliceLocationTrueCount = SurveillancePlot.GetSliceLocationTrueCount(incidents, prediction);
            Dictionary <long, Dictionary <string, List <double> > > sliceLocationThreats   = SurveillancePlot.GetSliceLocationThreats(prediction);

            foreach (long slice in sliceLocationTrueCount.Keys.OrderBy(slice => slice))
            {
                if (sliceLocationThreats.ContainsKey(slice)) // no prediction points might have been generated in the case of a feature-based classifier with no features
                {
                    string slicePlotTitle = prediction.Name;
                    if (sliceTicks > 0)
                    {
                        DateTime sliceStart = new DateTime(slice * sliceTicks);
                        DateTime sliceEnd   = sliceStart + new TimeSpan(sliceTicks - 1);
                        slicePlotTitle += Environment.NewLine +
                                          sliceStart.ToShortDateString() + " " + sliceStart.ToShortTimeString() + " - " +
                                          Environment.NewLine +
                                          sliceEnd.ToShortDateString() + " " + sliceEnd.ToShortTimeString();
                    }

                    Dictionary <string, List <PointF> > seriesPoints = new Dictionary <string, List <PointF> >();
                    string predictionSeriesName = sliceLocationTrueCount.Count > 1 ? "Slice " + sliceNum++ : "Prediction";
                    seriesPoints.Add(predictionSeriesName, SurveillancePlot.GetSurveillancePlotPoints(sliceLocationTrueCount[slice], sliceLocationThreats[slice], true, true));
                    seriesPoints.Add(OptimalSeriesName, SurveillancePlot.GetOptimalSurveillancePlotPoints(sliceLocationTrueCount[slice], sliceLocationThreats[slice], true, true));
                    prediction.AssessmentPlots.Add(new SurveillancePlot(slicePlotTitle, slice, seriesPoints, plotHeight, plotWidth, Plot.Format.JPEG, 2));
                    prediction.SliceThreatCorrelation.Add(slice, GetThreatCorrelation(sliceLocationThreats[slice], sliceLocationTrueCount[slice]));
                }
            }

            if (sliceLocationTrueCount.Count > 1)
            {
                Dictionary <string, int>            overallLocationTrueCount = SurveillancePlot.GetOverallLocationTrueCount(incidents, prediction);
                Dictionary <string, List <double> > overallLocationThreats   = SurveillancePlot.GetOverallLocationThreats(prediction);
                Dictionary <string, List <PointF> > seriesPoints             = new Dictionary <string, List <PointF> >();
                seriesPoints.Add("Overall", SurveillancePlot.GetSurveillancePlotPoints(overallLocationTrueCount, overallLocationThreats, true, true));
                seriesPoints.Add(OptimalSeriesName, SurveillancePlot.GetOptimalSurveillancePlotPoints(overallLocationTrueCount, overallLocationThreats, true, true));
                prediction.AssessmentPlots.Add(new SurveillancePlot(prediction.Name, -1, seriesPoints, plotHeight, plotWidth, Plot.Format.JPEG, 2));
                prediction.OverallCrimeThreatCorrelation = GetThreatCorrelation(overallLocationThreats, overallLocationTrueCount);
            }

            prediction.MostRecentlyEvaluatedIncidentTime = incidents.Max(i => i.Time);
            prediction.ReleaseAllLazyLoadedData();
        }