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