/// <summary> /// Save the dataset to a CSV file. /// </summary> /// <param name="targetFile">The target file.</param> /// <param name="format">The format to use.</param> /// <param name="set">The data set.</param> public static void SaveCSV(FileInfo targetFile, CSVFormat format, IMLDataSet set) { try { var file = new StreamWriter(targetFile.ToString()); foreach (IMLDataPair data in set) { var line = new StringBuilder(); for (int i = 0; i < data.Input.Count; i++) { double d = data.Input[i]; BasicFile.AppendSeparator(line, format); line.Append(format.Format(d, EncogFramework.DefaultPrecision)); } for (int i = 0; i < data.Ideal.Count; i++) { double d = data.Ideal[i]; BasicFile.AppendSeparator(line, format); line.Append(format.Format(d, EncogFramework.DefaultPrecision)); } file.WriteLine(line); } file.Close(); } catch (IOException ex) { throw new EncogError(ex); } }
/// <summary> /// Prepare the output file, write headers if needed. /// </summary> /// /// <param name="outputFile">The name of the output file.</param> /// <returns>The output stream for the text file.</returns> private StreamWriter AnalystPrepareOutputFile(FileInfo outputFile) { try { var tw = new StreamWriter(outputFile.OpenWrite()); // write headers, if needed if (ProduceOutputHeaders) { var line = new StringBuilder(); // first, handle any input fields if (_inputCount > 0) { for (int i = 0; i < _inputCount; i++) { BasicFile.AppendSeparator(line, Format); line.Append("\""); line.Append("input:" + i); line.Append("\""); } } // now, handle the ideal fields if (_idealCount > 0) { for (int i = 0; i < _idealCount; i++) { BasicFile.AppendSeparator(line, Format); line.Append("\""); line.Append("ideal:" + i); line.Append("\""); } } // now, handle the output fields if (_outputCount > 0) { for (int i = 0; i < _outputCount; i++) { BasicFile.AppendSeparator(line, Format); line.Append("\""); line.Append("output:" + i); line.Append("\""); } } tw.WriteLine(line.ToString()); } return(tw); } catch (IOException e) { throw new QuantError(e); } }
/// <summary> /// Add headings for a raw file. /// </summary> /// /// <param name="line">The line to write the raw headings to.</param> /// <param name="prefix">The prefix to place.</param> /// <param name="format">The format to use.</param> public void AddRawHeadings(StringBuilder line, String prefix, CSVFormat format) { int subFields = ColumnsNeeded; for (int i = 0; i < subFields; i++) { String str = CSVHeaders.TagColumn(_name, i, _timeSlice, subFields > 1); BasicFile.AppendSeparator(line, format); line.Append('\"'); if (prefix != null) { line.Append(prefix); } line.Append(str); line.Append('\"'); } }
/// <summary> /// Encode the headers used by this field. /// </summary> /// /// <returns>A string containing a comma separated list with the headers.</returns> public String EncodeHeaders() { var line = new StringBuilder(); switch (_action) { case NormalizationAction.SingleField: BasicFile.AppendSeparator(line, CSVFormat.EgFormat); line.Append('\"'); line.Append(_name); line.Append('\"'); break; case NormalizationAction.Equilateral: for (int i = 0; i < _classes.Count - 1; i++) { BasicFile.AppendSeparator(line, CSVFormat.EgFormat); line.Append('\"'); line.Append(_name); line.Append('-'); line.Append(i); line.Append('\"'); } break; case NormalizationAction.OneOf: for (int i = 0; i < _classes.Count; i++) { BasicFile.AppendSeparator(line, CSVFormat.EgFormat); line.Append('\"'); line.Append(_name); line.Append('-'); line.Append(i); line.Append('\"'); } break; default: return(null); } return(line.ToString()); }