use of water.rapids.Val in project h2o-3 by h2oai.
the class AstFunction method apply.
// Apply this function: evaluate all arguments, push a lexical scope mapping
// the IDs to the ARGs, then evaluate the body. After execution pop the
// lexical scope and return the results.
@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
// Evaluation all arguments
Val[] args = new Val[asts.length];
for (int i = 1; i < asts.length; i++) args[i] = stk.track(asts[i].exec(env));
AstFunction old = env._scope;
// Push a new lexical scope, extended from the old
env._scope = new AstFunction(this, args, _parent);
Val res = stk.untrack(_body.exec(env));
// Pop the lexical scope off (by restoring the old unextended scope)
env._scope = old;
return res;
}
use of water.rapids.Val in project h2o-3 by h2oai.
the class AstCBind method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
// Compute the variable args. Find the common row count
Val[] vals = new Val[asts.length];
Vec vec = null;
for (int i = 1; i < asts.length; i++) {
vals[i] = stk.track(asts[i].exec(env));
if (vals[i].isFrame()) {
Vec anyvec = vals[i].getFrame().anyVec();
// Ignore the empty frame
if (anyvec == null)
continue;
if (vec == null)
vec = anyvec;
else if (vec.length() != anyvec.length())
throw new IllegalArgumentException("cbind frames must have all the same rows, found " + vec.length() + " and " + anyvec.length() + " rows.");
}
}
boolean clean = false;
if (vec == null) {
vec = Vec.makeZero(1);
clean = true;
}
// Default to length 1
// Populate the new Frame
Frame fr = new Frame();
for (int i = 1; i < asts.length; i++) {
switch(vals[i].type()) {
case Val.FRM:
fr.add(vals[i].getFrame().names(), fr.makeCompatible(vals[i].getFrame()));
break;
case Val.FUN:
throw H2O.unimpl();
case Val.STR:
throw H2O.unimpl();
case Val.NUM:
// Auto-expand scalars to fill every row
double d = vals[i].getNum();
fr.add(Double.toString(d), vec.makeCon(d));
break;
default:
throw H2O.unimpl();
}
}
if (clean)
vec.remove();
return new ValFrame(fr);
}
use of water.rapids.Val 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);
}
use of water.rapids.Val in project h2o-3 by h2oai.
the class AstTmpAssign method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
// Note: non-standard evaluation of the first argument! Instead of being
// executed, it is stringified. This, for example, allows us to write an
// expression as
// (tmp= newid (* frame 3))
// instead of
// (tmp= "newid" (* frame 3))
// On the other hand, this makes us unable to create dynamic identifiers
// in Rapids, for example this is invalid:
// (tmp= (+ "id" 3) (* frame 3))
// Right now there is no need for dynamically generated identifiers, since
// we don't even have proper variables or loops or control structures yet.
//
Key<Frame> id = Key.make(env.expand(asts[1].str()));
Val srcVal = stk.track(asts[2].exec(env));
Frame srcFrame = srcVal.getFrame();
Value v = DKV.get(id);
if (v != null) {
if (v.get().equals(srcFrame))
return (ValFrame) srcVal;
else
throw new IllegalArgumentException("Temp ID " + id + " already exists");
}
Frame dst = new Frame(id, srcFrame._names, srcFrame.vecs());
// Track new session-wide ID
return new ValFrame(env._ses.track_tmp(dst));
}
use of water.rapids.Val in project h2o-3 by h2oai.
the class AstIsNa method exec.
@Override
public Val exec(Val... args) {
Val val = args[1];
switch(val.type()) {
case Val.NUM:
return new ValNum(op(val.getNum()));
case Val.FRM:
Frame fr = val.getFrame();
String[] newNames = new String[fr.numCols()];
for (int i = 0; i < newNames.length; i++) {
newNames[i] = "isNA(" + fr.name(i) + ")";
}
return new ValFrame(new MRTask() {
@Override
public void map(Chunk[] cs, NewChunk[] ncs) {
for (int col = 0; col < cs.length; col++) {
Chunk c = cs[col];
NewChunk nc = ncs[col];
for (int i = 0; i < c._len; i++) nc.addNum(c.isNA(i) ? 1 : 0);
}
}
}.doAll(fr.numCols(), Vec.T_NUM, fr).outputFrame(newNames, null));
case Val.STR:
return new ValNum(val.getStr() == null ? 1 : 0);
default:
throw H2O.unimpl("is.na unimpl: " + val.getClass());
}
}
Aggregations