use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.
the class AstBinOp method frame_op_scalar.
/**
* Auto-widen the scalar to every element of the frame
*/
private ValFrame frame_op_scalar(Frame fr, final String str) {
Frame res = new MRTask() {
@Override
public void map(Chunk[] chks, NewChunk[] cress) {
BufferedString vstr = new BufferedString();
for (int c = 0; c < chks.length; c++) {
Chunk chk = chks[c];
NewChunk cres = cress[c];
Vec vec = chk.vec();
// String Vectors: apply str_op as BufferedStrings to all elements
if (vec.isString()) {
final BufferedString conStr = new BufferedString(str);
for (int i = 0; i < chk._len; i++) cres.addNum(str_op(chk.atStr(vstr, i), conStr));
} else if (vec.isCategorical()) {
// categorical Vectors: convert string to domain value; apply op (not
// str_op). Not sure what the "right" behavior here is, can
// easily argue that should instead apply str_op to the categorical
// string domain value - except that this whole operation only
// makes sense for EQ/NE, and is much faster when just comparing
// doubles vs comparing strings. Note that if the string is not
// part of the categorical domain, the find op returns -1 which is never
// equal to any categorical dense integer (which are always 0+).
final double d = (double) ArrayUtils.find(vec.domain(), str);
for (int i = 0; i < chk._len; i++) cres.addNum(op(chk.atd(i), d));
} else {
// mixing string and numeric
// false or true only
final double d = op(1, 2);
for (int i = 0; i < chk._len; i++) cres.addNum(d);
}
}
}
}.doAll(fr.numCols(), Vec.T_NUM, fr).outputFrame(fr._names, null);
return new ValFrame(res);
}
use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.
the class AstColSlice method apply.
@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Val v = stk.track(asts[1].exec(env));
AstParameter col_list = (AstParameter) asts[2];
if (v instanceof ValRow) {
ValRow vv = (ValRow) v;
return vv.slice(col_list.columns(vv.getNames()));
}
Frame src = v.getFrame();
int[] cols = col_select(src.names(), col_list);
Frame dst = new Frame();
Vec[] vecs = src.vecs();
for (int col : cols) dst.add(src._names[col], vecs[col]);
return new ValFrame(dst);
}
use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.
the class AstRStrip method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
String set = asts[2].exec(env).getStr();
// Type check
for (Vec v : fr.vecs()) if (!(v.isCategorical() || v.isString()))
throw new IllegalArgumentException("trim() requires a string or categorical column. " + "Received " + fr.anyVec().get_type_str() + ". Please convert column to a string or categorical first.");
// Transform each vec
Vec[] nvs = new Vec[fr.numCols()];
int i = 0;
for (Vec v : fr.vecs()) {
if (v.isCategorical())
nvs[i] = rstripCategoricalCol(v, set);
else
nvs[i] = rstripStringCol(v, set);
i++;
}
return new ValFrame(new Frame(nvs));
}
use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.
the class AstStrLength method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
// Type check
for (Vec v : fr.vecs()) if (!(v.isCategorical() || v.isString()))
throw new IllegalArgumentException("length() requires a string or categorical column. " + "Received " + fr.anyVec().get_type_str() + ". Please convert column to a string or categorical first.");
// Transform each vec
Vec[] nvs = new Vec[fr.numCols()];
int i = 0;
for (Vec v : fr.vecs()) {
if (v.isCategorical())
nvs[i] = lengthCategoricalCol(v);
else
nvs[i] = lengthStringCol(v);
i++;
}
return new ValFrame(new Frame(nvs));
}
use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.
the class AstStrSplit method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
String splitRegEx = asts[2].exec(env).getStr();
// Type check
for (Vec v : fr.vecs()) if (!(v.isCategorical() || v.isString()))
throw new IllegalArgumentException("strsplit() requires a string or categorical column. " + "Received " + fr.anyVec().get_type_str() + ". Please convert column to a string or categorical first.");
// Transform each vec
ArrayList<Vec> vs = new ArrayList<>(fr.numCols());
for (Vec v : fr.vecs()) {
Vec[] splits;
if (v.isCategorical()) {
splits = strSplitCategoricalCol(v, splitRegEx);
for (Vec split : splits) vs.add(split);
} else {
splits = strSplitStringCol(v, splitRegEx);
for (Vec split : splits) vs.add(split);
}
}
return new ValFrame(new Frame(vs.toArray(new Vec[vs.size()])));
}
Aggregations