Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 0e7b6b11

Přidáno uživatelem Roman Kalivoda před téměř 4 roky(ů)

Re #9049 Implementation of GetDataFileNames and Load methods

Zobrazit rozdíly:

Server/ServerApp/Predictor/FeatureExtractor.cs
17 17
    /// </summary>
18 18
    class FeatureExtractor
19 19
    {
20

  
21
        private const double Confidence = 99.0;
22

  
20 23
        private static readonly ILog _log = LogManager.GetLogger(typeof(FeatureExtractor));
21 24

  
22 25
        /// <summary>
......
90 93
        {
91 94
            MLContext mlContext = new MLContext();
92 95
            IDataView input = mlContext.Data.LoadFromEnumerable(data);
93
            var pipeline = mlContext.Transforms.Conversion.ConvertType(nameof(ActivityInfo.amount)).Append(mlContext.Transforms.DetectIidSpike(nameof(AnomalyDetectionResult.Prediction), nameof(ActivityInfo.amount), 99.0, data.Count / 4));
96
            var pipeline = mlContext.Transforms.Conversion.ConvertType(nameof(ActivityInfo.amount)).Append(mlContext.Transforms.DetectIidSpike(nameof(AnomalyDetectionResult.Prediction), nameof(ActivityInfo.amount), Confidence, data.Count / 4));
94 97
            ITransformer transformer = pipeline.Fit(mlContext.Data.LoadFromEnumerable(new List<ActivityInfo>()));
95 98
            IDataView transformedData = transformer.Transform(input);
96 99
            List<AnomalyDetectionResult> predictions = mlContext.Data.CreateEnumerable<AnomalyDetectionResult>(transformedData, false).ToList();
......
98 101

  
99 102
            for (int i=0; i<predictions.Count; i++)
100 103
            {
101
                if(predictions[i].Prediction[0] == 1)
104
                if(predictions[i].Prediction[2] < (1 - Confidence))
102 105
                {
103 106
                    _log.Debug($"Rejecting an outlier activity: {predictions[i].Prediction[1]}, p-value: {predictions[i].Prediction[2]}, from: {data[i].startTime}");
104 107
                } else
Server/ServerApp/Predictor/IPredictionController.cs
20 20
        /// <summary>
21 21
        /// Trains all predictors. The training data are taken from given path location.
22 22
        /// </summary>
23
        /// <param name="locationKey">A string identifier of the location for which to train a predictor.</param>
24
        void Train(string locationKey = null);
23
        void Train();
25 24

  
26 25
        /// <summary>
27 26
        /// Loads trained predictors from files in <c>path</c>.
28 27
        /// </summary>
29
        /// <param name="locationKey">A string identifier of the location for which to load a predictor.</param>
30 28
        /// <param name="path">A path to folder with trained prediction model.</param>
31
        void Load(string locationKey = null, string path = null);
29
        void Load(string path);
30

  
31
        /// <summary>
32
        /// Rolls back the predictors to previous trained instances.
33
        /// </summary>
34
        void Rollback();
32 35

  
33 36
        /// <summary>
34 37
        /// Predicts turnout level at given time supposing given weather conditions.
......
36 39
        /// <param name="request">A request with time and weather information.</param>
37 40
        /// <returns>A server response filled with predictions.</returns>
38 41
        Response Predict(Request request);
42

  
43
        /// <summary>
44
        /// Returns the filenames of data files used to train current predictor instances
45
        /// </summary>
46
        /// <returns>Collection of filenames</returns>
47
        IEnumerable<string> GetDataFileNames();
39 48
    }
40 49
}
Server/ServerApp/Predictor/NaiveBayesClassifier.cs
39 39
            _mlContext = new MLContext();
40 40
        }
41 41

  
42
        public NaiveBayesClassifier(string filename) : this()
43
        {
44
            DataViewSchema modelSchema;
45
            this._trainedModel = _mlContext.Model.Load(filename, out modelSchema);
46
            // TODO check if the loaded model has valid input and output schema
47
            this._predictionEngine = _mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(this._trainedModel);
48
        }
49

  
42 50
        public void Fit(IEnumerable<ModelInput> trainInput)
43 51
        {
44 52
            this._trainingDataView = _mlContext.Data.LoadFromEnumerable(trainInput);
Server/ServerApp/Predictor/PredictionController.cs
9 9
using Newtonsoft.Json;
10 10
using ServerApp.WeatherPredictionParser;
11 11
using ServerApp.Parser.OutputInfo;
12
using System.Reflection;
13 12
using log4net;
13
using System.IO;
14
using System.Text.RegularExpressions;
15
using System.Linq;
14 16

  
15 17
namespace ServerApp.Predictor
16 18
{
......
21 23
    {
22 24
        private static readonly ILog _log = LogManager.GetLogger(typeof(PredictionController));
23 25

  
26
        /// <summary>
27
        /// ID of the current predictor instances.
28
        /// </summary>
29
        private string PredictorID;
30

  
24 31
        /// <summary>
25 32
        /// Configuration of the <c>Predictor</c>
26 33
        /// </summary>
27
        private PredictorConfiguration Configuration;
34
        public PredictorConfiguration Configuration { get; set; }
35

  
36
        /// <summary>
37
        /// Names of the files used to train the current predictor instances.
38
        /// </summary>
39
        private IEnumerable<string> DataFilenames;
28 40

  
29
        private List<IPredictor> Predictors;
41
        /// <summary>
42
        /// Current predictor instances
43
        /// </summary>
44
        private IPredictor[] Predictors;
30 45

  
31 46
        /// <summary>
32 47
        /// A reference to a data parser.
......
49 64
        /// <param name="dataParser">A data parser used to get training data.</param>
50 65
        public PredictionController(IJsonParser weatherService, IDataParser dataParser, string pathToConfig = null)
51 66
        {
67
            // TODO look for existing predictors
52 68
            _log.Info("Constructing a new PredictionController instance.");
53 69
            this.weatherService = weatherService;
54 70
            // load config or get the default one
......
58 74
            }
59 75
            try
60 76
            {
61
                string json = System.IO.File.ReadAllText(pathToConfig);
77
                string json = File.ReadAllText(pathToConfig);
62 78
                this.Configuration = JsonConvert.DeserializeObject<PredictorConfiguration>(json);
63 79
            }
64 80
            catch (System.IO.IOException e)
......
69 85
            }
70 86

  
71 87
            this.DataParser = dataParser;
72
            this.Predictors = new List<IPredictor>();
88
            this.Predictors = new IPredictor[this.Configuration.PredictorCount];
73 89
            this.FeatureExtractor = new FeatureExtractor(this.DataParser, this.Configuration);
90
            this.DataFilenames = dataParser.WeatherDataUsed.Concat(dataParser.ActivityDataUsed);
91
            this.PredictorID = DateTime.Now.ToString();
74 92

  
75 93
            for (int i = 0; i < this.Configuration.PredictorCount; i++)
76 94
            {
77
                Predictors.Add(new NaiveBayesClassifier());
95
                Predictors[i] = new NaiveBayesClassifier();
78 96
            }
79 97
            PredictorConfiguration.SaveConfig(PredictorConfiguration.DEFAULT_CONFIG_PATH, Configuration);
80 98
        }
......
83 101
            return new List<string>(this.Configuration.BuildingsToAreas.Keys);
84 102
        }
85 103

  
86
        public void Load(string locationKey = null, string path = null)
104
        public void Load(string predictorID)
87 105
        {
88
            if (locationKey is null)
89
            {
90
                throw new NotImplementedException();
91
            }
92
            else
106
            DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath);
107
            FileInfo[] files = di.GetFiles($"{predictorID}_*.zip");
108
            IPredictor[] newPredictors = new IPredictor[this.Configuration.PredictorCount];
109
            for (int i = 0; i < this.Configuration.PredictorCount; i++)
93 110
            {
94
                throw new NotImplementedException();
111
                newPredictors[i] = new NaiveBayesClassifier(Array.Find(files, f => Regex.IsMatch(f.Name, $"{predictorID}_{i}.zip")).FullName);
95 112
            }
113
            this.Predictors = newPredictors;
114
            files = di.GetFiles($"{predictorID}.txt");
115
            this.DataFilenames = File.ReadLines(files[0].FullName);
116
            this.PredictorID = predictorID;
117
        }
118

  
119
        public void Rollback()
120
        {
121

  
96 122
        }
97 123

  
98 124
        public Response Predict(Request request)
......
132 158
        private Prediction PredictSingle(Request request, DateTime predictionTime)
133 159
        {
134 160
            double[] predictedValues = new double[this.Configuration.BuildingsToAreas.Count];
135
            string[] predictedLabels = new string[this.Predictors.Count];
136
            for (int i = 0; i < this.Predictors.Count; i++)
161
            string[] predictedLabels = new string[this.Predictors.Length];
162
            for (int i = 0; i < this.Predictors.Length; i++)
137 163
            {
138 164
                if (request.useWeather)
139 165
                {
......
187 213
            return prediction;
188 214
        }
189 215

  
190
        public void Train(string locationKey = null)
216
        public void Train()
191 217
        {
192
            if (locationKey is null)
193
            // train all predictors
194
            {
195
                DataParser.Parse(DateTime.MinValue, DateTime.MaxValue, this.Configuration.TimeResolution, wholeDay: false);
196
                for (int i = 0; i < this.Predictors.Count; i++)
218
            DataParser.Parse(DateTime.MinValue, DateTime.MaxValue, this.Configuration.TimeResolution, wholeDay: false);
219
                for (int i = 0; i < this.Predictors.Length; i++)
197 220
                {
198 221
                    // train on all available data
199 222
                    List<ModelInput> data = FeatureExtractor.PrepareTrainingInput(i);
200 223
                    Console.WriteLine("Training predictor with {0} samples.", data.Count);
201 224
                    this.Predictors[i].Fit(data);
202 225
                }
203
            }
204
            else
205
            // train specified predictor only
206
            {
207
                throw new NotImplementedException();
208
            }
209 226
        }
210 227

  
211

  
228
        public IEnumerable<string> GetDataFileNames()
229
        {
230
            return this.DataFilenames;
231
        }
212 232
    }
213 233
}
Server/ServerApp/Predictor/PredictorConfiguration.cs
9 9

  
10 10
namespace ServerApp.Predictor
11 11
{
12
    class PredictorConfiguration
12
    public class PredictorConfiguration
13 13
    {
14
        public static readonly string DEFAULT_CONFIG_PATH = Path.GetFullPath(Path.GetDirectoryName(Environment.CurrentDirectory) + @"\Predictor.config");
14
        public static readonly string DEFAULT_CONFIG_PATH = Path.Combine(Path.GetDirectoryName(Environment.CurrentDirectory), "Predictor.config");
15

  
16
        public static readonly string DEFAULT_MODEL_DATA_PATH = Path.Combine(Path.GetDirectoryName(Environment.CurrentDirectory), "model_data");
15 17

  
16 18
        public int TimeResolution { get; set; }
17 19

  
......
19 21

  
20 22
        public int PredictorCount { get; set; }
21 23

  
24
        public string ModelDataPath { get; set; }
25

  
22 26
        public static PredictorConfiguration LoadConfig(string filename)
23 27
        {
24 28
            string json = System.IO.File.ReadAllText(filename);
......
44 48
            return new PredictorConfiguration
45 49
            {
46 50
                TimeResolution = 3,
51
                ModelDataPath = DEFAULT_MODEL_DATA_PATH,
47 52
                PredictorCount = 7,
48 53
                BuildingsToAreas = new Dictionary<string, int>
49 54
                {

Také k dispozici: Unified diff