/// <summary> /// Process the file. /// </summary> /// /// <param name="outputFile">The output file.</param> /// <param name="method">The method to use.</param> public void Process(FileInfo outputFile, IMLRegression method) { var csv = new ReadCSV(InputFilename.ToString(), ExpectInputHeaders, InputFormat); if (method.InputCount > _inputCount) { throw new AnalystError("This machine learning method has " + method.InputCount + " inputs, however, the data has " + _inputCount + " inputs."); } IMLData input = new BasicMLData(method.InputCount); StreamWriter tw = AnalystPrepareOutputFile(outputFile); ResetStatus(); while (csv.Next()) { UpdateStatus(false); var row = new LoadedRow(csv, _idealCount); int dataIndex = 0; // load the input data for (int i = 0; i < _inputCount; i++) { String str = row.Data[i]; double d = InputFormat.Parse(str); input[i] = d; dataIndex++; } // do we need to skip the ideal values? dataIndex += _idealCount; // compute the result IMLData output = method.Compute(input); // display the computed result for (int i = 0; i < _outputCount; i++) { double d = output[i]; row.Data[dataIndex++] = InputFormat.Format(d, Precision); } WriteRow(tw, row); } ReportDone(false); tw.Close(); csv.Close(); }
/// <summary> /// Read the CSV file. /// </summary> /// private void ReadFile() { ReadCSV csv = null; try { csv = new ReadCSV(InputFilename.ToString(), ExpectInputHeaders, InputFormat); ResetStatus(); int row = 0; while (csv.Next() && !ShouldStop()) { UpdateStatus("Reading data"); foreach (BaseCachedColumn column in Columns) { if (column is FileData) { if (column.Input) { var fd = (FileData)column; String str = csv.Get(fd.Index); double d = InputFormat.Parse(str); fd.Data[row] = d; } } } row++; } } finally { ReportDone("Reading data"); if (csv != null) { csv.Close(); } } }
/// <summary> /// Write the sorted output file. /// </summary> /// /// <param name="outputFile">The name of the output file.</param> private void WriteOutputFile(FileInfo outputFile) { StreamWriter tw = PrepareOutputFile(outputFile); var nonNumeric = new bool[Count]; bool first = true; ResetStatus(); // write the file foreach (LoadedRow row in _data) { UpdateStatus("Writing output"); // for the first row, determine types if (first) { for (int i = 0; i < Count; i++) { try { String str = row.Data[i]; InputFormat.Parse(str); nonNumeric[i] = false; } catch (Exception) { nonNumeric[i] = true; } } first = false; } // write the row var line = new StringBuilder(); for (int i = 0; i < Count; i++) { if (i > 0) { line.Append(","); } if (nonNumeric[i]) { line.Append("\""); line.Append(row.Data[i]); line.Append("\""); } else { line.Append(row.Data[i]); } } tw.WriteLine(line.ToString()); } ReportDone("Writing output"); // close the file tw.Close(); }