private List <ForecastResult> RunForecasts(string[] forecastToRun, DateTime t, double estimationFactor) { List <ForecastResult> allResults = new List <ForecastResult>(); for (int i = 0; i < forecastToRun.Length; i++) { ForecastEquation eq = null; try { eq = GetEquation(forecastToRun[i]); FireOnLogEvent(eq.Name, ""); ForecastResult result = eq.Evaluate(t, checkBoxLookAhead.Checked, estimationFactor); allResults.Add(result); } catch (Exception e) { var res = new ForecastResult(); string msg = e.Message; if (eq != null) { res.Equation.Name = " -->> ERROR " + eq.Name; msg = eq.Name + " " + msg; FireOnLogEvent(msg, ""); } res.Details.Add(msg); allResults.Add(res); } } return(allResults); }
private void buttonTestAllYears_Click(object sender, EventArgs e) { Logger.LogHistory.Clear(); Logger.EnableLogger(true); FileUtility.CleanTempPath(); string currentForecast = ""; try { Performance perf = new Performance(); Reclamation.TimeSeries.Parser.SeriesExpressionParser.Debug = false; string[] forecastToRun = forecastList1.SelectedItems; Cursor = Cursors.WaitCursor; this.textBoxOutput.Lines = new string[] { "calculating " + forecastToRun.Length + " forecasts." }; Application.DoEvents(); var t = dateTimePickerForecastDate.Value; double[] estimationFactors = { 1.0 }; if (this.comboBoxSubsequent.Text == "100% 120% 80%") { estimationFactors = new double[] { 1.0, 1.2, 0.8 }; } else if (this.comboBoxSubsequent.Text == "100% 150% 50%") { estimationFactors = new double[] { 1.0, 1.5, 0.5 }; } for (int i = 0; i < forecastToRun.Length; i++) { ForecastEquation eq = GetEquation(forecastToRun[i]); var tbl = eq.RunHistoricalForecasts(eq.StartYear, eq.EndYear + 1, checkBoxLookAhead.Checked, t.Month, estimationFactors); var fn = FileUtility.GetTempFileName(".csv"); CsvFile.WriteToCSV(tbl, fn, false); List <string> output = new List <string>(); output.AddRange(this.textBoxOutput.Lines); output.Add("completed " + forecastToRun[i] + " elapsed seconds = " + perf.ElapsedSeconds.ToString("F0")); this.textBoxOutput.Lines = output.ToArray(); Application.DoEvents(); System.Diagnostics.Process.Start(fn); } perf.Report("done with forecast performance ."); //this.textBoxOutput.Lines = query; } catch (Exception ex) { MessageBox.Show("Error: while running " + currentForecast + "\n" + ex.Message); } finally { Cursor = Cursors.Default; } }
public double[] ComputeCoefficients(ForecastEquation eq, string pathToR) { var tbl = eq.ComputeHistoricalCoefficients(eq.StartYear, eq.EndYear); // perf.Report("done."); // 1.2 seconds with cache/ 33 seconds without dataFile = FileUtility.GetTempFileName(".csv"); CsvFile.WriteToCSV(tbl, dataFile, false); dataFile = dataFile.Replace("\\", "/"); var rInput = new List <string>(); rInput.Add("# Forecast " + eq.Name); rInput.Add("a <- read.csv(\"" + dataFile + "\")"); rInput.Add("b <- subset(a, select=-Year)"); rInput.Add("cor(b)"); rInput.Add("summary(b)"); string s = "fit <- lm(Y1 ~ "; for (int i = 1; i < tbl.Columns.Count - 1; i++) { s += " + X" + i; } s += ", data=a)"; rInput.Add(s); rInput.Add("options(width=240)"); rInput.Add("summary(fit)"); rInput.Add("coefficients(fit)"); string rFile = FileUtility.GetTempFileName(".txt"); TextFile rtf = new TextFile(rInput.ToArray()); rtf.SaveAs(rFile); rFile = rFile.Replace("\\", "/"); var exe = Path.Combine(pathToR, "R\\bin\\R.exe"); if (!File.Exists(exe)) { throw new Exception("Could not find the R statistic program. It should be in a sub directory R"); } ProgramRunner pr = new ProgramRunner(); pr.Run(exe, "--no-restore --no-save --quiet < \"" + rFile); pr.WaitForExit(); Coefficients = GetCoefficients(pr.Output); Output = pr.Output; return(Coefficients); }
private ForecastEquation GetEquation(string forecastName) { currentForecast = forecastName; var fileName = FileUtility.GetTempFileName(".csv"); xls.SaveSheetToCsv(forecastName, fileName); Logger.WriteLine("Run " + forecastName); ForecastEquation eq = new ForecastEquation(fileName); return(eq); }
private void buttonRecomputeCoeficients_Click(object sender, EventArgs e) { // Performance perf = new Performance(); string fn = FileUtility.GetTempFileName(".csv"); xls.SaveSheetToCsv(xls.ActiveSheetName, fn); xls.Save(); // must save to get back to *.xls from *.csv eq = new ForecastEquation(fn); var cache = new HydrometDataCache(); cache.Add(eq.GetCbttPcodeList().ToArray(), new DateTime(eq.StartYear - 1, 10, 1), new DateTime(eq.EndYear, 9, 30)); HydrometMonthlySeries.Cache = cache; var dir = Path.GetDirectoryName(this.textBoxExcelFileName.Text); try { Cursor = Cursors.WaitCursor; Application.DoEvents(); R = new CoefficientCalculator(); var newCoefficients = R.ComputeCoefficients(eq, Path.GetDirectoryName(Application.ExecutablePath)); var dlg = new RegressionResults(); dlg.CompareToHistoryClicked += new EventHandler <EventArgs>(dlg_CompareToHistoryClicked); dlg.Output = R.Output; dlg.DataFile = R.dataFile; dlg.CoeficientsExisting = CoefficientCalculator.FormatCoefficients(eq.coefficients); dlg.CoefficientsComputed = CoefficientCalculator.FormatCoefficients(newCoefficients); if (dlg.ShowDialog() == DialogResult.OK) // save { // save back to excel. xls.UpdateCoeficients(xls.ActiveSheetName, newCoefficients); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { Cursor = Cursors.Default; } }
static void Main(string[] args) { if (args.Length != 5 && args.Length != 2) { Console.WriteLine("HydrometForecast.exe basin.csv date level look-ahead output"); Console.WriteLine("Where: "); Console.WriteLine(" basin.csv -- name of csv input file"); Console.WriteLine(" date -- date to run forecaset 2017-01-01"); Console.WriteLine(" level -- subsequent conditions 1.0 normal 0.8 for 80%, etc... "); Console.WriteLine(" look-ahead -- perfect forecast 0 or 1"); Console.WriteLine(" output -- filename for output"); Console.WriteLine("Example: HydrometForecast heise.csv 2016-1-1 1.0 0 output.txt"); Console.WriteLine("HydrometForecast.exe basin.csv output.csv"); return; } var filename = args[0]; ForecastEquation eq = new ForecastEquation(filename); if (args.Length == 2) { var cache = new HydrometDataCache(); cache.Add(eq.GetCbttPcodeList().ToArray(), new DateTime(eq.StartYear - 1, 10, 1), new DateTime(eq.EndYear, 9, 30)); HydrometMonthlySeries.Cache = cache; var tbl = eq.ComputeHistoricalCoefficients(eq.StartYear, eq.EndYear); CsvFile.WriteToCSV(tbl, args[1], false); } else { DateTime t = DateTime.Parse(args[1]); double level = double.Parse(args[2]); int lookAhead = int.Parse(args[3]); string outputFileName = args[4]; ForecastResult result = eq.Evaluate(t, lookAhead == 1, level); File.WriteAllLines(outputFileName, result.Details.ToArray()); } }
public ForecastResult() { ForecastPeriod = "error"; AverageRunoff = 0; Equation = new ForecastEquation(); }