Search in sources :

Example 1 with AstParameter

use of water.rapids.ast.AstParameter in project h2o-3 by h2oai.

the class Rapids method parseList.

/**
   * Parse and return a list of tokens: either a list of strings, or a list of numbers.
   * We do not support lists of mixed types, or lists containing variables (for now).
   */
private AstParameter parseList() {
    eatChar('[');
    char nextChar = skipWS();
    AstParameter res = isQuote(nextChar) ? parseStringList() : parseNumList();
    eatChar(']');
    return res;
}
Also used : AstParameter(water.rapids.ast.AstParameter)

Example 2 with AstParameter

use of water.rapids.ast.AstParameter in project h2o-3 by h2oai.

the class AstRectangleAssign method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame dst = stk.track(asts[1].exec(env)).getFrame();
    Val vsrc = stk.track(asts[2].exec(env));
    AstParameter col_list = (AstParameter) asts[3];
    // Column selection
    AstNumList cols_numlist = new AstNumList(col_list.columns(dst.names()));
    // Special for AstAssign: "empty" really means "all"
    if (cols_numlist.isEmpty())
        cols_numlist = new AstNumList(0, dst.numCols());
    // Allow R-like number list expansion: negative column numbers mean exclusion
    int[] cols = AstColSlice.col_select(dst.names(), cols_numlist);
    // Any COW optimized path changes Vecs in dst._vecs, and so needs a
    // defensive copy.  Any update-in-place path updates Chunks instead of
    // dst._vecs, and does not need a defensive copy.  To make life easier,
    // just make the copy now.
    dst = new Frame(dst._names, dst.vecs().clone());
    // Assign over the column slice
    if (asts[4] instanceof AstNum || asts[4] instanceof AstNumList) {
        // Explictly named row assignment
        AstNumList rows = (asts[4] instanceof AstNum) ? new AstNumList(((AstNum) asts[4]).getNum()) : ((AstNumList) asts[4]);
        // Empty rows is really: all rows
        if (rows.isEmpty())
            rows = new AstNumList(0, dst.numRows());
        switch(vsrc.type()) {
            case Val.NUM:
                assign_frame_scalar(dst, cols, rows, nanToNull(vsrc.getNum()), env._ses);
                break;
            case Val.STR:
                assign_frame_scalar(dst, cols, rows, vsrc.getStr(), env._ses);
                break;
            case Val.FRM:
                assign_frame_frame(dst, cols, rows, vsrc.getFrame(), env._ses);
                break;
            default:
                throw new IllegalArgumentException("Source must be a Frame or Number, but found a " + vsrc.getClass());
        }
    } else {
        // Boolean assignment selection?
        Frame rows = stk.track(asts[4].exec(env)).getFrame();
        switch(vsrc.type()) {
            case Val.NUM:
                assign_frame_scalar(dst, cols, rows, nanToNull(vsrc.getNum()), env._ses);
                break;
            case Val.STR:
                assign_frame_scalar(dst, cols, rows, vsrc.getStr(), env._ses);
                break;
            case Val.FRM:
                throw H2O.unimpl();
            default:
                throw new IllegalArgumentException("Source must be a Frame or Number, but found a " + vsrc.getClass());
        }
    }
    return new ValFrame(dst);
}
Also used : ValFrame(water.rapids.vals.ValFrame) AstNum(water.rapids.ast.params.AstNum) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) AstParameter(water.rapids.ast.AstParameter) AstNumList(water.rapids.ast.params.AstNumList)

Example 3 with AstParameter

use of water.rapids.ast.AstParameter in project h2o-3 by h2oai.

the class AstColPySlice method apply.

@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Val v = stk.track(asts[1].exec(env));
    AstParameter colList = (AstParameter) asts[2];
    if (v instanceof ValRow) {
        ValRow vv = (ValRow) v;
        return vv.slice(colList.columns(vv.getNames()));
    }
    Frame fr = v.getFrame();
    int[] cols = colList.columns(fr.names());
    Frame fr2 = new Frame();
    if (// Empty inclusion list?
    cols.length == 0)
        return new ValFrame(fr2);
    if (// Negative cols have number of cols added
    cols[0] < 0)
        for (int i = 0; i < cols.length; i++) cols[i] += fr.numCols();
    if (// Singletons must be in-range
    asts[2] instanceof AstNum && (cols[0] < 0 || cols[0] >= fr.numCols()))
        throw new IllegalArgumentException("Column must be an integer from 0 to " + (fr.numCols() - 1));
    for (// For all included columns
    int col : // For all included columns
    cols) if (// Ignoring out-of-range ones
    col >= 0 && col < fr.numCols())
        fr2.add(fr.names()[col], fr.vecs()[col]);
    return new ValFrame(fr2);
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) AstNum(water.rapids.ast.params.AstNum) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) ValRow(water.rapids.vals.ValRow) AstParameter(water.rapids.ast.AstParameter)

Example 4 with AstParameter

use of water.rapids.ast.AstParameter in project h2o-3 by h2oai.

the class AstSort method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame fr = stk.track(asts[1].exec(env)).getFrame();
    int[] cols = ((AstParameter) asts[2]).columns(fr.names());
    return new ValFrame(Merge.sort(fr, cols));
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) AstParameter(water.rapids.ast.AstParameter)

Example 5 with AstParameter

use of water.rapids.ast.AstParameter in project h2o-3 by h2oai.

the class AstColSlice method apply.

@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Val v = stk.track(asts[1].exec(env));
    AstParameter col_list = (AstParameter) asts[2];
    if (v instanceof ValRow) {
        ValRow vv = (ValRow) v;
        return vv.slice(col_list.columns(vv.getNames()));
    }
    Frame src = v.getFrame();
    int[] cols = col_select(src.names(), col_list);
    Frame dst = new Frame();
    Vec[] vecs = src.vecs();
    for (int col : cols) dst.add(src._names[col], vecs[col]);
    return new ValFrame(dst);
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) ValRow(water.rapids.vals.ValRow) Vec(water.fvec.Vec) AstParameter(water.rapids.ast.AstParameter)

Aggregations

AstParameter (water.rapids.ast.AstParameter)5 ValFrame (water.rapids.vals.ValFrame)4 Frame (water.fvec.Frame)3 Val (water.rapids.Val)2 AstNum (water.rapids.ast.params.AstNum)2 ValRow (water.rapids.vals.ValRow)2 Vec (water.fvec.Vec)1 AstNumList (water.rapids.ast.params.AstNumList)1