Search in sources :

Example 1 with GenMunger

use of hex.genmodel.GenMunger in project h2o-3 by h2oai.

the class MungeCsv method main.

/**
   * CSV reader and predictor test program.
   *
   * @param args Command-line args.
   * @throws Exception
   */
public static void main(String[] args) throws Exception {
    parseArgs(args);
    GenMunger rawMunger;
    rawMunger = (hex.genmodel.GenMunger) Class.forName(assemblyClassName).newInstance();
    BufferedReader input = new BufferedReader(new FileReader(inputCSVFileName));
    BufferedWriter output = new BufferedWriter(new FileWriter(outputCSVFileName));
    // Emit outputCSV column names.
    String[] rawHeader = rawMunger.outNames();
    StringBuilder header = new StringBuilder();
    for (int i = 0; i < rawHeader.length; ++i) {
        header.append("\"").append(rawHeader[i]).append("\"");
        if (i < rawHeader.length - 1)
            header.append(",");
    }
    output.write(header.toString());
    output.write("\n");
    // Loop over inputCSV one row at a time.
    int lineNum = 0;
    String line;
    try {
        while ((line = input.readLine()) != null) {
            lineNum++;
            // skip the header.
            if (lineNum == 1)
                continue;
            // Parse the CSV line.  Somewhat handles quoted commas.  But this ain't no parser test!
            RowData row;
            try {
                row = parseDataRow(line, rawMunger);
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
                System.out.println("Failed to parse row: " + lineNum);
                throw new RuntimeException();
            }
            RowData mungedRow = rawMunger.fit(row);
            for (int i = 0; i < rawMunger.outNames().length; ++i) {
                Object val = mungedRow == null ? Double.NaN : mungedRow.get(rawMunger.outNames()[i]);
                if (val instanceof Double)
                    output.write(String.valueOf(val));
                else
                    output.write("\"" + val + "\"");
                if (i < rawMunger.outNames().length - 1)
                    output.write(",");
            }
            output.write("\n");
        }
    } catch (Exception e) {
        System.out.println("Caught exception on line " + lineNum);
        System.out.println("");
        e.printStackTrace();
        System.exit(1);
    } finally {
        // Clean up.
        output.close();
        input.close();
    }
    // Predictions were successfully generated.  Calling program can now compare them with something.
    System.exit(0);
}
Also used : FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter) RowData(hex.genmodel.easy.RowData) GenMunger(hex.genmodel.GenMunger) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Aggregations

GenMunger (hex.genmodel.GenMunger)1 RowData (hex.genmodel.easy.RowData)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1