Revize 0d31f7e0
Přidáno uživatelem Roman Kalivoda před téměř 4 roky(ů)
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 |
using log4net; |
|
12 | 14 |
|
13 | 15 |
namespace ServerApp.Predictor |
14 | 16 |
{ |
... | ... | |
17 | 19 |
/// </summary> |
18 | 20 |
public class PredictionController : IPredictionController |
19 | 21 |
{ |
22 |
private static readonly ILog _log = LogManager.GetLogger(typeof(PredictionController)); |
|
23 |
|
|
20 | 24 |
/// <summary> |
21 | 25 |
/// Configuration of the <c>Predictor</c> |
22 | 26 |
/// </summary> |
... | ... | |
45 | 49 |
/// <param name="dataParser">A data parser used to get training data.</param> |
46 | 50 |
public PredictionController(IJsonParser weatherService, IDataParser dataParser, string pathToConfig = null) |
47 | 51 |
{ |
52 |
_log.Info("Constructing a new PredictionController instance."); |
|
48 | 53 |
this.weatherService = weatherService; |
49 | 54 |
// load config or get the default one |
50 | 55 |
if (pathToConfig is null) |
... | ... | |
55 | 60 |
{ |
56 | 61 |
string json = System.IO.File.ReadAllText(pathToConfig); |
57 | 62 |
this.Configuration = JsonConvert.DeserializeObject<PredictorConfiguration>(json); |
58 |
} catch (System.IO.IOException e) |
|
63 |
} |
|
64 |
catch (System.IO.IOException e) |
|
59 | 65 |
{ |
60 | 66 |
Console.WriteLine("Warning: could not find a configuration file, creating a new one:"); |
61 | 67 |
Console.WriteLine(e.Message.PadLeft(4)); |
... | ... | |
91 | 97 |
|
92 | 98 |
public Response Predict(Request request) |
93 | 99 |
{ |
100 |
_log.Info($"Received a prediction request: endDate={request.useEndDate}, weather={request.useWeather}"); |
|
94 | 101 |
DateTime start = new DateTime(year: request.start.year, month: request.start.month, day: request.start.day, hour: request.start.hour, minute: 0, second: 0); |
95 | 102 |
List<Prediction> predictions = new List<Prediction>(); |
96 | 103 |
if (request.useEndDate) |
... | ... | |
99 | 106 |
DateTime current = start; |
100 | 107 |
while (current < end) |
101 | 108 |
{ |
109 |
_log.Debug($"Predicting for date {current.Date.ToShortDateString()}"); |
|
102 | 110 |
while (current.Hour < Date.MAX_HOUR) |
103 | 111 |
{ |
112 |
_log.Debug($"Predicting for time {current.TimeOfDay.ToString()}"); |
|
104 | 113 |
var prediction = PredictSingle(request, current); |
105 | 114 |
predictions.Add(prediction); |
106 | 115 |
current = current.AddHours(this.Configuration.TimeResolution); |
107 | 116 |
} |
108 | 117 |
current = current.AddHours(23 - current.Hour + Date.MIN_HOUR); |
109 | 118 |
} |
110 |
} else |
|
119 |
} |
|
120 |
else |
|
111 | 121 |
{ |
112 |
if (request.useWeather) |
|
113 |
{ |
|
114 |
predictions.Add(PredictSingle(request, start)); |
|
115 |
} |
|
122 |
_log.Debug("Predicting for single DateTime."); |
|
123 |
predictions.Add(PredictSingle(request, start)); |
|
116 | 124 |
} |
117 | 125 |
var response = new Response(); |
118 | 126 |
response.hoursPerSegment = Configuration.TimeResolution; |
119 | 127 |
response.predicitons = predictions.ToArray(); |
128 |
_log.Debug($"Created a response."); |
|
120 | 129 |
return response; |
121 | 130 |
} |
122 | 131 |
|
... | ... | |
128 | 137 |
{ |
129 | 138 |
if (request.useWeather) |
130 | 139 |
{ |
140 |
_log.Debug("Predicting for requested weather."); |
|
131 | 141 |
predictedLabels[i] = this.Predictors[i].Predict(new ModelInput |
132 | 142 |
{ |
133 | 143 |
Rain = (float)request.rain, |
... | ... | |
139 | 149 |
} |
140 | 150 |
else |
141 | 151 |
{ |
142 |
List<WeatherInfo> weatherInfos = weatherService.GetPredictionForTime(from: current, to: current.AddHours(this.Configuration.TimeResolution)); |
|
152 |
_log.Debug("Retrieving weather info from the weather service."); |
|
153 |
weatherService.ParsePrediction(); |
|
154 |
WeatherInfo weatherInfo = weatherService.Current; |
|
143 | 155 |
predictedLabels[i] = this.Predictors[i].Predict(new ModelInput |
144 | 156 |
{ |
145 |
Rain = weatherInfos[0].rain,
|
|
146 |
Temp = (float)weatherInfos[0].temp,
|
|
147 |
Wind = (float)weatherInfos[0].wind,
|
|
157 |
Rain = weatherInfo.rain, |
|
158 |
Temp = (float)weatherInfo.temp, |
|
159 |
Wind = (float)weatherInfo.wind, |
|
148 | 160 |
Hour = current.Hour, |
149 | 161 |
Time = current |
150 | 162 |
}); |
... | ... | |
164 | 176 |
hour = current.Hour |
165 | 177 |
}; |
166 | 178 |
prediction.predictions = predictedValues; |
179 |
_log.Debug($"Created prediction for DateTime: {prediction.dateTime}"); |
|
167 | 180 |
return prediction; |
168 | 181 |
} |
169 | 182 |
|
Také k dispozici: Unified diff
Re #8953 tests