Search in sources :

Example 6 with JSONObject

use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.

the class ScalingTest method generateSpecFile.

// ----------------------------
private void generateSpecFile(int cols, String specFile) throws IOException, Exception {
    final String NAME = "name";
    final String METHOD = "method";
    final String SCALE_METHOD_Z = "z-score";
    final String SCALE_METHOD_M = "mean-subtraction";
    JSONObject outputSpec = new JSONObject();
    JSONArray scaleSpec = new JSONArray();
    for (int colID = 1; colID <= cols; colID++) {
        JSONObject obj = new JSONObject();
        obj.put(NAME, "V" + colID);
        if (colID <= cols / 2)
            obj.put(METHOD, SCALE_METHOD_M);
        else
            obj.put(METHOD, SCALE_METHOD_Z);
        scaleSpec.add(obj);
    }
    outputSpec.put(TfUtils.TXMETHOD_SCALE, scaleSpec);
    FileSystem fs = IOUtilFunctions.getFileSystem(specFile);
    try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fs.create(new Path(specFile), true)))) {
        out.write(outputSpec.toString());
    }
}
Also used : Path(org.apache.hadoop.fs.Path) JSONObject(org.apache.wink.json4j.JSONObject) FileSystem(org.apache.hadoop.fs.FileSystem) JSONArray(org.apache.wink.json4j.JSONArray) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 7 with JSONObject

use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.

the class Connection method readDoubleMatrix.

// //////////////////////////////////////////
// Read matrices
// //////////////////////////////////////////
/**
 * Reads an input matrix in arbitrary format from HDFS into a dense double array.
 * NOTE: this call currently only supports default configurations for CSV.
 *
 * @param fname the filename of the input matrix
 * @return matrix as a two-dimensional double array
 * @throws IOException if IOException occurs
 */
public double[][] readDoubleMatrix(String fname) throws IOException {
    try {
        // read json meta data
        String fnamemtd = DataExpression.getMTDFileName(fname);
        JSONObject jmtd = new DataExpression().readMetadataFile(fnamemtd, false);
        // parse json meta data
        long rows = jmtd.getLong(DataExpression.READROWPARAM);
        long cols = jmtd.getLong(DataExpression.READCOLPARAM);
        int brlen = jmtd.containsKey(DataExpression.ROWBLOCKCOUNTPARAM) ? jmtd.getInt(DataExpression.ROWBLOCKCOUNTPARAM) : -1;
        int bclen = jmtd.containsKey(DataExpression.COLUMNBLOCKCOUNTPARAM) ? jmtd.getInt(DataExpression.COLUMNBLOCKCOUNTPARAM) : -1;
        long nnz = jmtd.containsKey(DataExpression.READNUMNONZEROPARAM) ? jmtd.getLong(DataExpression.READNUMNONZEROPARAM) : -1;
        String format = jmtd.getString(DataExpression.FORMAT_TYPE);
        InputInfo iinfo = InputInfo.stringExternalToInputInfo(format);
        // read matrix file
        return readDoubleMatrix(fname, iinfo, rows, cols, brlen, bclen, nnz);
    } catch (Exception ex) {
        throw new IOException(ex);
    }
}
Also used : DataExpression(org.apache.sysml.parser.DataExpression) InputInfo(org.apache.sysml.runtime.matrix.data.InputInfo) JSONObject(org.apache.wink.json4j.JSONObject) IOException(java.io.IOException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLException(org.apache.sysml.api.DMLException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) ParseException(org.apache.sysml.parser.ParseException)

Example 8 with JSONObject

use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.

the class Connection method readStringFrame.

// //////////////////////////////////////////
// Read frames
// //////////////////////////////////////////
/**
 * Reads an input frame in arbitrary format from HDFS into a dense string array.
 * NOTE: this call currently only supports default configurations for CSV.
 *
 * @param fname the filename of the input frame
 * @return frame as a two-dimensional string array
 * @throws IOException if IOException occurs
 */
public String[][] readStringFrame(String fname) throws IOException {
    try {
        // read json meta data
        String fnamemtd = DataExpression.getMTDFileName(fname);
        JSONObject jmtd = new DataExpression().readMetadataFile(fnamemtd, false);
        // parse json meta data
        long rows = jmtd.getLong(DataExpression.READROWPARAM);
        long cols = jmtd.getLong(DataExpression.READCOLPARAM);
        String format = jmtd.getString(DataExpression.FORMAT_TYPE);
        InputInfo iinfo = InputInfo.stringExternalToInputInfo(format);
        // read frame file
        return readStringFrame(fname, iinfo, rows, cols);
    } catch (Exception ex) {
        throw new IOException(ex);
    }
}
Also used : DataExpression(org.apache.sysml.parser.DataExpression) InputInfo(org.apache.sysml.runtime.matrix.data.InputInfo) JSONObject(org.apache.wink.json4j.JSONObject) IOException(java.io.IOException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLException(org.apache.sysml.api.DMLException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) ParseException(org.apache.sysml.parser.ParseException)

Example 9 with JSONObject

use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.

the class Connection method convertToMatrix.

/**
 * Converts an input stream of a string matrix in csv or textcell format
 * into a matrix block. The meta data string is the SystemML generated
 * .mtd file including the number of rows and columns.
 *
 * @param input InputStream to a string matrix in csv or textcell format
 * @param meta string representing SystemML matrix metadata in JSON format
 * @return matrix as a matrix block
 * @throws IOException if IOException occurs
 */
public MatrixBlock convertToMatrix(InputStream input, String meta) throws IOException {
    try {
        // parse json meta data
        JSONObject jmtd = new JSONObject(meta);
        int rows = jmtd.getInt(DataExpression.READROWPARAM);
        int cols = jmtd.getInt(DataExpression.READCOLPARAM);
        String format = jmtd.getString(DataExpression.FORMAT_TYPE);
        // parse the input matrix
        return convertToMatrix(input, rows, cols, format);
    } catch (Exception ex) {
        throw new IOException(ex);
    }
}
Also used : JSONObject(org.apache.wink.json4j.JSONObject) IOException(java.io.IOException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLException(org.apache.sysml.api.DMLException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) ParseException(org.apache.sysml.parser.ParseException)

Example 10 with JSONObject

use of org.apache.wink.json4j.JSONObject in project incubator-systemml by apache.

the class Connection method convertToFrame.

/**
 * Converts an input stream of a string frame in csv or textcell format
 * into a frame block. The meta data string is the SystemML generated
 * .mtd file including the number of rows and columns.
 *
 * @param input InputStream to a string frame in csv or textcell format
 * @param meta string representing SystemML frame metadata in JSON format
 * @return frame as a frame block
 * @throws IOException if IOException occurs
 */
public FrameBlock convertToFrame(InputStream input, String meta) throws IOException {
    try {
        // parse json meta data
        JSONObject jmtd = new JSONObject(meta);
        int rows = jmtd.getInt(DataExpression.READROWPARAM);
        int cols = jmtd.getInt(DataExpression.READCOLPARAM);
        String format = jmtd.getString(DataExpression.FORMAT_TYPE);
        // parse the input frame
        return convertToFrame(input, rows, cols, format);
    } catch (Exception ex) {
        throw new IOException(ex);
    }
}
Also used : JSONObject(org.apache.wink.json4j.JSONObject) IOException(java.io.IOException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLException(org.apache.sysml.api.DMLException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) ParseException(org.apache.sysml.parser.ParseException)

Aggregations

JSONObject (org.apache.wink.json4j.JSONObject)24 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)14 IOException (java.io.IOException)12 Path (org.apache.hadoop.fs.Path)8 JSONArray (org.apache.wink.json4j.JSONArray)8 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 FileSystem (org.apache.hadoop.fs.FileSystem)5 DMLException (org.apache.sysml.api.DMLException)4 DataExpression (org.apache.sysml.parser.DataExpression)4 LanguageException (org.apache.sysml.parser.LanguageException)4 ParseException (org.apache.sysml.parser.ParseException)4 BufferedWriter (java.io.BufferedWriter)3 OutputStreamWriter (java.io.OutputStreamWriter)3 ArrayList (java.util.ArrayList)3 Entry (java.util.Map.Entry)2 FrameObject (org.apache.sysml.runtime.controlprogram.caching.FrameObject)2 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)2 KahanObject (org.apache.sysml.runtime.instructions.cp.KahanObject)2 RDDObject (org.apache.sysml.runtime.instructions.spark.data.RDDObject)2