Search in sources :

Example 56 with FileReader

use of java.io.FileReader in project h2o-3 by h2oai.

the class H2OBuildVersion method emitBuildVersionJavaFileIfNecessary.

public void emitBuildVersionJavaFileIfNecessary(File fileName) {
    try {
        String projectVersion = getProjectVersion();
        String branchName = getBranch();
        String lastCommitHash = calcLastCommitHash();
        String describe = calcDescribe();
        String compiledOn = calcCompiledOn();
        String compiledBy = calcCompiledBy();
        boolean needToEmit = false;
        if (!fileName.exists()) {
            System.out.print("NOTE: emitBuildVersionJava found no file, emitting new file");
            needToEmit = true;
        }
        int found = 0;
        if (!needToEmit) {
            final String[][] stuffToCheck = { { "branchName", branchName }, { "lastCommitHash", lastCommitHash }, { "projectVersion", projectVersion } };
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            String line = br.readLine();
            while ((!needToEmit) && (line != null)) {
                for (String[] feature : stuffToCheck) {
                    String name = feature[0];
                    String value = feature[1];
                    Pattern p = Pattern.compile(".*" + name + ".*\"(.*)\";.*");
                    Matcher m = p.matcher(line);
                    boolean b = m.matches();
                    if (b) {
                        found++;
                        String v = m.group(1);
                        if (!value.equals(v)) {
                            System.out.print("NOTE: emitBuildVersionJava found a mismatch, emitting new file (" + value + " not equal " + v + ")");
                            needToEmit = true;
                            break;
                        }
                    }
                }
                line = br.readLine();
            }
            br.close();
        }
        if ((!needToEmit) && (found != 3)) {
            System.out.print("NOTE: emitBuildVersionJava found too few things to check, emitting new file");
            needToEmit = true;
        }
        if (!needToEmit) {
            System.out.print("NOTE: emitBuildVersionJava found a match, nothing to do");
            return;
        }
        PrintWriter writer = new PrintWriter(fileName);
        writer.println("package water.init;");
        writer.println("");
        writer.println("public class BuildVersion extends AbstractBuildVersion {");
        writer.println("    public String branchName()     { return \"" + branchName + "\"; }");
        writer.println("    public String lastCommitHash() { return \"" + lastCommitHash + "\"; }");
        writer.println("    public String describe()       { return \"" + describe + "\"; }");
        writer.println("    public String projectVersion() { return \"" + projectVersion + "\"; }");
        writer.println("    public String compiledOn()     { return \"" + compiledOn + "\"; }");
        writer.println("    public String compiledBy()     { return \"" + compiledBy + "\"; }");
        writer.println("}");
        writer.close();
    } catch (Exception e) {
        System.out.println("");
        System.out.println("ERROR:  H2OBuildVersion emitBuildVersionJavaFileIfNecessary failed");
        System.out.println("");
        System.out.println(e);
        System.out.println("");
        System.exit(1);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) RuntimeException(java.lang.RuntimeException) PrintWriter(java.io.PrintWriter)

Example 57 with FileReader

use of java.io.FileReader 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)

Example 58 with FileReader

use of java.io.FileReader in project h2o-2 by h2oai.

the class LicenseManager method readFile.

private String readFile(String fileName) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while (line != null) {
            sb.append(line);
            sb.append("\n");
            line = br.readLine();
        }
        return sb.toString();
    } finally {
        br.close();
    }
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 59 with FileReader

use of java.io.FileReader in project h2o-3 by h2oai.

the class PredictCsv method run.

private void run() throws Exception {
    ModelCategory category = model.getModelCategory();
    CSVReader reader = new CSVReader(new FileReader(inputCSVFileName));
    BufferedWriter output = new BufferedWriter(new FileWriter(outputCSVFileName));
    // Emit outputCSV column names.
    switch(category) {
        case AutoEncoder:
            output.write(model.getHeader());
            break;
        case Binomial:
        case Multinomial:
            output.write("predict");
            String[] responseDomainValues = model.getResponseDomainValues();
            for (String s : responseDomainValues) {
                output.write(",");
                output.write(s);
            }
            break;
        case Clustering:
            output.write("cluster");
            break;
        case Regression:
            output.write("predict");
            break;
        default:
            throw new Exception("Unknown model category " + category);
    }
    output.write("\n");
    // Loop over inputCSV one row at a time.
    //
    // TODO: performance of scoring can be considerably improved if instead of scoring each row at a time we passed
    //       all the rows to the score function, in which case it can evaluate each tree for each row, avoiding
    //       multiple rounds of fetching each tree from the filesystem.
    //
    int lineNum = 0;
    try {
        String[] inputColumnNames = null;
        String[] splitLine;
        while ((splitLine = reader.readNext()) != null) {
            lineNum++;
            // Handle the header.
            if (lineNum == 1) {
                inputColumnNames = splitLine;
                continue;
            }
            // Parse the CSV line.  Don't handle quoted commas.  This isn't a parser test.
            RowData row = formatDataRow(splitLine, inputColumnNames);
            // Emit the result to the output file.
            switch(category) {
                case AutoEncoder:
                    {
                        throw new UnsupportedOperationException();
                    // AutoEncoderModelPrediction p = model.predictAutoEncoder(row);
                    // break;
                    }
                case Binomial:
                    {
                        BinomialModelPrediction p = model.predictBinomial(row);
                        output.write(p.label);
                        output.write(",");
                        for (int i = 0; i < p.classProbabilities.length; i++) {
                            if (i > 0) {
                                output.write(",");
                            }
                            output.write(myDoubleToString(p.classProbabilities[i]));
                        }
                        break;
                    }
                case Multinomial:
                    {
                        MultinomialModelPrediction p = model.predictMultinomial(row);
                        output.write(p.label);
                        output.write(",");
                        for (int i = 0; i < p.classProbabilities.length; i++) {
                            if (i > 0) {
                                output.write(",");
                            }
                            output.write(myDoubleToString(p.classProbabilities[i]));
                        }
                        break;
                    }
                case Clustering:
                    {
                        ClusteringModelPrediction p = model.predictClustering(row);
                        output.write(myDoubleToString(p.cluster));
                        break;
                    }
                case Regression:
                    {
                        RegressionModelPrediction p = model.predictRegression(row);
                        output.write(myDoubleToString(p.value));
                        break;
                    }
                default:
                    throw new Exception("Unknown model category " + category);
            }
            output.write("\n");
        }
    } catch (Exception e) {
        System.out.println("Caught exception on line " + lineNum);
        System.out.println("");
        e.printStackTrace();
        System.exit(1);
    }
    // Clean up.
    output.close();
    reader.close();
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) FileWriter(java.io.FileWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) RowData(hex.genmodel.easy.RowData) FileReader(java.io.FileReader) ModelCategory(hex.ModelCategory)

Example 60 with FileReader

use of java.io.FileReader in project buck by facebook.

the class OptimizerOptions method loadStringsFromFile.

/**
     * Loads a list of newline-separated strings into a new HashSet and returns
     * the HashSet.
     *
     * @param filename filename to process
     * @return set of all unique lines in the file
     */
private static HashSet<String> loadStringsFromFile(String filename) {
    HashSet<String> result = new HashSet<String>();
    try {
        FileReader fr = new FileReader(filename);
        BufferedReader bfr = new BufferedReader(fr);
        String line;
        while (null != (line = bfr.readLine())) {
            result.add(line);
        }
        fr.close();
    } catch (IOException ex) {
        // Let the exception percolate up as a RuntimeException.
        throw new RuntimeException("Error with optimize list: " + filename, ex);
    }
    return result;
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) HashSet(java.util.HashSet)

Aggregations

FileReader (java.io.FileReader)1873 BufferedReader (java.io.BufferedReader)1289 IOException (java.io.IOException)893 File (java.io.File)811 FileNotFoundException (java.io.FileNotFoundException)304 ArrayList (java.util.ArrayList)274 Test (org.junit.Test)197 FileWriter (java.io.FileWriter)148 HashMap (java.util.HashMap)116 Reader (java.io.Reader)99 BufferedWriter (java.io.BufferedWriter)98 Properties (java.util.Properties)66 InputStreamReader (java.io.InputStreamReader)64 LineNumberReader (java.io.LineNumberReader)61 Matcher (java.util.regex.Matcher)61 Map (java.util.Map)59 List (java.util.List)56 PrintWriter (java.io.PrintWriter)51 StringTokenizer (java.util.StringTokenizer)51 HashSet (java.util.HashSet)50