Revize 3c4b53fe
Přidáno uživatelem Roman Kalivoda před téměř 4 roky(ů)
Server/ServerApp/Predictor/PredictionController.cs | ||
---|---|---|
77 | 77 |
string json = File.ReadAllText(pathToConfig); |
78 | 78 |
this.Configuration = JsonConvert.DeserializeObject<PredictorConfiguration>(json); |
79 | 79 |
} |
80 |
catch (System.IO.IOException e)
|
|
80 |
catch (System.IO.FileNotFoundException e)
|
|
81 | 81 |
{ |
82 | 82 |
Console.WriteLine("Warning: could not find a configuration file, creating a new one:"); |
83 | 83 |
Console.WriteLine(e.Message.PadLeft(4)); |
... | ... | |
87 | 87 |
this.DataParser = dataParser; |
88 | 88 |
this.Predictors = new IPredictor[this.Configuration.PredictorCount]; |
89 | 89 |
this.FeatureExtractor = new FeatureExtractor(this.DataParser, this.Configuration); |
90 |
this.DataFilenames = dataParser.WeatherDataUsed.Concat(dataParser.ActivityDataUsed); |
|
91 |
this.PredictorID = DateTime.Now.ToString(); |
|
92 | 90 |
|
93 |
for (int i = 0; i < this.Configuration.PredictorCount; i++) |
|
91 |
DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath); |
|
92 |
FileInfo[] files = di.GetFiles(); |
|
93 |
if (Array.FindAll(files, f => Regex.IsMatch(f.Name, @"[-]?\d+_\d+.zip")).GroupBy(f => f.Name.Split("_.".ToCharArray())[0]).OrderBy(f => DateTime.FromBinary(Convert.ToInt64(f.Key))).Any()) |
|
94 | 94 |
{ |
95 |
Predictors[i] = new NaiveBayesClassifier(); |
|
95 |
_log.Info("Found existing predictors, loading the newest."); |
|
96 |
this.Load(Array.FindAll(files, f => Regex.IsMatch(f.Name, @"[-]?\d+_\d+.zip")).GroupBy(f => f.Name.Split("_.".ToCharArray())[0]).OrderBy(f => DateTime.FromBinary(Convert.ToInt64(f.Key))).Last().Select(f => f.Name.Split("_".ToCharArray())[0]).First()); |
|
97 |
} |
|
98 |
else |
|
99 |
{ |
|
100 |
_log.Info("No predictors found, creating new ones"); |
|
101 |
for (int i = 0; i < this.Configuration.PredictorCount; i++) |
|
102 |
{ |
|
103 |
Predictors[i] = new NaiveBayesClassifier(); |
|
104 |
} |
|
96 | 105 |
} |
97 | 106 |
PredictorConfiguration.SaveConfig(PredictorConfiguration.DEFAULT_CONFIG_PATH, Configuration); |
98 | 107 |
} |
... | ... | |
101 | 110 |
return new List<string>(this.Configuration.BuildingsToAreas.Keys); |
102 | 111 |
} |
103 | 112 |
|
104 |
public void Load(string predictorID)
|
|
113 |
public int Load(string predictorID)
|
|
105 | 114 |
{ |
106 | 115 |
DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath); |
107 | 116 |
FileInfo[] files = di.GetFiles($"{predictorID}_*.zip"); |
108 |
IPredictor[] newPredictors = new IPredictor[this.Configuration.PredictorCount]; |
|
117 |
if (Array.FindAll(files, f => Regex.IsMatch(f.Name, $@"{predictorID}_\d+.zip")).Any()){ |
|
118 |
IPredictor[] newPredictors = new IPredictor[this.Configuration.PredictorCount]; |
|
119 |
try |
|
120 |
{ |
|
121 |
for (int i = 0; i < this.Configuration.PredictorCount; i++) |
|
122 |
{ |
|
123 |
newPredictors[i] = new NaiveBayesClassifier(Array.Find(files, f => Regex.IsMatch(f.Name, $"{predictorID}_{i}.zip")).FullName); |
|
124 |
} |
|
125 |
this.Predictors = newPredictors; |
|
126 |
files = di.GetFiles($"{predictorID}.txt"); |
|
127 |
this.DataFilenames = File.ReadLines(files[0].FullName); |
|
128 |
this.PredictorID = predictorID; |
|
129 |
} catch (FileNotFoundException e) |
|
130 |
{ |
|
131 |
_log.Error(e.ToString()); |
|
132 |
return 2; |
|
133 |
} |
|
134 |
} else |
|
135 |
{ |
|
136 |
// TODO indicate exception |
|
137 |
_log.Debug("Could not find predictor with given predictorID"); |
|
138 |
return 1; |
|
139 |
} |
|
140 |
return 0; |
|
141 |
} |
|
142 |
|
|
143 |
public void Save() |
|
144 |
{ |
|
145 |
DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath); |
|
146 |
|
|
109 | 147 |
for (int i = 0; i < this.Configuration.PredictorCount; i++) |
110 | 148 |
{ |
111 |
newPredictors[i] = new NaiveBayesClassifier(Array.Find(files, f => Regex.IsMatch(f.Name, $"{predictorID}_{i}.zip")).FullName);
|
|
149 |
Predictors[i].Save(Path.Combine(di.FullName, $"{PredictorID}_{i}.zip"));
|
|
112 | 150 |
} |
113 |
this.Predictors = newPredictors; |
|
114 |
files = di.GetFiles($"{predictorID}.txt"); |
|
115 |
this.DataFilenames = File.ReadLines(files[0].FullName); |
|
116 |
this.PredictorID = predictorID; |
|
151 |
File.WriteAllLinesAsync(Path.Combine(di.FullName, $"{PredictorID}.txt"), this.DataFilenames); |
|
117 | 152 |
} |
118 | 153 |
|
119 |
public void Rollback()
|
|
154 |
public int Rollback()
|
|
120 | 155 |
{ |
156 |
DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath); |
|
157 |
FileInfo[] files = di.GetFiles(); |
|
158 |
if (Array.FindAll(files, f => Regex.IsMatch(f.Name, @"[-]?\d+_\d+.zip")).GroupBy(f => f.Name.Split("_.".ToCharArray())[0]).OrderBy(f => DateTime.FromBinary(Convert.ToInt64(f.Key))).Any()) |
|
159 |
{ |
|
160 |
string RollbackedPredictorID = Array.FindAll(files, f => Regex.IsMatch(f.Name, @"[-]?\d+_\d+.zip")).GroupBy(f => f.Name.Split("_.".ToCharArray())[0]).OrderBy(f => DateTime.FromBinary(Convert.ToInt64(f.Key))).Last().Select(f => f.Name.Split("_".ToCharArray())[0]).First(); |
|
161 |
this.Delete(this.PredictorID); |
|
162 |
return this.Load(RollbackedPredictorID); |
|
163 |
} else |
|
164 |
{ |
|
165 |
// indicate that older model does not exist |
|
166 |
return 1; |
|
167 |
} |
|
168 |
} |
|
169 |
|
|
170 |
private void Delete(string predictorID) |
|
171 |
{ |
|
172 |
DirectoryInfo di = new DirectoryInfo(Configuration.ModelDataPath); |
|
121 | 173 |
|
174 |
for (int i = 0; i < this.Configuration.PredictorCount; i++) |
|
175 |
{ |
|
176 |
File.Delete(Path.Combine(di.FullName, $"{PredictorID}_{i}.zip")); |
|
177 |
} |
|
178 |
File.Delete(Path.Combine(di.FullName, $"{PredictorID}.txt")); |
|
122 | 179 |
} |
123 | 180 |
|
124 | 181 |
public Response Predict(Request request) |
... | ... | |
216 | 273 |
public void Train() |
217 | 274 |
{ |
218 | 275 |
DataParser.Parse(DateTime.MinValue, DateTime.MaxValue, this.Configuration.TimeResolution, wholeDay: false); |
219 |
for (int i = 0; i < this.Predictors.Length; i++) |
|
220 |
{ |
|
221 |
// train on all available data |
|
222 |
List<ModelInput> data = FeatureExtractor.PrepareTrainingInput(i); |
|
223 |
Console.WriteLine("Training predictor with {0} samples.", data.Count); |
|
224 |
this.Predictors[i].Fit(data); |
|
225 |
} |
|
276 |
for (int i = 0; i < this.Predictors.Length; i++) |
|
277 |
{ |
|
278 |
// train on all available data |
|
279 |
List<ModelInput> data = FeatureExtractor.PrepareTrainingInput(i); |
|
280 |
Console.WriteLine("Training predictor with {0} samples.", data.Count); |
|
281 |
this.Predictors[i].Fit(data); |
|
282 |
} |
|
283 |
this.DataFilenames = this.DataParser.WeatherDataUsed.Concat(this.DataParser.ActivityDataUsed); |
|
284 |
this.PredictorID = DateTime.Now.ToBinary().ToString(); |
|
285 |
this.Save(); |
|
226 | 286 |
} |
227 | 287 |
|
228 | 288 |
public IEnumerable<string> GetDataFileNames() |
Také k dispozici: Unified diff
Re #9049 Implemented Rollback method