Search in sources :

Example 1 with AstRoot

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

the class AstCumu method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame f = stk.track(asts[1].exec(env)).getFrame();
    AstRoot axisAR = asts[2];
    for (Vec v : f.vecs()) {
        if (v.isCategorical() || v.isString() || v.isUUID())
            throw new IllegalArgumentException("Cumulative functions not applicable to enum, string, or UUID values");
    }
    double axis = axisAR.exec(env).getNum();
    if (axis != 1.0 && axis != 0.0)
        throw new IllegalArgumentException("Axis must be 0 or 1");
    if (f.numCols() == 1) {
        if (axis == 0.0) {
            AstCumu.CumuTask t = new AstCumu.CumuTask(f.anyVec().nChunks(), init());
            t.doAll(new byte[] { Vec.T_NUM }, f.anyVec());
            final double[] chkCumu = t._chkCumu;
            Vec cumuVec = t.outputFrame().anyVec();
            new MRTask() {

                @Override
                public void map(Chunk c) {
                    if (c.cidx() != 0) {
                        double d = chkCumu[c.cidx() - 1];
                        for (int i = 0; i < c._len; ++i) c.set(i, op(c.atd(i), d));
                    }
                }
            }.doAll(cumuVec);
            Key<Frame> k = Key.make();
            return new ValFrame(new Frame(k, null, new Vec[] { cumuVec }));
        } else {
            return new ValFrame(new Frame(f));
        }
    } else {
        if (axis == 0.0) {
            // down the column implementation
            AstCumu.CumuTaskWholeFrame t = new AstCumu.CumuTaskWholeFrame(f.anyVec().nChunks(), init(), f.numCols());
            Frame fr2 = t.doAll(f.numCols(), Vec.T_NUM, f).outputFrame(null, f.names(), null);
            final double[][] chkCumu = t._chkCumu;
            new MRTask() {

                @Override
                public void map(Chunk[] cs) {
                    if (cs[0].cidx() != 0) {
                        for (int i = 0; i < cs.length; i++) {
                            double d = chkCumu[i][cs[i].cidx() - 1];
                            for (int j = 0; j < cs[i]._len; ++j) cs[i].set(j, op(cs[i].atd(j), d));
                        }
                    }
                }
            }.doAll(fr2);
            return new ValFrame(new Frame(fr2));
        } else {
            AstCumu.CumuTaskAxis1 t = new AstCumu.CumuTaskAxis1(init());
            Frame fr2 = t.doAll(f.numCols(), Vec.T_NUM, f).outputFrame(null, f.names(), null);
            return new ValFrame(new Frame(fr2));
        }
    }
}
Also used : ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) Chunk(water.fvec.Chunk) NewChunk(water.fvec.NewChunk) ValFrame(water.rapids.vals.ValFrame) Vec(water.fvec.Vec) MRTask(water.MRTask) AstRoot(water.rapids.ast.AstRoot)

Example 2 with AstRoot

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

the class RapidsTest method astStr_ok.

private static void astStr_ok(String expr, String expected) {
    AstRoot res = Rapids.parse(expr);
    assertTrue(res instanceof AstStr);
    assertEquals(expected, ((AstStr) res).getStr());
}
Also used : AstStr(water.rapids.ast.params.AstStr) AstRoot(water.rapids.ast.AstRoot)

Example 3 with AstRoot

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

the class RapidsTest method astNumList_ok.

private static void astNumList_ok(String expr, double[] expected) {
    AstRoot res = Rapids.parse(expr);
    assertTrue(res instanceof AstNumList);
    if (expected != null)
        assertArrayEquals(expected, ((AstNumList) res).expand(), 1e-10);
}
Also used : AstRoot(water.rapids.ast.AstRoot) AstNumList(water.rapids.ast.params.AstNumList)

Example 4 with AstRoot

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

the class Rapids method parse.

/**
   * Parse a Rapids expression string into an Abstract Syntax Tree object.
   * @param rapids expression to parse
   */
public static AstRoot parse(String rapids) {
    Rapids r = new Rapids(rapids);
    AstRoot res = r.parseNext();
    if (r.skipWS() != ' ')
        throw new IllegalASTException("Syntax error: illegal Rapids expression `" + rapids + "`");
    return res;
}
Also used : AstRoot(water.rapids.ast.AstRoot)

Example 5 with AstRoot

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

the class Rapids method exec.

/**
   * Execute a single rapids call in a short-lived session
   * @param rapids expression to parse
   */
public static Val exec(String rapids) {
    Session session = new Session();
    try {
        AstRoot ast = Rapids.parse(rapids);
        Val val = session.exec(ast, null);
        // Frame is independent of the Session (which is disappearing).
        return session.end(val);
    } catch (Throwable ex) {
        throw session.endQuietly(ex);
    }
}
Also used : AstRoot(water.rapids.ast.AstRoot)

Aggregations

AstRoot (water.rapids.ast.AstRoot)14 ValFrame (water.rapids.vals.ValFrame)6 Frame (water.fvec.Frame)5 ArrayList (java.util.ArrayList)4 MRTask (water.MRTask)4 Vec (water.fvec.Vec)4 Chunk (water.fvec.Chunk)3 AstNumList (water.rapids.ast.params.AstNumList)3 AstStr (water.rapids.ast.params.AstStr)3 NewChunk (water.fvec.NewChunk)2 AstNum (water.rapids.ast.params.AstNum)2 QuantileModel (hex.quantile.QuantileModel)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 RapidsExpressionV3 (water.api.schemas3.RapidsHelpV3.RapidsExpressionV3)1 AstExec (water.rapids.ast.AstExec)1 AstFrame (water.rapids.ast.AstFrame)1 AstFunction (water.rapids.ast.AstFunction)1 AstStrList (water.rapids.ast.params.AstStrList)1 AstGroup (water.rapids.ast.prims.mungers.AstGroup)1