/**
   * Prepare the output file, write headers if needed.
   *
   * @param outputFile The output file.
   * @param input The number of input columns.
   * @param output The number of output columns.
   * @return The file to be written to.
   */
  private PrintWriter prepareOutputFile(final File outputFile, final int input, final int output) {
    try {
      final PrintWriter tw = new PrintWriter(new FileWriter(outputFile));

      // write headers, if needed
      if (isProduceOutputHeaders()) {
        final StringBuilder line = new StringBuilder();

        // handle provided fields, not all may be used, but all should
        // be displayed
        for (final String heading : this.getInputHeadings()) {
          BasicFile.appendSeparator(line, getFormat());
          line.append("\"");
          line.append(heading);
          line.append("\"");
        }

        // now the output fields that will be generated
        BasicFile.appendSeparator(line, getFormat());
        line.append("\"cluster\"");

        tw.println(line.toString());
      }

      return tw;

    } catch (final IOException e) {
      throw new QuantError(e);
    }
  }
  public static void saveCSV(File targetFile, CSVFormat format, MLDataSet set) {
    try {
      FileWriter outFile = new FileWriter(targetFile);
      PrintWriter out = new PrintWriter(outFile);

      for (MLDataPair data : set) {
        StringBuilder line = new StringBuilder();

        for (int i = 0; i < data.getInput().size(); i++) {
          double d = data.getInput().getData(i);
          BasicFile.appendSeparator(line, format);
          line.append(format.format(d, Encog.DEFAULT_PRECISION));
        }

        for (int i = 0; i < data.getIdeal().size(); i++) {
          double d = data.getIdeal().getData(i);
          BasicFile.appendSeparator(line, format);
          line.append(format.format(d, Encog.DEFAULT_PRECISION));
        }

        out.println(line);
      }

      out.close();
      outFile.close();

    } catch (IOException ex) {
      throw new EncogError(ex);
    }
  }
  /**
   * Write the headers.
   *
   * @param tw The output stream.
   */
  private void writeHeaders(final PrintWriter tw) {
    final StringBuilder line = new StringBuilder();
    for (final AnalystField stat : this.analyst.getScript().getNormalize().getNormalizedFields()) {
      final int needed = stat.getColumnsNeeded();

      for (int i = 0; i < needed; i++) {
        BasicFile.appendSeparator(line, getFormat());
        line.append('\"');
        line.append(CSVHeaders.tagColumn(stat.getName(), i, stat.getTimeSlice(), needed > 1));
        line.append('\"');
      }
    }
    tw.println(line.toString());
  }
Beispiel #4
0
  /**
   * Add headings for a raw file.
   *
   * @param line The line to write the raw headings to.
   * @param prefix The prefix to place.
   * @param format The format to use.
   */
  public final void addRawHeadings(
      final StringBuilder line, final String prefix, final CSVFormat format) {
    final int subFields = getColumnsNeeded();

    for (int i = 0; i < subFields; i++) {
      final String str = CSVHeaders.tagColumn(this.name, i, this.timeSlice, subFields > 1);
      BasicFile.appendSeparator(line, format);
      line.append('\"');
      if (prefix != null) {
        line.append(prefix);
      }
      line.append(str);
      line.append('\"');
    }
  }