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));
}
}
}
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());
}
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);
}
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;
}
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);
}
}
Aggregations