Search in sources :

Example 61 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class AstApply method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame fr = stk.track(asts[1].exec(env)).getFrame();
    double margin = stk.track(asts[2].exec(env)).getNum();
    AstPrimitive fun = stk.track(asts[3].exec(env)).getFun();
    int nargs = fun.nargs();
    if (nargs != -1 && nargs != 2)
        throw new IllegalArgumentException("Incorrect number of arguments; '" + fun + "' expects " + nargs + " but was passed " + 2);
    switch((int) margin) {
        case 1:
            return rowwise(env, fr, fun);
        case 2:
            return colwise(env, stk, fr, fun);
        default:
            throw new IllegalArgumentException("Only row-wise (margin 1) or col-wise (margin 2) allowed");
    }
}
Also used : ValFrame(water.rapids.vals.ValFrame)

Example 62 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class AstApply method rowwise.

// --------------------------------------------------------------------------
// Break each row into it's own Row, then execute the function passing the
// 1 argument.  All rows are independent, and run in parallel
private ValFrame rowwise(Env env, Frame fr, final AstPrimitive fun) {
    final String[] names = fr._names;
    // Current execution scope; needed to lookup variables
    final AstFunction scope = env._scope;
    // do a single row of the frame to determine the size of the output.
    double[] ds = new double[fr.numCols()];
    for (int col = 0; col < fr.numCols(); ++col) ds[col] = fr.vec(col).at(0);
    int noutputs = fun.apply(env, env.stk(), new AstRoot[] { fun, new AstRow(ds, fr.names()) }).getRow().length;
    Frame res = new MRTask() {

        @Override
        public void map(Chunk[] chks, NewChunk[] nc) {
            // Working row
            double[] ds = new double[chks.length];
            // Arguments to be called; they are reused endlessly
            AstRoot[] asts = new AstRoot[] { fun, new AstRow(ds, names) };
            // Session, again reused endlessly
            Session ses = new Session();
            Env env = new Env(ses);
            // For proper namespace lookup
            env._scope = scope;
            for (int row = 0; row < chks[0]._len; row++) {
                for (// Fill the row
                int col = 0; // Fill the row
                col < chks.length; // Fill the row
                col++) ds[col] = chks[col].atd(row);
                try (Env.StackHelp stk_inner = env.stk()) {
                    // Make the call per-row
                    double[] valRow = fun.apply(env, stk_inner, asts).getRow();
                    for (int newCol = 0; newCol < nc.length; ++newCol) nc[newCol].addNum(valRow[newCol]);
                }
            }
            // Mostly for the sanity checks
            ses.end(null);
        }
    }.doAll(noutputs, Vec.T_NUM, fr).outputFrame();
    return new ValFrame(res);
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) MRTask(water.MRTask) AstRoot(water.rapids.ast.AstRoot)

Example 63 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class AstAsNumeric method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame fr = stk.track(asts[1].exec(env)).getFrame();
    Vec[] nvecs = new Vec[fr.numCols()];
    Vec vv;
    for (int c = 0; c < nvecs.length; ++c) {
        vv = fr.vec(c);
        try {
            nvecs[c] = vv.toNumericVec();
        } catch (Exception e) {
            VecUtils.deleteVecs(nvecs, c);
            throw e;
        }
    }
    return new ValFrame(new Frame(fr._names, nvecs));
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) Vec(water.fvec.Vec)

Example 64 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class GroupByTest method chkTree.

private Frame chkTree(String tree, String fname, float d) {
    Frame fr = parse_test_file(Key.make("hex"), fname);
    Val val = Rapids.exec(tree);
    System.out.println(val.toString());
    if (val instanceof ValFrame)
        return val.getFrame();
    return null;
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame)

Example 65 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class GroupByTest method chkTree.

private Frame chkTree(String tree, String fname, boolean expectThrow) {
    Frame fr = parse_test_file(Key.make("hex"), fname);
    try {
        Val val = Rapids.exec(tree);
        System.out.println(val.toString());
        if (val instanceof ValFrame)
            return val.getFrame();
        throw new IllegalArgumentException("expected a frame return");
    } catch (IllegalArgumentException iae) {
        // If not expecting a throw, then throw which fails the junit
        if (!expectThrow)
            throw iae;
        // If expecting, then cleanup
        fr.delete();
        return null;
    }
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame)

Aggregations

ValFrame (water.rapids.vals.ValFrame)132 Frame (water.fvec.Frame)98 Val (water.rapids.Val)48 Vec (water.fvec.Vec)43 Test (org.junit.Test)38 MRTask (water.MRTask)32 Chunk (water.fvec.Chunk)24 NewChunk (water.fvec.NewChunk)23 BufferedString (water.parser.BufferedString)16 AstNumList (water.rapids.ast.params.AstNumList)11 AstNum (water.rapids.ast.params.AstNum)7 ValNum (water.rapids.vals.ValNum)7 AstRoot (water.rapids.ast.AstRoot)6 ValRow (water.rapids.vals.ValRow)6 ArrayList (java.util.ArrayList)5 Key (water.Key)5 AstStrList (water.rapids.ast.params.AstStrList)5 Futures (water.Futures)4 AstParameter (water.rapids.ast.AstParameter)4 Random (java.util.Random)3