/// <summary> /// Process the file and output to the target file. /// </summary> /// <param name="target">The target file to write to.</param> public void Process(string target) { var csv = new ReadCSV(InputFilename.ToString(), ExpectInputHeaders, InputFormat); TextWriter tw = new StreamWriter(target); ResetStatus(); while (csv.Next()) { var line = new StringBuilder(); UpdateStatus(false); line.Append(GetColumnData(FileData.Date, csv)); line.Append(" "); line.Append(GetColumnData(FileData.Time, csv)); line.Append(";"); line.Append(InputFormat.Format(double.Parse(GetColumnData(FileData.Open, csv)), Precision)); line.Append(";"); line.Append(InputFormat.Format(double.Parse(GetColumnData(FileData.High, csv)), Precision)); line.Append(";"); line.Append(InputFormat.Format(double.Parse(GetColumnData(FileData.Low, csv)), Precision)); line.Append(";"); line.Append(InputFormat.Format(double.Parse(GetColumnData(FileData.Close, csv)), Precision)); line.Append(";"); line.Append(InputFormat.Format(double.Parse(GetColumnData(FileData.Volume, csv)), Precision)); tw.WriteLine(line.ToString()); } ReportDone(false); csv.Close(); tw.Close(); }
/// <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> /// Process the file. /// </summary> /// /// <param name="outputFile">The output file.</param> /// <param name="method">THe method to use.</param> public void Process(FileInfo outputFile, IMLMethod method) { var csv = new ReadCSV(InputFilename.ToString(), ExpectInputHeaders, InputFormat); IMLData output; int outputLength = _analyst.DetermineTotalInputFieldCount(); StreamWriter tw = PrepareOutputFile(outputFile); ResetStatus(); while (csv.Next()) { UpdateStatus(false); var row = new LoadedRow(csv, _outputColumns); double[] inputArray = AnalystNormalizeCSV.ExtractFields(_analyst, _analystHeaders, csv, outputLength, true); if (_series.TotalDepth > 1) { inputArray = _series.Process(inputArray); } if (inputArray != null) { IMLData input = new BasicMLData(inputArray); // evaluation data if ((method is IMLClassification) && !(method is IMLRegression)) { // classification only? output = new BasicMLData(1); output[0] = ((IMLClassification)method).Classify(input); } else { // regression output = ((IMLRegression)method).Compute(input); } // skip file data int index = _fileColumns; int outputIndex = 0; // display output foreach (AnalystField field in _analyst.Script.Normalize.NormalizedFields) { if (_analystHeaders.Find(field.Name) != -1) { if (field.Output) { if (field.Classify) { // classification ClassItem cls = field.DetermineClass( outputIndex, output.Data); outputIndex += field.ColumnsNeeded; if (cls == null) { row.Data[index++] = "?Unknown?"; } else { row.Data[index++] = cls.Name; } } else { // regression double n = output[outputIndex++]; n = field.DeNormalize(n); row.Data[index++] = InputFormat .Format(n, Precision); } } } } } WriteRow(tw, row); } ReportDone(false); tw.Close(); csv.Close(); }
/// <summary> /// Write the CSV. /// </summary> /// /// <param name="filename">The target filename.</param> private void WriteCSV(FileInfo filename) { StreamWriter tw = null; try { ResetStatus(); tw = new StreamWriter(filename.Create()); // write the headers if (ExpectInputHeaders) { var line = new StringBuilder(); foreach (BaseCachedColumn column in Columns) { if (column.Output) { if (line.Length > 0) { line.Append(InputFormat.Separator); } line.Append("\""); line.Append(column.Name); line.Append("\""); } } tw.WriteLine(line.ToString()); } // starting and ending index int beginningIndex = BeginningIndex; int endingIndex = EndingIndex; // write the file data for (int row = beginningIndex; row <= endingIndex; row++) { UpdateStatus("Writing data"); var line_0 = new StringBuilder(); foreach (BaseCachedColumn column_1 in Columns) { if (column_1.Output) { if (line_0.Length > 0) { line_0.Append(InputFormat.Separator); } double d = column_1.Data[row]; line_0.Append(InputFormat.Format(d, Precision)); } } tw.WriteLine(line_0.ToString()); } } catch (IOException e) { throw (new QuantError(e)); } finally { if (tw != null) { tw.Close(); } } }