public void Populate(DiscreteChoiceModel m)
        {
            Items.Clear();

            if(m != null)
                foreach (Smoother s in m.Smoothers)
                {
                    Items.Add(s);
                    SetSelected(Items.IndexOf(s), true);
                }

            foreach (Smoother available in Smoother.Available)
                if (Items.Cast<Smoother>().Count(present => present.GetType().Equals(available.GetType())) == 0)
                    Items.Add(available);
        }
        public static IEnumerable <DiscreteChoiceModel> GetAll(bool excludeThoseCopiedForPredictions)
        {
            IFeatureExtractor fex;

            Configuration.TryGetFeatureExtractor(typeof(FeatureBasedDCM), out fex);
            List <DiscreteChoiceModel> models = new List <DiscreteChoiceModel>();
            BinaryFormatter            bf     = new BinaryFormatter();
            NpgsqlCommand    cmd    = DB.Connection.NewCommand("SELECT " + Columns.Select + " FROM " + Table);
            NpgsqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                DiscreteChoiceModel model = bf.Deserialize(new MemoryStream(reader[Table + "_" + Columns.Model] as byte[])) as DiscreteChoiceModel;
                if (!excludeThoseCopiedForPredictions || (!model.HasMadePredictions && !model.IsMakingAPrediction))
                {
                    models.Add(model);
                }
            }

            reader.Close();
            DB.Connection.Return(cmd.Connection);

            return(models);
        }
        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();
        }
 internal void CommitValues(DiscreteChoiceModel model)
 {
     model.Name = ModelName;
     model.TrainingArea = TrainingArea;
     model.TrainingStart = TrainingStart;
     model.TrainingEnd = TrainingEnd;
     model.IncidentTypes = IncidentTypes;
     model.Smoothers = Smoothers;
 }