Search in sources :

Example 1 with DataExpression

use of org.apache.sysml.parser.DataExpression 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 2 with DataExpression

use of org.apache.sysml.parser.DataExpression 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 3 with DataExpression

use of org.apache.sysml.parser.DataExpression in project incubator-systemml by apache.

the class CommonSyntacticValidator method setOutputStatement.

protected void setOutputStatement(ParserRuleContext ctx, ArrayList<ParameterExpression> paramExpression, StatementInfo info) {
    if (paramExpression.size() < 2) {
        notifyErrorListeners("incorrect usage of write function (at least 2 arguments required)", ctx.start);
        return;
    }
    if (paramExpression.get(0).getExpr() instanceof DataIdentifier) {
        HashMap<String, Expression> varParams = new HashMap<>();
        varParams.put(DataExpression.IO_FILENAME, paramExpression.get(1).getExpr());
        for (int i = 2; i < paramExpression.size(); i++) {
            // DataExpression.FORMAT_TYPE, DataExpression.DELIM_DELIMITER, DataExpression.DELIM_HAS_HEADER_ROW,  DataExpression.DELIM_SPARSE
            varParams.put(paramExpression.get(i).getName(), paramExpression.get(i).getExpr());
        }
        DataExpression dataExpression = new DataExpression(ctx, DataOp.WRITE, varParams, currentFile);
        info.stmt = new OutputStatement(ctx, (DataIdentifier) paramExpression.get(0).getExpr(), DataOp.WRITE, currentFile);
        ((OutputStatement) info.stmt).setExprParams(dataExpression);
    } else {
        notifyErrorListeners("incorrect usage of write function", ctx.start);
    }
}
Also used : DataExpression(org.apache.sysml.parser.DataExpression) DataIdentifier(org.apache.sysml.parser.DataIdentifier) RelationalExpression(org.apache.sysml.parser.RelationalExpression) BooleanExpression(org.apache.sysml.parser.BooleanExpression) ParameterizedBuiltinFunctionExpression(org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) DataExpression(org.apache.sysml.parser.DataExpression) HashMap(java.util.HashMap) OutputStatement(org.apache.sysml.parser.OutputStatement)

Example 4 with DataExpression

use of org.apache.sysml.parser.DataExpression in project incubator-systemml by apache.

the class CommonSyntacticValidator method buildForBuiltInFunction.

/**
 * Creates a builtin function expression.
 *
 * @param ctx antlr rule context
 * @param functionName Name of the builtin function
 * @param paramExpressions Array of parameter names and values
 * @return expression if found otherwise null
 */
protected Expression buildForBuiltInFunction(ParserRuleContext ctx, String functionName, ArrayList<ParameterExpression> paramExpressions) {
    // Double verification: verify passed function name is a (non-parameterized) built-in function.
    try {
        if (functions.contains(functionName)) {
            // It is a user function definition (which takes precedence if name same as built-in)
            return null;
        }
        Expression lsf = handleLanguageSpecificFunction(ctx, functionName, paramExpressions);
        if (lsf != null) {
            setFileLineColumn(lsf, ctx);
            return lsf;
        }
        BuiltinFunctionExpression bife = BuiltinFunctionExpression.getBuiltinFunctionExpression(ctx, functionName, paramExpressions, currentFile);
        if (bife != null) {
            // It is a builtin function
            return bife;
        }
        ParameterizedBuiltinFunctionExpression pbife = ParameterizedBuiltinFunctionExpression.getParamBuiltinFunctionExpression(ctx, functionName, paramExpressions, currentFile);
        if (pbife != null) {
            // It is a parameterized builtin function
            return pbife;
        }
        // built-in read, rand ...
        DataExpression dbife = DataExpression.getDataExpression(ctx, functionName, paramExpressions, currentFile, errorListener);
        if (dbife != null) {
            return dbife;
        }
    } catch (Exception e) {
        notifyErrorListeners("unable to process builtin function expression " + functionName + ":" + e.getMessage(), ctx.start);
    }
    return null;
}
Also used : DataExpression(org.apache.sysml.parser.DataExpression) RelationalExpression(org.apache.sysml.parser.RelationalExpression) BooleanExpression(org.apache.sysml.parser.BooleanExpression) ParameterizedBuiltinFunctionExpression(org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) DataExpression(org.apache.sysml.parser.DataExpression) ParameterizedBuiltinFunctionExpression(org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) ParameterizedBuiltinFunctionExpression(org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression) LanguageException(org.apache.sysml.parser.LanguageException)

Example 5 with DataExpression

use of org.apache.sysml.parser.DataExpression in project incubator-systemml by apache.

the class AutomatedTestBase method readDMLMetaDataFile.

public static MatrixCharacteristics readDMLMetaDataFile(String fileName) {
    try {
        String fname = baseDirectory + OUTPUT_DIR + fileName + ".mtd";
        JSONObject meta = new DataExpression().readMetadataFile(fname, false);
        long rlen = Long.parseLong(meta.get(DataExpression.READROWPARAM).toString());
        long clen = Long.parseLong(meta.get(DataExpression.READCOLPARAM).toString());
        return new MatrixCharacteristics(rlen, clen, -1, -1);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : DataExpression(org.apache.sysml.parser.DataExpression) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) JSONObject(org.apache.wink.json4j.JSONObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) IOException(java.io.IOException) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics)

Aggregations

DataExpression (org.apache.sysml.parser.DataExpression)7 IOException (java.io.IOException)4 LanguageException (org.apache.sysml.parser.LanguageException)4 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)4 JSONObject (org.apache.wink.json4j.JSONObject)4 BinaryExpression (org.apache.sysml.parser.BinaryExpression)3 BooleanExpression (org.apache.sysml.parser.BooleanExpression)3 BuiltinFunctionExpression (org.apache.sysml.parser.BuiltinFunctionExpression)3 Expression (org.apache.sysml.parser.Expression)3 ParameterExpression (org.apache.sysml.parser.ParameterExpression)3 ParameterizedBuiltinFunctionExpression (org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression)3 RelationalExpression (org.apache.sysml.parser.RelationalExpression)3 DMLException (org.apache.sysml.api.DMLException)2 ParseException (org.apache.sysml.parser.ParseException)2 InputInfo (org.apache.sysml.runtime.matrix.data.InputInfo)2 HashMap (java.util.HashMap)1 DataIdentifier (org.apache.sysml.parser.DataIdentifier)1 OutputStatement (org.apache.sysml.parser.OutputStatement)1 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)1