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/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
}

Také k dispozici: Unified diff